Debian Codegraph @

current/jgi/SplitSam6Way.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.PrintStream;
4
5 import fileIO.ByteFile;
6 import fileIO.ByteStreamWriter;
7 import fileIO.FileFormat;
8 import fileIO.ReadWrite;
9 import shared.Parse;
10 import shared.PreParser;
11 import shared.Shared;
12 import shared.Timer;
13 import shared.Tools;
14 import stream.SamLine;
15
16 /**
17 * @author Brian Bushnell
18 * @date Jun 15, 2017
19 *
20 */
21 public class SplitSam6Way {
22
23 public static void main(String[] args){
24 SplitSam6Way x=new SplitSam6Way(args);
25
26 //Close the print stream if it was redirected
27 Shared.closeStream(x.outstream);
28 }
29
30 private void printOptions(){
31 outstream.println("Syntax:\n");
32 outstream.println("java -ea -Xmx128m -cp <path> jgi.SplitSam6Way <input> <r1plus> <r1minus> <r1unmapped> <r2plus> <r2minus> <r2unmapped>");
33 outstream.println("If you do not want one of the output files, use the word 'null'.\n");
34 }
35
36 public SplitSam6Way(String[] args){
37
38 {//Preparse block for help, config files, and outstream
39 PreParser pp=new PreParser(args, getClass(), false);
40 args=pp.args;
41 outstream=pp.outstream;
42 }
43
44 if(args==null || args.length<7){
45 printOptions();
46 System.exit(1);
47 }
48
49 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
50
51 Timer t=new Timer();
52 long reads=0, bases=0;
53 long r1preads=0, r1mreads=0, r1ureads=0;
54 long r2preads=0, r2mreads=0, r2ureads=0;
55
56 String fin=args[0];
57 String fr1plus=args[1];
58 String fr1minus=args[2];
59 String fr1unmapped=args[3];
60 String fr2plus=args[4];
61 String fr2minus=args[5];
62 String fr2unmapped=args[6];
63
64 long maxReads=Long.MAX_VALUE;
65 if(args.length>7){
66 maxReads=Parse.parseKMG(args[7]);
67 }
68
69 // ByteFile.FORCE_MODE_BF1=true;
70 ByteFile tf=ByteFile.makeByteFile(fin, true);
71 ByteStreamWriter r1plus=("null".equalsIgnoreCase(fr1plus) ? null : new ByteStreamWriter(fr1plus, true, false, true, FileFormat.SAM));
72 ByteStreamWriter r1minus=("null".equalsIgnoreCase(fr1minus) ? null : new ByteStreamWriter(fr1minus, true, false, true, FileFormat.SAM));
73 ByteStreamWriter r1unmapped=("null".equalsIgnoreCase(fr1unmapped) ? null : new ByteStreamWriter(fr1unmapped, true, false, true, FileFormat.SAM));
74 ByteStreamWriter r2plus=("null".equalsIgnoreCase(fr2plus) ? null : new ByteStreamWriter(fr2plus, true, false, true, FileFormat.SAM));
75 ByteStreamWriter r2minus=("null".equalsIgnoreCase(fr2minus) ? null : new ByteStreamWriter(fr2minus, true, false, true, FileFormat.SAM));
76 ByteStreamWriter r2unmapped=("null".equalsIgnoreCase(fr2unmapped) ? null : new ByteStreamWriter(fr2unmapped, true, false, true, FileFormat.SAM));
77
78 if(r1plus!=null){r1plus.start();}
79 if(r1minus!=null){r1minus.start();}
80 if(r1unmapped!=null){r1unmapped.start();}
81 if(r2plus!=null){r2plus.start();}
82 if(r2minus!=null){r2minus.start();}
83 if(r2unmapped!=null){r2unmapped.start();}
84
85 for(byte[] line=tf.nextLine(); line!=null; line=tf.nextLine()){
86 if(line[0]=='@'){
87 if(r1plus!=null){r1plus.println(line);}
88 if(r1minus!=null){r1minus.println(line);}
89 if(r1unmapped!=null){r1unmapped.println(line);}
90 if(r2plus!=null){r2plus.println(line);}
91 if(r2minus!=null){r2minus.println(line);}
92 if(r2minus!=null){r2minus.println(line);}
93 }else{
94 if(reads>=maxReads){break;}
95
96 SamLine sl=new SamLine(line);
97 reads++;
98 bases+=sl.seq.length;
99
100 if(sl.pairnum()==0){
101 if(sl.mapped()){
102 if(sl.strand()==Shared.PLUS){
103 if(r1plus!=null){r1plus.println(line);}
104 r1preads++;
105 }else{
106 if(r1minus!=null){r1minus.println(line);}
107 r1mreads++;
108 }
109 }else{
110 if(r1unmapped!=null){r1unmapped.println(line);}
111 r1ureads++;
112 }
113 }else{
114 if(sl.mapped()){
115 if(sl.strand()==Shared.PLUS){
116 if(r2plus!=null){r2plus.println(line);}
117 r2preads++;
118 }else{
119 if(r2minus!=null){r2minus.println(line);}
120 r2mreads++;
121 }
122 }else{
123 if(r2unmapped!=null){r2unmapped.println(line);}
124 r2ureads++;
125 }
126 }
127 }
128 }
129
130 tf.close();
131
132 if(r1plus!=null){r1plus.poisonAndWait();}
133 if(r1minus!=null){r1minus.poisonAndWait();}
134 if(r1unmapped!=null){r1unmapped.poisonAndWait();}
135 if(r2plus!=null){r2plus.poisonAndWait();}
136 if(r2minus!=null){r2minus.poisonAndWait();}
137 if(r2unmapped!=null){r2unmapped.poisonAndWait();}
138
139 t.stop();
140 outstream.println(Tools.timeReadsBasesProcessed(t, reads, bases, 8));
141 outstream.println("R1 Plus Reads: "+r1preads);
142 outstream.println("R1 Minus Reads: "+r1mreads);
143 outstream.println("R1 Unmapped Reads: "+r1ureads);
144 outstream.println("R1 Plus Reads: "+r2preads);
145 outstream.println("R1 Minus Reads: "+r2mreads);
146 outstream.println("R1 Unmapped Reads: "+r2ureads);
147 }
148
149 private PrintStream outstream=System.err;
150
151 }