mf-generator
Advanced tools
Comparing version 0.5.0 to 0.5.2
{ | ||
"name": "mf-generator", | ||
"version": "0.5.0", | ||
"version": "0.5.2", | ||
"description": "", | ||
@@ -21,6 +21,9 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"chemical-elements": "^0.5.0", | ||
"mf-matcher": "^0.5.0", | ||
"mf-parser": "^0.5.0" | ||
"chemical-elements": "^0.5.2", | ||
"mf-finder": "^0.5.2", | ||
"mf-matcher": "^0.5.2", | ||
"mf-parser": "^0.5.2", | ||
"mf-utilities": "^0.5.2", | ||
"sum-object-keys": "^1.0.2" | ||
} | ||
} |
'use strict'; | ||
const { ELECTRON_MASS } = require('chemical-elements/src/constants'); | ||
const MF = require('mf-parser').MF; | ||
const matcher = require('mf-matcher'); | ||
const matcher = require('mf-matcher').msem; | ||
const sum = require('sum-object-keys'); | ||
const preprocessIonizations = require('mf-utilities/src/preprocessIonizations'); | ||
@@ -13,6 +14,7 @@ /** | ||
* @param keys | ||
* @param options | ||
* @param {object} options | ||
* @param {number} [options.limit=10000000] - Maximum number of results | ||
* @param {boolean} [canonizeMF=true] - Canonize molecular formula | ||
* @param {boolean} [uniqueMFs=true] - Force canonization and make MF unique | ||
* @param {string} [ionizations=''] - Comma separated list of ionizations (to charge the molecule) | ||
* @param {number} [options.filter.minMass=0] - Minimal monoisotopic mass | ||
@@ -22,4 +24,4 @@ * @param {number} [options.filter.maxMass=+Infinity] - Maximal monoisotopic mass | ||
* @param {number} [options.filter.maxEM=+Infinity] - Maximal neutral monoisotopic mass | ||
* @param {number} [options.filter.minMSEM=0] - Minimal observed monoisotopic mass | ||
* @param {number} [options.filter.maxMSEM=+Infinity] - Maximal observed monoisotopic mass | ||
* @param {number} [options.filter.targetMass] - Experimental observed mass | ||
* @param {number} [options.filter.precision=1000] - Precision | ||
* @param {number} [options.filter.minCharge=-Infinity] - Minimal charge | ||
@@ -36,6 +38,6 @@ * @param {number} [options.filter.maxCharge=+Infinity] - Maximal charge | ||
module.exports = function combineMFs(keys, options = {}) { | ||
module.exports = function generateMFs(keys, options = {}) { | ||
let { | ||
limit = 10000000, | ||
uniqueMFs | ||
uniqueMFs, | ||
} = options; | ||
@@ -45,3 +47,5 @@ if (uniqueMFs === undefined) uniqueMFs = true; | ||
if (options.canonizeMF === undefined) options.canonizeMF = true; | ||
options.ionizations = preprocessIonizations(options.ionizations); | ||
if (!Array.isArray(keys)) throw new Error('You need to specify an array of strings or arrays'); | ||
@@ -101,6 +105,7 @@ | ||
appendResult(results, currents, keys, options); | ||
if (uniqueMFs) { | ||
var uniqueMFsObject = {}; | ||
results.forEach((r) => { | ||
uniqueMFsObject[r.mf] = r; | ||
uniqueMFsObject[r.mf + r.ionization.mf] = r; | ||
}); | ||
@@ -133,3 +138,3 @@ results = Object.keys(uniqueMFsObject).map((k) => uniqueMFsObject[k]); | ||
function getEMFromParts(parts, currents) { | ||
function getEMFromParts(parts, currents, ionization) { | ||
var charge = 0; | ||
@@ -142,2 +147,3 @@ var em = 0; | ||
for (var i = 0; i < parts.length; i++) { | ||
@@ -156,15 +162,8 @@ var part = parts[i][currents[i]]; | ||
} | ||
var msem = em; | ||
if (charge > 0) { | ||
msem = msem / charge - ELECTRON_MASS; | ||
} else if (charge < 0) { | ||
msem = msem / (charge * -1) + ELECTRON_MASS; | ||
} else { | ||
msem = 0; | ||
} | ||
return { | ||
charge, | ||
em, | ||
msem, | ||
mw, | ||
ionization: ionization, | ||
unsaturation: validUnsaturation ? unsaturation / 2 + 1 : undefined, | ||
@@ -178,4 +177,6 @@ atoms | ||
canonizeMF, | ||
filter | ||
filter, | ||
ionizations | ||
} = options; | ||
// this script is designed to combine molecular formula | ||
@@ -185,27 +186,33 @@ // that may contain comments after a "$" sign | ||
var result = getEMFromParts(keys, currents); | ||
if (!matcher(result, filter)) return; | ||
for (var ionization of ionizations) { | ||
result.parts = []; | ||
result.mf = ''; | ||
var result = getEMFromParts(keys, currents, ionization); | ||
var comments = []; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i][currents[i]]; | ||
if (key) { | ||
result.parts[i] = key; | ||
if (key.indexOf('$') > -1) { | ||
comments.push(key.replace(/^[^$]*\$/, '')); | ||
key = key.replace(/\$.*/, ''); | ||
var match = matcher(result, filter); | ||
if (!match) return; | ||
result.ms = match; | ||
result.parts = []; | ||
result.mf = ''; | ||
var comments = []; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i][currents[i]]; | ||
if (key) { | ||
result.parts[i] = key; | ||
if (key.indexOf('$') > -1) { | ||
comments.push(key.replace(/^[^$]*\$/, '')); | ||
key = key.replace(/\$.*/, ''); | ||
} | ||
result.mf += key; | ||
} | ||
result.mf += key; | ||
} | ||
} | ||
if (canonizeMF) { | ||
result.mf = (new MF(result.mf)).toMF(); | ||
if (canonizeMF) { | ||
result.mf = (new MF(result.mf)).toMF(); | ||
} | ||
if (comments.length > 0) result.mf += `$${comments.join(' ')}`; | ||
results.push(result); | ||
} | ||
if (comments.length > 0) result.mf += `$${comments.join(' ')}`; | ||
results.push(result); | ||
} | ||
@@ -212,0 +219,0 @@ |
16885
410
6
+ Addedmf-finder@^0.5.2
+ Addedmf-utilities@^0.5.2
+ Addedsum-object-keys@^1.0.2
+ Addedisotope-abundances@2.0.4(transitive)
+ Addedmf-finder@0.5.12(transitive)
+ Addedmolecular-formula@1.1.5(transitive)
+ Addedsum-object-keys@1.0.2(transitive)
Updatedchemical-elements@^0.5.2
Updatedmf-matcher@^0.5.2
Updatedmf-parser@^0.5.2