| 1 |
package sort; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.io.IOException; |
| 5 |
import java.io.PrintStream; |
| 6 |
import java.util.ArrayList; |
| 7 |
import java.util.PriorityQueue; |
| 8 |
import java.util.concurrent.atomic.AtomicLong; |
| 9 |
|
| 10 |
import fileIO.ByteFile; |
| 11 |
import fileIO.FileFormat; |
| 12 |
import fileIO.ReadWrite; |
| 13 |
import shared.KillSwitch; |
| 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.CrisContainer; |
| 23 |
import stream.FASTQ; |
| 24 |
import stream.FastaReadInputStream; |
| 25 |
import stream.Read; |
| 26 |
import stream.SamLine; |
| 27 |
import structures.ListNum; |
| 28 |
import tax.AccessionToTaxid; |
| 29 |
import tax.GiToTaxid; |
| 30 |
import tax.TaxTree; |
| 31 |
import tracker.ReadStats; |
| 32 |
import var2.ScafMap; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class SortByName { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static void main(String[] args){ |
| 52 |
Timer t=new Timer(); |
| 53 |
final boolean oldFI=FASTQ.FORCE_INTERLEAVED, oldTI=FASTQ.TEST_INTERLEAVED; |
| 54 |
SortByName x=new SortByName(args); |
| 55 |
x.process(t); |
| 56 |
FASTQ.FORCE_INTERLEAVED=oldFI; |
| 57 |
FASTQ.TEST_INTERLEAVED=oldTI; |
| 58 |
|
| 59 |
|
| 60 |
Shared.closeStream(x.outstream); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public SortByName(String[] args){ |
| 68 |
|
| 69 |
{ |
| 70 |
PreParser pp=new PreParser(args, getClass(), false); |
| 71 |
args=pp.args; |
| 72 |
outstream=pp.outstream; |
| 73 |
} |
| 74 |
|
| 75 |
boolean setInterleaved=false; |
| 76 |
|
| 77 |
|
| 78 |
Shared.capBuffers(4); |
| 79 |
Shared.setBufferData(1000000); |
| 80 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 81 |
ReadWrite.setZipThreads(Shared.threads()); |
| 82 |
FASTQ.TEST_INTERLEAVED=true; |
| 83 |
FASTQ.FORCE_INTERLEAVED=false; |
| 84 |
|
| 85 |
|
| 86 |
Parser parser=new Parser(); |
| 87 |
boolean ascending=true; |
| 88 |
|
| 89 |
|
| 90 |
for(int i=0; i<args.length; i++){ |
| 91 |
String arg=args[i]; |
| 92 |
|
| 93 |
|
| 94 |
String[] split=arg.split("="); |
| 95 |
String a=split[0].toLowerCase(); |
| 96 |
String b=split.length>1 ? split[1] : null; |
| 97 |
|
| 98 |
if(a.equals("verbose")){ |
| 99 |
verbose=Parse.parseBoolean(b); |
| 100 |
}else if(a.equals("verbose2")){ |
| 101 |
assert(false) : "Verbose2 is disabled."; |
| 102 |
|
| 103 |
}else if(a.equals("delete")){ |
| 104 |
delete=Parse.parseBoolean(b); |
| 105 |
}else if(a.equalsIgnoreCase("deleteearly")){ |
| 106 |
deleteEarly=Parse.parseBoolean(b); |
| 107 |
}else if(a.equals("allowtemp") || a.equals("usetemp")){ |
| 108 |
allowTempFiles=Parse.parseBoolean(b); |
| 109 |
}else if(a.equals("memmult") || a.equals("mult")){ |
| 110 |
memBlockMult=(float) Double.parseDouble(b); |
| 111 |
}else if(a.equals("memlimit")){ |
| 112 |
memTotalMult=(float) Double.parseDouble(b); |
| 113 |
}else if(a.equals("ascending")){ |
| 114 |
ascending=Parse.parseBoolean(b); |
| 115 |
}else if(a.equals("descending")){ |
| 116 |
ascending=!Parse.parseBoolean(b); |
| 117 |
}else if(a.equals("length")){ |
| 118 |
if(Parse.parseBoolean(b)){ |
| 119 |
comparator=ReadLengthComparator.comparator; |
| 120 |
} |
| 121 |
}else if(a.equals("name")){ |
| 122 |
if(Parse.parseBoolean(b)){ |
| 123 |
comparator=ReadComparatorName.comparator; |
| 124 |
} |
| 125 |
}else if(a.equals("quality")){ |
| 126 |
if(Parse.parseBoolean(b)){ |
| 127 |
comparator=ReadQualityComparator.comparator; |
| 128 |
} |
| 129 |
}else if(a.equals("position")){ |
| 130 |
if(Parse.parseBoolean(b)){ |
| 131 |
comparator=ReadComparatorPosition.comparator; |
| 132 |
} |
| 133 |
}else if(a.equals("list") || a.equals("names")){ |
| 134 |
comparator=new ReadComparatorList(b); |
| 135 |
}else if(a.equals("random") || a.equals("shuffle")){ |
| 136 |
if(Parse.parseBoolean(b)){ |
| 137 |
comparator=ReadComparatorRandom.comparator; |
| 138 |
} |
| 139 |
}else if(a.equals("taxa")){ |
| 140 |
if(Parse.parseBoolean(b)){ |
| 141 |
comparator=ReadComparatorTaxa.comparator; |
| 142 |
} |
| 143 |
}else if(a.equals("topo") || a.equals("topological") || a.equals("alphabetic") || a.equals("sequence") || a.equals("lexographic")){ |
| 144 |
if(Parse.parseBoolean(b)){ |
| 145 |
comparator=ReadComparatorTopological.comparator; |
| 146 |
} |
| 147 |
}else if(a.equals("flowcell")){ |
| 148 |
if(Parse.parseBoolean(b)){ |
| 149 |
comparator=ReadComparatorFlowcell.comparator; |
| 150 |
} |
| 151 |
}else if(a.equals("crispr")){ |
| 152 |
if(Parse.parseBoolean(b)){ |
| 153 |
ReadComparatorCrispr.loadNet(); |
| 154 |
comparator=ReadComparatorCrispr.comparator; |
| 155 |
} |
| 156 |
}else if(a.equalsIgnoreCase("clump") || a.equalsIgnoreCase("clumpify")){ |
| 157 |
if(Parse.parseBoolean(b)){ |
| 158 |
comparator=ReadComparatorClump.comparator; |
| 159 |
} |
| 160 |
}else if(a.equals("table") || a.equals("gi") || a.equals("gitable")){ |
| 161 |
if(b==null || "ignore".equalsIgnoreCase(b) || "skip".equalsIgnoreCase(b)){ |
| 162 |
giTableFile=null; |
| 163 |
TaxTree.CRASH_IF_NO_GI_TABLE=false; |
| 164 |
}else{giTableFile=b;} |
| 165 |
}else if(a.equals("accession")){ |
| 166 |
accessionFile=b; |
| 167 |
}else if(a.equals("tree") || a.equals("taxtree")){ |
| 168 |
taxTreeFile=b; |
| 169 |
}else if(a.equals("maxfiles") || a.equals("files")){ |
| 170 |
maxFiles=Integer.parseInt(b); |
| 171 |
assert(maxFiles>1 && maxFiles<=10000000) : "Invalid value for maxfiles: "+maxFiles+"; range is 2-10000000"; |
| 172 |
setMaxFiles=true; |
| 173 |
}else if(a.equals("genkmer")){ |
| 174 |
genKmer=Parse.parseBoolean(b); |
| 175 |
}else if(a.equals("parse_flag_goes_here")){ |
| 176 |
|
| 177 |
}else if(parser.parse(arg, a, b)){ |
| 178 |
|
| 179 |
}else{ |
| 180 |
outstream.println("Unknown parameter "+args[i]); |
| 181 |
assert(false) : "Unknown parameter "+args[i]; |
| 182 |
|
| 183 |
} |
| 184 |
} |
| 185 |
if("auto".equalsIgnoreCase(taxTreeFile)){taxTreeFile=TaxTree.defaultTreeFile();} |
| 186 |
if("auto".equalsIgnoreCase(giTableFile)){giTableFile=TaxTree.defaultTableFile();} |
| 187 |
if("auto".equalsIgnoreCase(accessionFile)){accessionFile=TaxTree.defaultAccessionFile();} |
| 188 |
|
| 189 |
if(comparator==ReadComparatorTopological.comparator && genKmer){ |
| 190 |
comparator=ReadComparatorTopological5Bit.comparator; |
| 191 |
}else{ |
| 192 |
genKmer=false; |
| 193 |
} |
| 194 |
|
| 195 |
clump=(comparator==ReadComparatorClump.comparator); |
| 196 |
|
| 197 |
comparator.setAscending(ascending); |
| 198 |
SamLine.SET_FROM_OK=true; |
| 199 |
|
| 200 |
if(comparator==ReadComparatorRandom.comparator){ |
| 201 |
ListNum.setDeterministicRandomSeed(-1); |
| 202 |
ListNum.setDeterministicRandom(true); |
| 203 |
} |
| 204 |
|
| 205 |
{ |
| 206 |
Parser.processQuality(); |
| 207 |
|
| 208 |
maxReads=parser.maxReads; |
| 209 |
|
| 210 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 211 |
append=ReadStats.append=parser.append; |
| 212 |
setInterleaved=parser.setInterleaved; |
| 213 |
|
| 214 |
in1=parser.in1; |
| 215 |
in2=parser.in2; |
| 216 |
qfin1=parser.qfin1; |
| 217 |
qfin2=parser.qfin2; |
| 218 |
|
| 219 |
out1=parser.out1; |
| 220 |
out2=parser.out2; |
| 221 |
|
| 222 |
extin=parser.extin; |
| 223 |
extout=parser.extout; |
| 224 |
|
| 225 |
minlen=parser.minReadLength; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 234 |
in2=in1.replace("#", "2"); |
| 235 |
in1=in1.replace("#", "1"); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 240 |
out2=out1.replace("#", "2"); |
| 241 |
out1=out1.replace("#", "1"); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
if(in2!=null){ |
| 246 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 247 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 248 |
} |
| 249 |
|
| 250 |
assert(FastaReadInputStream.settingsOK()); |
| 251 |
|
| 252 |
|
| 253 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 254 |
|
| 255 |
|
| 256 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 257 |
ByteFile.FORCE_MODE_BF2=true; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");} |
| 262 |
|
| 263 |
|
| 264 |
if(!setInterleaved){ |
| 265 |
if(in2==null && out2!=null){ |
| 266 |
FASTQ.FORCE_INTERLEAVED=true; |
| 267 |
FASTQ.TEST_INTERLEAVED=false; |
| 268 |
}else if(in2!=null){ |
| 269 |
FASTQ.FORCE_INTERLEAVED=false; |
| 270 |
FASTQ.TEST_INTERLEAVED=false; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){ |
| 276 |
outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2); |
| 277 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n"); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if(!Tools.testInputFiles(false, true, in1, in2)){ |
| 282 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2)){ |
| 287 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 292 |
ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 293 |
|
| 294 |
|
| 295 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 296 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 297 |
|
| 298 |
tempExt=Tools.getTempExt(ffin1, ffout1, extout); |
| 299 |
|
| 300 |
if(comparator==ReadComparatorPosition.comparator){ |
| 301 |
if(ReadComparatorPosition.scafMap==null){ |
| 302 |
ReadComparatorPosition.scafMap=ScafMap.loadSamHeader(in1); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if((comparator==ReadComparatorTaxa.comparator)){ |
| 307 |
if(giTableFile!=null){ |
| 308 |
outstream.println("Loading gi table."); |
| 309 |
GiToTaxid.initialize(giTableFile); |
| 310 |
} |
| 311 |
if(accessionFile!=null){ |
| 312 |
outstream.println("Loading accession table."); |
| 313 |
AccessionToTaxid.load(accessionFile); |
| 314 |
} |
| 315 |
if(taxTreeFile!=null){ |
| 316 |
ReadComparatorTaxa.tree=TaxTree.loadTaxTree(taxTreeFile, outstream, true, false); |
| 317 |
assert(ReadComparatorTaxa.tree.nameMap()!=null); |
| 318 |
}else{ |
| 319 |
throw new RuntimeException("No tree specified."); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
qualityObserved=!ffin1.fasta(); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
void process(Timer t){ |
| 332 |
|
| 333 |
|
| 334 |
final ConcurrentReadInputStream cris; |
| 335 |
{ |
| 336 |
useSharedHeader=(ffin1.samOrBam() && ffout1!=null && ffout1.samOrBam()); |
| 337 |
cris=ConcurrentReadInputStream.getReadInputStream(maxReads, useSharedHeader, ffin1, ffin2, qfin1, qfin2); |
| 338 |
cris.start(); |
| 339 |
if(verbose){outstream.println("Started cris");} |
| 340 |
} |
| 341 |
boolean paired=cris.paired(); |
| 342 |
if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
readsProcessed=0; |
| 359 |
basesProcessed=0; |
| 360 |
|
| 361 |
|
| 362 |
processInner(cris); |
| 363 |
|
| 364 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 365 |
|
| 366 |
|
| 367 |
errorState|=ReadStats.writeAll(); |
| 368 |
|
| 369 |
errorState|=ReadWrite.closeStreams(cris); |
| 370 |
|
| 371 |
|
| 372 |
t.stop(); |
| 373 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 374 |
|
| 375 |
|
| 376 |
if(errorState){ |
| 377 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
public void processInner(final ConcurrentReadInputStream cris){ |
| 383 |
|
| 384 |
final int ziplevel0=ReadWrite.ZIPLEVEL; |
| 385 |
ReadWrite.ZIPLEVEL=Tools.mid(1, ReadWrite.ZIPLEVEL, 2); |
| 386 |
|
| 387 |
ArrayList<Read> storage=new ArrayList<Read>(); |
| 388 |
|
| 389 |
final long maxMem=Shared.memAvailable(1); |
| 390 |
final long memLimit=(long)(maxMem*memTotalMult); |
| 391 |
final long currentLimit=(long)(maxMem*memBlockMult); |
| 392 |
final int readLimit=2000000000; |
| 393 |
long currentMem=0; |
| 394 |
long bytesDumped=0; |
| 395 |
long dumps=0; |
| 396 |
|
| 397 |
AtomicLong outstandingMem=new AtomicLong(); |
| 398 |
|
| 399 |
if(verbose){outstream.println("maxMem="+maxMem+", memLimit="+memLimit+ |
| 400 |
", currentMem="+currentMem+", currentLimit="+currentLimit);} |
| 401 |
|
| 402 |
{ |
| 403 |
|
| 404 |
ListNum<Read> ln=cris.nextList(); |
| 405 |
|
| 406 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 407 |
|
| 408 |
|
| 409 |
if(reads!=null && !reads.isEmpty()){ |
| 410 |
Read r=reads.get(0); |
| 411 |
assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired()); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 416 |
if(verbose2){outstream.println("Fetched "+reads.size()+" reads.");} |
| 417 |
|
| 418 |
|
| 419 |
for(int idx=0; idx<reads.size(); idx++){ |
| 420 |
final Read r1=reads.get(idx); |
| 421 |
final Read r2=r1.mate; |
| 422 |
|
| 423 |
|
| 424 |
final int initialLength1=r1.length(); |
| 425 |
final int initialLength2=(r1.mateLength()); |
| 426 |
|
| 427 |
|
| 428 |
readsProcessed+=r1.pairCount(); |
| 429 |
basesProcessed+=initialLength1+initialLength2; |
| 430 |
maxLengthObserved=Tools.max(maxLengthObserved, initialLength1, initialLength2); |
| 431 |
|
| 432 |
if(minlen<1 || initialLength1>=minlen || initialLength2>=minlen){ |
| 433 |
if(genKmer){ReadComparatorTopological5Bit.genKmer(r1);} |
| 434 |
else if(clump){ReadComparatorClump.set(r1);} |
| 435 |
currentMem+=r1.countBytes()+(r2==null ? 0 : r2.countBytes()); |
| 436 |
storage.add(r1); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
if(allowTempFiles && (currentMem>=currentLimit || storage.size()>=readLimit)){ |
| 441 |
if(verbose){outstream.println("currentMem: "+currentMem+" >= "+currentLimit+", dumping. ");} |
| 442 |
outstandingMem.addAndGet(currentMem); |
| 443 |
|
| 444 |
sortAndDump(storage, currentMem, outstandingMem, null, null, false); |
| 445 |
storage=new ArrayList<Read>(); |
| 446 |
bytesDumped+=currentMem; |
| 447 |
dumps++; |
| 448 |
currentMem=0; |
| 449 |
if(verbose){outstream.println("Waiting on memory; outstandingMem="+outstandingMem);} |
| 450 |
waitOnMemory(outstandingMem, memLimit); |
| 451 |
if(verbose){outstream.println("Done waiting; outstandingMem="+outstandingMem);} |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
cris.returnList(ln); |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
ln=cris.nextList(); |
| 460 |
reads=(ln!=null ? ln.list : null); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
if(ln!=null){ |
| 465 |
cris.returnList(ln.id, ln.list==null || ln.list.isEmpty()); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
outstream.println("Finished reading input."); |
| 470 |
|
| 471 |
outstandingMem.addAndGet(currentMem); |
| 472 |
if(dumps==0){ |
| 473 |
ReadWrite.ZIPLEVEL=ziplevel0; |
| 474 |
outstream.println("Sorting."); |
| 475 |
if(out1!=null){ |
| 476 |
sortAndDump(storage, currentMem, outstandingMem, out1, out2, useSharedHeader); |
| 477 |
storage=null; |
| 478 |
waitOnMemory(outstandingMem, 0); |
| 479 |
}else{ |
| 480 |
Timer t=new Timer(); |
| 481 |
Shared.sort(storage, comparator); |
| 482 |
t.stop("Sort Time: \t\t\t"); |
| 483 |
} |
| 484 |
}else{ |
| 485 |
|
| 486 |
sortAndDump(storage, currentMem, outstandingMem, null, null, false); |
| 487 |
storage=null; |
| 488 |
waitOnMemory(outstandingMem, 0); |
| 489 |
outstream.println("Merging "+(dumps+1)+" files."); |
| 490 |
ReadWrite.ZIPLEVEL=ziplevel0; |
| 491 |
mergeAndDump(outTemp, useSharedHeader, false); |
| 492 |
} |
| 493 |
|
| 494 |
} |
| 495 |
|
| 496 |
private void waitOnMemory(AtomicLong outstandingMem, long target){ |
| 497 |
if(outstandingMem.get()>target){ |
| 498 |
if(verbose){outstream.println("Syncing; outstandingMem="+outstandingMem);} |
| 499 |
while(outstandingMem.get()>target){ |
| 500 |
try { |
| 501 |
synchronized(outstandingMem){ |
| 502 |
outstandingMem.wait(2000); |
| 503 |
} |
| 504 |
} catch (InterruptedException e) { |
| 505 |
|
| 506 |
e.printStackTrace(); |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
private final long maxSizeObserved(){ |
| 517 |
return Tools.max(maxSizeObservedStatic(), Tools.max(maxLengthObserved, 150)*(qualityObserved ? 2 : 1)+500); |
| 518 |
} |
| 519 |
final static long maxSizeObservedStatic(){ |
| 520 |
return Tools.max(maxLengthObservedStatic, 150)*(qualityObservedStatic ? 2 : 1)+500; |
| 521 |
} |
| 522 |
private static long mergeMemNeeded(long maxSizeObserved, int inFiles){ |
| 523 |
return (long)(maxSizeObserved*2*(inFiles+1)*1.6); |
| 524 |
} |
| 525 |
private static int maxMergeFiles(long maxSizeObserved){ |
| 526 |
final long memAvailable=Shared.memAvailableAdvanced(); |
| 527 |
int files=(int)Tools.max(3, memAvailable/Tools.max(maxSizeObserved, 500))-1; |
| 528 |
return Tools.min(files, 10000); |
| 529 |
} |
| 530 |
private static void adjustBuffers(long maxSizeObserved, int inFiles, PrintStream outstream){ |
| 531 |
if(Shared.numBuffers()<3){return;} |
| 532 |
long memNeeded=mergeMemNeeded(maxSizeObserved, inFiles); |
| 533 |
long memAvailable=Shared.memAvailableAdvanced(); |
| 534 |
if(memNeeded>memAvailable*0.8){ |
| 535 |
outstream.println("Reduced buffers prior to merging due to low memory."); |
| 536 |
Shared.capBufferLen(40); |
| 537 |
Shared.capBuffers(2); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
private ArrayList<String> mergeRecursive(final ArrayList<String> inList, boolean allowSubprocess){ |
| 544 |
assert(maxFiles>1); |
| 545 |
|
| 546 |
ArrayList<String> currentList=inList; |
| 547 |
final int oldZL=ReadWrite.ZIPLEVEL; |
| 548 |
final boolean deleteAfterMerge=(delete && deleteEarly); |
| 549 |
final int limit=Tools.min(mergeFileLimit, maxFiles, maxMergeFiles(maxSizeObserved())); |
| 550 |
while(currentList.size()>maxFiles){ |
| 551 |
ReadWrite.ZIPLEVEL=Tools.min(ReadWrite.ZIPLEVEL, 4); |
| 552 |
final int size=currentList.size(); |
| 553 |
final int groups=(size+limit-1)/limit; |
| 554 |
assert(groups>0 && groups<size); |
| 555 |
ArrayList<ArrayList<String>> listList=new ArrayList<ArrayList<String>>(); |
| 556 |
ArrayList<String> outList=new ArrayList<String>(); |
| 557 |
for(int i=0; i<groups; i++){ |
| 558 |
listList.add(new ArrayList<String>()); |
| 559 |
} |
| 560 |
for(int i=0; i<size; i++){ |
| 561 |
listList.get(i%groups).add(currentList.get(i)); |
| 562 |
} |
| 563 |
for(ArrayList<String> subList : listList){ |
| 564 |
String temp=getTempFile(); |
| 565 |
FileFormat ffoutTemp=FileFormat.testOutput(temp, FileFormat.FASTQ, null, true, false, false, false); |
| 566 |
mergeRecursiveInner(subList, ffoutTemp, null, deleteAfterMerge, allowSubprocess); |
| 567 |
outList.add(temp); |
| 568 |
} |
| 569 |
if(delete && !deleteEarly){ |
| 570 |
System.err.println("Deleting "+size+" temp files..."); |
| 571 |
for(ArrayList<String> subList : listList){ |
| 572 |
for(String fname : subList){new File(fname).delete();} |
| 573 |
} |
| 574 |
System.err.println(" done."); |
| 575 |
} |
| 576 |
currentList=outList; |
| 577 |
} |
| 578 |
ReadWrite.ZIPLEVEL=oldZL; |
| 579 |
return currentList; |
| 580 |
} |
| 581 |
|
| 582 |
public void mergeRecursiveInner(ArrayList<String> inList, FileFormat ffoutTemp1, FileFormat ffoutTemp2, boolean deleteAfterMerge, boolean allowSubprocess){ |
| 583 |
errorState|=mergeAndDump(inList, ffoutTemp1, ffoutTemp2, deleteAfterMerge, useSharedHeader, allowSubprocess, outstream, maxSizeObserved()); |
| 584 |
} |
| 585 |
|
| 586 |
private String getTempFile(){ |
| 587 |
String temp; |
| 588 |
File dir=new File("."); |
| 589 |
if(dir!=null && !dir.exists()){dir.mkdirs();} |
| 590 |
try { |
| 591 |
temp=File.createTempFile("sort_temp_", tempExt, dir).toString(); |
| 592 |
} catch (IOException e) { |
| 593 |
|
| 594 |
e.printStackTrace(); |
| 595 |
KillSwitch.kill(e.getMessage()); |
| 596 |
return null; |
| 597 |
} |
| 598 |
return temp; |
| 599 |
} |
| 600 |
|
| 601 |
private boolean mergeAndDump(ArrayList<String> fnames, boolean useHeader, boolean allowSubprocess) { |
| 602 |
final long maxSizeObserved=maxSizeObserved(); |
| 603 |
final int limit=Tools.min(mergeFileLimit, maxMergeFiles(maxSizeObserved)); |
| 604 |
if(fnames.size()>limit){ |
| 605 |
outstream.println("Performing recursive merge to reduce open files."); |
| 606 |
fnames=mergeRecursive(fnames, allowSubprocess); |
| 607 |
} |
| 608 |
return mergeAndDump(fnames, ffout1, ffout2, delete, useHeader, allowSubprocess, outstream, maxSizeObserved); |
| 609 |
} |
| 610 |
|
| 611 |
public static boolean mergeAndDump(ArrayList<String> fnames, FileFormat ffout1, FileFormat ffout2, |
| 612 |
boolean deleteAfterMerge, boolean useHeader, boolean allowSubprocess, PrintStream outstream, final long maxSizeObserved) { |
| 613 |
|
| 614 |
final int oldBuffers=Shared.numBuffers(); |
| 615 |
final int oldBufferLen=Shared.bufferLen(); |
| 616 |
if(!FileFormat.isFastaFile(fnames.get(0))){ |
| 617 |
synchronized(SortByName.class){ |
| 618 |
qualityObservedStatic=true; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
adjustBuffers(maxSizeObserved, fnames.size(), outstream); |
| 623 |
|
| 624 |
System.err.println("Merging "+fnames); |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
ListNum.setDeterministicRandom(false); |
| 631 |
boolean errorState=false; |
| 632 |
final ConcurrentReadOutputStream ros; |
| 633 |
if(ffout1!=null){ |
| 634 |
final int buff=1; |
| 635 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, null, null, buff, null, useHeader); |
| 636 |
ros.start(); |
| 637 |
}else{ros=null;} |
| 638 |
|
| 639 |
ArrayList<CrisContainer> cclist=new ArrayList<CrisContainer>(fnames.size()); |
| 640 |
PriorityQueue<CrisContainer> q=new PriorityQueue<CrisContainer>(fnames.size()); |
| 641 |
for(int i=0; i<fnames.size(); i++){ |
| 642 |
String fname=fnames.get(i); |
| 643 |
|
| 644 |
CrisContainer cc=new CrisContainer(fname, comparator, allowSubprocess); |
| 645 |
if(cc.peek()!=null){ |
| 646 |
cclist.add(cc); |
| 647 |
q.add(cc); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
mergeAndDump(q, ros, outstream); |
| 653 |
if(verbose){ |
| 654 |
outstream.println("Finished processing "+fnames); |
| 655 |
} |
| 656 |
|
| 657 |
for(CrisContainer cc : cclist){ |
| 658 |
errorState|=cc.close(); |
| 659 |
} |
| 660 |
if(deleteAfterMerge){ |
| 661 |
for(String fname : fnames){ |
| 662 |
new File(fname).delete(); |
| 663 |
} |
| 664 |
} |
| 665 |
if(ros!=null){errorState|=ReadWrite.closeStream(ros);} |
| 666 |
|
| 667 |
Shared.setBufferLen(oldBufferLen); |
| 668 |
Shared.setBuffers(oldBuffers); |
| 669 |
|
| 670 |
return errorState; |
| 671 |
} |
| 672 |
|
| 673 |
private static void mergeAndDump(final PriorityQueue<CrisContainer> q, final ConcurrentReadOutputStream ros, PrintStream outstream) { |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
long maxLen=0; |
| 681 |
|
| 682 |
final int limit=100000; |
| 683 |
ArrayList<Read> buffer=new ArrayList<Read>(2*limit); |
| 684 |
while(!q.isEmpty()){ |
| 685 |
if(verbose2){outstream.println("q size: "+q.size());} |
| 686 |
for(int i=0; !q.isEmpty() && (buffer.size()<limit || i<1); i++){ |
| 687 |
CrisContainer cc=q.poll(); |
| 688 |
if(verbose2){outstream.println("Polled a cc.");} |
| 689 |
ArrayList<Read> list=cc.fetch(); |
| 690 |
if(verbose2){outstream.println("Grabbed "+list.size()+" reads.");} |
| 691 |
buffer.addAll(list); |
| 692 |
if(verbose2){outstream.println("Buffer size: "+buffer.size());} |
| 693 |
if(cc.hasMore()){ |
| 694 |
if(verbose2){outstream.println("Returned cc.");} |
| 695 |
q.add(cc); |
| 696 |
}else{ |
| 697 |
if(verbose2){outstream.println("Discarded cc.");} |
| 698 |
} |
| 699 |
} |
| 700 |
Shared.sort(buffer, comparator); |
| 701 |
if(verbose2){outstream.println("Sorted buffer.");} |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
for(int i=1; i<buffer.size(); i++){ |
| 707 |
Read a=buffer.get(i-1); |
| 708 |
Read b=buffer.get(i); |
| 709 |
assert(comparator.compare(a, b)<=0) : "\n"+a+"\n"+b; |
| 710 |
|
| 711 |
} |
| 712 |
|
| 713 |
final Read peek=(q.isEmpty() ? null : q.peek().peek()); |
| 714 |
final int maxIndex=(peek==null ? buffer.size() : indexOfLowestAbovePivot(buffer, peek)); |
| 715 |
ArrayList<Read> list=new ArrayList<Read>(maxIndex); |
| 716 |
|
| 717 |
for(int index=0; index<maxIndex; index++){ |
| 718 |
Read r=buffer.get(index); |
| 719 |
assert(peek==null || comparator.compare(peek, r)>0) : "\n"+peek+"\n"+r; |
| 720 |
list.add(r); |
| 721 |
maxLen=Tools.max(maxLen, r.length()); |
| 722 |
} |
| 723 |
if(ros!=null){ros.add(list, 0);} |
| 724 |
|
| 725 |
ArrayList<Read> oldbuffer=buffer; |
| 726 |
buffer=new ArrayList<Read>(2*limit); |
| 727 |
for(int i=maxIndex, size=oldbuffer.size(); i<size; i++){ |
| 728 |
buffer.add(oldbuffer.get(i)); |
| 729 |
} |
| 730 |
if(verbose2){outstream.println("Buffer contains "+buffer.size()+" reads.");} |
| 731 |
} |
| 732 |
|
| 733 |
assert(buffer.isEmpty()); |
| 734 |
synchronized(SortByName.class){ |
| 735 |
maxLengthObservedStatic=Tools.max(maxLengthObservedStatic, maxLen); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
private static final int indexOfLowestAbovePivot(final ArrayList<Read> list, final Read pivot){ |
| 768 |
final int size=list.size(); |
| 769 |
final int maxIndex=binarySearch(list, pivot); |
| 770 |
if(maxIndex<0){return 0;} |
| 771 |
if(maxIndex>=size){return size;} |
| 772 |
Read r=list.get(maxIndex); |
| 773 |
final int x=comparator.compare(pivot, r); |
| 774 |
assert(x<=0) : x+"\n"+pivot.id+"\n"+r.id; |
| 775 |
final int ret=(x==0 ? maxIndex+1 : maxIndex); |
| 776 |
assert(ret==size || comparator.compare(pivot, list.get(ret))<0); |
| 777 |
assert(ret==0 || comparator.compare(pivot, list.get(ret-1))>=0); |
| 778 |
return ret; |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
private static final int binarySearch(final ArrayList<Read> list, final Read pivot){ |
| 783 |
int a=0, b=list.size()-1; |
| 784 |
while(b>a){ |
| 785 |
final int mid=(a+b)/2; |
| 786 |
final Read r=list.get(mid); |
| 787 |
final int x=comparator.compare(pivot, r); |
| 788 |
if(x<0){b=mid;} |
| 789 |
else if(x>0){a=mid+1;} |
| 790 |
else{return mid;} |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
while(a>=0 && a<list.size()-1){ |
| 802 |
final int x=comparator.compare(pivot, list.get(a+1)); |
| 803 |
assert(x<=0) : x+", "+a+", "+b+", "+list.size()+"\n"+pivot.id+"\n"+list.get(a).id; |
| 804 |
if(x<0){break;} |
| 805 |
a++; |
| 806 |
} |
| 807 |
|
| 808 |
if(a>=0 && a==list.size()-1){ |
| 809 |
if(comparator.compare(pivot, list.get(a))>0){a++;} |
| 810 |
} |
| 811 |
|
| 812 |
assert(a>=list.size() || comparator.compare(pivot, list.get(a))<=0) : |
| 813 |
comparator.compare(pivot, list.get(a))+"\n"+ |
| 814 |
a+", "+b+", "+list.size()+"\n"+pivot.id+"\n"+list.get(a).id; |
| 815 |
|
| 816 |
return a; |
| 817 |
} |
| 818 |
|
| 819 |
private void sortAndDump(final ArrayList<Read> storage, final long currentMem, final AtomicLong outstandingMem, String fname1, String fname2, boolean useHeader) { |
| 820 |
String temp1=fname1; |
| 821 |
String temp2=fname2; |
| 822 |
if(temp1==null){ |
| 823 |
synchronized(outTemp){ |
| 824 |
if(verbose2){outstream.println("Synced to outTemp to make a temp file.");} |
| 825 |
temp1=getTempFile(); |
| 826 |
if(verbose2){outstream.println("Temp file: "+temp1);} |
| 827 |
outTemp.add(temp1); |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
if(verbose || true){outstream.println("Created a WriteThread for "+temp1+(temp2==null ? "" : ", "+temp2));} |
| 832 |
WriteThread wt=new WriteThread(storage, currentMem, outstandingMem, temp1, temp2, useHeader, outstream); |
| 833 |
wt.start(); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
private static class WriteThread extends Thread{ |
| 841 |
|
| 842 |
public WriteThread(final ArrayList<Read> storage_, final long currentMem_, final AtomicLong outstandingMem_, |
| 843 |
String fname1_, String fname2_, boolean useHeader_, PrintStream outstream_){ |
| 844 |
storage=storage_; |
| 845 |
currentMem=currentMem_; |
| 846 |
outstandingMem=outstandingMem_; |
| 847 |
fname1=fname1_; |
| 848 |
fname2=fname2_; |
| 849 |
useHeader=useHeader_; |
| 850 |
outstream=outstream_; |
| 851 |
} |
| 852 |
|
| 853 |
@Override |
| 854 |
public void run(){ |
| 855 |
|
| 856 |
if(verbose){outstream.println("Started a WriteThread.");} |
| 857 |
final FileFormat ffout1=FileFormat.testOutput(fname1, FileFormat.FASTQ, null, true, false, false, false); |
| 858 |
final FileFormat ffout2=FileFormat.testOutput(fname2, FileFormat.FASTQ, null, true, false, false, false); |
| 859 |
final ConcurrentReadOutputStream ros; |
| 860 |
if(ffout1!=null){ |
| 861 |
final int buff=4; |
| 862 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, null, null, buff, null, useHeader); |
| 863 |
ros.start(); |
| 864 |
}else{ros=null;} |
| 865 |
|
| 866 |
if(verbose){outstream.println("Started a ros.");} |
| 867 |
|
| 868 |
|
| 869 |
Shared.sort(storage, comparator); |
| 870 |
|
| 871 |
if(verbose){outstream.println("Sorted reads.");} |
| 872 |
|
| 873 |
ArrayList<Read> buffer=new ArrayList<Read>(200); |
| 874 |
long id=0; |
| 875 |
for(int i=0, lim=storage.size(); i<lim; i++){ |
| 876 |
Read r=storage.set(i, null); |
| 877 |
buffer.add(r); |
| 878 |
if(buffer.size()>=200){ |
| 879 |
if(ros!=null){ros.add(buffer, id);} |
| 880 |
id++; |
| 881 |
buffer=new ArrayList<Read>(200); |
| 882 |
} |
| 883 |
} |
| 884 |
if(ros!=null && buffer.size()>0){ros.add(buffer, id);} |
| 885 |
errorState|=ReadWrite.closeStream(ros); |
| 886 |
if(verbose){outstream.println("Closed ros.");} |
| 887 |
|
| 888 |
synchronized(outstandingMem){ |
| 889 |
outstandingMem.addAndGet(-currentMem); |
| 890 |
if(verbose){outstream.println("Decremented outstandingMem: "+outstandingMem);} |
| 891 |
outstandingMem.notify(); |
| 892 |
if(verbose){outstream.println("Notified outstandingMem.");} |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
final ArrayList<Read> storage; |
| 897 |
final long currentMem; |
| 898 |
final AtomicLong outstandingMem; |
| 899 |
final String fname1; |
| 900 |
final String fname2; |
| 901 |
final boolean useHeader; |
| 902 |
boolean errorState=false; |
| 903 |
final PrintStream outstream; |
| 904 |
|
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
private String in1=null; |
| 913 |
|
| 914 |
private String in2=null; |
| 915 |
|
| 916 |
private String qfin1=null; |
| 917 |
private String qfin2=null; |
| 918 |
|
| 919 |
|
| 920 |
private String out1=null; |
| 921 |
|
| 922 |
private String out2=null; |
| 923 |
|
| 924 |
private ArrayList<String> outTemp=new ArrayList<String>(); |
| 925 |
|
| 926 |
|
| 927 |
private String extin=null; |
| 928 |
|
| 929 |
private String extout=null; |
| 930 |
|
| 931 |
private String tempExt=null; |
| 932 |
|
| 933 |
private String giTableFile=null;; |
| 934 |
private String taxTreeFile=null; |
| 935 |
private String accessionFile=null; |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
long maxLengthObserved=0; |
| 940 |
private static long maxLengthObservedStatic=0; |
| 941 |
private final boolean qualityObserved; |
| 942 |
private static boolean qualityObservedStatic; |
| 943 |
|
| 944 |
|
| 945 |
protected long readsProcessed=0; |
| 946 |
|
| 947 |
protected long basesProcessed=0; |
| 948 |
|
| 949 |
|
| 950 |
private long maxReads=-1; |
| 951 |
|
| 952 |
private boolean delete=true; |
| 953 |
private boolean deleteEarly=false; |
| 954 |
|
| 955 |
private boolean useSharedHeader=false; |
| 956 |
|
| 957 |
private boolean allowTempFiles=true; |
| 958 |
|
| 959 |
private int minlen=0; |
| 960 |
|
| 961 |
private float memBlockMult=0.30f; |
| 962 |
private float memTotalMult=0.65f; |
| 963 |
|
| 964 |
|
| 965 |
private int maxFiles=12; |
| 966 |
private boolean setMaxFiles=false; |
| 967 |
|
| 968 |
private static int mergeFileLimit=24; |
| 969 |
|
| 970 |
private boolean genKmer=true; |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
private final FileFormat ffin1; |
| 978 |
|
| 979 |
private final FileFormat ffin2; |
| 980 |
|
| 981 |
|
| 982 |
private final FileFormat ffout1; |
| 983 |
|
| 984 |
private final FileFormat ffout2; |
| 985 |
|
| 986 |
private final boolean clump; |
| 987 |
|
| 988 |
private static ReadComparator comparator=ReadComparatorName.comparator; |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
private PrintStream outstream=System.err; |
| 996 |
|
| 997 |
public static boolean verbose=false; |
| 998 |
|
| 999 |
public static final boolean verbose2=false; |
| 1000 |
|
| 1001 |
public boolean errorState=false; |
| 1002 |
|
| 1003 |
private boolean overwrite=true; |
| 1004 |
|
| 1005 |
private boolean append=false; |
| 1006 |
|
| 1007 |
} |