Debian Codegraph @

current/pacbio/MergeReadsAndGenome.java

raw symbols SCIP
1 package pacbio;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import dna.Data;
7 import fileIO.FileFormat;
8 import fileIO.ReadWrite;
9 import fileIO.TextStreamWriter;
10 import shared.KillSwitch;
11 import shared.Parse;
12 import shared.Parser;
13 import shared.PreParser;
14 import shared.Tools;
15 import stream.ConcurrentLegacyReadInputStream;
16 import stream.ConcurrentReadInputStream;
17 import stream.FastaReadInputStream;
18 import stream.Read;
19 import stream.SequentialReadInputStream;
20 import structures.ListNum;
21 import tracker.ReadStats;
22
23 /**
24 * @author Brian Bushnell
25 * @date Dec 7, 2012
26 *
27 */
28 public class MergeReadsAndGenome {
29
30
31 public static void main(String[] args){
32
33 {//Preparse block for help, config files, and outstream
34 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
35 args=pp.args;
36 //outstream=pp.outstream;
37 }
38
39 int genome=-1;
40 String in[]=null;
41 String out=null;
42 long reads=-1;
43 int readlen=300;
44 boolean overwrite=true;
45 boolean append=false;
46 int sequentialOverlap=5;
47 boolean sequentialStrandAlt=true;
48 ReadWrite.ZIPLEVEL=2;
49
50 FastaReadInputStream.TARGET_READ_LEN=250;
51 FastaReadInputStream.SPLIT_READS=(FastaReadInputStream.TARGET_READ_LEN>0);
52
53
54 for(int i=0; i<args.length; i++){
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.parseFasta(arg, a, b)){
65 //do nothing
66 }else if(a.equals("in")){
67 if("null".equalsIgnoreCase(b)){
68 //do nothing
69 }else{
70 in=b.split(",");
71 }
72 }else if(a.equals("out")){
73 out=b;
74 }else if(a.equals("build") || a.equals("genome")){
75 genome=Integer.parseInt(b);
76 }else if(a.equals("append") || a.equals("app")){
77 append=ReadStats.append=Parse.parseBoolean(b);
78 }else if(a.equals("overwrite") || a.equals("ow")){
79 overwrite=Parse.parseBoolean(b);
80 System.out.println("Set overwrite to "+overwrite);
81 }else if(a.equals("reads")){
82 reads=Parse.parseKMG(b);
83 }else if(a.equals("readlen") || a.equals("length") || a.equals("len")){
84 readlen=Integer.parseInt(b);
85 }else if(a.equals("sequentialoverlap")){
86 sequentialOverlap=Integer.parseInt(b);
87 }else if(a.equals("sequentialstrandalt")){
88 sequentialStrandAlt=Parse.parseBoolean(b);
89 }else if(a.equals("verbose")){
90 verbose=Parse.parseBoolean(b);
91 }else{
92 System.err.println("Unknown parameter "+split[i]);
93 assert(false);
94 }
95 }
96
97 assert(FastaReadInputStream.settingsOK());
98 if(in!=null){
99 File a=new File(out);
100 for(String s : in){
101 File b=new File(s);
102 if(a.equals(b)){throw new RuntimeException("Input file may not equal output file: "+a.toString());}
103 }
104 }
105 assert(out!=null);
106
107 TextStreamWriter tsw=new TextStreamWriter(out, overwrite, false, false);
108 tsw.start();
109
110 long id=0;
111
112 if(genome>=0){
113 Data.setGenome(genome);
114 SequentialReadInputStream.UNLOAD=true;
115 // SequentialReadInputStream.verbose=true;
116 SequentialReadInputStream ris=new SequentialReadInputStream(reads, readlen, Tools.max(50, readlen/2), sequentialOverlap, sequentialStrandAlt);
117 ConcurrentLegacyReadInputStream cris=new ConcurrentLegacyReadInputStream(ris, reads);
118 cris.start();
119 id=appendReads(cris, tsw, id);
120 ReadWrite.closeStream(cris);
121 }
122
123 if(in!=null){
124 for(String s : in){
125 final ConcurrentReadInputStream cris;
126 {
127 FileFormat ff1=FileFormat.testInput(s, FileFormat.FASTQ, null, true, false);
128 cris=ConcurrentReadInputStream.getReadInputStream(-1, true, ff1, null);
129 if(verbose){System.err.println("Started cris");}
130 cris.start(); //4567
131 }
132 id=appendReads(cris, tsw, id);
133 ReadWrite.closeStream(cris);
134 }
135 }
136
137 tsw.poisonAndWait();
138 }
139
140 public static long appendReads(ConcurrentReadInputStream cris, TextStreamWriter tsw, long id){
141 ListNum<Read> ln=cris.nextList();
142 ArrayList<Read> reads=(ln!=null ? ln.list : null);
143 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
144
145 for(Read r : reads){
146 Read b=r.mate;
147 Read a=correctRead(r, id);
148 if(a!=null){
149 tsw.println(a);
150 id++;
151 }
152 b=correctRead(b, id);
153 if(b!=null){
154 tsw.println(b);
155 id++;
156 }
157 }
158
159 cris.returnList(ln);
160 //System.err.println("fetching list");
161 ln=cris.nextList();
162 reads=(ln!=null ? ln.list : null);
163 }
164 if(verbose){System.err.println("Finished reading");}
165 cris.returnList(ln);
166 if(verbose){System.err.println("Returned list");}
167 return id;
168 }
169
170 public static Read correctRead(Read r, long id){
171 if(r==null){return null;}
172 r.numericID=id;
173 r.id=""+id;
174 if(r.chrom<1){return r;}
175
176 int startN=0;
177 int stopN=r.length()-1;
178 while(startN<r.length() && r.bases[startN]=='N'){startN++;}
179 while(stopN>0 && r.bases[stopN]=='N'){stopN--;}
180 if(startN>0 || stopN<r.length()-1){
181 if(r.length()-startN-stopN<50){return null;}
182 r.bases=KillSwitch.copyOfRange(r.bases, startN, stopN+1);
183 if(r.quality!=null){r.quality=KillSwitch.copyOfRange(r.quality, startN, stopN+1);}
184 }
185 return r;
186 }
187
188 public static boolean verbose=false;
189
190 }