| 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 |
{ |
| 22 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 23 |
args=pp.args; |
| 24 |
|
| 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 |
|
| 47 |
|
| 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 |
|
| 104 |
if(c!='@'){ |
| 105 |
SamLine sl=new SamLine(s); |
| 106 |
lines++; |
| 107 |
|
| 108 |
int id=(parsecustom && seen!=null ? ((((int)sl.parseNumericId())<<1)|sl.pairnum()) : (int)lines); |
| 109 |
|
| 110 |
|
| 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 |
|
| 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()){ |
| 197 |
discarded++; |
| 198 |
unmapped++; |
| 199 |
}else if(r.ambiguous()){ |
| 200 |
|
| 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 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 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 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if(loose){ |
| 259 |
|
| 260 |
|
| 261 |
truePositiveLoose++; |
| 262 |
}else{ |
| 263 |
|
| 264 |
|
| 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 |
|
| 277 |
truePositiveStrict++; |
| 278 |
}else{ |
| 279 |
|
| 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 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 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 |
} |