Debian Codegraph @

current/pacbio/MergeFastaContigs.java

raw symbols SCIP
1 package pacbio;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6
7 import fileIO.FileFormat;
8 import fileIO.ReadWrite;
9 import fileIO.TextFile;
10 import fileIO.TextStreamWriter;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Timer;
15 import stream.ConcurrentReadInputStream;
16 import stream.FASTQ;
17 import stream.Read;
18 import structures.ListNum;
19 import tracker.ReadStats;
20
21 /**
22 * @author Brian Bushnell
23 * @date Jul 10, 2012
24 *
25 */
26 public class MergeFastaContigs {
27
28
29 public static void main(String[] args){
30
31 {//Preparse block for help, config files, and outstream
32 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
33 args=pp.args;
34 //outstream=pp.outstream;
35 }
36
37 Timer t=new Timer();
38 String infile=null;
39 String outfile=null;
40 String outindex=null;
41 int npl=-1;
42 int npl2=-1;
43
44 for(int i=0; i<args.length; i++){
45 final String arg=args[i];
46 final String[] split=arg.split("=");
47 String a=split[0].toLowerCase();
48 String b=split.length>1 ? split[1] : null;
49
50 if(Parser.parseZip(arg, a, b)){
51 //do nothing
52 }else if(a.equals("in") && split.length>0){
53 infile=b;
54 }else if(a.equals("out") && split.length>0){
55 outfile=b;
56 }else if(a.equals("index") && split.length>0){
57 outindex=b;
58 }else if(a.equals("npad")){
59 npl=N_PAD_LENGTH=Integer.parseInt(b);
60 }else if(a.equals("npad2")){
61 npl2=N_PAD_LENGTH2=Integer.parseInt(b);
62 }else if(a.equals("maxdataout")){
63 maxDataOut=Integer.parseInt(b);
64 }else if(a.equals("mincontig")){
65 MIN_CONTIG_TO_ADD=Integer.parseInt(b);
66 }else if(a.equals("maxlen")){
67 MAX_OUTPUT_LEN=Integer.parseInt(b);
68 }else if(a.equals("maxchroms")){
69 maxChromsOut=Integer.parseInt(b);
70 }else if(a.equals("maxdata")){
71 maxDataOut=Parse.parseKMG(b);
72 }else if(a.equals("verbose")){
73 verbose=Parse.parseBoolean(b);
74 }else if(a.equals("append") || a.equals("app")){
75 append=ReadStats.append=Parse.parseBoolean(b);
76 }else if(a.equals("overwrite") || a.equals("ow")){
77 overwrite=Parse.parseBoolean(b);
78 }else if(a.equals("padfront") || a.equals("padstart")){
79 PAD_START=Parse.parseBoolean(b);
80 }else{
81 throw new RuntimeException("Unknown argument "+arg);
82 }
83 }
84
85 if(infile==null){infile=args[0];}
86 if(outfile==null){outfile=args[1];}
87 if(outindex==null){outindex=args[2];}
88
89 try {
90 if(npl<0 && args.length>3){N_PAD_LENGTH=Integer.parseInt(args[3]);}
91 if(npl2<0 && args.length>4){N_PAD_LENGTH2=Integer.parseInt(args[4]);}
92 } catch (NumberFormatException e) {
93 //ignore
94 }
95
96 if(infile.contains(".fq.") || infile.endsWith(".fq") || infile.contains(".fastq.") || infile.endsWith(".fastq")){
97 mergeFastq(infile, outfile, outindex);
98 }else{
99 if(new File(infile).exists()){
100 // System.err.println("Warning: This will run correctly, but I suggest against putting commas in your filenames.");
101 // assert false : infile+", "+outfile+", "+outindex;
102 mergeFasta(new String[] {infile}, outfile, outindex);
103 }else{
104 String[] files=infile.split(",");
105 for(String s : files){
106 if(!new File(s).exists()){throw new RuntimeException("Cannot find file "+s);}
107 }
108 mergeFasta(files, outfile, outindex);
109 }
110 }
111 t.stop();
112
113 System.out.println("MergeFastaContigs output for "+Arrays.toString(args));
114 System.out.println("definedBasesIn: \t"+definedBasesIn);
115 System.out.println("contigsIn: \t"+contigsIn);
116 System.out.println("definedBasesOut: \t"+definedBasesOut);
117 System.out.println("basesOut: \t"+dataOut);
118 System.out.println("contigsOut: \t"+contigsOut);
119 System.out.println("chromsOut: \t"+chromsOut);
120
121 System.out.println("Time:\t"+t);
122
123 }
124
125
126
127 /**
128 * @param infile
129 * @param outfile
130 * @param outindex
131 */
132 public static void merge(String infile, String outfile, String outindex) {
133 StringBuilder temp=new StringBuilder(MIN_CONTIG_TO_ADD);
134 TextFile tf=new TextFile(infile, false);
135
136 // OutputStream cos=ReadWrite.getOutputStream(outfile, false);
137 // PrintWriter cpw=new PrintWriter(cos);
138
139 long loc=N_PAD_LENGTH;
140 int chrom=1;
141 System.out.println(">chr"+chrom);
142 npad=npad(N_PAD_LENGTH);
143 printAsLines(npad, 0);
144
145 String s=null;
146 String label=null;
147 for(s=tf.nextLine(); chrom<maxChromsOut && dataOut<maxDataOut; s=tf.nextLine()){
148 if(s==null || s.charAt(0)=='>'){
149
150 if(s!=null){contigsIn++;}
151
152 //evict current contig
153 if(temp.length()>=MIN_CONTIG_TO_ADD){
154
155 long newloc=loc+temp.length()+N_PAD_LENGTH;
156 if(newloc>=MAX_OUTPUT_LEN){
157 //Evict old chrom
158
159 //Make new chrom
160 chrom++;
161 loc=N_PAD_LENGTH;
162 newloc=loc+temp.length()+N_PAD_LENGTH;
163 System.out.println("\n>chr"+chrom);
164 printAsLines(npad, 0);
165 }
166
167 printAsLines(temp, (int)(loc%lineBreak));
168
169 definedBasesOut+=temp.length();
170 contigsOut++;
171
172 printAsLines(npad, (int)((loc+temp.length())%lineBreak));
173 System.err.println(chrom+"\t"+loc+"\t"+label);
174 loc=newloc;
175 }else{
176 // System.err.println("Ignored "+temp);
177 }
178
179 if(s==null){break;}
180 temp.setLength(0);
181 label=s.substring(1);
182 }else{
183 //append line to current contig
184 temp.append(s);
185 definedBasesIn+=s.length();
186 }
187 }
188 tf.close();
189
190 chromsOut=chrom;
191 System.out.println();
192 }
193
194 /**
195 * @param infiles
196 * @param outfile
197 * @param outindex
198 */
199 public static void mergeFasta(String infiles[], String outfile, String outindex) {
200
201 if(new File(outfile).exists()){
202 for(String s : infiles){assert(!s.equalsIgnoreCase(outfile));}
203 }
204
205 //if(verbose){System.err.println("A");}
206
207 StringBuilder temp=new StringBuilder(MIN_CONTIG_TO_ADD);
208 TextFile tf;
209
210 TextStreamWriter cout=new TextStreamWriter(outfile, overwrite, false, false);
211 TextStreamWriter iout=new TextStreamWriter(outindex, overwrite, false, false);
212
213 cout.start();
214 iout.start();
215 //if(verbose){System.err.println("B");}
216
217 long loc=(PAD_START ? N_PAD_LENGTH2 : 0);
218 int chrom=1;
219 cout.print(">chr"+chrom+"\n");
220 npad=npad(N_PAD_LENGTH);
221 npad2=npad2(N_PAD_LENGTH2);
222 assert(npad.length()<=npad2.length());
223 if(PAD_START){printAsLines(npad2, 0, cout);}
224 boolean np2=true;
225 // cout.poison();
226 // assert(false) : "\n"+npad+"\n\n\n"+npad2+"\n";
227 //if(verbose){System.err.println("C");}
228
229 // assert(false) : PAD_START+", "+np2;
230
231 for(String fname : infiles){
232 tf=new TextFile(fname, false);
233 String s=null;
234 String label=null;
235 if(verbose){System.err.println("Processing file "+fname);}
236 for(s=tf.nextLine(); chrom<maxChromsOut && dataOut<maxDataOut; s=tf.nextLine()){
237 //if(verbose){System.err.print("");}
238 if(verbose){System.err.println("Processing line "+s);}
239 if(s==null || s.charAt(0)=='>'){
240 if(verbose){System.err.println("Contig break");}
241 // System.err.println("chrom="+chrom+", maxChromsOut="+maxChromsOut);
242
243 if(s!=null){contigsIn++;}
244 if(verbose){System.err.println("Contigs="+contigsIn);}
245
246 //evict current contig
247 if(temp.length()>=MIN_CONTIG_TO_ADD){
248 if(verbose){System.err.println("Big enough to add");}
249
250 long newloc=loc+temp.length()+N_PAD_LENGTH;
251 if(newloc>=MAX_OUTPUT_LEN){
252 if(verbose){System.err.println("newloc>=MAX_OUTPUT_LEN");}
253 //Evict old chrom
254 printAsLines(npad2, (int)(loc%lineBreak), cout);
255
256 //Make new chrom
257 chrom++;
258 loc=N_PAD_LENGTH2;
259 newloc=loc+temp.length()+N_PAD_LENGTH;
260 cout.print("\n>chr"+chrom+"\n");
261 if(PAD_START){printAsLines(npad2, 0, cout);}
262 np2=true;
263 }
264 if(verbose){System.err.println("G");}
265
266 printAsLines(temp, (int)(loc%lineBreak), cout);
267
268 definedBasesOut+=temp.length();
269 contigsOut++;
270
271 if(np2){
272 if(verbose){System.err.println("np2");}
273 if(PAD_START){
274 if(verbose){System.err.println("PAD_START");}
275 loc=N_PAD_LENGTH2;
276 newloc=N_PAD_LENGTH2+temp.length();
277 }else{
278 if(verbose){System.err.println("~PAD_START");}
279 loc=0;
280 newloc=temp.length();
281 }
282 }else{
283 if(verbose){System.err.println("PAD_START");}
284 printAsLines(npad, (int)((loc+temp.length())%lineBreak), cout);
285 }
286 if(verbose){System.err.println("H");}
287 if(label!=null){iout.print(chrom+"\t"+loc+"\t"+label+"\n");}
288 loc=newloc;
289 np2=false;
290 }else{
291 // System.err.println("Ignored "+temp);
292 }
293 if(verbose){System.err.println("Done with contig");}
294
295 temp.setLength(0);
296 if(s==null){break;}
297 label=s.substring(1);
298 }else{
299 np2=false;
300 //if(verbose){System.err.print("J");}
301 //append line to current contig
302 temp.append(s);
303 definedBasesIn+=s.length();
304 if(verbose){System.err.println("Normal line. definedBasesIn="+definedBasesIn);}
305 }
306 //if(verbose){System.err.print("K");}
307 }
308 tf.close();
309 //if(verbose){System.err.print("L");}
310 }
311 //if(verbose){System.err.println("M");}
312
313 chromsOut=chrom;
314
315 assert(temp.length()==0) : temp.length();
316 printAsLines(npad2, (int)(loc%lineBreak), cout);
317 //if(verbose){System.err.println("N");}
318
319
320 cout.print("\n");
321 cout.poisonAndWait();
322 iout.poisonAndWait();
323 //if(verbose){System.err.println("O");}
324 }
325
326
327
328 /**
329 * @param in1
330 * @param outfile
331 * @param outindex
332 */
333 public static void mergeFastq(String in1, String outfile, String outindex) {
334 StringBuilder temp=new StringBuilder(MIN_CONTIG_TO_ADD);
335
336 FASTQ.TEST_INTERLEAVED=false;
337 FASTQ.DETECT_QUALITY=false;
338 long maxReads=-1;
339
340 final ConcurrentReadInputStream cris;
341 {
342 FileFormat ff1=FileFormat.testInput(in1, FileFormat.FASTQ, null, true, true);
343 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ff1, null);
344 // if(verbose){System.err.println("Started cris");}
345 cris.start(); //4567
346 }
347
348
349 TextStreamWriter cout=new TextStreamWriter(outfile, overwrite, false, false);
350 TextStreamWriter iout=new TextStreamWriter(outindex, overwrite, false, false);
351
352 cout.start();
353 iout.start();
354
355 long loc=N_PAD_LENGTH2;
356 int chrom=1;
357 cout.print(">chr"+chrom+"\n");
358 npad=npad(N_PAD_LENGTH);
359 npad2=npad2(N_PAD_LENGTH2);
360 assert(npad.length()<=npad2.length());
361 printAsLines(npad2, 0, cout);
362
363
364 String s=null;
365 String label=null;
366
367
368 ListNum<Read> ln=cris.nextList();
369 ArrayList<Read> reads=(ln!=null ? ln.list : null);
370
371
372
373
374 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
375 //System.err.println("reads.size()="+reads.size());
376 for(Read r : reads){
377
378 s=new String(r.bases);
379 label=r.id;
380
381 temp.append(s);
382
383 if(temp.length()>=MIN_CONTIG_TO_ADD){
384
385 long newloc=loc+temp.length()+N_PAD_LENGTH;
386 if(newloc>=MAX_OUTPUT_LEN){
387 //Evict old chrom
388 printAsLines(npad2, (int)(loc%lineBreak), cout);
389
390 //Make new chrom
391 chrom++;
392 loc=N_PAD_LENGTH2;
393 newloc=loc+temp.length()+N_PAD_LENGTH;
394 cout.print("\n>chr"+chrom+"\n");
395 printAsLines(npad2, 0, cout);
396 }
397
398 printAsLines(temp, (int)(loc%lineBreak), cout);
399 printAsLines(npad, (int)((loc+temp.length())%lineBreak), cout);
400 iout.println(chrom+"\t"+loc+"\t"+label);
401 loc=newloc;
402 }else{
403 // System.err.println("Ignored "+temp);
404 }
405
406 temp.setLength(0);
407 if(s==null){break;}
408 label=s.substring(1);
409
410
411 }
412 //System.err.println("returning list");
413 cris.returnList(ln);
414 //System.err.println("fetching list");
415 ln=cris.nextList();
416 reads=(ln!=null ? ln.list : null);
417 }
418
419
420 cris.returnList(ln);
421
422 assert(temp.length()==0) : temp.length();
423 printAsLines(npad2, (int)(loc%lineBreak), cout);
424
425
426 ReadWrite.closeStream(cris);
427
428 cout.print("\n");
429 cout.poison();
430 iout.poison();
431 }
432
433 private static void printAsLines(CharSequence sb, int mod){
434 dataOut+=sb.length();
435 assert(mod<lineBreak);
436 if(mod>0){
437 CharSequence s=sb.subSequence(0, min(lineBreak-mod, sb.length()));
438 if(s.length()+mod==lineBreak){
439 System.out.println(s);
440 }else{
441 System.out.print(s);
442 }
443 }
444
445 int loc=lineBreak-mod;
446 for(; loc<sb.length(); loc+=lineBreak){
447 CharSequence s=sb.subSequence(loc, min(loc+lineBreak, sb.length()));
448 if(s.length()==lineBreak){
449 System.out.println(s);
450 }else{
451 System.out.print(s);
452 }
453 }
454 }
455 private static void printAsLines(CharSequence sb, int mod, TextStreamWriter cout){
456 dataOut+=sb.length();
457 assert(mod<lineBreak);
458 if(mod>0){
459
460 CharSequence s=sb.subSequence(0, min(lineBreak-mod, sb.length()));
461
462 // System.out.println(mod+", "+s.length()+", "+(s.length()+mod)+", "+lineBreak);
463
464 if(s.length()+mod==lineBreak){
465 cout.println(s);
466 }else{
467 cout.print(s);
468 }
469 }
470
471 int loc=(mod==0 ? 0 : lineBreak-mod);
472 for(; loc<sb.length(); loc+=lineBreak){
473 CharSequence s=sb.subSequence(loc, min(loc+lineBreak, sb.length()));
474
475 // System.out.println(mod+", "+s.length()+", "+(s.length()+mod)+", "+lineBreak+", loc="+loc);
476
477 if(s.length()==lineBreak){
478 cout.println(s);
479 }else{
480 cout.print(s);
481 }
482 }
483 }
484
485 public static String npad(int N_PAD_LENGTH){
486 if(npad==null || npad.length()!=N_PAD_LENGTH){
487 StringBuilder sb=new StringBuilder(N_PAD_LENGTH);
488 for(int i=0; i<N_PAD_LENGTH; i++){
489 sb.append('N');
490 }
491 npad=sb.toString();
492 }
493 return npad;
494 }
495
496 public static String npad2(int N_PAD_LENGTH){
497 if(npad2==null || npad2.length()!=N_PAD_LENGTH){
498 StringBuilder sb=new StringBuilder(N_PAD_LENGTH);
499 for(int i=0; i<N_PAD_LENGTH; i++){
500 sb.append('N');
501 }
502 npad2=sb.toString();
503 }
504 return npad2;
505 }
506
507 private static final int min(int x, int y){return x<y ? x : y;}
508 @SuppressWarnings("unused")
509 private static final int max(int x, int y){return x>y ? x : y;}
510
511 static long definedBasesIn=0;
512 static long contigsIn=0;
513 static long definedBasesOut=0;
514 static long contigsOut=0;
515 static long chromsOut=0;
516
517 public static int lineBreak=80;
518 public static int modulo=lineBreak+1;
519 public static int N_PAD_LENGTH=300;
520 public static int N_PAD_LENGTH2=2000; //for ends
521 public static int MIN_CONTIG_TO_ADD=150;
522 public static int MAX_OUTPUT_LEN=220000000; //200M allows expansion to 262M
523 public static int maxChromsOut=60000;
524 public static long maxDataOut=Long.MAX_VALUE;
525 private static long dataOut=0;
526 public static String npad, npad2;
527 public static boolean overwrite=true;
528 public static boolean append=false;
529 public static boolean PAD_START=true; //Set to true to add padding to beginning.
530 public static boolean verbose=false;
531
532 }