Debian Codegraph @

current/jgi/FilterReadsWithSubs.java

raw symbols SCIP
1 package jgi;
2
3 import java.util.ArrayList;
4
5 import fileIO.FileFormat;
6 import fileIO.ReadWrite;
7 import shared.Parse;
8 import shared.Parser;
9 import shared.PreParser;
10 import shared.Shared;
11 import shared.Timer;
12 import shared.Tools;
13 import stream.ConcurrentReadInputStream;
14 import stream.ConcurrentReadOutputStream;
15 import stream.Read;
16 import stream.SamLine;
17 import structures.ListNum;
18
19 /**
20 * Filters to select only reads with substitution errors
21 * for bases with quality scores in a certain interval.
22 * Used for manually examining specific reads that may have
23 * incorrectly calibrated quality scores, for example.
24 * @author Brian Bushnell
25 * @date May 5, 2015
26 *
27 */
28 public class FilterReadsWithSubs {
29
30 public static void main(String[] args){
31 //Start a timer immediately upon code entrance.
32 Timer t=new Timer();
33
34 //Create an instance of this class
35 FilterReadsWithSubs x=new FilterReadsWithSubs(args);
36
37 //Run the object
38 x.process(t);
39
40 //Close the print stream if it was redirected
41 Shared.closeStream(x.outstream);
42 }
43
44 public FilterReadsWithSubs(String[] args){
45
46 {//Preparse block for help, config files, and outstream
47 PreParser pp=new PreParser(args, getClass(), false);
48 args=pp.args;
49 outstream=pp.outstream;
50 }
51
52 SamLine.SET_FROM_OK=true;
53 minq=0;
54 maxq=99;
55 minSubs=1;
56 countIndels=true;
57 keepPerfect=false;
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 if(b!=null && b.equalsIgnoreCase("null")){b=null;}
66
67 if(a.equals("parse_flag_goes_here")){
68 //Set a variable here
69
70 // System.err.println("Calling parseArgument("+arg+","+a+","+b+")");
71 }else if(a.equals("minq")){
72 minq=Parse.parseIntKMG(b);
73 }else if(a.equals("maxq")){
74 maxq=Parse.parseIntKMG(b);
75 }else if(a.equals("keepperfect")){
76 keepPerfect=Parse.parseBoolean(b);
77 }else if(a.equals("countindels")){
78 countIndels=Parse.parseBoolean(b);
79 }else if(a.equals("minsubs")){
80 minSubs=Integer.parseInt(b);
81 }else if(a.equals("clips") || a.equalsIgnoreCase("maxclips") || a.equalsIgnoreCase("maxclip")){
82 maxClips=Integer.parseInt(b);
83 if(maxClips<0) {maxClips=Integer.MAX_VALUE;}
84 }else if(a.equalsIgnoreCase("minclips") || a.equalsIgnoreCase("minclip")){
85 minClips=Integer.parseInt(b);
86 }else if(parser.parse(arg, a, b)){//Parse standard flags in the parser
87 //do nothing
88 }else{
89 outstream.println("Unknown parameter "+args[i]);
90 assert(false) : "Unknown parameter "+args[i];
91 }
92 }
93
94 {//Process parser fields
95 Parser.processQuality();
96
97 maxReads=parser.maxReads;
98 in1=parser.in1;
99 out1=parser.out1;
100 }
101
102 ffout1=FileFormat.testOutput(out1, FileFormat.SAM, null, true, true, false, false);
103 ffin1=FileFormat.testInput(in1, FileFormat.SAM, null, true, true);
104 }
105
106 void process(Timer t){
107
108 final ConcurrentReadInputStream cris;
109 {
110 cris=ConcurrentReadInputStream.getReadInputStream(maxReads, true, ffin1, null);
111 cris.start();
112 }
113 boolean paired=cris.paired();
114
115 final ConcurrentReadOutputStream ros;
116 if(out1!=null){
117 final int buff=4;
118
119 if(cris.paired() && (in1==null || !in1.contains(".sam"))){
120 outstream.println("Writing interleaved.");
121 }
122
123 assert(!out1.equalsIgnoreCase(in1) && !out1.equalsIgnoreCase(in1)) : "Input file and output file have same name.";
124
125 ros=ConcurrentReadOutputStream.getStream(ffout1, null, buff, null, ffout1.samOrBam());
126 ros.start();
127 }else{ros=null;}
128
129 {
130
131 ListNum<Read> ln=cris.nextList();
132 ArrayList<Read> reads=(ln!=null ? ln.list : null);
133
134 if(reads!=null && !reads.isEmpty()){
135 Read r=reads.get(0);
136 assert((ffin1==null || ffin1.samOrBam()) || (r.mate!=null)==cris.paired());
137 }
138
139 while(ln!=null && reads!=null && reads.size()>0){//ln!=null prevents a compiler potential null access warning
140 if(verbose){outstream.println("Fetched "+reads.size()+" reads.");}
141
142 for(int idx=0; idx<reads.size(); idx++){
143 final Read r1=reads.get(idx);
144 readsProcessed+=r1.pairCount();
145 basesProcessed+=r1.pairLength();
146
147 boolean keep=processRead(r1);
148 if(keep) {
149 readsOut+=r1.pairCount();
150 basesOut+=r1.pairLength();
151 }else{reads.set(idx, null);}
152 }
153
154 if(ros!=null){ros.add(reads, ln.id);}
155
156 cris.returnList(ln);
157 if(verbose){outstream.println("Returned a list.");}
158 ln=cris.nextList();
159 reads=(ln!=null ? ln.list : null);
160 }
161 if(ln!=null){
162 cris.returnList(ln.id, ln.list==null || ln.list.isEmpty());
163 }
164 }
165 ReadWrite.closeStreams(cris, ros);
166 if(verbose){outstream.println("Finished.");}
167
168 //Report timing and results
169 t.stop();
170 outstream.println(Tools.timeReadsBasesProcessed(t, readsProcessed, basesProcessed, 8));
171 outstream.println(Tools.readsBasesOut(readsProcessed, basesProcessed, readsOut, basesOut, 8, false));
172 }
173
174 private boolean processRead(Read r1) {
175 final byte[] quals=r1.quality, bases=r1.bases;
176 final byte[] match=(r1.match==null ? null : !r1.shortmatch() ? r1.match : Read.toLongMatchString(r1.match));
177 if(match==null || quals==null || bases==null){return false;}
178
179 int subs=0, passingSubs=0;
180 int indels=0;
181 int clips=0;
182 for(int qpos=0, mpos=0; mpos<match.length; mpos++){
183
184 final byte m=match[mpos];
185 final byte mprev=match[Tools.max(mpos-1, 0)];
186 final byte mnext=match[Tools.min(mpos+1, match.length-1)];
187
188 final byte q=quals[qpos];
189 final byte b=bases[qpos];
190
191 if(m=='S'){
192 subs++;
193 if(q>=minq && q<=maxq){passingSubs++;}
194 }else if(m=='I'){
195 indels++;
196 }else if(m=='m'){
197 if(mprev=='D' || mnext=='D'){
198 indels++;
199 }
200 }else if(m=='D'){
201 //do nothing
202 }else if(m=='N'){
203 //do nothing
204 }else if(m=='C'){
205 clips++;
206 }else{
207 throw new RuntimeException("Bad symbol m='"+((char)m)+"'\n"+new String(match)+"\n"+new String(bases)+"\n");
208 }
209 if(clips>maxClips) {return false;}
210
211 if(m!='D'){qpos++;}
212 }
213 if(clips>maxClips || clips<minClips) {return false;}
214 // System.err.println("match="+new String(match));
215 // System.err.println("clips="+clips);
216 // assert(false) : clips+", "+subs+", "+indels+", "+maxClips+", "+minClips;
217 if(subs>=minSubs && (passingSubs>0 || minSubs<1)) {return true;}
218 if(countIndels && indels>0) {return true;}
219 return keepPerfect && subs==0 && indels==0;
220 }
221
222 /*--------------------------------------------------------------*/
223
224 /*--------------------------------------------------------------*/
225
226 private String in1=null;
227 private String out1=null;
228
229 private final FileFormat ffin1;
230 private final FileFormat ffout1;
231
232 /*--------------------------------------------------------------*/
233
234 private long maxReads=-1;
235 private long readsProcessed=0;
236 private long readsOut=0;
237 private long basesProcessed=0;
238 private long basesOut=0;
239
240 public int minq, maxq, minSubs;
241 public int maxClips=Integer.MAX_VALUE;
242 public int minClips=0;
243 public boolean countIndels;
244 public boolean keepPerfect;
245
246 /*--------------------------------------------------------------*/
247
248 private java.io.PrintStream outstream=System.err;
249 public static boolean verbose=false;
250
251 }