| 1 |
package jgi; |
| 2 |
|
| 3 |
|
| 4 |
import java.io.File; |
| 5 |
import java.io.PrintStream; |
| 6 |
import java.util.Arrays; |
| 7 |
import java.util.HashMap; |
| 8 |
import java.util.HashSet; |
| 9 |
|
| 10 |
import dna.Data; |
| 11 |
import dna.Scaffold; |
| 12 |
import fileIO.ByteFile; |
| 13 |
import fileIO.ReadWrite; |
| 14 |
import fileIO.TextFile; |
| 15 |
import fileIO.TextStreamWriter; |
| 16 |
import shared.KillSwitch; |
| 17 |
import shared.LineParserS1; |
| 18 |
import shared.Parse; |
| 19 |
import shared.Parser; |
| 20 |
import shared.PreParser; |
| 21 |
import shared.Shared; |
| 22 |
import shared.Tools; |
| 23 |
import stream.Read; |
| 24 |
import stream.SamLine; |
| 25 |
import structures.LongList; |
| 26 |
import tracker.ReadStats; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class SamToEst { |
| 41 |
|
| 42 |
public static void main(String[] args){ |
| 43 |
|
| 44 |
{ |
| 45 |
PreParser pp=new PreParser(args, new Object() { }.getClass().getEnclosingClass(), false); |
| 46 |
args=pp.args; |
| 47 |
outstream=pp.outstream; |
| 48 |
} |
| 49 |
|
| 50 |
if(!ByteFile.FORCE_MODE_BF1 && !ByteFile.FORCE_MODE_BF2 && Shared.threads()>2){ |
| 51 |
ByteFile.FORCE_MODE_BF2=true; |
| 52 |
} |
| 53 |
|
| 54 |
ReadWrite.USE_UNPIGZ=true; |
| 55 |
|
| 56 |
String est=null, stats=null, ref=null, sam=null; |
| 57 |
float fractionForAllCaptured=0.98f; |
| 58 |
|
| 59 |
for(int i=0; i<args.length; i++){ |
| 60 |
String arg=args[i]; |
| 61 |
String[] split=arg.split("="); |
| 62 |
String a=split[0].toLowerCase(); |
| 63 |
String b=split.length>1 ? split[1] : null; |
| 64 |
|
| 65 |
if(Parser.parseCommonStatic(arg, a, b)){ |
| 66 |
|
| 67 |
}else if(Parser.parseZip(arg, a, b)){ |
| 68 |
|
| 69 |
}else if(Parser.parseQuality(arg, a, b)){ |
| 70 |
|
| 71 |
}else if(a.equals("in") || a.equals("input") || a.equals("in1") || a.equals("sam")){ |
| 72 |
sam=b; |
| 73 |
}else if(a.equals("out") || a.equals("output") || a.equals("stats")){ |
| 74 |
stats=b; |
| 75 |
}else if(a.equals("ref")){ |
| 76 |
ref=b; |
| 77 |
}else if(a.equals("est")){ |
| 78 |
est=b; |
| 79 |
}else if(a.equals("fraction")){ |
| 80 |
fractionForAllCaptured=Float.parseFloat(b); |
| 81 |
}else if(a.equals("append") || a.equals("app")){ |
| 82 |
append=ReadStats.append=Parse.parseBoolean(b); |
| 83 |
}else if(a.equals("overwrite") || a.equals("ow")){ |
| 84 |
overwrite=Parse.parseBoolean(b); |
| 85 |
}else if(sam==null && i==0 && Tools.looksLikeInputStream(arg)){ |
| 86 |
sam=arg; |
| 87 |
}else if(stats==null && i==1 && !arg.contains("=")){ |
| 88 |
stats=arg; |
| 89 |
}else{ |
| 90 |
outstream.println("Unknown parameter "+args[i]); |
| 91 |
assert(false) : "Unknown parameter "+args[i]; |
| 92 |
|
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
{ |
| 97 |
Parser.processQuality(); |
| 98 |
} |
| 99 |
|
| 100 |
if(stats==null){stats="stdout";} |
| 101 |
SamToEst ste=new SamToEst(sam, stats, ref, est, fractionForAllCaptured); |
| 102 |
ste.process(); |
| 103 |
|
| 104 |
|
| 105 |
Shared.closeStream(outstream); |
| 106 |
} |
| 107 |
|
| 108 |
public SamToEst(String in_, String stats_, String ref_, String est_, float fractionForAll_){ |
| 109 |
in=in_; |
| 110 |
stats=stats_; |
| 111 |
ref=ref_; |
| 112 |
estFile=est_; |
| 113 |
fractionForAll=fractionForAll_; |
| 114 |
} |
| 115 |
|
| 116 |
public void process(){ |
| 117 |
HashMap<String, EST> table=new HashMap<String, EST>(initialSize); |
| 118 |
TextFile tf=new TextFile(in, true); |
| 119 |
String line=null; |
| 120 |
|
| 121 |
String program=null; |
| 122 |
String version=null; |
| 123 |
|
| 124 |
boolean bbmap=false; |
| 125 |
float bbversion=-1; |
| 126 |
LineParserS1 lp=new LineParserS1('\t'); |
| 127 |
|
| 128 |
for(line=tf.nextLine(); line!=null && line.startsWith("@"); line=tf.nextLine()){ |
| 129 |
final String a=lp.parseString(1); |
| 130 |
|
| 131 |
if(a.equals("@SQ")){ |
| 132 |
Scaffold sc=new Scaffold(lp); |
| 133 |
|
| 134 |
|
| 135 |
refBases+=sc.length; |
| 136 |
refCount++; |
| 137 |
}else if(a.equals("@PG")){ |
| 138 |
lp.set(line); |
| 139 |
for(int i=1, terms=lp.terms(); i<terms; i++) { |
| 140 |
String s=lp.parseString(i); |
| 141 |
if(s.startsWith("PN:")){ |
| 142 |
String s2=s.substring(3); |
| 143 |
if(s2.equalsIgnoreCase("bbmap") || s2.startsWith("BBMap")){bbmap=true;} |
| 144 |
if(program==null){program=Data.forceIntern(s.substring(3));} |
| 145 |
}else if(s.startsWith("VN:")){ |
| 146 |
if(bbmap && bbversion<0){bbversion=Float.parseFloat(s.substring(3));} |
| 147 |
if(version==null){version=Data.forceIntern(s.substring(3));} |
| 148 |
} |
| 149 |
} |
| 150 |
}else if(a.equals("@RG")){ |
| 151 |
|
| 152 |
}else if(a.equals("@HD")){ |
| 153 |
|
| 154 |
}else if(a.equals("@CO")){ |
| 155 |
|
| 156 |
}else{ |
| 157 |
|
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
EST current=null; |
| 162 |
boolean err=false; |
| 163 |
for(; line!=null; line=tf.nextLine()){ |
| 164 |
|
| 165 |
if(line.length()==0){ |
| 166 |
|
| 167 |
}else if(line.charAt(0)=='@'){ |
| 168 |
if(!err){ |
| 169 |
outstream.println("Unexpected header line: "+line); |
| 170 |
outstream.println("This should not cause problems, and is probably due to concatenated sam files.\n" + |
| 171 |
"Supressing future unexpected header warnings."); |
| 172 |
err=true; |
| 173 |
} |
| 174 |
|
| 175 |
if(line.startsWith("@SQ")){ |
| 176 |
String[] split=line.split("\t"); |
| 177 |
Scaffold sc=new Scaffold(split); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
} |
| 184 |
}else{ |
| 185 |
|
| 186 |
SamLine sl=new SamLine(line); |
| 187 |
if(USE_SECONDARY || sl.primary()){ |
| 188 |
|
| 189 |
if(sl.mapped() && sl.cigar!=null){ |
| 190 |
String cigar=sl.cigar; |
| 191 |
if(cigar.contains("D") || cigar.contains("N")){ |
| 192 |
int len=0; |
| 193 |
for(int i=0; i<cigar.length(); i++){ |
| 194 |
char c=cigar.charAt(i); |
| 195 |
if(Tools.isDigit(c)){ |
| 196 |
len=(len*10)+(c-'0'); |
| 197 |
}else{ |
| 198 |
if(c=='D' || c=='N'){ |
| 199 |
introns.increment(len, 1); |
| 200 |
} |
| 201 |
len=0; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
String name=sl.qname; |
| 213 |
int x=name.lastIndexOf('_'); |
| 214 |
int part=1; |
| 215 |
|
| 216 |
if(x>5 && name.charAt(x-5)=='_' && name.charAt(x-4)=='p' && name.charAt(x-3)=='a' && name.charAt(x-2)=='r' && name.charAt(x-1)=='t'){ |
| 217 |
int partlen=name.length()-x-1; |
| 218 |
if(partlen>0 && partlen<6){ |
| 219 |
int p2=0; |
| 220 |
for(int i=x+1; i<name.length(); i++){ |
| 221 |
char c=name.charAt(i); |
| 222 |
int c2=c-'0'; |
| 223 |
if(c2<0 || c2>9){ |
| 224 |
p2=-1; |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
if(p2>-1){ |
| 229 |
part=p2; |
| 230 |
name=name.substring(0, x-5); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
}else{ |
| 248 |
|
| 249 |
} |
| 250 |
}else{ |
| 251 |
|
| 252 |
} |
| 253 |
}else{ |
| 254 |
|
| 255 |
} |
| 256 |
if(current==null || !current.name.equals(name)){ |
| 257 |
|
| 258 |
if(current!=null){addEst(current);} |
| 259 |
current=new EST(name); |
| 260 |
} |
| 261 |
current.add(sl); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
if(current!=null){addEst(current);} |
| 266 |
tf.close(); |
| 267 |
|
| 268 |
if(stats!=null){ |
| 269 |
final TextStreamWriter tsw=new TextStreamWriter(stats, overwrite, false, false); |
| 270 |
tsw.start(); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
boolean oldStyle=false; |
| 285 |
|
| 286 |
if(oldStyle){ |
| 287 |
tsw.println("ref:\t"+ref); |
| 288 |
tsw.println("est:\t"+estFile); |
| 289 |
tsw.println("sam:\t"+in); |
| 290 |
|
| 291 |
tsw.println("numRef:\t"+refCount+"\t"+refBases); |
| 292 |
tsw.println("numEst:\t"+estCount+"\t"+estBases); |
| 293 |
tsw.println("type\t#ests\t%ests\t#bases\t%bases"); |
| 294 |
}else{ |
| 295 |
|
| 296 |
tsw.println("ref_file="+ref); |
| 297 |
tsw.println("est_file="+estFile); |
| 298 |
tsw.println("sam_file="+in); |
| 299 |
|
| 300 |
tsw.println("n_ref_scaffolds="+refCount); |
| 301 |
tsw.println("n_ref_bases="+refBases); |
| 302 |
tsw.println("n_est="+estCount); |
| 303 |
tsw.println("n_est_bases="+estBases); |
| 304 |
tsw.println("type\tn_est\tpct_est\tn_bases\tpct_bases"); |
| 305 |
} |
| 306 |
|
| 307 |
double multE=100.0/estCount; |
| 308 |
double multB=100.0/estBases; |
| 309 |
|
| 310 |
double allBasesPct=multE*allBasesMapped; |
| 311 |
double mostBasesPct=multE*mostBasesMapped; |
| 312 |
double someBasesPct=multE*someBasesMapped; |
| 313 |
double noBasesPct=multE*noBasesMapped; |
| 314 |
double multiScaffoldPct=multE*multiScaffold; |
| 315 |
|
| 316 |
double allBasesPctB=multB*allBasesMappedB; |
| 317 |
double mostBasesPctB=multB*mostBasesMappedB; |
| 318 |
double someBasesPctB=multB*someBasesMappedB; |
| 319 |
double noBasesPctB=multB*noBasesMappedB; |
| 320 |
double multiScaffoldPctB=multB*multiScaffoldB; |
| 321 |
|
| 322 |
int min=0, max=0, median=0; |
| 323 |
long sum=0, count=0; |
| 324 |
for(int i=minIntron; i<introns.size; i++){ |
| 325 |
long x=introns.get(i); |
| 326 |
if(x>0){ |
| 327 |
if(min==0){min=i;} |
| 328 |
max=i; |
| 329 |
sum+=(i*x); |
| 330 |
count+=x; |
| 331 |
} |
| 332 |
} |
| 333 |
if(count>0){ |
| 334 |
long half=(count+1)/2; |
| 335 |
assert(half<=count); |
| 336 |
long count2=0; |
| 337 |
for(int i=0; count2<half; i++){ |
| 338 |
long x=introns.get(i); |
| 339 |
if(x>0){ |
| 340 |
count2+=x; |
| 341 |
median=i; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
tsw.println("all:\t"+allBasesMapped+"\t"+Tools.format("%.4f%%",allBasesPct)+"\t"+allBasesMappedB+"\t"+Tools.format("%.4f%%",allBasesPctB)); |
| 347 |
tsw.println("most:\t"+mostBasesMapped+"\t"+Tools.format("%.4f%%",mostBasesPct)+"\t"+mostBasesMappedB+"\t"+Tools.format("%.4f%%",mostBasesPctB)); |
| 348 |
tsw.println("some:\t"+someBasesMapped+"\t"+Tools.format("%.4f%%",someBasesPct)+"\t"+someBasesMappedB+"\t"+Tools.format("%.4f%%",someBasesPctB)); |
| 349 |
tsw.println("zero:\t"+noBasesMapped+"\t"+Tools.format("%.4f%%",noBasesPct)+"\t"+noBasesMappedB+"\t"+Tools.format("%.4f%%",noBasesPctB)); |
| 350 |
tsw.println("multi:\t"+multiScaffold+"\t"+Tools.format("%.4f%%",multiScaffoldPct)+"\t"+multiScaffoldB+"\t"+Tools.format("%.4f%%",multiScaffoldPctB)); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
tsw.println("introns\tmin\tmax\tmedian\taverage"); |
| 357 |
tsw.println(count+"\t"+min+"\t"+max+"\t"+median+"\t"+Tools.format("%.1f", (sum/(double)(Tools.max(count,1))))); |
| 358 |
|
| 359 |
tsw.poisonAndWait(); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
private void addEst(EST est){ |
| 364 |
|
| 365 |
estCount++; |
| 366 |
partCount+=est.parts; |
| 367 |
estBases+=est.length; |
| 368 |
estBasesMapped+=est.mappedLength; |
| 369 |
partCountMapped+=est.mappedParts; |
| 370 |
|
| 371 |
for(int i=0; i<est.msdicn.length; i++){ |
| 372 |
msdicnOverall[i]+=est.msdicn[i]; |
| 373 |
} |
| 374 |
|
| 375 |
if(est.scafnames.size()>1){ |
| 376 |
multiScaffold++; |
| 377 |
multiScaffoldB+=est.length; |
| 378 |
} |
| 379 |
|
| 380 |
if(est.mappedParts==est.parts){ |
| 381 |
|
| 382 |
allPartsMapped++; |
| 383 |
}else if(est.mappedParts>=Tools.max(1, est.parts/2)){ |
| 384 |
|
| 385 |
mostPartsMapped++; |
| 386 |
}else if(est.mappedParts>0){ |
| 387 |
|
| 388 |
somePartsMapped++; |
| 389 |
}else{ |
| 390 |
|
| 391 |
noPartsMapped++; |
| 392 |
} |
| 393 |
|
| 394 |
int match=est.match(); |
| 395 |
if(match>=(est.length*fractionForAll)){ |
| 396 |
|
| 397 |
allBasesMapped++; |
| 398 |
allBasesMappedB+=est.length; |
| 399 |
}else if(match>=est.length/2){ |
| 400 |
|
| 401 |
mostBasesMapped++; |
| 402 |
mostBasesMappedB+=est.length; |
| 403 |
}else if(match>0){ |
| 404 |
|
| 405 |
someBasesMapped++; |
| 406 |
someBasesMappedB+=est.length; |
| 407 |
}else{ |
| 408 |
|
| 409 |
noBasesMapped++; |
| 410 |
noBasesMappedB+=est.length; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
public final float fractionForAll; |
| 415 |
public final String in, stats, ref, estFile; |
| 416 |
|
| 417 |
public long refBases=0; |
| 418 |
public long estBases=0; |
| 419 |
public long estBasesMapped=0; |
| 420 |
|
| 421 |
public long refCount=0; |
| 422 |
public long estCount=0; |
| 423 |
public long partCount=0; |
| 424 |
public long partCountMapped=0; |
| 425 |
|
| 426 |
public long good=0, best=0, miss=0, zero=0; |
| 427 |
public long multiScaffold=0, multiScaffoldB=0; |
| 428 |
public long allPartsMapped=0, mostPartsMapped=0, somePartsMapped=0, noPartsMapped=0; |
| 429 |
public long allBasesMapped=0, mostBasesMapped=0, someBasesMapped=0, noBasesMapped=0; |
| 430 |
public long allBasesMappedB=0, mostBasesMappedB=0, someBasesMappedB=0, noBasesMappedB=0; |
| 431 |
public long[] msdicnOverall=new long[6]; |
| 432 |
public LongList introns=new LongList(1); |
| 433 |
|
| 434 |
public int initialSize=4096; |
| 435 |
public boolean ADD_FROM_REF=true; |
| 436 |
public boolean USE_SECONDARY=false; |
| 437 |
public static int minIntron=10; |
| 438 |
public static boolean overwrite=true; |
| 439 |
public static boolean append=false; |
| 440 |
|
| 441 |
|
| 442 |
static PrintStream outstream=System.err; |
| 443 |
|
| 444 |
public static class EST{ |
| 445 |
|
| 446 |
public EST(String name_){ |
| 447 |
name=name_; |
| 448 |
outstream.println("New EST: "+name); |
| 449 |
} |
| 450 |
|
| 451 |
public void add(SamLine sl){ |
| 452 |
outstream.println("Adding samline "+sl.qname+" to EST "+name); |
| 453 |
parts++; |
| 454 |
|
| 455 |
length+=sl.seq.length; |
| 456 |
if(sl.mapped()){ |
| 457 |
|
| 458 |
mappedLength+=sl.seq.length; |
| 459 |
mappedParts++; |
| 460 |
if(sl.cigar!=null){ |
| 461 |
String matchTag=sl.matchTag(); |
| 462 |
|
| 463 |
int[] temp; |
| 464 |
if(matchTag==null){ |
| 465 |
temp=SamLine.cigarToMsdic(sl.cigar); |
| 466 |
}else{ |
| 467 |
temp=Read.matchToMsdicn(matchTag.getBytes()); |
| 468 |
} |
| 469 |
for(int i=0; i<temp.length; i++){ |
| 470 |
msdicn[i]+=temp[i]; |
| 471 |
} |
| 472 |
} |
| 473 |
if(sl.rname()!=null){ |
| 474 |
scafnames.add(new String(sl.rname())); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
public int match(){return msdicn[0];} |
| 480 |
|
| 481 |
@Override |
| 482 |
public String toString(){ |
| 483 |
StringBuilder sb=new StringBuilder(); |
| 484 |
sb.append(name).append('\t'); |
| 485 |
sb.append(length).append('\t'); |
| 486 |
sb.append(mappedLength).append('\t'); |
| 487 |
sb.append(parts).append('\t'); |
| 488 |
sb.append(mappedParts).append('\t'); |
| 489 |
sb.append(Arrays.toString(msdicn)).append('\t'); |
| 490 |
sb.append(scafnames).append('\t'); |
| 491 |
return sb.toString(); |
| 492 |
} |
| 493 |
|
| 494 |
final String name; |
| 495 |
int length=0, mappedLength=0; |
| 496 |
int parts=0, mappedParts=0; |
| 497 |
HashSet<String> scafnames=new HashSet<String>(4); |
| 498 |
|
| 499 |
int[] msdicn=KillSwitch.allocInt1D(6); |
| 500 |
|
| 501 |
} |
| 502 |
|
| 503 |
} |