| 1 |
package clump; |
| 2 |
|
| 3 |
import java.io.File; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import fileIO.ByteFile; |
| 7 |
import fileIO.FileFormat; |
| 8 |
import fileIO.ReadWrite; |
| 9 |
import shared.Parse; |
| 10 |
import shared.Parser; |
| 11 |
import shared.PreParser; |
| 12 |
import shared.Shared; |
| 13 |
import shared.Timer; |
| 14 |
import shared.Tools; |
| 15 |
import stream.ConcurrentReadInputStream; |
| 16 |
import stream.ConcurrentReadOutputStream; |
| 17 |
import stream.FASTQ; |
| 18 |
import stream.FastaReadInputStream; |
| 19 |
import stream.Read; |
| 20 |
import tracker.ReadStats; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class KmerSort1 extends KmerSort { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static void main(String[] args){ |
| 38 |
final boolean pigz=ReadWrite.USE_PIGZ, unpigz=ReadWrite.USE_UNPIGZ; |
| 39 |
final float ztd=ReadWrite.ZIP_THREAD_MULT(); |
| 40 |
final int mzt=ReadWrite.MAX_ZIP_THREADS(); |
| 41 |
final int oldzl=ReadWrite.ZIPLEVEL; |
| 42 |
Timer t=new Timer(); |
| 43 |
KmerSort1 x=new KmerSort1(args); |
| 44 |
x.process(t); |
| 45 |
ReadWrite.USE_PIGZ=pigz; |
| 46 |
ReadWrite.USE_UNPIGZ=unpigz; |
| 47 |
ReadWrite.setZipThreadMult(ztd); |
| 48 |
ReadWrite.setZipThreads(mzt); |
| 49 |
ReadWrite.ZIPLEVEL=oldzl; |
| 50 |
|
| 51 |
|
| 52 |
Shared.closeStream(x.outstream); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public KmerSort1(String[] args){ |
| 60 |
|
| 61 |
{ |
| 62 |
PreParser pp=new PreParser(args, getClass(), false); |
| 63 |
args=pp.args; |
| 64 |
outstream=pp.outstream; |
| 65 |
} |
| 66 |
|
| 67 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 68 |
ReadWrite.setZipThreads(Shared.threads()); |
| 69 |
|
| 70 |
Parser parser=new Parser(); |
| 71 |
for(int i=0; i<args.length; i++){ |
| 72 |
String arg=args[i]; |
| 73 |
String[] split=arg.split("="); |
| 74 |
String a=split[0].toLowerCase(); |
| 75 |
String b=split.length>1 ? split[1] : null; |
| 76 |
|
| 77 |
if(a.equals("verbose")){ |
| 78 |
verbose=KmerComparator.verbose=Parse.parseBoolean(b); |
| 79 |
}else if(a.equals("parse_flag_goes_here")){ |
| 80 |
|
| 81 |
}else if(a.equals("k")){ |
| 82 |
k=Integer.parseInt(b); |
| 83 |
assert(k>0 && k<32); |
| 84 |
}else if(a.equals("mincount") || a.equals("mincr")){ |
| 85 |
minCount=Integer.parseInt(b); |
| 86 |
}else if(a.equals("rename") || a.equals("addname")){ |
| 87 |
addName=Parse.parseBoolean(b); |
| 88 |
}else if(a.equals("shortname") || a.equals("shortnames")){ |
| 89 |
if(b!=null && b.equals("shrink")){ |
| 90 |
shrinkName=true; |
| 91 |
}else{ |
| 92 |
shrinkName=false; |
| 93 |
shortName=Parse.parseBoolean(b); |
| 94 |
} |
| 95 |
}else if(a.equals("rcomp") || a.equals("reversecomplement")){ |
| 96 |
rcomp=Parse.parseBoolean(b); |
| 97 |
}else if(a.equals("ecco")){ |
| 98 |
ecco=Parse.parseBoolean(b); |
| 99 |
}else if(a.equals("condense") || a.equals("consensus") || a.equals("concensus")){ |
| 100 |
condense=Parse.parseBoolean(b); |
| 101 |
}else if(a.equals("correct") || a.equals("ecc")){ |
| 102 |
correct=Parse.parseBoolean(b); |
| 103 |
}else if(a.equals("passes")){ |
| 104 |
passes=Integer.parseInt(b); |
| 105 |
} |
| 106 |
|
| 107 |
else if(a.equals("dedupe")){ |
| 108 |
dedupe=Parse.parseBoolean(b); |
| 109 |
}else if(a.equals("markduplicates")){ |
| 110 |
dedupe=Clump.markOnly=Parse.parseBoolean(b); |
| 111 |
}else if(a.equals("markall")){ |
| 112 |
boolean x=Parse.parseBoolean(b); |
| 113 |
if(x){ |
| 114 |
dedupe=Clump.markOnly=Clump.markAll=true; |
| 115 |
}else{ |
| 116 |
Clump.markAll=false; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
else if(a.equals("prefilter")){ |
| 121 |
KmerReduce.prefilter=Parse.parseBoolean(b); |
| 122 |
}else if(a.equals("entryfilter")){ |
| 123 |
entryfilter=Parse.parseBoolean(b); |
| 124 |
}else if(a.equals("groups") || a.equals("g") || a.equals("sets") || a.equals("ways")){ |
| 125 |
groups=Integer.parseInt(b); |
| 126 |
splitInput=(groups>1); |
| 127 |
}else if(a.equals("seed")){ |
| 128 |
KmerComparator.defaultSeed=Long.parseLong(b); |
| 129 |
}else if(a.equals("hashes")){ |
| 130 |
KmerComparator.setHashes(Integer.parseInt(b)); |
| 131 |
}else if(a.equals("border")){ |
| 132 |
KmerComparator.defaultBorder=Integer.parseInt(b); |
| 133 |
}else if(a.equals("minprob")){ |
| 134 |
KmerComparator.minProb=Float.parseFloat(b); |
| 135 |
|
| 136 |
}else if(a.equals("unpair")){ |
| 137 |
unpair=Parse.parseBoolean(b); |
| 138 |
}else if(a.equals("repair")){ |
| 139 |
repair=Parse.parseBoolean(b); |
| 140 |
}else if(a.equals("namesort") || a.equals("sort")){ |
| 141 |
namesort=Parse.parseBoolean(b); |
| 142 |
}else if(a.equals("reorder") || a.equals("reorderclumps") || a.equals("reordermode")){ |
| 143 |
reorderMode=REORDER_AUTO; |
| 144 |
if(b==null || b.equalsIgnoreCase("auto") || b.equalsIgnoreCase("a")){ |
| 145 |
reorderMode=REORDER_AUTO; |
| 146 |
}else if(b.equalsIgnoreCase("unpaired") || b.equalsIgnoreCase("consensus") || b.equalsIgnoreCase("reorder") || b.equalsIgnoreCase("c")){ |
| 147 |
reorderMode=REORDER_CONSENSUS; |
| 148 |
}else if(b.equalsIgnoreCase("pair") || b.equalsIgnoreCase("pairs") || b.equalsIgnoreCase("paired") || b.equalsIgnoreCase("p")){ |
| 149 |
reorderMode=REORDER_PAIRED; |
| 150 |
}else{ |
| 151 |
boolean x=Parse.parseBoolean(b); |
| 152 |
if(x){ |
| 153 |
reorderMode=REORDER_AUTO; |
| 154 |
}else{ |
| 155 |
reorderMode=REORDER_FALSE; |
| 156 |
} |
| 157 |
} |
| 158 |
}else if(a.equals("reorderpaired") || a.equals("reorderclumpspaired")){ |
| 159 |
boolean x=Parse.parseBoolean(b); |
| 160 |
if(x){ |
| 161 |
reorderMode=REORDER_PAIRED; |
| 162 |
}else{ |
| 163 |
reorderMode=REORDER_FALSE; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
else if(a.equals("fetchthreads")){ |
| 168 |
|
| 169 |
}else if(Clump.parseStatic(arg, a, b)){ |
| 170 |
|
| 171 |
}else if(parser.parse(arg, a, b)){ |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
else{ |
| 176 |
outstream.println("Unknown parameter "+args[i]); |
| 177 |
assert(false) : "Unknown parameter "+args[i]; |
| 178 |
|
| 179 |
} |
| 180 |
} |
| 181 |
Clump.renameConsensus=condense; |
| 182 |
if(dedupe){KmerComparator.compareSequence=true;} |
| 183 |
assert(!(reorderMode==REORDER_PAIRED && dedupe)) : "REORDER_PAIRED and dedupe are incompatible."; |
| 184 |
|
| 185 |
{ |
| 186 |
Parser.processQuality(); |
| 187 |
|
| 188 |
maxReads=parser.maxReads; |
| 189 |
|
| 190 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 191 |
append=ReadStats.append=parser.append; |
| 192 |
|
| 193 |
in1=parser.in1; |
| 194 |
in2=parser.in2; |
| 195 |
|
| 196 |
out1=parser.out1; |
| 197 |
out2=parser.out2; |
| 198 |
|
| 199 |
extin=parser.extin; |
| 200 |
extout=parser.extout; |
| 201 |
} |
| 202 |
|
| 203 |
assert(FastaReadInputStream.settingsOK()); |
| 204 |
|
| 205 |
if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){ |
| 206 |
in2=in1.replace("#", "2"); |
| 207 |
in1=in1.replace("#", "1"); |
| 208 |
} |
| 209 |
if(in2!=null){ |
| 210 |
if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");} |
| 211 |
FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false; |
| 212 |
} |
| 213 |
|
| 214 |
if(in1==null){throw new RuntimeException("Error - at least one input file is required.");} |
| 215 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 216 |
ByteFile.FORCE_MODE_BF2=true; |
| 217 |
} |
| 218 |
|
| 219 |
if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;} |
| 220 |
if(out1!=null && out2==null && out1.indexOf('#')>-1){ |
| 221 |
out2=out1.replace("#", "2"); |
| 222 |
out1=out1.replace("#", "1"); |
| 223 |
} |
| 224 |
|
| 225 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 226 |
outstream.println((out1==null)+", "+out1); |
| 227 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n"); |
| 228 |
} |
| 229 |
|
| 230 |
ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 231 |
ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false); |
| 232 |
|
| 233 |
if(groups>1 && in1.contains("%") && (splitInput || !new File(in1).exists())){ |
| 234 |
ffin1=new FileFormat[groups]; |
| 235 |
ffin2=new FileFormat[groups]; |
| 236 |
for(int i=0; i<groups; i++){ |
| 237 |
ffin1[i]=FileFormat.testInput(in1.replaceFirst("%", ""+i), FileFormat.FASTQ, extin, true, true); |
| 238 |
ffin2[i]=in2==null ? null : FileFormat.testInput(in2.replaceFirst("%", ""+i), FileFormat.FASTQ, extin, true, true); |
| 239 |
} |
| 240 |
}else{ |
| 241 |
assert(!in1.contains("%") && groups==1) : "The % symbol must only be present in the input filename if groups>1."; |
| 242 |
ffin1=new FileFormat[1]; |
| 243 |
ffin1[0]=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true); |
| 244 |
ffin2=new FileFormat[1]; |
| 245 |
ffin2[0]=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true); |
| 246 |
groups=1; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
if((reorderMode!=REORDER_FALSE) && (passes>1 || condense || correct || groups>1)){ |
| 251 |
outstream.println("Clump reordering disabled because "+(passes>1 ? "passes>1" : condense ? " condense=t" : correct ? " ecc=t" : "groups>1")); |
| 252 |
reorderMode=REORDER_FALSE; |
| 253 |
} |
| 254 |
|
| 255 |
if(reorderMode==REORDER_PAIRED){ |
| 256 |
if(!unpair || !repair){ |
| 257 |
outstream.println("Unpair and repair enabled because clump reorder mode is set to paired."); |
| 258 |
unpair=true; |
| 259 |
repair=true; |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
@Override |
| 270 |
void process(Timer t){ |
| 271 |
|
| 272 |
preprocess(); |
| 273 |
|
| 274 |
final ConcurrentReadOutputStream ros; |
| 275 |
if(out1!=null){ |
| 276 |
final int buff=1; |
| 277 |
assert(!out1.equalsIgnoreCase(in1)) : "Input file and output file have same name."; |
| 278 |
|
| 279 |
ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, null, null, buff, null, useSharedHeader); |
| 280 |
ros.start(); |
| 281 |
}else{ros=null;} |
| 282 |
|
| 283 |
readsProcessed=basesProcessed=diskProcessed=memProcessed=0; |
| 284 |
|
| 285 |
|
| 286 |
processInner(ros); |
| 287 |
|
| 288 |
printStats(t); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
void processInner(final ConcurrentReadOutputStream ros){ |
| 293 |
if(verbose){outstream.println("Making comparator.");} |
| 294 |
KmerComparator kc=new KmerComparator(k, addName, (rcomp || condense || correct)); |
| 295 |
|
| 296 |
ClumpList.UNRCOMP=(!rcomp && !condense); |
| 297 |
Timer t=new Timer(); |
| 298 |
|
| 299 |
final int conservativePasses=Clump.conservativeFlag ? passes : Tools.max(1, passes/2); |
| 300 |
if(groups==1 && passes>1){Clump.setConservative(true);} |
| 301 |
|
| 302 |
useSharedHeader=(ffin1[0].samOrBam() && ffout1!=null && ffout1.samOrBam()); |
| 303 |
|
| 304 |
for(int group=0; group<groups; group++){ |
| 305 |
if(verbose){outstream.println("Starting cris "+group+".");} |
| 306 |
|
| 307 |
final ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, |
| 308 |
useSharedHeader && groups==1, ffin1[group], ffin2[group], null, null); |
| 309 |
cris.start(); |
| 310 |
if(reorderMode!=REORDER_FALSE){ |
| 311 |
assert(groups==1) : "Too many groups for reorder: "+groups; |
| 312 |
if(reorderMode==REORDER_AUTO){ |
| 313 |
if(cris.paired() && !dedupe){ |
| 314 |
reorderMode=REORDER_PAIRED; |
| 315 |
unpair=repair=true; |
| 316 |
}else{ |
| 317 |
reorderMode=REORDER_CONSENSUS; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
if(verbose){t.start("Fetching reads.");} |
| 323 |
ArrayList<Read> reads=fetchReads1(cris, kc); |
| 324 |
quantizeQuality=false; |
| 325 |
|
| 326 |
|
| 327 |
if(verbose){t.start("Sorting.");} |
| 328 |
Shared.sort(reads, kc); |
| 329 |
if(verbose){t.stop("Sort time: ");} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
if(verbose){t.start("Making clumps.");} |
| 336 |
readsProcessedThisPass=reads.size(); |
| 337 |
|
| 338 |
ClumpList cl=new ClumpList(reads, k, reorderMode==REORDER_CONSENSUS); |
| 339 |
|
| 340 |
if(reorderMode!=REORDER_FALSE){ |
| 341 |
reads.clear(); |
| 342 |
if(reorderMode==REORDER_PAIRED){cl.reorderPaired();} |
| 343 |
else if(reorderMode==REORDER_CONSENSUS){cl.reorder();} |
| 344 |
else{assert(false) : reorderMode;} |
| 345 |
for(Clump c : cl){ |
| 346 |
reads.addAll(c); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
clumpsProcessedThisPass=cl.size(); |
| 351 |
clumpsProcessedTotal+=clumpsProcessedThisPass; |
| 352 |
if(verbose){t.stop("Clump time: ");} |
| 353 |
|
| 354 |
if(dedupe){ |
| 355 |
reads.clear(); |
| 356 |
if(verbose){t.start("Deduping.");} |
| 357 |
reads=processClumps(cl, ClumpList.DEDUPE); |
| 358 |
|
| 359 |
if(passes>1 && groups==1){ |
| 360 |
|
| 361 |
FASTQ.DETECT_QUALITY=FASTQ.DETECT_QUALITY_OUT=false; |
| 362 |
FASTQ.ASCII_OFFSET=FASTQ.ASCII_OFFSET_OUT; |
| 363 |
|
| 364 |
if(verbose){outstream.println("Pass 1.\n");} |
| 365 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 366 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 367 |
|
| 368 |
for(int pass=1; pass<passes; pass++){ |
| 369 |
|
| 370 |
kc=new KmerComparator(k, kc.seed<0 ? -1 : kc.seed+1, kc.border-1, kc.hashes, false, kc.rcompReads); |
| 371 |
reads=runOnePass(reads, kc); |
| 372 |
|
| 373 |
if(verbose){outstream.println("Seed: "+kc.seed);} |
| 374 |
if(verbose){outstream.println("Pass "+(pass+1)+".");} |
| 375 |
outstream.println(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if(verbose){t.stop("Dedupe time: ");} |
| 380 |
}else if(condense){ |
| 381 |
reads.clear(); |
| 382 |
if(verbose){t.start("Condensing.");} |
| 383 |
reads=processClumps(cl, ClumpList.CONDENSE); |
| 384 |
if(verbose){t.stop("Condense time: ");} |
| 385 |
}else if(correct){ |
| 386 |
reads.clear(); |
| 387 |
if(verbose){t.start("Correcting.");} |
| 388 |
reads=processClumps(cl, ClumpList.CORRECT); |
| 389 |
if(verbose){t.stop("Correct time: ");} |
| 390 |
|
| 391 |
if(verbose){outstream.println("Seed: "+kc.seed);} |
| 392 |
if(groups>1){ |
| 393 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 394 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 395 |
if(correct){ |
| 396 |
outstream.println("Corrections: \t"+correctionsThisPass); |
| 397 |
} |
| 398 |
outstream.println(); |
| 399 |
} |
| 400 |
|
| 401 |
if(passes>1 && groups==1){ |
| 402 |
|
| 403 |
FASTQ.DETECT_QUALITY=FASTQ.DETECT_QUALITY_OUT=false; |
| 404 |
FASTQ.ASCII_OFFSET=FASTQ.ASCII_OFFSET_OUT; |
| 405 |
|
| 406 |
if(verbose){outstream.println("Pass 1.");} |
| 407 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 408 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 409 |
if(correct){ |
| 410 |
outstream.println("Corrections: \t"+correctionsThisPass); |
| 411 |
} |
| 412 |
outstream.println(); |
| 413 |
|
| 414 |
for(int pass=1; pass<passes; pass++){ |
| 415 |
|
| 416 |
if(pass>=conservativePasses){Clump.setConservative(false);} |
| 417 |
|
| 418 |
kc=new KmerComparator(k, kc.seed<0 ? -1 : kc.seed+1, kc.border-1, kc.hashes, false, kc.rcompReads); |
| 419 |
reads=runOnePass(reads, kc); |
| 420 |
|
| 421 |
if(verbose){outstream.println("Seed: "+kc.seed);} |
| 422 |
if(verbose){outstream.println("Pass "+(pass+1)+".");} |
| 423 |
if(verbose){outstream.println("Reads: \t"+readsProcessedThisPass);} |
| 424 |
outstream.println("Clumps: \t"+clumpsProcessedThisPass); |
| 425 |
if(correct){ |
| 426 |
outstream.println("Corrections: \t"+correctionsThisPass); |
| 427 |
} |
| 428 |
outstream.println(); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
if(repair || namesort){ |
| 434 |
if(groups>1){ |
| 435 |
if(verbose){t.start("Name-sorting.");} |
| 436 |
reads=nameSort(reads, false); |
| 437 |
if(verbose){t.stop("Sort time: ");} |
| 438 |
}else{ |
| 439 |
if(namesort){ |
| 440 |
if(verbose){t.start("Name-sorting.");} |
| 441 |
reads=idSort(reads, repair); |
| 442 |
if(verbose){t.stop("Sort time: ");} |
| 443 |
}else{ |
| 444 |
reads=read1Only(reads); |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
if(ros!=null){ |
| 450 |
assert(reads.size()==readsProcessedThisPass || dedupe || condense || |
| 451 |
(reads.size()*2==readsProcessedThisPass && repair)) : |
| 452 |
reads.size()+", "+readsProcessedThisPass; |
| 453 |
if(verbose){t.start("Writing.");} |
| 454 |
for(Read r : reads){ |
| 455 |
readsOut+=r.pairCount(); |
| 456 |
basesOut+=r.pairLength(); |
| 457 |
} |
| 458 |
ros.add(reads, 0); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
if(ros!=null){ |
| 463 |
if(verbose){outstream.println("Waiting for writing to complete.");} |
| 464 |
errorState=ReadWrite.closeStream(ros)|errorState; |
| 465 |
if(verbose){t.stop("Write time: ");} |
| 466 |
} |
| 467 |
|
| 468 |
if(verbose){outstream.println("Done!");} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
private final FileFormat ffin1[]; |
| 492 |
private final FileFormat ffin2[]; |
| 493 |
|
| 494 |
private final FileFormat ffout1; |
| 495 |
private final FileFormat ffout2; |
| 496 |
|
| 497 |
} |