Skip to content

Commit

Permalink
feat(ms-spectrum): export fromMonoisotopicMass (#260)
Browse files Browse the repository at this point in the history
* feat(ms-spectrum): export `fromMonoisotopicMass`

moved from `emdb` package to remove circular dependency between `emdb` and `ms-spectrum`

it was internal, method was accessible through `EMDB.prototype.fromMonoisotopicMass`

closes: #70

* test: add `numberMFs` test case for coverage
  • Loading branch information
tpoisseau authored Dec 22, 2024
1 parent 629973d commit dd8d2a4
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 15 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import globals from 'globals';
export default [
{
ignores: [
'packages/*/coverage',
'packages/*/dist',
'packages/*/docs',
'packages/*/examples',
Expand Down
3 changes: 1 addition & 2 deletions packages/emdb/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Spectrum } from 'ms-spectrum';
import { Spectrum, fromMonoisotopicMass } from 'ms-spectrum';

import { appendFragmentsInfo } from './append/appendFragmentsInfo.js';
import { fromArray } from './from/fromArray.js';
import { fromMolecules } from './from/fromMolecules.js';
import { fromMonoisotopicMass } from './from/fromMonoisotopicMass.js';
import { fromNucleicSequence } from './from/fromNucleicSequence.js';
import { fromPeptidicSequence } from './from/fromPeptidicSequence.js';
import { fromRange } from './from/fromRange.js';
Expand Down
2 changes: 1 addition & 1 deletion packages/ms-spectrum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"homepage": "https://github.com/cheminfo/mass-tools/tree/main/packages/emdb#readme",
"dependencies": {
"cheminfo-types": "^1.8.1",
"emdb": "^3.3.17",
"is-any-array": "^2.0.1",
"mf-finder": "^3.4.11",
"mf-parser": "^3.3.0",
"mf-utilities": "^3.3.4",
"ml-gsd": "^12.1.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect, test } from 'vitest';

import { fromMonoisotopicMass } from '../..';

test('fromMonoisotopicMass', async () => {
const { mfs } = await fromMonoisotopicMass(120, {
allowNeutral: true,
});
expect(mfs).toHaveLength(8);
});

test('fromMonoisotopicMass string', async () => {
const { mfs } = await fromMonoisotopicMass('120,60', {
allowNeutral: true,
});
expect(mfs).toHaveLength(10);
});

test('fromMonoisotopicMass array', async () => {
const { mfs } = await fromMonoisotopicMass([60, 120], {
allowNeutral: true,
});
expect(mfs).toHaveLength(10);
});

test('fromMonoisotopicMass with ionizations', async () => {
const { mfs } = await fromMonoisotopicMass(120, {
allowNeutral: false,
ionizations: ', H+, K+',
precision: 100,
});
expect(mfs).toHaveLength(9);
});

test('fromMonoisotopicMass large database', async () => {
const { mfs } = await fromMonoisotopicMass(1000, {
ranges: 'C0-100 H0-100 N0-100 O0-100',
filter: {
unsaturation: {
min: 0,
max: 100,
onlyInteger: true,
},
},
precision: 100,
allowNeutral: true,
limit: 10000,
});
expect(mfs).toHaveLength(1407);
});
6 changes: 2 additions & 4 deletions packages/ms-spectrum/src/getFragmentPeaks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EMDB } from 'emdb';
import { MF } from 'mf-parser';

import { fromMonoisotopicMass } from './from/fromMonoisotopicMass';
import { getPeaks } from './getPeaks.js';

/**
Expand All @@ -18,8 +18,6 @@ import { getPeaks } from './getPeaks.js';
*/

export async function getFragmentPeaks(peaks, mf, options = {}) {
const emdb = new EMDB();

const { ionizations = '', precision } = options;

const mfInfo = new MF(mf).getInfo();
Expand All @@ -28,7 +26,7 @@ export async function getFragmentPeaks(peaks, mf, options = {}) {
.join(' ');
peaks = getPeaks(peaks, options);
for (let peak of peaks) {
const { mfs } = await emdb.fromMonoisotopicMass(peak.x, {
const { mfs } = await fromMonoisotopicMass(peak.x, {
precision,
ranges,
ionizations,
Expand Down
1 change: 1 addition & 0 deletions packages/ms-spectrum/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './getPeaks.js';
export * from './getBestPeaks.js';
export * from './MSComparator.js';
export * from './jsgraph/index.js';
export * from './from/fromMonoisotopicMass.js';
Original file line number Diff line number Diff line change
@@ -1,5 +1,132 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`test getPeaksAnnotation > cap numberMFs to 2 1`] = `
[
{
"_highlight": undefined,
"labels": [
{
"color": "red",
"position": {
"dx": "2px",
"dy": "-17px",
"x": 12,
"y": 1,
},
"text": "12.00000",
},
],
"position": [
{
"dy": "-5px",
"x": 12,
"y": 1,
},
{
"dy": "-25px",
"x": 12,
"y": 1,
},
],
"type": "line",
},
{
"_highlight": undefined,
"labels": [
{
"color": "red",
"position": {
"dx": "2px",
"dy": "-17px",
"x": 24,
"y": 2,
},
"text": "24.00000",
},
],
"position": [
{
"dy": "-5px",
"x": 24,
"y": 2,
},
{
"dy": "-25px",
"x": 24,
"y": 2,
},
],
"type": "line",
},
{
"_highlight": undefined,
"labels": [
{
"color": "red",
"position": {
"dx": "2px",
"dy": "-17px",
"x": 36,
"y": 3,
},
"text": "36.00000",
},
],
"position": [
{
"dy": "-5px",
"x": 36,
"y": 3,
},
{
"dy": "-25px",
"x": 36,
"y": 3,
},
],
"type": "line",
},
{
"_highlight": undefined,
"labels": [
{
"color": "red",
"position": {
"dx": "2px",
"dy": "-17px",
"x": 42,
"y": 4,
},
"text": "42.00000",
},
{
"color": "grey",
"position": {
"dx": "2px",
"dy": "-4px",
"x": 42,
"y": 4,
},
"text": "Z:1",
},
],
"position": [
{
"dy": "-5px",
"x": 42,
"y": 4,
},
{
"dy": "-25px",
"x": 42,
"y": 4,
},
],
"type": "line",
},
]
`;

exports[`test getPeaksAnnotation > default options 1`] = `
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ describe('test getPeaksAnnotation', () => {
});
expect(result).toMatchSnapshot();
});

it('cap numberMFs to 2', async () => {
let result = await getPeaksAnnotation(peaks, {
numberMFs: 2,
});
expect(result).toMatchSnapshot();
});
});
17 changes: 9 additions & 8 deletions packages/ms-spectrum/src/jsgraph/getPeaksAnnotation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EMDB } from 'emdb';
import { fromMonoisotopicMass } from '../from/fromMonoisotopicMass';

/**
*
Expand All @@ -16,8 +16,6 @@ import { EMDB } from 'emdb';
* @returns {Promise}
*/
export async function getPeaksAnnotation(bestPeaks, options = {}) {
const emdb = new EMDB();

let {
numberDigits = 5,
shift = 0,
Expand Down Expand Up @@ -119,6 +117,8 @@ export async function getPeaksAnnotation(bestPeaks, options = {}) {
});
}

let mfs = [];

if (numberMFs) {
// we have 2 cases. Either there is a shift and we deal with differences
// otherwise it is absolute
Expand All @@ -135,16 +135,17 @@ export async function getPeaksAnnotation(bestPeaks, options = {}) {
currentMfPrefs.precision =
(currentMfPrefs.precision / Math.max(Math.abs(peak.x + shift), 1)) *
peak.x;
await emdb.fromMonoisotopicMass(
({ mfs } = await fromMonoisotopicMass(
Math.abs((peak.x + shift) * charge),
currentMfPrefs,
);
));
} else {
await emdb.fromMonoisotopicMass(Math.abs(peak.x * charge), mfPrefs);
({ mfs } = await fromMonoisotopicMass(
Math.abs(peak.x * charge),
mfPrefs,
));
}

let mfs = emdb.get('monoisotopic');

let numberOfMFS = Math.min(mfs.length, numberMFs);

for (let i = 0; i < numberOfMFS; i++) {
Expand Down

0 comments on commit dd8d2a4

Please sign in to comment.