Debian Codegraph @

current/jgi/SplitPairsAndSingles.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.LinkedHashMap;
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 shared.TrimRead;
18 import stream.ConcurrentReadInputStream;
19 import stream.ConcurrentReadOutputStream;
20 import stream.DualCris;
21 import stream.FASTQ;
22 import stream.FastaReadInputStream;
23 import stream.Read;
24 import stream.SamLine;
25 import structures.ListNum;
26 import tracker.ReadStats;
27
28 /**
29 * @author Brian Bushnell
30 * @date Sep 4, 2013
31 *
32 */
33 public final class SplitPairsAndSingles {
34
35 public static void main(String[] args){
36 SplitPairsAndSingles x=new SplitPairsAndSingles(args);
37 x.process();
38
39 //Close the print stream if it was redirected
40 Shared.closeStream(outstream);
41 }
42
43 public SplitPairsAndSingles(String[] args){
44
45 {//Preparse block for help, config files, and outstream
46 PreParser pp=new PreParser(args, getClass(), false);
47 args=pp.args;
48 outstream=pp.outstream;
49 }
50
51 ReadWrite.ZIPLEVEL=2;
52 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
53 ReadWrite.setZipThreads(Shared.threads());
54
55 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
56 ByteFile.FORCE_MODE_BF2=true;
57 }
58
59 Parser parser=new Parser();
60 parser.trimq=trimq;
61 parser.minReadLength=minReadLength;
62 boolean setOut=false, setOuts=false;
63 boolean fixInterleaving_=false, repair_=false, allowIdenticalPairNames_=false;
64
65 for(int i=0; i<args.length; i++){
66
67 final String arg=args[i];
68 String[] split=arg.split("=");
69 String a=split[0].toLowerCase();
70 String b=split.length>1 ? split[1] : null;
71
72 if(Parser.parseCommonStatic(arg, a, b)){
73 //do nothing
74 }else if(Parser.parseZip(arg, a, b)){
75 //do nothing
76 }else if(Parser.parseQuality(arg, a, b)){
77 //do nothing
78 }else if(Parser.parseFasta(arg, a, b)){
79 //do nothing
80 }else if(parser.parseInterleaved(arg, a, b)){
81 //do nothing
82 }else if(parser.parseTrim(arg, a, b)){
83 //do nothing
84 }else if(a.equals("in") || a.equals("in1")){
85 in1=b;
86 }else if(a.equals("in2")){
87 in2=b;
88 }else if(a.equals("out") || a.equals("out1") || a.equals("outp") || a.equals("outp1") || a.equals("outpair") || a.equals("outpair1")){
89 out1=b;
90 setOut=true;
91 }else if(a.equals("out2") || a.equals("outp2") || a.equals("outpair2")){
92 out2=b;
93 }else if(a.equals("outs") || a.equals("outsingle") || a.equals("outb") || a.equals("outbad")){
94 outsingle=b;
95 setOut=true;
96 }else if(a.equals("append") || a.equals("app")){
97 append=ReadStats.append=Parse.parseBoolean(b);
98 }else if(a.equals("overwrite") || a.equals("ow")){
99 overwrite=Parse.parseBoolean(b);
100 }else if(a.equals("showspeed") || a.equals("ss")){
101 showSpeed=Parse.parseBoolean(b);
102 }else if(a.equals("verbose")){
103 verbose=Parse.parseBoolean(b);
104 }else if(a.equals("addslash")){
105 addslash=Parse.parseBoolean(b);
106 }else if(a.equals("addcolon")){
107 addcolon=Parse.parseBoolean(b);
108 }else if(a.equals("reads") || a.startsWith("maxreads")){
109 maxReads=Parse.parseKMG(b);
110 }else if(a.equals("fixinterleaving") || a.equals("fi") || a.equals("fint") || a.equals("fixint")){
111 fixInterleaving_=Parse.parseBoolean(b);
112 if(fixInterleaving_){repair_=false;}
113 }else if(a.equals("allowidenticalnames") || a.equals("ain")){
114 allowIdenticalPairNames_=Parse.parseBoolean(b);
115 }else if(a.equals("repair") || a.equals("rp")){
116 repair_=Parse.parseBoolean(b);
117 if(repair_){fixInterleaving_=false;}
118 }else if(i==0 && in1==null && arg.indexOf('=')<0 && arg.lastIndexOf('.')>0){
119 in1=args[i];
120 }else if(i==1 && out1==null && arg.indexOf('=')<0 && arg.lastIndexOf('.')>0){
121 out1=args[i];
122 setOut=true;
123 }else if(i==2 && outsingle==null && arg.indexOf('=')<0 && arg.lastIndexOf('.')>0){
124 outsingle=args[i];
125 setOuts=true;
126 }else{
127 throw new RuntimeException("Unknown parameter "+args[i]);
128 }
129 }
130
131 {//Process parser fields
132 Parser.processQuality();
133
134 qtrimLeft=parser.qtrimLeft;
135 qtrimRight=parser.qtrimRight;
136 trimq=parser.trimq;
137 trimE=parser.trimE();
138 minReadLength=parser.minReadLength;
139 }
140
141 allowIdenticalPairNames=allowIdenticalPairNames_;
142 fixInterleaving=fixInterleaving_;
143 repair=repair_;
144 assert(!repair || ! fixInterleaving) : "ERROR: Choose 'fixInterleaving' or 'repair', but not both.";
145
146 assert(FastaReadInputStream.settingsOK());
147
148 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
149
150 if(in1!=null && in1.contains("#") && !new File(in1).exists()){
151 int pound=in1.lastIndexOf('#');
152 String a=in1.substring(0, pound);
153 String b=in1.substring(pound+1);
154 in1=a+1+b;
155 in2=a+2+b;
156 }
157 if(in2!=null && (in2.contains("=") || in2.equalsIgnoreCase("null"))){in2=null;}
158
159 if(fixInterleaving){
160 if(in2!=null){
161 System.err.println("ERROR: 'FixInterleaving' mode only works with a single interleaved input file, not paired input files.");
162 System.err.println("Aborting.");
163 System.exit(1);
164 }
165 parser.setInterleaved=true;
166 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
167 outstream.println("Paired input disabled; running in FixInterleaving mode");
168 }
169
170 if(repair){
171 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
172 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
173 }else{
174 if(!parser.setInterleaved && in2==null){
175 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=true;
176 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
177 }
178 if(in2!=null){
179 if(FASTQ.FORCE_INTERLEAVED){System.err.println("Reset INTERLEAVED to false because paired input files were specified.");}
180 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
181 }
182 }
183
184 if(out1!=null && out1.contains("#")){
185 int pound=out1.lastIndexOf('#');
186 String a=out1.substring(0, pound);
187 String b=out1.substring(pound+1);
188 out1=a+1+b;
189 out2=a+2+b;
190 }
191
192 if(!setOut){
193 System.err.println("No output stream specified. To write to stdout, please specify 'out=stdout.fq' or similar.");
194 // out1="stdout.fq";
195 outstream=System.err;
196 out2=null;
197 }else if("stdout".equalsIgnoreCase(out1) || "standarddout".equalsIgnoreCase(out1)){
198 out1="stdout.fq";
199 outstream=System.err;
200 out2=null;
201 }
202 if(out1!=null && !Tools.canWrite(out1, overwrite)){throw new RuntimeException("Output file "+out1+" already exists, and overwrite="+overwrite);}
203
204 assert(!in1.equalsIgnoreCase(out1));
205 assert(!in1.equalsIgnoreCase(outsingle));
206 assert(!in1.equalsIgnoreCase(in2));
207 assert(out1==null || !out1.equalsIgnoreCase(out2)) : "out2 may not be defined without out1, and out1 may not equal out2.";
208 assert(out1==null || !out1.equalsIgnoreCase(outsingle));
209
210 pairMap=(repair ? new LinkedHashMap<String, Read>() : null);
211
212 //Close the print stream if it was redirected
213 Shared.closeStream(outstream);
214 }
215
216 public void process(){
217
218 Timer t=new Timer();
219
220 process2();
221
222 t.stop();
223
224 outstream.println("\nInput: \t"+readsIn+" reads \t\t"+basesIn+" bases.");
225
226 if(qtrimLeft || qtrimRight){
227 outstream.println("Trimmed: \t"+readsTrimmed+" reads ("+Tools.format("%.2f",readsTrimmed*100.0/readsIn)+"%) \t"+
228 basesTrimmed+" bases ("+Tools.format("%.2f",basesTrimmed*100.0/basesIn)+"%)");
229 }
230 outstream.println("Result: \t"+readsOut+" reads ("+Tools.format("%.2f",readsOut*100.0/readsIn)+"%) \t"+
231 basesOut+" bases ("+Tools.format("%.2f",basesOut*100.0/basesIn)+"%)");
232 outstream.println("Pairs: \t"+pairsOut+" reads ("+Tools.format("%.2f",pairsOut*100.0/readsIn)+"%) \t"+
233 pairBasesOut+" bases ("+Tools.format("%.2f",pairBasesOut*100.0/basesIn)+"%)");
234 outstream.println("Singletons: \t"+singlesOut+" reads ("+Tools.format("%.2f",singlesOut*100.0/readsIn)+"%) \t"+
235 singleBasesOut+" bases ("+Tools.format("%.2f",singleBasesOut*100.0/basesIn)+"%)");
236
237 if(showSpeed){
238 outstream.println();
239 outstream.println(Tools.timeReadsBasesProcessed(t, readsIn, basesIn, 8));
240 }
241
242 if(errorState){
243 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
244 }
245 }
246
247 private void process2(){
248 final ConcurrentReadInputStream cris;
249 if(in2!=null && repair){
250 FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
251 FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true);
252 cris=DualCris.getReadInputStream(maxReads, true, ff1, ff2, null, null);
253 }else{
254 FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
255 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, null, null, null);
256 }
257 if(verbose){System.err.println("Started cris");}
258 cris.start();
259 boolean paired=cris.paired();
260 if(verbose){System.err.println("Paired: "+paired);}
261
262 final ConcurrentReadOutputStream ros, rosb;
263 final int buff=4;
264 if(out1!=null){
265 FileFormat ff1=FileFormat.testOutput(out1, FileFormat.FASTQ, null, true, overwrite, append, false);
266 FileFormat ff2=FileFormat.testOutput(out2, FileFormat.FASTQ, null, true, overwrite, append, false);
267 ros=ConcurrentReadOutputStream.getStream(ff1, ff2, buff, null, true);
268 ros.start();
269 }else{ros=null;}
270 if(outsingle!=null){
271 FileFormat ff1=FileFormat.testOutput(outsingle, FileFormat.FASTQ, null, true, overwrite, append, false);
272 rosb=ConcurrentReadOutputStream.getStream(ff1, null, buff, null, true);
273 rosb.start();
274 }else{rosb=null;}
275 if(ros!=null || rosb!=null){
276 outstream.println("Started output stream.");
277 }
278
279 // assert(false) : out1+", "+out2+", "+outsingle;
280 if(fixInterleaving){
281 process3_fixInterleaving(cris, ros, rosb);
282 }else if(repair){
283 if(cris.getClass()==DualCris.class){
284 process3_repair((DualCris)cris, ros, rosb);
285 }else{
286 process3_repair(cris, ros, rosb);
287 }
288 }else{
289 process3(cris, ros, rosb);
290 }
291
292
293 ReadWrite.closeStreams(cris, ros, rosb);
294 }
295 //
296 // private void process3_old(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros, final ConcurrentReadOutputStream rosb){
297 //
298 // ListNum<Read> ln=cris.nextList();
299 // ArrayList<Read> reads0=(ln!=null ? ln.list : null);
300 // ArrayList<Read> single=(rosb==null ? null : new ArrayList<Read>(Shared.bufferLen()));
301 //
302 // while(reads0!=null && reads0.size()>0){
303 // ArrayList<Read> reads=(ArrayList<Read>) reads0.clone();
304 // int removed=0;
305 // for(int i=0; i<reads.size(); i++){
306 // Read r1=reads.get(i);
307 // Read r2=r1.mate;
308 //
309 // readsIn++;
310 // basesIn+=r1.length();
311 // if(r2!=null){
312 // readsIn++;
313 // basesIn+=r2.length();
314 // }
315 //
316 // {
317 // if(trimLeft || trimRight){
318 // if(r1!=null){
319 // int x=TrimRead.trimFast(r1, trimLeft, trimRight, trimq, 1);
320 // basesTrimmed+=x;
321 // readsTrimmed+=(x>0 ? 1 : 0);
322 // }
323 // if(r2!=null){
324 // int x=TrimRead.trimFast(r2, trimLeft, trimRight, trimq, 1);
325 // basesTrimmed+=x;
326 // readsTrimmed+=(x>0 ? 1 : 0);
327 // }
328 // }
329 //
330 // final int rlen1=(r1==null ? -1 : r1.length());
331 // final int rlen2=(r2==null ? -1 : r2.length());
332 //
333 // if(verbose){System.err.println("rlen1="+rlen1+", rlen2="+rlen2);}
334 //
335 // if(rlen1<minReadLength || rlen2<minReadLength){
336 // reads.set(i, null);
337 // removed++;
338 // r1.mate=null;
339 // if(r2!=null){
340 // r2.mate=null;
341 // }
342 //
343 // if(rlen1>=minReadLength){
344 // single.add(r1);
345 // singlesOut++;
346 // singleBasesOut+=rlen1;
347 // }
348 // if(rlen2>=minReadLength){
349 // single.add(r2);
350 // singlesOut++;
351 // singleBasesOut+=rlen2;
352 // }
353 // }else{
354 // if(r1!=null){
355 // pairsOut++;
356 // pairBasesOut+=rlen2;
357 // }
358 // if(r2!=null){
359 // pairsOut++;
360 // pairBasesOut+=rlen2;
361 // }
362 // }
363 // }
364 // }
365 //
366 // if(rosb!=null){
367 // if(verbose){System.err.println("Adding "+single.size()+" to single out.");}
368 // rosb.add(new ArrayList<Read>(single), ln.id);
369 // single.clear();
370 // }
371 //
372 // if(ros!=null){
373 // if(removed>0){Tools.condenseStrict(reads);}
374 // ArrayList<Read> x=new ArrayList<Read>(reads.size());
375 // x.addAll(reads);
376 // if(verbose){System.err.println("Adding "+x.size()+" to pair out.");}
377 // ros.add(x, ln.id);
378 // }
379 //
380 // cris.returnList(ln);
381 // ln=cris.nextList();
382 // reads0=(ln!=null ? ln.list : null);
383 // }
384 // cris.returnList(ln);
385 //
386 // readsOut+=singlesOut+pairsOut;
387 // basesOut+=singleBasesOut+pairBasesOut;
388 // }
389
390 private void process3(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros, final ConcurrentReadOutputStream rosb){
391
392 ListNum<Read> ln=cris.nextList();
393 ArrayList<Read> reads=ln.list;
394
395 final ArrayList<Read> pairs=(ros==null ? null : new ArrayList<Read>(Shared.bufferLen()));
396 final ArrayList<Read> singles=(rosb==null ? null : new ArrayList<Read>(Shared.bufferLen()));
397
398 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
399 for(int i=0; i<reads.size(); i++){
400 Read r1=reads.get(i);
401 Read r2=r1.mate;
402 processPair(r1, r2, pairs, singles);
403 }
404
405 cris.returnList(ln);
406 ln=cris.nextList();
407 reads=(ln!=null ? ln.list : null);
408
409 if(rosb!=null){
410 if(verbose){System.err.println("Adding "+singles.size()+" to single out.");}
411 rosb.add(new ArrayList<Read>(singles), ln.id);
412 singles.clear();
413 }
414
415 if(ros!=null){
416 if(verbose){System.err.println("Adding "+pairs.size()+" to pair out.");}
417 ros.add(new ArrayList<Read>(pairs), ln.id);
418 pairs.clear();
419 }
420 }
421 cris.returnList(ln);
422
423 readsOut+=singlesOut+pairsOut;
424 basesOut+=singleBasesOut+pairBasesOut;
425 }
426
427 private void process3_fixInterleaving(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros, final ConcurrentReadOutputStream rosb){
428
429 ListNum<Read> ln=cris.nextList();
430 ArrayList<Read> reads=ln.list;
431
432 final ArrayList<Read> pairs=(ros==null ? null : new ArrayList<Read>(Shared.bufferLen()));
433 final ArrayList<Read> singles=(rosb==null ? null : new ArrayList<Read>(Shared.bufferLen()));
434
435 Read current=null, prev=null;
436
437 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
438 for(int i=0; i<reads.size(); i++){
439
440 current=reads.get(i);
441 // if(verbose){System.err.println("Fetched "+current);}
442
443 if(prev!=null){
444 boolean b=FASTQ.testPairNames(prev, current, allowIdenticalPairNames);
445 if(b){
446 if(verbose){System.err.println("A");}
447 processPair(prev, current, pairs, singles);
448 prev=null;
449 current=null;
450 }else{
451 if(verbose){System.err.println("B");}
452 processPair(prev, null, null, singles);
453 prev=null;
454 }
455 }
456 prev=current;
457 current=null;
458 }
459
460 // if(verbose){System.err.println("X\n"+current+"\n"+prev+"\n");}
461
462 cris.returnList(ln);
463 ln=cris.nextList();
464 reads=(ln!=null ? ln.list : null);
465
466 if((ln==null || reads==null || reads.isEmpty()) && prev!=null){ //Process last read
467 boolean b=FASTQ.testPairNames(prev, current, allowIdenticalPairNames);
468 if(b){
469 if(verbose){System.err.println("C");}
470 processPair(prev, current, pairs, singles);
471 prev=null;
472 current=null;
473 }else{
474 if(verbose){System.err.println("D");}
475 processPair(prev, null, null, singles);
476 prev=null;
477 }
478 }
479
480 if(rosb!=null){
481 if(verbose){System.err.println("Adding "+singles.size()+" to single out.");}
482 rosb.add(new ArrayList<Read>(singles), ln.id);
483 singles.clear();
484 }
485
486 if(ros!=null){
487 if(verbose){System.err.println("Adding "+pairs.size()+" to pair out.");}
488 ros.add(new ArrayList<Read>(pairs), ln.id);
489 pairs.clear();
490 }
491 }
492 cris.returnList(ln);
493
494 readsOut+=singlesOut+pairsOut;
495 basesOut+=singleBasesOut+pairBasesOut;
496 }
497
498 private void process3_repair(final DualCris cris, final ConcurrentReadOutputStream ros, final ConcurrentReadOutputStream rosb){
499
500 ListNum<Read> ln=cris.nextList();
501 ArrayList<Read> reads=ln.list;
502
503 final ArrayList<Read> pairs=(ros==null ? null : new ArrayList<Read>(Shared.bufferLen()));
504
505 boolean foundR1=false, foundR2=false;
506 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
507 for(Read r1 : reads){
508 Read r2=r1.mate;
509
510 if(r1.pairnum()==0){foundR1=true;}
511 else{foundR2=true;}
512 if(r2!=null){
513 if(r2.pairnum()==0){foundR1=true;}
514 else{foundR2=true;}
515 }
516
517 {
518 Read pair=repair(r1);
519 if(pair!=null && pairs!=null){pairs.add(pair);}
520 }
521 {
522 Read pair=repair(r2);
523 if(pair!=null && pairs!=null){pairs.add(pair);}
524 }
525 }
526
527 // if(verbose){System.err.println("X\n"+current+"\n"+prev+"\n");}
528
529 cris.returnList(ln.id, foundR1, foundR2);
530 foundR1=foundR2=false;
531 ln=cris.nextList();
532 reads=(ln!=null ? ln.list : null);
533
534 if(ros!=null){
535 if(verbose){System.err.println("Adding "+pairs.size()+" to pair out.");}
536 ros.add(new ArrayList<Read>(pairs), ln.id);
537 pairs.clear();
538 }
539 }
540 cris.returnList(ln.id, foundR1, foundR2);
541
542 if(!pairMap.isEmpty()){
543 final ArrayList<Read> singles=new ArrayList<Read>(pairMap.size());
544 for(String key : pairMap.keySet()){
545 Read r=pairMap.get(key);
546 singles.add(r);
547 singlesOut++;
548 singleBasesOut+=r.length();
549 }
550 pairMap.clear();
551 if(verbose){System.err.println("Adding "+singles.size()+" to single out.");}
552 if(rosb!=null){rosb.add(singles, 0);}
553 }
554
555 readsOut+=singlesOut+pairsOut;
556 basesOut+=singleBasesOut+pairBasesOut;
557 }
558
559 private void process3_repair(final ConcurrentReadInputStream cris, final ConcurrentReadOutputStream ros, final ConcurrentReadOutputStream rosb){
560
561 ListNum<Read> ln=cris.nextList();
562 ArrayList<Read> reads=ln.list;
563
564 final ArrayList<Read> pairs=(ros==null ? null : new ArrayList<Read>(Shared.bufferLen()));
565
566 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
567 for(Read r1 : reads){
568 Read r2=r1.mate;
569
570 {
571 Read pair=repair(r1);
572 if(pair!=null && pairs!=null){pairs.add(pair);}
573 }
574 {
575 Read pair=repair(r2);
576 if(pair!=null && pairs!=null){pairs.add(pair);}
577 }
578 }
579
580 // if(verbose){System.err.println("X\n"+current+"\n"+prev+"\n");}
581
582 cris.returnList(ln);
583 ln=cris.nextList();
584 reads=(ln!=null ? ln.list : null);
585
586 if(ros!=null && pairs!=null){//pairs!=null is implied
587 if(verbose){System.err.println("Adding "+pairs.size()+" to pair out.");}
588 ros.add(new ArrayList<Read>(pairs), ln.id);
589 pairs.clear();
590 }
591 }
592 cris.returnList(ln);
593
594 if(!pairMap.isEmpty()){
595 final ArrayList<Read> singles=new ArrayList<Read>(pairMap.size());
596 for(String key : pairMap.keySet()){
597 Read r=pairMap.get(key);
598 singles.add(r);
599 singlesOut++;
600 singleBasesOut+=r.length();
601 }
602 pairMap.clear();
603 if(verbose){System.err.println("Adding "+singles.size()+" to single out.");}
604 if(rosb!=null){rosb.add(singles, 0);}
605 }
606
607 readsOut+=singlesOut+pairsOut;
608 basesOut+=singleBasesOut+pairBasesOut;
609 }
610
611
612 private int processPair(Read r1, Read r2, ArrayList<Read> pairs, ArrayList<Read> singles){
613 int removed=0;
614 readsIn++;
615 basesIn+=r1.length();
616 if(r2!=null){
617 readsIn++;
618 basesIn+=r2.length();
619 }
620
621 if(qtrimLeft || qtrimRight){
622 if(r1!=null){
623 int x=TrimRead.trimFast(r1, qtrimLeft, qtrimRight, trimq, trimE, 1);
624 basesTrimmed+=x;
625 readsTrimmed+=(x>0 ? 1 : 0);
626 if(addcolon){
627 String colon=colon1;
628 if(!r1.id.contains(colon)){r1.id+=colon;}
629 }
630 }
631 if(r2!=null){
632 int x=TrimRead.trimFast(r2, qtrimLeft, qtrimRight, trimq, trimE, 1);
633 basesTrimmed+=x;
634 readsTrimmed+=(x>0 ? 1 : 0);
635 if(addcolon){
636 String colon=colon2;
637 if(!r2.id.contains(colon)){r2.id+=colon;}
638 }
639 }
640 }
641 final int rlen1=(r1==null ? -1 : r1.length());
642 final int rlen2=(r2==null ? -1 : r2.length());
643 if(verbose){System.err.println("rlen="+rlen1+", rlen2="+rlen2);}
644
645 if(rlen1>=minReadLength && rlen2>=minReadLength){
646 if(verbose){System.err.println("Sending to pair out:\t"+r1.id+"\t"+r2.id);}
647 r1.mate=r2;
648 r2.mate=r1;
649 r1.setPairnum(0);
650 r2.setPairnum(1);
651 if(pairs!=null){pairs.add(r1);}
652 pairsOut+=2;
653 pairBasesOut+=(rlen1+rlen2);
654 }else if(rlen1>=minReadLength){
655 if(verbose){System.err.println("Sending r1 to single out:\t"+r1.id+"\t"+(r2==null ? "*" : r2.id));}
656 r1.mate=null;
657 r1.setPairnum(0);
658 if(singles!=null){singles.add(r1);}
659 singlesOut++;
660 singleBasesOut+=rlen1;
661 if(r2!=null){removed++;}
662 }else if(rlen2>=minReadLength){
663 if(verbose){System.err.println("Sending r2 to single out:\t"+(r1==null ? "*" : r1.id)+"\t"+r2.id);}
664 r2.mate=null;
665 r2.setPairnum(0);
666 if(singles!=null){singles.add(r2);}
667 singlesOut++;
668 singleBasesOut+=rlen2;
669 if(r1!=null){removed++;}
670 }else{
671 if(verbose){System.err.println("Removed both reads:\t"+(r1==null ? "*" : r1.id)+"\t"+(r2==null ? "*" : r2.id));}
672 if(r1!=null){removed++;}
673 if(r2!=null){removed++;}
674 }
675 return removed;
676 }
677
678
679 private Read repair(Read r){
680 if(r==null){return null;}
681 r.mate=null;
682
683 readsIn++;
684 basesIn+=r.length();
685 final String id=r.id;
686
687 final SamLine sl=r.samline;
688 if(sl!=null && (!sl.primary() || sl.supplementary())){return null;}
689
690 assert(id!=null) : "Read number "+r.numericID+" has no name and thus cannot be re-paired. To ignore this, run with the -da flag.";
691 if(id==null){return null;}
692 final int slash=id.indexOf('/');
693 String[] split=id.split("\\s+");
694
695 if(split.length==1 && slash>0){
696 split=new String[] {id.substring(0, slash), id.substring(slash)};
697 }
698
699 assert(split.length>0);
700 String prefix=split[0];
701 String suffix=(split.length==1 ? null : split[split.length-1]);
702
703 if(sl!=null){
704 r.setPairnum(sl.pairnum());
705 }else if(suffix!=null){
706 if(suffix.startsWith("/1") || suffix.startsWith("1:")){
707 r.setPairnum(0);
708 }else if(suffix.startsWith("/2") || suffix.startsWith("2:")){
709 r.setPairnum(1);
710 }else if(id.contains("/1") || id.contains("/2")){
711 split=id.split("/");
712 prefix=split[0];
713 suffix=(split.length==1 ? null : split[split.length-1]);
714
715 if(suffix!=null){
716 if(suffix.startsWith("1")){
717 r.setPairnum(0);
718 }else if(suffix.startsWith("2")){
719 r.setPairnum(1);
720 }
721 }else{
722 //pairnum cannot be determined
723 }
724 }else{
725 //pairnum cannot be determined
726 }
727 }else{
728 //pairnum cannot be determined
729 }
730
731 if(addcolon){
732 String colon=(r.pairnum()==0 ? colon1 : colon2);
733 if(!r.id.contains(colon)){r.id+=colon;}
734 }
735
736 Read old=pairMap.remove(prefix);
737
738 // System.out.println("Processing:\n"+r+"\n"+old+"\n"+readsIn+", "+readsOut+", "+pairsOut);
739
740 if(old==null){
741 pairMap.put(prefix, r);
742 return null;
743 }else{
744 r.mate=old;
745 old.mate=r;
746
747 int len=r.length()+old.length();
748 pairsOut+=2;
749 pairBasesOut+=len;
750
751 if(old.pairnum()==1){
752 r.setPairnum(0);
753 return r;
754 }else{
755 old.setPairnum(0);
756 r.setPairnum(1);
757 return old;
758 }
759 }
760 }
761
762
763 private String in1=null, in2=null;
764 private String out1=null, out2=null;
765 private String outsingle=null;
766 private long maxReads=-1;
767 public boolean errorState=false;
768
769 long readsIn=0;
770 long basesIn=0;
771 long readsOut=0;
772 long basesOut=0;
773 long pairsOut=0;
774 long pairBasesOut=0;
775 long singlesOut=0;
776 long singleBasesOut=0;
777 long readsTrimmed=0;
778 long basesTrimmed=0;
779
780 private final LinkedHashMap<String, Read> pairMap;
781
782 private float trimq=6;
783 /** Error rate for trimming (derived from trimq) */
784 private final float trimE;
785 private int minReadLength=20;
786 private final boolean qtrimLeft, qtrimRight;
787
788 private final boolean fixInterleaving;
789 private final boolean allowIdenticalPairNames;
790 private final boolean repair;
791
792 private boolean addslash=false;
793 private boolean addcolon=false;
794
795 private static PrintStream outstream=System.err;
796 /** Permission to overwrite existing files */
797 public static boolean overwrite=true;
798 /** Permission to append to existing files */
799 public static boolean append=false;
800 public static boolean showSpeed=true;
801 public static boolean verbose=false;
802
803 private static final String slash1=" /1";
804 private static final String slash2=" /2";
805 private static final String colon1=" 1:";
806 private static final String colon2=" 2:";
807
808 }