| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 |
1×
1×
8×
8×
8×
8×
8×
8×
40×
4×
1×
1×
2×
2×
3×
1×
20132×
20132×
20132×
20132×
18×
18×
43×
18×
11×
11×
10066×
124×
11×
10066×
10066×
11×
66×
66×
93×
66×
66×
66×
66×
66×
66×
29650×
29650×
29650×
29650×
29650×
11276×
29650×
18440×
29650×
38157484×
38157484×
38157484×
38157484×
29650×
29650×
25202×
4448×
29650×
66×
11×
11×
18×
18×
18×
18×
18×
10×
8×
18×
56×
56×
38×
38×
38×
56×
28×
28×
56×
56×
11×
2×
26×
13×
13×
11×
11×
11×
10066×
167×
11×
| /*
Port of emass done by Michael Porter
Based on an algorithm developed by Alan L. Rockwood.
Published in
Rockwood, A.L. and Haimi, P.: "Efficent calculation of
Accurate Masses of Isotopic Peaks",
Journal of The American Society for Mass Spectrometry
JASMS 03-2263, in press
which contained the following copyright and disclaimer:
Copyright (c) 2005 Perttu Haimi and Alan L. Rockwood
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the following conditions are met:
* Redistributions of source code must retain the
above copyright notice, this list of conditions
and the following disclaimer.
* Redistributions in binary form must reproduce
the above copyright notice, this list of conditions
and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors
may be used to endorse or promote products derived
from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var isoAbund = require('isotope-abundances');
module.exports = class emass {
constructor(options) {
this.ELECTRON = .00054858;
this.limit = this.opt(options, 'limit', 1E-10);
this.customIsotopes = this.opt(options, 'customIsotopes', {});
this.abundance_decimals = this.opt(options, 'abundanceDecimals', 8);
this.mass_decimals = this.opt(options, 'massDecimals', 6);
this.cutoff = this.opt(options, 'cutoff', 0.0001);
}
// From here https://stackoverflow.com/questions/23577632/optional-arguments-in-nodejs-functions
opt(options, name, default_value){
return options && options[name]!==undefined ? options[name] : default_value;
}
addCustomIsotopes(element, isotopes) {
this.customIsotopes[element] = {
"Isotopes": isotopes
};
}
clearCustomIsotopes() {
this.customIsotopes = {};
}
deleteCustomIsotopes(element) {
delete this.customIsotopes[element];
}
setPruneLimit(limit) {
this.limit = limit;
}
setCutoff(cutoff) {
this.cutoff = cutoff;
}
setAbundanceDecimals(abundance_decimals) {
this.abundance_decimals = abundance_decimals;
}
setMassDecimals(mass_decimals) {
this.mass_decimals = mass_decimals;
}
print_list(l) {
var out = "";
for(var i=0; i<l.length; i++) {
out += JSON.stringify(l[i]);
}
return out;
}
// This code comes from https://stackoverflow.com/questions/15762768/javascript-math-round-to-two-decimal-places
round(n, digits) {
Iif (digits === undefined) {
digits = 0;
}
var multiplicator = Math.pow(10, digits);
n = parseFloat((n * multiplicator).toFixed(11));
return parseFloat((Math.round(n) / multiplicator).toFixed(digits));
}
create_atom_list(isotopes) {
var atom_list = [];
for(var i=0; i<isotopes.length; i++) {
atom_list.push({'Mass':isotopes[i].Mass, 'Abundance':isotopes[i].Abundance})
}
return atom_list;
}
normalize(results) {
var maxVal = -1;
for(var i=0; i<results.length; i++) {
if(results[i].Abundance > maxVal) {
maxVal = results[i].Abundance;
}
}
for(var i=0; i<results.length; i++) {
results[i].Mass = this.round(results[i].Mass, this.mass_decimals);
results[i].Abundance = this.round(results[i].Abundance/maxVal, this.abundance_decimals);
}
return results;
}
prune(f) {
while(f[0].Abundance < this.limit) {
f.shift();
}
while(f[f.length-1].Abundance < this.limit) {
f.pop();
}
return f;
}
convolute(g, f) {
var h = [];
var g_n = g.length;
var f_n = f.length;
Iif(g_n === 0 || f_n === 0) {
return h;
}
for(var k=0; k<g_n+f_n-1; k++) {
var sumweight = 0;
var summass = 0;
var start;
var end;
if(k < f_n-1) { start = 0; }
else { start = k-f_n+1; }
if(k < g_n-1) { end = k; }
else { end = g_n - 1; }
for(var i=start; i<end+1; i++) {
var weight = g[i].Abundance * f[k-i].Abundance;
var mass = g[i].Mass + f[k-i].Mass;
sumweight += weight;
summass += weight * mass;
}
var p;
if(sumweight === 0) {
p = {'Mass':-1, 'Abundance':sumweight};
}
else {
p = {'Mass':summass/sumweight, 'Abundance':sumweight};
}
h.push(p)
}
return h;
}
calculate(formula, charge=0) {
var result = [{'Mass':0,'Abundance':1}];
for (var element in formula) {
Eif (formula.hasOwnProperty(element)) {
var n = parseInt(formula[element]);
var j = 0;
var atom_list = [];
if (!(element in this.customIsotopes)) {
atom_list = [this.create_atom_list(isoAbund(element).Isotopes)];
}
else {
atom_list = [this.create_atom_list(this.customIsotopes[element].Isotopes)];
}
while(n > 0) {
var size = atom_list.length;
if(j === size) {
atom_list.push([]);
atom_list[j] = this.convolute(atom_list[j-1], atom_list[j-1]);
atom_list[j] = this.prune(atom_list[j])
}
if(n & 1) {
result = this.convolute(result, atom_list[j]);
result = this.prune(result);
}
n = (n >> 1);
j++;
}
}
}
if(charge !== 0) {
for(var i=0; i<result.length; i++) {
if(charge>0) {
result[i].Mass = result[i].Mass/Math.abs(charge) - this.ELECTRON;
}
else {
result[i].Mass = result[i].Mass/Math.abs(charge) + this.ELECTRON;
}
}
}
result = this.normalize(result);
var filtered_result = [];
for(var i=0; i<result.length; i++) {
if(result[i].Abundance >= this.cutoff) {
filtered_result.push(result[i]);
}
}
return filtered_result;
}
} |