Debian Codegraph @

current/template/A_Sample_Unpaired.java

raw symbols SCIP
1 package template;
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.ConcurrentReadOutputStream;
21 import stream.FASTQ;
22 import stream.FastaReadInputStream;
23 import stream.Read;
24 import structures.ListNum;
25 import tracker.ReadStats;
26
27 /**
28 * @author Brian Bushnell
29 * @date Oct 17, 2014
30 *
31 */
32 public class A_Sample_Unpaired {
33
34 public static void main(String[] args){
35 //Start a timer immediately upon code entrance.
36 Timer t=new Timer();
37
38 //Create an instance of this class
39 A_Sample_Unpaired x=new A_Sample_Unpaired(args);
40
41 //Run the object
42 x.process(t);
43
44 //Close the print stream if it was redirected
45 Shared.closeStream(x.outstream);
46 }
47
48 public A_Sample_Unpaired(String[] args){
49
50 {//Preparse block for help, config files, and outstream
51 PreParser pp=new PreParser(args, getClass(), false);
52 args=pp.args;
53 outstream=pp.outstream;
54 }
55
56 //Set shared static variables
57 Shared.capBuffers(4); //Only for singlethreaded programs
58 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
59 ReadWrite.setZipThreads(Shared.threads());
60 FASTQ.TEST_INTERLEAVED=FASTQ.FORCE_INTERLEAVED=false;
61
62 Parser parser=new Parser();
63 for(int i=0; i<args.length; i++){
64 String arg=args[i];
65 String[] split=arg.split("=");
66 String a=split[0].toLowerCase();
67 String b=split.length>1 ? split[1] : null;
68 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
69
70 if(a.equals("verbose")){
71 verbose=Parse.parseBoolean(b);
72 ByteFile1.verbose=verbose;
73 ByteFile2.verbose=verbose;
74 stream.FastaReadInputStream.verbose=verbose;
75 ConcurrentGenericReadInputStream.verbose=verbose;
76 stream.FastqReadInputStream.verbose=verbose;
77 ReadWrite.verbose=verbose;
78 }else if(parser.parse(arg, a, b)){
79 //do nothing
80 }
81
82 else if(parser.in1==null && i==0 && Tools.looksLikeInputStream(arg)){
83 parser.in1=arg;
84 }else if(parser.out1==null && i==1 && !arg.contains("=")){
85 parser.out1=arg;
86 }else{
87 outstream.println("Unknown parameter "+args[i]);
88 assert(false) : "Unknown parameter "+args[i];
89 // throw new RuntimeException("Unknown parameter "+args[i]);
90 }
91 }
92
93 {//Process parser fields
94 Parser.processQuality();
95
96 maxReads=parser.maxReads;
97
98 overwrite=ReadStats.overwrite=parser.overwrite;
99 append=ReadStats.append=parser.append;
100
101 in1=parser.in1;
102 qfin1=parser.qfin1;
103
104 out1=parser.out1;
105 qfout1=parser.qfout1;
106
107 extin=parser.extin;
108 extout=parser.extout;
109 }
110
111 assert(FastaReadInputStream.settingsOK());
112
113 if(in1==null){throw new RuntimeException("Error - at least one input file is required.");}
114 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2){
115 ByteFile.FORCE_MODE_BF2=false;
116 ByteFile.FORCE_MODE_BF1=true;
117 }
118
119 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
120
121 if(!Tools.testOutputFiles(overwrite, append, false, out1)){
122 outstream.println((out1==null)+", "+out1);
123 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+"\n");
124 }
125
126 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false);
127
128 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true);
129 }
130
131 void process(Timer t){
132
133 final ConcurrentReadInputStream cris;
134 {
135 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null, qfin1, null);
136 cris.start();
137 if(verbose){outstream.println("Started cris");}
138 }
139 boolean paired=cris.paired();
140 // if(verbose){
141 if(!ffin1.samOrBam()){outstream.println("Input is being processed as "+(paired ? "paired" : "unpaired"));}
142 // }
143
144 final ConcurrentReadOutputStream ros;
145 if(out1!=null){
146 final int buff=4;
147
148 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
149
150 ros=ConcurrentReadOutputStream.getStream(ffout1, null, qfout1, null, buff, null, false);
151 ros.start();
152 }else{ros=null;}
153
154 long readsProcessed=0, readsOut=0;
155 long basesProcessed=0, basesOut=0;
156
157 {
158
159 ListNum<Read> ln=cris.nextList();
160 ArrayList<Read> reads=(ln!=null ? ln.list : null);
161
162 // outstream.println("Fetched "+reads);
163
164 if(reads!=null && !reads.isEmpty()){
165 Read r=reads.get(0);
166 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
167 }
168
169 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
170
171 final ArrayList<Read> listOut=new ArrayList<Read>(reads.size());
172 for(int idx=0; idx<reads.size(); idx++){
173 final Read r1=reads.get(idx);
174
175 final int initialLength1=r1.length();
176 final boolean keep=process(r1);
177 if(keep){
178 listOut.add(r1);
179 readsOut++;
180 basesOut+=r1.length();
181 }
182
183 readsProcessed++;
184 basesProcessed+=initialLength1;
185 }
186
187 if(ros!=null){ros.add(listOut, ln.id);}
188
189 cris.returnList(ln);
190 ln=cris.nextList();
191 reads=(ln!=null ? ln.list : null);
192 }
193 if(ln!=null){
194 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
195 }
196 }
197
198 errorState|=ReadStats.writeAll();
199
200 errorState|=ReadWrite.closeStreams(cris, ros);
201
202 t.stop();
203 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
204 outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false));
205
206 if(errorState){
207 throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt.");
208 }
209 }
210
211 /*--------------------------------------------------------------*/
212
213 private boolean process(Read r){
214 assert(false);
215 return true;
216 }
217
218 /*--------------------------------------------------------------*/
219
220 private String in1=null;
221
222 private String qfin1=null;
223
224 private String out1=null;
225
226 private String qfout1=null;
227
228 private String extin=null;
229 private String extout=null;
230
231 /*--------------------------------------------------------------*/
232
233 private long maxReads=-1;
234
235 /*--------------------------------------------------------------*/
236
237 private final FileFormat ffin1;
238
239 private final FileFormat ffout1;
240
241
242 /*--------------------------------------------------------------*/
243
244 private PrintStream outstream=System.err;
245 public static boolean verbose=false;
246 public boolean errorState=false;
247 private boolean overwrite=true;
248 private boolean append=false;
249
250 }