Comparing version 0.7.0 to 0.8.0
{ | ||
"name": "mf-parser", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "", | ||
@@ -21,7 +21,7 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"atom-sorter": "^0.7.0", | ||
"atom-sorter": "^0.8.0", | ||
"chemical-elements": "^0.7.0", | ||
"chemical-groups": "^0.7.0", | ||
"mf-utilities": "^0.7.0" | ||
"chemical-groups": "^0.8.0", | ||
"mf-utilities": "^0.8.0" | ||
} | ||
} |
115
src/MF.js
'use strict'; | ||
const parse = require('./parse'); | ||
@@ -9,2 +8,3 @@ const toDisplay = require('./util/toDisplay'); | ||
const getInfo = require('./util/getInfo'); | ||
const getEA = require('./util/getEA'); | ||
const getIsotopesInfo = require('./util/getIsotopesInfo'); | ||
@@ -15,68 +15,79 @@ const partsToMF = require('./util/partsToMF'); | ||
class MF { | ||
constructor(mf) { | ||
this.parsed = parse(mf); | ||
this.cache = {}; | ||
} | ||
constructor(mf) { | ||
this.parsed = parse(mf); | ||
this.cache = {}; | ||
} | ||
toDisplay() { | ||
if (!this.cache.displayed) this.cache.displayed = toDisplay(this.parsed); | ||
return this.cache.displayed; | ||
toDisplay() { | ||
if (!this.cache.displayed) this.cache.displayed = toDisplay(this.parsed); | ||
return this.cache.displayed; | ||
} | ||
toHtml() { | ||
if (!this.cache.html) { | ||
this.toDisplay(); | ||
this.cache.html = toHtml(this.cache.displayed); | ||
} | ||
return this.cache.html; | ||
} | ||
toHtml() { | ||
if (!this.cache.html) { | ||
this.toDisplay(); | ||
this.cache.html = toHtml(this.cache.displayed); | ||
} | ||
return this.cache.html; | ||
toParts(options) { | ||
if (!this.cache.parts) { | ||
this.cache.parts = toParts(this.parsed, options); | ||
} | ||
return this.cache.parts; | ||
} | ||
toParts(options) { | ||
if (!this.cache.parts) { | ||
this.cache.parts = toParts(this.parsed, options); | ||
} | ||
return this.cache.parts; | ||
/** | ||
* Returns an object with the global MF, global charge, monoisotopic mass and mass | ||
* as well as the same informations for all the parts | ||
*/ | ||
getInfo(options = {}) { | ||
if (!this.cache.info) { | ||
this.toParts(); | ||
this.cache.info = getInfo(this.cache.parts, options); | ||
} | ||
return this.cache.info; | ||
} | ||
/** | ||
* Returns an object with the global MF, global charge, monoisotopic mass and mass | ||
* as well as the same informations for all the parts | ||
*/ | ||
getInfo(options = {}) { | ||
if (!this.cache.info) { | ||
this.toParts(); | ||
this.cache.info = getInfo(this.cache.parts, options); | ||
} | ||
return this.cache.info; | ||
/** | ||
* Returns an object with the elemental analysis | ||
*/ | ||
getEA(options = {}) { | ||
if (!this.cache.ea) { | ||
this.toParts(); | ||
this.cache.ea = getEA(this.cache.parts, options); | ||
} | ||
return this.cache.ea; | ||
} | ||
/** | ||
* Returns an array with each atom and isotopic composition | ||
*/ | ||
getIsotopesInfo(options = {}) { | ||
if (!this.cache.isotopesInfo) { | ||
this.toParts(); | ||
this.cache.isotopesInfo = getIsotopesInfo(this.cache.parts, options); | ||
} | ||
return this.cache.isotopesInfo; | ||
/** | ||
* Returns an array with each atom and isotopic composition | ||
*/ | ||
getIsotopesInfo(options = {}) { | ||
if (!this.cache.isotopesInfo) { | ||
this.toParts(); | ||
this.cache.isotopesInfo = getIsotopesInfo(this.cache.parts, options); | ||
} | ||
return this.cache.isotopesInfo; | ||
} | ||
/** | ||
* Get a canonized MF | ||
*/ | ||
toMF() { | ||
if (!this.cache.mf) { | ||
this.toParts(); | ||
this.cache.mf = partsToMF(this.cache.parts); | ||
} | ||
return this.cache.mf; | ||
/** | ||
* Get a canonized MF | ||
*/ | ||
toMF() { | ||
if (!this.cache.mf) { | ||
this.toParts(); | ||
this.cache.mf = partsToMF(this.cache.parts); | ||
} | ||
return this.cache.mf; | ||
} | ||
canonize() { | ||
this.toParts(); | ||
this.cache.displayed = partsToDisplay(this.cache.parts); | ||
this.cache.html = undefined; | ||
} | ||
canonize() { | ||
this.toParts(); | ||
this.cache.displayed = partsToDisplay(this.cache.parts); | ||
this.cache.html = undefined; | ||
} | ||
} | ||
module.exports = MF; |
@@ -12,2 +12,3 @@ 'use strict'; | ||
const isotopes = require('./getIsotopesObject'); | ||
const getIsotopeRatioInfo = require('./getIsotopeRatioInfo'); | ||
@@ -129,28 +130,2 @@ /** | ||
function getIsotopeRatioInfo(value) { | ||
let result = { mass: 0, monoisotopicMass: 0 }; | ||
let element = elements[value.atom]; | ||
if (!element) throw new Error(`Element not found: ${value.atom}`); | ||
let isotopesArray = element.isotopes; | ||
let ratios = normalize(value.ratio); | ||
let max = Math.max(...ratios); | ||
if (ratios.length > isotopesArray.length) { | ||
throw new Error( | ||
`the number of specified ratios is bigger that the number of stable isotopes: ${ | ||
value.atom | ||
}` | ||
); | ||
} | ||
for (let i = 0; i < ratios.length; i++) { | ||
result.mass += ratios[i] * isotopesArray[i].mass; | ||
if (max === ratios[i] && result.monoisotopicMass === 0) { | ||
result.monoisotopicMass = isotopesArray[i].mass; | ||
} | ||
} | ||
return result; | ||
} | ||
function normalize(array) { | ||
let sum = array.reduce((prev, current) => prev + current, 0); | ||
return array.map(a => a / sum); | ||
} |
@@ -7,3 +7,3 @@ 'use strict'; | ||
* Convert a MF part to an array of atoms | ||
* This procedure will suppress the isotpes ! | ||
* This procedure will suppress the isotopes ! | ||
* This is mainly used to make queries | ||
@@ -13,24 +13,24 @@ */ | ||
module.exports = function partToAtoms(part) { | ||
var atoms = {}; | ||
for (let line of part) { | ||
switch (line.kind) { | ||
case Kind.ISOTOPE: | ||
if (!atoms[line.value.atom]) atoms[line.value.atom] = 0; | ||
atoms[line.value.atom] += line.multiplier; | ||
break; | ||
case Kind.ISOTOPE_RATIO: | ||
if (!atoms[line.value.atom]) atoms[line.value.atom] = 0; | ||
atoms[line.value.atom] += line.multiplier; | ||
break; | ||
case Kind.ATOM: | ||
if (!atoms[line.value]) atoms[line.value] = 0; | ||
atoms[line.value] += line.multiplier; | ||
break; | ||
case Kind.CHARGE: | ||
break; | ||
default: | ||
throw new Error('partToMF unhandled Kind: ', line.kind); | ||
} | ||
var atoms = {}; | ||
for (let line of part) { | ||
switch (line.kind) { | ||
case Kind.ISOTOPE: | ||
if (!atoms[line.value.atom]) atoms[line.value.atom] = 0; | ||
atoms[line.value.atom] += line.multiplier; | ||
break; | ||
case Kind.ISOTOPE_RATIO: | ||
if (!atoms[line.value.atom]) atoms[line.value.atom] = 0; | ||
atoms[line.value.atom] += line.multiplier; | ||
break; | ||
case Kind.ATOM: | ||
if (!atoms[line.value]) atoms[line.value] = 0; | ||
atoms[line.value] += line.multiplier; | ||
break; | ||
case Kind.CHARGE: | ||
break; | ||
default: | ||
throw new Error('partToMF unhandled Kind: ', line.kind); | ||
} | ||
return atoms; | ||
} | ||
return atoms; | ||
}; |
57600
32
1640
+ Addedatom-sorter@0.8.0(transitive)
+ Addedchemical-groups@0.8.0(transitive)
+ Addedmf-utilities@0.8.0(transitive)
- Removedatom-sorter@0.7.0(transitive)
- Removedchemical-groups@0.7.0(transitive)
- Removedmf-utilities@0.7.0(transitive)
Updatedatom-sorter@^0.8.0
Updatedchemical-groups@^0.8.0
Updatedmf-utilities@^0.8.0