| 1 |
package bin; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.PrintStream; |
| 5 |
import java.util.ArrayList; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.Collection; |
| 8 |
import java.util.HashMap; |
| 9 |
import java.util.concurrent.locks.ReadWriteLock; |
| 10 |
|
| 11 |
import fileIO.ByteFile; |
| 12 |
import fileIO.ByteStreamWriter; |
| 13 |
import fileIO.FileFormat; |
| 14 |
import fileIO.ReadWrite; |
| 15 |
import shared.LineParser1; |
| 16 |
import shared.Parse; |
| 17 |
import shared.Parser; |
| 18 |
import shared.PreParser; |
| 19 |
import shared.Shared; |
| 20 |
import shared.Timer; |
| 21 |
import shared.Tools; |
| 22 |
import stream.ConcurrentReadInputStream; |
| 23 |
import stream.Read; |
| 24 |
import structures.IntHashMap; |
| 25 |
import structures.IntLongHashMap; |
| 26 |
import structures.ListNum; |
| 27 |
import structures.LongList; |
| 28 |
import template.Accumulator; |
| 29 |
import template.ThreadWaiter; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class GradeBins implements Accumulator<GradeBins.ProcessThread> { |
| 38 |
|
| 39 |
public static void main(String[] args){ |
| 40 |
|
| 41 |
Timer t=new Timer(); |
| 42 |
|
| 43 |
|
| 44 |
GradeBins x=new GradeBins(args); |
| 45 |
|
| 46 |
|
| 47 |
x.process(t); |
| 48 |
|
| 49 |
|
| 50 |
Shared.closeStream(x.outstream); |
| 51 |
} |
| 52 |
|
| 53 |
public GradeBins(String[] args){ |
| 54 |
|
| 55 |
{ |
| 56 |
PreParser pp=new PreParser(args, getClass(), false); |
| 57 |
args=pp.args; |
| 58 |
outstream=pp.outstream; |
| 59 |
} |
| 60 |
|
| 61 |
Parser parser=new Parser(); |
| 62 |
for(int i=0; i<args.length; i++){ |
| 63 |
String arg=args[i]; |
| 64 |
String[] split=arg.split("="); |
| 65 |
String a=split[0].toLowerCase(); |
| 66 |
String b=split.length>1 ? split[1] : null; |
| 67 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 68 |
|
| 69 |
if(a.equals("parse_flag_goes_here")){ |
| 70 |
|
| 71 |
}else if(a.equals("in")){ |
| 72 |
Tools.getFileOrFiles(b, in, true, false, false, false); |
| 73 |
}else if(a.equals("size")){ |
| 74 |
totalSize=Parse.parseKMG(b); |
| 75 |
}else if(a.equals("minsize")){ |
| 76 |
minSize=Parse.parseIntKMG(b); |
| 77 |
}else if(a.equals("ref") || a.equals("contigs") || a.equals("assembly")){ |
| 78 |
ref=b; |
| 79 |
}else if(a.equals("hist")){ |
| 80 |
hist=b; |
| 81 |
}else if(a.equals("report")){ |
| 82 |
report=b; |
| 83 |
}else if(a.equals("taxin")){ |
| 84 |
taxIn=b; |
| 85 |
}else if(a.equals("taxout")){ |
| 86 |
taxOut=b; |
| 87 |
}else if(a.equals("tax") || a.equals("size")){ |
| 88 |
tax=b; |
| 89 |
}else if(a.equals("loadmt")){ |
| 90 |
loadMT=Parse.parseBoolean(b); |
| 91 |
}else if(a.equalsIgnoreCase("checkm")){ |
| 92 |
checkMFile=b; |
| 93 |
}else if(a.equalsIgnoreCase("eukcc")){ |
| 94 |
eukCCFile=b; |
| 95 |
}else if(b==null && new File(arg).isFile()){ |
| 96 |
in.add(arg); |
| 97 |
}else if(b==null && new File(arg).isDirectory()){ |
| 98 |
Tools.getFileOrFiles(arg, in, true, false, false, false); |
| 99 |
}else if(parser.parse(arg, a, b)){ |
| 100 |
|
| 101 |
}else{ |
| 102 |
assert(false) : "Unknown parameter "+args[i]; |
| 103 |
outstream.println("Unknown parameter "+args[i]); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
{ |
| 108 |
Parser.processQuality(); |
| 109 |
|
| 110 |
maxReads=parser.maxReads; |
| 111 |
|
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void process(Timer t){ |
| 116 |
|
| 117 |
BinObject.grading=true; |
| 118 |
|
| 119 |
if(tax!=null && taxIn==null && taxOut==null) { |
| 120 |
boolean taxExists=(tax==null ? false : new File(tax).canRead()); |
| 121 |
if(taxExists && ref==null) {taxIn=tax;} |
| 122 |
else {taxOut=tax;} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
if(taxIn!=null) { |
| 127 |
if(ref!=null) { |
| 128 |
System.err.println("Reading from "+taxIn+" instead of "+ref); |
| 129 |
} |
| 130 |
sizeMap=loadTaxIn(taxIn); |
| 131 |
}else { |
| 132 |
sizeMap=makeSizeMap(ref); |
| 133 |
} |
| 134 |
if(taxOut!=null) { |
| 135 |
writeTaxOut(taxOut, sizeMap, countMap); |
| 136 |
} |
| 137 |
checkMMap=loadCheckM(checkMFile); |
| 138 |
eukCCMap=loadEukCC(eukCCFile); |
| 139 |
ArrayList<BinStats> bins=(loadMT ? loadMT(in) : loadST(in)); |
| 140 |
|
| 141 |
printResults(bins); |
| 142 |
|
| 143 |
t.stop(); |
| 144 |
outstream.println(); |
| 145 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 146 |
} |
| 147 |
|
| 148 |
void printResults(ArrayList<BinStats> bins) { |
| 149 |
for(BinStats bin : bins) { |
| 150 |
readsProcessed+=bin.contigs; |
| 151 |
basesProcessed+=bin.size; |
| 152 |
sizes.add(bin.size); |
| 153 |
} |
| 154 |
|
| 155 |
if(verbose){outstream.println("Finished.");} |
| 156 |
|
| 157 |
outstream.println(); |
| 158 |
printCleanDirty(bins); |
| 159 |
|
| 160 |
outstream.println(); |
| 161 |
printL90(sizes, totalSize); |
| 162 |
|
| 163 |
outstream.println(); |
| 164 |
printScore(bins, totalSize, totalContigs, taxIDsIn, true); |
| 165 |
|
| 166 |
outstream.println(); |
| 167 |
printBinQuality(bins, minSize, outstream); |
| 168 |
|
| 169 |
if(hist!=null) { |
| 170 |
ChartMaker.makeChartFromBinStats(hist, bins); |
| 171 |
} |
| 172 |
if(report!=null) { |
| 173 |
printClusterReport(bins, minSize, report); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public static void printScore(ArrayList<BinStats> bins, |
| 178 |
long totalSize, long totalContigs, long taxIDsIn, boolean validation) { |
| 179 |
long cleanContigs=0, contamContigs=0; |
| 180 |
long cleanSize=0, contamSize=0; |
| 181 |
long badContigs=0; |
| 182 |
double compltScore=0, contamScore=0; |
| 183 |
double totalScore=0, totalScore2=0; |
| 184 |
IntHashMap tidBins=new IntHashMap(); |
| 185 |
int labels=0; |
| 186 |
for(BinStats bin : bins) { |
| 187 |
if(bin.taxid>0) { |
| 188 |
tidBins.increment(bin.taxid); |
| 189 |
labels++; |
| 190 |
} |
| 191 |
long contam=Math.round(bin.contam*bin.size); |
| 192 |
contamScore+=contam; |
| 193 |
compltScore+=Math.round(bin.complt*(bin.size-contam)); |
| 194 |
double score=Math.max(0, bin.complt-5*bin.contam); |
| 195 |
totalScore+=score; |
| 196 |
totalScore2+=score*score; |
| 197 |
badContigs+=bin.badContigs; |
| 198 |
if(contam<1) { |
| 199 |
cleanSize+=bin.size; |
| 200 |
cleanContigs+=bin.contigs; |
| 201 |
}else { |
| 202 |
contamSize+=bin.size; |
| 203 |
contamContigs+=bin.contigs; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
outstream.println("Sequence Recovery: \t"+ |
| 208 |
String.format("%.3f", (cleanSize+contamSize)*100.0/totalSize)); |
| 209 |
outstream.println("Contig Recovery: \t"+ |
| 210 |
String.format("%.3f", (cleanContigs+contamContigs)*100.0/totalContigs)); |
| 211 |
if(taxIDsIn>0) { |
| 212 |
outstream.println("Bad Contigs: \t"+ |
| 213 |
String.format("%.3f", badContigs*100.0/(cleanContigs+contamContigs))); |
| 214 |
outstream.println("Genomes Represented: \t"+ |
| 215 |
String.format("%.3f", (tidBins.size())*100.0/taxIDsIn)); |
| 216 |
} |
| 217 |
if(validation) { |
| 218 |
outstream.println("Completeness Score: \t"+ |
| 219 |
String.format("%.3f", 100*compltScore/totalSize)); |
| 220 |
outstream.println("Contamination Score: \t"+ |
| 221 |
String.format("%.4f", 100*contamScore/totalSize)); |
| 222 |
|
| 223 |
|
| 224 |
outstream.println("Total Score: \t"+ |
| 225 |
String.format("%.2f", totalScore2)); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
public static void printCleanDirty(ArrayList<BinStats> bins) { |
| 230 |
long cleanBins=0, contamBins=0; |
| 231 |
long cleanContigs=0, contamContigs=0; |
| 232 |
long cleanSize=0, contamSize=0; |
| 233 |
long partialCleanSize=0, partialContamSize=0; |
| 234 |
long badContigs=0; |
| 235 |
for(BinStats bin : bins) { |
| 236 |
long contam=Math.round(bin.contam*bin.size); |
| 237 |
badContigs+=bin.badContigs; |
| 238 |
if(contam<1) { |
| 239 |
cleanBins++; |
| 240 |
cleanSize+=bin.size; |
| 241 |
cleanContigs+=bin.contigs; |
| 242 |
}else { |
| 243 |
contamBins++; |
| 244 |
contamSize+=bin.size; |
| 245 |
contamContigs+=bin.contigs; |
| 246 |
partialCleanSize+=(bin.size-contam); |
| 247 |
partialContamSize+=contam; |
| 248 |
} |
| 249 |
} |
| 250 |
outstream.println(QuickBin.formatString("Clean Bins", 29, cleanBins, contamBins)); |
| 251 |
outstream.println(QuickBin.formatString("Dirty Bins", 29, contamBins, cleanBins)); |
| 252 |
outstream.println(QuickBin.formatString("Clean Bin Bases", 29, cleanSize, contamSize)); |
| 253 |
outstream.println(QuickBin.formatString("Dirty Bin Bases", 29, contamSize, cleanSize)); |
| 254 |
outstream.println(QuickBin.formatString("Tainted Bases", 29, |
| 255 |
partialCleanSize, cleanSize+contamSize-partialCleanSize)); |
| 256 |
outstream.println(QuickBin.formatString("Contam Bases", 29, |
| 257 |
partialContamSize, cleanSize+contamSize-partialContamSize)); |
| 258 |
outstream.println("Bad Contigs: \t"+ |
| 259 |
String.format("%.3f", badContigs*100.0/(cleanContigs+contamContigs))); |
| 260 |
} |
| 261 |
|
| 262 |
public static ArrayList<BinStats> loadST(ArrayList<String> in){ |
| 263 |
ArrayList<BinStats> bins=new ArrayList<BinStats>(in.size()); |
| 264 |
for(String s : in) { |
| 265 |
final BinStats c; |
| 266 |
Cluster clust=loadCluster(s); |
| 267 |
calcContam(s, clust); |
| 268 |
c=new BinStats(clust); |
| 269 |
bins.add(c); |
| 270 |
} |
| 271 |
return bins; |
| 272 |
} |
| 273 |
|
| 274 |
public ArrayList<BinStats> loadMT(ArrayList<String> in){ |
| 275 |
|
| 276 |
ArrayList<BinStats> bins=new ArrayList<BinStats>(in.size()); |
| 277 |
|
| 278 |
|
| 279 |
int threads=Shared.threads(); |
| 280 |
if(threads>16) {threads=Tools.mid(16, threads/2, 32);} |
| 281 |
|
| 282 |
|
| 283 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 284 |
for(int i=0; i<threads; i++){ |
| 285 |
alpt.add(new ProcessThread(in, bins, i, threads)); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
boolean success=ThreadWaiter.startAndWait(alpt, this); |
| 290 |
this.success&=!success; |
| 291 |
|
| 292 |
|
| 293 |
return bins; |
| 294 |
} |
| 295 |
|
| 296 |
static Cluster loadCluster(String fname) { |
| 297 |
FileFormat ffin=FileFormat.testInput(fname, FileFormat.FASTA, null, true, true); |
| 298 |
|
| 299 |
final ConcurrentReadInputStream cris; |
| 300 |
{ |
| 301 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin, null); |
| 302 |
cris.start(); |
| 303 |
} |
| 304 |
Cluster c=new Cluster(0); |
| 305 |
{ |
| 306 |
ListNum<Read> ln=cris.nextList(); |
| 307 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 308 |
|
| 309 |
|
| 310 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 311 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 312 |
|
| 313 |
for(int idx=0; idx<reads.size(); idx++){ |
| 314 |
final Read r1=reads.get(idx); |
| 315 |
|
| 316 |
|
| 317 |
Contig a=new Contig(r1.name(), r1.bases, (int)r1.numericID); |
| 318 |
c.tetramers=new int[0]; |
| 319 |
int tid=DataLoader.parseTaxID(a.name); |
| 320 |
a.taxid=a.labelTaxid=tid; |
| 321 |
c.add(a); |
| 322 |
} |
| 323 |
|
| 324 |
cris.returnList(ln); |
| 325 |
if(verbose){outstream.println("Returned a list.");} |
| 326 |
ln=cris.nextList(); |
| 327 |
reads=(ln!=null ? ln.list : null); |
| 328 |
} |
| 329 |
if(ln!=null){ |
| 330 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 331 |
} |
| 332 |
} |
| 333 |
ReadWrite.closeStream(cris); |
| 334 |
return c; |
| 335 |
} |
| 336 |
|
| 337 |
static void calcContam(String fname, Cluster c) { |
| 338 |
fname=new File(fname).getName(); |
| 339 |
String core=ReadWrite.stripToCore(fname); |
| 340 |
CCLine dummy=new CCLine(0, 0); |
| 341 |
CCLine checkm=(checkMMap==null ? null : checkMMap.get(core)); |
| 342 |
CCLine eukcc=(eukCCMap==null ? null : eukCCMap.get(core)); |
| 343 |
assert((checkMMap==null) == (checkm==null)) : checkm; |
| 344 |
|
| 345 |
if(checkm==null && eukcc==null) { |
| 346 |
c.calcContam(sizeMap); |
| 347 |
return; |
| 348 |
} |
| 349 |
if(checkm==null) {checkm=dummy;} |
| 350 |
if(eukcc==null) {eukcc=dummy;} |
| 351 |
CCLine best=(checkm.completeness>=eukcc.completeness ? checkm : eukcc); |
| 352 |
c.completeness=best.completeness; |
| 353 |
c.contam=best.contam; |
| 354 |
} |
| 355 |
|
| 356 |
static ArrayList<BinStats> toStats(Collection<? extends Bin> bins, int minSize) { |
| 357 |
ArrayList<BinStats> list=new ArrayList<BinStats>(); |
| 358 |
for(Bin b : bins) { |
| 359 |
if(b.size()>=minSize) { |
| 360 |
BinStats bs=new BinStats(b); |
| 361 |
list.add(bs); |
| 362 |
} |
| 363 |
} |
| 364 |
return list; |
| 365 |
} |
| 366 |
|
| 367 |
static void printClusterReport(Collection<? extends Bin> bins, int minSize, String fname) { |
| 368 |
ArrayList<BinStats> list=toStats(bins, minSize); |
| 369 |
printClusterReport(list, minSize, fname); |
| 370 |
} |
| 371 |
|
| 372 |
static void printClusterReport(ArrayList<BinStats> bins, int minSize, String fname) { |
| 373 |
if(fname==null) {return;} |
| 374 |
ByteStreamWriter bsw=new ByteStreamWriter(fname, true, false, false); |
| 375 |
bsw.start(); |
| 376 |
bsw.print("#Bin\tSize\tContigs\tGC\tDepth\tMinDepth\tMaxDepth\tContam\tTaxID\tType\n"); |
| 377 |
int i=0; |
| 378 |
for(BinStats b : bins) { |
| 379 |
if(b.size>=minSize) { |
| 380 |
bsw.print(i).tab().print(b.size).tab().print(b.contigs).tab(); |
| 381 |
bsw.print(b.gc, 3).tab().print(b.depth, 2).tab(); |
| 382 |
bsw.print(b.minDepth, 2).tab().print(b.maxDepth, 2).tab(); |
| 383 |
bsw.print(b.complt, 5).tab().print(b.contam, 5).tab(); |
| 384 |
bsw.print(b.taxid).tab().print(b.type()).nl(); |
| 385 |
i++; |
| 386 |
} |
| 387 |
} |
| 388 |
bsw.poison(); |
| 389 |
} |
| 390 |
|
| 391 |
static void printBinQuality(Collection<? extends Bin> bins, int minSize, PrintStream outstream) { |
| 392 |
ArrayList<BinStats> list=toStats(bins, minSize); |
| 393 |
printBinQuality(list, minSize, outstream); |
| 394 |
} |
| 395 |
|
| 396 |
static void printBinQuality(ArrayList<BinStats> bins, int minSize, PrintStream outstream) { |
| 397 |
long uhq=0, uhqINC=0, uhqCON=0; |
| 398 |
long vhq=0, vhqINC=0, vhqCON=0; |
| 399 |
long hq=0, hqINC=0, hqCON=0; |
| 400 |
long mq=0, mqINC=0, mqCON=0; |
| 401 |
long lq=0, lqINC=0, lqCON=0; |
| 402 |
long vlq=0, vlqINC=0, vlqCON=0; |
| 403 |
|
| 404 |
long uhqSize=0; |
| 405 |
long vhqSize=0; |
| 406 |
long hqSize=0; |
| 407 |
long mqSize=0; |
| 408 |
long lqSize=0; |
| 409 |
long vlqSize=0; |
| 410 |
|
| 411 |
for(BinStats b : bins) { |
| 412 |
final long size=b.size; |
| 413 |
final float comp=b.complt, contam=b.contam; |
| 414 |
if(size>=minSize) { |
| 415 |
if(contam<=0.05f && comp>=0.9f) { |
| 416 |
hq++; |
| 417 |
hqSize+=size; |
| 418 |
if(comp>=0.99f && contam<=0.01f) { |
| 419 |
uhq++; |
| 420 |
uhqSize+=size; |
| 421 |
if(comp<1) {uhqINC++;} |
| 422 |
if(contam>0) {uhqCON++;} |
| 423 |
}else if(comp>=0.95f && contam<=0.02f) { |
| 424 |
vhq++; |
| 425 |
vhqSize+=size; |
| 426 |
if(comp<.99f) {vhqINC++;} |
| 427 |
if(contam>0.01f) {vhqCON++;} |
| 428 |
}else { |
| 429 |
if(comp<.95f) {hqINC++;} |
| 430 |
if(contam>0.02f) {hqCON++;} |
| 431 |
} |
| 432 |
}else if(contam<0.10f && comp>=0.5f) { |
| 433 |
mq++; |
| 434 |
mqSize+=size; |
| 435 |
if(comp<.90f) {mqINC++;} |
| 436 |
if(contam>0.05f) {mqCON++;} |
| 437 |
}else { |
| 438 |
lq++; |
| 439 |
lqSize+=size; |
| 440 |
if(contam>0.20f || comp<0.20f) { |
| 441 |
vlq++; |
| 442 |
vlqSize+=size; |
| 443 |
if(comp<0.2f) {vlqINC++;} |
| 444 |
if(contam>0.2f) {vlqCON++;} |
| 445 |
}else { |
| 446 |
if(comp<0.5f) {lqINC++;} |
| 447 |
if(contam>0.1f) {lqCON++;} |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
vhq+=uhq; |
| 454 |
vhqSize+=uhqSize; |
| 455 |
|
| 456 |
outstream.println("Quality\tBins\tIncomp\tContam\tBases"); |
| 457 |
outstream.println("UHQ\t"+uhq+"\t"+uhqINC+"\t"+uhqCON+"\t"+uhqSize); |
| 458 |
outstream.println("VHQ\t"+vhq+"\t"+vhqINC+"\t"+vhqCON+"\t"+vhqSize); |
| 459 |
outstream.println("HQ\t"+hq+"\t"+hqINC+"\t"+hqCON+"\t"+hqSize); |
| 460 |
outstream.println("MQ\t"+mq+"\t"+mqINC+"\t"+mqCON+"\t"+mqSize); |
| 461 |
outstream.println("LQ\t"+lq+"\t"+lqINC+"\t"+lqCON+"\t"+lqSize); |
| 462 |
outstream.println("VLQ\t"+vlq+"\t"+vlqINC+"\t"+vlqCON+"\t"+vlqSize); |
| 463 |
String hqm=""+(hq+mq/4f); |
| 464 |
if(hqm.endsWith(".0")) {hqm=hqm.substring(0, hqm.length()-2);} |
| 465 |
outstream.println("HQ+MQ/4\t"+hqm+"\t\t\t"+(hqSize+mqSize/4)); |
| 466 |
} |
| 467 |
|
| 468 |
static void printL90FromBins(Collection<? extends Bin> bins, long basesLoaded) { |
| 469 |
LongList sizes=new LongList(bins.size()); |
| 470 |
for(Bin b : bins) { |
| 471 |
sizes.add(b.size()); |
| 472 |
} |
| 473 |
GradeBins.printL90(sizes, basesLoaded); |
| 474 |
} |
| 475 |
|
| 476 |
static void printL90(Collection<BinStats> bins, long basesLoaded) { |
| 477 |
LongList sizes=new LongList(bins.size()); |
| 478 |
for(BinStats b : bins) { |
| 479 |
sizes.add(b.size); |
| 480 |
} |
| 481 |
GradeBins.printL90(sizes, basesLoaded); |
| 482 |
} |
| 483 |
|
| 484 |
static void printL90(LongList list, long basesLoaded) { |
| 485 |
long c99=(long)(0.99f*basesLoaded); |
| 486 |
long c95=(long)(0.95f*basesLoaded); |
| 487 |
long c90=(long)(0.90f*basesLoaded); |
| 488 |
long c80=(long)(0.80f*basesLoaded); |
| 489 |
long c75=(long)(0.75f*basesLoaded); |
| 490 |
long c50=(long)(0.50f*basesLoaded); |
| 491 |
long c40=(long)(0.40f*basesLoaded); |
| 492 |
long c30=(long)(0.30f*basesLoaded); |
| 493 |
long c25=(long)(0.25f*basesLoaded); |
| 494 |
long c20=(long)(0.20f*basesLoaded); |
| 495 |
long c10=(long)(0.10f*basesLoaded); |
| 496 |
long c05=(long)(0.05f*basesLoaded); |
| 497 |
long c01=(long)(0.01f*basesLoaded); |
| 498 |
|
| 499 |
list.sort(); |
| 500 |
list.reverse(); |
| 501 |
long prev=0, sum2=0; |
| 502 |
for(int i=0; i<list.size(); i++) { |
| 503 |
long size=list.get(i); |
| 504 |
prev=sum2; |
| 505 |
sum2+=size; |
| 506 |
int num=i+1; |
| 507 |
|
| 508 |
if(sum2>=c01 && prev<c01) {System.err.println("L01: "+size+"\t"+"N01: "+num);} |
| 509 |
|
| 510 |
if(sum2>=c10 && prev<c10) {System.err.println("L10: "+size+"\t"+"N10: "+num);} |
| 511 |
if(sum2>=c20 && prev<c20) {System.err.println("L20: "+size+"\t"+"N20: "+num);} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
if(sum2>=c50 && prev<c50) {System.err.println("L50: "+size+"\t"+"N50: "+num);} |
| 516 |
|
| 517 |
|
| 518 |
if(sum2>=c90 && prev<c90) {System.err.println("L90: "+size+"\t"+"N90: "+num);} |
| 519 |
|
| 520 |
|
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
IntLongHashMap makeSizeMap(String fname) { |
| 525 |
FileFormat ffin=FileFormat.testInput(fname, FileFormat.FASTA, null, true, true); |
| 526 |
|
| 527 |
final ConcurrentReadInputStream cris; |
| 528 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin, null); |
| 529 |
cris.start(); |
| 530 |
|
| 531 |
IntLongHashMap map=new IntLongHashMap(); |
| 532 |
countMap=new IntHashMap(); |
| 533 |
long sizeSum=0, contigSum=0; |
| 534 |
{ |
| 535 |
ListNum<Read> ln=cris.nextList(); |
| 536 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 537 |
|
| 538 |
|
| 539 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 540 |
if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} |
| 541 |
|
| 542 |
for(int idx=0; idx<reads.size(); idx++){ |
| 543 |
final Read r=reads.get(idx); |
| 544 |
readsProcessed++; |
| 545 |
basesProcessed+=r.length(); |
| 546 |
sizeSum+=r.length(); |
| 547 |
contigSum++; |
| 548 |
|
| 549 |
|
| 550 |
int tid=DataLoader.parseTaxID(r.id); |
| 551 |
long ret=map.increment(tid, r.length()); |
| 552 |
countMap.increment(tid); |
| 553 |
if(ret==r.length() && tid>0) {taxIDsIn++;} |
| 554 |
} |
| 555 |
|
| 556 |
cris.returnList(ln); |
| 557 |
if(verbose){outstream.println("Returned a list.");} |
| 558 |
ln=cris.nextList(); |
| 559 |
reads=(ln!=null ? ln.list : null); |
| 560 |
} |
| 561 |
if(ln!=null){ |
| 562 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 563 |
} |
| 564 |
} |
| 565 |
if(totalSize==0) {totalSize=sizeSum;} |
| 566 |
if(totalContigs==0) {totalContigs=contigSum;} |
| 567 |
ReadWrite.closeStream(cris); |
| 568 |
return map; |
| 569 |
} |
| 570 |
|
| 571 |
private IntLongHashMap loadTaxIn(String fname) { |
| 572 |
ByteFile bf=ByteFile.makeByteFile(fname, true); |
| 573 |
LineParser1 lp=new LineParser1('\t'); |
| 574 |
|
| 575 |
IntLongHashMap map=new IntLongHashMap(); |
| 576 |
countMap=new IntHashMap(); |
| 577 |
long sizeSum=0, contigSum=0; |
| 578 |
for(ListNum<byte[]> ln=bf.nextList(); ln!=null; ln=bf.nextList()) { |
| 579 |
for(byte[] line : ln) { |
| 580 |
lp.set(line); |
| 581 |
if(!lp.startsWith('#')) { |
| 582 |
int tid=lp.parseInt(0); |
| 583 |
long size=lp.parseLong(1); |
| 584 |
int contigs=lp.parseInt(2); |
| 585 |
long ret=map.increment(tid, size); |
| 586 |
countMap.increment(tid, contigs); |
| 587 |
sizeSum+=size; |
| 588 |
contigSum+=contigs; |
| 589 |
if(ret==size && tid>0) {taxIDsIn++;} |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
if(totalSize==0) {totalSize=sizeSum;} |
| 594 |
if(totalContigs==0) {totalContigs=contigSum;} |
| 595 |
bf.close(); |
| 596 |
return map; |
| 597 |
} |
| 598 |
|
| 599 |
private void writeTaxOut(String fname, IntLongHashMap sizeMap, IntHashMap countMap) { |
| 600 |
ByteStreamWriter bsw=ByteStreamWriter.makeBSW(fname, overwrite, false, false); |
| 601 |
bsw.print("#taxID\tSize\tContigs\n"); |
| 602 |
int[] tids=sizeMap.toArray(); |
| 603 |
Arrays.sort(tids); |
| 604 |
for(int tid : tids) { |
| 605 |
bsw.print(tid).tab().print(sizeMap.get(tid)).tab().println(countMap.get(tid)); |
| 606 |
} |
| 607 |
bsw.poison(); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
public static HashMap<String, CCLine> loadCheckM(String fname){ |
| 613 |
if(fname==null) {return null;} |
| 614 |
File f=new File(fname); |
| 615 |
if(f.isDirectory()) { |
| 616 |
if(!fname.endsWith("/")) {fname=fname+"/";} |
| 617 |
fname=fname+"quality_report.tsv"; |
| 618 |
} |
| 619 |
ArrayList<byte[]> lines=ByteFile.toLines(fname); |
| 620 |
HashMap<String, CCLine> map=new HashMap<String, CCLine>(lines.size()); |
| 621 |
LineParser1 lp=new LineParser1('\t'); |
| 622 |
for(byte[] line : lines) { |
| 623 |
lp.set(line); |
| 624 |
if(!lp.startsWith("Name\t")) { |
| 625 |
String name=ReadWrite.stripToCore(lp.parseString(0)); |
| 626 |
float comp=lp.parseFloat(1)/100; |
| 627 |
float contam=lp.parseFloat(2)/100; |
| 628 |
comp=Tools.mid(0, 1, comp); |
| 629 |
contam=Tools.mid(0, 1, contam); |
| 630 |
assert(comp>=0 && comp<=1) : new String(line); |
| 631 |
|
| 632 |
|
| 633 |
CCLine cc=new CCLine(comp, contam); |
| 634 |
map.put(name, cc); |
| 635 |
} |
| 636 |
} |
| 637 |
return map; |
| 638 |
} |
| 639 |
|
| 640 |
public static HashMap<String, CCLine> loadEukCC(String fname){ |
| 641 |
if(fname==null) {return null;} |
| 642 |
File f=new File(fname); |
| 643 |
if(f.isDirectory()) { |
| 644 |
if(!fname.endsWith("/")) {fname=fname+"/";} |
| 645 |
fname=fname+"eukcc.csv"; |
| 646 |
} |
| 647 |
ArrayList<byte[]> lines=ByteFile.toLines(fname); |
| 648 |
HashMap<String, CCLine> map=new HashMap<String, CCLine>(lines.size()); |
| 649 |
LineParser1 lp=new LineParser1('\t'); |
| 650 |
for(byte[] line : lines) { |
| 651 |
lp.set(line); |
| 652 |
if(!lp.startsWith("bin\tcompleteness")) { |
| 653 |
String name=ReadWrite.stripToCore(lp.parseString(0)); |
| 654 |
float comp=lp.parseFloat(1)/100; |
| 655 |
float contam=lp.parseFloat(2)/100; |
| 656 |
comp=Tools.mid(0, 1, comp); |
| 657 |
contam=Tools.mid(0, 1, contam); |
| 658 |
assert(comp>=0 && comp<=1) : new String(line); |
| 659 |
|
| 660 |
|
| 661 |
CCLine cc=new CCLine(comp, contam); |
| 662 |
map.put(name, cc); |
| 663 |
} |
| 664 |
} |
| 665 |
return map; |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
@Override |
| 673 |
public void accumulate(ProcessThread t) { |
| 674 |
success=(success && t.success); |
| 675 |
} |
| 676 |
|
| 677 |
@Override |
| 678 |
public ReadWriteLock rwlock() { |
| 679 |
return null; |
| 680 |
} |
| 681 |
|
| 682 |
@Override |
| 683 |
public boolean success() { |
| 684 |
return success; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
static class ProcessThread extends Thread { |
| 694 |
|
| 695 |
ProcessThread(ArrayList<String> fnames_, ArrayList<BinStats> bins_, |
| 696 |
int tid_, int threads_){ |
| 697 |
fnames=fnames_; |
| 698 |
bins=bins_; |
| 699 |
tid=tid_; |
| 700 |
threads=threads_; |
| 701 |
} |
| 702 |
|
| 703 |
@Override |
| 704 |
public void run() { |
| 705 |
for(int i=tid; i<fnames.size(); i+=threads) { |
| 706 |
String fname=fnames.get(i); |
| 707 |
Cluster clust=loadCluster(fname); |
| 708 |
calcContam(fname, clust); |
| 709 |
BinStats bs=new BinStats(clust); |
| 710 |
synchronized(bins) { |
| 711 |
bins.add(bs); |
| 712 |
} |
| 713 |
} |
| 714 |
success=true; |
| 715 |
} |
| 716 |
|
| 717 |
private final ArrayList<String> fnames; |
| 718 |
private final ArrayList<BinStats> bins; |
| 719 |
private final int tid; |
| 720 |
private final int threads; |
| 721 |
boolean success=false; |
| 722 |
|
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
private static class CCLine { |
| 728 |
|
| 729 |
CCLine(float completeness_, float contam_) { |
| 730 |
this(completeness_, contam_, -1); |
| 731 |
} |
| 732 |
|
| 733 |
CCLine(float completeness_, float contam_, long size_) { |
| 734 |
completeness=completeness_; |
| 735 |
contam=contam_; |
| 736 |
size=size_; |
| 737 |
assert(completeness>=0 && completeness<=1) : completeness; |
| 738 |
assert(contam>=0 && contam<=1) : contam; |
| 739 |
assert(size>0 || size==-1); |
| 740 |
} |
| 741 |
|
| 742 |
public String toString() {return size+", "+completeness+", "+contam;} |
| 743 |
|
| 744 |
long size=-1; |
| 745 |
float completeness=-1; |
| 746 |
float contam=-1; |
| 747 |
|
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
private ArrayList<String> in=new ArrayList<String>(); |
| 753 |
private String taxIn=null; |
| 754 |
private String taxOut=null; |
| 755 |
private String tax=null; |
| 756 |
private String ref=null; |
| 757 |
private String hist=null; |
| 758 |
private String checkMFile=null; |
| 759 |
private String eukCCFile=null; |
| 760 |
private String report=null; |
| 761 |
private LongList sizes=new LongList(); |
| 762 |
private ArrayList<BinStats> bins=new ArrayList<BinStats>(); |
| 763 |
private double contamScore=0; |
| 764 |
private double compltScore=0; |
| 765 |
private int minSize=1; |
| 766 |
private boolean loadMT=true; |
| 767 |
|
| 768 |
private static IntLongHashMap sizeMap; |
| 769 |
private static IntHashMap countMap; |
| 770 |
private static HashMap<String, CCLine> checkMMap; |
| 771 |
private static HashMap<String, CCLine> eukCCMap; |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
private static long maxReads=-1; |
| 776 |
private long readsProcessed=0, basesProcessed=0; |
| 777 |
private long totalSize=0, totalContigs=0; |
| 778 |
private long taxIDsIn=0; |
| 779 |
boolean overwrite=true; |
| 780 |
boolean success=true; |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
private static java.io.PrintStream outstream=System.err; |
| 785 |
public static boolean verbose=false; |
| 786 |
|
| 787 |
} |