| 1 |
package clump; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
import java.util.concurrent.SynchronousQueue; |
| 8 |
import java.util.concurrent.atomic.AtomicInteger; |
| 9 |
|
| 10 |
import fileIO.ByteFile; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import jgi.BBMerge; |
| 14 |
import shared.Parse; |
| 15 |
import shared.Parser; |
| 16 |
import shared.PreParser; |
| 17 |
import shared.Shared; |
| 18 |
import shared.Timer; |
| 19 |
import shared.Tools; |
| 20 |
import stream.ConcurrentReadInputStream; |
| 21 |
import stream.ConcurrentReadOutputStream; |
| 22 |
import stream.FASTQ; |
| 23 |
import stream.FastaReadInputStream; |
| 24 |
import stream.Read; |
| 25 |
import structures.ListNum; |
| 26 |
import tracker.ReadStats; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class KmerSort3 extends KmerSort { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static void main(String[] args){ |
| 44 |
assert(false); |
| 45 |
main(-1, 1, 1, args); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static void main(long fileMem, int outerPassNum, int outerPasses, String[] args){ |
| 53 |
final boolean pigz=ReadWrite.USE_PIGZ, unpigz=ReadWrite.USE_UNPIGZ; |
| 54 |
final float ztd=ReadWrite.ZIP_THREAD_MULT(); |
| 55 |
final int mzt=ReadWrite.MAX_ZIP_THREADS(); |
| 56 |
final int oldzl=ReadWrite.ZIPLEVEL; |
| 57 |
Timer t=new Timer(); |
| 58 |
KmerSort3 x=new KmerSort3(fileMem, outerPassNum, outerPasses, args); |
| 59 |
x.process(t); |
| 60 |
ReadWrite.USE_PIGZ=pigz; |
| 61 |
ReadWrite.USE_UNPIGZ=unpigz; |
| 62 |
ReadWrite.setZipThreadMult(ztd); |
| 63 |
ReadWrite.setZipThreads(mzt); |
| 64 |
ReadWrite.ZIPLEVEL=oldzl; |
| 65 |
|
| 66 |
|
| 67 |
Shared.closeStream(x.outstream); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public KmerSort3(long fileMem_, int outerPassNum_, int outerPasses_, String[] args){ |
| 75 |
|
| 76 |
{ |
| 77 |
PreParser pp=new PreParser(args, getClass(), false); |
| 78 |
args=pp.args; |
| 79 |
outstream=pp.outstream; |
| 80 |
} |
| 81 |
|
| 82 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 83 |
ReadWrite.setZipThreads(Shared.threads()); |
| 84 |
|
| 85 |
outerPassNum=outerPassNum_; |
| 86 |
outerPasses=outerPasses_; |
| 87 |
Parser parser=new Parser(); |
| 88 |
for(int i=0; i<args.length; i++){ |
| 89 |
String arg=args[i]; |
| 90 |
String[] split=arg.split("="); |
| 91 |
String a=split[0].toLowerCase(); |
| 92 |
String b=split.length>1 ? split[1] : null; |
| 93 |
|
| 94 |
if(a.equals("verbose")){ |
| 95 |
verbose=KmerComparator.verbose=Parse.parseBoolean(b); |
| 96 |
}else if(a.equals("parse_flag_goes_here")){ |
| 97 |
|
| 98 |
}else if(a.equals("k")){ |
| 99 |
k=Integer.parseInt(b); |
| 100 |
assert(k>0 && k<32); |
| 101 |
}else if(a.equals("mincount") || a.equals("mincr")){ |
| 102 |
minCount=Integer.parseInt(b); |
| 103 |
}else if(a.equals("rename") || a.equals("addname")){ |
| 104 |
addName=Parse.parseBoolean(b); |
| 105 |
}else if(a.equals("shortname") || a.equals("shortnames")){ |
| 106 |
if(b!=null && b.equals("shrink")){ |
| 107 |
shrinkName=true; |
| 108 |
}else{ |
| 109 |
shrinkName=false; |
| 110 |
shortName=Parse.parseBoolean(b); |
| 111 |
} |
| 112 |
}else if(a.equals("rcomp") || a.equals("reversecomplement")){ |
| 113 |
rcomp=Parse.parseBoolean(b); |
| 114 |
}else if(a.equals("ecco")){ |
| 115 |
ecco=Parse.parseBoolean(b); |
| 116 |
}else if(a.equals("condense") || a.equals("consensus") || a.equals("concensus")){ |
| 117 |
condense=Parse.parseBoolean(b); |
| 118 |
}else if(a.equals("correct") || a.equals("ecc")){ |
| 119 |
correct=Parse.parseBoolean(b); |
| 120 |
}else if(a.equals("passes")){ |
| 121 |
passes=Integer.parseInt(b); |
| 122 |
} |
| 123 |
|
| 124 |
else if(a.equals("dedupe")){ |
| 125 |
dedupe=Parse.parseBoolean(b); |
| 126 |
}else if(a.equals("markduplicates")){ |
| 127 |
dedupe=Clump.markOnly=Parse.parseBoolean(b); |
| 128 |
}else if(a.equals("markall")){ |
| 129 |
boolean x=Parse.parseBoolean(b); |
| 130 |
if(x){ |
| 131 |
dedupe=Clump.markOnly=Clump.markAll=true; |
| 132 |
}else{ |
| 133 |
Clump.markAll=false; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
else if(a.equals("prefilter")){ |
| 138 |
KmerReduce.prefilter=Parse.parseBoolean(b); |
| 139 |
}else if(a.equals("entryfilter")){ |
| 140 |
entryfilter=Parse.parseBoolean(b); |
| 141 |
}else if(a.equals("groups") || a.equals("g") || a.equals("sets") || a.equals("ways")){ |
| 142 |
groups=Integer.parseInt(b); |
| 143 |
splitInput=(groups>1); |
| 144 |
}else if(a.equals("seed")){ |
| 145 |
KmerComparator.defaultSeed=Long.parseLong(b); |
| 146 |
}else if(a.equals("hashes")){ |
| 147 |
KmerComparator.setHashes(Integer.parseInt(b)); |
| 148 |
}else if(a.equals("border")){ |
| 149 |
KmerComparator.defaultBorder=Integer.parseInt(b); |
| 150 |
}else if(a.equals("minprob")){ |
| 151 |
KmerComparator.minProb=Float.parseFloat(b); |
| 152 |
|
| 153 |
}else if(a.equals("unpair")){ |
| 154 |
unpair=Parse.parseBoolean(b); |
| 155 |
}else if(a.equals("repair")){ |
| 156 |
repair=Parse.parseBoolean(b); |
| 157 |
}else if(a.equals("namesort") || a.equals("sort")){ |
| 158 |
namesort=Parse.parseBoolean(b); |
| 159 |
}else if(a.equals("fetchthreads")){ |
| 160 |
fetchThreads=Integer.parseInt(b); |
| 161 |
assert(fetchThreads>0) : fetchThreads+"\nFetch threads must be at least 1."; |
| 162 |
}else if(a.equals("reorder") || a.equals("reorderclumps")){ |
| 163 |
|
| 164 |
}else if(a.equals("reorderpaired") || a.equals("reorderclumpspaired")){ |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
else if(Clump.parseStatic(arg, a, b)){ |
| 169 |
|
| 170 |
}else if(parser.parse(arg, a, b)){ |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
else{ |
| 175 |
outstream.println("Unknown parameter "+args[i]); |
| 176 |
assert(false) : "Unknown parameter "+args[i]; |
| 177 |
|
| 178 |
} |
| 179 |
} |
| 180 |
Clump.renameConsensus=condense; |
| 181 |
if(dedupe){KmerComparator.compareSequence=true;} |
| 182 |
assert(!(reorderMode==REORDER_PAIRED && dedupe)) : "REORDER_PAIRED and dedupe are incompatible."; |
| 183 |
|
| 184 |
{ |
| 185 |
Parser.processQuality(); |
| 186 |
|
| 187 |
maxReads=parser.maxReads; |
| 188 |
|
| 189 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 190 |
append=ReadStats.append=parser.append; |
| 191 |
|
| 192 |
in1=parser.in1; |
| 193 |
in2=parser.in2; |
| 194 |
|
| 195 |
out1=parser.out1; |
| 196 |
out2=parser.out2; |
| 197 |
|
| 198 |
extin=parser.extin; |
| 199 |
extout=parser.extout; |
| 200 |
} |
| 201 |
|
| 202 |
assert(FastaReadInputStream.settingsOK()); |
| 203 |
|
| 204 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 205 |
in2=in1.replace("#", "2"); |
| 206 |
in1=in1.replace("#", "1"); |
| 207 |
} |
| 208 |
if(in2!=null){ |
| 209 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 210 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 211 |
} |
| 212 |
|
| 213 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 214 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 215 |
ByteFile.FORCE_MODE_BF2=true; |
| 216 |
} |
| 217 |
|
| 218 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 219 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 220 |
out2=out1.replace("#", "2"); |
| 221 |
out1=out1.replace("#", "1"); |
| 222 |
} |
| 223 |
|
| 224 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 225 |
outstream.println((out1==null)+", "+out1); |
| 226 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 227 |
} |
| 228 |
|
| 229 |
if(out1==null){ffout1=ffout2=null;} |
| 230 |
else{ |
| 231 |
int g=out1.contains("%") ? groups : 1; |
| 232 |
ffout1=new FileFormat[g]; |
| 233 |
ffout2=new FileFormat[g]; |
| 234 |
if(g==1){ |
| 235 |
ffout1[0]=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 236 |
ffout2[0]=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 237 |
}else{ |
| 238 |
ReadWrite.ZIPLEVEL=2; |
| 239 |
ReadWrite.setZipThreadMult(Tools.min(0.5f, 2f/(g+1))); |
| 240 |
for(int i=0; i<g; i++){ |
| 241 |
ffout1[i]=FileFormat.testOutput(out1.replaceFirst("%", ""+i), FileFormat.FASTQ, extout, (g<10), overwrite, append, false); |
| 242 |
ffout2[i]=(out2==null ? null : FileFormat.testOutput(out2.replaceFirst("%", ""+i), FileFormat.FASTQ, extout, (g<10), overwrite, append, false)); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if(groups>1 && in1.contains("%") && (splitInput || !new File(in1).exists())){ |
| 248 |
ffin1=new FileFormat[groups]; |
| 249 |
ffin2=new FileFormat[groups]; |
| 250 |
for(int i=0; i<groups; i++){ |
| 251 |
ffin1[i]=FileFormat.testInput(in1.replaceFirst("%", ""+i), FileFormat.FASTQ, extin, true, true); |
| 252 |
ffin2[i]=in2==null ? null : FileFormat.testInput(in2.replaceFirst("%", ""+i), FileFormat.FASTQ, extin, true, true); |
| 253 |
} |
| 254 |
}else{ |
| 255 |
assert(!in1.contains("%") && groups==1) : "The % symbol must only be present in the input filename if groups>1."; |
| 256 |
ffin1=new FileFormat[1]; |
| 257 |
ffin1[0]=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 258 |
ffin2=new FileFormat[1]; |
| 259 |
ffin2[0]=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 260 |
groups=1; |
| 261 |
} |
| 262 |
|
| 263 |
long sizeSum=0, expectedMemSum=0; |
| 264 |
for(FileFormat ff : ffin1){ |
| 265 |
long x=new File(ff.name()).length(); |
| 266 |
sizeSum+=x; |
| 267 |
if(ff.compressed()){x*=40;}else{x*=8;} |
| 268 |
expectedMemSum+=x; |
| 269 |
} |
| 270 |
for(FileFormat ff : ffin2){ |
| 271 |
if(ff!=null){ |
| 272 |
long x=new File(ff.name()).length(); |
| 273 |
sizeSum+=x; |
| 274 |
if(ff.compressed()){x*=40;}else{x*=8;} |
| 275 |
expectedMemSum+=x; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
expectedSizePerGroup=(sizeSum+groups+1)/(Tools.max(groups, 1)); |
| 280 |
expectedMemPerGroup=(expectedMemSum+groups+1)/(Tools.max(groups, 1)); |
| 281 |
totalMem=Shared.memAvailable(1); |
| 282 |
fileSize=sizeSum; |
| 283 |
fileMem=fileMem_<1 ? 40*fileSize : fileMem_; |
| 284 |
memRatio=fileMem*1.0/Tools.max(1, fileSize); |
| 285 |
|
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
@Override |
| 295 |
void process(Timer t){ |
| 296 |
|
| 297 |
preprocess(); |
| 298 |
|
| 299 |
final ConcurrentReadOutputStream[] rosa=(ffout1==null ? null : new ConcurrentReadOutputStream[ffout1.length]); |
| 300 |
for(int i=0; rosa!=null && i<rosa.length; i++){ |
| 301 |
final int buff=1; |
| 302 |
|
| 303 |
assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 304 |
|
| 305 |
rosa[i]=ConcurrentReadOutputStream.getStream(ffout1[i], ffout2[i], null, null, buff, null, false); |
| 306 |
rosa[i].start(); |
| 307 |
} |
| 308 |
|
| 309 |
readsProcessed=basesProcessed=diskProcessed=memProcessed=0; |
| 310 |
|
| 311 |
|
| 312 |
processInner(rosa); |
| 313 |
lastMemProcessed=memThisPass; |
| 314 |
|
| 315 |
printStats(t); |
| 316 |
|
| 317 |
if(outerPassNum<outerPasses){outstream.println();} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
void processInner(final ConcurrentReadOutputStream rosa[]){ |
| 322 |
if(verbose){outstream.println("Making comparator.");} |
| 323 |
KmerComparator kc=new KmerComparator(k, addName, (rcomp || condense || correct)); |
| 324 |
|
| 325 |
ClumpList.UNRCOMP=(!rcomp && !condense); |
| 326 |
Timer t=new Timer(); |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
useSharedHeader=(ffin1[0].samOrBam() && ffout1!=null && ffout1[0]!=null && ffout1[0].samOrBam()); |
| 332 |
|
| 333 |
fetchThreads=Tools.min(groups, fetchThreads, ffin1.length); |
| 334 |
assert(fetchThreads>0); |
| 335 |
SynchronousQueue<ArrayList<Read>> listQ=new SynchronousQueue<ArrayList<Read>>(); |
| 336 |
ArrayList<FetchThread3> alft=fetchReads(kc, fetchThreads, listQ, rosa); |
| 337 |
int poisonCount=0; |
| 338 |
|
| 339 |
for(int group=0; group<groups; group++){ |
| 340 |
|
| 341 |
if(verbose){t.start("Fetching reads.");} |
| 342 |
ArrayList<Read> reads=null; |
| 343 |
|
| 344 |
|
| 345 |
while(poisonCount<fetchThreads && reads==null){ |
| 346 |
try { |
| 347 |
reads=listQ.take(); |
| 348 |
} catch (InterruptedException e) { |
| 349 |
|
| 350 |
e.printStackTrace(); |
| 351 |
} |
| 352 |
if(reads==POISON){ |
| 353 |
assert(reads.isEmpty()); |
| 354 |
poisonCount++; |
| 355 |
if(verbose){System.err.println("Encountered poison; count="+poisonCount);} |
| 356 |
reads=null; |
| 357 |
} |
| 358 |
} |
| 359 |
if(verbose){t.stop("Fetched "+(reads==null ? 0 : reads.size())+" reads: ");} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
if(verbose){t.start("Making clumps.");} |
| 367 |
readsProcessedThisPass=reads.size(); |
| 368 |
ClumpList cl=new ClumpList(reads, k, false); |
| 369 |
clumpsProcessedThisPass=cl.size(); |
| 370 |
clumpsProcessedTotal+=clumpsProcessedThisPass; |
| 371 |
if(verbose){t.stop("Clump time: ");} |
| 372 |
|
| 373 |
if(dedupe){ |
| 374 |
reads.clear(); |
| 375 |
if(verbose){t.start("Deduping.");} |
| 376 |
reads=processClumps(cl, ClumpList.DEDUPE); |
| 377 |
if(verbose){t.stop("Dedupe time: ");} |
| 378 |
}else if(condense){ |
| 379 |
reads.clear(); |
| 380 |
if(verbose){t.start("Condensing.");} |
| 381 |
reads=processClumps(cl, ClumpList.CONDENSE); |
| 382 |
if(verbose){t.stop("Condense time: ");} |
| 383 |
}else if(correct){ |
| 384 |
reads.clear(); |
| 385 |
if(verbose){t.start("Correcting.");} |
| 386 |
reads=processClumps(cl, ClumpList.CORRECT); |
| 387 |
if(verbose){t.stop("Correct time: ");} |
| 388 |
|
| 389 |
if(verbose){outstream.println("Seed: "+kc.seed);} |
| 390 |
if(groups>1){ |
| 391 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 392 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 393 |
if(correct){ |
| 394 |
outstream.println("Corrections: \t"+correctionsThisPass); |
| 395 |
} |
| 396 |
outstream.println(); |
| 397 |
} |
| 398 |
|
| 399 |
if(passes>1 && groups==1){ |
| 400 |
|
| 401 |
FASTQ.DETECT_QUALITY=FASTQ.DETECT_QUALITY_OUT=false; |
| 402 |
FASTQ.ASCII_OFFSET=FASTQ.ASCII_OFFSET_OUT; |
| 403 |
|
| 404 |
if(verbose){outstream.println("Pass 1.");} |
| 405 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 406 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 407 |
if(correct){ |
| 408 |
outstream.println("Corrections: \t"+correctionsThisPass); |
| 409 |
} |
| 410 |
outstream.println(); |
| 411 |
|
| 412 |
for(int pass=1; pass<passes; pass++){ |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
kc=new KmerComparator(k, kc.seed<0 ? -1 : kc.seed+1, kc.border-1, kc.hashes, false, kc.rcompReads); |
| 417 |
reads=runOnePass(reads, kc); |
| 418 |
|
| 419 |
if(verbose){outstream.println("Seed: "+kc.seed);} |
| 420 |
if(verbose){outstream.println("Pass "+(pass+1)+".");} |
| 421 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 422 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 423 |
if(correct){ |
| 424 |
outstream.println("Corrections: \t"+correctionsThisPass); |
| 425 |
} |
| 426 |
outstream.println(); |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if(repair || namesort){ |
| 432 |
if(groups>1){ |
| 433 |
if(verbose){t.start("Name-sorting.");} |
| 434 |
reads=nameSort(reads, false); |
| 435 |
if(verbose){t.stop("Sort time: ");} |
| 436 |
}else{ |
| 437 |
if(namesort){ |
| 438 |
if(verbose){t.start("Name-sorting.");} |
| 439 |
reads=idSort(reads, repair); |
| 440 |
if(verbose){t.stop("Sort time: ");} |
| 441 |
}else{ |
| 442 |
reads=read1Only(reads); |
| 443 |
} |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
for(Read r : reads){ |
| 448 |
readsOut+=r.pairCount(); |
| 449 |
basesOut+=r.pairLength(); |
| 450 |
} |
| 451 |
|
| 452 |
if(doHashAndSplit || groups==0){ |
| 453 |
addToRos(rosa, reads, t, kc); |
| 454 |
}else{ |
| 455 |
if(group>0){ |
| 456 |
ConcurrentReadOutputStream ros=rosa[group-1]; |
| 457 |
errorState|=ReadWrite.closeStream(ros); |
| 458 |
} |
| 459 |
rosa[group].add(reads, 0); |
| 460 |
} |
| 461 |
reads=null; |
| 462 |
} |
| 463 |
|
| 464 |
if(verbose){outstream.println("Closing fetch threads.");} |
| 465 |
while(poisonCount<fetchThreads){ |
| 466 |
ArrayList<Read> reads=null; |
| 467 |
try { |
| 468 |
reads=listQ.take(); |
| 469 |
} catch (InterruptedException e) { |
| 470 |
|
| 471 |
e.printStackTrace(); |
| 472 |
} |
| 473 |
if(reads==POISON){ |
| 474 |
poisonCount++; |
| 475 |
reads=null; |
| 476 |
} |
| 477 |
} |
| 478 |
long readsThisPass=closeFetchThread3s(alft); |
| 479 |
if(verbose){outstream.println("Closed fetch threads.");} |
| 480 |
|
| 481 |
if(rosa!=null){ |
| 482 |
if(verbose){outstream.println("Waiting for writing to complete.");} |
| 483 |
for(ConcurrentReadOutputStream ros : rosa){ |
| 484 |
errorState=ReadWrite.closeStream(ros)|errorState; |
| 485 |
} |
| 486 |
if(verbose){t.stop("Write time: ");} |
| 487 |
} |
| 488 |
|
| 489 |
if(verbose && outerPassNum==outerPasses){outstream.println("Done!");} |
| 490 |
} |
| 491 |
|
| 492 |
private void addToRos(ConcurrentReadOutputStream[] rosa, ArrayList<Read> list, Timer t, KmerComparator old){ |
| 493 |
if(rosa==null){return;} |
| 494 |
assert(rosa.length>0); |
| 495 |
if(rosa.length==1){ |
| 496 |
if(verbose){t.start("Writing.");} |
| 497 |
rosa[0].add(list, 0); |
| 498 |
return; |
| 499 |
} |
| 500 |
KmerComparator kc=new KmerComparator(old.k, old.seed+1, old.border-1, old.hashes, false, false); |
| 501 |
final int div=rosa.length; |
| 502 |
assert(div==groups); |
| 503 |
@SuppressWarnings("unchecked") |
| 504 |
ArrayList<Read>[] array=new ArrayList[div]; |
| 505 |
for(int i=0; i<array.length; i++){ |
| 506 |
array[i]=new ArrayList<Read>(); |
| 507 |
} |
| 508 |
if(verbose){t.start("Splitting.");} |
| 509 |
hashAndSplit(list, kc, array); |
| 510 |
if(verbose){t.stop("Split time: ");} |
| 511 |
if(verbose){t.start("Writing.");} |
| 512 |
for(int i=0; i<div; i++){ |
| 513 |
rosa[i].add(array[i], 0); |
| 514 |
array[i]=null; |
| 515 |
} |
| 516 |
if(verbose){System.err.println("Sent writable reads.");} |
| 517 |
} |
| 518 |
|
| 519 |
public ArrayList<FetchThread3> fetchReads(final KmerComparator kc, final int fetchThreads, SynchronousQueue<ArrayList<Read>> listQ, ConcurrentReadOutputStream[] rosa){ |
| 520 |
AtomicInteger nextGroup=new AtomicInteger(0); |
| 521 |
if(verbose){outstream.println("Making "+fetchThreads+" fetch thread"+(fetchThreads==1 ? "." : "s."));} |
| 522 |
ArrayList<FetchThread3> alft=new ArrayList<FetchThread3>(fetchThreads); |
| 523 |
for(int i=0; i<fetchThreads; i++){alft.add(new FetchThread3(kc, listQ, nextGroup, rosa));} |
| 524 |
|
| 525 |
if(verbose){outstream.println("Starting threads.");} |
| 526 |
for(FetchThread3 ft : alft){ft.start();} |
| 527 |
|
| 528 |
assert(alft.size()==fetchThreads); |
| 529 |
|
| 530 |
return alft; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
private long closeFetchThread3s(ArrayList<FetchThread3> alft){ |
| 535 |
readsThisPass=0; |
| 536 |
memThisPass=0; |
| 537 |
entryFilteredThisPass=0; |
| 538 |
|
| 539 |
for(FetchThread3 ft : alft){ |
| 540 |
|
| 541 |
|
| 542 |
while(ft.getState()!=Thread.State.TERMINATED){ |
| 543 |
try { |
| 544 |
ft.join(); |
| 545 |
} catch (InterruptedException e) { |
| 546 |
e.printStackTrace(); |
| 547 |
} |
| 548 |
} |
| 549 |
entryFilteredThisPass+=ft.entryFilteredT; |
| 550 |
readsThisPass+=ft.readsProcessedT; |
| 551 |
basesProcessed+=ft.basesProcessedT; |
| 552 |
diskProcessed+=ft.diskProcessedT; |
| 553 |
memThisPass+=ft.memProcessedT; |
| 554 |
|
| 555 |
errorState|=ft.errorStateT; |
| 556 |
} |
| 557 |
entryFiltered+=entryFilteredThisPass; |
| 558 |
readsProcessed+=readsThisPass; |
| 559 |
memProcessed+=memThisPass; |
| 560 |
|
| 561 |
ecco=false; |
| 562 |
return readsThisPass; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
private class FetchThread3 extends Thread{ |
| 574 |
|
| 575 |
FetchThread3(final KmerComparator kc_, SynchronousQueue<ArrayList<Read>> listQ_, AtomicInteger nextGroup_, ConcurrentReadOutputStream[] rosa_){ |
| 576 |
kc=kc_; |
| 577 |
listQ=listQ_; |
| 578 |
nextGroup=nextGroup_; |
| 579 |
rosa=rosa_; |
| 580 |
} |
| 581 |
|
| 582 |
@Override |
| 583 |
public void run(){ |
| 584 |
for(ArrayList<Read> reads=fetchNext(); reads!=null; reads=fetchNext()){ |
| 585 |
boolean success=false; |
| 586 |
while(!success){ |
| 587 |
try { |
| 588 |
listQ.put(reads); |
| 589 |
success=true; |
| 590 |
} catch (InterruptedException e) { |
| 591 |
|
| 592 |
e.printStackTrace(); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
if(verbose){System.err.println("No more reads to fetch.");} |
| 597 |
boolean success=false; |
| 598 |
while(!success){ |
| 599 |
try { |
| 600 |
if(verbose){System.err.println("Adding poison.");} |
| 601 |
listQ.put(POISON); |
| 602 |
success=true; |
| 603 |
} catch (InterruptedException e) { |
| 604 |
|
| 605 |
e.printStackTrace(); |
| 606 |
} |
| 607 |
} |
| 608 |
if(verbose){System.err.println("A fetch thread finished.");} |
| 609 |
} |
| 610 |
|
| 611 |
private ArrayList<Read> fetchNext(){ |
| 612 |
final int group=nextGroup.getAndIncrement(); |
| 613 |
if(group>=groups){return null;} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
final ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, false, ffin1[group], ffin2[group], null, null); |
| 618 |
cris.start(); |
| 619 |
|
| 620 |
|
| 621 |
if(!Clump.forceProcess){ |
| 622 |
final long size; |
| 623 |
double expectedMem; |
| 624 |
{ |
| 625 |
size=new File(ffin1[group].name()).length()+(ffin2[group]==null ? 0 : new File(ffin2[group].name()).length()); |
| 626 |
|
| 627 |
expectedMem=(fileMem*(double)size)/fileSize; |
| 628 |
} |
| 629 |
|
| 630 |
if(size-1000>expectedSizePerGroup*3 || expectedMem*3>totalMem){ |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
if(expectedMem>0.3*totalMem && (fileMem<1 || fileMem>0.8*totalMem)){ |
| 639 |
synchronized(this.getClass()){ |
| 640 |
if(!memWarned){ |
| 641 |
outstream.println("\n*** Warning ***\n" |
| 642 |
+ "A temp file may be too big to store in memory, due to uneven splitting:"); |
| 643 |
|
| 644 |
outstream.println("expectedMem="+((long)expectedMem)+", fileMem="+fileMem+", available="+totalMem); |
| 645 |
} |
| 646 |
if(entryfilter){ |
| 647 |
if(!memWarned){ |
| 648 |
outstream.println( |
| 649 |
"If the program crashes, increase memory or manually increase groups.\n"); |
| 650 |
} |
| 651 |
}else if(dedupe & !Clump.opticalOnly){ |
| 652 |
if(!memWarned){ |
| 653 |
outstream.println( |
| 654 |
"entryfilter is automatically being set to true to reduce memory load while removing duplicates.\n" |
| 655 |
+"If the program crashes, increase memory or manually increase groups.\n"); |
| 656 |
} |
| 657 |
entryfilter=true; |
| 658 |
}else if(repair || namesort){ |
| 659 |
if(!memWarned){ |
| 660 |
outstream.println("It cannot be streamed to output unaltered because "+(namesort ? " namesort=t" : "repair=t")); |
| 661 |
outstream.println("If this causes the program to crash, please re-run with more memory or groups.\n"); |
| 662 |
} |
| 663 |
}else{ |
| 664 |
if(!memWarned){ |
| 665 |
outstream.println( |
| 666 |
"It will be streamed to output unaltered.\n" |
| 667 |
+ "To avoid this behavior, increase memory or increase groups.\n" |
| 668 |
+ "Set the flag forceprocess=t to disable this check.\n"); |
| 669 |
} |
| 670 |
|
| 671 |
ArrayList<Read> list=streamNext_inner(cris); |
| 672 |
|
| 673 |
return list; |
| 674 |
} |
| 675 |
memWarned=true; |
| 676 |
} |
| 677 |
} |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
return fetchNext_inner(cris); |
| 682 |
} |
| 683 |
|
| 684 |
private ArrayList<Read> streamNext_inner(ConcurrentReadInputStream cris){ |
| 685 |
StreamToOutput sto=new StreamToOutput(cris, rosa, kc, (repair || namesort), false); |
| 686 |
errorStateT|=sto.process(); |
| 687 |
readsProcessed+=sto.readsIn; |
| 688 |
basesProcessed+=sto.basesIn; |
| 689 |
readsOut+=sto.readsIn; |
| 690 |
basesOut+=sto.basesIn; |
| 691 |
|
| 692 |
return new ArrayList<Read>(); |
| 693 |
} |
| 694 |
|
| 695 |
private ArrayList<Read> fetchNext_inner(ConcurrentReadInputStream cris){ |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
final int subthreads=Tools.mid(1, Shared.threads()/2, 8); |
| 700 |
ArrayList<FetchSubThread> alht=new ArrayList<FetchSubThread>(subthreads); |
| 701 |
long readsThisGroup=0, memThisGroup=0, entryFilteredThisGroup=0; |
| 702 |
for(int i=0; i<subthreads; i++){alht.add(new FetchSubThread(i, cris, kc, unpair));} |
| 703 |
|
| 704 |
|
| 705 |
for(FetchSubThread ht : alht){ht.start();} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
for(FetchSubThread ht : alht){ |
| 710 |
|
| 711 |
|
| 712 |
while(ht.getState()!=Thread.State.TERMINATED){ |
| 713 |
try { |
| 714 |
ht.join(); |
| 715 |
} catch (InterruptedException e) { |
| 716 |
e.printStackTrace(); |
| 717 |
} |
| 718 |
} |
| 719 |
entryFilteredThisGroup+=ht.entryFilteredT; |
| 720 |
readsThisGroup+=ht.readsProcessedST; |
| 721 |
basesProcessedT+=ht.basesProcessedST; |
| 722 |
diskProcessedT+=ht.diskProcessedST; |
| 723 |
memThisGroup+=ht.memProcessedST; |
| 724 |
} |
| 725 |
entryFilteredT+=entryFilteredThisGroup; |
| 726 |
readsProcessedT+=readsThisGroup; |
| 727 |
memProcessedT+=memThisGroup; |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
errorStateT=ReadWrite.closeStream(cris)|errorStateT; |
| 732 |
|
| 733 |
|
| 734 |
long readsLeft=readsThisGroup-entryFilteredThisGroup; |
| 735 |
long slotsLeft=cris.paired() && !unpair ? readsLeft/2 : readsLeft; |
| 736 |
assert(slotsLeft<=Shared.MAX_ARRAY_LEN && readsProcessedT>=0); |
| 737 |
ArrayList<Read> list=new ArrayList<Read>((int)slotsLeft); |
| 738 |
for(int i=0; i<subthreads; i++){ |
| 739 |
FetchSubThread ht=alht.set(i, null); |
| 740 |
list.addAll(ht.storage); |
| 741 |
} |
| 742 |
assert(list.size()==slotsLeft) : list.size()+", "+readsThisGroup+", "+slotsLeft+", "+cris.paired(); |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
Shared.sort(list, kc); |
| 747 |
|
| 748 |
|
| 749 |
return list; |
| 750 |
} |
| 751 |
|
| 752 |
final SynchronousQueue<ArrayList<Read>> listQ; |
| 753 |
final AtomicInteger nextGroup; |
| 754 |
final KmerComparator kc; |
| 755 |
final ConcurrentReadOutputStream[] rosa; |
| 756 |
|
| 757 |
protected long entryFilteredT=0; |
| 758 |
protected long readsProcessedT=0; |
| 759 |
protected long basesProcessedT=0; |
| 760 |
protected long diskProcessedT=0; |
| 761 |
protected long memProcessedT=0; |
| 762 |
protected boolean errorStateT=false; |
| 763 |
|
| 764 |
|
| 765 |
private class FetchSubThread extends Thread{ |
| 766 |
|
| 767 |
FetchSubThread(int id_, ConcurrentReadInputStream cris_, KmerComparator kc_, boolean unpair_){ |
| 768 |
id=id_; |
| 769 |
cris=cris_; |
| 770 |
kcT=kc_; |
| 771 |
storage=new ArrayList<Read>(); |
| 772 |
unpairT=unpair_; |
| 773 |
entryFilterTable=(entryfilter ? new HashMap<Long, Read>() : null); |
| 774 |
} |
| 775 |
|
| 776 |
@Override |
| 777 |
public void run(){ |
| 778 |
ListNum<Read> ln=cris.nextList(); |
| 779 |
final boolean paired=cris.paired(); |
| 780 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 781 |
|
| 782 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 783 |
|
| 784 |
for(Read r : reads){ |
| 785 |
if(!r.validated()){ |
| 786 |
r.validate(true); |
| 787 |
if(r.mate!=null){r.mate.validate(true);} |
| 788 |
} |
| 789 |
readsProcessedST+=1+r.mateCount(); |
| 790 |
basesProcessedST+=r.length()+r.mateLength(); |
| 791 |
diskProcessedST+=r.countFastqBytes()+r.countMateFastqBytes(); |
| 792 |
memProcessedST+=r.countBytes()+r.countMateBytes()+ReadKey.overhead; |
| 793 |
if(shrinkName){ |
| 794 |
Clumpify.shrinkName(r); |
| 795 |
Clumpify.shrinkName(r.mate); |
| 796 |
}else if(shortName){ |
| 797 |
Clumpify.shortName(r); |
| 798 |
Clumpify.shortName(r.mate); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
if(ecco){ |
| 803 |
for(Read r : reads){ |
| 804 |
Read r2=r.mate; |
| 805 |
assert(r.obj==null) : "TODO: Pivot should not have been generated yet, though it may be OK."; |
| 806 |
assert(r2!=null) : "ecco requires paired reads."; |
| 807 |
if(r2!=null){ |
| 808 |
int x=BBMerge.findOverlapStrict(r, r2, true); |
| 809 |
if(x>=0){ |
| 810 |
r.obj=null; |
| 811 |
r2.obj=null; |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
if(entryFilterTable!=null){ |
| 818 |
int removed=0; |
| 819 |
for(int i=0; i<reads.size(); i++){ |
| 820 |
Read r=reads.get(i); |
| 821 |
final long key=Hasher.hashPair(r); |
| 822 |
final Long key2=Long.valueOf(key); |
| 823 |
final Read old=entryFilterTable.get(key2); |
| 824 |
if(old==null){ |
| 825 |
entryFilterTable.put(key2, r); |
| 826 |
}else{ |
| 827 |
boolean same=Hasher.equalsPaired(r, old); |
| 828 |
if(same){ |
| 829 |
removed++; |
| 830 |
entryFilteredT+=r.pairCount(); |
| 831 |
reads.set(i, null); |
| 832 |
} |
| 833 |
} |
| 834 |
} |
| 835 |
if(removed>0){Tools.condenseStrict(reads);} |
| 836 |
} |
| 837 |
|
| 838 |
ArrayList<Read> hashList=reads; |
| 839 |
if(paired && unpairT){ |
| 840 |
hashList=new ArrayList<Read>(reads.size()*2); |
| 841 |
for(Read r1 : reads){ |
| 842 |
Read r2=r1.mate; |
| 843 |
assert(r2!=null); |
| 844 |
hashList.add(r1); |
| 845 |
hashList.add(r2); |
| 846 |
if(groups>1 || !repair || namesort){ |
| 847 |
r1.mate=null; |
| 848 |
r2.mate=null; |
| 849 |
} |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
kcT.hash(hashList, table, minCount, true); |
| 854 |
storage.addAll(hashList); |
| 855 |
cris.returnList(ln.id, false); |
| 856 |
ln=cris.nextList(); |
| 857 |
reads=(ln!=null ? ln.list : null); |
| 858 |
} |
| 859 |
if(ln!=null){ |
| 860 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
if(parallelSort){ |
| 865 |
storage.sort(kcT); |
| 866 |
|
| 867 |
}else{ |
| 868 |
Collections.sort(storage, kcT); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
final int id; |
| 873 |
final ConcurrentReadInputStream cris; |
| 874 |
final KmerComparator kcT; |
| 875 |
final ArrayList<Read> storage; |
| 876 |
final boolean unpairT; |
| 877 |
final HashMap<Long, Read> entryFilterTable; |
| 878 |
public long entryFilteredT=0; |
| 879 |
|
| 880 |
protected long readsProcessedST=0; |
| 881 |
protected long basesProcessedST=0; |
| 882 |
protected long diskProcessedST=0; |
| 883 |
protected long memProcessedST=0; |
| 884 |
} |
| 885 |
|
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
protected static long lastMemProcessed=0; |
| 897 |
|
| 898 |
final long expectedSizePerGroup; |
| 899 |
private final long expectedMemPerGroup; |
| 900 |
final long totalMem; |
| 901 |
final long fileMem; |
| 902 |
final long fileSize; |
| 903 |
|
| 904 |
private final int outerPassNum; |
| 905 |
private final int outerPasses; |
| 906 |
|
| 907 |
private final double memRatio; |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
static final ArrayList<Read> POISON=new ArrayList<Read>(); |
| 912 |
protected static int fetchThreads=2; |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
final FileFormat ffin1[]; |
| 919 |
final FileFormat ffin2[]; |
| 920 |
|
| 921 |
private final FileFormat ffout1[]; |
| 922 |
private final FileFormat ffout2[]; |
| 923 |
|
| 924 |
} |