| 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 |
|
| 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 |
|
| 121 |
public final int compareTo(Sketchable o) { |
| 122 |
if(size()!=o.size()) {return size()>o.size() ? -1 : 1;} |
| 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) { |
| 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 |
|
| 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 |
|
| 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 |
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;} |
| 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 |
|
| 304 |
|
| 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;} |
| 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(); |
| 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(); |
| 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 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
public int countReciprocalEdges(Cluster b) { |
| 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 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 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 |
|
| 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 |
|
| 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; |
| 509 |
SketchRecord topHit; |
| 510 |
SketchRecord secondHit; |
| 511 |
|
| 512 |
} |