Debian Codegraph @

current/jgi/FilterBySequence.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.HashSet;
8 import java.util.LinkedHashSet;
9
10 import dna.AminoAcid;
11 import fileIO.ByteFile;
12 import fileIO.FileFormat;
13 import fileIO.ReadWrite;
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.FASTQ;
23 import stream.FastaReadInputStream;
24 import stream.Read;
25 import structures.ListNum;
26 import tracker.ReadStats;
27
28 /**
29 * Filters by exact sequence matches.
30 * Similar to Dedupe.
31 *
32 * @author Brian Bushnell
33 * @date December 18, 2015
34 *
35 */
36 public class FilterBySequence {
37
38 /*--------------------------------------------------------------*/
39 /*---------------- Initialization ----------------*/
40 /*--------------------------------------------------------------*/
41
42 /**
43 * Code entrance from the command line.
44 * @param args Command line arguments
45 */
46 public static void main(String[] args){
47 //Start a timer immediately upon code entrance.
48 Timer t=new Timer();
49
50 //Create an instance of this class
51 FilterBySequence x=new FilterBySequence(args);
52
53 //Run the object
54 x.process(t);
55
56 //Close the print stream if it was redirected
57 Shared.closeStream(x.outstream);
58 }
59
60 /**
61 * Constructor.
62 * @param args Command line arguments
63 */
64 public FilterBySequence(String[] args){
65
66 {//Preparse block for help, config files, and outstream
67 PreParser pp=new PreParser(args, getClass(), false);
68 args=pp.args;
69 outstream=pp.outstream;
70 }
71
72 boolean setInterleaved=false; //Whether interleaved was explicitly set.
73
74 //Set shared static variables
75 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
76 ReadWrite.setZipThreads(Shared.threads());
77 // FASTQ.FORCE_INTERLEAVED=false;
78 // FASTQ.TEST_INTERLEAVED=false;
79
80 //Create a parser object
81 Parser parser=new Parser();
82
83 //Parse each argument
84 for(int i=0; i<args.length; i++){
85 String arg=args[i];
86
87 //Break arguments into their constituent parts, in the form of "a=b"
88 String[] split=arg.split("=");
89 String a=split[0].toLowerCase();
90 String b=split.length>1 ? split[1] : null;
91
92 if(a.equals("verbose")){
93 verbose=Parse.parseBoolean(b);
94 }else if(a.equals("ordered")){
95 ordered=Parse.parseBoolean(b);
96 }else if(a.equals("storebases") || a.equals("keepbases") || a.equals("sb")){
97 storeBases=Parse.parseBoolean(b);
98 }else if(a.equals("include")){
99 include=Parse.parseBoolean(b);
100 }else if(a.equals("exclude")){
101 include=!Parse.parseBoolean(b);
102 }else if(a.equals("rcomp")){
103 rcomp=Parse.parseBoolean(b);
104 }else if(a.equals("casesensitive") || a.equals("case")){
105 toUpperCase=!Parse.parseBoolean(b);
106 }else if(a.equals("hdist") || a.equals("subs") || a.equals("s")){
107 maxSubs=Integer.parseInt(b);
108 }else if(a.equals("mismatchfraction") || a.equals("mf") || a.equals("f") || a.equals("fraction")
109 || a.equals("sf") || a.equals("subfraction")){
110 mismatchFraction=Float.parseFloat(b);
111 }else if(a.equals("lengthdif") || a.equals("maxlengthdif") || a.equals("lendif")){
112 maxLengthDif=Integer.parseInt(b);
113 }else if(a.equals("parse_flag_goes_here")){
114 long fake_variable=Parse.parseKMG(b);
115 //Set a variable here
116 }else if(a.equals("ref")){
117 if(b==null){ref=null;}
118 else{ref=b.split(",");}
119 }else if(a.equals("literal")){
120 if(b==null){literal=null;}
121 else{literal=b.split(",");}
122 }else if(parser.parse(arg, a, b)){//Parse standard flags in the parser
123 //do nothing
124 }else{
125 outstream.println("Unknown parameter "+args[i]);
126 assert(false) : "Unknown parameter "+args[i];
127 }
128 }
129
130 {//Process parser fields
131 Parser.processQuality();
132
133 maxReads=parser.maxReads;
134
135 overwrite=ReadStats.overwrite=parser.overwrite;
136 append=ReadStats.append=parser.append;
137 setInterleaved=parser.setInterleaved;
138
139 in1=parser.in1;
140 in2=parser.in2;
141 qfin1=parser.qfin1;
142 qfin2=parser.qfin2;
143
144 out1=parser.out1;
145 out2=parser.out2;
146 qfout1=parser.qfout1;
147 qfout2=parser.qfout2;
148
149 extin=parser.extin;
150 extout=parser.extout;
151 }
152
153 //Do input file # replacement
154 if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){
155 in2=in1.replace("#", "2");
156 in1=in1.replace("#", "1");
157 }
158
159 //Do output file # replacement
160 if(out1!=null && out2==null && out1.indexOf('#')>-1){
161 out2=out1.replace("#", "2");
162 out1=out1.replace("#", "1");
163 }
164
165 //Adjust interleaved detection based on the number of input files
166 if(in2!=null){
167 if(FASTQ.FORCE_INTERLEAVED){outstream.println("Reset INTERLEAVED to false because paired input files were specified.");}
168 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
169 }
170
171 assert(FastaReadInputStream.settingsOK());
172
173 //Ensure there is an input file
174 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
175
176 //Adjust the number of threads for input file reading
177 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
178 ByteFile.FORCE_MODE_BF2=true;
179 }
180
181 //Ensure out2 is not set without out1
182 if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");}
183
184 //Adjust interleaved settings based on number of output files
185 if(!setInterleaved){
186 assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n";
187 if(in2!=null){ //If there are 2 input streams.
188 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
189 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
190 }else{ //There is one input stream.
191 if(out2!=null){
192 FASTQ.FORCE_INTERLEAVED=true;
193 FASTQ.TEST_INTERLEAVED=false;
194 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
195 }
196 }
197 }
198
199 //Ensure output files can be written
200 if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){
201 outstream.println((out1==null)+", "+(out2==null)+", "+out1+", "+out2);
202 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n");
203 }
204
205 //Ensure input files can be read
206 if(!Tools.testInputFiles(false, true, in1, in2)){
207 throw new RuntimeException("\nCan't read some input files.\n");
208 }
209
210 //Ensure ref files can be read
211 if(!Tools.testInputFiles(true, true, ref)){
212 throw new RuntimeException("\nCan't read to some reference files.\n");
213 }
214
215 //Ensure that no file was specified multiple times
216 if(!Tools.testForDuplicateFiles(true, in1, in2, out1, out2)){
217 throw new RuntimeException("\nSome file names were specified multiple times.\n");
218 }
219
220 //Create output FileFormat objects
221 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, ordered);
222 ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, ordered);
223
224 //Create input FileFormat objects
225 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true);
226 ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true);
227
228 assert(ref!=null || literal!=null) : "No reference sequences.";
229
230 if(ref!=null){
231 ffref=new FileFormat[ref.length];
232 for(int i=0; i<ref.length; i++){
233 ffref[i]=FileFormat.testInput(ref[i], FileFormat.FASTQ, null, true, true);
234 }
235 }else{
236 ffref=null;
237 }
238
239 refSet=new HashSet<Code>();
240 refList=new ArrayList<byte[]>();
241 }
242
243 /*--------------------------------------------------------------*/
244 /*---------------- Outer Methods ----------------*/
245 /*--------------------------------------------------------------*/
246
247 /** Create read streams and process all data */
248 void process(Timer t){
249
250 //Turn off read validation in the input threads to increase speed
251 final boolean vic=Read.VALIDATE_IN_CONSTRUCTOR;
252 Read.VALIDATE_IN_CONSTRUCTOR=Shared.threads()<4;
253
254 {
255 if(ffref!=null){
256 for(FileFormat ff : ffref){
257 processReference(ff);
258 }
259 }
260 if(literal!=null){
261 for(String s : literal){
262 addToRef(new Code(s.getBytes()));
263 }
264 }
265
266 System.err.println("Loaded "+refSet.size()+" unique reference sequence"+(refSet.size()==1 ? "." : "s."));
267 }
268
269 //Create a read input stream
270 final ConcurrentReadInputStream cris;
271 {
272 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, qfin1, qfin2);
273 cris.start(); //Start the stream
274 if(verbose){outstream.println("Started cris");}
275 }
276 boolean paired=cris.paired();
277 if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));}
278
279 //Optionally create a read output stream
280 final ConcurrentReadOutputStream ros;
281 if(ffout1!=null){
282 //Select output buffer size based on whether it needs to be ordered
283 final int buff=(ordered ? Tools.mid(16, 128, (Shared.threads()*2)/3) : 8);
284
285 //Notify user of output mode
286 if(cris.paired() && out2==null && (in1!=null && !ffin1.samOrBam() && !ffout1.samOrBam())){
287 outstream.println("Writing interleaved.");
288 }
289
290 ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, false);
291 ros.start(); //Start the stream
292 }else{ros=null;}
293
294 //Reset counters
295 readsProcessed=0;
296 basesProcessed=0;
297
298 //Process the reads in separate threads
299 spawnProcessThreads(cris, ros);
300
301 if(verbose){outstream.println("Finished; closing streams.");}
302
303 //Write anything that was accumulated by ReadStats
304 errorState|=ReadStats.writeAll();
305 //Close the read streams
306 errorState|=ReadWrite.closeStreams(cris, ros);
307
308 //Reset read validation
309 Read.VALIDATE_IN_CONSTRUCTOR=vic;
310
311 //Report timing and results
312 t.stop();
313 outstream.println();
314 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 12));
315 outstream.println();
316 outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 12, false));
317
318 //Throw an exception of there was an error in a thread
319 if(errorState){
320 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
321 }
322 }
323
324 /** Load ref sequences */
325 private void processReference(final FileFormat ff){
326 final ConcurrentReadInputStream cris;
327 {
328 cris=ConcurrentReadInputStream.getReadInputStream(-1, false, ff, null, null, null);
329 cris.start(); //Start the stream
330 }
331 spawnLoadThreads(cris);
332 ReadWrite.closeStream(cris);
333 }
334
335
336 /** Spawn process threads */
337 private void spawnLoadThreads(final ConcurrentReadInputStream cris){
338
339 //Do anything necessary prior to processing
340
341 //Determine how many threads may be used
342 final int threads=Shared.threads();
343
344 //Fill a list with LoadThreads
345 ArrayList<LoadThread> alpt=new ArrayList<LoadThread>(threads);
346 for(int i=0; i<threads; i++){
347 alpt.add(new LoadThread(cris, i));
348 }
349
350 //Start the threads
351 for(LoadThread pt : alpt){
352 pt.start();
353 }
354
355 //Wait for completion of all threads
356 boolean success=true;
357 for(LoadThread pt : alpt){
358
359 //Wait until this thread has terminated
360 while(pt.getState()!=Thread.State.TERMINATED){
361 try {
362 //Attempt a join operation
363 pt.join();
364 } catch (InterruptedException e) {
365 //Potentially handle this, if it is expected to occur
366 e.printStackTrace();
367 }
368 }
369
370 //Accumulate per-thread statistics
371 readsLoaded+=pt.readsProcessedT;
372 basesLoaded+=pt.basesProcessedT;
373 success&=pt.success;
374 }
375
376 //Track whether any threads failed
377 if(!success){errorState=true;}
378
379 //Do anything necessary after processing
380
381 }
382
383 /** Spawn process threads */
384 private void spawnProcessThreads(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros){
385
386 //Do anything necessary prior to processing
387
388 //Determine how many threads may be used
389 final int threads=Shared.threads();
390
391 //Fill a list with ProcessThreads
392 ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads);
393 for(int i=0; i<threads; i++){
394 alpt.add(new ProcessThread(cris, ros, i));
395 }
396
397 //Start the threads
398 for(ProcessThread pt : alpt){
399 pt.start();
400 }
401
402 //Wait for completion of all threads
403 boolean success=true;
404 for(ProcessThread pt : alpt){
405
406 //Wait until this thread has terminated
407 while(pt.getState()!=Thread.State.TERMINATED){
408 try {
409 //Attempt a join operation
410 pt.join();
411 } catch (InterruptedException e) {
412 //Potentially handle this, if it is expected to occur
413 e.printStackTrace();
414 }
415 }
416
417 //Accumulate per-thread statistics
418 readsProcessed+=pt.readsProcessedT;
419 basesProcessed+=pt.basesProcessedT;
420 readsOut+=pt.readsOutT;
421 basesOut+=pt.basesOutT;
422 success&=pt.success;
423 }
424
425 //Track whether any threads failed
426 if(!success){errorState=true;}
427
428 //Do anything necessary after processing
429
430 }
431
432 /*--------------------------------------------------------------*/
433 /*---------------- Inner Methods ----------------*/
434 /*--------------------------------------------------------------*/
435
436 private void addToRef(Collection<Code> codes) {
437 synchronized(refSet) {
438 for(Code c : codes) {addToRef(c);}
439 }
440 }
441
442 private void addToRef(Code c) {
443 boolean novel=refSet.add(c);
444 if(novel) {refList.add(c.bases);}
445 // System.err.println(new String(c.bases)+": "+novel);
446 }
447
448 /*--------------------------------------------------------------*/
449 /*---------------- Inner Classes ----------------*/
450 /*--------------------------------------------------------------*/
451
452 private class ProcessThread extends Thread {
453
454 //Constructor
455 ProcessThread(final ConcurrentReadInputStream cris_, final ConcurrentReadOutputStream ros_, final int tid_){
456 cris=cris_;
457 ros=ros_;
458 tid=tid_;
459 }
460
461 //Called by start()
462 @Override
463 public void run(){
464 //Do anything necessary prior to processing
465
466 //Process the reads
467 processInner();
468
469 //Do anything necessary after processing
470
471 //Indicate successful exit status
472 success=true;
473 }
474
475 /** Iterate through the reads */
476 void processInner(){
477
478 //Grab the first ListNum of reads
479 ListNum<Read> ln=cris.nextList();
480 //Grab the actual read list from the ListNum
481 ArrayList<Read> reads=(ln!=null ? ln.list : null);
482
483 //Check to ensure pairing is as expected
484 if(reads!=null && !reads.isEmpty()){
485 Read r=reads.get(0);
486 // assert(ffin1.samOrBam() || (r.mate!=null)==cris.paired()); //Disabled due to non-static access
487 }
488
489 //As long as there is a nonempty read list...
490 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
491 // if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} //Disabled due to non-static access
492
493 //Loop through each read in the list
494 for(int idx=0; idx<reads.size(); idx++){
495 final Read r1=reads.get(idx);
496 final Read r2=r1.mate;
497
498 //Validate reads in worker threads
499 if(!r1.validated()){r1.validate(true);}
500 if(r2!=null && !r2.validated()){r2.validate(true);}
501
502 //Track the initial length for statistics
503 final int initialLength1=r1.length();
504 final int initialLength2=r1.mateLength();
505
506 //Increment counters
507 readsProcessedT+=r1.pairCount();
508 basesProcessedT+=initialLength1+initialLength2;
509
510 {
511 //Reads are processed in this block.
512 boolean keep=processReadPair(r1, r2);
513 if(!keep){reads.set(idx, null);}
514 else{
515 readsOutT+=r1.pairCount();
516 basesOutT+=initialLength1+initialLength2;
517 }
518 }
519 }
520
521 //Output reads to the output stream
522 if(ros!=null){ros.add(reads, ln.id);}
523
524 //Notify the input stream that the list was used
525 cris.returnList(ln);
526 // if(verbose){outstream.println("Returned a list.");} //Disabled due to non-static access
527
528 //Fetch a new list
529 ln=cris.nextList();
530 reads=(ln!=null ? ln.list : null);
531 }
532
533 //Notify the input stream that the final list was used
534 if(ln!=null){
535 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
536 }
537 }
538
539 /**
540 * Process a read or a read pair.
541 * @param r1 Read 1
542 * @param r2 Read 2 (may be null)
543 * @return True if the reads should be kept, false if they should be discarded.
544 */
545 boolean processReadPair(final Read r1, final Read r2){
546 boolean contained=containsBoth(r1, r2);
547 return contained==include;
548 }
549
550 boolean containsBoth(final Read r1, final Read r2){
551 boolean match1=contains(r1);
552 if(r2==null || !match1) {return match1;}
553 boolean match2=contains(r2);
554 return match1 && match2;
555 }
556
557 boolean contains(final Read r){
558 // System.err.println(new String(r.bases)+": Query");
559 if(r==null || r.bases==null || r.bases.length<1) {return true;}//Empty string is always contained
560 final Code c=new Code(r.bases);
561 // System.err.println(refSet.contains(c)+", "+include);
562 if(refSet.contains(c)){return true;}
563 // System.err.println("b");
564 if(maxSubs<1 && maxLengthDif<1 && mismatchFraction<=0) {return false;}
565 // System.err.println("c");
566 return bruteForce(r.bases);
567 }
568
569 boolean bruteForce(byte[] bases) {
570 if(bases==null) {return false;}
571 boolean match=compare(bases);
572 // System.err.println("g: "+match);
573 if(match || !rcomp) {return match;}
574 AminoAcid.reverseComplementBasesInPlace(bases);
575 match=compare(bases);
576 // System.err.println("h: "+match);
577 AminoAcid.reverseComplementBasesInPlace(bases);
578 return match;
579 }
580
581 boolean compare(byte[] bases) {
582 int minlen=bases.length-maxLengthDif, maxlen=bases.length+maxLengthDif;
583 // System.err.println("d: "+minlen+"-"+maxlen);
584 // System.err.println(new String(bases)+": Query2");
585 boolean match=false;
586 for(byte[] ref : refList) {
587 if(ref.length>=minlen && ref.length<=maxlen) {
588 // System.err.println(new String(ref)+": Ref");
589 final byte[] longer, shorter;
590 if(bases.length<ref.length) {
591 shorter=bases; longer=ref;
592 }else {
593 shorter=ref; longer=bases;
594 }
595 match=compare(longer, shorter);
596 if(match) {break;}
597 }
598 }
599 // System.err.println("compare returned "+match);
600 return match;
601 }
602
603 boolean compare(byte[] a, byte[] b) {
604 final int maxSubs0=Tools.max(maxSubs,
605 (int)Math.round(Tools.min(a.length, b.length)*mismatchFraction));
606 final int aStartMax=a.length-b.length;
607 for(int aStart=0; aStart<=aStartMax; aStart++) {
608 int subs=0;
609 for(int apos=aStart, bpos=0; bpos<b.length && subs<=maxSubs0; apos++, bpos++) {
610 subs+=(a[apos]==b[bpos] ? 0 : 1);
611 }
612 if(subs<=maxSubs0) {return true;}
613 // System.err.println("Subs="+subs+", maxSubs="+maxSubs);
614 }
615 return false;
616 }
617
618 /** Number of reads processed by this thread */
619 protected long readsProcessedT=0;
620 /** Number of bases processed by this thread */
621 protected long basesProcessedT=0;
622
623 /** Number of reads output by this thread */
624 protected long readsOutT=0;
625 /** Number of bases output by this thread */
626 protected long basesOutT=0;
627
628 /** True only if this thread has completed successfully */
629 boolean success=false;
630
631 /** Shared input stream */
632 private final ConcurrentReadInputStream cris;
633 /** Shared output stream */
634 private final ConcurrentReadOutputStream ros;
635 /** Thread ID */
636 final int tid;
637 }
638
639 private class LoadThread extends Thread {
640
641 //Constructor
642 LoadThread(final ConcurrentReadInputStream cris_, final int tid_){
643 cris=cris_;
644 tid=tid_;
645 }
646
647 //Called by start()
648 @Override
649 public void run(){
650 //Do anything necessary prior to processing
651
652 //Process the reads
653 processInner();
654
655 //Do anything necessary after processing
656
657 //Indicate successful exit status
658 success=true;
659 }
660
661 /** Iterate through the reads */
662 void processInner(){
663
664 //Grab the first ListNum of reads
665 ListNum<Read> ln=cris.nextList();
666 //Grab the actual read list from the ListNum
667 ArrayList<Read> reads=(ln!=null ? ln.list : null);
668
669 //Check to ensure pairing is as expected
670 if(reads!=null && !reads.isEmpty()){
671 Read r=reads.get(0);
672 // assert(ffin1.samOrBam() || (r.mate!=null)==cris.paired()); //Disabled due to non-static access
673 }
674
675 LinkedHashSet<Code> codes=new LinkedHashSet<Code>(4000);
676
677 //As long as there is a nonempty read list...
678 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
679 // if(verbose){outstream.println("Fetched "+reads.size()+" reads.");} //Disabled due to non-static access
680
681 //Loop through each read in the list
682 for(int idx=0; idx<reads.size(); idx++){
683 final Read r1=reads.get(idx);
684 final Read r2=r1.mate;
685
686 //Validate reads in worker threads
687 if(!r1.validated()){r1.validate(true);}
688 if(r2!=null && !r2.validated()){r2.validate(true);}
689
690 //Track the initial length for statistics
691 final int initialLength1=r1.length();
692 final int initialLength2=r1.mateLength();
693
694 //Increment counters
695 readsProcessedT+=r1.pairCount();
696 basesProcessedT+=initialLength1+initialLength2;
697
698 if(r1!=null){codes.add(new Code(r1.bases));}
699 if(r2!=null){codes.add(new Code(r2.bases));}
700 }
701
702 if(codes.size()>2000){
703 synchronized(refSet){
704 addToRef(codes);
705 codes.clear();
706 }
707 }
708
709 //Notify the input stream that the list was used
710 cris.returnList(ln);
711 // if(verbose){outstream.println("Returned a list.");} //Disabled due to non-static access
712
713 //Fetch a new list
714 ln=cris.nextList();
715 reads=(ln!=null ? ln.list : null);
716 }
717
718 if(codes.size()>0){
719 synchronized(refSet){
720 addToRef(codes);
721 codes.clear();
722 }
723 }
724
725 //Notify the input stream that the final list was used
726 if(ln!=null){
727 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
728 }
729 }
730
731 /** Number of reads processed by this thread */
732 protected long readsProcessedT=0;
733 /** Number of bases processed by this thread */
734 protected long basesProcessedT=0;
735
736 /** True only if this thread has completed successfully */
737 boolean success=false;
738
739 /** Shared input stream */
740 private final ConcurrentReadInputStream cris;
741 /** Thread ID */
742 final int tid;
743 }
744
745 private class Code {
746
747 Code(byte[] bases_){
748 long fwd=Dedupe.hash(bases_);
749 long rev=Dedupe.hashReversed(bases_);
750 a=(rcomp ? Tools.max(fwd, rev) : fwd);
751 b=(rcomp ? Tools.min(fwd, rev) : rev);
752
753 if(storeBases){
754 if(a==fwd && !toUpperCase){
755 bases=bases_;
756 }else{
757 bases=bases_.clone();
758 if(a!=fwd){AminoAcid.reverseComplementBasesInPlace(bases);}
759 for(int i=0; i<bases.length; i++){
760 bases[i]=(byte) Tools.toUpperCase(bases[i]);
761 }
762 }
763 }else{
764 bases=null;
765 }
766 }
767
768 @Override
769 public boolean equals(Object o){
770 return equals((Code)o);
771 }
772
773 public boolean equals(Code c){
774 if(a!=c.a || b!=c.b){return false;}
775 if(bases==null || c.bases==null){return true;}
776 return Tools.equals(bases, c.bases);
777 }
778
779 // public boolean revEquals(Code c){
780 // if(a!=c.b || b!=c.a){return false;}
781 // if(bases==null || c.bases==null){return true;}
782 // return AminoAcid.equalsReverseComp(bases, c.bases);
783 // }
784
785 @Override
786 public int hashCode(){
787 return (int)(a&0x7FFFFFFF);
788 }
789
790 final long a, b;
791 final byte[] bases;
792
793 }
794
795 /*--------------------------------------------------------------*/
796 /*---------------- Fields ----------------*/
797 /*--------------------------------------------------------------*/
798
799 /** Primary input file path */
800 private String in1=null;
801 /** Secondary input file path */
802 private String in2=null;
803
804 private String qfin1=null;
805 private String qfin2=null;
806
807 /** Primary output file path */
808 private String out1=null;
809 /** Secondary output file path */
810 private String out2=null;
811
812 private String qfout1=null;
813 private String qfout2=null;
814
815 /** Override input file extension */
816 private String extin=null;
817 /** Override output file extension */
818 private String extout=null;
819
820 /** Ref input file path */
821 private String[] ref=null;
822
823 /** Literals */
824 private String[] literal=null;
825
826 private HashSet<Code> refSet;
827 private ArrayList<byte[]> refList;
828
829 private boolean storeBases=true;
830
831 private boolean include=true;
832
833 private boolean rcomp=true;
834
835 private boolean toUpperCase=true;
836
837 /*--------------------------------------------------------------*/
838
839 /** Number of reads processed */
840 protected long readsProcessed=0;
841 /** Number of bases processed */
842 protected long basesProcessed=0;
843
844 protected long readsLoaded=0;
845 protected long basesLoaded=0;
846
847 /** Number of reads output*/
848 protected long readsOut=0;
849 /** Number of bases output*/
850 protected long basesOut=0;
851
852 /** Quit after processing this many input reads; -1 means no limit */
853 private long maxReads=-1;
854
855 private int maxSubs=0;
856 private float mismatchFraction=0f;
857 private int maxLengthDif=0;
858
859 /*--------------------------------------------------------------*/
860 /*---------------- Final Fields ----------------*/
861 /*--------------------------------------------------------------*/
862
863 /** Primary input file */
864 private final FileFormat ffin1;
865 /** Secondary input file */
866 private final FileFormat ffin2;
867
868 /** Primary output file */
869 private final FileFormat ffout1;
870 /** Secondary output file */
871 private final FileFormat ffout2;
872
873 /** Reference Files */
874 private final FileFormat[] ffref;
875
876 /*--------------------------------------------------------------*/
877 /*---------------- Common Fields ----------------*/
878 /*--------------------------------------------------------------*/
879
880 /** Print status messages to this output stream */
881 private PrintStream outstream=System.err;
882 /** Print verbose messages */
883 public static boolean verbose=false;
884 /** True if an error was encountered */
885 public boolean errorState=false;
886 /** Overwrite existing output files */
887 private boolean overwrite=true;
888 /** Append to existing output files */
889 private boolean append=false;
890 /** Reads are output in input order */
891 private boolean ordered=false;
892
893 }