Debian Codegraph @

current/align2/MakeRocCurve.java

raw symbols SCIP
1 package align2;
2
3 import java.io.File;
4 import java.util.BitSet;
5
6 import fileIO.TextFile;
7 import shared.Parse;
8 import shared.PreParser;
9 import shared.Timer;
10 import shared.Tools;
11 import stream.CustomHeader;
12 import stream.Read;
13 import stream.SamLine;
14
15 public class MakeRocCurve {
16
17
18 public static void main(String[] args){
19
20 {//Preparse block for help, config files, and outstream
21 PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false);
22 args=pp.args;
23 //outstream=pp.outstream;
24 }
25
26 Timer t=new Timer();
27 String in=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("ssaha2") || a.equals("subtractleadingclip")){
43 // SamLine.SUBTRACT_LEADING_SOFT_CLIP=Parse.parseBoolean(b);
44 }else if(a.equals("blasr")){
45 BLASR=Parse.parseBoolean(b);
46 }else if(a.equals("bitset")){
47 USE_BITSET=Parse.parseBoolean(b);
48 }else if(a.equals("thresh")){
49 THRESH2=Integer.parseInt(b);
50 }else if(a.equals("allowspaceslash")){
51 allowSpaceslash=Parse.parseBoolean(b);
52 }else if(a.equals("outputerrors")){
53 // OUTPUT_ERRORS=true;
54 }else if(i==0 && args[i].indexOf('=')<0 && (a.startsWith("stdin") || new File(args[0]).exists())){
55 in=args[0];
56 }else if(i==1 && args[i].indexOf('=')<0 && Tools.isDigit(a.charAt(0))){
57 reads=Parse.parseKMG(a);
58 }
59 }
60
61 if(USE_BITSET){
62 int x=400000;
63 if(reads>0 && reads<=Integer.MAX_VALUE){x=(int)reads;}
64 try {
65 seen=new BitSet(x);
66 } catch (Exception e) {
67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 System.out.println("Did not have enough memory to allocate bitset; duplicate mappings will not be detected.");
70 }
71 }
72
73 process(in);
74
75 System.out.println("ROC Curve for "+in);
76 System.out.println(header());
77 gradeList(reads);
78 t.stop();
79 System.err.println("Time: \t"+t);
80
81 }
82
83 public static void process(String samfile){
84 TextFile tf=new TextFile(samfile, false);
85
86 String s=null;
87 for(s=tf.nextLine(); s!=null; s=tf.nextLine()){
88 char c=s.charAt(0);
89 if(c!='@'/* && c!=' ' && c!='\t'*/){
90 SamLine sl=new SamLine(s);
91 final int id=((((int)sl.parseNumericId())<<1)|sl.pairnum());
92 assert(sl!=null);
93 Read r=sl.toRead(true);
94 if(r!=null){
95 r.samline=sl;
96 if(sl.primary() && (seen==null || !seen.get(id))){
97 if(seen!=null){seen.set(id);}
98 calcStatistics1(r, sl);
99 }
100 }else{
101 assert(false) : "'"+"'";
102 System.err.println("Bad read from line '"+s+"'");
103 }
104 // calcStatistics1(r);
105 }
106 }
107 tf.close();
108 }
109
110 public static String header(){
111 return "minScore\tmapped\tretained\ttruePositiveStrict\tfalsePositiveStrict\ttruePositiveLoose" +
112 "\tfalsePositiveLoose\tfalseNegative\tdiscarded\tambiguous";
113 }
114
115 public static void gradeList(long reads){
116
117 int truePositiveStrict=0;
118 int falsePositiveStrict=0;
119
120 int truePositiveLoose=0;
121 int falsePositiveLoose=0;
122
123 int mapped=0;
124 int mappedRetained=0;
125 int unmapped=0;
126
127 int discarded=0;
128 int ambiguous=0;
129
130 int primary=0;
131
132
133 for(int q=truePositiveStrictA.length-1; q>=0; q--){
134 if(mappedA[q]>0 || unmappedA[q]>0){
135 truePositiveStrict+=truePositiveStrictA[q];
136 falsePositiveStrict+=falsePositiveStrictA[q];
137 truePositiveLoose+=truePositiveLooseA[q];
138 falsePositiveLoose+=falsePositiveLooseA[q];
139 mapped+=mappedA[q];
140 mappedRetained+=mappedRetainedA[q];
141 unmapped+=unmappedA[q];
142 discarded+=discardedA[q];
143 ambiguous+=ambiguousA[q];
144 primary+=primaryA[q];
145
146 double tmult=100d/reads;
147
148 double mappedB=mapped*tmult;
149 double retainedB=mappedRetained*tmult;
150 double truePositiveStrictB=truePositiveStrict*tmult;
151 double falsePositiveStrictB=falsePositiveStrict*tmult;
152 double truePositiveLooseB=truePositiveLoose*tmult;
153 double falsePositiveLooseB=falsePositiveLoose*tmult;
154 double falseNegativeB=(reads-mapped)*tmult;
155 double discardedB=discarded*tmult;
156 double ambiguousB=ambiguous*tmult;
157
158 StringBuilder sb=new StringBuilder();
159 sb.append(q);
160 sb.append('\t');
161 sb.append(Tools.format("%.4f", mappedB));
162 sb.append('\t');
163 sb.append(Tools.format("%.4f", retainedB));
164 sb.append('\t');
165 sb.append(Tools.format("%.4f", truePositiveStrictB));
166 sb.append('\t');
167 sb.append(Tools.format("%.4f", falsePositiveStrictB));
168 sb.append('\t');
169 sb.append(Tools.format("%.4f", truePositiveLooseB));
170 sb.append('\t');
171 sb.append(Tools.format("%.4f", falsePositiveLooseB));
172 sb.append('\t');
173 sb.append(Tools.format("%.4f", falseNegativeB));
174 sb.append('\t');
175 sb.append(Tools.format("%.4f", discardedB));
176 sb.append('\t');
177 sb.append(Tools.format("%.4f", ambiguousB));
178
179 System.out.println(sb);
180 }else{
181 assert(truePositiveStrictA[q]==0) : q;
182 assert(falsePositiveStrictA[q]==0) : q;
183 assert(truePositiveLooseA[q]==0) : q;
184 assert(falsePositiveLooseA[q]==0) : q;
185 }
186
187 }
188 }
189
190 public static void calcStatistics1(final Read r, SamLine sl){
191
192 int q=r.mapScore;
193
194 int THRESH=0;
195 primaryA[q]++;
196 if(q<0){q=0;}
197 if(q>=discardedA.length){q=discardedA.length-1;}
198
199 if(r.discarded()/* || r.mapScore==0*/){
200 discardedA[q]++;
201 unmappedA[q]++;
202 }else if(r.ambiguous()){
203 // assert(r.mapped()) : "\n"+r+"\n"+sl+"\n";
204 if(r.mapped()){mappedA[q]++;}
205 ambiguousA[q]++;
206 }else if(r.mapScore<1){
207 unmappedA[q]++;
208 }else if(!r.mapped()){
209 unmappedA[q]++;
210 }
211 // else if(r.mapScore<=minQuality){
212 // if(r.mapped()){mappedA[q]++;}
213 // ambiguousA[q]++;
214 // }
215 else{
216
217 mappedA[q]++;
218 mappedRetainedA[q]++;
219
220 if(parsecustom){
221 CustomHeader h=new CustomHeader(sl.qname, sl.pairnum());
222 boolean strict=isCorrectHit(sl, h);
223 boolean loose=isCorrectHitLoose(sl, h);
224
225 // SiteScore os=r.originalSite;
226 // int trueChrom=os.chrom;
227 // byte trueStrand=os.strand;
228 // int trueStart=os.start;
229 // int trueStop=os.stop;
230 // SiteScore ss=new SiteScore(r.chrom, r.strand(), r.start, r.stop, 0, 0);
231 // byte[] originalContig=sl.originalContig();
232 // if(BLASR){
233 // originalContig=(originalContig==null || Tools.indexOf(originalContig, (byte)'/')<0 ? originalContig :
234 // KillSwitch.copyOfRange(originalContig, 0, Tools.lastIndexOf(originalContig, (byte)'/')));
235 // }
236 // int cstart=sl.originalContigStart();
237 //
238 // boolean strict=isCorrectHit(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH, originalContig, sl.rname(), cstart);
239 // boolean loose=isCorrectHitLoose(ss, trueChrom, trueStrand, trueStart, trueStop, THRESH+THRESH2, originalContig, sl.rname(), cstart);
240 //
241 // // if(!strict){
242 // // System.out.println(ss+", "+new String(originalContig)+", "+new String(sl.rname()));
243 // // assert(false);
244 // // }
245 //
246 // // System.out.println("loose = "+loose+" for "+r.toText());
247
248 if(loose){
249 // System.err.println("TPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
250 // +ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
251 truePositiveLooseA[q]++;
252 }else{
253 // System.err.println("FPL\t"+trueChrom+", "+trueStrand+", "+trueStart+", "+trueStop+"\tvs\t"
254 // +ss.chrom+", "+ss.strand+", "+ss.start+", "+ss.stop);
255 falsePositiveLooseA[q]++;
256 }
257
258 if(strict){
259 // System.err.println("TPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
260 truePositiveStrictA[q]++;
261 }else{
262 // System.err.println("FPS\t"+trueStart+", "+trueStop+"\tvs\t"+ss.start+", "+ss.stop);
263 falsePositiveStrictA[q]++;
264 }
265 }
266 }
267 }
268
269 public static boolean isCorrectHit(SamLine sl, CustomHeader h){
270 if(!sl.mapped()){return false;}
271 if(h.strand!=sl.strand()){return false;}
272 int start=sl.start(true, true);
273 int stop=sl.stop(start, true, true);
274 if(h.start!=start){return false;}
275 if(h.stop!=stop){return false;}
276 if(!h.rname.equals(sl.rnameS())){return false;}
277 return true;
278 }
279
280 public static boolean isCorrectHitLoose(SamLine sl, CustomHeader h){
281 if(!sl.mapped()){return false;}
282 if(h.strand!=sl.strand()){return false;}
283 int start=sl.start(true, true);
284 int stop=sl.stop(start, true, true);
285 if(!h.rname.equals(sl.rnameS())){return false;}
286
287 if(h.start!=start){return false;}
288 if(h.stop!=stop){return false;}
289 return(absdif(h.start, start)<=THRESH2 || absdif(h.stop, stop)<=THRESH2);
290 }
291
292
293
294
295 // public static boolean isCorrectHit(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
296 // byte[] originalContig, byte[] contig, int cstart){
297 // if(ss.strand!=trueStrand){return false;}
298 // if(originalContig!=null){
299 // if(!Arrays.equals(originalContig, contig)){
300 // if(allowSpaceslash && originalContig.length==contig.length+3 && Tools.startsWith(originalContig, contig) &&
301 // (Character.isWhitespace(originalContig[originalContig.length-3]))){
302 // //do nothing
303 // }else{
304 // return false;
305 // }
306 // }
307 // }else{
308 // if(ss.chrom!=trueChrom){return false;}
309 // }
310 //
311 // assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
312 // assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
313 // int cstop=cstart+trueStop-trueStart;
314 //// return (absdif(ss.start, trueStart)<=thresh && absdif(ss.stop, trueStop)<=thresh);
315 // return (absdif(ss.start, cstart)<=thresh && absdif(ss.stop, cstop)<=thresh);
316 // }
317 //
318 //
319 // public static boolean isCorrectHitLoose(SiteScore ss, int trueChrom, byte trueStrand, int trueStart, int trueStop, int thresh,
320 // byte[] originalContig, byte[] contig, int cstart){
321 // if(ss.strand!=trueStrand){return false;}
322 // if(originalContig!=null){
323 // if(!Arrays.equals(originalContig, contig)){return false;}
324 // }else{
325 // if(ss.chrom!=trueChrom){return false;}
326 // }
327 //
328 // assert(ss.stop>ss.start) : ss.toText()+", "+trueStart+", "+trueStop;
329 // assert(trueStop>trueStart) : ss.toText()+", "+trueStart+", "+trueStop;
330 // int cstop=cstart+trueStop-trueStart;
331 //// return (absdif(ss.start, trueStart)<=thresh || absdif(ss.stop, trueStop)<=thresh);
332 // return (absdif(ss.start, cstart)<=thresh || absdif(ss.stop, cstop)<=thresh);
333 // }
334
335 private static final int absdif(int a, int b){
336 return a>b ? a-b : b-a;
337 }
338
339 public static int truePositiveStrictA[]=new int[1000];
340 public static int falsePositiveStrictA[]=new int[1000];
341
342 public static int truePositiveLooseA[]=new int[1000];
343 public static int falsePositiveLooseA[]=new int[1000];
344
345 public static int mappedA[]=new int[1000];
346 public static int mappedRetainedA[]=new int[1000];
347 public static int unmappedA[]=new int[1000];
348
349 public static int discardedA[]=new int[1000];
350 public static int ambiguousA[]=new int[1000];
351
352 public static int primaryA[]=new int[1000];
353
354 public static boolean parsecustom=true;
355
356 public static int THRESH2=20;
357 public static boolean BLASR=false;
358 public static boolean USE_BITSET=true;
359 public static BitSet seen=null;
360 public static boolean allowSpaceslash=true;
361
362 }