| 1 |
package template; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.concurrent.locks.ReadWriteLock; |
| 6 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 7 |
|
| 8 |
import fileIO.ByteFile; |
| 9 |
import fileIO.FileFormat; |
| 10 |
import fileIO.ReadWrite; |
| 11 |
import shared.Parse; |
| 12 |
import shared.Parser; |
| 13 |
import shared.PreParser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import stream.ConcurrentReadInputStream; |
| 18 |
import stream.ConcurrentReadOutputStream; |
| 19 |
import stream.FastaReadInputStream; |
| 20 |
import stream.Read; |
| 21 |
import stream.SamLine; |
| 22 |
import stream.SamReadStreamer; |
| 23 |
import stream.SamStreamer; |
| 24 |
import structures.ListNum; |
| 25 |
import tracker.ReadStats; |
| 26 |
import var2.Realigner; |
| 27 |
import var2.SamFilter; |
| 28 |
import var2.ScafMap; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class A_SampleSamStreamer implements Accumulator<A_SampleSamStreamer.ProcessThread> { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static void main(String[] args){ |
| 51 |
|
| 52 |
Timer t=new Timer(); |
| 53 |
|
| 54 |
|
| 55 |
A_SampleSamStreamer x=new A_SampleSamStreamer(args); |
| 56 |
|
| 57 |
|
| 58 |
x.process(t); |
| 59 |
|
| 60 |
|
| 61 |
Shared.closeStream(x.outstream); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public A_SampleSamStreamer(String[] args){ |
| 69 |
|
| 70 |
{ |
| 71 |
PreParser pp=new PreParser(args, getClass(), false); |
| 72 |
args=pp.args; |
| 73 |
outstream=pp.outstream; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 78 |
ReadWrite.setZipThreads(Shared.threads()); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
{ |
| 88 |
final Parser parser=parse(args); |
| 89 |
|
| 90 |
Parser.processQuality(); |
| 91 |
|
| 92 |
maxReads=parser.maxReads; |
| 93 |
overwrite=ReadStats.overwrite=parser.overwrite; |
| 94 |
append=ReadStats.append=parser.append; |
| 95 |
|
| 96 |
in=parser.in1; |
| 97 |
extin=parser.extin; |
| 98 |
|
| 99 |
out=parser.out1; |
| 100 |
extout=parser.extout; |
| 101 |
} |
| 102 |
|
| 103 |
{ |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
if(ploidy<1){System.err.println("WARNING: ploidy not set; assuming ploidy=1."); ploidy=1;} |
| 108 |
samFilter.setSamtoolsFilter(); |
| 109 |
|
| 110 |
streamerThreads=Tools.max(1, Tools.min(streamerThreads, Shared.threads())); |
| 111 |
assert(streamerThreads>0) : streamerThreads; |
| 112 |
} |
| 113 |
|
| 114 |
validateParams(); |
| 115 |
fixExtensions(); |
| 116 |
checkFileExistence(); |
| 117 |
checkStatics(); |
| 118 |
|
| 119 |
|
| 120 |
ffout=FileFormat.testOutput(out, FileFormat.SAM, extout, true, overwrite, append, ordered); |
| 121 |
assert(false) : "TODO: Default output format might be fasta."; |
| 122 |
|
| 123 |
|
| 124 |
ffin=FileFormat.testInput(in, FileFormat.SAM, extin, true, true); |
| 125 |
ffref=FileFormat.testInput(ref, FileFormat.FASTA, null, true, true); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
private Parser parse(String[] args){ |
| 134 |
|
| 135 |
|
| 136 |
Parser parser=new Parser(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
for(int i=0; i<args.length; i++){ |
| 143 |
String arg=args[i]; |
| 144 |
|
| 145 |
|
| 146 |
String[] split=arg.split("="); |
| 147 |
String a=split[0].toLowerCase(); |
| 148 |
String b=split.length>1 ? split[1] : null; |
| 149 |
if(b!=null && b.equalsIgnoreCase("null")){b=null;} |
| 150 |
|
| 151 |
if(a.equals("verbose")){ |
| 152 |
verbose=Parse.parseBoolean(b); |
| 153 |
}else if(a.equals("ref")){ |
| 154 |
ref=b; |
| 155 |
}else if(a.equals("ordered")){ |
| 156 |
ordered=Parse.parseBoolean(b); |
| 157 |
}else if(a.equals("realign")){ |
| 158 |
realign=Parse.parseBoolean(b); |
| 159 |
}else if(a.equals("ploidy")){ |
| 160 |
ploidy=Integer.parseInt(b); |
| 161 |
}else if(a.equals("clearfilters")){ |
| 162 |
if(Parse.parseBoolean(b)){ |
| 163 |
samFilter.clear(); |
| 164 |
} |
| 165 |
}else if(a.equals("parse_flag_goes_here")){ |
| 166 |
long fake_variable=Parse.parseKMG(b); |
| 167 |
|
| 168 |
}else if(samFilter.parse(arg, a, b)){ |
| 169 |
|
| 170 |
}else if(parser.parse(arg, a, b)){ |
| 171 |
|
| 172 |
}else{ |
| 173 |
outstream.println("Unknown parameter "+args[i]); |
| 174 |
assert(false) : "Unknown parameter "+args[i]; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return parser; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
private void fixExtensions(){ |
| 183 |
in=Tools.fixExtension(in); |
| 184 |
ref=Tools.fixExtension(ref); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
private void checkFileExistence(){ |
| 189 |
|
| 190 |
|
| 191 |
if(in==null){throw new RuntimeException("Error - an input file is required.");} |
| 192 |
|
| 193 |
|
| 194 |
assert(false) : "TODO: Check."; |
| 195 |
if(ref==null){throw new RuntimeException("Error - a reference file is required.");} |
| 196 |
|
| 197 |
|
| 198 |
if(!Tools.testOutputFiles(overwrite, append, false, out)){ |
| 199 |
outstream.println((out==null)+", "+out); |
| 200 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out+"\n"); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
if(!Tools.testInputFiles(false, true, in, ref)){ |
| 205 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
if(!Tools.testForDuplicateFiles(true, in, ref, out)){ |
| 210 |
throw new RuntimeException("\nSome file names were specified multiple times.\n"); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
private static void checkStatics(){ |
| 216 |
|
| 217 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 218 |
ByteFile.FORCE_MODE_BF2=true; |
| 219 |
} |
| 220 |
|
| 221 |
assert(FastaReadInputStream.settingsOK()); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
private boolean validateParams(){ |
| 226 |
|
| 227 |
assert(false) : "TODO"; |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
void process(Timer t){ |
| 237 |
|
| 238 |
|
| 239 |
final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR; |
| 240 |
Read.VALIDATE_IN_CONSTRUCTOR=Shared.threads()<4; |
| 241 |
|
| 242 |
|
| 243 |
final SamStreamer ss=makeStreamer(ffin); |
| 244 |
|
| 245 |
|
| 246 |
loadScafMapFromReference(); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
final ConcurrentReadOutputStream ros=makeCros(); |
| 251 |
|
| 252 |
|
| 253 |
readsProcessed=readsOut=0; |
| 254 |
basesProcessed=basesOut=0; |
| 255 |
|
| 256 |
|
| 257 |
spawnThreads(ss, ros); |
| 258 |
|
| 259 |
if(verbose){outstream.println("Finished; closing streams.");} |
| 260 |
|
| 261 |
|
| 262 |
errorState|=ReadStats.writeAll(); |
| 263 |
|
| 264 |
errorState|=ReadWrite.closeStream(ros); |
| 265 |
|
| 266 |
|
| 267 |
Read.VALIDATE_IN_CONSTRUCTOR=vic; |
| 268 |
|
| 269 |
|
| 270 |
t.stop(); |
| 271 |
outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8)); |
| 272 |
outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false)); |
| 273 |
|
| 274 |
|
| 275 |
if(errorState){ |
| 276 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
private void loadScafMapFromReference(){ |
| 281 |
if(loadedRef){return;} |
| 282 |
assert(ref!=null); |
| 283 |
scafMap=ScafMap.loadReference(ref, scafMap, samFilter, true); |
| 284 |
if(realign){Realigner.map=scafMap;} |
| 285 |
loadedRef=true; |
| 286 |
} |
| 287 |
|
| 288 |
private void loadReferenceCustom(){ |
| 289 |
ConcurrentReadInputStream cris=makeRefCris(); |
| 290 |
for(ListNum<Read> ln=cris.nextList(); ln!=null && ln.size()>0; ln=cris.nextList()) { |
| 291 |
|
| 292 |
assert(false) : "TODO"; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
private ConcurrentReadInputStream makeRefCris(){ |
| 297 |
ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffref, null); |
| 298 |
cris.start(); |
| 299 |
if(verbose){outstream.println("Started cris");} |
| 300 |
boolean paired=cris.paired(); |
| 301 |
assert(!paired) : "References should not be paired."; |
| 302 |
return cris; |
| 303 |
} |
| 304 |
|
| 305 |
private SamStreamer makeStreamer(FileFormat ff){ |
| 306 |
if(ff==null){return null;} |
| 307 |
SamStreamer ss=new SamReadStreamer(ff, streamerThreads, true, maxReads); |
| 308 |
ss.start(); |
| 309 |
if(verbose){outstream.println("Started Streamer");} |
| 310 |
return ss; |
| 311 |
} |
| 312 |
|
| 313 |
private ConcurrentReadOutputStream makeCros(){ |
| 314 |
if(ffout==null){return null;} |
| 315 |
|
| 316 |
|
| 317 |
final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8); |
| 318 |
|
| 319 |
final ConcurrentReadOutputStream ros=ConcurrentReadOutputStream.getStream(ffout, null, buff, null, false); |
| 320 |
ros.start(); |
| 321 |
return ros; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
private void spawnThreads(final SamStreamer ss, final ConcurrentReadOutputStream ros){ |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
final int threads=Shared.threads(); |
| 335 |
|
| 336 |
|
| 337 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 338 |
for(int i=0; i<threads; i++){ |
| 339 |
alpt.add(new ProcessThread(ss, ros, i)); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
for(ProcessThread pt : alpt){ |
| 344 |
pt.start(); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
boolean success=ThreadWaiter.waitForThreadsToFinish(alpt, this); |
| 349 |
errorState&=!success; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
} |
| 354 |
|
| 355 |
@Override |
| 356 |
public final void accumulate(ProcessThread pt){ |
| 357 |
readsProcessed+=pt.readsProcessedT; |
| 358 |
basesProcessed+=pt.basesProcessedT; |
| 359 |
readsOut+=pt.readsOutT; |
| 360 |
basesOut+=pt.basesOutT; |
| 361 |
errorState|=(!pt.success); |
| 362 |
} |
| 363 |
|
| 364 |
@Override |
| 365 |
public final boolean success(){return !errorState;} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
class ProcessThread extends Thread { |
| 378 |
|
| 379 |
|
| 380 |
ProcessThread(final SamStreamer ss_, final ConcurrentReadOutputStream ros_, final int tid_){ |
| 381 |
ss=ss_; |
| 382 |
ros=ros_; |
| 383 |
tid=tid_; |
| 384 |
realigner=(realign ? new Realigner() : null); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
@Override |
| 389 |
public void run(){ |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
processInner(); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
success=true; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
void processInner(){ |
| 403 |
|
| 404 |
|
| 405 |
for(ListNum<Read> ln=ss.nextReads(); ln!=null; ln=ss.nextReads()){ |
| 406 |
|
| 407 |
|
| 408 |
processList(ln); |
| 409 |
} |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
void processList(ListNum<Read> ln){ |
| 414 |
|
| 415 |
|
| 416 |
final ArrayList<Read> reads=ln.list; |
| 417 |
|
| 418 |
|
| 419 |
for(int idx=0; idx<reads.size(); idx++){ |
| 420 |
final Read r=reads.get(idx); |
| 421 |
|
| 422 |
|
| 423 |
if(!r.validated()){r.validate(true);} |
| 424 |
|
| 425 |
|
| 426 |
final int initialLength=r.length(); |
| 427 |
|
| 428 |
|
| 429 |
readsProcessedT+=r.pairCount(); |
| 430 |
basesProcessedT+=initialLength; |
| 431 |
|
| 432 |
{ |
| 433 |
|
| 434 |
boolean keep=processRead(r); |
| 435 |
|
| 436 |
if(!keep){reads.set(idx, null);} |
| 437 |
else{ |
| 438 |
readsOutT++; |
| 439 |
basesOutT+=r.length(); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
if(ros!=null){ros.add(reads, ln.id);} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
boolean processRead(final Read r){ |
| 455 |
if(r.bases==null || r.length()<=1){return false;} |
| 456 |
final SamLine sl=r.samline; |
| 457 |
if(samFilter!=null && !samFilter.passesFilter(sl)){return false;} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
assert(false) : "TODO"; |
| 465 |
return true; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
protected long readsProcessedT=0; |
| 470 |
|
| 471 |
protected long basesProcessedT=0; |
| 472 |
|
| 473 |
|
| 474 |
protected long readsOutT=0; |
| 475 |
|
| 476 |
protected long basesOutT=0; |
| 477 |
|
| 478 |
|
| 479 |
boolean success=false; |
| 480 |
|
| 481 |
|
| 482 |
private final SamStreamer ss; |
| 483 |
|
| 484 |
private final ConcurrentReadOutputStream ros; |
| 485 |
|
| 486 |
final int tid; |
| 487 |
|
| 488 |
final Realigner realigner; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
private String in=null; |
| 497 |
|
| 498 |
private String ref=null; |
| 499 |
|
| 500 |
|
| 501 |
private String out=null; |
| 502 |
|
| 503 |
|
| 504 |
private String extin=null; |
| 505 |
|
| 506 |
private String extout=null; |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
protected long readsProcessed=0; |
| 512 |
|
| 513 |
protected long basesProcessed=0; |
| 514 |
|
| 515 |
|
| 516 |
protected long readsOut=0; |
| 517 |
|
| 518 |
protected long basesOut=0; |
| 519 |
|
| 520 |
|
| 521 |
private long maxReads=-1; |
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
private int streamerThreads=SamStreamer.DEFAULT_THREADS; |
| 527 |
|
| 528 |
private boolean loadedRef=false; |
| 529 |
|
| 530 |
private boolean realign=false; |
| 531 |
|
| 532 |
private int ploidy=1; |
| 533 |
|
| 534 |
public ScafMap scafMap; |
| 535 |
public final SamFilter samFilter=new SamFilter(); |
| 536 |
|
| 537 |
@Override |
| 538 |
public final ReadWriteLock rwlock() {return rwlock;} |
| 539 |
private final ReadWriteLock rwlock=new ReentrantReadWriteLock(); |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
private final FileFormat ffin; |
| 547 |
|
| 548 |
private final FileFormat ffref; |
| 549 |
|
| 550 |
|
| 551 |
private final FileFormat ffout; |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
private PrintStream outstream=System.err; |
| 559 |
|
| 560 |
public static boolean verbose=false; |
| 561 |
|
| 562 |
public boolean errorState=false; |
| 563 |
|
| 564 |
private boolean overwrite=true; |
| 565 |
|
| 566 |
private boolean append=false; |
| 567 |
|
| 568 |
private boolean ordered=false; |
| 569 |
|
| 570 |
} |