mass-fragmentation
Advanced tools
Comparing version 1.6.0 to 1.7.0
{ | ||
"name": "mass-fragmentation", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "Code to fragment molecules", | ||
@@ -30,3 +30,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "6603850b30bead58389d612027251b5a406e4d1d" | ||
"gitHead": "5792f7e51124ce002a78753392b4e352e705a229" | ||
} |
@@ -13,3 +13,11 @@ import { writeFileSync } from 'fs'; | ||
OCL, | ||
masses: [97.5624, 105.0697, 58.065, 194.1173, 163.0752, 133.0647, 135.0439], | ||
peaks: [ | ||
{ mass: 97.5624, intensity: 100 }, | ||
{ mass: 105.0697, intensity: 100 }, | ||
{ mass: 58.065, intensity: 100 }, | ||
{ mass: 194.1173, intensity: 100 }, | ||
{ mass: 163.0752, intensity: 100 }, | ||
{ mass: 133.0647, intensity: 100 }, | ||
{ mass: 135.0439, intensity: 100 }, | ||
], | ||
accuracy: 50, | ||
@@ -16,0 +24,0 @@ }); |
@@ -11,4 +11,4 @@ import OCL from 'openchemlib'; | ||
const { trees, validNodes, masses } = reactionFragmentation(molecule); | ||
expect(validNodes).toHaveLength(159); | ||
expect(masses).toHaveLength(18); | ||
expect(validNodes).toHaveLength(624); | ||
expect(masses).toHaveLength(70); | ||
expect(trees).toMatchSnapshot(); | ||
@@ -24,3 +24,3 @@ }); | ||
}); | ||
expect(validNodes).toHaveLength(294); | ||
expect(validNodes).toHaveLength(297); | ||
expect(masses).toHaveLength(35); | ||
@@ -35,6 +35,6 @@ | ||
const { trees, validNodes, masses } = reactionFragmentation(molecule); | ||
expect(validNodes).toHaveLength(550); | ||
expect(masses).toHaveLength(54); | ||
expect(validNodes).toHaveLength(1266); | ||
expect(masses).toHaveLength(50); | ||
expect(trees).toMatchSnapshot(); | ||
}); | ||
}); |
@@ -6,3 +6,3 @@ import { getDatabase } from '../getDatabase.js'; | ||
const db = getDatabase(); | ||
expect(db).toHaveLength(190); | ||
expect(db).toHaveLength(251); | ||
}); | ||
@@ -14,3 +14,3 @@ it('kind:all and ionizationKind:esiPositive', () => { | ||
}); | ||
expect(db).toHaveLength(145); | ||
expect(db).toHaveLength(181); | ||
}); | ||
@@ -22,3 +22,3 @@ it('kind:all and ionizationKind:esiNegative', () => { | ||
}); | ||
expect(db).toHaveLength(0); | ||
expect(db).toHaveLength(25); | ||
}); | ||
@@ -37,3 +37,3 @@ it('kind:all and ionizationKind:ei', () => { | ||
}); | ||
expect(db).toHaveLength(7); | ||
expect(db).toHaveLength(11); | ||
}); | ||
@@ -45,3 +45,3 @@ it('kind:ionization and ionizationKind:esiPositive', () => { | ||
}); | ||
expect(db).toHaveLength(7); | ||
expect(db).toHaveLength(11); | ||
}); | ||
@@ -95,3 +95,3 @@ it('kind:ionization and ionizationKind:esiNegative', () => { | ||
}); | ||
expect(db).toHaveLength(183); | ||
expect(db).toHaveLength(240); | ||
}); | ||
@@ -103,3 +103,3 @@ it('kind:reaction and ionizationKind:esiPositive', () => { | ||
}); | ||
expect(db).toHaveLength(138); | ||
expect(db).toHaveLength(170); | ||
}); | ||
@@ -111,3 +111,3 @@ it('kind:reaction and ionizationKind:esiNegative', () => { | ||
}); | ||
expect(db).toHaveLength(0); | ||
expect(db).toHaveLength(25); | ||
}); | ||
@@ -114,0 +114,0 @@ it('kind:reaction and ionizationKind:ei', () => { |
import { render, moleculeRenderer } from 'react-tree-svg'; | ||
/** | ||
* @typedef {object} MassPeak | ||
* @property {number} mass | ||
* @property {number} intensity | ||
*/ | ||
/** | ||
* @param {object[]} trees | ||
* @param {object} [options={}] | ||
* @param {object} [options.OCL] | ||
* @param {number} [options.accuracy] | ||
* @param {MassPeak[]} [options.peaks] | ||
* @returns | ||
*/ | ||
export function getFragmentationSVG(trees, options = {}) { | ||
const { OCL, accuracy, masses = [] } = options; | ||
const { OCL, accuracy, peaks = [] } = options; | ||
const maxIntensity = | ||
peaks?.length > 0 | ||
? Math.log(Math.max(...peaks.map((peak) => peak.intensity)) + 1) | ||
: 0; | ||
const rendererOptions = { | ||
@@ -22,5 +41,8 @@ nodeRenderer: moleculeRenderer, | ||
for (const molecule of node.molecules) { | ||
if (isInRange(masses, molecule?.info?.mz, accuracy)) { | ||
const peak = getPeakInRange(peaks, molecule?.info?.mz, accuracy); | ||
if (peak) { | ||
return { | ||
fillOpacity: 0.2, | ||
fillOpacity: maxIntensity | ||
? (0.2 * Math.log(peak.intensity + 1)) / maxIntensity | ||
: 0.2, | ||
fill: 'red', | ||
@@ -45,14 +67,14 @@ }; | ||
function isInRange(masses, mass, accuracy) { | ||
if (!mass || !masses) { | ||
return false; | ||
function getPeakInRange(peaks, mass, accuracy) { | ||
if (!mass || !Array.isArray(peaks)) { | ||
return undefined; | ||
} | ||
const massAccuracy = (accuracy * mass) / 1e6; | ||
for (const value of masses) { | ||
if (Math.abs(value - mass) <= massAccuracy) { | ||
return true; | ||
for (const peak of peaks) { | ||
if (Math.abs(peak.mass - mass) <= massAccuracy) { | ||
return peak; | ||
} | ||
} | ||
return false; | ||
return undefined; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
2852475
9568