Skip to content

Commit b23ddf4

Browse files
committed
filter resulting combineMF based on various criteria of mass and charge
1 parent a4fe7db commit b23ddf4

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

Diff for: src/combineMFs.js

+44-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,27 @@ var removeMFLastPart = require('./util/removeMFLastPart.js');
77
// TODO replace from the value coming from chemcalc
88
var ELECTRON_MASS=5.4857990946e-4;
99

10+
/**
11+
*
12+
* @param keys
13+
* @param options
14+
* @param {number} [options.limit=1000000] - Maximum number of results
15+
* @param {number} [options.minMass=0] - Minimal monoisotopic mass
16+
* @param {number} [options.maxMass=+Infinity] - Maximal monoisotopic mass
17+
* @param {number} [options.minMSMass=0] - Minimal observed monoisotopic mass
18+
* @param {number} [options.maxMSMass=+Infinity] - Maximal observed monoisotopic mass
19+
* @param {number} [options.minCharge=-Infinity] - Minimal charge
20+
* @param {number} [options.maxCharge=+Infinity] - Maximal charge
21+
* @returns {Array}
22+
*/
23+
1024
function combineMFs (keys, options) {
11-
var options=options || {};
12-
options.limit=options.limit || 1000000;
25+
26+
options=Object.assign({}, {
27+
limit:1000000,
28+
minCharge:Number.NEGATIVE_INFINITY,
29+
maxCharge:Number.POSITIVE_INFINITY
30+
}, options);
1331
if (!Array.isArray(keys)) return [];
1432

1533

@@ -52,7 +70,7 @@ function combineMFs (keys, options) {
5270
while (position < currents.length) {
5371
if (currents[position] < sizes[position]) {
5472
evolution++;
55-
appendResult(results, currents, keys);
73+
appendResult(results, currents, keys, options);
5674
currents[position]++;
5775
for (var i = 0; i < position; i++) {
5876
currents[i] = 0;
@@ -65,7 +83,7 @@ function combineMFs (keys, options) {
6583
throw new Error('You have reached the limit of '+options.limit+'. You could still change this value using options.limit but it is likely to crash.');
6684
}
6785
}
68-
appendResult(results, currents, keys);
86+
appendResult(results, currents, keys, options);
6987
return results;
7088
}
7189

@@ -115,13 +133,30 @@ function getEMFromParts(parts, currents) {
115133
}
116134
}
117135

118-
function appendResult(results, currents, keys) {
136+
function appendResult(results, currents, keys, options) {
119137
// this script is designed to combine molecular formula
120138
// that may contain comments after a "$" sign
121139
// therefore we should put all the comments at the ned
140+
141+
var info=getEMFromParts(keys, currents);
142+
143+
var em=info.em;
144+
var msem=info.msem;
145+
var charge=info.charge;
146+
147+
if ( (typeof options.minMass!=='undefined' && em<options.minMass)
148+
|| (typeof options.maxMass!=='undefined' && em>options.maxMass)) return;
149+
if ( (typeof options.minMSMass!=='undefined' && msem<options.minMSMass)
150+
|| (typeof options.maxMSMass!=='undefined' && msem>options.maxMSMass)) return;
151+
if (charge<options.minCharge || charge>options.maxCharge) return;
152+
122153
var result = {mf: ''};
123154
var comments = [];
124155

156+
result.em=em;
157+
result.msem=msem;
158+
result.charge=charge;
159+
125160
for (var i = 0; i < keys.length; i++) {
126161
var key = keys[i][currents[i]];
127162
if (key) {
@@ -132,12 +167,12 @@ function appendResult(results, currents, keys) {
132167
}
133168
result.mf += key;
134169
}
135-
var info=getEMFromParts(keys, currents);
136-
result.em=info.em;
137-
result.msem=info.msem;
138-
result.charge=info.charge;
139170
}
140171

172+
173+
174+
175+
141176
if (comments.length > 0) result.mf += '$' + comments.join(' ');
142177

143178
results.push(result);

Diff for: test/combineMF.js

+10
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ describe('combine MFs test', function () {
8989
result.length.should.equal(101*101);
9090
});
9191

92+
it('From array of string to large array and filter', function () {
93+
var mfsArray=["C0-100","O0-100"];
94+
var result=CE.combineMFs(mfsArray,
95+
{
96+
minMass: 0.1,
97+
maxMass:13
98+
});
99+
result.length.should.equal(1);
100+
});
101+
92102
it('Strange comments', function () {
93103
var mfsArray=["C$1>10","O$D2>20"];
94104
var result=CE.combineMFs(mfsArray);

0 commit comments

Comments
 (0)