Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RicardoSilvestr committed Oct 13, 2023
2 parents b7177e3 + 9167b72 commit 72471e2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/mfs-deconvolution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"homepage": "https://github.com/cheminfo/mass-tools/tree/main/packages/emdb#readme",
"dependencies": {
"@lukeed/uuid": "^2.0.1",
"cheminfo-types": "^1.7.2",
"isotopic-distribution": "^3.0.1",
"mf-generator": "^3.0.2",
Expand Down
32 changes: 26 additions & 6 deletions packages/mfs-deconvolution/src/__tests__/mfsDeconvolution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,17 @@ describe('mfsDeconvolution', () => {
const logger = new FifoLogger();
const text = readFileSync(join(__dirname, './data/isotopic.txt'));
const spectrum = new Spectrum(parseXY(text));
const { mfs, reconstructed, matchingScore, difference } =
await mfsDeconvolution(spectrum, ['HValOH', '([13C]C-1)0-10,Br'], {
ionizations: 'H+',
logger,
peakWidthFct: (em) => em / 1000,
});
const {
mfs,
reconstructed,
matchingScore,
difference,
getFilteredReconstructed,
} = await mfsDeconvolution(spectrum, ['HValOH', '([13C]C-1)0-10,Br'], {
ionizations: 'H+',
logger,
peakWidthFct: (em) => em / 1000,
});
expect(matchingScore).toBeCloseTo(1);
const absoluteQuantities = mfs.map((mf) => mf.absoluteQuantity);
expect(absoluteQuantities).toBeDeepCloseTo([100, 50, 20, 10, 0, 0, 0], 3);
Expand All @@ -142,5 +147,20 @@ describe('mfsDeconvolution', () => {
expect(difference.x).toHaveLength(19);
expect(difference.y).toHaveLength(19);
expect(logger.getLogs()).toHaveLength(5);

const filteredReconstructed = getFilteredReconstructed();
expect(filteredReconstructed).toStrictEqual(reconstructed);

const reallyFilteredReconstructed = getFilteredReconstructed(
mfs.slice(2, 4).map((mf) => mf.id),
);
expect(reallyFilteredReconstructed.x).toStrictEqual(
filteredReconstructed.x,
);
expect(reallyFilteredReconstructed.y).toBeDeepCloseTo([
0, 0, 19.174220339122392, 10.423872010621597, 0.35511193913682987,
0.04532585451294238, 0.0012090061609091609, 0.00005384872306899919,
0.0000010912189424224057, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
});
});
19 changes: 18 additions & 1 deletion packages/mfs-deconvolution/src/mfsDeconvolution.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 } from '@lukeed/uuid';
import { IsotopicDistribution } from 'isotopic-distribution';
import { generateMFs } from 'mf-generator';
import { fcnnlsVector } from 'ml-fcnnls';
Expand Down Expand Up @@ -48,8 +49,10 @@ export async function mfsDeconvolution(spectrum, ranges, options = {}) {

const peakWidthFct = getPeakWidthFct(options);
const centroids = getCentroids(spectrum, { threshold });

let mfs = await generateMFs(ranges, { filter, ionizations });
mfs.forEach((mf) => {
mf.id = v4();
});
mfs = addIsotopicDistributionAndCheckMF(mfs, { logger, peakWidthFct });

mfs.sort((mf1, mf2) => mf1.ms.em - mf2.ms.em);
Expand Down Expand Up @@ -116,7 +119,21 @@ export async function mfsDeconvolution(spectrum, ranges, options = {}) {
},
mfs,
matchingScore,
getFilteredReconstructed,
};

function getFilteredReconstructed(ids = mfs.map((mf) => mf.id)) {
const filteredW = W.clone();
for (let i = 0; i < mfs.length; i++) {
if (ids.includes(mfs[i].id)) continue;
filteredW.set(0, i, 0);
}
const filteredReconstructed = filteredW.mmul(A).getRow(0);
return {
x: combined.x,
y: filteredReconstructed,
};
}
}

/**
Expand Down

0 comments on commit 72471e2

Please sign in to comment.