Debian Codegraph @

current/jgi/CountGC.java

raw symbols SCIP
1 package jgi;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.PrintStream;
7 import java.util.Arrays;
8
9 import fileIO.FileFormat;
10 import fileIO.ReadWrite;
11 import fileIO.TextStreamWriter;
12 import shared.KillSwitch;
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
20 /**
21 * @author Brian Bushnell
22 * @date Dec 13, 2012
23 *
24 */
25 public class CountGC {
26
27 public static void main(String[] args){
28
29 Timer t=new Timer();
30 if(args.length==0){
31 System.out.println("Usage: CountGC in=<infile> out=<outfile>");
32 System.out.println("Alternately, 'out=stdout' will print to standard out.");
33 System.out.println("Optional flag, format:");
34 System.out.println("format=1\tid start stop A C G T N GC");
35 System.out.println("format=2\tid gc");
36 System.out.println("format=4\tid length gc");
37 System.out.println("Output is always tab-delimited. AGCT are fractions of defined bases; N is fraction of total bases.");
38 System.exit(0);
39 }
40
41 {//Preparse block for help, config files, and outstream
42 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
43 args=pp.args;
44 outstream=pp.outstream;
45 }
46
47 boolean benchmark=false;
48 ReadWrite.USE_UNPIGZ=true;
49
50 String in=null, out=null;
51
52 for(int i=0; i<args.length; i++){
53
54 if(true){
55 final String arg=args[i];
56 final String[] split=arg.split("=");
57 String a=split[0].toLowerCase();
58 String b=split.length>1 ? split[1] : null;
59
60 if(Parser.parseCommonStatic(arg, a, b)){
61 //do nothing
62 }else if(Parser.parseZip(arg, a, b)){
63 //do nothing
64 }else if(Parser.parseQuality(arg, a, b)){
65 //do nothing
66 }else if(a.equals("in")){
67 in=b;
68 }else if(a.equals("out")){
69 out=b;
70 if(b==null || "summaryonly".equalsIgnoreCase(b) || "none".equalsIgnoreCase(b)){
71 out=null;
72 SUMMARY_ONLY=true;
73 }else if("benchmark".equalsIgnoreCase(b)){
74 benchmark=true;
75 out=null;
76 SUMMARY_ONLY=true;
77 }
78 }else if(a.equals("benchmark")){
79 benchmark=Parse.parseBoolean(b);
80 if(benchmark){
81 out=null;
82 SUMMARY_ONLY=true;
83 }
84 }else if(a.equals("format")){
85 FORMAT=Integer.parseInt(b);
86 if(FORMAT!=1 && FORMAT!=2 && FORMAT!=4){
87 throw new RuntimeException("\nUnknown format: "+FORMAT+"; valid values are 1, 2, and 4.\n");
88 }
89 }else if(in==null && i==0 && !args[i].contains("=")){
90 in=args[i];
91 }else if(out==null && i==1 && !args[i].contains("=")){
92 out=args[i];
93 }
94 }
95 }
96
97 {//Process parser fields
98 Parser.processQuality();
99 }
100
101 long[] counts=null;
102 long sum=0;
103
104 if(out==null || out.equalsIgnoreCase("stdout") || out.equalsIgnoreCase("standardout")){out=null;}
105
106 InputStream is=null;
107 {
108 if(in==null){throw new RuntimeException("No input file.");}
109 if(in.equalsIgnoreCase("stdin") || in.equalsIgnoreCase("standardin")){
110 is=System.in;
111 }else{
112 File f=new File(in);
113 if((!f.exists() || f.isDirectory()) && !in.toLowerCase().startsWith("stdin")){
114 throw new RuntimeException("Input file does not appear to be valid: "+in);
115 }
116 }
117 }
118
119 if(is==null){is=ReadWrite.getInputStream(in, false, true);}
120 try {
121 if(benchmark){sum=bench2(is);}
122 else{
123 FileFormat ff=FileFormat.testInput(in, FileFormat.FASTA, null, true, true);
124 boolean fastq=ff.fastq();
125 boolean fasta=!fastq; //Default.
126 if(fastq){counts=countFastq(is, out);}
127 else if(fasta){counts=countFasta(is, out);}
128 else{throw new RuntimeException("Unknown or unsupported file format.");}
129 }
130 } catch (IOException e) {
131 e.printStackTrace();
132 }
133 try {
134 if(is!=System.in){is.close();}
135 } catch (IOException e) {
136 e.printStackTrace();
137 }
138
139
140 t.stop();
141
142 if(benchmark){
143 outstream.println("Time: \t"+t);
144 long bytes=new File(in).length();
145 if(bytes<1){bytes=LIMSUM;}
146 double mbps1=bytes*1000d/t.elapsed;
147 double mbps2=sum*1000d/t.elapsed;
148 outstream.println(Tools.format("Raw Speed: \t%.2f MBytes/s",mbps1));
149 outstream.println(Tools.format("Uncompressed Speed:\t%.2f MBytes/s",mbps2));
150 }else{
151 outstream.println(toString2(new StringBuilder("Overall"), counts));
152 outstream.println("Time: \t"+t);
153 long bytes=new File(in).length();
154 if(bytes<1){bytes=LIMSUM;}
155 double mbps=bytes*1000d/t.elapsed;
156 double mbpps=shared.Vector.sum(counts)*1000d/t.elapsed;
157 outstream.println(Tools.format("Speed:\t%.2f MBytes/s",mbps));
158 outstream.println(Tools.format(" \t%.2f MBases/s",mbpps));
159 }
160
161 //Close the print stream if it was redirected
162 Shared.closeStream(outstream);
163 }
164
165 public static long bench2(InputStream is) throws IOException{
166 final byte[] buf=new byte[32768];
167 long sum=0;
168 for(long len=is.read(buf); len>0; len=is.read(buf)){sum+=len;}
169 return sum;
170 }
171
172 public static long[] countFasta(InputStream is, String out) throws IOException{
173
174 long limsum=0;
175 final byte[] buf=new byte[32768];
176 final TextStreamWriter tsw=(out==null ? null : new TextStreamWriter(out, true, false, false));
177 if(tsw!=null){tsw.start();}
178 final int[] counts=KillSwitch.allocInt1D(6);
179 final long[] overall=new long[6];
180 final StringBuilder hdr=new StringBuilder();
181 boolean hdmode=false;
182
183 int i=0;
184 int lim=is.read(buf);
185 limsum+=lim;
186
187 while(lim>0){
188 if(hdmode){
189 while(i<lim){
190 byte c=buf[i];
191 i++;
192 if(c<=slashr){hdmode=false; break;}
193 hdr.append((char)c);
194 }
195 }
196
197 if(!hdmode){
198 while(i<lim){
199 byte c=buf[i];
200 i++;
201
202 if(c==carrot){
203 hdmode=true;
204 if(hdr.length()>0 || shared.Vector.sum(counts)>0){
205 if(tsw!=null){tsw.print(toString2(hdr, counts));}else if(!SUMMARY_ONLY){System.out.print(toString2(hdr, counts));}
206 hdr.setLength(0);
207 for(int j=0; j<counts.length; j++){
208 overall[j]+=counts[j];
209 counts[j]=0;
210 }
211 }
212 break;
213 }
214 counts[charToNum[c]]++;
215
216 }
217 }
218 if(i>=lim){
219 i=0;
220 lim=is.read(buf);
221 limsum+=lim;
222 }
223 }
224
225 if(hdr.length()>0 || shared.Vector.sum(counts)>0){
226 if(tsw!=null){tsw.print(toString2(hdr, counts));}else if(!SUMMARY_ONLY){System.out.println(toString2(hdr, counts));}
227 hdr.setLength(0);
228 for(int j=0; j<counts.length; j++){
229 overall[j]+=counts[j];
230 counts[j]=0;
231 }
232 }
233
234 if(tsw!=null){
235 tsw.poisonAndWait();
236 }
237 LIMSUM=limsum;
238 return overall;
239 }
240
241 public static long[] countFastq(InputStream is, String out) throws IOException{
242 // assert(false) : "Fastq mode - TODO"; //TODO
243 long limsum=0;
244 final byte[] buf=new byte[32768];
245 final TextStreamWriter tsw=(out==null ? null : new TextStreamWriter(out, true, false, false));
246 if(tsw!=null){tsw.start();}
247 final int[] counts=KillSwitch.allocInt1D(6);
248 final long[] overall=new long[6];
249 final StringBuilder hdr=new StringBuilder();
250
251 int mode=3;
252
253 int i=0;
254 int lim=is.read(buf);
255 limsum+=lim;
256
257 while(mode==3 && lim>0){
258 while(i<lim && buf[i]!=at){i++;}
259 if(i>=lim){
260 lim=is.read(buf);
261 limsum+=lim;
262 }else{
263 assert(buf[i]==at);
264 mode=0;
265 }
266 }
267
268 while(lim>0){
269 if(mode==0){
270 while(i<lim){
271 byte c=buf[i];
272 i++;
273 if(c<=slashr){mode++; break;}
274 if(c!=at){hdr.append((char)c);}
275 }
276 while(i<lim && buf[i]<=slashr){i++;} //In case of \n\r
277 }
278
279 if(mode==1){
280 while(i<lim){
281 byte c=buf[i];
282 i++;
283 if(c<=slashr){
284 mode++;
285 if(hdr.length()>0 || shared.Vector.sum(counts)>0){
286 if(tsw!=null){tsw.print(toString2(hdr, counts));}else if(!SUMMARY_ONLY){System.out.print(toString2(hdr, counts));}
287 hdr.setLength(0);
288 for(int j=0; j<counts.length; j++){
289 overall[j]+=counts[j];
290 counts[j]=0;
291 }
292 }
293 break;
294 }
295 counts[charToNum[c]]++;
296 }
297 while(i<lim && buf[i]<=slashr){i++;} //In case of \n\r
298 }
299
300 if(mode==2){
301 while(i<lim){
302 byte c=buf[i];
303 i++;
304 if(c<=slashr){mode++; break;}
305 }
306 while(i<lim && buf[i]<=slashr){i++;} //In case of \n\r
307 }
308
309 if(mode==3){
310 while(i<lim){
311 byte c=buf[i];
312 i++;
313 if(c<=slashr){mode=0; break;}
314 }
315 while(i<lim && buf[i]<=slashr){i++;} //In case of \n\r
316 }
317
318 if(i>=lim){
319 i=0;
320 lim=is.read(buf);
321 limsum+=lim;
322 }
323 }
324
325 if(hdr.length()>0 || shared.Vector.sum(counts)>0){
326 if(tsw!=null){tsw.print(toString2(hdr, counts));}else if(!SUMMARY_ONLY){System.out.println(toString2(hdr, counts));}
327 hdr.setLength(0);
328 for(int j=0; j<counts.length; j++){
329 overall[j]+=counts[j];
330 counts[j]=0;
331 }
332 }
333
334 if(tsw!=null){
335 tsw.poisonAndWait();
336 }
337 LIMSUM=limsum;
338 return overall;
339 }
340
341 private static String toString2(StringBuilder sb, int[] counts){
342 final long sum1=(long)counts[0]+(long)counts[1]+(long)counts[2]+(long)counts[3];
343 final long sum2=sum1+counts[4];
344 final float inv1=1f/Tools.max(1, sum1);
345 final float inv2=1f/Tools.max(1, sum2);
346 if(FORMAT==1){
347 return sb.append(Tools.format("\t%d\t%.5f\t%.5f\t%.5f\t%.5f\t%.5f\t%.5f\n",
348 sum2, counts[0]*inv1, counts[1]*inv1, counts[2]*inv1, counts[3]*inv1, counts[4]*inv2, (counts[1]+counts[2])*inv1)).toString();
349 }else if(FORMAT==2){
350 return sb.append(Tools.format("\t%.5f\n", (counts[1]+counts[2])*inv1)).toString();
351 }else if(FORMAT==4){
352 return sb.append(Tools.format("\t%d\t%.5f\n", sum2, (counts[1]+counts[2])*inv1)).toString();
353 }else{
354 throw new RuntimeException("Unknown format.");
355 }
356 }
357
358 private static String toString2(StringBuilder sb, long[] counts){
359 final long sum1=(long)counts[0]+(long)counts[1]+(long)counts[2]+(long)counts[3];
360 final long sum2=sum1+counts[4];
361 final float inv1=1f/Tools.max(1, sum1);
362 final float inv2=1f/Tools.max(1, sum2);
363 if(FORMAT==1){
364 return sb.append(Tools.format("\t%d\t%.5f\t%.5f\t%.5f\t%.5f\t%.5f\t%.5f\n",
365 sum2, counts[0]*inv1, counts[1]*inv1, counts[2]*inv1, counts[3]*inv1, counts[4]*inv2, (counts[1]+counts[2])*inv1)).toString();
366 }else if(FORMAT==2){
367 return sb.append(Tools.format("\t%.5f\n", (counts[1]+counts[2])*inv1)).toString();
368 }else if(FORMAT==4){
369 return sb.append(Tools.format("\t%d\t%.5f\n", sum2, (counts[1]+counts[2])*inv1)).toString();
370 }else{
371 throw new RuntimeException("Unknown format.");
372 }
373 }
374
375 private static final byte[] charToNum=makeCharToNum();
376 public static int FORMAT=1;
377 public static boolean SUMMARY_ONLY=false;
378 private static long LIMSUM=0;
379
380 final static byte slashr='\r', slashn='\n', carrot='>', at='@';
381
382 static PrintStream outstream=System.err;
383
384 /**
385 * @return
386 */
387 private static byte[] makeCharToNum() {
388 byte[] r=new byte[256];
389 Arrays.fill(r, (byte)4);
390 r['a']=r['A']=0;
391 r['c']=r['C']=1;
392 r['g']=r['G']=2;
393 r['t']=r['T']=3;
394 r['\n']=r['\r']=r['>']=r['@']=r['+']=5;
395 return r;
396 }
397 }