| 1 |
package stream; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.concurrent.ArrayBlockingQueue; |
| 5 |
import java.util.concurrent.TimeUnit; |
| 6 |
|
| 7 |
import dna.Data; |
| 8 |
import shared.KillSwitch; |
| 9 |
import shared.Parse; |
| 10 |
import shared.Parser; |
| 11 |
import shared.PreParser; |
| 12 |
import shared.Timer; |
| 13 |
import shared.Tools; |
| 14 |
import structures.ListNum; |
| 15 |
|
| 16 |
public class ConcurrentGenericReadInputStream extends ConcurrentReadInputStream { |
| 17 |
|
| 18 |
public static void main(String[] args){ |
| 19 |
|
| 20 |
{ |
| 21 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 22 |
args=pp.args; |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
String in1=args[0]; |
| 27 |
String in2=(args.length<2 || args[1].equalsIgnoreCase("null") || args[1].contains("=") ? null : args[1]); |
| 28 |
if(in2!=null){ |
| 29 |
assert(!in1.equalsIgnoreCase(in2)); |
| 30 |
FASTQ.TEST_INTERLEAVED=false; |
| 31 |
}else{ |
| 32 |
FASTQ.TEST_INTERLEAVED=true; |
| 33 |
FASTQ.FORCE_INTERLEAVED=true; |
| 34 |
} |
| 35 |
|
| 36 |
long maxReads=-1; |
| 37 |
for(int i=1; i<args.length; i++){ |
| 38 |
final String arg=args[i]; |
| 39 |
final String[] split=arg.split("="); |
| 40 |
String a=split[0].toLowerCase(); |
| 41 |
String b=split.length>1 ? split[1] : null; |
| 42 |
|
| 43 |
if(Parser.parseZip(arg, a, b)){ |
| 44 |
|
| 45 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 46 |
|
| 47 |
}else if(Parser.parseFasta(arg, a, b)){ |
| 48 |
|
| 49 |
}else if(a.equals("reads") || a.startsWith("maxreads")){ |
| 50 |
maxReads=Parse.parseKMG(b); |
| 51 |
}else{ |
| 52 |
throw new RuntimeException("Unknown parameter "+args[i]); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
Parser.processQuality(); |
| 57 |
|
| 58 |
assert(FastaReadInputStream.settingsOK()); |
| 59 |
Timer t=new Timer(); |
| 60 |
|
| 61 |
ConcurrentReadInputStream cris=getReadInputStream(maxReads, false, true, in1, in2); |
| 62 |
System.out.println("Fetched "+cris.getClass().getName()); |
| 63 |
{ |
| 64 |
Object[] p=cris.producers(); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
System.out.print("Producers: "); |
| 69 |
String comma=""; |
| 70 |
for(Object o : p){ |
| 71 |
System.out.print(comma+(o==null ? "null" : o.getClass().getName())); |
| 72 |
comma=", "; |
| 73 |
} |
| 74 |
System.out.println(); |
| 75 |
} |
| 76 |
boolean paired=cris.paired(); |
| 77 |
System.out.println("paired="+paired); |
| 78 |
cris.start(); |
| 79 |
|
| 80 |
ListNum<Read> ln=cris.nextList(); |
| 81 |
ArrayList<Read> reads=(ln!=null ? ln.list : null); |
| 82 |
|
| 83 |
if(reads!=null && !reads.isEmpty()){ |
| 84 |
Read r=reads.get(0); |
| 85 |
assert((r.mate!=null)==paired); |
| 86 |
} |
| 87 |
|
| 88 |
long readCount=0; |
| 89 |
long baseCount=0; |
| 90 |
|
| 91 |
while(ln!=null && reads!=null && reads.size()>0){ |
| 92 |
|
| 93 |
for(Read r : reads){ |
| 94 |
Read r2=r.mate; |
| 95 |
if(r!=null){ |
| 96 |
readCount++; |
| 97 |
if(r.bases!=null){ |
| 98 |
baseCount+=r.length(); |
| 99 |
} |
| 100 |
} |
| 101 |
if(r2!=null){ |
| 102 |
readCount++; |
| 103 |
if(r2.bases!=null){ |
| 104 |
baseCount+=r2.length(); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
cris.returnList(ln); |
| 109 |
|
| 110 |
ln=cris.nextList(); |
| 111 |
reads=(ln!=null ? ln.list : null); |
| 112 |
|
| 113 |
} |
| 114 |
System.err.println("Finished reading"); |
| 115 |
cris.returnList(ln); |
| 116 |
|
| 117 |
cris.close(); |
| 118 |
t.stop(); |
| 119 |
|
| 120 |
System.out.println("Reads: \t"+readCount); |
| 121 |
System.out.println("Bases: \t"+baseCount); |
| 122 |
System.out.println("Avg Length: \t"+Tools.format("%.2f",baseCount*1.0/readCount)); |
| 123 |
System.out.println("Time: \t"+t); |
| 124 |
} |
| 125 |
|
| 126 |
public ConcurrentGenericReadInputStream(ReadInputStream source1, ReadInputStream source2, long maxReadsToGenerate){ |
| 127 |
super((source1==null ? "null" : source1.fname())+","+(source2==null ? "null" : source2.fname())); |
| 128 |
assert(source1!=source2); |
| 129 |
producer1=source1; |
| 130 |
depot=new ConcurrentDepot<Read>(BUF_LEN, NUM_BUFFS); |
| 131 |
|
| 132 |
producer2=source2; |
| 133 |
assert(source2==null || !FASTQ.FORCE_INTERLEAVED) : "Please do not set 'interleaved=true' with dual input files."; |
| 134 |
maxReads=maxReadsToGenerate>=0 ? maxReadsToGenerate : Long.MAX_VALUE; |
| 135 |
if(maxReads==0){ |
| 136 |
System.err.println("crisG: Warning - created a read stream for 0 reads."); |
| 137 |
assert(false); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
if(producer1!=null){p1q=new ArrayBlockingQueue<ArrayList<Read>>(4);} |
| 142 |
if(producer2!=null){p2q=new ArrayBlockingQueue<ArrayList<Read>>(4);} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public synchronized ListNum<Read> nextList() { |
| 147 |
ArrayList<Read> list=null; |
| 148 |
if(verbose){System.err.println("crisG: **************** nextList() was called; shutdown="+shutdown+", depot.full="+depot.full.size());} |
| 149 |
while(list==null){ |
| 150 |
if(shutdown){ |
| 151 |
if(verbose){System.err.println("crisG: **************** nextList() returning null; shutdown="+shutdown+", depot.full="+depot.full.size());} |
| 152 |
return null; |
| 153 |
} |
| 154 |
try { |
| 155 |
list=depot.full.take(); |
| 156 |
assert(list!=null); |
| 157 |
} catch (InterruptedException e) { |
| 158 |
|
| 159 |
e.printStackTrace(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if(verbose){System.err.println("crisG: **************** nextList() returning list of size "+list.size()+"; shutdown="+shutdown+", depot.full="+depot.full.size());} |
| 164 |
ListNum<Read> ln=new ListNum<Read>(list, listnum); |
| 165 |
listnum++; |
| 166 |
return ln; |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public void returnList(long listNumber, boolean poison){ |
| 171 |
if(poison){ |
| 172 |
if(verbose){System.err.println("crisG: A: Adding empty list to full.");} |
| 173 |
depot.full.add(new ArrayList<Read>(0)); |
| 174 |
}else{ |
| 175 |
if(verbose){System.err.println("crisG: A: Adding empty list to empty.");} |
| 176 |
depot.empty.add(new ArrayList<Read>(BUF_LEN)); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public void run() { |
| 182 |
try { |
| 183 |
run0(); |
| 184 |
} catch (AssertionError e) { |
| 185 |
KillSwitch.assertionKill(e); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
private void run0() { |
| 190 |
|
| 191 |
synchronized(running){ |
| 192 |
assert(!running[0]) : "This cris was started by multiple threads."; |
| 193 |
running[0]=true; |
| 194 |
} |
| 195 |
|
| 196 |
ReadThread rt1=null; |
| 197 |
ReadThread rt2=null; |
| 198 |
rt1=new ReadThread(producer1, p1q); |
| 199 |
rt2=(producer2==null ? null : new ReadThread(producer2, p2q)); |
| 200 |
rt1.start(); |
| 201 |
if(rt2!=null){rt2.start();} |
| 202 |
|
| 203 |
threads=(rt1==null ? new Thread[] {Thread.currentThread()} : |
| 204 |
rt2==null ? new Thread[] {Thread.currentThread(), rt1} : |
| 205 |
new Thread[] {Thread.currentThread(), rt1, rt2}); |
| 206 |
|
| 207 |
try { |
| 208 |
readLists(); |
| 209 |
} catch (OutOfMemoryError e) { |
| 210 |
KillSwitch.memKill(e); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
addPoison(); |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
if(verbose){System.err.println("crisG: cris finished addPoison.");} |
| 219 |
while(!depot.empty.isEmpty() && !shutdown){ |
| 220 |
|
| 221 |
if(verbose){System.err.println("crisG: B: Adding empty lists to full.");} |
| 222 |
depot.full.add(depot.empty.poll()); |
| 223 |
} |
| 224 |
if(verbose){System.err.println("crisG: cris thread syncing before shutdown.");} |
| 225 |
|
| 226 |
synchronized(running){ |
| 227 |
assert(running[0]); |
| 228 |
running[0]=false; |
| 229 |
} |
| 230 |
if(verbose){System.err.println("crisG: cris thread terminated. Final depot size: "+depot.full.size()+", "+depot.empty.size());} |
| 231 |
} |
| 232 |
|
| 233 |
private final void addPoison(){ |
| 234 |
|
| 235 |
|
| 236 |
if(verbose){System.err.println("crisG: C: Adding poison to full.");} |
| 237 |
depot.full.add(new ArrayList<Read>()); |
| 238 |
for(int i=1; i<depot.bufferCount; i++){ |
| 239 |
ArrayList<Read> list=null; |
| 240 |
while(list==null){ |
| 241 |
try { |
| 242 |
list=depot.empty.poll(1000, TimeUnit.MILLISECONDS); |
| 243 |
} catch (InterruptedException e) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if(shutdown){ |
| 248 |
i=depot.bufferCount; |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
if(list!=null){ |
| 254 |
if(verbose){System.err.println("crisG: D: Adding list("+list.size()+") to full.");} |
| 255 |
depot.full.add(list); |
| 256 |
} |
| 257 |
} |
| 258 |
if(verbose){System.err.println("crisG: Added poison.");} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
private final void readLists(){ |
| 312 |
ArrayList<Read> buffer1=null; |
| 313 |
ArrayList<Read> buffer2=null; |
| 314 |
ArrayList<Read> list=null; |
| 315 |
int next=0; |
| 316 |
|
| 317 |
|
| 318 |
if(verbose){System.err.println(getClass().getName()+" entering read lists loop.");} |
| 319 |
while(buffer1!=poison && (buffer1!=null || (!shutdown && generated<maxReads))){ |
| 320 |
|
| 321 |
if(verbose){System.err.println("crisG: looping: buffer1==null "+(buffer1==null)+", buffer1==poison "+(buffer1==poison) |
| 322 |
+", shutdown="+shutdown+", generated<maxReads="+(generated<maxReads));} |
| 323 |
while(list==null){ |
| 324 |
if(verbose){System.err.println("crisG: Fetching an empty list: generated="+generated+"/"+maxReads);} |
| 325 |
try { |
| 326 |
list=depot.empty.take(); |
| 327 |
} catch (InterruptedException e) { |
| 328 |
|
| 329 |
e.printStackTrace(); |
| 330 |
if(shutdown){break;} |
| 331 |
} |
| 332 |
if(verbose){System.err.println("crisG: Fetched "+(list==null ? "null" : ""+list.size()));} |
| 333 |
} |
| 334 |
|
| 335 |
if(verbose){System.err.println("crisG: Left empty fetch loop.");} |
| 336 |
if(shutdown || list==null){ |
| 337 |
|
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
if(verbose){System.err.println("crisG: Entering full fetch loop.");} |
| 343 |
long bases=0; |
| 344 |
while(list.size()<depot.bufferSize && generated<maxReads && bases<MAX_DATA){ |
| 345 |
if(verbose){System.err.println("crisG: list.size()="+list.size()+", depot.bufferSize="+depot.bufferSize+", generated="+generated);} |
| 346 |
if(buffer1==null || next>=buffer1.size()){ |
| 347 |
buffer1=null; |
| 348 |
while(!shutdown && buffer1==null){ |
| 349 |
try { |
| 350 |
buffer1=p1q.take(); |
| 351 |
} catch (InterruptedException e) { |
| 352 |
|
| 353 |
e.printStackTrace(); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
if(buffer1!=null && p2q!=null){ |
| 359 |
buffer2=null; |
| 360 |
while(!shutdown && buffer2==null){ |
| 361 |
try { |
| 362 |
buffer2=p2q.take(); |
| 363 |
} catch (InterruptedException e) { |
| 364 |
|
| 365 |
e.printStackTrace(); |
| 366 |
} |
| 367 |
} |
| 368 |
if(buffer2!=null && FASTQ.PAIR_READS){pair(buffer1, buffer2);} |
| 369 |
if(REMOVE_DISCARDED_READS){removeDiscarded(buffer1, buffer2);} |
| 370 |
} |
| 371 |
|
| 372 |
next=0; |
| 373 |
} |
| 374 |
|
| 375 |
if(buffer1==null || buffer1==poison || shutdown){ |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if(verbose){System.err.println("crisG: Breaking because buffer1==null: "+(buffer1==null)+" || buffer1==poison: "+(buffer1==poison)+" || shutdown: "+shutdown);} |
| 382 |
break; |
| 383 |
} |
| 384 |
assert(buffer1.size()<=BUF_LEN); |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
if(buffer2!=null){ |
| 391 |
|
| 392 |
|
| 393 |
if(buffer2!=null && (buffer1==null || buffer2.size()!=buffer1.size()) && !ALLOW_UNEQUAL_LENGTHS){ |
| 394 |
System.err.println("crisG: Error: Misaligned read streams."); |
| 395 |
errorState=true; |
| 396 |
return; |
| 397 |
} |
| 398 |
assert(ALLOW_UNEQUAL_LENGTHS || buffer2==null || buffer2.size()==buffer1.size()); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
{ |
| 429 |
|
| 430 |
while(next<buffer1.size() && list.size()<depot.bufferSize && generated<maxReads && bases<MAX_DATA){ |
| 431 |
Read a=buffer1.get(next); |
| 432 |
Read b=a.mate; |
| 433 |
readsIn++; |
| 434 |
basesIn+=a.length(); |
| 435 |
if(b!=null){ |
| 436 |
readsIn++; |
| 437 |
basesIn+=b.length(); |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
assert(a.pairnum()==0 && b.pairnum()==1 && a.mate==b && b.mate==a && a.numericID==b.numericID) : |
| 445 |
"There is something wrong with the read pairing.\n"+ |
| 446 |
a.pairnum()+", "+(b.pairnum())+", "+(a.mate==b)+", "+(b.mate==a)+", "+(a.numericID)+", "+(b.numericID); |
| 447 |
} |
| 448 |
if(randy==null || randy.nextFloat()<samplerate){ |
| 449 |
list.add(a); |
| 450 |
bases+=a.length(); |
| 451 |
if(a.mate!=null){ |
| 452 |
bases+=a.mateLength(); |
| 453 |
|
| 454 |
} |
| 455 |
} |
| 456 |
incrementGenerated(1); |
| 457 |
next++; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
if(next>=buffer1.size()){ |
| 463 |
buffer1=null; |
| 464 |
buffer2=null; |
| 465 |
next=0; |
| 466 |
|
| 467 |
}else{ |
| 468 |
|
| 469 |
} |
| 470 |
|
| 471 |
} |
| 472 |
if(verbose){System.err.println("crisG: Loop end: list.size()="+(list.size()+", depot.bufferSize="+depot.bufferSize+", generated="+generated));} |
| 473 |
|
| 474 |
if(verbose){System.err.println(Thread.currentThread().getName());} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
if(verbose){System.err.println("crisG: F: Adding list("+list.size()+") to full.");} |
| 480 |
depot.full.add(list); |
| 481 |
list=null; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
if(buffer1==poison){ |
| 486 |
if(verbose){System.err.println("crisG: Detected poison from buffer1.");} |
| 487 |
break; |
| 488 |
} |
| 489 |
if(verbose){System.err.println("crisG: Finished loop iteration.\n");} |
| 490 |
if(verbose){System.err.println("crisG: loop end: buffer1==null "+(buffer1==null)+", buffer1==poison "+(buffer1==poison) |
| 491 |
+", shutdown="+shutdown+", generated<maxReads="+(generated<maxReads));} |
| 492 |
|
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
p1q.clear(); |
| 498 |
if(p2q!=null){p2q.clear();} |
| 499 |
} |
| 500 |
|
| 501 |
private final void pair(ArrayList<Read> buffer1, ArrayList<Read> buffer2){ |
| 502 |
final int len1=buffer1.size(), len2=buffer2.size(); |
| 503 |
assert(ALLOW_UNEQUAL_LENGTHS || len1==len2) : "\nThere appear to be different numbers of reads in the paired input files." + |
| 504 |
"\nThe pairing may have been corrupted by an upstream process. It may be fixable by running repair.sh."; |
| 505 |
final int lim=Tools.min(len1, len2); |
| 506 |
|
| 507 |
if(FASTQ.FLIP_R2){ |
| 508 |
for(Read r2 : buffer2) {r2.reverseComplement();} |
| 509 |
} |
| 510 |
|
| 511 |
for(int i=0; i<lim; i++){ |
| 512 |
Read a=buffer1.get(i); |
| 513 |
Read b=buffer2.get(i); |
| 514 |
|
| 515 |
assert(a.numericID==b.numericID) : "\n"+a.numericID+", "+b.numericID+"\n"+a.toText(false)+"\n"+b.toText(false)+"\n"; |
| 516 |
assert(a.mate==null) : "Please set interleaved=false when using dual input files.\n"+a.id+"\n"+a.mate.id+"\n"+b.id+"\n"+producer1+"\n"+producer2; |
| 517 |
assert(b.mate==null) : "Please set interleaved=false when using dual input files."; |
| 518 |
a.mate=b; |
| 519 |
b.mate=a; |
| 520 |
|
| 521 |
assert(a.pairnum()==0); |
| 522 |
b.setPairnum(1); |
| 523 |
|
| 524 |
} |
| 525 |
|
| 526 |
if(len1>len2){ |
| 527 |
|
| 528 |
}else if(len2>len1){ |
| 529 |
for(int i=lim; i<len2; i++){ |
| 530 |
Read b=buffer2.get(i); |
| 531 |
b.setPairnum(0); |
| 532 |
buffer1.add(b); |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
private static final int removeDiscarded(ArrayList<Read> buffer1, ArrayList<Read> buffer2){ |
| 538 |
int removed=0; |
| 539 |
if(buffer2==null){ |
| 540 |
for(int i=0; i<buffer1.size(); i++){ |
| 541 |
Read a=buffer1.get(i); |
| 542 |
if(a.discarded()){ |
| 543 |
buffer1.set(i, null); |
| 544 |
removed++; |
| 545 |
} |
| 546 |
} |
| 547 |
}else{ |
| 548 |
for(int i=0; i<buffer1.size(); i++){ |
| 549 |
Read a=buffer1.get(i); |
| 550 |
Read b=buffer2.get(i); |
| 551 |
if(a.discarded() || b.discarded()){ |
| 552 |
buffer1.set(i, null); |
| 553 |
buffer2.set(i, null); |
| 554 |
removed++; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
if(removed>0){ |
| 559 |
Tools.condenseStrict(buffer1); |
| 560 |
if(buffer2!=null){Tools.condenseStrict(buffer2);} |
| 561 |
} |
| 562 |
return removed; |
| 563 |
} |
| 564 |
|
| 565 |
private boolean shutdown=false; |
| 566 |
|
| 567 |
@Override |
| 568 |
public void shutdown(){ |
| 569 |
|
| 570 |
shutdown=true; |
| 571 |
if(!shutdown){ |
| 572 |
for(Thread t : threads){ |
| 573 |
if(t!=null && t.isAlive()){ |
| 574 |
t.interrupt(); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
@Override |
| 581 |
public synchronized void restart(){ |
| 582 |
shutdown=false; |
| 583 |
p1q.clear(); |
| 584 |
if(p2q!=null){p2q.clear();} |
| 585 |
producer1.restart(); |
| 586 |
if(producer2!=null){producer2.restart();} |
| 587 |
depot=new ConcurrentDepot<Read>(BUF_LEN, NUM_BUFFS); |
| 588 |
generated=0; |
| 589 |
basesIn=0; |
| 590 |
readsIn=0; |
| 591 |
listnum=0; |
| 592 |
nextProgress=PROGRESS_INCR; |
| 593 |
lastTime=System.nanoTime(); |
| 594 |
} |
| 595 |
|
| 596 |
@Override |
| 597 |
public synchronized void close(){ |
| 598 |
if(verbose){System.err.println("crisG: Called shutdown for "+producer1+"; "+threads[0].getState());} |
| 599 |
|
| 600 |
shutdown(); |
| 601 |
errorState|=producer1.close(); |
| 602 |
if(producer2!=null){errorState|=producer2.close();} |
| 603 |
if(threads!=null && threads[0]!=null && threads[0].isAlive()){ |
| 604 |
|
| 605 |
while(threads[0].isAlive()){ |
| 606 |
|
| 607 |
ArrayList<Read> list=null; |
| 608 |
for(int i=0; i<1000 && list==null && threads[0].isAlive(); i++){ |
| 609 |
try { |
| 610 |
list=depot.full.poll(200, TimeUnit.MILLISECONDS); |
| 611 |
} catch (InterruptedException e) { |
| 612 |
|
| 613 |
System.err.println("crisG: Do not be alarmed by the following error message:"); |
| 614 |
e.printStackTrace(); |
| 615 |
break; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
if(list!=null){ |
| 620 |
list.clear(); |
| 621 |
depot.empty.add(list); |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
} |
| 626 |
|
| 627 |
if(threads!=null){ |
| 628 |
for(int i=1; i<threads.length; i++){ |
| 629 |
while(threads[i]!=null && threads[i].getState()!=Thread.State.TERMINATED){ |
| 630 |
try { |
| 631 |
threads[i].join(); |
| 632 |
} catch (InterruptedException e) { |
| 633 |
|
| 634 |
e.printStackTrace(); |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
if(verbose){System.err.println("crisG: shutdown exited; errorState="+errorState);} |
| 647 |
} |
| 648 |
|
| 649 |
@Override |
| 650 |
public boolean paired() { |
| 651 |
return FASTQ.PAIR_READS && (producer1.paired() || producer2!=null); |
| 652 |
} |
| 653 |
|
| 654 |
@Override |
| 655 |
public boolean verbose(){return verbose;} |
| 656 |
|
| 657 |
private class ReadThread extends Thread{ |
| 658 |
ReadThread(ReadInputStream producer_, ArrayBlockingQueue<ArrayList<Read>> pq_){ |
| 659 |
producer=producer_; |
| 660 |
pq=pq_; |
| 661 |
} |
| 662 |
|
| 663 |
@Override |
| 664 |
public void run(){ |
| 665 |
try { |
| 666 |
readLists(); |
| 667 |
} catch (AssertionError e) { |
| 668 |
KillSwitch.assertionKill(e); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
private final void readLists(){ |
| 673 |
|
| 674 |
ArrayList<Read> list=null; |
| 675 |
|
| 676 |
if(verbose){System.err.println(getClass().getName()+" entering read lists loop.");} |
| 677 |
while(list!=null || (!shutdown && producer.hasMore() && generatedLocal<maxReads)){ |
| 678 |
|
| 679 |
if(verbose){System.err.println(getClass().getName()+" looping: buffer1==null "+(list==null)+", shutdown="+shutdown+ |
| 680 |
", producer.hasMore()="+producer.hasMore()+", generated<maxReads="+(generatedLocal<maxReads));} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
if(verbose){System.err.println(getClass().getName()+" Entering full fetch loop.");} |
| 685 |
while(generatedLocal<maxReads){ |
| 686 |
|
| 687 |
if(verbose){System.err.println(getClass().getName()+" depot.bufferSize="+depot.bufferSize+", generated="+generatedLocal);} |
| 688 |
|
| 689 |
try { |
| 690 |
list=producer.nextList(); |
| 691 |
} catch (OutOfMemoryError e){ |
| 692 |
KillSwitch.memKill(e); |
| 693 |
} catch (Throwable e1) { |
| 694 |
|
| 695 |
|
| 696 |
e1.printStackTrace(); |
| 697 |
list=null; |
| 698 |
shutdown=true; |
| 699 |
try { |
| 700 |
pq.put(new ArrayList<Read>(1)); |
| 701 |
} catch (InterruptedException e) { |
| 702 |
|
| 703 |
e.printStackTrace(); |
| 704 |
} |
| 705 |
errorState=true; |
| 706 |
} |
| 707 |
if(verbose){System.err.println(getClass().getName()+" grabbed a list of size "+(list==null ? "null" : list.size()+""));} |
| 708 |
|
| 709 |
if(list==null){ |
| 710 |
|
| 711 |
if(verbose){System.err.println(getClass().getName()+" broke loop on null list.");} |
| 712 |
break; |
| 713 |
} |
| 714 |
assert(list.size()>0); |
| 715 |
assert(list.size()<=BUF_LEN); |
| 716 |
|
| 717 |
if(list.size()+generatedLocal>maxReads){ |
| 718 |
|
| 719 |
if(verbose){System.err.println("crisG: Removing extra reads.");} |
| 720 |
while(list.size()+generatedLocal>maxReads){list.remove(list.size()-1);} |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
while(list!=null && !shutdown){ |
| 725 |
|
| 726 |
try { |
| 727 |
if(verbose){System.err.println("crisG: Trying to add list");} |
| 728 |
pq.put(list); |
| 729 |
generatedLocal+=list.size(); |
| 730 |
list=null; |
| 731 |
if(verbose){ |
| 732 |
System.out.println("crisG: Added list; pq.size() = "+pq.size()); |
| 733 |
} |
| 734 |
} catch (InterruptedException e) { |
| 735 |
|
| 736 |
e.printStackTrace(); |
| 737 |
} |
| 738 |
|
| 739 |
} |
| 740 |
|
| 741 |
if(verbose){System.err.println("crisG: looping");} |
| 742 |
} |
| 743 |
|
| 744 |
if(verbose){System.err.println(getClass().getName()+" Finished inner loop iteration.\n");} |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
if(verbose){System.err.println(getClass().getName()+" attempting to poison output queue.");} |
| 749 |
boolean b=true; |
| 750 |
while(b){ |
| 751 |
|
| 752 |
try { |
| 753 |
|
| 754 |
pq.put(poison); |
| 755 |
b=false; |
| 756 |
} catch (InterruptedException e) { |
| 757 |
|
| 758 |
e.printStackTrace(); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
if(verbose){System.err.println(getClass().getName()+" exited read lists loop: "+(list==null)+", "+shutdown+", "+producer.hasMore()+", "+generatedLocal+", "+maxReads);} |
| 764 |
|
| 765 |
} |
| 766 |
|
| 767 |
private final ArrayBlockingQueue<ArrayList<Read>> pq; |
| 768 |
private final ReadInputStream producer; |
| 769 |
private long generatedLocal=0; |
| 770 |
} |
| 771 |
|
| 772 |
private void incrementGenerated(long amt){ |
| 773 |
generated+=amt; |
| 774 |
if(SHOW_PROGRESS && generated>=nextProgress){ |
| 775 |
if(SHOW_PROGRESS2){ |
| 776 |
nextProgress+=PROGRESS_INCR; |
| 777 |
long x=System.nanoTime(); |
| 778 |
long duration=x-lastTime; |
| 779 |
lastTime=x; |
| 780 |
Data.sysout.println(Tools.format("%.1f", duration*0.000000001)); |
| 781 |
|
| 782 |
}else{ |
| 783 |
nextProgress+=PROGRESS_INCR; |
| 784 |
Data.sysout.print('.'); |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
} |
| 789 |
|
| 790 |
@Override |
| 791 |
public void setSampleRate(float rate, long seed){ |
| 792 |
samplerate=rate; |
| 793 |
if(rate>=1f){ |
| 794 |
randy=null; |
| 795 |
}else if(seed>-1){ |
| 796 |
randy=new java.util.Random(seed); |
| 797 |
}else{ |
| 798 |
randy=new java.util.Random(); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
@Override |
| 803 |
public long basesIn(){return basesIn;} |
| 804 |
@Override |
| 805 |
public long readsIn(){return readsIn;} |
| 806 |
|
| 807 |
@Override |
| 808 |
public boolean errorState(){ |
| 809 |
return errorState || |
| 810 |
(producer1==null ? false : producer1.errorState()) || (producer2==null ? false : producer2.errorState());} |
| 811 |
|
| 812 |
private boolean errorState=false; |
| 813 |
|
| 814 |
private boolean[] running=new boolean[] {false}; |
| 815 |
|
| 816 |
private float samplerate=1f; |
| 817 |
private java.util.Random randy=null; |
| 818 |
|
| 819 |
private ArrayBlockingQueue<ArrayList<Read>> p1q; |
| 820 |
private ArrayBlockingQueue<ArrayList<Read>> p2q; |
| 821 |
|
| 822 |
|
| 823 |
@Override |
| 824 |
public Object[] producers(){return producer2==null ? new Object[] {producer1} : new Object[] {producer1, producer2};} |
| 825 |
|
| 826 |
private Thread[] threads; |
| 827 |
|
| 828 |
public final ReadInputStream producer1; |
| 829 |
public final ReadInputStream producer2; |
| 830 |
private ConcurrentDepot<Read> depot; |
| 831 |
|
| 832 |
private long basesIn=0; |
| 833 |
private long readsIn=0; |
| 834 |
|
| 835 |
private long maxReads; |
| 836 |
private long generated=0; |
| 837 |
private long listnum=0; |
| 838 |
private long nextProgress=PROGRESS_INCR; |
| 839 |
private long lastTime=System.nanoTime(); |
| 840 |
|
| 841 |
public static boolean verbose=false; |
| 842 |
|
| 843 |
private static final ArrayList<Read> poison=new ArrayList<Read>(0); |
| 844 |
|
| 845 |
} |