Debian Codegraph @

current/stream/ConcurrentGenericReadInputStream.java

raw symbols SCIP
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 {//Preparse block for help, config files, and outstream
21 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
22 args=pp.args;
23 //outstream=pp.outstream;
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 //do nothing
45 }else if(Parser.parseQuality(arg, a, b)){
46 //do nothing
47 }else if(Parser.parseFasta(arg, a, b)){
48 //do nothing
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 // while(p[0]==null){
66 // p=cris.producers();
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(); //4567
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){//ln!=null prevents a compiler potential null access warning
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 // System.err.println("fetching list");
110 ln=cris.nextList();
111 reads=(ln!=null ? ln.list : null);
112 // System.out.println("reads: "+(reads==null ? "null" : reads.size()));
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 // assert(false) : BUF_LEN+", "+NUM_BUFFS;
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 // if(maxReads<Long.MAX_VALUE){System.err.println("crisG: maxReads="+maxReads);}
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 // TODO Auto-generated catch block
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 // producer.start();
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 // readSingles();
213
214 addPoison();
215
216 //End thread
217
218 if(verbose){System.err.println("crisG: cris finished addPoison.");}
219 while(!depot.empty.isEmpty() && !shutdown){
220 // System.out.println("crisG: Ending");
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){//TODO Note: for some reason syncing on 'this' instead of 'running' causes a hang. Something else must be syncing improperly on this.
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 //System.err.println("crisG: Adding poison.");
235 //Add poison pills
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 // TODO Auto-generated catch block
245 // System.err.println("crisG: Do not be alarmed by the following error message:");
246 // e.printStackTrace();
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 // @Deprecated
262 // private final void readSingles(){
263 //
264 // while(!shutdown && producer1.hasMore() && generated<maxReads){
265 // ArrayList<Read> list=null;
266 // while(list==null){
267 // try {
268 // list=depot.empty.take();
269 // } catch (InterruptedException e) {
270 // // TODO Auto-generated catch block
271 // e.printStackTrace();
272 // if(shutdown){break;}
273 // }
274 // }
275 // if(shutdown || list==null){break;}
276 //
277 // long bases=0;
278 // while(list.size()<depot.bufferSize && generated<maxReads && bases<MAX_DATA){
279 // Read a=producer1.next();
280 // Read b=(producer2==null ? null : producer2.next());
281 // if(a==null){break;}
282 // readsIn++;
283 // basesIn+=a.length();
284 // if(b!=null){
285 // readsIn++;
286 // basesIn+=b.length();
287 // }
288 // if(randy==null || randy.nextFloat()<samplerate){
289 // list.add(a);
290 // if(b!=null){
291 // assert(a.numericID==b.numericID) : "\n"+a.numericID+", "+b.numericID+"\n"+a.toText(false)+"\n"+b.toText(false)+"\n";
292 // assert(a.mate==null) : "Please set interleaved=false when using dual input files.\n"+a.id+"\n"+a.mate.id+"\n"+producer1+"\n"+producer2;
293 // assert(b.mate==null) : "Please set interleaved=false when using dual input files.";
294 // a.mate=b;
295 // b.mate=a;
296 //
297 // assert(a.pairnum()==0);
298 // b.setPairnum(1);
299 // bases+=(b.bases==null ? 0 : b.length());
300 // }
301 // bases+=(a.bases==null ? 0 : a.length());
302 // }
303 // incrementGenerated(1);
304 // }
305 //
306 // if(verbose){System.err.println("crisG: E: Adding list("+list.size()+") to full.");}
307 // depot.full.add(list);
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 // System.out.println("crisG: a");
318 if(verbose){System.err.println(getClass().getName()+" entering read lists loop.");}
319 while(buffer1!=poison && (buffer1!=null || (!shutdown && generated<maxReads))){
320 // System.out.println("crisG: b");
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 // TODO Auto-generated catch block
329 e.printStackTrace();
330 if(shutdown){break;}
331 }
332 if(verbose){System.err.println("crisG: Fetched "+(list==null ? "null" : ""+list.size()));}
333 }
334 // System.out.println("crisG: c");
335 if(verbose){System.err.println("crisG: Left empty fetch loop.");}
336 if(shutdown || list==null){
337 //System.err.println("crisG: Shutdown triggered; breaking.");
338 break;
339 }
340 // System.out.println("crisG: d");
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 // TODO Auto-generated catch block
353 e.printStackTrace();
354 }
355 }
356 // System.out.println("crisG: e");
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 // TODO Auto-generated catch block
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 // System.out.println("crisG: f");
372 next=0;
373 }
374 // System.out.println("crisG: g");
375 if(buffer1==null || buffer1==poison || shutdown){
376 // if(list!=null && list.size()>0){
377 // if(verbose){System.err.println("crisG: G: Adding list("+list.size()+") to full.");}
378 // depot.full.add(list);
379 // list=null;
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); //Although this is not really necessary.
385
386 // assert(!set2.contains(buffer1)) : buffer1.hashCode();
387 // set2.add(buffer1);
388 // System.out.println(buffer1.hashCode());
389
390 if(buffer2!=null){
391 // System.out.println("crisG: h");
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 //Code disabled because it does not actually seem to make anything faster.
402 // if(buffer1.size()<=(BUF_LEN-list.size()) && (buffer1.size()+generated)<maxReads && randy==null){
403 // //System.out.println("crisG: j");
404 // //Then do a quicker bulk operation
405 //
406 // for(Read a : buffer1){
407 // list.add(a);
408 // Read b=a.mate;
409 // readsIn++;
410 // basesIn+=a.length();
411 // bases+=(a.bases==null ? 0 : a.length());
412 //// bases+=(b==null || b.bases==null ? 0 : b.length());
413 // if(b!=null){
414 // readsIn++;
415 // basesIn+=b.length();
416 // bases+=b.length();
417 // assert(a.pairnum()==0 && b.pairnum()==1);
418 // }
419 //// System.out.println(generated+", "+readsIn+", "+(b==null));
420 // }
421 //// list.addAll(buffer1); //This is actually slower due to an array clone operation.
422 // incrementGenerated(buffer1.size());
423 //
424 // next=0;
425 // buffer1=null;
426 // buffer2=null;
427 // }else
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 // assert(a.numericID==b.numericID) : "\n"+a.numericID+", "+b.numericID+"\n"+a.toText(false)+"\n"+b.toText(false)+"\n";
439 // a.mate=b;
440 // b.mate=a;
441 //
442 // assert(a.pairnum()==0);
443 // b.setPairnum(1);
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 // assert(a.pairnum()==0 && a.mate.pairnum()==1);
454 }
455 }
456 incrementGenerated(1);
457 next++;
458 }
459 // System.out.println("crisG: l");
460
461
462 if(next>=buffer1.size()){
463 buffer1=null;
464 buffer2=null;
465 next=0;
466 // System.out.println("crisG: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
467 }else{
468 // System.out.println("crisG: ------------------------------------------------");
469 }
470 // System.out.println("crisG: m");
471 }
472 if(verbose){System.err.println("crisG: Loop end: list.size()="+(list.size()+", depot.bufferSize="+depot.bufferSize+", generated="+generated));}
473 // System.out.println("crisG: n");
474 if(verbose){System.err.println(Thread.currentThread().getName());}
475 }
476
477 // System.out.println("crisG: p");
478 // System.err.println("crisG: Adding list to full depot. Shutdown="+shutdown);
479 if(verbose){System.err.println("crisG: F: Adding list("+list.size()+") to full.");}
480 depot.full.add(list);
481 list=null;
482 // System.err.println("crisG: Added.");
483
484 // System.out.println("crisG: o");
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 // System.out.println("crisG: q");
493 }
494 // System.out.println("crisG: r");
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 // assert(a.pairnum()!=b.pairnum());
524 }
525
526 if(len1>len2){
527 //do nothing;
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 // System.err.println("crisG: Called shutdown.");
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; //Added Oct 9, 2014
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 // if(verbose){System.err.println(((FastqReadInputStream)producer1).tf.isOpen());}
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 // System.out.println("crisG: B");
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 // TODO Auto-generated catch block
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 // TODO Auto-generated catch block
634 e.printStackTrace();
635 }
636 }
637 }
638 }
639
640 //This assertion should be impossible but somehow it fired.
641 //However, by the time the warning was issued, "isAlive" returned false anyway.
642 // assert(threads==null || threads.length<2 || threads[1]==null || !threads[1].isAlive()) : ((ReadThread)threads[1]).generatedLocal+", "+threads[1].isAlive()+", "+threads[1].getState();
643 // threads=null;
644 // System.out.println("crisG: C");
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 // System.out.println("crisG: E");
687 if(verbose){System.err.println(getClass().getName()+" depot.bufferSize="+depot.bufferSize+", generated="+generatedLocal);}
688 // System.out.println("crisG: F");
689 try {
690 list=producer.nextList();
691 } catch (OutOfMemoryError e){
692 KillSwitch.memKill(e);
693 } catch (Throwable e1) {
694 // TODO
695 // System.err.print('*');
696 e1.printStackTrace();
697 list=null;
698 shutdown=true;
699 try {
700 pq.put(new ArrayList<Read>(1));
701 } catch (InterruptedException e) {
702 // TODO Auto-generated catch block
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 // System.out.println("crisG: G");
709 if(list==null){
710 // System.out.println("crisG: H");
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); //Although this is not really necessary.
716 // System.out.println("crisG: I");
717 if(list.size()+generatedLocal>maxReads){
718 // System.out.println("crisG: J");
719 if(verbose){System.err.println("crisG: Removing extra reads.");}
720 while(list.size()+generatedLocal>maxReads){list.remove(list.size()-1);}
721 // System.out.println("crisG: K");
722 }
723 // System.out.println("crisG: A");
724 while(list!=null && !shutdown){
725 // System.out.println("crisG: B");
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 // TODO Auto-generated catch block
736 e.printStackTrace();
737 }
738 // System.out.println("crisG: C");
739 }
740 // System.out.println("crisG: D");
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 //TODO Note that this could cause a deadlock if there was a premature shutdown, so the consumer died while the queue was full.
752 try {
753 // pq.offer(poison, 10000, TimeUnit.SECONDS);
754 pq.put(poison);
755 b=false;
756 } catch (InterruptedException e) {
757 // TODO Auto-generated catch block
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 // Data.sysout.println((long)(0.5+duration*0.000000001)+" ");
782 }else{
783 nextProgress+=PROGRESS_INCR;
784 Data.sysout.print('.');
785 }
786 }
787 // System.err.println("crisG: generated="+generated+"\treadsIn="+readsIn);
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 /** TODO */
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 }