Skip to content

Commit

Permalink
feat: peak picking returns id and keep id if it exists (#108)
Browse files Browse the repository at this point in the history
* chore: initial steps to keep id property

* feat(broadenPeaks): conditional type

* chore(joinBroadPeaks): conditional type

* feat(setShape): generic function n output option
pure function now

* chore(gsd): peaks with id

* chore: refactor broadenPeaks

* chore(joinBroadPeaks): refactor

* chore(joinBroadPeaks): remove conditional type

* chore(optimizePeaks): conditional types

* fix(addMissingShape): convert to pure

* fix(optimizePeaks): convert to generic n conditional types

* feat: addMissingIDs
  • Loading branch information
jobo322 authored Aug 27, 2022
1 parent 928ea11 commit f22f5a5
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 101 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@
"xy-parser": "^5.0.2"
},
"dependencies": {
"@lukeed/uuid": "^2.0.0",
"cheminfo-types": "^1.1.0",
"ml-peak-shape-generator": "^4.1.1",
"ml-savitzky-golay-generalized": "^4.0.1",
"ml-spectra-fitting": "^4.1.0",
"ml-spectra-fitting": "^4.1.1",
"ml-spectra-processing": "^11.6.0"
}
}
4 changes: 4 additions & 0 deletions src/GSDBroadenPeak.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Shape1D } from 'ml-peak-shape-generator';

export interface GSDBroadenPeak {
id?: string;
x: number;
y: number;
width: number;
shape?: Shape1D;
index: number;
from: { x: number };
to: { x: number };
Expand Down
1 change: 1 addition & 0 deletions src/GSDPeak.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Shape1D } from 'ml-peak-shape-generator';

export interface GSDPeak {
id?: string;
x: number;
y: number;
/**
Expand Down
1 change: 1 addition & 0 deletions src/GSDPeakOptimized.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Shape1D } from 'ml-peak-shape-generator';

export interface GSDPeakOptimized {
id?: string;
x: number;
y: number;
width: number;
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/gaussian-smooth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('gaussian simulated peaks', () => {
});
expect(peakList).toBeDeepCloseTo([
{
id: peakList[0].id,
x: -0.5,
y: 0.6945098953985852,
ddY: -259.83290100626783,
Expand All @@ -48,6 +49,7 @@ describe('gaussian simulated peaks', () => {
},
},
{
id: peakList[1].id,
x: 0.5,
y: 0.6945098953985852,
ddY: -259.83290100626783,
Expand Down
15 changes: 11 additions & 4 deletions src/__tests__/gaussian.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('smooth:false option', () => {
},
},
];
expect(peakList).toBeDeepCloseTo(expected);
expect(peakList).toBeDeepCloseTo(addMissingID(peakList, expected));
});

it('negative maxima peaks', () => {
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('smooth:false option', () => {
},
},
];
expect(peakList).toBeDeepCloseTo(expected);
expect(peakList).toBeDeepCloseTo(addMissingID(peakList, expected));
});

it('Negative peaks', () => {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('smooth:false option', () => {
},
];

expect(peakList).toBeDeepCloseTo(expected);
expect(peakList).toBeDeepCloseTo(addMissingID(peakList, expected));
});

it('minima peaks', () => {
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('smooth:false option', () => {
},
];

expect(peakList).toBeDeepCloseTo(expected);
expect(peakList).toBeDeepCloseTo(addMissingID(peakList, expected));
});

it('negative peaks with maxCriteria true', () => {
Expand All @@ -196,3 +196,10 @@ describe('smooth:false option', () => {
expect(peakList).toHaveLength(0);
});
});

function addMissingID(peaks, expected) {
for (let i = 0; i < expected.length; i++) {
expected[i].id = peaks[i].id;
}
return expected;
}
4 changes: 4 additions & 0 deletions src/__tests__/power.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('power', () => {
let peakList = gsd(data);
let expected = [
{
id: peakList[0].id,
x: 5,
y: 10000,
width: 0.3,
Expand All @@ -77,6 +78,7 @@ describe('power', () => {
// This shape is anyway quite strange
const expected = [
{
id: peakList[0].id,
x: 4.45,
y: 7921,
width: 0.05,
Expand All @@ -92,6 +94,7 @@ describe('power', () => {
},
},
{
id: peakList[1].id,
x: 5,
y: 10000,
width: 0.2,
Expand All @@ -107,6 +110,7 @@ describe('power', () => {
},
},
{
id: peakList[2].id,
x: 5.55,
y: 7921,
width: 0.05,
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/ubiquitin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ describe('Global spectra deconvolution ubiquitin', () => {
realTopDetection: true,
sgOptions: { windowSize: 7, polynomial: 3 },
});

expect(peaks[0]).toBeDeepCloseTo({
id: peaks[0].id,
x: 200.05527917306466,
y: 28.795378784444413,
ddY: -15468134.039875854,
Expand Down
15 changes: 10 additions & 5 deletions src/gsd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as generateID } from '@lukeed/uuid';
import type { DataXY } from 'cheminfo-types';
import { Shape1D } from 'ml-peak-shape-generator';
import { sgg, SGGOptions } from 'ml-savitzky-golay-generalized';
Expand All @@ -10,6 +11,8 @@ import {
} from 'ml-spectra-processing';

import { GSDPeak } from './GSDPeak';
import { MakeMandatory } from './utils/MakeMandatory';
import { MakeOptional } from './utils/MakeOptional';
import { optimizeTop } from './utils/optimizeTop';
import { setShape } from './utils/setShape';

Expand Down Expand Up @@ -52,7 +55,8 @@ export interface GSDOptions {
*/
shape?: Shape1D;
}

