Debian Codegraph @

current/cluster/MergeReadHeaders.java

raw symbols SCIP
1 package cluster;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.util.ArrayList;
6
7 import dna.Data;
8 import fileIO.ByteFile;
9 import fileIO.ByteFile1;
10 import fileIO.ByteFile2;
11 import fileIO.FileFormat;
12 import fileIO.ReadWrite;
13 import fileIO.TextFile;
14 import shared.Parse;
15 import shared.Parser;
16 import shared.PreParser;
17 import shared.Shared;
18 import shared.Timer;
19 import shared.Tools;
20 import stream.ConcurrentGenericReadInputStream;
21 import stream.ConcurrentReadInputStream;
22 import stream.ConcurrentReadOutputStream;
23 import stream.FASTQ;
24 import stream.FastaReadInputStream;
25 import stream.Read;
26 import structures.ListNum;
27 import tracker.ReadStats;
28
29 /**
30 * Appears to replace read headers with those in a text file.
31 * @author Brian Bushnell
32 * @date Feb 7, 2014
33 *
34 */
35 public class MergeReadHeaders {
36
37 public static void main(String[] args){
38 Timer t=new Timer();
39 MergeReadHeaders x=new MergeReadHeaders(args);
40 x.process(t);
41
42 //Close the print stream if it was redirected
43 Shared.closeStream(x.outstream);
44 }
45
46 public MergeReadHeaders(String[] args){
47
48 {//Preparse block for help, config files, and outstream
49 PreParser pp=new PreParser(args, getClass(), false);
50 args=pp.args;
51 outstream=pp.outstream;
52 }
53
54 Parser parser=new Parser();
55 Shared.capBuffers(4);
56 ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true;
57 ReadWrite.setZipThreads(Shared.threads());
58
59
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.parseCommonStatic(arg, a, b)){
67 //do nothing
68 }else if(Parser.parseZip(arg, a, b)){
69 //do nothing
70 }else if(Parser.parseQuality(arg, a, b)){
71 //do nothing
72 }else if(Parser.parseFasta(arg, a, b)){
73 //do nothing
74 }else if(parser.parseInterleaved(arg, a, b)){
75 //do nothing
76 }else if(a.equals("passes")){
77 assert(false) : "'passes' is disabled.";
78 // passes=Integer.parseInt(b);
79 }else if(a.equals("verbose")){
80 verbose=Parse.parseBoolean(b);
81 ByteFile1.verbose=verbose;
82 ByteFile2.verbose=verbose;
83 stream.FastaReadInputStream.verbose=verbose;
84 ConcurrentGenericReadInputStream.verbose=verbose;
85 stream.FastqReadInputStream.verbose=verbose;
86 ReadWrite.verbose=verbose;
87 }else if(a.equals("reads") || a.equals("maxreads")){
88 maxReads=Parse.parseKMG(b);
89 }else if(a.equals("t") || a.equals("threads")){
90 Shared.setThreads(b);
91 }else if(a.equals("build") || a.equals("genome")){
92 Data.setGenome(Integer.parseInt(b));
93 }else if(a.equals("header")){
94 headerFile=b;
95 }else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("input1")){
96 in1=b;
97 }else if(a.equals("in2") || a.equals("input2")){
98 in2=b;
99 }else if(a.equals("out") || a.equals("output") || a.equals("out1") || a.equals("output1")){
100 out1=b;
101 }else if(a.equals("out2") || a.equals("output2")){
102 out2=b;
103 }else if(a.equals("extin")){
104 extin=b;
105 }else if(a.equals("extout")){
106 extout=b;
107 }else if(a.equals("append") || a.equals("app")){
108 append=ReadStats.append=Parse.parseBoolean(b);
109 }else if(a.equals("overwrite") || a.equals("ow")){
110 overwrite=Parse.parseBoolean(b);
111 }else if(in1==null && i==0 && Tools.looksLikeInputStream(arg)){
112 in1=arg;
113 }else if(out1==null && i==1 && !arg.contains("=")){
114 out1=arg;
115 }else{
116 System.err.println("Unknown parameter "+args[i]);
117 assert(false) : "Unknown parameter "+args[i];
118 // throw new RuntimeException("Unknown parameter "+args[i]);
119 }
120 }
121
122
123 if(in1!=null && in2==null && in1.indexOf('#')>-1 && !new File(in1).exists()){
124 in2=in1.replace("#", "2");
125 in1=in1.replace("#", "1");
126 }
127 if(out1!=null && out2==null && out1.indexOf('#')>-1){
128 out2=out1.replace("#", "2");
129 out1=out1.replace("#", "1");
130 }
131 if(in2!=null){
132 if(FASTQ.FORCE_INTERLEAVED){System.err.println("Reset INTERLEAVED to false because paired input files were specified.");}
133 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
134 }
135
136 assert(FastaReadInputStream.settingsOK());
137
138 if(in1==null || headerFile==null){throw new RuntimeException("Error - at least one input file and a header file are required.");}
139 if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){
140 // if(ReadWrite.isCompressed(in1)){ByteFile.FORCE_MODE_BF2=true;}
141 ByteFile.FORCE_MODE_BF2=true;
142 }
143
144 if(out1==null && out2!=null){throw new RuntimeException("Error - cannot define out2 without defining out1.");}
145
146 if(!parser.setInterleaved){
147 assert(in1!=null && (out1!=null || out2==null)) : "\nin1="+in1+"\nin2="+in2+"\nout1="+out1+"\nout2="+out2+"\n";
148 if(in2!=null){ //If there are 2 input streams.
149 FASTQ.FORCE_INTERLEAVED=FASTQ.TEST_INTERLEAVED=false;
150 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
151 }else{ //There is one input stream.
152 if(out2!=null){
153 FASTQ.FORCE_INTERLEAVED=true;
154 FASTQ.TEST_INTERLEAVED=false;
155 outstream.println("Set INTERLEAVED to "+FASTQ.FORCE_INTERLEAVED);
156 }
157 }
158 }
159
160 if(out1!=null && out1.equalsIgnoreCase("null")){out1=null;}
161 if(out2!=null && out2.equalsIgnoreCase("null")){out2=null;}
162
163 if(!Tools.testOutputFiles(overwrite, append, false, out1, out2)){
164 throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output files "+out1+", "+out2+"\n");
165 }
166
167 Parser.processQuality();
168
169 ffout1=FileFormat.testOutput(out1, FileFormat.FASTQ, extout, true, overwrite, append, false);
170 ffout2=FileFormat.testOutput(out2, FileFormat.FASTQ, extout, true, overwrite, append, false);
171
172 ffin1=FileFormat.testInput(in1, FileFormat.FASTQ, extin, true, true);
173 ffin2=FileFormat.testInput(in2, FileFormat.FASTQ, extin, true, true);
174
175 ffheader=FileFormat.testInput(headerFile, FileFormat.TEXT, null, true, true);
176 }
177
178 void process(Timer t){
179
180
181 final ConcurrentReadInputStream cris;
182 {
183 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, ffin2, null, null);
184 if(verbose){System.err.println("Started cris");}
185 cris.start();
186 }
187 boolean paired=cris.paired();
188 if(verbose){System.err.println("Input is "+(paired ? "paired" : "unpaired"));}
189
190 ConcurrentReadOutputStream ros=null;
191 if(out1!=null){
192 final int buff=4;
193
194 if(cris.paired() && out2==null && (in1==null || !in1.contains(".sam"))){
195 outstream.println("Writing interleaved.");
196 }
197
198 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
199 assert(out2==null || (!out2.equalsIgnoreCase(in1) && !out2.equalsIgnoreCase(in2))) : "out1 and out2 have same name.";
200
201 ros=ConcurrentReadOutputStream.getStream(ffout1, ffout2, null, null, buff, null, false);
202 ros.start();
203 }
204
205 long readsProcessed=0;
206 long basesProcessed=0;
207
208 TextFile tf=new TextFile(ffheader);
209
210 {
211
212 ListNum<Read> ln=cris.nextList();
213 ArrayList<Read> reads=(ln!=null ? ln.list : null);
214
215 // System.err.println("Fetched "+reads);
216
217 if(reads!=null && !reads.isEmpty()){
218 Read r=reads.get(0);
219 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
220 }
221
222 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
223
224 for(int idx=0; idx<reads.size(); idx++){
225 final Read r1=reads.get(idx);
226 final Read r2=r1.mate;
227
228 {
229 readsProcessed++;
230 basesProcessed+=r1.length();
231 r1.id=processHeader(tf.readLine());
232 }
233 if(r2!=null){
234 readsProcessed++;
235 basesProcessed+=r2.length();
236 r2.id=processHeader(tf.readLine());
237 }
238
239
240 boolean remove=false;
241 if(remove){reads.set(idx, null);}
242 }
243
244 if(ros!=null){ros.add(reads, ln.id);}
245
246 cris.returnList(ln);
247 ln=cris.nextList();
248 reads=(ln!=null ? ln.list : null);
249 }
250 if(ln!=null){
251 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
252 }
253 }
254
255 errorState|=ReadWrite.closeStreams(cris, ros);
256 errorState|=tf.close();
257
258 t.stop();
259 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
260
261 if(errorState){
262 throw new RuntimeException("ReformatReads terminated in an error state; the output may be corrupt.");
263 }
264 }
265
266 public static String processHeader(String s){
267 assert(s!=null);
268 return s;
269 }
270
271 /*--------------------------------------------------------------*/
272
273 public boolean errorState=false;
274
275 private String headerFile=null;
276
277 private String in1=null;
278 private String in2=null;
279
280 private String out1=null;
281 private String out2=null;
282
283 private String extin=null;
284 private String extout=null;
285
286 private boolean overwrite=true;
287 private boolean append=false;
288
289 private long maxReads=-1;
290
291 private final FileFormat ffheader;
292
293 private final FileFormat ffin1;
294 private final FileFormat ffin2;
295
296 private final FileFormat ffout1;
297 private final FileFormat ffout2;
298
299 private PrintStream outstream=System.err;
300
301 /*--------------------------------------------------------------*/
302
303 public static boolean verbose=false;
304
305 }