Debian Codegraph @

current/jgi/GradeMergedReads.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6
7 import fileIO.ByteFile;
8 import fileIO.ByteFile1;
9 import fileIO.ByteFile2;
10 import fileIO.FileFormat;
11 import fileIO.ReadWrite;
12 import shared.Parse;
13 import shared.Parser;
14 import shared.PreParser;
15 import shared.Shared;
16 import shared.Timer;
17 import shared.Tools;
18 import stream.ConcurrentGenericReadInputStream;
19 import stream.ConcurrentReadInputStream;
20 import stream.CustomHeader;
21 import stream.FASTQ;
22 import stream.FastaReadInputStream;
23 import stream.Read;
24 import structures.ListNum;
25
26 /**
27 * @author Brian Bushnell
28 * @date May 20, 2014
29 *
30 */
31 public class GradeMergedReads {
32
33
34 public static void main(String[] args){
35 Timer t=new Timer();
36 GradeMergedReads x=new GradeMergedReads(args);
37 x.process(t);
38
39 //Close the print stream if it was redirected
40 Shared.closeStream(x.outstream);
41 }
42
43 public GradeMergedReads(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 Shared.capBuffers(4);
52 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
53 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
54 FASTQ.DETECT_QUALITY=false;
55
56 Parser parser=new Parser();
57 for(int i=0; i<args.length; i++){
58 String arg=args[i];
59 String[] split=arg.split("=");
60 String a=split[0].toLowerCase();
61 String b=split.length>1 ? split[1] : null;
62
63 if(parser.parse(arg, a, b)){
64 //do nothing
65 }else if(a.equals("raw") || a.equals("raw1")){
66 assert(b!=null) : "Bad parameter: "+arg;
67 raw1=b;
68 if(b.indexOf("#")>=0){
69 raw1=b.replace('#', '1');
70 raw2=b.replace('#', '2');
71 }
72 }else if(a.equals("raw2")){
73 raw2=b;
74 }else if(a.equals("verbose")){
75 verbose=Parse.parseBoolean(b);
76 ByteFile1.verbose=verbose;
77 ByteFile2.verbose=verbose;
78 stream.FastaReadInputStream.verbose=verbose;
79 ConcurrentGenericReadInputStream.verbose=verbose;
80 stream.FastqReadInputStream.verbose=verbose;
81 ReadWrite.verbose=verbose;
82 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
83 parser.in1=arg;
84 }else{
85 System.err.println("Unknown parameter "+i+": "+args[i]);
86 assert(false) : "Unknown parameter "+i+": "+args[i];
87 // +"\n"+arg+", "+parser.in1+", "+arg.contains("=")+", "+(arg.toLowerCase().startsWith("stdin")+", "+new File(arg).exists()+", "+new File(arg).getAbsolutePath());
88 // throw new RuntimeException("Unknown parameter "+args[i]);
89 }
90 }
91
92 {//Process parser fields
93 Parser.processQuality();
94
95 maxReads=parser.maxReads;
96 in=parser.in1;
97 extin=parser.extin;
98 }
99
100 assert(FastaReadInputStream.settingsOK());
101
102 if(in==null){throw new RuntimeException("Error - at least one input file is required.");}
103 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
104 ByteFile.FORCE_MODE_BF2=true;
105 }
106
107 FASTQ.PARSE_CUSTOM=false;
108
109 ffin=FileFormat.testInput(in, FileFormat.FASTQ, extin, true, true);
110 }
111
112 void process(Timer t){
113
114 long mergeable=0, total=0;
115 if(raw1!=null){
116 FileFormat ffraw1=FileFormat.testInput(raw1, FileFormat.FASTQ, extin, true, true);
117 FileFormat ffraw2=FileFormat.testInput(raw2, FileFormat.FASTQ, extin, true, true);
118 ConcurrentReadInputStream cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffraw1, ffraw2, null, null);
119 cris.start();
120
121 {
122
123 ListNum<Read> ln=cris.nextList();
124 ArrayList<Read> reads=(ln!=null ? ln.list : null);
125
126 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
127 for(int idx=0; idx<reads.size(); idx++){
128 final Read r1=reads.get(idx);
129 String s=r1.id;
130 total++;
131 final int insert=parseInsert(r1.id);
132 if(insert>0 && insert<r1.pairLength()){
133 mergeable++;
134 }
135 }
136
137 cris.returnList(ln);
138 ln=cris.nextList();
139 reads=(ln!=null ? ln.list : null);
140 }
141 errorState|=ReadWrite.closeStream(cris);
142 }
143 }
144
145 final ConcurrentReadInputStream cris;
146 {
147 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin, null, null, null);
148 if(verbose){System.err.println("Started cris");}
149 cris.start(); //4567
150 }
151
152 long readsProcessed=0;
153 long basesProcessed=0;
154
155 long correct=0;
156 long tooLong=0;
157 long tooShort=0;
158
159 {
160
161 ListNum<Read> ln=cris.nextList();
162 ArrayList<Read> reads=(ln!=null ? ln.list : null);
163
164 // System.err.println("Fetched "+reads);
165
166 if(reads!=null && !reads.isEmpty()){
167 Read r=reads.get(0);
168 assert((ffin==null || ffin.samOrBam()) || (r.mate!=null)==cris.paired());
169 }
170
171 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
172
173 for(int idx=0; idx<reads.size(); idx++){
174 final Read r1=reads.get(idx);
175
176 final int initialLength1=r1.length();
177 final int insert=parseInsert(r1.id);
178
179 int delta=insert-initialLength1;
180 if(delta==0){
181 correct++;
182 }else if(delta>0){
183 tooLong++;
184 }else{
185 tooShort++;
186 }
187
188 readsProcessed++;
189 basesProcessed+=initialLength1;
190 }
191
192 cris.returnList(ln);
193 ln=cris.nextList();
194 reads=(ln!=null ? ln.list : null);
195 }
196 if(ln!=null){
197 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
198 }
199 }
200
201 errorState|=ReadWrite.closeStream(cris);
202
203 t.stop();
204
205 long incorrect=tooShort+tooLong;
206 double snr=10*Math.log10((correct+incorrect+0.0001)/(incorrect+0.0001));
207
208 if(total>0){
209 outstream.println("Input Total: \t"+total+" pairs");
210 outstream.println("Input Overlapping: \t"+Tools.format("%.5f",mergeable*100.0/total)+"%\t"+mergeable+" reads");
211 }
212 outstream.println("Correct: \t"+Tools.format("%.5f",correct*100.0/readsProcessed)+"%\t"+correct+" reads");
213 outstream.println("Incorrect: \t"+Tools.format("%.5f",incorrect*100.0/readsProcessed)+"%\t"+incorrect+" reads");
214 outstream.println("Too Short: \t"+Tools.format("%.5f",tooShort*100.0/readsProcessed)+"%\t"+tooShort+" reads");
215 outstream.println("Too Long: \t"+Tools.format("%.5f",tooLong*100.0/readsProcessed)+"%\t"+tooLong+" reads");
216 outstream.println("SNR: \t"+Tools.format("%.3f",snr));
217
218 outstream.println();
219 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
220
221 if(errorState){
222 throw new RuntimeException("GradeMergedReads terminated in an error state; the output may be corrupt.");
223 }
224 }
225
226 public static int parseInsert(String s){
227 if(s.startsWith("SYN")){
228 CustomHeader h=new CustomHeader(s, 0);
229 return h.insert;
230 }
231 assert(s.startsWith("insert=")) : "Can't parse insert size for header "+s;
232
233 // int space=s.indexOf(' ');
234 // if(space<0){space=s.length();}
235 int space=s.length();
236 int equals=s.indexOf('=');
237 for(int i=equals+1;i<s.length();i++){//For programs that rename my reads!
238 if(!Tools.isDigit(s.charAt(i))){
239 space=i;
240 break;
241 }
242 }
243 s=s.substring(equals+1, space);
244 int insert=Integer.parseInt(s);
245 return insert;
246 }
247
248 /*--------------------------------------------------------------*/
249
250 /*--------------------------------------------------------------*/
251
252 private String in=null;
253
254 private String extin=null;
255
256 private String raw1=null;
257 private String raw2=null;
258
259 /*--------------------------------------------------------------*/
260
261 private long maxReads=-1;
262
263 /*--------------------------------------------------------------*/
264
265 private final FileFormat ffin;
266
267
268 /*--------------------------------------------------------------*/
269
270 private PrintStream outstream=System.err;
271 public static boolean verbose=false;
272 public boolean errorState=false;
273
274
275 }