| 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 |
{ |
| 21 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 22 |
args=pp.args; |
| 23 |
|
| 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 |
|
| 43 |
|
| 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 |
|
| 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 |
|
| 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!='@'){ |
| 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 |
|
| 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()){ |
| 200 |
discardedA[q]++; |
| 201 |
unmappedA[q]++; |
| 202 |
}else if(r.ambiguous()){ |
| 203 |
|
| 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 |
|
| 212 |
|
| 213 |
|
| 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 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
if(loose){ |
| 249 |
|
| 250 |
|
| 251 |
truePositiveLooseA[q]++; |
| 252 |
}else{ |
| 253 |
|
| 254 |
|
| 255 |
falsePositiveLooseA[q]++; |
| 256 |
} |
| 257 |
|
| 258 |
if(strict){ |
| 259 |
|
| 260 |
truePositiveStrictA[q]++; |
| 261 |
}else{ |
| 262 |
|
| 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 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 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 |
} |