Debian Codegraph @

current/jgi/GetReads.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.HashSet;
7
8 import dna.Data;
9 import fileIO.ByteFile1;
10 import fileIO.FileFormat;
11 import fileIO.ReadWrite;
12 import fileIO.TextStreamWriter;
13 import shared.Parse;
14 import shared.Parser;
15 import shared.PreParser;
16 import shared.Shared;
17 import shared.Timer;
18 import shared.Tools;
19 import stream.ConcurrentReadInputStream;
20 import stream.FASTQ;
21 import stream.FastaReadInputStream;
22 import stream.Read;
23 import structures.ListNum;
24 import tracker.ReadStats;
25
26 /**
27 * Grab reads with specified numbers from a file.
28 * TODO Note that much of this is ripped directly from ReformatReads, but is incorrect, because this class does not support dual output files.
29 * @author Brian Bushnell
30 * @date Jul 10, 2013
31 *
32 */
33 public class GetReads {
34
35 public static void main(String[] args){
36 GetReads x=new GetReads(args);
37
38 //Close the print stream if it was redirected
39 Shared.closeStream(x.outstream);
40 }
41
42 public GetReads(String[] args){
43
44 {//Preparse block for help, config files, and outstream
45 PreParser pp=new PreParser(args, getClass(), false);
46 args=pp.args;
47 outstream=pp.outstream;
48 }
49
50 Timer t=new Timer();
51
52 Parser parser=new Parser();
53 String in1=null;
54 String in2=null;
55
56 String qfin1=null;
57 String qfin2=null;
58
59 String out1=null;
60 String out2=null;
61
62 String qfout1=null;
63 String qfout2=null;
64
65 boolean errorState=false;
66 long maxReads=-1;
67 int passes=1;
68 boolean testsize=false;
69 boolean overwrite=false, append=false;
70 float samplerate=1f;
71 long sampleseed=1;
72
73
74
75 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
76
77 HashSet<Long> table=new HashSet<Long>();
78
79 for(int i=0; i<args.length; i++){
80 String arg=args[i];
81 String[] split=arg.split("=");
82 String a=split[0].toLowerCase();
83 String b=split.length>1 ? split[1] : null;
84
85 if(Parser.parseCommonStatic(arg, a, b)){
86 //do nothing
87 }else if(Parser.parseZip(arg, a, b)){
88 //do nothing
89 }else if(Parser.parseQuality(arg, a, b)){
90 //do nothing
91 }else if(Parser.parseFasta(arg, a, b)){
92 //do nothing
93 }else if(parser.parseInterleaved(arg, a, b)){
94 //do nothing
95 }else if(a.equals("id") || a.equals("number")){
96 assert(b!=null) : "Bad parameter: "+arg;
97 String[] b2=b.split(",");
98 for(String c : b2){
99 final long x, y;
100 if(c.indexOf('-')>=0){
101 String[] c2=c.split("-");
102 assert(c2.length==2) : c;
103 x=Long.parseLong(c2[0]);
104 y=Long.parseLong(c2[1]);
105 }else{
106 x=y=Long.parseLong(c);
107 }
108 for(long z=x; z<=y; z++){
109 table.add(z);
110 }
111 }
112 }else if(a.equals("passes")){
113 passes=Integer.parseInt(b);
114 }else if(a.equals("verbose")){
115 verbose=Parse.parseBoolean(b);
116 ByteFile1.verbose=verbose;
117 stream.FastaReadInputStream.verbose=verbose;
118 }else if(a.equals("reads") || a.startsWith("maxreads")){
119 maxReads=Parse.parseKMG(b);
120 }else if(a.equals("build") || a.equals("genome")){
121 Data.setGenome(Integer.parseInt(b));
122 }else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1")){
123 assert(b!=null) : "Bad parameter: "+arg;
124 in1=b;
125 if(b.indexOf('#')>-1 && !new File(b).exists()){
126 in1=b.replace("#", "1");
127 in2=b.replace("#", "2");
128 }
129 }else if(a.equals("in2") || a.equals("input2")){
130 in2=b;
131 }else if(a.equals("out") || a.equals("output") || a.equals("out1") || a.equals("output1")){
132 assert(b!=null) : "Bad parameter: "+arg;
133 out1=b;
134 if(b.indexOf('#')>-1){
135 out1=b.replace("#", "1");
136 out2=b.replace("#", "2");
137 }
138 }else if(a.equals("out2") || a.equals("output2")){
139 out2=b;
140 }else if(a.equals("qfin") || a.equals("qfin1")){
141 qfin1=b;
142 }else if(a.equals("qfout") || a.equals("qfout1")){
143 qfout1=b;
144 }else if(a.equals("qfin2")){
145 qfin2=b;
146 }else if(a.equals("qfout2")){
147 qfout2=b;
148 }else if(a.equals("testsize")){
149 testsize=Parse.parseBoolean(b);
150 }else if(a.equals("append") || a.equals("app")){
151 append=ReadStats.append=Parse.parseBoolean(b);
152 }else if(a.equals("overwrite") || a.equals("ow")){
153 overwrite=Parse.parseBoolean(b);
154 }else if(a.equals("samplerate")){
155 samplerate=Float.parseFloat(b);
156 assert(samplerate<=1f && samplerate>=0f) : "samplerate="+samplerate+"; should be between 0 and 1";
157 }else if(a.equals("sampleseed")){
158 sampleseed=Long.parseLong(b);
159 }else if(a.startsWith("minscaf") || a.startsWith("mincontig")){
160 stream.FastaReadInputStream.MIN_READ_LEN=Integer.parseInt(b);
161 }else if(in1==null && i==0 && Tools.looksLikeInputStream(arg)){
162 in1=arg;
163 if(arg.indexOf('#')>-1 && !new File(arg).exists()){
164 in1=arg.replace("#", "1");
165 in2=arg.replace("#", "2");
166 }
167 }else{
168 System.err.println("Unknown parameter "+args[i]);
169 assert(false) : "Unknown parameter "+args[i];
170 // throw new RuntimeException("Unknown parameter "+args[i]);
171 }
172 }
173
174 {//Process parser fields
175 Parser.processQuality();
176 }
177
178 assert(FastaReadInputStream.settingsOK());
179
180 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
181
182 if(out1==null){
183 if(out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");}
184 out1="stdout";
185 }
186
187 if(!parser.setInterleaved){
188 assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n";
189 if(in2!=null){ //If there are 2 input streams.
190 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
191 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
192 }else{ //There is one input stream.
193 if(out2!=null){
194 FASTQ.FORCE_INTERLEAVED=true;
195 FASTQ.TEST_INTERLEAVED=false;
196 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
197 }
198 }
199 }
200
201 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
202 if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;}
203
204 if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){
205 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n");
206 }
207
208
209 FileFormat ffin=FileFormat.testInput(in1, 0, null, true, true);
210 FileFormat ffout=FileFormat.testOutput(out1, 0, null, true, overwrite, append, false);
211
212
213 final boolean useSharedHeader=(ffin!=null && ffout!=null && ffin.samOrBam() && ffout.samOrBam());
214
215 if(ffin!=null && ffout!=null && ffin.samOrBam() && (ffout.samOrBam() || ffout.bread())){
216 throw new RuntimeException("\nDirect conversion of sam to sam or bread are not currently supported.\nAll other conversions are possible.");
217 }
218
219
220 ConcurrentReadInputStream cris;
221 {
222 FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
223 FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true);
224 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, useSharedHeader, ff1, ff2);
225 }
226
227 cris.setSampleRate(samplerate, sampleseed);
228 outstream.println("Input is "+(cris.paired() ? "paired" : "unpaired"));
229 cris.start(); //4567
230
231 TextStreamWriter tsw=new TextStreamWriter(out1, overwrite, false, false);
232 tsw.start();
233
234
235 long readsProcessed=0;
236 long basesProcessed=0;
237
238 for(int pass=1; pass<=passes; pass++){
239 // outstream.println("pass="+pass);
240 if(pass>1){
241 FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
242 FileFormat ff2=FileFormat.testInput(in2, FileFormat.FASTQ, null, true, true);
243 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, useSharedHeader, ff1, ff2);
244 cris.setSampleRate(samplerate, sampleseed);
245 cris.start();
246 }
247
248 ListNum<Read> ln=cris.nextList();
249 ArrayList<Read> reads=(ln!=null ? ln.list : null);
250
251 if(reads!=null && !reads.isEmpty()){
252 Read r=reads.get(0);
253 assert(ffin==null || ffin.samOrBam() || (r.mate!=null)==cris.paired());//ffin cannot be null
254 }
255
256 while(reads!=null && reads.size()>0 && !table.isEmpty() && ln!=null){//ln!=null is implied
257
258 for(Read r1 : reads){
259 {
260 readsProcessed++;
261 basesProcessed+=r1.length();
262 }
263 Read r2=r1.mate;
264 if(r2!=null){
265 readsProcessed++;
266 basesProcessed+=r2.length();
267 }
268
269 if(table.remove(r1.numericID)){
270 tsw.println(r1);
271 if(r2!=null){tsw.println(r2);}
272 if(table.isEmpty()){break;}
273 }
274 }
275
276 cris.returnList(ln);
277 ln=cris.nextList();
278 reads=(ln!=null ? ln.list : null);
279 }
280 cris.returnList(ln);
281 errorState|=ReadWrite.closeStream(cris);
282 }
283
284 if(tsw!=null){
285 tsw.poisonAndWait();
286 }
287
288 errorState|=(cris.errorState());
289
290 t.stop();
291 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
292
293 if(testsize){
294 long bytesProcessed=(new File(in1).length()+(in2==null ? 0 : new File(in2).length()))*passes;
295 double xpnano=bytesProcessed/(double)(t.elapsed);
296 String xpstring=(bytesProcessed<100000 ? ""+bytesProcessed : bytesProcessed<100000000 ? (bytesProcessed/1000)+"k" : (bytesProcessed/1000000)+"m");
297 while(xpstring.length()<8){xpstring=" "+xpstring;}
298 outstream.println("Bytes Processed: "+xpstring+" \t"+Tools.format("%.2fm bytes/sec", xpnano*1000));
299 }
300
301 if(errorState){
302 throw new RuntimeException("GetReads terminated in an error state; the output may be corrupt.");
303 }
304
305 }
306
307 private PrintStream outstream=System.err;
308 public static boolean verbose=false;
309
310 }