Debian Codegraph @

current/bin/Bin.java

raw symbols SCIP
1 package bin;
2
3 import json.JsonObject;
4 import shared.Tools;
5 import structures.ByteBuilder;
6 import structures.FloatList;
7 import structures.IntHashMap;
8 import structures.IntLongHashMap;
9
10 public abstract class Bin extends BinObject implements Sketchable, Iterable<Contig> {
11
12 @Override
13 public final int taxid() {return taxid;}
14
15 abstract String name();
16
17 @Override
18 public final float gc() {return gcSum/(float)size();}
19
20 public final void clearDepth() {
21 depth.clear();
22 normDepth=null;
23 }
24
25 public float depthRatio(Bin b) {
26 float max=1;
27 for(int i=0; i<depth.size; i++) {
28 float d1=depth.get(i)+depthBoost;
29 float d2=b.depth.get(i)+depthBoost;
30 float ratio=Tools.max(d1,d2)/Tools.min(d1,d2);
31 max=Tools.max(max, ratio);
32 }
33 return max;
34 }
35
36 public boolean pure() {return pure(0.01f);}
37 public boolean pure(float fraction) {return labelTaxid>0;}
38
39 public final void setDepth(float d, int sample) {
40 depth.set(sample, d);
41 }
42
43 public final void appendDepth(float d) {
44 depth.add(d);
45 }
46
47 public final int numDepths() {
48 return depth.size;
49 }
50
51 public final float numEdges() {
52 return pairMap==null ? 0 : pairMap.size();
53 }
54
55 public final float depth(int sample) {
56 return (sample==1 && depth.size==1 ? 0 : depth.get(sample));
57 }
58
59 public float minContigDepth() {return depth();}
60 public float maxContigDepth() {return depth();}
61
62 public final float maxDepth() {
63 return depth.max();
64 }
65
66 public final float depthTotal() {
67 return (float)depth.sum();
68 }
69
70 public float[] normDepth() {
71 if(depth.size()<2) {return null;}
72 if(normDepth==null) {fillNormDepth();}
73 assert(normDepth.length==depth.size()) : normDepth.length+", "+depth.size;
74 assert(normDepth.length>1);
75 return normDepth;
76 }
77
78 synchronized void fillNormDepth() {
79 assert(normDepth==null || (normDepth.length>1 && normDepth.length==numDepths()));
80 if(normDepth==null) {normDepth=new float[depth.size];}
81 assert(normDepth.length>1) : normDepth.length;
82 float sum=0;
83 for(int i=0; i<depth.size; i++) {
84 float f=depth.get(i);
85 f=(float)Math.log(f+1);
86 sum+=f;
87 normDepth[i]=f;
88 }
89 float inv=1/Tools.max(sum, 0.1f);
90 for(int i=0; i<normDepth.length; i++) {
91 normDepth[i]*=inv;
92 }
93 }
94
95 /** Uses a weighted sum of linear and geometric means */
96 public final float depth() {
97 if(depthZeroProxy) {return depth.get(0);}
98 if(avgDepthValid) {return avgDepth;}
99 synchronized(this) {
100 if(depth.size()==1) {avgDepth=depth.get(0);}
101 else {
102 double product=1;
103 double sum=0;
104 for(int i=0; i<depth.size; i++) {
105 float d=depth.get(i);
106 product*=(d+0.25f);
107 sum+=d;
108 }
109 double inv=1.0/depth.size;
110 float geo=(float)(Math.pow(product, inv)-0.25);
111 float linear=(float)(sum*inv);
112 avgDepth=geo*0.75f+linear*0.25f;
113 }
114 avgDepthValid=true;
115 }
116 return avgDepth;
117 }
118
119 @Override
120 /** Biggest first */
121 public final int compareTo(Sketchable o) {
122 if(size()!=o.size()) {return size()>o.size() ? -1 : 1;}//Biggest first
123 return o.id()-id();
124 }
125
126 @Override
127 public final void setFrom(JsonObject all) {
128 assert(sketchedSize<size());
129 clearTax();
130 JsonObject top=null, second=null;
131 if(all!=null && all.jmapSize()>0) {
132 for(String key : all.jmap.keySet()){
133 JsonObject hit=all.jmap.get(key);
134 if(top==null) {top=hit;}
135 else {
136 if(hit.getLong("TaxID")!=1806490) {//Achromobacter sp. ATCC35328; messes with E.coli.
137 second=hit;
138 break;
139 }
140 }
141 }
142 }
143 topHit=(top==null ? null : new SketchRecord(top));
144 secondHit=(second==null ? null : new SketchRecord(second));
145 taxid=(topHit==null ? -1 : topHit.taxid);
146 genusTaxid=(topHit==null ? -1 : topHit.genusTaxid);
147 sketchedSize=size();
148 }
149
150 @Override
151 public final void clearTax() {
152 taxid=genusTaxid=-1;
153 topHit=secondHit=null;
154 sketchedSize=0;
155 }
156
157 @Override
158 public final String toString() {
159 return toBytes().toString();
160 }
161
162 public final ByteBuilder toBytes() {
163 ByteBuilder bb=new ByteBuilder();
164 bb.append(isCluster() ? "Cluster " : "Contig ").append(id()).append(":");
165 bb.tab().append("Size ").append(size());
166 bb.tab().append("Contigs ").append(numContigs());
167 bb.tab().append("GC ").append(gc(), 3);
168 bb.tab().append("Depth ").append(depth(), 1);
169 if(depth.size()>1) {
170 for(int i=0; i<depth.size; i++) {bb.comma().append(depth(i), 1);}
171 }
172 bb.tab().append("TaxID ").append(taxid);
173 if(validation) {
174 bb.tab().append("TaxID0 ").append(labelTaxid);
175 if(completeness>=0) {
176 bb.tab().append("Complt ").append(completeness*100, 2);
177 bb.tab().append("Contam ").append(contam*100, 2);
178 }
179 }
180 // if(labelTaxid>0) {bb.tab().append("TaxID0 ").append(labelTaxid);}
181 if(topHit!=null) {topHit.appendTo(bb.nl().tab().tab());}
182 if(secondHit!=null) {secondHit.appendTo(bb.nl().tab().tab());}
183 return bb;
184 }
185
186 /** Higher is more similar */
187 // private final float similarityTo(Bin b) {
188 // final float ratio=depthRatio(b);
189 // final float gc=gc(), gc2=b.gc();
190 // final float gcDif=Math.abs(gc-gc2)+1f;
191 // final float simDif=SimilarityMeasures.calculateDifferenceAverage(counts, b.counts)*0.5f+1f;
192 // final float covariance=1+covariance(b)*32;
193 // float product=simDif*ratio*gcDif*covariance;
194 // return 1f/product;
195 // }
196 //
197 // /** Higher is more similar */
198 // private static final float similarity(float ratio_, float gcDif_,
199 // float simDif_, float covariance_, long edges_) {
200 // final float ratio=ratio_;
201 // final float gcDif=gcDif_+1f;
202 // final float simDif=simDif_*0.5f+1f;
203 // final float covariance=1+covariance_*32;
204 // float product=simDif*ratio*gcDif*covariance;
205 // if(BinObject.verbose) {
206 // System.err.println(product+"="+simDif+"*"+ratio+"*"+gcDif+"*"+covariance);
207 // }
208 // return 1f/product;
209 // }
210 //
211 // public final float similarityTo(Bin b, float stringency) {
212 // long size=Tools.min(size(), b.size());
213 // float sizeMult=Binner.sizeAdjustMult(size);
214 // stringency*=sizeMult;
215 //
216 // float maxKmerDif=Binner.maxKmerDif2*stringency;
217 // float maxDepthRatio=1+((Binner.maxDepthRatio2-1)*stringency);
218 // float maxGCDif=Binner.maxGCDif2*stringency;
219 // float maxProduct=maxKmerDif*maxDepthRatio*Binner.productMult;
220 // float maxCovariance=Binner.maxCovariance2*stringency;
221 // return similarityTo(b, maxGCDif, maxDepthRatio, maxKmerDif, maxProduct, maxCovariance);
222 // }
223 //
224 // /** Higher is more similar */
225 // public final float similarityTo(Bin b, float maxGCDif, float maxDepthRatio,
226 // float maxKmerDif, float maxProduct, float maxCovariance) {
227 // long edges1=countEdgesTo(b);
228 // long edges2=b.countEdgesTo(this);
229 // float mult=(edges1>1 ? 1.4f : 1f)*(edges2>1 ? 1.4f : 1f);
230 //
231 // if(BinObject.verbose) {
232 // System.err.println("Comparing to "+b.id()+": "+
233 // "maxKmerDif="+maxKmerDif+", maxDepthRatio="+maxDepthRatio+
234 // ", maxProduct="+maxProduct+", maxGCDif="+maxGCDif+
235 // ", maxCovariance="+maxCovariance);
236 // }
237 //
238 // float gcDif=Math.abs(gc()-b.gc());
239 //// float acDif=Math.abs(acRatio-b.acRatio);
240 //// float eDif=Math.abs(entropy-b.entropy);
241 //// if(gcDif>maxGCDif*mult || eDif>maxGCDif*mult*4.5f) {return -1;}
242 // if(gcDif>maxGCDif*mult) {return -1;}
243 //// if(acDif>maxGCDif*mult*1f) {return -1;}
244 // final float depthRatio=depthRatio(b);
245 // final float covariance=covariance(b);
246 // if(depthRatio>maxDepthRatio*mult || covariance>maxCovariance*mult) {return -1;}
247 // final float kmerDif=SimilarityMeasures.calculateDifferenceAverage(counts, b.counts);
248 // final float product=kmerDif*depthRatio;
249 // if(kmerDif>maxKmerDif*mult || product>maxProduct*mult) {return -1;}
250 // return similarity(depthRatio, gcDif, kmerDif, covariance, Tools.min(edges1, edges2));
251 // }
252
253 public float covariance(Bin b) {
254 if(depth.size()<2) {return 0;}
255
256 float[] nda=normDepth();
257 float[] ndb=b.normDepth();
258 assert(nda!=null) : getClass()+", "+b.getClass();
259 assert(ndb!=null) : getClass()+", "+b.getClass();
260 assert(nda.length==numDepths()) : getClass()+", "+b.getClass();
261 assert(ndb.length==b.numDepths()) : getClass()+", "+b.getClass();
262 assert(nda.length==ndb.length) : getClass()+", "+b.getClass();
263 float f=SimilarityMeasures.cosineDifference(normDepth(), b.normDepth());
264 return f;
265 }
266
267 public final long sketchedSize() {return sketchedSize;}
268
269 public final void calcContam(IntLongHashMap sizeMap) {
270 IntLongHashMap taxmap=new IntLongHashMap(7);
271 long sum=0;
272 for(Contig c : this) {
273 int tid=c.labelTaxid;
274 taxmap.increment(tid, c.size());
275 sum+=c.size();
276 }
277 assert(sum==size());
278 int[] keys=taxmap.keys();
279 long[] values=taxmap.values();
280 final int invalid=taxmap.invalid();
281 int tid=-1;
282 long maxSize=-1;
283 for(int i=0; i<keys.length; i++) {
284 int key=keys[i];
285 long value=values[i];
286 if(key!=invalid && value>maxSize) {
287 tid=key;
288 maxSize=value;
289 }
290 }
291 taxid=tid;
292 long targetSize=sizeMap.get(tid);
293 if(targetSize==sizeMap.invalid()) {targetSize=sum;}//unknown...
294 completeness=maxSize/(float)targetSize;
295 contam=(sum-maxSize)/(float)sum;
296 badContigs=0;
297 for(Contig c : this) {
298 if(c.labelTaxid>0 && c.labelTaxid!=taxid) {badContigs++;}
299 }
300 }
301
302 public final void calcContamContigs(IntLongHashMap sizeMap) {
303 // taxid=primaryTaxID();
304 // long targetSize=sizeMap.get(taxid);
305 IntLongHashMap taxmap=new IntLongHashMap(7);
306 long sum=0;
307 for(Contig c : this) {
308 int tid=c.labelTaxid;
309 taxmap.increment(tid, c.size());
310 sum+=c.size();
311 }
312 assert(sum==size());
313 int[] keys=taxmap.keys();
314 long[] values=taxmap.values();
315 final int invalid=taxmap.invalid();
316 int tid=-1;
317 long maxSize=-1;
318 for(int i=0; i<keys.length; i++) {
319 int key=keys[i];
320 long value=values[i];
321 if(key!=invalid && value>maxSize) {
322 tid=key;
323 maxSize=value;
324 }
325 }
326 taxid=tid;
327 long targetSize=sizeMap.get(tid);
328 if(targetSize==sizeMap.invalid()) {targetSize=sum;}//unknown...
329 completeness=maxSize/(float)targetSize;
330 contam=(sum-maxSize)/(float)sum;
331 }
332
333 public final int primaryTaxid() {
334 IntLongHashMap taxmap=new IntLongHashMap(7);
335 long sum=0;
336 for(Contig c : this) {
337 int tid=c.labelTaxid;
338 taxmap.increment(tid, c.size());
339 sum+=c.size();
340 }
341 assert(sum==size());
342 int[] keys=taxmap.keys();
343 long[] values=taxmap.values();
344 final int invalid=taxmap.invalid();
345 int tid=-1;
346 long maxSize=-1;
347 for(int i=0; i<keys.length; i++) {
348 int key=keys[i];
349 long value=values[i];
350 if(key!=invalid && value>maxSize) {
351 tid=key;
352 maxSize=value;
353 }
354 }
355 return tid;
356 }
357
358 abstract boolean sameCluster(Bin b);
359
360 public abstract boolean isCluster();
361
362 public abstract Cluster toCluster();
363
364 public abstract Cluster cluster();
365
366 public abstract boolean isValid();
367
368 public final boolean isEmpty() {return numContigs()<1;}
369
370 public int transEdgesTo(Bin b) {
371 if(!b.isCluster()) {return transEdgesTo((Contig)b);}
372 else {return transEdgesTo((Cluster)b);}
373 }
374
375 public int transEdgesTo(Contig b) {
376 if(pairMap==null || Binner.goodTransEdgeMult==1) {return 0;}
377 int[] keys=pairMap.keys();//, values=pairMap.values();
378 int invalid=pairMap.invalid();
379 int v=pairMap.get(b.id());
380 int max=0;
381 for(int i=0; i<keys.length; i++) {
382 int key=keys[i];
383 if(key!=invalid) {
384 Contig c=DataLoader.allContigs.get(key);
385 max=Tools.max(max, c.countEdgesTo(b));
386 }
387 }
388 return Tools.max(v, max);
389 }
390
391 public int transEdgesTo(Cluster b) {
392 if(pairMap==null || Binner.goodTransEdgeMult==1) {return 0;}
393 int[] keys=pairMap.keys();//, values=pairMap.values();
394 int invalid=pairMap.invalid();
395 int v=pairMap.get(b.id());
396 int max=0;
397 for(int i=0; i<keys.length; i++) {
398 int key=keys[i];
399 if(key!=invalid) {
400 Contig c=DataLoader.allContigs.get(key);
401 max=Tools.max(max, c.countEdgesTo(b));
402 }
403 }
404 return Tools.max(v, max);
405 }
406
407 public int countEdgesTo(Bin b) {
408 if(!b.isCluster()) {return countEdgesTo((Contig)b);}
409 else {return countEdgesTo((Cluster)b);}
410 }
411
412 public int countEdgesTo(Contig b) {
413 return pairMap==null ? 0 : Tools.max(0, pairMap.get(b.id()));
414 }
415
416 public int countEdgesTo(Cluster b) {
417 if(pairMap==null) {return 0;}
418 final int[] keys=pairMap.keys(), values=pairMap.values();
419 final int invalid=pairMap.invalid();
420 long sum=0;
421 int max=0;
422 for(int i=0; i<keys.length; i++) {
423 int key=keys[i];
424 if(key!=invalid && b.contigSet.contains(key)) {
425 int v=values[i];
426 sum+=v;
427 max=Tools.max(max, v);
428 }
429 }
430 return max;
431 }
432
433 public int countReciprocalEdges(Bin b) {
434 if(!b.isCluster()) {return countReciprocalEdges((Contig)b);}
435 else {return countReciprocalEdges((Cluster)b);}
436 }
437
438 public int countReciprocalEdges(Contig b) {
439 if(pairMap==null || b.pairMap==null) {return 0;}
440 return Tools.max(0, Tools.min(pairMap.get(b.id()), b.countEdgesTo(this)));
441 // final int[] keysB=b.pairMap.keys();
442 // final int invalidB=b.pairMap.invalid();
443 // for(int i=0; i<keysB.length; i++) {
444 // int key=keysB[i];
445 // if(key!=invalid && b.contigSet.contains(key)) {
446 // int v=values[i];
447 // sum+=v;
448 // max=Tools.max(max, v);
449 // }
450 // }
451 //
452 // return pairMap==null ? 0 : Tools.max(0, pairMap.get(b.id()));
453 }
454
455 public int countReciprocalEdges(Cluster b) {//Slow
456 if(pairMap==null || b.pairMap==null) {return 0;}
457 if(!this.isCluster()) {return b.countReciprocalEdges((Contig)this);}
458 if(size()>b.size()) {return b.countReciprocalEdges((Cluster)this);}
459 //At this point they are both clusters and this one is smaller
460
461 // final int[] keys=pairMap.keys(), values=pairMap.values();
462 // final int invalid=pairMap.invalid();
463 // int max=0;
464 // for(int i=0; i<keys.length; i++) {
465 // int key=keys[i];
466 // if(key!=invalid && b.contigSet.contains(key)) {
467 // int v1=values[i], v2=b.pairMap.get(key)
468 // max=Tools.max(max, v);
469 // }
470 // }
471
472 int max=0;
473 for(Contig c : ((Cluster)this).contigs) {
474 max=Tools.max(max, b.countReciprocalEdges(c));
475 }
476
477 return max;
478 }
479
480 public int numTetramers;
481 public int numPentamers;
482 // public float invKmers;
483
484 public int[] dimers;
485 public int[] trimers;
486 public int[] tetramers;
487 public int[] pentamers;
488 public long gcSum;
489 public long sketchedSize;
490 // public float acRatio;
491
492 public long depthSum=0;
493 private float avgDepth=-1;
494 boolean avgDepthValid=false;
495 private FloatList depth=new FloatList(1);
496 private float[] normDepth;
497 public IntHashMap pairMap;
498 float completeness=0, contam=0;
499 int badContigs=0;
500 float entropy;
501 float strandedness;
502 float score;
503
504 int dest=-1;
505
506 public int taxid;
507 public int genusTaxid;
508 public int labelTaxid;//For validation on labeled data
509 SketchRecord topHit;
510 SketchRecord secondHit;
511
512 }