-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfiltering.js
598 lines (551 loc) · 15.9 KB
/
filtering.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import crypto from "crypto";
let revertant_mutations_set = null;
let cachedChildrenArray = null;
const getNumericFilterFunction = (number_method, number_value) => {
if (number_method === "==") {
return (x) => x === number_value;
}
if (number_method === ">") {
return (x) => x > number_value;
}
if (number_method === "<") {
return (x) => x < number_value;
}
if (number_method === ">=") {
return (x) => x >= number_value;
}
if (number_method === "<=") {
return (x) => x <= number_value;
}
throw new Error("Invalid number_method: " + number_method);
};
const getRevertantMutationsSet = (all_data, node_to_mut, mutations) => {
const root = all_data.find((node) => node.node_id === node.parent_id);
const root_mutations = node_to_mut[root.node_id];
const all_genes = [...new Set(mutations.map((m) => m.gene))];
const gene_sequence = Object.fromEntries(all_genes.map((g) => [g, {}]));
root_mutations.forEach((mut) => {
const m = mutations[mut];
gene_sequence[m.gene][m.residue_pos] = m.new_residue;
});
const revertant_mutations = mutations.filter(
(m) =>
m.gene in gene_sequence &&
gene_sequence[m.gene][m.residue_pos] === m.new_residue &&
m.new_residue !== m.previous_residue
);
return new Set(revertant_mutations.map((m) => m.mutation_id));
};
const count_per_hash = {};
const reduceOverPlotting = (input, precisionX, precisionY, xType) => {
const included_points = {};
precisionX = precisionX / 5;
console.log(
"REDUCING20",
"precisionX:",
precisionX,
"precisionY:",
precisionY
);
const filtered = input.filter((node) => {
const rounded_x = Math.round(node[xType] * precisionX) / precisionX;
const rounded_y = Math.round(node.y * precisionY) / precisionY;
if (included_points[rounded_x]) {
if (included_points[rounded_x][rounded_y]) {
return false;
} else {
included_points[rounded_x][rounded_y] = 1;
return true;
}
} else {
included_points[rounded_x] = { [rounded_y]: 1 };
return true;
}
});
return filtered;
};
function binary_search_for_insertion_point(values, search) {
// Returns where in values to insert search to maintain sorted order.
let low = 0;
let high = values.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (values[mid] < search) {
low = mid + 1;
} else if (values[mid] > search) {
high = mid - 1;
} else {
return mid;
}
}
return low;
}
function filter(input, y_positions, min_y, max_y) {
// do binary search for min_y and max_y
const min_y_index = binary_search_for_insertion_point(y_positions, min_y);
const max_y_index = binary_search_for_insertion_point(y_positions, max_y);
console.log("min_y_index:", min_y_index);
console.log("max_y_index:", max_y_index);
// Return sliced input array
return input.slice(min_y_index, max_y_index + 1);
}
function search(input, search_spec) {}
const addParents = (data, filtered) => {
const start_time = Date.now();
const selected_node_ids = filtered.map((node) => node.node_id);
// creat a set to keep track of selected_node_ids
const selected_node_ids_set = new Set(selected_node_ids);
const starting_size = filtered.length;
for (let i = 0; i < selected_node_ids.length; i++) {
const node_id = selected_node_ids[i];
const parent_id = data[node_id].parent_id;
//console.log(i);
// if parent_id is not in selected_node_ids, then add it
if (!selected_node_ids_set.has(parent_id)) {
selected_node_ids_set.add(parent_id);
selected_node_ids.push(parent_id);
//console.log("adding parent:", parent_id);
//console.log("New length is", selected_node_ids.length);
}
}
const with_parents = data.filter((node) =>
selected_node_ids_set.has(node.node_id)
);
const final_size = with_parents.length;
console.log("Adding parents took " + (Date.now() - start_time) + "ms.");
console.log("Went from " + starting_size + " to " + final_size + " nodes.");
return with_parents;
};
function getPrecision(min_y, max_y) {
const precision = 2000.0 / (max_y - min_y);
return precision;
}
function getNodes(data, y_positions, min_y, max_y, min_x, max_x, xType) {
console.log("GETNODES", min_y, max_y, min_x, max_x, xType);
const start_time = Date.now();
// get min_x, max_x, min_y, max_y from URL
const filtered =
min_y !== undefined ? filter(data, y_positions, min_y, max_y) : data;
const time2 = Date.now();
console.log("Filtering took " + (time2 - start_time) + "ms.");
console.log("Min_y:", min_y, "Max_y:", max_y);
const reduced_leaves = reduceOverPlotting(
filtered.filter((node) => node.num_tips == 1),
getPrecision(min_x, max_x),
getPrecision(min_y, max_y),
xType
);
const time3 = Date.now();
console.log("Reducing took " + (time3 - time2) + "ms.");
const reduced = addParents(data, reduced_leaves);
return reduced;
}
function searchFiltering({
data,
spec,
mutations,
node_to_mut,
all_data,
cache_helper,
}) {
const spec_copy = { ...spec };
spec_copy.key = "cache";
const hash_spec = crypto
.createHash("md5")
.update(JSON.stringify(spec_copy))
.digest("hex")
.slice(0, 8);
if (cache_helper && cache_helper.retrieve_from_cache) {
const cached_ids = cache_helper.retrieve_from_cache(hash_spec);
if (cached_ids !== undefined) {
console.log("Found cached data");
return cached_ids.map((id) => all_data[id]);
}
}
const result = searchFilteringIfUncached({
data,
spec,
mutations,
node_to_mut,
all_data,
cache_helper,
});
if (cache_helper && cache_helper.store_in_cache) {
cache_helper.store_in_cache(
hash_spec,
result.map((node) => node.node_id)
);
}
return result;
}
function searchFilteringIfUncached({
data,
spec,
mutations,
node_to_mut,
all_data,
cache_helper,
}) {
if (spec.type == "boolean") {
if (spec.boolean_method == "and") {
if (spec.subspecs.length == 0) {
return [];
}
let workingData = data;
spec.subspecs.forEach((subspec) => {
const new_results = new Set(
searchFiltering({
data: all_data,
spec: subspec,
mutations: mutations,
node_to_mut: node_to_mut,
all_data: all_data,
cache_helper: cache_helper,
})
);
workingData = workingData.filter((n) => new_results.has(n));
});
return workingData;
}
if (spec.boolean_method == "or") {
if (spec.subspecs.length == 0) {
return [];
}
let workingData = new Set();
spec.subspecs.forEach((subspec) => {
const results = searchFiltering({
data: all_data,
spec: subspec,
mutations: mutations,
node_to_mut: node_to_mut,
all_data: all_data,
cache_helper: cache_helper,
});
workingData = new Set([...workingData, ...results]);
});
return Array.from(workingData);
}
if (spec.boolean_method == "not") {
let negatives_set = new Set();
spec.subspecs.forEach((subspec) => {
const results = searchFiltering({
data: all_data,
spec: subspec,
mutations: mutations,
node_to_mut: node_to_mut,
all_data: all_data,
cache_helper: cache_helper,
});
negatives_set = new Set([...negatives_set, ...results]);
});
return data.filter((node) => !negatives_set.has(node));
}
}
console.log(spec);
let filtered;
if (["text_match", "text_exact"].includes(spec.method) && spec.text === "") {
return [];
}
if (spec.position) {
spec.position = parseInt(spec.position);
}
if (spec.method === "text_match") {
// case insensitive
spec.text = spec.text.toLowerCase();
filtered = data.filter(
(node) =>
node[spec.type] && node[spec.type].toLowerCase().includes(spec.text)
);
return filtered;
} else if (spec.method === "text_exact") {
// case insensitive
spec.text = spec.text.toLowerCase();
filtered = data.filter(
(node) => node[spec.type] && node[spec.type].toLowerCase() === spec.text
);
return filtered;
} else if (spec.method === "text_per_line") {
// case insensitive
const possible_matches = new Set(
spec.text
.toLowerCase()
.split("\n")
.map((line) => {
return line.trim();
})
.filter((line) => line !== "")
);
filtered = data.filter((node) => {
if (node[spec.type]) {
const to_test = node[spec.type].toLowerCase().trim();
//console.log(to_test);
// check if node's spec type is in possible_matches
return possible_matches.has(to_test);
} else {
return false;
}
});
return filtered;
} else if (spec.method === "mutation") {
const relevant_mutations = mutations
.filter((mutation) => {
return (
mutation &&
mutation.gene === spec.gene &&
mutation.residue_pos === spec.position &&
(spec.new_residue === "any" ||
mutation.new_residue === spec.new_residue)
);
})
.map((mutation) => mutation.mutation_id);
console.log("relevant_mutations:", relevant_mutations);
const relevant_mutations_set = new Set(relevant_mutations);
//console.log("node_to_mut:", node_to_mut);
//console.log("NODE", data[0]);
filtered = data.filter(
(node) =>
node_to_mut[node.node_id].some((mutation_id) =>
relevant_mutations_set.has(mutation_id)
) && node.num_tips > spec.min_tips
);
//console.log("filtered:", filtered);
return filtered;
} else if (spec.method === "revertant") {
if (!all_data) {
all_data = data;
}
if (revertant_mutations_set === null) {
revertant_mutations_set = getRevertantMutationsSet(
all_data,
node_to_mut,
mutations
);
}
filtered = data.filter(
(node) =>
node.num_tips > spec.min_tips &&
node_to_mut[node.node_id].some((mutation_id) =>
revertant_mutations_set.has(mutation_id)
)
);
//console.log("filtered:", filtered);
return filtered;
} else if (spec.method === "genotype") {
const genotype = {
gene: spec.gene,
position: spec.position,
new_residue: spec.new_residue,
};
return filterByGenotype(data, genotype, mutations, node_to_mut, all_data);
} else if (spec.method === "number") {
if (spec.number == "") {
return [];
}
const number_value = parseFloat(spec.number);
const filterFunc = getNumericFilterFunction(
spec.number_method,
number_value
);
filtered = data.filter((node) => filterFunc(node[spec.type]));
return filtered;
}
return [];
}
function singleSearch({
data,
spec,
min_y,
max_y,
y_positions,
mutations,
node_to_mut,
xType,
min_x,
max_x,
cache_helper,
}) {
const text_spec = JSON.stringify(spec);
const max_to_return = 10000;
const hash_spec = crypto
.createHash("md5")
.update(text_spec)
.digest("hex")
.slice(0, 8);
let filtered = null;
if (count_per_hash[hash_spec] === undefined) {
filtered = searchFiltering({
data,
spec,
mutations,
node_to_mut,
all_data: data,
cache_helper,
});
count_per_hash[hash_spec] = filtered.length;
}
const num_returned = count_per_hash[hash_spec];
let result;
if (num_returned > max_to_return) {
const filtered = searchFiltering({
data,
spec,
mutations,
node_to_mut,
all_data: data,
cache_helper,
});
// TODO if we ensured all searches maintained order we could use binary search here
const filtered_cut = filtered.filter(
(node) => node.y < max_y && node.y > min_y
);
console.log("length of filtered_cut:", filtered_cut.length);
console.log("min_y:", min_y, "max_y:", max_y);
// reduce overplotting:
const reduced = reduceOverPlotting(
filtered_cut,
getPrecision(min_x, max_x),
getPrecision(min_y, max_y),
xType
);
result = {
type: "filtered",
data: reduced,
total_count: num_returned,
};
} else {
if (filtered === null) {
filtered = searchFiltering({
data,
spec,
mutations,
node_to_mut,
all_data: data,
cache_helper,
});
}
result = {
type: "complete",
data: filtered,
total_count: num_returned,
};
}
return result;
}
function addMutations(input, mutations, node_to_mut) {
const start_time = new Date();
const result = input.map((node) => ({
...node,
mutations: node_to_mut[node.node_id],
}));
console.log("addMutations:", new Date() - start_time);
return result;
}
function getChildrenArray(input) {
if (cachedChildrenArray) {
return cachedChildrenArray;
}
const start_time = new Date();
const childrenArray = input.map((x) => []);
input.forEach((node) => {
if (node.parent_id !== node.node_id) {
childrenArray[node.parent_id].push(node.node_id);
}
});
console.log("getChildrenArray:", new Date() - start_time);
cachedChildrenArray = childrenArray;
return childrenArray;
}
const preOrder = (input, node_id) => {
const output = [];
const childrenArray = getChildrenArray(input);
const stack = [node_id];
while (stack.length > 0) {
const node_id = stack.pop();
output.push(node_id);
childrenArray[node_id].forEach((child_id) => {
stack.push(child_id);
});
}
return output;
};
const getTipAtts = (input, node_id, attribute) => {
const childrenArray = getChildrenArray(input);
const allChildren = preOrder(input, node_id);
const allTips = allChildren.filter((x) => childrenArray[x].length === 0);
const allAtts = allTips.map((x) => input[x][attribute]);
return allAtts;
};
const filterByGenotype = (data, genotype, mutations, node_to_mut, all_data) => {
const genotype_cache = {};
const { gene, position, new_residue } = genotype;
const relevant_mutations = mutations.filter((mutation) => {
return (
mutation && mutation.gene === gene && mutation.residue_pos === position
);
});
const positive_mutations = new Set(
relevant_mutations
.filter((mutation) => mutation.new_residue === new_residue)
.map((m) => m.mutation_id)
);
// if no positive mutations then return empty array
if (positive_mutations.size === 0) {
return [];
}
const negative_mutations = new Set(
relevant_mutations
.filter((mutation) => mutation.new_residue !== new_residue)
.map((m) => m.mutation_id)
);
const output = data.filter((node) => {
// console.log("node:",node);
let cur_node = node;
const to_label = [];
while (true) {
// console.log("cur_node:",cur_node);
to_label.push(cur_node.node_id);
const cache_value = genotype_cache[cur_node.node_id];
const is_positive =
cache_value === true ||
node_to_mut[cur_node.node_id].some((mutation_id) =>
positive_mutations.has(mutation_id)
);
if (is_positive) {
// console.log("positive");
to_label.forEach((node_id) => {
genotype_cache[node_id] = true;
});
return true;
}
const is_negative =
cache_value === false ||
node_to_mut[cur_node.node_id].some((mutation_id) =>
negative_mutations.has(mutation_id)
);
if (is_negative) {
// console.log("negative");
to_label.forEach((node_id) => {
genotype_cache[node_id] = false;
});
return false;
}
if (cur_node.parent_id === cur_node.node_id) {
break;
}
cur_node = all_data[cur_node.parent_id];
}
// If we get to this point we reached the root node and still didn't find anything, so just return
return false;
});
return output;
};
export default {
reduceOverPlotting,
filter,
search,
addParents,
getNodes,
singleSearch,
addMutations,
getTipAtts,
};