-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.js
371 lines (334 loc) · 10.8 KB
/
binary.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const _ = require('lodash');
// EXAMPLE BINNED SPECTRA
//
const groupBy = function (data, key) { // `data` is an array of objects, `key` is the key (or property accessor) to group by
// reduce runs this anonymous function on each element of `data` (the `item` parameter,
// // returning the `storage` parameter at the end
return data.reduce((storage, item) => {
// // get the first instance of the key by which we're grouping
const group = item[key];
//
// // set `storage` for this instance of group to the outer scope (if not empty) or initialize it
storage[group] = storage[group] || [];
//
// // add this item to its group within `storage`
storage[group].push(item);
//
// // return the updated storage to the reduce function, which will then loop through the next
return storage;
}, {}); // {} is the initial value of the storage
};
exports.groupBy = groupBy;
// [{"mz": v1, "intensity": v2}, ...]
// TODO needs a sorted array
var binarySearch = function (arr, target) {
const midpoint = Math.floor(arr.length / 2);
if (arr[midpoint] === target) {
return arr[midpoint];
}
if (arr.length === 1) {
return arr[0];
}
if (arr[midpoint] > target) {
return binarySearch(arr.slice(0, midpoint), target);
} if (arr[midpoint] < target) {
return binarySearch(arr.slice(midpoint), target);
}
};
full_merge = function (spec1_spec2, spec2_spec1) {
// first swap labels
spec2_spec1 = spec2_spec1.map((peakObj) => ({
id_1: peakObj.id_2,
id_2: peakObj.id_1,
mz_1: peakObj.mz_2,
mz_2: peakObj.mz_1,
intensity_1: peakObj.intensity_2,
intensity_2: peakObj.intensity_1,
}));
let mergedSpectrum = spec1_spec2.concat(spec2_spec1);
mergedSpectrum = mergedSpectrum.map((peakObj) => ({
id_1: peakObj.id_1,
intensity_1: peakObj.intensity_1,
mz_1: peakObj.mz_1,
id_2: peakObj.id_2,
intensity_2: peakObj.intensity_2,
mz_2: peakObj.mz_2,
id: `${peakObj.id_1}_${peakObj.id_2}`,
}));
const non_duplicated_merged_spectrum = _.uniqBy(mergedSpectrum, 'id');
let splice_non_matched = non_duplicated_merged_spectrum.filter((x) => (x.id_1 === -1) || (x.id_2 === -1));
const splice_matched = non_duplicated_merged_spectrum.filter((x) => (x.id_1 !== -1) && (x.id_2 !== -1));
const spliced_matched_add = _.uniqBy(splice_matched.map((peakObj) => ({
id_1: peakObj.id_1,
intensity_1: peakObj.intensity_1,
mz_1: peakObj.mz_1,
id_2: -1,
intensity_2: 0,
mz_2: -1,
})), 'id_1');
const spliced_matched_add2 = _.uniqBy(splice_matched.map((peakObj) => ({
id_2: peakObj.id_2,
intensity_2: peakObj.intensity_2,
mz_2: peakObj.mz_2,
id_1: -1,
intensity_1: 0,
mz_1: -1,
})), 'id_2');
splice_non_matched = spliced_matched_add
.concat(spliced_matched_add2)
.concat(splice_non_matched);
// remove the -1 stuff for external
const non_duplicated_merged_spectrum_without_non_matching = splice_matched;
let grp1 = groupBy(non_duplicated_merged_spectrum_without_non_matching, 'id_1');
// with entries -> every entry is a array
grp1 = Object.values(grp1).map((objPeak) => objPeak.reduce((prev, current) => (prev.intensity_1 + prev.intensity_2 > current.intensity_1 + current.intensity_2 ? prev : current), {}));
let grp2 = groupBy(grp1, 'id_2');
grp2 = Object.values(grp2).map((objPeak) => objPeak.reduce((prev, current) => (prev.intensity_1 + prev.intensity_2 > current.intensity_1 + current.intensity_2 ? prev : current), {}));
grp2 = grp2.concat(splice_non_matched);
grp1 = groupBy(grp2, 'id_1');
// return grp1["5"];
// with entries -> every entry is a array
grp1 = Object.values(grp1).map((objPeak) => {
if (objPeak[0].id_1 === -1) {
return objPeak;
}
return objPeak.reduce((prev, current) => (prev.id_2 > current.id_2 ? prev : current), {});
});
grp2 = groupBy([].concat(...grp1), 'id_2');
grp2 = Object.values(grp2).map((objPeak) => {
if (objPeak[0].id_2 === -1) {
return objPeak;
}
return objPeak.reduce((prev, current) => (prev.id_1 > current.id_1 ? prev : current), {});
});
const arr = [].concat(...grp2)
.map((peakObj) => ({
id_1: peakObj.id_1,
intensity_1: peakObj.intensity_1,
mz_1: peakObj.mz_1,
id_2: peakObj.id_2,
intensity_2: peakObj.intensity_2,
mz_2: peakObj.mz_2,
}));
return arr.filter((v, i, a) => a.findIndex((t) => (t.id_1 === v.id_1 && t.id_2 === v.id_2)) === i);
// I am very sorry :(
// TODO: simplify to oneliner
};
getClosestValues = function (a, x) {
let lo = -1; let
hi = a.length;
while (hi - lo > 1) {
const mid = Math.round((lo + hi) / 2);
if (a[mid] <= x) {
lo = mid;
} else {
hi = mid;
}
}
if (a[lo] == x) hi = lo;
return [a[lo], a[hi]];
};
getClosestValues_specF = function (property) {
return function (a, x) {
let lo = -1; let
hi = a.length;
while (hi - lo > 1) {
const mid = Math.round((lo + hi) / 2);
if (a[mid][property] <= x) {
lo = mid;
} else {
hi = mid;
}
}
if (a[lo] == x) hi = lo;
return [a[lo], a[hi]];
};
};
// x is a m/z value
getClosestValues_spec = function (a, x) {
let lo = -1; let
hi = a.length;
while (hi - lo > 1) {
const mid = Math.round((lo + hi) / 2);
if (a[mid].mz <= x) {
lo = mid;
} else {
hi = mid;
}
}
if (a[lo] == x) hi = lo;
return [a[lo], a[hi]];
};
getClosestValues_spec2_FACTORY = function (selection) {
return function (a, x) {
/*
* ppm part
*/
f = getClosestValues_specF(selection);
return f(a, x)
.filter((x) => x !== undefined)
.reduce((prev, next) => (Math.abs(prev[selection] - x) < Math.abs(next[selection] - x) ? prev : next), 0);
};
};
selectMostIntensePeak = function (mergedSpectrum) {
return mergedSpectrum.map((peakObj) => {
if (peakObj.id_1.length === 0) {
var mostIntense = {
mz: -1,
intensity: 0,
id: -1,
};
} else {
var mostIntense = peakObj.id_1.map((id, i) => ({
mz: peakObj.mz_1[i],
intensity: peakObj.intensity_1[i],
id: peakObj.id_1[i],
})).reduce((prev, current) => (prev.intensity > current.intensity ? prev : current));
}
return {
id_1: mostIntense.id,
intensity_1: mostIntense.intensity,
mz_1: mostIntense.mz,
id_2: peakObj.id_2,
intensity_2: peakObj.intensity_2,
mz_2: peakObj.mz_2,
};
});
};
getClosestValues_spec2 = getClosestValues_spec2_FACTORY('mz');
var binarySearch_spec = function (arr, target) {
const midpoint = Math.floor(arr.length / 2);
if (arr[midpoint].mz == target) {
return arr[midpoint];
}
if (arr.length == 1) {
return arr[0];
}
if (arr[midpoint].mz > target) {
return binarySearch_spec(arr.slice(0, midpoint), target);
} if (arr[midpoint].mz < target) {
return binarySearch_spec(arr.slice(midpoint), target);
}
};
my_sorter = function (attribute, type) {
return function (peak1, peak2) {
if (peak1[attribute] > peak2[attribute]) {
return type === 'asc' ? 1 : -1;
}
if (peak2[attribute] > peak1[attribute]) {
return type === 'asc' ? -1 : 1;
}
return 0;
};
};
/*
*peak 1 is always reference (we assume the bottom/predicted one
* checking for smaller or greater not equal case
*/
compare_ppm_FACTORY = function (property) {
return function (peak1, peak2, ppm) {
// asking if peak2 is close to peak1
error = 1 / Math.pow(10, 6) * ppm * peak1[property];
return (peak2.mz < peak1[property] + error && peak2.mz > peak1[property] - error);
};
};
/* property e.g. mz_2
* type ppm/Da
* value e.g. 10
*/
compare_FACTORY = function (property, type = 'ppm', value = 10) {
return function (peak1, peak2) {
// asking if peak2 is close to peak1
let error;
switch (type) {
case 'ppm':
error = 1 / Math.pow(10, 6) * value * peak1[property];
break;
case 'Da':
error = value;
break;
default:
error = 1;
throw 'Error';
}
return (peak2.mz <= (peak1[property] + error) && peak2.mz >= (peak1[property] - error));
};
};
exports.compare_ppm = compare_ppm_FACTORY('mz');
exports.my_sorter = my_sorter;
exports.compare_ppm_FACTORY = compare_ppm_FACTORY;
exports.compare_FACTORY = compare_FACTORY;
generate_searchF = function (spectrum, type = 'ppm', value = 10) {
return function (peak) {
getClosestValues_spec2F = getClosestValues_spec2_FACTORY('mz_2');
a = getClosestValues_spec2F(spectrum, peak.mz);
// compare_ppmF = compare_ppm_FACTORY('mz_2');
compare_F = compare_FACTORY('mz_2', type, value);
is_inside = compare_F(a, peak); // TODO correct here?
if (is_inside) {
a.intensity_1.push(peak.intensity);
a.mz_1.push(peak.mz);
a.id_1.push(peak.id);
return true;
}
return false;
// return true;
// return false;
};
};
add_exp_flag = function (peak) {
peak.intensity_2 = 0;
return (peak);
};
extract_mzs = function (prev, peak) {
prev.intensity_1.push(peak.exp_intensity);
prev.intensity_2.push(peak.intensity);
return (prev);
};
/**
* solves question of specrum_2 is how much part of 1
*/
binary_search_spectrum = function (spectrum_1, spectrum_2, type = 'ppm', value = 10) {
const sorter_asc_mz = my_sorter('mz', 'asc');
spectrum_2 = spectrum_2.sort(sorter_asc_mz);
spectrum_2 = spectrum_2.map((peak, i) => ({
intensity_2: peak.intensity,
mz_2: peak.mz,
id_2: i,
id_1: [],
mz_1: [],
intensity_1: [],
}));
const sorter_asc_intensity = my_sorter('intensity', 'asc');
spectrum_1 = spectrum_1.sort(sorter_asc_mz);
spectrum_1 = spectrum_1.map((peak, i) => ({
id: i,
mz: peak.mz,
intensity: peak.intensity,
}));
const f_peak = generate_searchF(spectrum_2, type, value);
spectrum_1.map(f_peak);
return (spectrum_2);
};
binary_full_merge = function (spectrum_1, spectrum_2) {
merge1 = binary_search_spectrum(spectrum_1, spectrum_2);
merge2 = binary_search_spectrum(spectrum_2, spectrum_1);
to_add_1 = merge2.intensity_1.filter((x, i) => x === 0);
to_add_2 = merge2.intensity_2.filter((x, i) => merge2.intensity_1[i] === 0);
merge1.intensity_1 = merge1.intensity_1.concat(to_add_2);
merge1.intensity_2 = merge1.intensity_2.concat(to_add_1);
return (merge1);
};
exports.generate_searchF = generate_searchF;
exports.add_exp_flag = add_exp_flag;
exports.extract_mzs = extract_mzs;
exports.binary_search_spectrum = binary_search_spectrum;
exports.binarySearch_spec = binarySearch_spec;
exports.binarySearch = binarySearch;
exports.getClosestValues = getClosestValues;
exports.getClosestValues_spec = getClosestValues_spec;
exports.getClosestValues_spec2 = getClosestValues_spec2;
exports.binary_full_merge = binary_full_merge;
exports.selectMostIntensePeak = selectMostIntensePeak;
exports.groupBy = groupBy;
exports.full_merge = full_merge;