Debian Codegraph @

current/align2/GradeSamFile.java

raw symbols SCIP
1 package align2;
2
3 import java.io.File;
4 import java.util.BitSet;
5
6 import fileIO.FileFormat;
7 import fileIO.TextFile;
8 import fileIO.TextStreamWriter;
9 import shared.Parse;
10 import shared.PreParser;
11 import shared.Tools;
12 import stream.CustomHeader;
13 import stream.Read;
14 import stream.SamLine;
15
16 public class GradeSamFile {
17
18
19 public static void main(String[] args){
20
21 {//Preparse block for help, config files, and outstream
22 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
23 args=pp.args;
24 //outstream=pp.outstream;
25 }
26
27 String in=null, outl=null, outs=null;
28 long reads=-1;
29
30 for(int i=0; i<args.length; i++){
31 final String arg=args[i];
32 final String[] split=arg.split("=");
33 String a=split[0].toLowerCase();
34 String b=split.length>1 ? split[1] : null;
35
36 if(a.equals("in") || a.equals("in1")){
37 in=b;
38 }else if(a.equals("reads")){
39 reads=Parse.parseKMG(b);
40 }else if(a.equals("parsecustom")){
41 parsecustom=Parse.parseBoolean(b);
42 }else if(a.equals("thresh")){
43 THRESH2=Integer.parseInt(b);
44 }else if(a.equals("printerr")){
45 printerr=Parse.parseBoolean(b);
46 // }else if(a.equals("ssaha2") || a.equals("subtractleadingclip")){
47 // SamLine.SUBTRACT_LEADING_SOFT_CLIP=Parse.parseBoolean(b);
48 }else if(a.equals("blasr")){
49 BLASR=Parse.parseBoolean(b);
50 }else if(a.equals("q") || a.equals("quality") || a.startsWith("minq")){
51 minQuality=Integer.parseInt(b);
52 }else if(a.equals("bitset")){
53 USE_BITSET=Parse.parseBoolean(b);
54 }else if(a.equals("outloose") || a.equals("outl")){
55 outl=b;
56 }else if(a.equals("outstrict") || a.equals("outs")){
57 outs=b;
58 }else if(i==0 && args[i].indexOf('=')<0 && (a.startsWith("stdin") || new File(args[0]).exists())){
59 in=args[0];
60 }else if(i==1 && args[i].indexOf('=')<0 && Tools.isDigit(a.charAt(0))){
61 reads=Parse.parseKMG(a);
62 }else{
63 throw new RuntimeException("Unknown parameter "+arg);
64 }
65 }
66
67 if(outl!=null){
68 ffLoose=FileFormat.testOutput(outl, FileFormat.SAM, null, false, true, false, false);
69 tswLoose=new TextStreamWriter(ffLoose);
70 tswLoose.start();
71 }
72
73 if(outs!=null){
74 ffStrict=FileFormat.testOutput(outs, FileFormat.SAM, null, false, true, false, false);
75 tswStrict=new TextStreamWriter(ffStrict);
76 tswStrict.start();
77 }
78
79 if(USE_BITSET){
80 int x=400000;
81 if(reads>0 && reads<=Integer.MAX_VALUE){x=(int)reads;}
82 try {
83 seen=new BitSet(x);
84 } catch (Throwable e) {
85 seen=null;
86 e.printStackTrace();
87 System.out.println("Did not have enough memory to allocate bitset; duplicate mappings will not be detected.");
88 }
89 }
90
91 assert(in!=null) : args[0]+".exists() ? "+new File(args[0]).exists();
92
93 if(reads<1){
94 assert(false) : "Number of expected reads was not specified. Please add a parameter reads=<number> or disable assertions.";
95 System.err.println("Warning - number of expected reads was not specified.");
96 }
97
98 TextFile tf=new TextFile(in, false);
99
100 String s=null;
101 for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
102 char c=s.charAt(0);
103 // System.out.println(s);
104 if(c!='@'/* && c!=' ' && c!='\t'*/){
105 SamLine sl=new SamLine(s);
106 lines++;
107
108 int id=(parsecustom && seen!=null ? ((((int)sl.parseNumericId())<<1)|sl.pairnum()) : (int)lines);
109 // System.out.println(sl.parseNumericId()+", "+sl.pairnum()+", "+id+"");
110 // if(id%500==10){assert(false);}
111 if(sl.primary() && (!parsecustom || seen==null || !seen.get(id))){
112 Read r=sl.toRead(parsecustom);
113 if(seen!=null){seen.set(id);}
114 if(parsecustom && r.originalSite==null){
115 assert(false);
116 System.err.println("Turned off custom parsing.");
117 parsecustom=false;
118 }
119 //System.out.println(r);
120 calcStatistics1(r, sl);
121 }else{
122 secondary++;
123 }
124 }
125 }
126
127 if(tswLoose!=null){tswLoose.poisonAndWait();}
128 if(tswStrict!=null){tswStrict.poisonAndWait();}
129
130 if(reads<-1){reads=primary;}
131
132 double tmult=100d/reads;
133
134 double mappedB=mapped*tmult;
135 double retainedB=mappedRetained*tmult;
136 double truePositiveStrictB=truePositiveStrict*tmult;
137 double falsePositiveStrictB=falsePositiveStrict*tmult;
138 double truePositiveLooseB=truePositiveLoose*tmult;
139 double falsePositiveLooseB=falsePositiveLoose*tmult;
140 double falseNegativeB=(reads-mapped)*tmult;
141 double discardedB=discarded*tmult;
142 double ambiguousB=ambiguous*tmult;
143
144 System.out.println();
145 System.out.println("Mapping Statistics for "+args[0]+":");
146 System.out.println("primary alignments: \t"+primary+" found of "+reads+" expected");
147 System.out.println("secondary alignments: \t"+secondary+" found");
148 System.out.println(Tools.format("mapped: \t"+(mappedB<10?" ":"")+"%.3f", mappedB)+"%");
149 System.out.println(Tools.format("retained: \t"+(retainedB<10?" ":"")+"%.3f", retainedB)+"%");
150 System.out.println(Tools.format("discarded: \t"+(discardedB<10?" ":"")+"%.3f", discardedB)+"%");
151 System.out.println(Tools.format("ambiguous: \t"+(ambiguousB<10?" ":"")+"%.3f", ambiguousB)+"%");
152 if(parsecustom){
153 System.out.println();
154 System.out.println("Strict correctness (both ends exactly correct):");
155 System.out.println(Tools.format("true positive: \t"+(truePositiveStrictB<10?" ":"")+"%.3f", truePositiveStrictB)+"%");
156 System.out.println(Tools.format("false positive: \t"+(falsePositiveStrictB<10?" ":"")+"%.3f", falsePositiveStrictB)+"%");
157 System.out.println();
158 System.out.println("Loose correctness (one end approximately correct):");
159 System.out.println(Tools.format("true positive: \t"+(truePositiveLooseB<10?" ":"")+"%.3f", truePositiveLooseB)+"%");
160 System.out.println(Tools.format("false positive: \t"+(falsePositiveLooseB<10?" ":"")+"%.3f", falsePositiveLooseB)+"%");
161 }
162 System.out.println();
163 System.out.println(Tools.format("false negative: \t"+(falseNegativeB<10?" ":"")+"%.3f", falseNegativeB)+"%");
164
165 if(printerr){
166 System.err.println();
167 System.err.println("Mapping Statistics for "+args[0]+":");
168 System.err.println("primary alignments: \t"+primary+" found of "+reads+" expected");
169 System.err.println("secondary alignments: \t"+secondary+" found");
170 System.err.println(Tools.format("mapped: \t"+(mappedB<10?" ":"")+"%.3f", mappedB)+"%");
171 System.err.println(Tools.format("retained: \t"+(retainedB<10?" ":"")+"%.3f", retainedB)+"%");
172 System.err.println(Tools.format("discarded: \t"+(discardedB<10?" ":"")+"%.3f", discardedB)+"%");
173 System.err.println(Tools.format("ambiguous: \t"+(ambiguousB<10?" ":"")+"%.3f", ambiguousB)+"%");
174 if(parsecustom){
175 System.err.println();
176 System.err.println("Strict correctness (both ends exactly correct):");
177 System.err.println(Tools.format("true positive: \t"+(truePositiveStrictB<10?" ":"")+"%.3f", truePositiveStrictB)+"%");
178 System.err.println(Tools.format("false positive: \t"+(falsePositiveStrictB<10?" ":"")+"%.3f", falsePositiveStrictB)+"%");
179 System.err.println();
180 System.err.println("Loose correctness (one end approximately correct):");
181 System.err.println(Tools.format("true positive: \t"+(truePositiveLooseB<10?" ":"")+"%.3f", truePositiveLooseB)+"%");
182 System.err.println(Tools.format("false positive: \t"+(falsePositiveLooseB<10?" ":"")+"%.3f", falsePositiveLooseB)+"%");
183 }
184 System.err.println();
185 System.err.println(Tools.format("false negative: \t"+(falseNegativeB<10?" ":"")+"%.3f", falseNegativeB)+"%");
186 }
187
188
189 }
190
191
192 public static void calcStatistics1(final Read r, SamLine sl){
193
194 primary++;
195
196 if(r.discarded()/* || r.mapScore==0*/){
197 discarded++;
198 unmapped++;
199 }else if(r.ambiguous()){
200 // assert(r.mapped()) : "\n"+r+"\n"+sl+"\n";
201 if(r.mapped()){mapped++;}
202 ambiguous++;
203 }else if(r.mapScore<1){
204 unmapped++;
205 }else if(r.mapScore<=minQuality){
206 if(r.mapped()){mapped++;}
207 ambiguous++;
208 }else{
209 if(!r.mapped()){
210 unmapped++;
211 }else{
212 mapped++;
213 mappedRetained++;
214
215 if(parsecustom){
216 CustomHeader h=new CustomHeader(sl.qname, sl.pairnum());
217 boolean strict=isCorrectHit(sl, h);
218 boolean loose=isCorrectHitLoose(sl, h);
219
220 // System.err.println(sl.strand()+", "+sl.start(true, true)+", "+sl.stop(sl.start(true, true), true, true)+", "+sl.rnameS());
221 // System.err.println(h.strand+", "+h.start+", "+h.stop+", "+h.rname);
222 // System.err.println(strict+", "+loose);
223 // System.err.println();
224 // assert(false);
225
226 // SiteScore os=r.originalSite;
227 // System.out.println("A1: "+os);
228 // assert(os!=null);
229 // final int trueChrom=os.chrom;
230 // final byte trueStrand=os.strand;
231 // final int trueStart=os.start;
232 // final int trueStop=os.stop;
233 // // System.err.println();
234 // // System.err.println(sl);
235 // // System.err.println();
236 // // System.err.println(r);
237 // // System.err.println();
238 // SiteScore ss=new SiteScore(r.chrom, r.strand(), r.start, r.stop, 0, 0);
239 // byte[] originalContig=sl.originalContig();
240 // if(BLASR){
241 // originalContig=(originalContig==null || Tools.indexOf(originalContig, (byte)'/')<0 ? originalContig :
242 // KillSwitch.copyOfRange(originalContig, 0, Tools.lastIndexOf(originalContig, (byte)'/')));
243 // }
244 // int cstart=sl.originalContigStart();
245 //
246 // // System.out.println("A2: "+trueStart+", "+cstart);
247 // boolean strict=isCorrectHit(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH, originalContig, sl.rname(), cstart, r);
248 // boolean loose=isCorrectHitLoose(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH+THRESH2, originalContig, sl.rname(), cstart);
249
250
251 // if(!strict){
252 // System.out.println(ss+", "+new String(originalContig)+", "+new String(sl.rname()));
253 // assert(false);
254 // }
255
256 // System.out.println("loose = "+loose+" for "+r.toText());
257
258 if(loose){
259 // System.err.println("TPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
260 // +ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
261 truePositiveLoose++;
262 }else{
263 // System.err.println("FPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
264 // +ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
265 falsePositiveLoose++;
266 if(tswLoose!=null){
267 if(ffLoose.samOrBam()){
268 tswLoose.println(sl.toText());
269 }else{
270 tswLoose.println(r);
271 }
272 }
273 }
274
275 if(strict){
276 // System.err.println("TPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
277 truePositiveStrict++;
278 }else{
279 // System.err.println("FPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
280 falsePositiveStrict++;
281 if(tswStrict!=null){
282 if(ffStrict.samOrBam()){
283 tswStrict.println(sl.toText());
284 }else{
285 tswStrict.println(r);
286 }
287 }
288 }
289 }
290 }
291 }
292
293 }
294
295 public static boolean isCorrectHit(SamLine sl, CustomHeader h){
296 if(!sl.mapped()){return false;}
297 if(h.strand!=sl.strand()){return false;}
298 int start=sl.start(true, true);
299 int stop=sl.stop(start, true, true);
300 if(h.start!=start){return false;}
301 if(h.stop!=stop){return false;}
302 if(!h.rname.equals(sl.rnameS())){return false;}
303 return true;
304 }
305
306 public static boolean isCorrectHitLoose(SamLine sl, CustomHeader h){
307 if(!sl.mapped()){return false;}
308 if(h.strand!=sl.strand()){return false;}
309 int start=sl.start(true, true);
310 int stop=sl.stop(start, true, true);
311 if(!h.rname.equals(sl.rnameS())){return false;}
312
313 if(h.start!=start){return false;}
314 if(h.stop!=stop){return false;}
315 return(absdif(h.start, start)<=THRESH2 || absdif(h.stop, stop)<=THRESH2);
316 }
317
318 // public static boolean isCorrectHit(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
319 // byte[] originalContig, byte[] contig, int cstart, Read r){
320 //
321 // final int cstop=cstart+trueStop-trueStart;
322 //
323 //// System.out.println("\n"+r.id);
324 //// System.out.println(" \tstrand"+/*"\tchrom"+*/"\tstart\tstop\t");//+"scaf");
325 //// System.out.println("Original:\t"+trueStrand+/*"\t"+trueChrom+*/"\t"+trueStart+"\t"+trueStop+"\t");//+new String(originalContig));
326 //// System.out.println("Mapped: \t"+ss.strand+/*"\t"+ss.chrom+*/"\t"+ss.start+"\t"+ss.stop+"\t");//+new String(contig));
327 //// System.out.println("cs: \t"+trueStrand+/*"\t"+trueChrom+*/"\t"+cstart+"\t"+cstop+"\t");//+new String(contig));
328 //
329 // if(ss.strand!=trueStrand){return false;}
330 // if(originalContig!=null){
331 // if(!Arrays.equals(originalContig, contig)){return false;}
332 // }else{
333 // if(ss.chrom!=trueChrom){return false;}
334 // }
335 //
336 // assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
337 // assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
338 //// return (absdif(ss.start, trueStart)<=thresh && absdif(ss.stop, trueStop)<=thresh);
339 // return (absdif(ss.start, cstart)<=thresh && absdif(ss.stop, cstop)<=thresh);
340 // }
341 //
342 //
343 // public static boolean isCorrectHitLoose(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
344 // byte[] originalContig, byte[] contig, int cstart){
345 // if(ss.strand!=trueStrand){return false;}
346 // if(originalContig!=null){
347 // if(!Arrays.equals(originalContig, contig)){return false;}
348 // }else{
349 // if(ss.chrom!=trueChrom){return false;}
350 // }
351 //
352 // assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
353 // assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
354 // int cstop=cstart+trueStop-trueStart;
355 //// return (absdif(ss.start, trueStart)<=thresh || absdif(ss.stop, trueStop)<=thresh);
356 // return (absdif(ss.start, cstart)<=thresh || absdif(ss.stop, cstop)<=thresh);
357 // }
358
359 private static final int absdif(int a, int b){
360 return a>b ? a-b : b-a;
361 }
362
363 public static FileFormat ffLoose=null;
364 public static FileFormat ffStrict=null;
365 public static TextStreamWriter tswLoose=null;
366 public static TextStreamWriter tswStrict=null;
367
368 public static int truePositiveStrict=0;
369 public static int falsePositiveStrict=0;
370
371 public static int truePositiveLoose=0;
372 public static int falsePositiveLoose=0;
373
374 public static int mapped=0;
375 public static int mappedRetained=0;
376 public static int unmapped=0;
377
378 public static int discarded=0;
379 public static int ambiguous=0;
380
381 public static long lines=0;
382 public static long primary=0;
383 public static long secondary=0;
384
385 public static int minQuality=3;
386
387 public static boolean parsecustom=true;
388 public static boolean printerr=false;
389
390 public static int THRESH2=20;
391 public static boolean BLASR=false;
392 public static boolean USE_BITSET=true;
393 public static BitSet seen=null;
394
395 }