-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVirus.java
217 lines (190 loc) · 4.79 KB
/
Virus.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
/* Virus infection that has genotype, phenotype and ancestry */
import java.util.*;
public class Virus {
// simulation fields
private Virus parent;
private Phenotype phenotype;
private double birth; // measured in years relative to burnin
private int deme;
// additional reconstruction fields
private boolean marked;
private boolean trunk; // fill this at the end of the simulation
private List<Virus> children = new ArrayList<Virus>(0); // will be void until simulation ends
private double layout;
private int coverage; // how many times this Virus has been covered in tracing the tree backwards
// initialization
public Virus() {
phenotype = PhenotypeFactory.makeVirusPhenotype();
birth = Parameters.getDate();
}
// replication, copies the virus, but remembers the ancestry
public Virus(Virus v, int d) {
parent = v;
phenotype = v.getPhenotype();
birth = Parameters.getDate();
deme = d;
}
public Virus(Virus v, int d, Phenotype p) {
parent = v;
phenotype = p;
birth = Parameters.getDate();
deme = d;
}
public Virus(int d, Phenotype p) {
parent = null;
phenotype = p;
birth = Parameters.getDate();
deme = d;
}
// methods
public Phenotype getPhenotype() {
return phenotype;
}
public void setPhenotype(Phenotype p) {
phenotype = p;
}
public double getBirth() {
return birth;
}
public Virus getParent() {
return parent;
}
public void setParent(Virus v) {
parent = v;
}
public boolean isTrunk() {
return trunk;
}
public void makeTrunk() {
trunk = true;
}
public void mark() {
marked = true;
}
public boolean isMarked() {
return marked;
}
public int getDeme() {
return deme;
}
public double getLayout() {
return layout;
}
public void setLayout(double y) {
layout = y;
}
public int getCoverage() {
return coverage;
}
public void incrementCoverage() {
coverage++;
}
// add virus node as child if does not already exist
public void addChild(Virus v) {
if (!children.contains(v)) {
children.add(v);
}
}
public int getNumberOfChildren() {
return children.size();
}
public List<Virus> getChildren() {
return children;
}
public boolean isTip() {
return getNumberOfChildren() == 0 ? true : false;
}
// returns a mutated copy, original virus left intact
public Virus mutate() {
Phenotype mutP = phenotype.mutate(); // mutated copy
Virus mutV = new Virus(this,deme,mutP);
return mutV;
}
public Virus commonAncestor(Virus virusB) {
// Algorithm proceeds by recursively visiting parents.
// It terminates when either lineage arrives at a node already in the ancestry set,
// which must have been already visited by the other lineage and thus represents
// a common ancestor.
assert(virusB != null);
if(virusB == this) {
return this;
}
Virus lineageA = this;
Virus lineageB = virusB;
Set<Virus> ancestrySet = new HashSet<>();
ancestrySet.add(lineageA);
ancestrySet.add(lineageB);
while(lineageA != null || lineageB != null) {
if(lineageA != null) {
lineageA = lineageA.getParent();
if(lineageA != null && !ancestrySet.add(lineageA)) {
return lineageA;
}
}
if(lineageB != null) {
lineageB = lineageB.getParent();
if(lineageB != null && !ancestrySet.add(lineageB)) {
return lineageB;
}
}
}
return null;
}
public double distance(Virus virusB) {
Virus ancestor = commonAncestor(virusB);
if (ancestor != null) {
double distA = getBirth() - ancestor.getBirth();
double distB = virusB.getBirth() - ancestor.getBirth();
return distA + distB;
}
else {
return 0;
}
}
public double antigenicDistance(Virus virusB) {
return phenotype.distance(virusB.getPhenotype());
}
// is there a coalescence event within x amount of time? (measured in years)
public double coalescence(Virus virusB, double windowTime) {
Virus lineageA = this;
Virus lineageB = virusB;
Set<Virus> ancestry = new HashSet<Virus>();
double success = 0.0;
double startTime = lineageA.getBirth();
double time = startTime;
while (time > startTime - windowTime) {
if (lineageA.getParent() != null) {
lineageA = lineageA.getParent();
time = lineageA.getBirth();
ancestry.add(lineageA);
}
else {
break;
}
}
startTime = lineageB.getBirth();
time = startTime;
while (time > startTime - windowTime) {
if (lineageB.getParent() != null) {
lineageB = lineageB.getParent();
time = lineageB.getBirth();
if (!ancestry.add(lineageB)) {
success = 1.0;
break;
}
}
else {
break;
}
}
return success;
}
// this is the interval from this virus's birth back to its parent's birth
public double serialInterval() {
Virus p = getParent();
return getBirth() - p.getBirth();
}
public String toString() {
return Integer.toHexString(this.hashCode());
}
}