forked from smirarab/ASTRAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBipartitionWeightCalculator.java
360 lines (312 loc) · 10.5 KB
/
BipartitionWeightCalculator.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
package phylonet.coalescent;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import phylonet.tree.model.Tree;
import phylonet.tree.model.sti.STITreeCluster;
// TODO: why extend the abstract? It doesn't seem to follow the same pattern exactly
class BipartitionWeightCalculator extends AbstractWeightCalculator<Tripartition> {
WQInference inference;
private WQDataCollection dataCollection;
private Integer[] geneTreesAsInts;
public BipartitionWeightCalculator(AbstractInference<Tripartition> inference,
Integer[] geneAsInts) {
super(false);
this.dataCollection = (WQDataCollection) inference.dataCollection;
this.inference = (WQInference) inference;
this.geneTreesAsInts = geneAsInts;
}
class Intersects {
long s0;
long s1;
long s2;
long s3;
public Intersects(long s0, long s1, long s2, long s3) {
this.s0 = s0;
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
public Intersects(Intersects side1, Intersects side2) {
this(side1.s0+side2.s0,
side1.s1+side2.s1,
side1.s2+side2.s2,
side1.s3+side2.s3);
}
public Intersects(Intersects other) {
this.s0 = other.s0;
this.s1 = other.s1;
this.s2 = other.s2;
this.s3 = other.s3;
}
public void addin(Intersects pop) {
this.s0 += pop.s0;
this.s1 += pop.s1;
this.s2 += pop.s2;
this.s3 += pop.s3;
}
public void subtract(Intersects pop) {
this.s0 -= pop.s0;
this.s1 -= pop.s1;
this.s2 -= pop.s2;
this.s3 -= pop.s3;
}
public String toString() {
return this.s0+","+this.s1+"|"+this.s2+","+this.s3;
}
public boolean isNotEmpty() {
return (this.s0 + this.s1 + this.s2 + this.s3) != 0;
}
public boolean hasEmpty() {
return this.maxPossible() == 0;
}
public long maxPossible() {
return (this.s0 * this.s1 * this.s2 * this.s3);
}
}
private long allcases(Intersects side1, Intersects side2, Intersects side3) {
return F(side1.s0,side2.s1,side3.s2,side3.s3)+
F(side1.s1,side2.s0,side3.s2,side3.s3)+
F(side1.s2,side2.s3,side3.s0,side3.s1)+
F(side1.s3,side2.s2,side3.s0,side3.s1)+
F(side3.s0,side2.s1,side1.s2,side1.s3)+
F(side3.s1,side2.s0,side1.s2,side1.s3)+
F(side3.s2,side2.s3,side1.s0,side1.s1)+
F(side3.s3,side2.s2,side1.s0,side1.s1)+
F(side1.s0,side3.s1,side2.s2,side2.s3)+
F(side1.s1,side3.s0,side2.s2,side2.s3)+
F(side1.s2,side3.s3,side2.s0,side2.s1)+
F(side1.s3,side3.s2,side2.s0,side2.s1);
}
Intersects getSide(int i, Quadrapartition quart) {
if (quart.cluster1.getBitSet().get(i)) {
return new Intersects(1,0,0,0);
} else if (quart.cluster2.getBitSet().get(i)) {
return new Intersects(0,1,0,0);
} else if (quart.cluster3.getBitSet().get(i)) {
return new Intersects(0,0,1,0);
} else if (quart.cluster4.getBitSet().get(i)) {
return new Intersects(0,0,0,1);
}
else {
return new Intersects(0,0,0,0);
}
}
class Results {
double [] qs;
int effn;
Results (double [] q, int n){
qs = q;
effn = n;
}
}
@Override
Long calculateWeight(Tripartition t,
AbstractComputeMinCostTask<Tripartition> minCostTask) {
// TODO Auto-generated method stub
return null;
}
public Results getWeight(Quadrapartition [] quad ) {
long [] fi = {0l,0l,0l};
long mi = 0l;
double [] weight = {0l,0l,0l};
int effectiven = 0;
Intersects [] allsides = new Intersects[3];
boolean newTree = true;
boolean cruise = false;
//Quadrapartition [] quad = new Quadrapartition [] {quadm, new Quadrapartition(quadm.cluster1, quadm.cluster3, quadm.cluster2, quadm.cluster4), new Quadrapartition(quadm.cluster1, quadm.cluster4, quadm.cluster2, quadm.cluster3)};
Iterator<STITreeCluster> tit = dataCollection.treeAllClusters.iterator();
Deque<Intersects> [] stack = new Deque [] {new ArrayDeque<Intersects>(), new ArrayDeque<Intersects>(), new ArrayDeque<Intersects>()};
for (Integer gtb: geneTreesAsInts){
//n++;
if (newTree) {
STITreeCluster all = tit.next();
for (int i=0; i<3; i++){
allsides[i] = new Intersects(
quad[i].cluster1.getBitSet().intersectionSize(all.getBitSet()),
quad[i].cluster2.getBitSet().intersectionSize(all.getBitSet()),
quad[i].cluster3.getBitSet().intersectionSize(all.getBitSet()),
quad[i].cluster4.getBitSet().intersectionSize(all.getBitSet())
);
}
newTree = false;
mi = allsides[0].maxPossible();
if ( mi != 0) {
effectiven++;
} else {
cruise = true;
}
//sum += F(allsides.s0, allsides.s1, allsides.s2, allsides.s3);
}
if (gtb == Integer.MIN_VALUE) {
if (!cruise) {
//long fiall = fi[0] + fi[1] + fi[2];
//if (fiall != 0)
//double tf = 0.0;
for (int i=0; i<3; i++) {
double efffreq = (fi[i]+0.0)/(2.0*mi);
/*if (efffreq != 1 && efffreq != 0)
System.err.println(efffreq);*/
weight[i] += efffreq;
//tf += efffreq;
}
//if (tf < .99) {
//System.err.println("Warning: a gene tree is contributing only "+tf +" to total score of the branch with mi: "+mi);
//}
}
for (int i=0; i<3; i++) stack[i].clear();
newTree = true;
cruise = false;
fi = new long [] {0l,0l,0l};
mi = 0;
} else {
if (cruise) continue;
if (gtb >= 0){
for (int i=0; i<3; i++) stack[i].push(getSide(gtb, quad[i]));
} else if (gtb == -2) {
for (int i=0; i<3; i++) {
Intersects side1 = stack[i].pop();
Intersects side2 = stack[i].pop();
Intersects newSide = new Intersects(side1, side2);
stack[i].push(newSide);
Intersects side3 = new Intersects(allsides[i]);
side3.subtract(newSide);
fi[i] += allcases(side1, side2, side3);
}
//geneTreesAsIntersects[n] = newSide;
} else {
for (int i=0; i<3; i++) {
ArrayList<Intersects> children = new ArrayList<Intersects>();
Intersects newSide = new Intersects(0,0,0,0);
for (int j = gtb; j < 0 ; j++) {
Intersects pop = stack[i].pop();
children.add(pop);
newSide.addin(pop);
}
stack[i].push(newSide);
Intersects sideRemaining = new Intersects (allsides[i]);
sideRemaining.subtract(newSide);
if ( sideRemaining.isNotEmpty()) {
children.add(sideRemaining);
}
long sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, sum01 = 0, sum23 = 0;
for (int j = 0; j < children.size(); j++){
Intersects side = children.get(j);
sum0 += side.s0;
sum1 += side.s1;
sum2 += side.s2;
sum3 += side.s3;
sum01 += side.s0 * side.s1;
sum23 += side.s2 * side.s3;
}
for (int j = 0; j < children.size(); j++) {
Intersects side = children.get(j);
fi[i] += side.s0 * side.s1 * ((sum2 - side.s2) * (sum3 - side.s3) - sum23 + side.s2 * side.s3);
fi[i] += side.s2 * side.s3 * ((sum0 - side.s0) * (sum1 - side.s1) - sum01 + side.s0 * side.s1);
}
}
}
}
}
return new Results(weight,effectiven);
}
/* private boolean checkFutileCalcs(Intersects side1, Intersects side2) {
return ((side1.s0+side2.s0 == 0? 1 :0) +
(side1.s1+side2.s1 == 0? 1 :0) +
(side1.s2+side2.s2 == 0? 1:0) > 1);
}*/
private long F(long a,long b,long c, long d) {
if (a<0 || b<0 || c<0|| d<0) {
throw new RuntimeException("negative side not expected: "+a+" "+b+" "+c);
}
return a*b*c*d;
}
class Quadrapartition {
STITreeCluster cluster1;
STITreeCluster cluster2;
STITreeCluster cluster3;
STITreeCluster cluster4;
private int _hash = 0;
public Quadrapartition(STITreeCluster c1, STITreeCluster c2, STITreeCluster c3,STITreeCluster c4) {
initialize(c1, c2, c3, c4);
}
private void initialize(STITreeCluster c1, STITreeCluster c2,
STITreeCluster c3, STITreeCluster c4) {
if (c1 == null || c2 == null || c3 == null) {
throw new RuntimeException("none cluster" +c1+" "+c2+" "+c3);
}
int n1 = c1.getBitSet().nextSetBit(0), n2 = c2.getBitSet().nextSetBit(0),
n3 = c3.getBitSet().nextSetBit(0), n4=c4.getBitSet().nextSetBit(0);
int ntg1;
int ntg2;
STITreeCluster cluster_tmp1;
STITreeCluster cluster_tmp2;
STITreeCluster cluster_tmp3;
STITreeCluster cluster_tmp4;
if (n1 < n2 ) {
ntg1 = n1;
cluster_tmp1 = c1;
cluster_tmp2 = c2;
}
else {
ntg1 = n2;
cluster_tmp1 = c2;
cluster_tmp2 = c1;
}
if (n3<n4) {
ntg2 = n3;
cluster_tmp3 = c3;
cluster_tmp4 = c4;
}
else {
ntg2 = n4;
cluster_tmp3 = c4;
cluster_tmp4 = c3;
}
if(ntg1<ntg2){
cluster1 = cluster_tmp1;
cluster2 = cluster_tmp2;
cluster3 = cluster_tmp3;
cluster4 = cluster_tmp4;
}
else{
cluster1 = cluster_tmp3;
cluster2 = cluster_tmp4;
cluster3 = cluster_tmp1;
cluster4 = cluster_tmp2;
}
}
@Override
public boolean equals(Object obj) {
Quadrapartition trip = (Quadrapartition) obj;
return this == obj ||
((trip.cluster1.equals(this.cluster1) &&
trip.cluster2.equals(this.cluster2) &&
trip.cluster4.equals(this.cluster4) &&
trip.cluster3.equals(this.cluster3)));
}
@Override
public int hashCode() {
if (_hash == 0) {
_hash = cluster1.hashCode() * cluster2.hashCode()
* cluster4.hashCode() * cluster3.hashCode();
}
return _hash;
}
@Override
public String toString() {
return cluster1.getBitSet().toString2()+"|"+cluster2.getBitSet().toString2()+
"#"+cluster3.getBitSet().toString2()+"|"+cluster4.getBitSet().toString2();
}
public String toString2() {
return cluster1.toString()+"|"+cluster2.toString()+
"#"+cluster3.toString()+ "|"+cluster4.toString();
}
}
@Override
public void preCalculateWeights(List<Tree> trees, List<Tree> extraTrees) {
// TODO Auto-generated method stub
}
}