Debian Codegraph @

current/jgi/TranslateSixFrames.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 dna.AminoAcid;
8 import fileIO.ByteFile;
9 import fileIO.ByteFile1;
10 import fileIO.ByteFile2;
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
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.ConcurrentGenericReadInputStream;
20 import stream.ConcurrentReadInputStream;
21 import stream.ConcurrentReadOutputStream;
22 import stream.FASTQ;
23 import stream.FastaReadInputStream;
24 import stream.Read;
25 import structures.ListNum;
26
27 /**
28 * @author Brian Bushnell
29 * @date Sep 11, 2012
30 *
31 */
32 public class TranslateSixFrames {
33
34 public static void main(String[] args){
35 Timer t=new Timer();
36 TranslateSixFrames x=new TranslateSixFrames(args);
37 x.process(t);
38
39 //Close the print stream if it was redirected
40 Shared.closeStream(x.outstream);
41 }
42
43 public TranslateSixFrames(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 boolean setInterleaved=false; //Whether it was explicitly set.
52
53 Shared.capBuffers(4);
54 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
55 ReadWrite.setZipThreads(Shared.threads());
56
57 int frames=6;
58
59 Parser parser=new Parser();
60 for(int i=0; i<args.length; i++){
61 String arg=args[i];
62 String[] split=arg.split("=");
63 String a=split[0].toLowerCase();
64 String b=split.length>1 ? split[1] : null;
65
66 if(parser.parse(arg, a, b)){
67 //do nothing
68 }else if(a.equals("verbose")){
69 verbose=Parse.parseBoolean(b);
70 ByteFile1.verbose=verbose;
71 ByteFile2.verbose=verbose;
72 stream.FastaReadInputStream.verbose=verbose;
73 ConcurrentGenericReadInputStream.verbose=verbose;
74 stream.FastqReadInputStream.verbose=verbose;
75 ReadWrite.verbose=verbose;
76 }else if(a.equals("tag")){
77 addTag=Parse.parseBoolean(b);
78 }else if(a.equals("skipquality")){
79 skipquality=Parse.parseBoolean(b);
80 }else if(a.equals("translatequality")){
81 skipquality=!Parse.parseBoolean(b);
82 }else if(a.equals("frames")){
83 frames=Integer.parseInt(b);
84 assert(frames>=0 && frames<=6) : "Frames must be in the range of 0 to 6";
85 }else if(a.equals("aain")){
86 NT_IN=!Parse.parseBoolean(b);
87 }else if(a.equals("ntin")){
88 NT_IN=Parse.parseBoolean(b);
89 }else if(a.equals("aaout")){
90 NT_OUT=!Parse.parseBoolean(b);
91 }else if(a.equals("ntout")){
92 NT_OUT=Parse.parseBoolean(b);
93 }else if(a.equals("frames")){
94 frames=Integer.parseInt(b);
95 assert(frames>=0 && frames<=6) : "Frames must be in the range of 0 to 6";
96 }else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
97 parser.in1=arg;
98 }else if(parser.out1==null && i==1 && !arg.contains("=")){
99 parser.out1=arg;
100 }else{
101 System.err.println("Unknown parameter "+args[i]);
102 assert(false) : "Unknown parameter "+args[i];
103 // throw new RuntimeException("Unknown parameter "+args[i]);
104 }
105 }
106 FRAMES=frames;
107 Shared.AMINO_IN=!NT_IN;
108
109 {//Process parser fields
110 Parser.processQuality();
111
112 maxReads=parser.maxReads;
113 samplerate=parser.samplerate;
114 sampleseed=parser.sampleseed;
115
116 overwrite=parser.overwrite;
117 append=parser.append;
118
119 setInterleaved=parser.setInterleaved;
120
121 in1=parser.in1;
122 in2=parser.in2;
123 qfin1=parser.qfin1;
124 qfin2=parser.qfin2;
125
126 out1=parser.out1;
127 out2=parser.out2;
128 qfout1=parser.qfout1;
129 qfout2=parser.qfout2;
130
131 extin=parser.extin;
132 extout=parser.extout;
133 }
134
135 if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){
136 in2=in1.replace("#", "2");
137 in1=in1.replace("#", "1");
138 }
139 if(out1!=null && out2==null && out1.indexOf('#')>-1){
140 out2=out1.replace("#", "2");
141 out1=out1.replace("#", "1");
142 }
143 if(in2!=null){
144 if(FASTQ.FORCE_INTERLEAVED){System.err.println("Reset INTERLEAVED to false because paired input files were specified.");}
145 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
146 }
147
148 assert(FastaReadInputStream.settingsOK());
149
150 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
151 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
152 // if(ReadWrite.isCompressed(in1)){ByteFile.FORCE_MODE_BF2=true;}
153 ByteFile.FORCE_MODE_BF2=true;
154 }
155
156 if(out1==null){
157 if(out1==null){throw new RuntimeException("Error - cannot define out2 without defining out1.");}
158 if(!parser.setOut){
159 out1="stdout";
160 }
161 }
162
163 if(!setInterleaved){
164 assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n";
165 if(in2!=null){ //If there are 2 input streams.
166 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
167 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
168 }else{ //There is one input stream.
169 if(out2!=null){
170 FASTQ.FORCE_INTERLEAVED=true;
171 FASTQ.TEST_INTERLEAVED=false;
172 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
173 }
174 }
175 }
176
177 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
178 if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;}
179
180 if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){
181 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n");
182 }
183
184 ffout1=FileFormat.testOutput(out1, FileFormat.FASTA, extout, true, overwrite, append, false);
185 ffout2=FileFormat.testOutput(out2, FileFormat.FASTA, extout, true, overwrite, append, false);
186
187 ffin1=FileFormat.testInput(in1, FileFormat.FASTA, extin, true, true);
188 ffin2=FileFormat.testInput(in2, FileFormat.FASTA, extin, true, true);
189
190 if((ffout1!=null && ffout1.fasta()) || (ffin1!=null && ffin1.fasta())){skipquality=true;}
191 }
192
193 void process(Timer t){
194
195 final ConcurrentReadInputStream cris;
196 {
197 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, useSharedHeader, ffin1, ffin2, qfin1, qfin2);
198 cris.setSampleRate(samplerate, sampleseed);
199 if(verbose){System.err.println("Started cris");}
200 cris.start(); //4567
201 }
202 boolean paired=cris.paired();
203 if(verbose){System.err.println("Input is "+(paired ? "paired" : "unpaired"));}
204
205 ConcurrentReadOutputStream ros=null;
206 if(out1!=null){
207 final int buff=4;
208
209 if(cris.paired() && out2==null && (in1==null || !in1.contains(".sam"))){
210 outstream.println("Writing interleaved.");
211 }
212
213 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
214 assert(out2==null || (!out2.equalsIgnoreCase(in1) && !out2.equalsIgnoreCase(in2))) : "out1 and out2 have same name.";
215
216 // System.err.println("Calling ConcurrentReadOutputStream with out1="+out1+", out2="+out2+", qfout1="+qfout1+", qfout2="+qfout2);
217 ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, qfout1, qfout2, buff, null, useSharedHeader);
218 ros.start();
219 }
220
221 long readsProcessed=0;
222 long basesProcessed=0;
223
224 long readsOut1=0;
225 long readsOut2=0;
226
227 long basesOut1=0;
228 long basesOut2=0;
229
230 {
231
232 ListNum<Read> ln=cris.nextList();
233 ArrayList<Read> reads=(ln!=null ? ln.list : null);
234
235 // System.err.println("Fetched "+reads);
236
237 if(reads!=null && !reads.isEmpty()){
238 Read r=reads.get(0);
239 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
240 }
241
242 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
243
244 final ArrayList<Read> listOut=new ArrayList<Read>(reads.size()*(NT_IN ? FRAMES : 1));
245
246 for(int idx=0; idx<reads.size(); idx++){
247 final Read r1=reads.get(idx);
248 final Read r2=r1.mate;
249
250 final int initialLength1=r1.length();
251 final int initialLength2=(r1.mateLength());
252
253 if(NT_IN){//Translate to amino acids
254 toFrames(r1, skipquality, addTag, FRAMES, listOut);
255 }else{
256 listOut.add(r1);
257 }
258
259
260 {
261 readsProcessed++;
262 basesProcessed+=initialLength1;
263 }
264 if(r2!=null){
265 readsProcessed++;
266 basesProcessed+=initialLength2;
267 }
268
269 if(addslash){
270 if(r1.id==null){r1.id=""+r1.numericID;}
271 if(!r1.id.contains(" /1")){r1.id+=" /1";}
272 if(r2!=null){
273 if(r2.id==null){r2.id=""+r2.numericID;}
274 if(!r2.id.contains(" /2")){r2.id+=" /2";}
275 }
276 }
277 }
278
279 if(NT_OUT){//Translate to nucleotides
280 for(int i=0; i<listOut.size(); i++){
281 final Read aa1=listOut.get(i);
282 final Read aa2=aa1.mate;
283 final Read nt1=aa1.aminoToNucleic();
284 if(aa2!=null){
285 final Read nt2=aa2.aminoToNucleic();
286 nt1.mate=nt2;
287 nt2.mate=nt1;
288 }
289 listOut.set(i, nt1);
290 }
291 }
292
293 for(Read r1 : listOut){
294 Read r2=r1.mate;
295 readsOut1++;
296 basesOut1+=r1.length();
297
298 if(r2!=null){
299 readsOut2++;
300 basesOut2+=r2.length();
301 }
302 }
303
304 if(ros!=null){ros.add(listOut, ln.id);}
305
306 cris.returnList(ln);
307 ln=cris.nextList();
308 reads=(ln!=null ? ln.list : null);
309 }
310 if(ln!=null){
311 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
312 }
313 }
314
315 errorState|=ReadWrite.closeStreams(cris, ros);
316
317 t.stop();
318
319 long readsOut=readsOut1+readsOut2;
320 long basesOut=basesOut1+basesOut2;
321
322 String rostring=(readsOut<100000 ? ""+readsOut : readsOut<100000000 ? (readsOut/1000)+"k" : (readsOut/1000000)+"m");
323 String aastring=(basesOut<100000 ? ""+basesOut : basesOut<100000000 ? (basesOut/1000)+"k" : (basesOut/1000000)+"m");
324
325 while(rostring.length()<8){rostring=" "+rostring;}
326 while(aastring.length()<8){aastring=" "+aastring;}
327
328 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
329 outstream.println("Reads Out: "+rostring);
330 outstream.println((NT_OUT ? "Bases Out: " : "Amino Acids Out: ")+aastring);
331
332 if(errorState){
333 throw new RuntimeException("TranslateSixFrames terminated in an error state; the output may be corrupt.");
334 }
335 }
336
337 public static final ArrayList<Read> toFrames(Read r1, boolean skipquality, boolean addTag, int frames){
338 return toFrames(r1, skipquality, addTag, frames, new ArrayList<Read>(frames));
339 }
340
341 public static final ArrayList<Read> toFrames(Read r1, boolean skipquality, boolean addTag, int frames, ArrayList<Read> listOut){
342 final Read r2=r1.mate;
343 final byte[][] bm1=AminoAcid.toAAsSixFrames(r1.bases);
344 final byte[][] qm1=(skipquality ? QNULL : AminoAcid.toQualitySixFrames(r1.quality, 0));
345 final byte[][] bm2=(r2==null ? null : AminoAcid.toAAsSixFrames(r2.bases));
346 final byte[][] qm2=(r2==null ? null : (skipquality ? QNULL : AminoAcid.toQualitySixFrames(r2.quality, 0)));
347
348 for(int i=0; i<frames; i++){
349 Read aa1=new Read(bm1[i], qm1[i], (addTag ? r1.id+frametag[i] : r1.id), r1.numericID, r1.flags|Read.AAMASK, r1.chrom, r1.start, r1.stop);
350 Read aa2=null;
351 if(r2!=null){
352 aa2=new Read(bm2[i], qm2[i], (addTag ? r2.id+frametag[i] : r2.id), r2.numericID, r2.flags|Read.AAMASK, r2.chrom, r2.start, r2.stop);
353 aa1.mate=aa2;
354 aa2.mate=aa1;
355 }
356 if(aa1.bases!=null || (aa2!=null && aa2.bases!=null)){listOut.add(aa1);}
357 }
358 return listOut;
359 }
360
361 /*--------------------------------------------------------------*/
362
363 /*--------------------------------------------------------------*/
364
365 private String in1=null;
366 private String in2=null;
367
368 private String qfin1=null;
369 private String qfin2=null;
370
371 private String out1=null;
372 private String out2=null;
373
374 private String qfout1=null;
375 private String qfout2=null;
376
377 private String extin=null;
378 private String extout=null;
379
380 /*--------------------------------------------------------------*/
381
382 /** Add /1 and /2 to paired reads */
383 private boolean addslash=false;
384
385 private boolean skipquality=false;
386
387 private boolean NT_IN=true;
388 private boolean NT_OUT=false;
389
390 private long maxReads=-1;
391 private float samplerate=1f;
392 private long sampleseed=-1;
393
394 private final int FRAMES;
395
396 /*--------------------------------------------------------------*/
397
398 private final FileFormat ffin1;
399 private final FileFormat ffin2;
400
401 private final FileFormat ffout1;
402 private final FileFormat ffout2;
403
404 private static final String[] frametag=new String[] {" fr1", " fr2", " fr3", " fr4", " fr5", " fr6"};
405 private static final byte[][] QNULL=new byte[6][];
406 private boolean addTag=true;
407
408 /*--------------------------------------------------------------*/
409
410 private PrintStream outstream=System.err;
411 public static boolean verbose=false;
412 public boolean errorState=false;
413 private boolean overwrite=true;
414 private boolean append=false;
415 private boolean useSharedHeader;
416
417 }