forked from trvrb/antigen
/
VirusTree.java
658 lines (520 loc) · 16.5 KB
/
VirusTree.java
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/* Stores a list of Viruses that have sampled during the course of the simulation */
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class VirusTree {
// fields
private static Virus root = Parameters.urVirus;
private static List<Virus> tips = new ArrayList<Virus>();
public static double xMin;
public static double xMax;
public static double yMin;
public static double yMax;
public static double zMin;
public static double zMax;
static final Comparator<Virus> descendantOrder = new Comparator<Virus>() {
public int compare(Virus v1, Virus v2) {
Integer descendantsV1 = new Integer(getNumberOfDescendants(v1));
Integer descendantsV2 = new Integer(getNumberOfDescendants(v2));
return descendantsV1.compareTo(descendantsV2);
}
};
// static methods
public static void add(Virus v) {
tips.add(v);
}
public static void clear() {
tips.clear();
}
public static List<Virus> getTips() {
return tips;
}
public static Virus getRoot() {
return root;
}
// return a random tip that lies between year from and year to
public static Virus getRandomTipFromTo(double from, double to) {
// fill temporary list
List<Virus> select = new ArrayList<Virus>();
for (Virus v : tips) {
double x = v.getBirth();
if (x >= from && x < to) {
select.add(v);
}
}
// pull random virus from this list
Virus rV = null;
if (select.size() > 0) {
int index = Random.nextInt(0,select.size()-1);
rV = select.get(index);
}
return rV;
}
public static int getDemeCount(int d) {
int count = 0;
for (Virus v : tips) {
if (v.getDeme() == d) {
count++;
}
}
return count;
}
// work backwards for each sample filling the children lists
public static void fillBackward() {
for (Virus child : tips) {
Virus parent = child.getParent();
while (parent != null) {
parent.addChild(child);
parent.incrementCoverage();
child = parent;
parent = child.getParent();
}
}
}
public static void dropTips() {
List<Virus> reducedTips = new ArrayList<Virus>();
for (Virus v : tips) {
if (Random.nextBoolean(Parameters.treeProportion)) {
reducedTips.add(v);
}
}
tips = reducedTips;
}
// marking to by time, not proportional to prevalence
public static void markTips() {
// for (Virus v : tips) {
// if (Random.nextBoolean(Parameters.treeProportion)) {
// while (v.getParent() != null) {
// v.mark();
// v = v.getParent();
// }
// }
// }
for (double i = 0; i < Parameters.getDate(); i+=0.1) {
Virus v = getRandomTipFromTo(i,i+0.1);
if (v != null) {
while (v.getParent() != null) {
v.mark();
v = v.getParent();
}
}
}
}
// prune tips
public static void pruneTips() {
List<Virus> reducedTips = new ArrayList<Virus>();
for (int d = 0; d < Parameters.demeCount; d++) {
double keepProportion = (double) Parameters.tipSamplesPerDeme / (double) getDemeCount(d);
for (Virus v : tips) {
if (Random.nextBoolean(keepProportion) && v.getDeme() == d) {
reducedTips.add(v);
}
}
}
tips = reducedTips;
}
// returns virus v and all its descendents via a depth-first traversal
public static List<Virus> postOrderNodes(Virus v) {
List<Virus> vNodes = new ArrayList<Virus>();
vNodes.add(v);
vNodes = postOrderChildren(vNodes);
return vNodes;
}
public static List<Virus> postOrderNodes() {
return postOrderNodes(root);
}
// returns virus v and all its descendents via a depth-first traversal
public static List<Virus> postOrderChildren(List<Virus> vNodes) {
Virus last = vNodes.get(vNodes.size()-1);
for (Virus child : last.getChildren()) {
vNodes.add(child);
postOrderChildren(vNodes);
}
return vNodes;
}
// Count total descendents of a Virus, working through its children and its children's children
public static int getNumberOfDescendants(Virus v) {
int numberOfDescendants = v.getNumberOfChildren();
for (Virus child : v.getChildren()) {
numberOfDescendants += getNumberOfDescendants(child);
}
return numberOfDescendants;
}
public static int getNumberOfDescendants() {
return getNumberOfDescendants(root);
}
// sorts children lists so that first member is child with more descendents than second member
public static void sortChildrenByDescendants(Virus v) {
List<Virus> children = v.getChildren();
Collections.sort(children, descendantOrder);
for (Virus child : children) {
sortChildrenByDescendants(child);
}
}
public static void sortChildrenByDescendants() {
sortChildrenByDescendants(root);
}
// sets Virus layout based on a postorder traversal
public static void setLayoutByDescendants() {
List<Virus> vNodes = postOrderNodes();
// set layout of tips based on traversal
double y = 0;
for (Virus v : vNodes) {
// if (tips.contains(v)) {
if (v.isTip()) {
v.setLayout(y);
y++;
}
}
// update layout of internal nodes
Collections.reverse(vNodes);
for (Virus v : vNodes) {
if (v.getNumberOfChildren() > 0) {
double mean = 0;
for (Virus child : v.getChildren()) {
mean += child.getLayout();
}
mean /= v.getNumberOfChildren();
v.setLayout(mean);
}
}
}
// looks at a virus and its grandparent, if traits are identical and there is no branching
// then make virus child rather than grandchild
// returns v.parent after all is said and done
public static Virus collapse(Virus v) {
Virus vp = null;
Virus vgp = null;
if (v.getParent() != null) {
vp = v.getParent();
if (vp.getParent() != null) {
vgp = vp.getParent();
}
}
if (vp != null && vgp != null) {
if (vp.getNumberOfChildren() == 1 && v.getPhenotype() == vp.getPhenotype() && v.isTrunk() == vp.isTrunk() && v.getDeme() == vp.getDeme()) {
List<Virus> vgpChildren = vgp.getChildren();
int vpIndex = vgpChildren.indexOf(vp);
if (vpIndex >= 0) {
// replace virus as child of grandparent
vgpChildren.set(vpIndex, v);
// replace grandparent as parent of virus
v.setParent(vgp);
// erase parent
vp = null;
}
}
}
return v.getParent();
}
// walks backward using the list of tips, collapsing where possible
public static void streamline() {
for (Virus v : tips) {
Virus vp = v;
while (vp != null) {
vp = collapse(vp);
}
}
}
// rotate the 2d euclidean space using PCA, returning an x-axis with maximum variance
public static void rotate() {
if (Parameters.phenotypeSpace == "geometric") {
// load a 2d array with phenotypes
List<Virus> virusList = postOrderNodes();
int n = virusList.size();
int m = 2;
double[][] input = new double[n][m];
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
GeometricPhenotype p = (GeometricPhenotype) v.getPhenotype();
double x = p.getTraitA();
double y = p.getTraitB();
input[i][0] = x;
input[i][1] = y;
}
// project this array
double[][] projected = SimplePCA.project(input);
// reset phenotypes based on projection
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
GeometricPhenotype p = (GeometricPhenotype) v.getPhenotype();
double x = projected[i][0];
double y = projected[i][1];
p.setTraitA(x);
p.setTraitB(y);
}
}
if (Parameters.phenotypeSpace == "geometric3d") {
// load a 2d array with phenotypes
List<Virus> virusList = postOrderNodes();
int n = virusList.size();
int m = 3;
double[][] input = new double[n][m];
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
GeometricPhenotype3D p = (GeometricPhenotype3D) v.getPhenotype();
double x = p.getTraitA();
double y = p.getTraitB();
double z = p.getTraitC();
input[i][0] = x;
input[i][1] = y;
input[i][2] = z;
}
// project this array
double[][] projected = SimplePCA.project3D(input);
// reset phenotypes based on projection
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
GeometricPhenotype3D p = (GeometricPhenotype3D) v.getPhenotype();
double x = projected[i][0];
double y = projected[i][1];
double z = projected[i][2];
p.setTraitA(x);
p.setTraitB(y);
p.setTraitC(z);
}
}
}
// flips the 2d euclidean space so that first sample is always to the left of the last sample
public static void flip() {
if (Parameters.phenotypeSpace == "geometric") {
List<Virus> virusList = postOrderNodes();
int n = virusList.size();
// find first and last virus
Virus firstVirus = virusList.get(0);
Virus lastVirus = virusList.get(0);
double firstDate = firstVirus.getBirth();
double lastDate = lastVirus.getBirth();
for (Virus v : virusList) {
if (v.getBirth() < firstDate) {
firstDate = v.getBirth();
firstVirus = v;
}
if (v.getBirth() > lastDate) {
lastDate = v.getBirth();
lastVirus = v;
}
}
// is the x-value of first virus greater than the x-value of last virus?
// if so, flip
GeometricPhenotype p = (GeometricPhenotype) firstVirus.getPhenotype();
double firstX = p.getTraitA();
p = (GeometricPhenotype) lastVirus.getPhenotype();
double lastX = p.getTraitA();
if (firstX > lastX) {
// I think that postOrderNodes() has replicates in it, need to go through some hoops because of this
double[] input = new double[n];
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
p = (GeometricPhenotype) v.getPhenotype();
input[i] = p.getTraitA();;
}
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
p = (GeometricPhenotype) v.getPhenotype();
double x = -1*input[i];
p.setTraitA(x);
}
}
}
if (Parameters.phenotypeSpace == "geometric3d") {
List<Virus> virusList = postOrderNodes();
int n = virusList.size();
// find first and last virus
Virus firstVirus = virusList.get(0);
Virus lastVirus = virusList.get(0);
double firstDate = firstVirus.getBirth();
double lastDate = lastVirus.getBirth();
for (Virus v : virusList) {
if (v.getBirth() < firstDate) {
firstDate = v.getBirth();
firstVirus = v;
}
if (v.getBirth() > lastDate) {
lastDate = v.getBirth();
lastVirus = v;
}
}
// is the x-value of first virus greater than the x-value of last virus?
// if so, flip
GeometricPhenotype3D p = (GeometricPhenotype3D) firstVirus.getPhenotype();
double firstX = p.getTraitA();
p = (GeometricPhenotype3D) lastVirus.getPhenotype();
double lastX = p.getTraitA();
if (firstX > lastX) {
// I think that postOrderNodes() has replicates in it, need to go through some hoops because of this
double[] input = new double[n];
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
p = (GeometricPhenotype3D) v.getPhenotype();
input[i] = p.getTraitA();;
}
for (int i = 0; i < n; i++) {
Virus v = virusList.get(i);
p = (GeometricPhenotype3D) v.getPhenotype();
double x = -1*input[i];
p.setTraitA(x);
}
}
}
}
// walks through list of nodes and update min and max ranges appropriately
public static void updateRange() {
xMin = 0.0;
xMax = 0.0;
yMin = 0.0;
yMax = 0.0;
zMin = 0.0;
zMax = 0.0;
if (Parameters.phenotypeSpace == "geometric") {
for (Virus v : postOrderNodes()) {
GeometricPhenotype p = (GeometricPhenotype) v.getPhenotype();
double x = p.getTraitA();
double y = p.getTraitB();
if (xMin > x) { xMin = x; }
if (xMax < x) { xMax = x; }
if (yMin > y) { yMin = y; }
if (yMax < y) { yMax = y; }
}
}
if (Parameters.phenotypeSpace == "geometric3d") {
for (Virus v : postOrderNodes()) {
GeometricPhenotype3D p = (GeometricPhenotype3D) v.getPhenotype();
double x = p.getTraitA();
double y = p.getTraitB();
double z = p.getTraitC();
if (xMin > x) { xMin = x; }
if (xMax < x) { xMax = x; }
if (yMin > y) { yMin = y; }
if (yMax < y) { yMax = y; }
if (zMin > z) { zMin = z; }
if (zMax < z) { zMax = z; }
}
}
xMin = Math.floor(xMin) - 10;
xMax = Math.ceil(xMax) + 10;
yMin = Math.floor(yMin) - 10;
yMax = Math.ceil(yMax) + 10;
zMin = Math.floor(zMin) - 10;
zMax = Math.ceil(zMax) + 10;
}
public static void printRange() {
try {
File rangeFile = new File("out.range");
rangeFile.delete();
rangeFile.createNewFile();
PrintStream rangeStream = new PrintStream(rangeFile);
rangeStream.printf("%.4f,%.4f,%.4f,%.4f,%.4f,%.4f\n", xMin, xMax, yMin, yMax, zMin, zMax);
rangeStream.close();
} catch(IOException ex) {
System.out.println("Could not write to file");
System.exit(0);
}
}
public static void printTips() {
try {
File tipFile = new File("out.tips");
tipFile.delete();
tipFile.createNewFile();
PrintStream tipStream = new PrintStream(tipFile);
tipStream.printf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n", "name", "year", "trunk", "tip", "mark", "location", "layout", "ag1", "ag2");
for (int i = 0; i < tips.size(); i++) {
Virus v = tips.get(i);
tipStream.printf("\"%s\",%.4f,%d,%d,%d,%d,%.4f,%s\n", v, v.getBirth(), v.isTrunk()?1:0, v.isTip()?1:0, v.isMarked()?1:0, v.getDeme(), v.getLayout(), v.getPhenotype());
}
tipStream.close();
} catch(IOException ex) {
System.out.println("Could not write to file");
System.exit(0);
}
}
public static void printBranches() {
try {
File branchFile = new File("out.branches");
branchFile.delete();
branchFile.createNewFile();
PrintStream branchStream = new PrintStream(branchFile);
for (Virus v : postOrderNodes()) {
if (v.getParent() != null) {
Virus vp = v.getParent();
branchStream.printf("{\"%s\",%.4f,%d,%d,%d,%d,%.4f,%s}\t", v, v.getBirth(), v.isTrunk()?1:0, v.isTip()?1:0, v.isMarked()?1:0, v.getDeme(), v.getLayout(), v.getPhenotype());
branchStream.printf("{\"%s\",%.4f,%d,%d,%d,%d,%.4f,%s}\t", vp, vp.getBirth(), vp.isTrunk()?1:0, vp.isTip()?1:0, v.isMarked()?1:0, vp.getDeme(), vp.getLayout(), vp.getPhenotype());
branchStream.printf("%d\n", vp.getCoverage());
}
}
branchStream.close();
} catch(IOException ex) {
System.out.println("Could not write to file");
System.exit(0);
}
}
public static int sideBranchMutations() {
int count = 0;
for (Virus v : postOrderNodes()) {
if (v.getParent() != null && v.getBirth() < Parameters.getDate() - Parameters.yearsFromMK) {
Virus vp = v.getParent();
if (!v.isTrunk() && !vp.isTrunk() && v.getPhenotype() != vp.getPhenotype()) {
count++;
}
}
}
return count;
}
public static double sideBranchOpportunity() {
double time = 0;
for (Virus v : postOrderNodes()) {
if (v.getParent() != null && v.getBirth() < Parameters.getDate() - Parameters.yearsFromMK) {
Virus vp = v.getParent();
if (!v.isTrunk() && !vp.isTrunk()) {
time += v.getBirth() - vp.getBirth();
}
}
}
return time;
}
public static int trunkMutations() {
int count = 0;
for (Virus v : postOrderNodes()) {
if (v.getParent() != null && v.getBirth() < Parameters.getDate() - Parameters.yearsFromMK) {
Virus vp = v.getParent();
if (v.isTrunk() && vp.isTrunk() && v.getPhenotype() != vp.getPhenotype()) {
count++;
}
}
}
return count;
}
public static double trunkOpportunity() {
double time = 0;
for (Virus v : postOrderNodes()) {
if (v.getParent() != null && v.getBirth() < Parameters.getDate() - Parameters.yearsFromMK) {
Virus vp = v.getParent();
if (v.isTrunk() && vp.isTrunk()) {
time += v.getBirth() - vp.getBirth();
}
}
}
return time;
}
public static void printMK() {
try {
File mkFile = new File("out.mk");
mkFile.delete();
mkFile.createNewFile();
PrintStream mkStream = new PrintStream(mkFile);
mkStream.printf("sideBranchMut\tsideBranchOpp\tsideBranchRate\ttrunkMut\ttrunkOpp\ttrunkRate\tmk\n");
int sideBranchMut = sideBranchMutations();
double sideBranchOpp = sideBranchOpportunity();
double sideBranchRate = sideBranchMut / sideBranchOpp;
int trunkMut = trunkMutations();
double trunkOpp = trunkOpportunity();
double trunkRate = trunkMut / trunkOpp;
double mk = trunkRate / sideBranchRate;
mkStream.printf("%d,%.4f,%.4f,%d,%.4f,%.4f,%.4f\n", sideBranchMut, sideBranchOpp, sideBranchRate, trunkMut, trunkOpp, trunkRate, mk);
mkStream.close();
} catch(IOException ex) {
System.out.println("Could not write to file");
System.exit(0);
}
}
}