export type GSDPeakID = MakeMandatory<GSDPeak, 'id'>;
export type GSDPeakIDOptionalShape = MakeOptional<GSDPeak, 'shape'>;
/**
* Global spectra deconvolution
* @param data - Object data with x and y arrays. Values in x has to be growing
Expand All @@ -61,7 +65,7 @@ export interface GSDOptions {
*/

export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeakID[] {
let {
sgOptions = {
windowSize: 9,
Expand Down Expand Up @@ -217,7 +221,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {

let lastK = -1;

const peaks: GSDPeak[] = [];
const peaks: GSDPeakIDOptionalShape[] = [];
for (const minddYIndex of minddY) {
let deltaX = x[minddYIndex];
let possible = -1;
Expand Down Expand Up @@ -245,6 +249,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
if (yData[minddYIndex] > yThreshold) {
let width = Math.abs(intervalR[possible].x - intervalL[possible].x);
peaks.push({
id: generateID(),
x: deltaX,
y: yData[minddYIndex],
width,
Expand All @@ -254,7 +259,7 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
from: intervalL[possible],
to: intervalR[possible],
},
} as GSDPeak);
});
}
}
}
Expand All @@ -274,5 +279,5 @@ export function gsd(data: DataXY, options: GSDOptions = {}): GSDPeak[] {
return a.x - b.x;
});

return setShape(peaks, { shape });
return setShape(peaks, { shape }) as GSDPeakID[];
}
59 changes: 33 additions & 26 deletions src/post/__tests__/broadenPeaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ describe('broadenPeaks', () => {
let result = broadenPeaks(
[
{
id: '1',
x: -0.5,
y: 1,
ddY: 0,
Expand Down Expand Up @@ -327,6 +328,7 @@ describe('broadenPeaks', () => {
ddY: 0,
width: 0.08,
index: 575,
shape: { kind: 'gaussian' },
inflectionPoints: {
from: { index: 573, x: 10.46 },
to: { index: 577, x: 10.54 },
Expand All @@ -335,31 +337,36 @@ describe('broadenPeaks', () => {
],
{ factor: 20 },
);
expect(result).toBeDeepCloseTo([
{
x: -0.5,
y: 1,
index: 25,
width: 1.3,
from: { x: -1.3 },
to: { x: 0 },
},
{
x: 0.5,
y: 1,
index: 75,
width: 1.3,
from: { x: 0 },
to: { x: 1.3 },
},
{
x: 10.5,
y: 1,
index: 575,
width: 1.6,
from: { x: 9.7 },
to: { x: 11.3 },
},
]);
expect(result).toBeDeepCloseTo(
[
{
id: '1',
x: -0.5,
y: 1,
index: 25,
width: 1.3,
from: { x: -1.3 },
to: { x: 0 },
},
{
x: 0.5,
y: 1,
index: 75,
width: 1.3,
from: { x: 0 },
to: { x: 1.3 },
},
{
x: 10.5,
y: 1,
index: 575,
shape: { kind: 'gaussian' },
width: 1.6,
from: { x: 9.7 },
to: { x: 11.3 },
},
],
1,
);
});
});
1 change: 0 additions & 1 deletion src/post/__tests__/joinBroadPeaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('joinBroadPeaks', () => {
broadRatio: 0.03,
broadWidth: 0.25,
});

expect(peaks.length).toBeGreaterThan(3);
expect(newPeaks).toHaveLength(3);
});
Expand Down
Loading

0 comments on commit f22f5a5

Please sign in to comment.