Comparing version 0.6.2 to 0.6.3
{ | ||
"name": "mf-finder", | ||
"version": "0.6.2", | ||
"version": "0.6.3", | ||
"description": "Find a molecular formula from a monoisotopic mass", | ||
@@ -21,8 +21,8 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"atom-sorter": "^0.6.2", | ||
"chemical-elements": "^0.6.2", | ||
"mf-parser": "^0.6.2", | ||
"mf-utilities": "^0.6.2", | ||
"atom-sorter": "^0.6.3", | ||
"chemical-elements": "^0.6.3", | ||
"mf-parser": "^0.6.3", | ||
"mf-utilities": "^0.6.3", | ||
"molecular-formula": "^1.1.0" | ||
} | ||
} |
160
src/index.js
@@ -17,3 +17,2 @@ 'use strict'; | ||
let targetMassCache; | ||
@@ -32,4 +31,4 @@ | ||
{ mf: 'O', min: 0, max: 100 }, | ||
{ mf: 'N', min: 0, max: 100 }, | ||
], | ||
{ mf: 'N', min: 0, max: 100 } | ||
] | ||
} = options; | ||
@@ -39,9 +38,21 @@ | ||
// we calculate not the real unsaturation but the one before dividing by 2 + 1 | ||
let fakeMinUnsaturation = (unsaturation.min === undefined) ? Number.MIN_SAFE_INTEGER : (unsaturation.min - 1) * 2; | ||
let fakeMaxUnsaturation = (unsaturation.max === undefined) ? Number.MAX_SAFE_INTEGER : (unsaturation.max - 1) * 2; | ||
let fakeMinUnsaturation = | ||
unsaturation.min === undefined | ||
? Number.MIN_SAFE_INTEGER | ||
: (unsaturation.min - 1) * 2; | ||
let fakeMaxUnsaturation = | ||
unsaturation.max === undefined | ||
? Number.MAX_SAFE_INTEGER | ||
: (unsaturation.max - 1) * 2; | ||
let filterCharge = (minCharge !== Number.MIN_SAFE_INTEGER || maxCharge !== Number.MAX_SAFE_INTEGER); | ||
let filterCharge = | ||
minCharge !== Number.MIN_SAFE_INTEGER || | ||
maxCharge !== Number.MAX_SAFE_INTEGER; | ||
let result = { | ||
mfs: [] | ||
mfs: [], | ||
info: { | ||
numberMFEvaluated: 0, | ||
numberResults: 0 | ||
} | ||
}; | ||
@@ -53,3 +64,2 @@ let orderMapping = []; // used to sort the atoms | ||
for (let ionization of ionizations) { | ||
let currentIonization = { | ||
@@ -66,9 +76,8 @@ currentMonoisotopicMass: ionization.em, | ||
if (possibilities.length === 0) return { mfs: [] }; | ||
targetMassCache = new TargetMassCache(targetMass, possibilities, Object.assign({}, options, { charge: ionization.charge })); | ||
result.info = { | ||
numberMFEvaluated: 0, | ||
numberResults: 0, | ||
}; | ||
targetMassCache = new TargetMassCache( | ||
targetMass, | ||
possibilities, | ||
Object.assign({}, options, { charge: ionization.charge }) | ||
); | ||
let theEnd = false; | ||
@@ -89,3 +98,5 @@ let maxPosition = possibilities.length; | ||
if (result.info.numberMFEvaluated++ > maxIterations) { | ||
throw Error(`Iteration number is over the current maximum of: ${maxIterations}`); | ||
throw Error( | ||
`Iteration number is over the current maximum of: ${maxIterations}` | ||
); | ||
} | ||
@@ -98,7 +109,13 @@ if (filterUnsaturation) { | ||
(unsaturation.onlyNonInteger && isOdd === 0) || | ||
(fakeMinUnsaturation > unsaturationValue) || | ||
(fakeMaxUnsaturation < unsaturationValue) | ||
) isValid = false; | ||
fakeMinUnsaturation > unsaturationValue || | ||
fakeMaxUnsaturation < unsaturationValue | ||
) { | ||
isValid = false; | ||
} | ||
} | ||
if (filterCharge && (lastPossibility.currentCharge < minCharge || lastPossibility.currentCharge > maxCharge)) { | ||
if ( | ||
filterCharge && | ||
(lastPossibility.currentCharge < minCharge || | ||
lastPossibility.currentCharge > maxCharge) | ||
) { | ||
isValid = false; | ||
@@ -108,5 +125,12 @@ } | ||
if (isValid) { | ||
let minMass = targetMassCache.getMinMass(lastPossibility.currentCharge); | ||
let maxMass = targetMassCache.getMaxMass(lastPossibility.currentCharge); | ||
if ((lastPossibility.currentMonoisotopicMass < minMass) || (lastPossibility.currentMonoisotopicMass > maxMass)) { | ||
let minMass = targetMassCache.getMinMass( | ||
lastPossibility.currentCharge | ||
); | ||
let maxMass = targetMassCache.getMaxMass( | ||
lastPossibility.currentCharge | ||
); | ||
if ( | ||
lastPossibility.currentMonoisotopicMass < minMass || | ||
lastPossibility.currentMonoisotopicMass > maxMass | ||
) { | ||
isValid = false; | ||
@@ -116,3 +140,11 @@ } | ||
if (isValid) { | ||
result.mfs.push(getResult(possibilities, targetMass, allowNeutral, ionization, orderMapping)); | ||
result.mfs.push( | ||
getResult( | ||
possibilities, | ||
targetMass, | ||
allowNeutral, | ||
ionization, | ||
orderMapping | ||
) | ||
); | ||
result.info.numberResults++; | ||
@@ -125,3 +157,6 @@ } | ||
currentAtom = possibilities[currentPosition]; | ||
previousAtom = (currentPosition === 0) ? currentIonization : possibilities[currentPosition - 1]; | ||
previousAtom = | ||
currentPosition === 0 | ||
? currentIonization | ||
: possibilities[currentPosition - 1]; | ||
if (currentAtom.currentCount < currentAtom.currentMaxCount) { | ||
@@ -132,3 +167,6 @@ currentAtom.currentCount++; | ||
currentPosition++; | ||
setCurrentMinMax(possibilities[currentPosition], possibilities[currentPosition - 1]); | ||
setCurrentMinMax( | ||
possibilities[currentPosition], | ||
possibilities[currentPosition - 1] | ||
); | ||
} else { | ||
@@ -153,8 +191,20 @@ break; | ||
function updateCurrentAtom(currentAtom, previousAtom) { | ||
currentAtom.currentMonoisotopicMass = previousAtom.currentMonoisotopicMass + currentAtom.em * currentAtom.currentCount; | ||
currentAtom.currentCharge = previousAtom.currentCharge + currentAtom.charge * currentAtom.currentCount; | ||
currentAtom.currentUnsaturation = previousAtom.currentUnsaturation + currentAtom.unsaturation * currentAtom.currentCount; | ||
currentAtom.currentMonoisotopicMass = | ||
previousAtom.currentMonoisotopicMass + | ||
currentAtom.em * currentAtom.currentCount; | ||
currentAtom.currentCharge = | ||
previousAtom.currentCharge + | ||
currentAtom.charge * currentAtom.currentCount; | ||
currentAtom.currentUnsaturation = | ||
previousAtom.currentUnsaturation + | ||
currentAtom.unsaturation * currentAtom.currentCount; | ||
} | ||
function getResult(possibilities, targetMass, allowNeutralMolecules, ionization, orderMapping) { | ||
function getResult( | ||
possibilities, | ||
targetMass, | ||
allowNeutralMolecules, | ||
ionization, | ||
orderMapping | ||
) { | ||
let lastPossibility = possibilities[possibilities.length - 1]; | ||
@@ -170,3 +220,2 @@ | ||
// we check that the first time we meet the ionization group it does not end | ||
@@ -180,10 +229,15 @@ // in the final result | ||
result.mf += `(${possibility.mf})`; | ||
if (possibility.currentCount !== 1) result.mf += possibility.currentCount; | ||
if (possibility.currentCount !== 1) { | ||
result.mf += possibility.currentCount; | ||
} | ||
} else { | ||
result.mf += possibility.mf; | ||
if (possibility.currentCount !== 1) result.mf += possibility.currentCount; | ||
if (possibility.currentCount !== 1) { | ||
result.mf += possibility.currentCount; | ||
} | ||
} | ||
} | ||
} | ||
result.unsaturation = (result.unsaturation + Math.abs(result.charge)) / 2 + 1; | ||
result.unsaturation = | ||
(result.unsaturation + Math.abs(result.charge)) / 2 + 1; | ||
result.ms = getMsInfo(result, { targetMass, allowNeutralMolecules }); | ||
@@ -199,7 +253,28 @@ return result; | ||
currentAtom.currentCount = currentAtom.currentMinCount - 1; | ||
} else { // no more change of charge, we can optimize | ||
let currentMass = previousAtom !== undefined ? previousAtom.currentMonoisotopicMass : 0; | ||
let currentCharge = previousAtom !== undefined ? previousAtom.currentCharge : 0; | ||
currentAtom.currentMinCount = Math.max(Math.floor((targetMassCache.getMinMass(currentCharge) - currentMass - currentAtom.maxInnerMass) / currentAtom.em), currentAtom.originalMinCount); | ||
currentAtom.currentMaxCount = Math.min(Math.floor((targetMassCache.getMaxMass(currentCharge) - currentMass - currentAtom.minInnerMass) / currentAtom.em), currentAtom.originalMaxCount); | ||
} else { | ||
// no more change of charge, we can optimize | ||
let currentMass = | ||
previousAtom !== undefined | ||
? previousAtom.currentMonoisotopicMass | ||
: 0; | ||
let currentCharge = | ||
previousAtom !== undefined ? previousAtom.currentCharge : 0; | ||
currentAtom.currentMinCount = Math.max( | ||
Math.floor( | ||
(targetMassCache.getMinMass(currentCharge) - | ||
currentMass - | ||
currentAtom.maxInnerMass) / | ||
currentAtom.em | ||
), | ||
currentAtom.originalMinCount | ||
); | ||
currentAtom.currentMaxCount = Math.min( | ||
Math.floor( | ||
(targetMassCache.getMaxMass(currentCharge) - | ||
currentMass - | ||
currentAtom.minInnerMass) / | ||
currentAtom.em | ||
), | ||
currentAtom.originalMaxCount | ||
); | ||
currentAtom.currentCount = currentAtom.currentMinCount - 1; | ||
@@ -209,3 +284,2 @@ } | ||
function initializePossibilities(possibilities, currentIonization) { | ||
@@ -224,3 +298,9 @@ for (let i = 0; i < possibilities.length; i++) { | ||
function possibilitiesToString(possibilities) { | ||
return possibilities.map((a) => [`mf:${a.mf}`, `current:${a.currentCount}`, `min:${a.currentMinCount}`, `max:${a.currentMaxCount}`, `charge:${a.currentCharge}`]); | ||
return possibilities.map((a) => [ | ||
`mf:${a.mf}`, | ||
`current:${a.currentCount}`, | ||
`min:${a.currentMinCount}`, | ||
`max:${a.currentMaxCount}`, | ||
`charge:${a.currentCharge}` | ||
]); | ||
} | ||
@@ -227,0 +307,0 @@ |
36203
908
Updatedatom-sorter@^0.6.3
Updatedchemical-elements@^0.6.3
Updatedmf-parser@^0.6.3
Updatedmf-utilities@^0.6.3