| 1 |
package var2; |
| 2 |
|
| 3 |
import java.io.Serializable; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.Arrays; |
| 6 |
import java.util.Random; |
| 7 |
|
| 8 |
import align2.QualityTools; |
| 9 |
import dna.AminoAcid; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import shared.KillSwitch; |
| 12 |
import shared.Parse; |
| 13 |
import shared.Parser; |
| 14 |
import shared.Shared; |
| 15 |
import shared.Timer; |
| 16 |
import shared.Tools; |
| 17 |
import stream.Read; |
| 18 |
import stream.SamLine; |
| 19 |
import structures.ByteBuilder; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class Var implements Comparable<Var>, Serializable, Cloneable { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private static final long serialVersionUID = 3328626403863586829L; |
| 34 |
|
| 35 |
|
| 36 |
public static void main(String[] args){ |
| 37 |
if(args.length>1){ |
| 38 |
for(int i=1; i<args.length; i++){ |
| 39 |
String arg=args[i]; |
| 40 |
String[] split=arg.split("="); |
| 41 |
String a=split[0].toLowerCase(); |
| 42 |
String b=split.length>1 ? split[1] : null; |
| 43 |
Parser.parseCommonStatic(arg, a, b); |
| 44 |
} |
| 45 |
} |
| 46 |
Timer t=new Timer(); |
| 47 |
VarMap vmap; |
| 48 |
FileFormat ff=FileFormat.testInput(args[0], FileFormat.VAR, null, true, true); |
| 49 |
if(ff.vcf()){ |
| 50 |
ScafMap smap=ScafMap.loadVcfHeader(ff); |
| 51 |
vmap=VcfLoader.loadVcfFile(args[0], smap, false, false); |
| 52 |
}else{ |
| 53 |
vmap=VcfLoader.loadVarFile(args[0], null); |
| 54 |
} |
| 55 |
t.stop("Loaded "+vmap.size()+" variants.\nTime: \t"); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
@Override |
| 63 |
public Var clone(){ |
| 64 |
try { |
| 65 |
return (Var)super.clone(); |
| 66 |
} catch (CloneNotSupportedException e) { |
| 67 |
|
| 68 |
e.printStackTrace(); |
| 69 |
throw new RuntimeException(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public Var(int scafnum_, int start_, int stop_, int allele_, int type_){ |
| 74 |
this(scafnum_, start_, stop_, AL_MAP[allele_], type_); |
| 75 |
} |
| 76 |
|
| 77 |
public Var(Var v) { |
| 78 |
this(v.scafnum, v.start, v.stop, v.allele, v.type); |
| 79 |
} |
| 80 |
|
| 81 |
public Var(int scafnum_, int start_, int stop_, byte[] allele_, int type_){ |
| 82 |
scafnum=scafnum_; |
| 83 |
start=start_; |
| 84 |
stop=stop_; |
| 85 |
allele=allele_; |
| 86 |
type=type_; |
| 87 |
hashcode=hash(); |
| 88 |
|
| 89 |
assert(allele.length>1 || allele==AL_0 || |
| 90 |
allele==AL_A || allele==AL_C || allele==AL_G || allele==AL_T || allele==AL_N) : new String(new String(allele_)+", "+allele_.length); |
| 91 |
assert(start<=stop) : "\n"+Var.toBasicHeader()+"\n"+this+"\n"; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
public Var(final byte[] line, final byte delimiter){ |
| 103 |
int a=0, b=0; |
| 104 |
|
| 105 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 106 |
assert(b>a) : "Missing field 0: "+new String(line); |
| 107 |
scafnum=Parse.parseInt(line, a, b); |
| 108 |
b++; |
| 109 |
a=b; |
| 110 |
|
| 111 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 112 |
assert(b>a) : "Missing field 1: "+new String(line); |
| 113 |
start=Parse.parseInt(line, a, b); |
| 114 |
b++; |
| 115 |
a=b; |
| 116 |
|
| 117 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 118 |
assert(b>a) : "Missing field 2: "+new String(line); |
| 119 |
stop=Parse.parseInt(line, a, b); |
| 120 |
b++; |
| 121 |
a=b; |
| 122 |
|
| 123 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 124 |
assert(b>a) : "Missing field 3: "+new String(line); |
| 125 |
type=typeInitialArray[line[a]]; |
| 126 |
b++; |
| 127 |
a=b; |
| 128 |
|
| 129 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 130 |
assert(b>=a) : "Missing field 4: "+new String(line); |
| 131 |
if(b==a){allele=AL_0;} |
| 132 |
else if(b==a+1){allele=AL_MAP[line[a]];} |
| 133 |
else{allele=Arrays.copyOfRange(line, a, b);} |
| 134 |
b++; |
| 135 |
a=b; |
| 136 |
|
| 137 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 138 |
assert(b>a) : "Missing field 5: "+new String(line); |
| 139 |
r1plus=Parse.parseInt(line, a, b); |
| 140 |
b++; |
| 141 |
a=b; |
| 142 |
|
| 143 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 144 |
assert(b>a) : "Missing field 6: "+new String(line); |
| 145 |
r1minus=Parse.parseInt(line, a, b); |
| 146 |
b++; |
| 147 |
a=b; |
| 148 |
|
| 149 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 150 |
assert(b>a) : "Missing field 7: "+new String(line); |
| 151 |
r2plus=Parse.parseInt(line, a, b); |
| 152 |
b++; |
| 153 |
a=b; |
| 154 |
|
| 155 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 156 |
assert(b>a) : "Missing field 8: "+new String(line); |
| 157 |
r2minus=Parse.parseInt(line, a, b); |
| 158 |
b++; |
| 159 |
a=b; |
| 160 |
|
| 161 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 162 |
assert(b>a) : "Missing field 9: "+new String(line); |
| 163 |
properPairCount=Parse.parseInt(line, a, b); |
| 164 |
b++; |
| 165 |
a=b; |
| 166 |
|
| 167 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 168 |
assert(b>a) : "Missing field 10: "+new String(line); |
| 169 |
lengthSum=Parse.parseLong(line, a, b); |
| 170 |
b++; |
| 171 |
a=b; |
| 172 |
|
| 173 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 174 |
assert(b>a) : "Missing field 11: "+new String(line); |
| 175 |
mapQSum=Parse.parseLong(line, a, b); |
| 176 |
b++; |
| 177 |
a=b; |
| 178 |
|
| 179 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 180 |
assert(b>a) : "Missing field 12: "+new String(line); |
| 181 |
mapQMax=Parse.parseInt(line, a, b); |
| 182 |
b++; |
| 183 |
a=b; |
| 184 |
|
| 185 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 186 |
assert(b>a) : "Missing field 13: "+new String(line); |
| 187 |
baseQSum=Parse.parseLong(line, a, b); |
| 188 |
b++; |
| 189 |
a=b; |
| 190 |
|
| 191 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 192 |
assert(b>a) : "Missing field 14: "+new String(line); |
| 193 |
baseQMax=Parse.parseInt(line, a, b); |
| 194 |
b++; |
| 195 |
a=b; |
| 196 |
|
| 197 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 198 |
assert(b>a) : "Missing field 15: "+new String(line); |
| 199 |
endDistSum=Parse.parseLong(line, a, b); |
| 200 |
b++; |
| 201 |
a=b; |
| 202 |
|
| 203 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 204 |
assert(b>a) : "Missing field 16: "+new String(line); |
| 205 |
endDistMax=Parse.parseInt(line, a, b); |
| 206 |
b++; |
| 207 |
a=b; |
| 208 |
|
| 209 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 210 |
assert(b>a) : "Missing field 17: "+new String(line); |
| 211 |
idSum=Parse.parseLong(line, a, b); |
| 212 |
b++; |
| 213 |
a=b; |
| 214 |
|
| 215 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 216 |
assert(b>a) : "Missing field 18: "+new String(line); |
| 217 |
idMax=Parse.parseInt(line, a, b); |
| 218 |
b++; |
| 219 |
a=b; |
| 220 |
|
| 221 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 222 |
assert(b>a) : "Missing field 19: "+new String(line); |
| 223 |
coverage=Parse.parseInt(line, a, b); |
| 224 |
b++; |
| 225 |
a=b; |
| 226 |
|
| 227 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 228 |
assert(b>a) : "Missing field 20: "+new String(line); |
| 229 |
minusCoverage=Parse.parseInt(line, a, b); |
| 230 |
b++; |
| 231 |
a=b; |
| 232 |
|
| 233 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 234 |
assert(b>a) : "Missing field 21: "+new String(line); |
| 235 |
nearbyVarCount=Parse.parseInt(line, a, b); |
| 236 |
b++; |
| 237 |
a=b; |
| 238 |
|
| 239 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 240 |
assert(b>a) : "Missing field 22: "+new String(line); |
| 241 |
flagged=Parse.parseInt(line, a, b)>0; |
| 242 |
b++; |
| 243 |
a=b; |
| 244 |
|
| 245 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 246 |
|
| 247 |
|
| 248 |
b++; |
| 249 |
a=b; |
| 250 |
|
| 251 |
while(b<line.length && line[b]!=delimiter){b++;} |
| 252 |
if(b>a){ |
| 253 |
|
| 254 |
b++; |
| 255 |
a=b; |
| 256 |
} |
| 257 |
|
| 258 |
hashcode=hash(); |
| 259 |
assert(allele.length>1 || allele==AL_0 || |
| 260 |
allele==AL_A || allele==AL_C || allele==AL_G || allele==AL_T || allele==AL_N); |
| 261 |
assert(start<=stop) : this.toString(); |
| 262 |
assert(type>=LJUNCT || type==type_old()) : type+", "+type_old()+", "+this.toString(); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
public static Var fromVCF(byte[] line, ScafMap scafMap, boolean parseCoverage, boolean parseExtended) { |
| 267 |
int a=0, b=0; |
| 268 |
|
| 269 |
|
| 270 |
while(b<line.length && line[b]!='\t'){b++;} |
| 271 |
assert(b>a) : "Missing field 0: "+new String(line); |
| 272 |
String scaf=new String(line, a, b-a); |
| 273 |
b++; |
| 274 |
a=b; |
| 275 |
|
| 276 |
|
| 277 |
while(b<line.length && line[b]!='\t'){b++;} |
| 278 |
assert(b>a) : "Missing field 1: "+new String(line); |
| 279 |
int pos=Parse.parseInt(line, a, b); |
| 280 |
b++; |
| 281 |
a=b; |
| 282 |
|
| 283 |
|
| 284 |
while(b<line.length && line[b]!='\t'){b++;} |
| 285 |
assert(b>a) : "Missing field 2: "+new String(line); |
| 286 |
|
| 287 |
b++; |
| 288 |
a=b; |
| 289 |
|
| 290 |
|
| 291 |
while(b<line.length && line[b]!='\t'){b++;} |
| 292 |
assert(b>a) : "Missing field 3: "+new String(line); |
| 293 |
|
| 294 |
int reflen=line[a]=='.' ? 0 : b-a; |
| 295 |
b++; |
| 296 |
a=b; |
| 297 |
|
| 298 |
|
| 299 |
while(b<line.length && line[b]!='\t'){b++;} |
| 300 |
assert(b>a) : "Missing field 4: "+new String(line); |
| 301 |
byte[] alt; |
| 302 |
if(b<=a+1){alt=AL_MAP[line[a]];} |
| 303 |
else{alt=Arrays.copyOfRange(line, a, b);} |
| 304 |
b++; |
| 305 |
a=b; |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
int infoStart=b; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
final int start; |
| 318 |
if(alt.length!=reflen && alt.length>0){ |
| 319 |
alt=Arrays.copyOfRange(alt, 1, alt.length); |
| 320 |
start=pos; |
| 321 |
if(alt.length==0){alt=AL_0;} |
| 322 |
else if(alt.length==1 && AL_MAP[alt[0]]!=null){alt=AL_MAP[alt[0]];} |
| 323 |
if(reflen==1){reflen--;} |
| 324 |
}else{ |
| 325 |
start=pos-1; |
| 326 |
} |
| 327 |
final int readlen=(alt==null || alt.length==0 || alt[0]=='.' ? 0 : alt.length); |
| 328 |
final int stop=start+reflen; |
| 329 |
assert(scaf!=null); |
| 330 |
assert(scafMap!=null); |
| 331 |
final int scafNum=scafMap.getNumber(scaf); |
| 332 |
assert(scafNum>=0) : scaf+"\n"+scafMap.keySet()+"\n"+scafMap.altKeySet()+"\n"; |
| 333 |
|
| 334 |
|
| 335 |
int type=-1; |
| 336 |
if(parseExtended){ |
| 337 |
type=Tools.max(type, parseVcfIntDelimited(line, "TYP=", infoStart)); |
| 338 |
if(type<0){type=typeStartStop(start, stop, alt);} |
| 339 |
}else{ |
| 340 |
type=typeStartStop(start, stop, alt); |
| 341 |
} |
| 342 |
Var v=new Var(scafNum, start, stop, alt, type); |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
if(parseCoverage){ |
| 347 |
infoStart=Tools.indexOfDelimited(line, "R1P=", infoStart, (byte)';'); |
| 348 |
|
| 349 |
|
| 350 |
int r1p=Tools.max(0, parseVcfIntDelimited(line, "R1P=", infoStart)); |
| 351 |
int r1m=Tools.max(0, parseVcfIntDelimited(line, "R1M=", infoStart)); |
| 352 |
int r2p=Tools.max(0, parseVcfIntDelimited(line, "R2P=", infoStart)); |
| 353 |
int r2m=Tools.max(0, parseVcfIntDelimited(line, "R2M=", infoStart)); |
| 354 |
|
| 355 |
|
| 356 |
int cov=parseVcfIntDelimited(line, "DP=", infoStart); |
| 357 |
assert(cov>0) : new String(line, infoStart, line.length-infoStart); |
| 358 |
int mcov=parseVcfIntDelimited(line, "MCOV=", infoStart); |
| 359 |
|
| 360 |
v.coverage=cov; |
| 361 |
v.minusCoverage=mcov; |
| 362 |
v.r1plus=r1p; |
| 363 |
v.r1minus=r1m; |
| 364 |
v.r2plus=r2p; |
| 365 |
v.r2minus=r2m; |
| 366 |
} |
| 367 |
|
| 368 |
if(parseExtended){ |
| 369 |
infoStart=Tools.indexOfDelimited(line, "PPC=", infoStart, (byte)';'); |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
int pc=Tools.max(0, parseVcfIntDelimited(line, "PPC=", infoStart)); |
| 374 |
|
| 375 |
|
| 376 |
double raf=parseVcfDoubleDelimited(line, "RAF=", infoStart); |
| 377 |
long ls=Tools.max(0, parseVcfLongDelimited(line, "LS=", infoStart)); |
| 378 |
|
| 379 |
|
| 380 |
long mqs=Tools.max(0, parseVcfLongDelimited(line, "MQS=", infoStart)); |
| 381 |
int mqm=Tools.max(0, parseVcfIntDelimited(line, "MQM=", infoStart)); |
| 382 |
long bqs=Tools.max(0, parseVcfLongDelimited(line, "BQS=", infoStart)); |
| 383 |
int bqm=Tools.max(0, parseVcfIntDelimited(line, "BQM=", infoStart)); |
| 384 |
|
| 385 |
|
| 386 |
long eds=Tools.max(0, parseVcfLongDelimited(line, "EDS=", infoStart)); |
| 387 |
int edm=Tools.max(0, parseVcfIntDelimited(line, "EDM=", infoStart)); |
| 388 |
long ids=Tools.max(0, parseVcfLongDelimited(line, "IDS=", infoStart)); |
| 389 |
int idm=Tools.max(0, parseVcfIntDelimited(line, "IDM=", infoStart)); |
| 390 |
|
| 391 |
|
| 392 |
int nvc=Tools.max(0, parseVcfIntDelimited(line, "NVC=", infoStart)); |
| 393 |
int flg=Tools.max(0, parseVcfIntDelimited(line, "FLG=", infoStart)); |
| 394 |
|
| 395 |
|
| 396 |
v.properPairCount=pc; |
| 397 |
v.revisedAlleleFraction=raf; |
| 398 |
v.lengthSum=ls; |
| 399 |
v.mapQSum=mqs; |
| 400 |
v.mapQMax=mqm; |
| 401 |
v.baseQSum=bqs; |
| 402 |
v.baseQMax=bqm; |
| 403 |
v.endDistSum=eds; |
| 404 |
v.endDistMax=edm; |
| 405 |
v.idSum=ids; |
| 406 |
v.idMax=idm; |
| 407 |
v.nearbyVarCount=nvc; |
| 408 |
v.flagged=(flg>0); |
| 409 |
} |
| 410 |
|
| 411 |
return v; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
private static int parseVcfIntDelimited(byte[] line, String query, int start){ |
| 416 |
return (int)Tools.min(Integer.MAX_VALUE, parseVcfLongDelimited(line, query, start)); |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
private static long parseVcfLongDelimited(byte[] line, String query, final int start){ |
| 421 |
int loc=Tools.indexOfDelimited(line, query, start, (byte)';'); |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
if(loc<0){return -1;} |
| 426 |
long current=0; |
| 427 |
long mult=1; |
| 428 |
if(loc>0){ |
| 429 |
if(line[loc+query.length()]=='-'){mult=-1; loc++;} |
| 430 |
for(int i=loc+query.length(); i<line.length; i++){ |
| 431 |
final byte x=line[i]; |
| 432 |
if(Tools.isDigit(x)){ |
| 433 |
current=current*10+(x-'0'); |
| 434 |
}else{ |
| 435 |
assert(x==tab || x==colon) : x+", "+query+", "+new String(line, loc, i-loc+1); |
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
return mult*current; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
private static double parseVcfDoubleDelimited(byte[] line, String query, int start){ |
| 445 |
int loc=Tools.indexOfDelimited(line, query, start, (byte)';'); |
| 446 |
if(loc<0){return -1;} |
| 447 |
if(loc>0){ |
| 448 |
loc=loc+query.length(); |
| 449 |
int loc2=loc+1; |
| 450 |
while(loc2<line.length){ |
| 451 |
byte b=line[loc2]; |
| 452 |
if(b==tab || b==colon){break;} |
| 453 |
loc2++; |
| 454 |
} |
| 455 |
return Parse.parseDouble(line, loc, loc2); |
| 456 |
} |
| 457 |
return -1; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
public Var clear() { |
| 465 |
coverage=-1; |
| 466 |
minusCoverage=-1; |
| 467 |
|
| 468 |
r1plus=0; |
| 469 |
r1minus=0; |
| 470 |
r2plus=0; |
| 471 |
r2minus=0; |
| 472 |
properPairCount=0; |
| 473 |
|
| 474 |
mapQSum=0; |
| 475 |
mapQMax=0; |
| 476 |
|
| 477 |
baseQSum=0; |
| 478 |
baseQMax=0; |
| 479 |
|
| 480 |
endDistSum=0; |
| 481 |
endDistMax=0; |
| 482 |
|
| 483 |
idSum=0; |
| 484 |
idMax=0; |
| 485 |
|
| 486 |
lengthSum=0; |
| 487 |
|
| 488 |
revisedAlleleFraction=-1; |
| 489 |
nearbyVarCount=-1; |
| 490 |
forced=false; |
| 491 |
flagged=false; |
| 492 |
return this; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
public void addCoverage(Var b){ |
| 497 |
assert(this.equals(b)); |
| 498 |
coverage+=b.coverage; |
| 499 |
minusCoverage+=b.minusCoverage; |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
public void add(Var b){ |
| 504 |
final int oldReads=alleleCount(); |
| 505 |
|
| 506 |
|
| 507 |
assert(oldReads==0 || baseQSum/oldReads<=60) : this; |
| 508 |
|
| 509 |
assert(this.equals(b)); |
| 510 |
r1plus+=b.r1plus; |
| 511 |
r1minus+=b.r1minus; |
| 512 |
r2plus+=b.r2plus; |
| 513 |
r2minus+=b.r2minus; |
| 514 |
properPairCount+=b.properPairCount; |
| 515 |
lengthSum+=b.lengthSum; |
| 516 |
|
| 517 |
mapQSum+=b.mapQSum; |
| 518 |
mapQMax=Tools.max(mapQMax, b.mapQMax); |
| 519 |
baseQSum+=b.baseQSum; |
| 520 |
baseQMax=Tools.max(baseQMax, b.baseQMax); |
| 521 |
|
| 522 |
endDistSum+=b.endDistSum; |
| 523 |
endDistMax=Tools.max(endDistMax, b.endDistMax); |
| 524 |
|
| 525 |
idSum+=b.idSum; |
| 526 |
idMax=Tools.max(idMax, b.idMax); |
| 527 |
|
| 528 |
|
| 529 |
assert(alleleCount()>=oldReads) : "\n"+this+"\n"+b; |
| 530 |
assert(alleleCount()==oldReads+b.alleleCount()) : "\n"+this+"\n"+b; |
| 531 |
assert(alleleCount()==0 || baseQSum/alleleCount()<=60) : "\n"+this+"\n"+b; |
| 532 |
} |
| 533 |
|
| 534 |
public void add(Read r){ |
| 535 |
final SamLine sl=r.samline; |
| 536 |
final int bstart=calcBstart(r, sl); |
| 537 |
final int bstop=calcBstop(bstart, r); |
| 538 |
add(r, bstart, bstop); |
| 539 |
} |
| 540 |
|
| 541 |
public void add(Read r, final int bstart, final int bstop){ |
| 542 |
|
| 543 |
final int oldReads=alleleCount(); |
| 544 |
final SamLine sl=r.samline; |
| 545 |
|
| 546 |
if(sl.strand()==0){ |
| 547 |
if(sl.pairnum()==0){ |
| 548 |
r1plus++; |
| 549 |
}else{ |
| 550 |
r2plus++; |
| 551 |
} |
| 552 |
}else{ |
| 553 |
if(sl.pairnum()==0){ |
| 554 |
r1minus++; |
| 555 |
}else{ |
| 556 |
r2minus++; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
lengthSum+=r.length(); |
| 561 |
properPairCount+=(sl.properPair() ? 1 : 0); |
| 562 |
mapQSum+=sl.mapq; |
| 563 |
mapQMax=Tools.max(mapQMax, sl.mapq); |
| 564 |
|
| 565 |
int baseQ=calcBaseQ(bstart, bstop, r, sl); |
| 566 |
baseQSum+=baseQ; |
| 567 |
baseQMax=Tools.max(baseQMax, baseQ); |
| 568 |
|
| 569 |
int endDist=calcEndDist(bstart, bstop, r); |
| 570 |
endDistSum+=endDist; |
| 571 |
endDistMax=Tools.max(endDistMax, endDist); |
| 572 |
|
| 573 |
int id=(int)(1000*Read.identitySkewed(r.match, false, false, false, true)); |
| 574 |
idSum+=id; |
| 575 |
idMax=Tools.max(idMax, id); |
| 576 |
|
| 577 |
assert(alleleCount()>0) : this; |
| 578 |
assert(alleleCount()==oldReads+1) : this; |
| 579 |
assert(baseQSum/alleleCount()<=60) : this; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public static ArrayList<Var> toVars(Read r, SamLine sl, boolean callNs, ScafMap scafMap){ |
| 587 |
|
| 588 |
final int scafnum=scafMap.getNumber(sl.rnameS()); |
| 589 |
return toVars(r, sl, callNs, scafnum); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public static ArrayList<Var> toVars(Read r, SamLine sl, boolean callNs, final int scafnum){ |
| 601 |
final boolean hasV=r.containsVariants(); |
| 602 |
final boolean callSID=(CALL_DEL || CALL_INS || CALL_SUB) && hasV; |
| 603 |
final boolean callJ=(CALL_JUNCTION) && (hasV || r.containsClipping()); |
| 604 |
if(!callSID && !callJ){return null;} |
| 605 |
|
| 606 |
r.toLongMatchString(false); |
| 607 |
if(sl.strand()==1 && !r.swapped()){ |
| 608 |
r.reverseComplement(); |
| 609 |
r.setSwapped(true); |
| 610 |
} |
| 611 |
|
| 612 |
ArrayList<Var> sidList=null, jList=null; |
| 613 |
if(callSID){sidList=toSubsAndIndels(r, sl, callNs, scafnum);} |
| 614 |
if(callJ){jList=toJunctions(r, sl, scafnum, hasV, 8);} |
| 615 |
|
| 616 |
if(sidList!=null){ |
| 617 |
if(jList!=null){sidList.addAll(jList);} |
| 618 |
return sidList; |
| 619 |
}else{ |
| 620 |
return jList; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
private static ArrayList<Var> toSubsAndIndels(Read r, SamLine sl, boolean callNs, final int scafnum){ |
| 636 |
final byte[] match=r.match; |
| 637 |
final byte[] bases=r.bases; |
| 638 |
final int rpos0=sl.pos-1; |
| 639 |
ArrayList<Var> list=new ArrayList<Var>(); |
| 640 |
|
| 641 |
int bstart=-1, bstop=-1; |
| 642 |
int rstart=-1, rstop=-1; |
| 643 |
int mode=-1; |
| 644 |
|
| 645 |
int mpos=0, bpos=0, rpos=rpos0; |
| 646 |
for(; mpos<match.length; mpos++){ |
| 647 |
byte m=match[mpos]; |
| 648 |
|
| 649 |
if(m!=mode){ |
| 650 |
if(mode=='D'){ |
| 651 |
bstop=bpos; |
| 652 |
rstop=rpos; |
| 653 |
|
| 654 |
if(CALL_DEL){ |
| 655 |
Var v=new Var(scafnum, rstart, rstop, 0, DEL); |
| 656 |
v.add(r, bstart, bstop); |
| 657 |
list.add(v); |
| 658 |
} |
| 659 |
bstart=bstop=rstart=rstop=-1; |
| 660 |
}else if(mode=='I'){ |
| 661 |
bstop=bpos; |
| 662 |
rstop=rpos; |
| 663 |
int blen=bstop-bstart; |
| 664 |
if(CALL_INS){ |
| 665 |
Var v; |
| 666 |
if(blen==1){ |
| 667 |
v=new Var(scafnum, rstart, rstop, bases[bstart], INS); |
| 668 |
}else{ |
| 669 |
v=new Var(scafnum, rstart, rstop, Arrays.copyOfRange(bases, bstart, bstop), INS); |
| 670 |
} |
| 671 |
v.add(r, bstart, bstop); |
| 672 |
list.add(v); |
| 673 |
} |
| 674 |
bstart=bstop=rstart=rstop=-1; |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
if(m=='C'){ |
| 679 |
bpos++; |
| 680 |
}else if(m=='m' || m=='S' || m=='N'){ |
| 681 |
if(m=='S' || (m=='N' && callNs)){ |
| 682 |
|
| 683 |
|
| 684 |
if(CALL_SUB){ |
| 685 |
Var v=new Var(scafnum, rpos, rpos+1, bases[bpos], SUB); |
| 686 |
|
| 687 |
v.add(r, bpos, bpos+1); |
| 688 |
list.add(v); |
| 689 |
|
| 690 |
if(TEST_REF_VARIANTS && v.type()==SUB && v.allele.length==1){ |
| 691 |
final byte call=v.allele[0]; |
| 692 |
final Scaffold scaf=ScafMap.defaultScafMap().getScaffold(scafnum); |
| 693 |
final byte ref=scaf.bases[v.start]; |
| 694 |
|
| 695 |
assert(ref!=call) : (char)call+"="+(char)ref+" at scaf "+scafnum+" pos "+v.start+"\n" |
| 696 |
+sl+"\n"+ScafMap.defaultScafMap().getScaffold(scafnum).getSequence(sl)+"\n"; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
} |
| 701 |
bpos++; |
| 702 |
rpos++; |
| 703 |
}else if(m=='D'){ |
| 704 |
if(mode!=m){ |
| 705 |
rstart=rpos; |
| 706 |
bstart=bpos; |
| 707 |
} |
| 708 |
rpos++; |
| 709 |
}else if(m=='I'){ |
| 710 |
if(mode!=m){ |
| 711 |
rstart=rpos; |
| 712 |
bstart=bpos; |
| 713 |
} |
| 714 |
bpos++; |
| 715 |
}else{ |
| 716 |
assert(false) : "Unhandled symbol "+(char)m; |
| 717 |
} |
| 718 |
mode=m; |
| 719 |
} |
| 720 |
|
| 721 |
if(mode=='D'){ |
| 722 |
bstop=bpos; |
| 723 |
rstop=rpos; |
| 724 |
if(CALL_DEL){ |
| 725 |
Var v=new Var(scafnum, rstart, rstop, 0, DEL); |
| 726 |
v.add(r, bstart, bstop); |
| 727 |
list.add(v); |
| 728 |
} |
| 729 |
bstart=bstop=rstart=rstop=-1; |
| 730 |
}else if(mode=='I'){ |
| 731 |
bstop=bpos; |
| 732 |
rstop=rpos-1; |
| 733 |
int blen=bstop-bstart; |
| 734 |
if(CALL_INS){ |
| 735 |
Var v; |
| 736 |
assert(rstart<=rstop) : "\n"+rstart+", "+rstop+", "+rpos+ |
| 737 |
"\n"+bstart+", "+bstop+", "+bpos+ |
| 738 |
"\n"+r+"\n"+sl; |
| 739 |
if(blen==1){ |
| 740 |
v=new Var(scafnum, rstart, rstop, bases[bstart], INS); |
| 741 |
}else{ |
| 742 |
v=new Var(scafnum, rstart, rstop, Arrays.copyOfRange(bases, bstart, bstop), INS); |
| 743 |
} |
| 744 |
v.add(r, bstart, bstop); |
| 745 |
list.add(v); |
| 746 |
} |
| 747 |
bstart=bstop=rstart=rstop=-1; |
| 748 |
} |
| 749 |
|
| 750 |
return list; |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
private static int countLeftClip(byte[] match){ |
| 755 |
|
| 756 |
byte mode=match[0]; |
| 757 |
if(mode!='C'){return 0;} |
| 758 |
int current=0; |
| 759 |
boolean hasDigit=false; |
| 760 |
|
| 761 |
for(int mpos=0; mpos<match.length; mpos++){ |
| 762 |
byte c=match[mpos]; |
| 763 |
|
| 764 |
if(mode==c){ |
| 765 |
current++; |
| 766 |
}else if(Tools.isDigit(c)){ |
| 767 |
current=(hasDigit ? current : 0)*10+c-'0'; |
| 768 |
hasDigit=true; |
| 769 |
}else{ |
| 770 |
break; |
| 771 |
} |
| 772 |
} |
| 773 |
return current; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
private static int countRightClip(byte[] match){ |
| 778 |
int mpos=match.length-1; |
| 779 |
boolean hasDigit=false; |
| 780 |
while(mpos>=0 && Tools.isDigit(match[mpos])){ |
| 781 |
hasDigit=true; |
| 782 |
mpos--; |
| 783 |
} |
| 784 |
if(!hasDigit){return countRightClipLong(match);} |
| 785 |
|
| 786 |
byte mode=match[mpos]; |
| 787 |
if(mode!='C'){return 0;} |
| 788 |
int current=0; |
| 789 |
|
| 790 |
for(; mpos<match.length; mpos++){ |
| 791 |
byte c=match[mpos]; |
| 792 |
|
| 793 |
if(mode==c){ |
| 794 |
current++; |
| 795 |
}else if(Tools.isDigit(c)){ |
| 796 |
current=(hasDigit ? current : 0)*10+c-'0'; |
| 797 |
hasDigit=true; |
| 798 |
}else{ |
| 799 |
break; |
| 800 |
} |
| 801 |
} |
| 802 |
return current; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
private static int countLeftClipLong(byte[] longmatch){ |
| 807 |
for(int i=0; i<longmatch.length; i++){ |
| 808 |
if(longmatch[i]!='C'){return i;} |
| 809 |
} |
| 810 |
return longmatch.length; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
private static int countRightClipLong(byte[] longmatch){ |
| 815 |
for(int i=0, j=longmatch.length-1; i<longmatch.length; i++, j--){ |
| 816 |
if(longmatch[j]!='C'){return i;} |
| 817 |
} |
| 818 |
return longmatch.length; |
| 819 |
} |
| 820 |
|
| 821 |
private static ArrayList<Var> toJunctions(Read r, SamLine sl, final int scafnum, |
| 822 |
boolean containsVars, final int minClip){ |
| 823 |
final byte[] match0=r.match, match; |
| 824 |
final byte[] bases=r.bases; |
| 825 |
|
| 826 |
final int start, stop; |
| 827 |
int leftClip=countLeftClip(match0), rightClip=countRightClip(match0); |
| 828 |
if(leftClip==0 && rightClip==0){ |
| 829 |
int[] rvec=KillSwitch.allocInt1D(2); |
| 830 |
byte[] smatch=SoftClipper.softClipMatch(match0, minClip, false, r.start, r.stop, rvec); |
| 831 |
if(smatch==null){ |
| 832 |
return null; |
| 833 |
}else{ |
| 834 |
start=rvec[0]; |
| 835 |
stop=rvec[1]; |
| 836 |
match=smatch; |
| 837 |
leftClip=countLeftClip(match); |
| 838 |
rightClip=countRightClip(match); |
| 839 |
} |
| 840 |
}else{ |
| 841 |
if(leftClip<minClip && rightClip<minClip){return null;} |
| 842 |
start=r.start; |
| 843 |
stop=r.stop; |
| 844 |
match=match0; |
| 845 |
} |
| 846 |
|
| 847 |
ArrayList<Var> list=new ArrayList<Var>(); |
| 848 |
if(leftClip>=minClip){ |
| 849 |
int bpos=leftClip-1; |
| 850 |
byte jcall=bases[bpos]; |
| 851 |
int jpos=start+leftClip; |
| 852 |
Var v=new Var(scafnum, jpos, jpos+1, jcall, LJUNCT); |
| 853 |
v.add(r, bpos, bpos+1); |
| 854 |
list.add(v); |
| 855 |
} |
| 856 |
if(rightClip>=minClip){ |
| 857 |
int bpos=bases.length-rightClip; |
| 858 |
byte jcall=bases[bpos]; |
| 859 |
int jpos=stop-rightClip+1; |
| 860 |
Var v=new Var(scafnum, jpos, jpos+1, jcall, RJUNCT); |
| 861 |
v.add(r, bpos, bpos+1); |
| 862 |
list.add(v); |
| 863 |
} |
| 864 |
return list; |
| 865 |
} |
| 866 |
|
| 867 |
public int calcBstart(Read r, SamLine sl){ |
| 868 |
r.toLongMatchString(false); |
| 869 |
byte[] match=r.match; |
| 870 |
final int rstart=sl.pos-1; |
| 871 |
final int type=type(); |
| 872 |
|
| 873 |
int bstart=-1; |
| 874 |
|
| 875 |
for(int mpos=0, rpos=rstart, bpos=0; mpos<match.length; mpos++){ |
| 876 |
byte m=match[mpos]; |
| 877 |
if(m=='C'){ |
| 878 |
bpos++; |
| 879 |
}else if(m=='m' || m=='S' || m=='N'){ |
| 880 |
if(rpos==rstart){ |
| 881 |
assert(type==SUB || type==NOCALL) : type+", "+bpos+", "+rpos+"\n"+new String(match); |
| 882 |
bstart=bpos; |
| 883 |
break; |
| 884 |
} |
| 885 |
bpos++; |
| 886 |
rpos++; |
| 887 |
}else if(m=='D'){ |
| 888 |
if(rpos==rstart){ |
| 889 |
assert(type==DEL) : type+", "+rpos+"\n"+new String(match); |
| 890 |
bstart=bpos; |
| 891 |
break; |
| 892 |
} |
| 893 |
rpos++; |
| 894 |
}else if(m=='I'){ |
| 895 |
if(rpos==rstart && type==INS){ |
| 896 |
bstart=bpos; |
| 897 |
break; |
| 898 |
} |
| 899 |
bpos++; |
| 900 |
}else{ |
| 901 |
assert(false) : "Unhandled symbol "+(char)m; |
| 902 |
} |
| 903 |
} |
| 904 |
assert(bstart>=0); |
| 905 |
return bstart; |
| 906 |
} |
| 907 |
|
| 908 |
public int calcBstop(int bstart, Read r){ |
| 909 |
assert(bstart>=0); |
| 910 |
int bstop=bstart+readlen(); |
| 911 |
assert(bstop<=r.length()); |
| 912 |
return bstop; |
| 913 |
} |
| 914 |
|
| 915 |
public int calcEndDist(int bstart, int bstop, Read r){ |
| 916 |
int dist=Tools.min(bstart, r.length()-bstop); |
| 917 |
assert(dist>=0 && dist<=r.length()/2) : dist+", "+r.length()+", "+bstart+", "+bstop+"\n"+this+"\n"+ |
| 918 |
"\n"+new String(r.match)+"\n"+r.samline+"\n"; |
| 919 |
assert(dist<=(r.length()-readlen())/2) : "\ndist="+dist+", r.len="+r.length()+", readlen="+readlen()+", allele='"+new String(allele)+ |
| 920 |
"', allele.length="+allele.length+"\n"+"allele2="+toString(allele)+"\n(r.length()-readlen())/2="+((r.length()-readlen())/2)+"bstart="+bstart+", bstop="+bstop+ |
| 921 |
"\nthis="+this+"\nmatch="+new String(r.match)+"\nsamline="+r.samline+"\n"; |
| 922 |
return dist; |
| 923 |
} |
| 924 |
|
| 925 |
String toString(byte[] array) { |
| 926 |
StringBuilder sb=new StringBuilder(); |
| 927 |
for(byte b : array){ |
| 928 |
sb.append((int)b).append(','); |
| 929 |
} |
| 930 |
return sb.toString(); |
| 931 |
} |
| 932 |
|
| 933 |
public int calcBaseQ(final int bstart0, final int bstop0, Read r, SamLine sl){ |
| 934 |
final byte[] quals=r.quality; |
| 935 |
if(quals==null){return Shared.FAKE_QUAL;} |
| 936 |
final int type=type(); |
| 937 |
final int bstart, bstop; |
| 938 |
final int len=r.length(); |
| 939 |
|
| 940 |
if(sl.strand()==0 || (sl.strand()==1 && r.swapped())){ |
| 941 |
bstart=bstart0; |
| 942 |
bstop=bstop0; |
| 943 |
}else{ |
| 944 |
bstart=len-bstop0-1; |
| 945 |
bstop=len-bstart0-1; |
| 946 |
assert(bstop-bstart==bstop0-bstart0); |
| 947 |
} |
| 948 |
|
| 949 |
int sum=0, avg=0; |
| 950 |
if(type==DEL){ |
| 951 |
if(bstart==0){ |
| 952 |
sum=avg=quals[0]; |
| 953 |
}else if(bstop>=len-1){ |
| 954 |
sum=avg=quals[len-1]; |
| 955 |
}else{ |
| 956 |
assert(bstop==bstart) : bstart0+", "+bstop0+", "+bstart+", "+bstop+"\n"+ |
| 957 |
r.length()+", "+r.swapped()+", "+type()+", "+readlen()+", "+reflen()+ |
| 958 |
"\n"+this+"\n"+new String(r.match)+"\n"+r.samline+"\n"; |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
sum=quals[bstart]+quals[bstop+1]; |
| 964 |
avg=sum/2; |
| 965 |
} |
| 966 |
}else{ |
| 967 |
for(int i=bstart; i<bstop; i++){ |
| 968 |
sum+=quals[i]; |
| 969 |
} |
| 970 |
avg=Math.round(sum/(bstop-bstart)); |
| 971 |
} |
| 972 |
return avg; |
| 973 |
} |
| 974 |
|
| 975 |
public int reflen(){ |
| 976 |
return stop-start; |
| 977 |
} |
| 978 |
|
| 979 |
int readlen(){ |
| 980 |
return (allele==null || allele.length==0 || allele[0]=='.' ? 0 : allele.length); |
| 981 |
} |
| 982 |
|
| 983 |
public int type(){return type;} |
| 984 |
|
| 985 |
int type_old(){ |
| 986 |
int reflen=reflen(), readlen=readlen(); |
| 987 |
return typeReadlenReflen(readlen, reflen, allele); |
| 988 |
} |
| 989 |
|
| 990 |
static int typeStartStop(int start, int stop, byte[] allele){ |
| 991 |
final int readlen=(allele.length==0 || allele[0]=='.' ? 0 : allele.length); |
| 992 |
final int reflen=stop-start; |
| 993 |
return typeReadlenReflen(readlen, reflen, allele); |
| 994 |
} |
| 995 |
|
| 996 |
static int typeReadlenReflen(int readlen, int reflen, byte[] allele){ |
| 997 |
if(reflen<readlen){return INS;} |
| 998 |
if(reflen>readlen){return DEL;} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
for(byte b : allele){ |
| 1005 |
if(b!='N'){return SUB;} |
| 1006 |
} |
| 1007 |
return NOCALL; |
| 1008 |
} |
| 1009 |
|
| 1010 |
String typeString(){ |
| 1011 |
return typeArray[type()]; |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
@Override |
| 1019 |
public boolean equals(Object b){ |
| 1020 |
return equals((Var)b); |
| 1021 |
} |
| 1022 |
|
| 1023 |
public boolean equals(Var b){ |
| 1024 |
return hashcode==b.hashcode && compareTo(b)==0; |
| 1025 |
} |
| 1026 |
|
| 1027 |
@Override |
| 1028 |
public int hashCode(){ |
| 1029 |
return hashcode; |
| 1030 |
} |
| 1031 |
|
| 1032 |
public long toKey() { |
| 1033 |
final int len=(type==DEL ? reflen() : readlen()); |
| 1034 |
final long key=type^((hash(allele)&0x3F)>>alleleShift)^(len>>lenShift)^(Long.rotateRight(start, startShift)); |
| 1035 |
return key&0x7FFFFFFFFFFFFFFFL; |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
} |
| 1040 |
|
| 1041 |
@Override |
| 1042 |
public int compareTo(Var v){ |
| 1043 |
if(scafnum!=v.scafnum){return scafnum-v.scafnum;} |
| 1044 |
final int typeA=type(), typeB=v.type(); |
| 1045 |
int stA=start+(typeA==DEL ? -1 : 0); |
| 1046 |
int stB=v.start+(typeB==DEL ? -1 : 0); |
| 1047 |
if(stA!=stB){return stA-stB;} |
| 1048 |
if(typeA!=typeB){return typeA-typeB;} |
| 1049 |
if(stop!=v.stop){return stop-v.stop;} |
| 1050 |
return compare(allele, v.allele); |
| 1051 |
} |
| 1052 |
|
| 1053 |
public int compare(byte[] a, byte[] b){ |
| 1054 |
if(a==b){return 0;} |
| 1055 |
if(a.length!=b.length){return b.length-a.length;} |
| 1056 |
for(int i=0; i<a.length; i++){ |
| 1057 |
byte ca=a[i], cb=b[i]; |
| 1058 |
if(ca!=cb){return ca-cb;} |
| 1059 |
} |
| 1060 |
return 0; |
| 1061 |
} |
| 1062 |
|
| 1063 |
@Override |
| 1064 |
public String toString(){ |
| 1065 |
return toTextQuick(new ByteBuilder()).toString(); |
| 1066 |
} |
| 1067 |
|
| 1068 |
public ByteBuilder toTextQuick(ByteBuilder bb){ |
| 1069 |
return toText(bb, 0.99f, 30, 30, 150, 1, 2, null); |
| 1070 |
} |
| 1071 |
|
| 1072 |
public static String toVarHeader(double properPairRate, double totalQualityAvg, double mapqAvg, double rarity, double minAlleleFraction, int ploidy, |
| 1073 |
long reads, long pairs, long properPairs, long bases, String ref){ |
| 1074 |
StringBuilder sb=new StringBuilder(); |
| 1075 |
|
| 1076 |
final double readLengthAvg=bases/Tools.max(1.0, reads); |
| 1077 |
sb.append("#fileformat\tVar_"+varFormat+"\n"); |
| 1078 |
sb.append("#BBMapVersion\t"+Shared.BBTOOLS_VERSION_STRING+"\n"); |
| 1079 |
sb.append("#ploidy\t"+ploidy+"\n"); |
| 1080 |
sb.append(Tools.format("#rarity\t%.5f\n", rarity)); |
| 1081 |
sb.append(Tools.format("#minAlleleFraction\t%.4f\n", minAlleleFraction)); |
| 1082 |
sb.append("#reads\t"+reads+"\n"); |
| 1083 |
sb.append("#pairedReads\t"+pairs+"\n"); |
| 1084 |
sb.append("#properlyPairedReads\t"+properPairs+"\n"); |
| 1085 |
sb.append(Tools.format("#readLengthAvg\t%.2f\n", readLengthAvg)); |
| 1086 |
sb.append(Tools.format("#properPairRate\t%.4f\n", properPairRate)); |
| 1087 |
sb.append(Tools.format("#totalQualityAvg\t%.4f\n", totalQualityAvg)); |
| 1088 |
sb.append(Tools.format("#mapqAvg\t%.2f\n", mapqAvg)); |
| 1089 |
if(ref!=null){sb.append("#reference\t"+ref+"\n");} |
| 1090 |
|
| 1091 |
sb.append("#scaf\tstart\tstop\ttype\tcall\tr1p\tr1m\tr2p\tr2m\tpaired\tlengthSum"); |
| 1092 |
sb.append("\tmapq\tmapqMax\tbaseq\tbaseqMax\tedist\tedistMax\tid\tidMax"); |
| 1093 |
sb.append("\tcov\tminusCov"); |
| 1094 |
sb.append("\tnearbyVarCount"); |
| 1095 |
sb.append("\tflagged"); |
| 1096 |
sb.append("\tcontigEndDist"); |
| 1097 |
sb.append("\tphredScore"); |
| 1098 |
if(extendedText){ |
| 1099 |
sb.append("\treadCount\talleleFraction\trevisedAF\tstrandRatio\tbaseqAvg\tmapqAvg\tedistAvg\tidentityAvg"); |
| 1100 |
sb.append("\tedistScore\tidentityScore\tqualityScore\tpairedScore\tbiasScore\tcoverageScore\thomopolymerScore\tscore"); |
| 1101 |
|
| 1102 |
} |
| 1103 |
return sb.toString(); |
| 1104 |
} |
| 1105 |
|
| 1106 |
public static String toBasicHeader(){ |
| 1107 |
StringBuilder sb=new StringBuilder(); |
| 1108 |
sb.append("#scaf\tstart\tstop\ttype\tcall\tr1p\tr1m\tr2p\tr2m\tpaired\tlengthSum"); |
| 1109 |
sb.append("\tmapq\tmapqMax\tbaseq\tbaseqMax\tedist\tedistMax\tid\tidMax"); |
| 1110 |
sb.append("\tcov\tminusCov"); |
| 1111 |
sb.append("\tnearVarCount"); |
| 1112 |
sb.append("\tcontigEndDist"); |
| 1113 |
sb.append("\tphredScore"); |
| 1114 |
return sb.toString(); |
| 1115 |
} |
| 1116 |
|
| 1117 |
public ByteBuilder toText(ByteBuilder bb, double properPairRate, double totalQualityAvg, double totalMapqAvg, double readLengthAvg, double rarity, int ploidy, ScafMap map){ |
| 1118 |
useIdentity=true; |
| 1119 |
bb.append(scafnum).tab(); |
| 1120 |
bb.append(start).tab(); |
| 1121 |
bb.append(stop).tab(); |
| 1122 |
bb.append(typeArray[type()]).tab(); |
| 1123 |
for(byte b : allele){bb.append(b);} |
| 1124 |
bb.tab(); |
| 1125 |
|
| 1126 |
bb.append(r1plus).tab(); |
| 1127 |
bb.append(r1minus).tab(); |
| 1128 |
bb.append(r2plus).tab(); |
| 1129 |
bb.append(r2minus).tab(); |
| 1130 |
bb.append(properPairCount).tab(); |
| 1131 |
bb.append(lengthSum).tab(); |
| 1132 |
|
| 1133 |
bb.append(mapQSum).tab(); |
| 1134 |
bb.append(mapQMax).tab(); |
| 1135 |
bb.append(baseQSum).tab(); |
| 1136 |
bb.append(baseQMax).tab(); |
| 1137 |
bb.append(endDistSum).tab(); |
| 1138 |
bb.append(endDistMax).tab(); |
| 1139 |
bb.append(idSum).tab(); |
| 1140 |
bb.append(idMax).tab(); |
| 1141 |
|
| 1142 |
bb.append(coverage).tab(); |
| 1143 |
bb.append(minusCoverage).tab(); |
| 1144 |
bb.append(nearbyVarCount).tab(); |
| 1145 |
bb.append(flagged ? 1 : 0).tab(); |
| 1146 |
|
| 1147 |
final int scafEndDist=!doNscan ? nScan : (map==null ? start : contigEndDist(map)); |
| 1148 |
bb.append(scafEndDist).tab(); |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
final double score=score(properPairRate, totalQualityAvg, totalMapqAvg, readLengthAvg, rarity, ploidy, map); |
| 1153 |
|
| 1154 |
bb.append(toPhredScore(score), 2).tab(); |
| 1155 |
|
| 1156 |
if(extendedText){ |
| 1157 |
|
| 1158 |
bb.append(alleleCount()).tab(); |
| 1159 |
final double af=alleleFraction(); |
| 1160 |
bb.append(af, 4).tab(); |
| 1161 |
bb.append(revisedAlleleFraction(af, readLengthAvg), 4).tab(); |
| 1162 |
bb.append(strandRatio(), 4).tab(); |
| 1163 |
bb.append(baseQAvg(), 2).tab(); |
| 1164 |
bb.append(mapQAvg(), 2).tab(); |
| 1165 |
bb.append(edistAvg(), 2).tab(); |
| 1166 |
bb.append(identityAvg(), 2).tab(); |
| 1167 |
|
| 1168 |
bb.append(edistScore(), 4).tab(); |
| 1169 |
bb.append(identityScore(), 4).tab(); |
| 1170 |
bb.append(qualityScore(totalQualityAvg, totalMapqAvg), 4).tab(); |
| 1171 |
bb.append(pairedScore(properPairRate, scafEndDist), 4).tab(); |
| 1172 |
bb.append(biasScore(properPairRate, scafEndDist), 4).tab(); |
| 1173 |
bb.append(coverageScore(ploidy, rarity, readLengthAvg), 4).tab(); |
| 1174 |
bb.append(homopolymerScore(map), 4).tab(); |
| 1175 |
bb.append(score, 4).tab(); |
| 1176 |
|
| 1177 |
} |
| 1178 |
|
| 1179 |
bb.length--; |
| 1180 |
|
| 1181 |
return bb; |
| 1182 |
} |
| 1183 |
|
| 1184 |
public static String toVcfHeader(double properPairRate, double totalQualityAvg, double mapqAvg, |
| 1185 |
double rarity, double minAlleleFraction, int ploidy, long reads, long pairs, |
| 1186 |
long properPairs, long bases, String ref, ScafMap map, String sampleName, |
| 1187 |
boolean trimWhitespace) { |
| 1188 |
StringBuilder sb=new StringBuilder(); |
| 1189 |
|
| 1190 |
sb.append("##fileformat=VCFv4.2\n"); |
| 1191 |
sb.append("##BBMapVersion="+Shared.BBTOOLS_VERSION_STRING+"\n"); |
| 1192 |
sb.append("##ploidy="+ploidy+"\n"); |
| 1193 |
sb.append(Tools.format("##rarity=%.5f\n", rarity)); |
| 1194 |
sb.append(Tools.format("##minallelefraction=%.5f\n", minAlleleFraction)); |
| 1195 |
sb.append("##reads="+reads+"\n"); |
| 1196 |
sb.append("##pairedReads="+pairs+"\n"); |
| 1197 |
sb.append("##properlyPairedReads="+properPairs+"\n"); |
| 1198 |
sb.append(Tools.format("##readLengthAvg=%.3f\n", (bases/Tools.max(reads, 1.0)))); |
| 1199 |
sb.append(Tools.format("##properPairRate=%.5f\n", properPairRate)); |
| 1200 |
sb.append(Tools.format("##totalQualityAvg=%.3f\n", totalQualityAvg)); |
| 1201 |
sb.append(Tools.format("##mapqAvg=%.3f\n", mapqAvg)); |
| 1202 |
if(ref!=null){sb.append("##reference="+ref+"\n");} |
| 1203 |
|
| 1204 |
for(Scaffold scaf : map.list){ |
| 1205 |
String name=scaf.name; |
| 1206 |
if(trimWhitespace){name=Tools.trimWhitespace(name);} |
| 1207 |
sb.append("##contig=<ID="+name+",length="+scaf.length+">\n"); |
| 1208 |
} |
| 1209 |
|
| 1210 |
{ |
| 1211 |
sb.append("##FILTER=<ID=FAIL,Description=\"Fail\">\n"); |
| 1212 |
sb.append("##FILTER=<ID=PASS,Description=\"Pass\">\n"); |
| 1213 |
|
| 1214 |
sb.append("##INFO=<ID=SN,Number=1,Type=Integer,Description=\"Scaffold Number\">\n"); |
| 1215 |
sb.append("##INFO=<ID=STA,Number=1,Type=Integer,Description=\"Start\">\n"); |
| 1216 |
sb.append("##INFO=<ID=STO,Number=1,Type=Integer,Description=\"Stop\">\n"); |
| 1217 |
sb.append("##INFO=<ID=TYP,Number=1,Type=String,Description=\"Type\">\n"); |
| 1218 |
|
| 1219 |
sb.append("##INFO=<ID=R1P,Number=1,Type=Integer,Description=\"Read1 Plus Count\">\n"); |
| 1220 |
sb.append("##INFO=<ID=R1M,Number=1,Type=Integer,Description=\"Read1 Minus Count\">\n"); |
| 1221 |
sb.append("##INFO=<ID=R2P,Number=1,Type=Integer,Description=\"Read2 Plus Count\">\n"); |
| 1222 |
sb.append("##INFO=<ID=R2M,Number=1,Type=Integer,Description=\"Read2 Minus Count\">\n"); |
| 1223 |
|
| 1224 |
sb.append("##INFO=<ID=AD,Number=1,Type=Integer,Description=\"Allele Depth\">\n"); |
| 1225 |
sb.append("##INFO=<ID=DP,Number=1,Type=Integer,Description=\"Total Depth\">\n"); |
| 1226 |
|
| 1227 |
sb.append("##INFO=<ID=MCOV,Number=1,Type=Integer,Description=\"Minus Coverage\">\n"); |
| 1228 |
sb.append("##INFO=<ID=PPC,Number=1,Type=Integer,Description=\"Paired Count\">\n"); |
| 1229 |
|
| 1230 |
sb.append("##INFO=<ID=AF,Number=1,Type=Float,Description=\"Allele Fraction\">\n"); |
| 1231 |
sb.append("##INFO=<ID=RAF,Number=1,Type=Float,Description=\"Revised Allele Fraction\">\n"); |
| 1232 |
sb.append("##INFO=<ID=LS,Number=1,Type=Integer,Description=\"Length Sum\">\n"); |
| 1233 |
|
| 1234 |
sb.append("##INFO=<ID=MQS,Number=1,Type=Integer,Description=\"MAPQ Sum\">\n"); |
| 1235 |
sb.append("##INFO=<ID=MQM,Number=1,Type=Integer,Description=\"MAPQ Max\">\n"); |
| 1236 |
sb.append("##INFO=<ID=BQS,Number=1,Type=Integer,Description=\"Base Quality Sum\">\n"); |
| 1237 |
sb.append("##INFO=<ID=BQM,Number=1,Type=Integer,Description=\"Base Quality Max\">\n"); |
| 1238 |
|
| 1239 |
sb.append("##INFO=<ID=EDS,Number=1,Type=Integer,Description=\"End Distance Sum\">\n"); |
| 1240 |
sb.append("##INFO=<ID=EDM,Number=1,Type=Integer,Description=\"End Distance Max\">\n"); |
| 1241 |
sb.append("##INFO=<ID=IDS,Number=1,Type=Integer,Description=\"Identity Sum\">\n"); |
| 1242 |
sb.append("##INFO=<ID=IDM,Number=1,Type=Integer,Description=\"Identity Max\">\n"); |
| 1243 |
|
| 1244 |
sb.append("##INFO=<ID=NVC,Number=1,Type=Integer,Description=\"Nearby Variation Count\">\n"); |
| 1245 |
sb.append("##INFO=<ID=FLG,Number=1,Type=Integer,Description=\"Flagged\">\n"); |
| 1246 |
sb.append("##INFO=<ID=CED,Number=1,Type=Integer,Description=\"Contig End Distance\">\n"); |
| 1247 |
sb.append("##INFO=<ID=HMP,Number=1,Type=Integer,Description=\"Homopolymer Count\">\n"); |
| 1248 |
sb.append("##INFO=<ID=SB,Number=1,Type=Float,Description=\"Strand Bias\">\n"); |
| 1249 |
sb.append("##INFO=<ID=DP4,Number=4,Type=Integer,Description=\"Ref+, Ref-, Alt+, Alt-\">\n"); |
| 1250 |
|
| 1251 |
sb.append("##FORMAT=<ID=GT,Number=1,Type=String,Description=\"Genotype\">\n"); |
| 1252 |
sb.append("##FORMAT=<ID=DP,Number=1,Type=Integer,Description=\"Read Depth\">\n"); |
| 1253 |
sb.append("##FORMAT=<ID=AD,Number=1,Type=Integer,Description=\"Allele Depth\">\n"); |
| 1254 |
sb.append("##FORMAT=<ID=AF,Number=1,Type=Float,Description=\"Allele Fraction\">\n"); |
| 1255 |
sb.append("##FORMAT=<ID=RAF,Number=1,Type=Float,Description=\"Revised Allele Fraction\">\n"); |
| 1256 |
sb.append("##FORMAT=<ID=NVC,Number=1,Type=Integer,Description=\"Nearby Variation Count\">\n"); |
| 1257 |
sb.append("##FORMAT=<ID=FLG,Number=1,Type=Integer,Description=\"Flagged\">\n"); |
| 1258 |
sb.append("##FORMAT=<ID=SB,Number=1,Type=Float,Description=\"Strand Bias\">\n"); |
| 1259 |
|
| 1260 |
sb.append("##FORMAT=<ID=SC,Number=1,Type=Float,Description=\"Score\">\n"); |
| 1261 |
sb.append("##FORMAT=<ID=PF,Number=1,Type=String,Description=\"Pass Filter\">\n"); |
| 1262 |
|
| 1263 |
} |
| 1264 |
|
| 1265 |
sb.append("#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT"); |
| 1266 |
if(sampleName!=null){sb.append('\t').append(sampleName);} |
| 1267 |
return sb.toString(); |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
public ByteBuilder toVCF(ByteBuilder bb, double properPairRate, double totalQualityAvg, double mapqAvg, double readLengthAvg, |
| 1275 |
int ploidy, ScafMap map, VarFilter filter, boolean trimWhitespace){ |
| 1276 |
|
| 1277 |
final Scaffold scaf=map.getScaffold(scafnum); |
| 1278 |
final byte[] bases=scaf.bases; |
| 1279 |
final int reflen=reflen(), readlen=readlen(), type=type(); |
| 1280 |
final double score=phredScore(properPairRate, totalQualityAvg, mapqAvg, readLengthAvg, filter.rarity, ploidy, map); |
| 1281 |
final boolean pass=(filter==null ? true : |
| 1282 |
filter.passesFilter(this, properPairRate, totalQualityAvg, mapqAvg, readLengthAvg, ploidy, map, true)); |
| 1283 |
bb.append(trimWhitespace ? Tools.trimWhitespace(scaf.name) : scaf.name).tab(); |
| 1284 |
boolean indel=(type==INS || type==DEL); |
| 1285 |
boolean addPrevBase=true; |
| 1286 |
final int vcfStart=start+(indel && addPrevBase ? 0 : 1); |
| 1287 |
bb.append(vcfStart).tab(); |
| 1288 |
bb.append('.').tab(); |
| 1289 |
|
| 1290 |
byte prevBase=(bases==null ? (byte)'N' : bases[Tools.mid(start-1, 0, bases.length-1)]); |
| 1291 |
if(UPPER_CASE_ALLELES){prevBase=(byte) Tools.toUpperCase(prevBase);} |
| 1292 |
|
| 1293 |
if(addPrevBase){ |
| 1294 |
if(reflen==0 || allele.length<1){bb.append(prevBase);} |
| 1295 |
for(int i=0, rpos=start; i<reflen; i++, rpos++){ |
| 1296 |
bb.append(bases==null || rpos<0 || rpos>=bases.length ? (char)'N' : (char)bases[rpos]); |
| 1297 |
} |
| 1298 |
bb.tab(); |
| 1299 |
|
| 1300 |
if(reflen==0 || allele.length<1){bb.append(prevBase);} |
| 1301 |
bb.append(allele).tab(); |
| 1302 |
}else{ |
| 1303 |
if(reflen==0){ |
| 1304 |
bb.append('.'); |
| 1305 |
}else{ |
| 1306 |
for(int i=0, rpos=start; i<reflen; i++, rpos++){ |
| 1307 |
char refBase=bases==null || rpos<0 || rpos>=bases.length ? (char)'N' : (char)bases[rpos]; |
| 1308 |
if(UPPER_CASE_ALLELES){refBase=Tools.toUpperCase(refBase);} |
| 1309 |
bb.append(refBase); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
bb.tab(); |
| 1313 |
|
| 1314 |
if(allele.length<1){ |
| 1315 |
bb.append('.').tab(); |
| 1316 |
}else{ |
| 1317 |
bb.append(allele).tab(); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
bb.append(score, 2).tab(); |
| 1323 |
bb.append(pass ? "PASS\t" : "FAIL\t"); |
| 1324 |
|
| 1325 |
final int scafEndDist=!doNscan ? nScan : (map==null ? start : contigEndDist(map)); |
| 1326 |
final int count=alleleCount(); |
| 1327 |
final double af=alleleFraction(); |
| 1328 |
final double raf=revisedAlleleFraction(af, readLengthAvg); |
| 1329 |
final double strandBias=strandBiasScore(scafEndDist); |
| 1330 |
|
| 1331 |
{ |
| 1332 |
assert(Scaffold.trackStrand()==(minusCoverage>=0)) : Scaffold.trackStrand()+", "+minusCoverage; |
| 1333 |
final int covMinus=(Scaffold.trackStrand() ? minusCoverage : coverage/2); |
| 1334 |
final int covPlus=Tools.max(0, coverage-covMinus); |
| 1335 |
final int refMinus=Tools.max(0, covMinus-alleleMinusCount()); |
| 1336 |
final int refPlus=Tools.max(0, covPlus-allelePlusCount()); |
| 1337 |
|
| 1338 |
bb.append("SN=").append(scafnum).append(';'); |
| 1339 |
bb.append("STA=").append(start).append(';'); |
| 1340 |
bb.append("STO=").append(stop).append(';'); |
| 1341 |
bb.append("TYP=").append(typeArray[type()]).append(';'); |
| 1342 |
|
| 1343 |
bb.append("R1P=").append(r1plus).append(';'); |
| 1344 |
bb.append("R1M=").append(r1minus).append(';'); |
| 1345 |
bb.append("R2P=").append(r2plus).append(';'); |
| 1346 |
bb.append("R2M=").append(r2minus).append(';'); |
| 1347 |
|
| 1348 |
bb.append("AD=").append(count).append(';'); |
| 1349 |
bb.append("DP=").append(Tools.max(coverage, count)).append(';'); |
| 1350 |
bb.append("MCOV=").append(minusCoverage).append(';'); |
| 1351 |
bb.append("PPC=").append(properPairCount).append(';'); |
| 1352 |
|
| 1353 |
bb.append("AF=").append(af,4).append(';'); |
| 1354 |
bb.append("RAF=").append(raf,4).append(';'); |
| 1355 |
bb.append("LS=").append(lengthSum).append(';'); |
| 1356 |
|
| 1357 |
bb.append("MQS=").append(mapQSum).append(';'); |
| 1358 |
bb.append("MQM=").append(mapQMax).append(';'); |
| 1359 |
bb.append("BQS=").append(baseQSum).append(';'); |
| 1360 |
bb.append("BQM=").append(baseQMax).append(';'); |
| 1361 |
|
| 1362 |
bb.append("EDS=").append(endDistSum).append(';'); |
| 1363 |
bb.append("EDM=").append(endDistMax).append(';'); |
| 1364 |
bb.append("IDS=").append(idSum).append(';'); |
| 1365 |
bb.append("IDM=").append(idMax).append(';'); |
| 1366 |
|
| 1367 |
bb.append("NVC=").append(nearbyVarCount).append(';'); |
| 1368 |
bb.append("FLG=").append(flagged ? 1 : 0).append(';'); |
| 1369 |
bb.append("CED=").append(scafEndDist).append(';'); |
| 1370 |
bb.append("HMP=").append(homopolymerCount(map)).append(';'); |
| 1371 |
bb.append("SB=").append(strandBias,4).append(';'); |
| 1372 |
|
| 1373 |
if(Scaffold.trackStrand()){ |
| 1374 |
bb.append("DP4=").append(refPlus).append(',').append(refMinus).append(','); |
| 1375 |
bb.append(allelePlusCount()).append(',').append(alleleMinusCount()).append(';'); |
| 1376 |
} |
| 1377 |
|
| 1378 |
bb.length--; |
| 1379 |
} |
| 1380 |
{ |
| 1381 |
bb.tab(); |
| 1382 |
bb.append("GT:DP:AD:AF:RAF:NVC:FLG:SB:SC:PF"); |
| 1383 |
bb.tab(); |
| 1384 |
|
| 1385 |
bb.append(genotype(ploidy, pass)); |
| 1386 |
bb.append(':'); |
| 1387 |
bb.append(Tools.max(coverage, count)); |
| 1388 |
bb.append(':'); |
| 1389 |
bb.append(count); |
| 1390 |
bb.append(':'); |
| 1391 |
bb.append(af,4); |
| 1392 |
bb.append(':'); |
| 1393 |
|
| 1394 |
bb.append(raf,4); |
| 1395 |
bb.append(':'); |
| 1396 |
bb.append(nearbyVarCount); |
| 1397 |
bb.append(':'); |
| 1398 |
bb.append(flagged ? 1 : 0); |
| 1399 |
bb.append(':'); |
| 1400 |
bb.append(strandBias,4); |
| 1401 |
bb.append(':'); |
| 1402 |
|
| 1403 |
bb.append(score,2); |
| 1404 |
bb.append(':'); |
| 1405 |
bb.append(pass ? "PASS" : "FAIL"); |
| 1406 |
} |
| 1407 |
|
| 1408 |
return bb; |
| 1409 |
} |
| 1410 |
|
| 1411 |
public int calcCopies(int ploidy){ |
| 1412 |
final double af=alleleFraction(); |
| 1413 |
|
| 1414 |
if(ploidy==1){ |
| 1415 |
return af<0.4 ? 0 : 1; |
| 1416 |
}else if(ploidy==2){ |
| 1417 |
if(af<0.2){return 0;} |
| 1418 |
if(af<0.8){return 1;} |
| 1419 |
return 2; |
| 1420 |
} |
| 1421 |
|
| 1422 |
int copies=(int)Math.round(ploidy*af); |
| 1423 |
if(af>=0.5){copies=Tools.max(copies, 1);} |
| 1424 |
copies=Tools.mid(MIN_VAR_COPIES, copies, ploidy); |
| 1425 |
return copies; |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
private String genotype(int ploidy, boolean pass) { |
| 1430 |
if(!pass && noPassDotGenotype){ |
| 1431 |
if(ploidy==1){return ".";} |
| 1432 |
else if(ploidy==2){return "./.";} |
| 1433 |
StringBuilder sb=new StringBuilder(ploidy*2-1); |
| 1434 |
sb.append('.'); |
| 1435 |
for(int i=1; i<ploidy; i++){ |
| 1436 |
sb.append('/').append('.'); |
| 1437 |
} |
| 1438 |
return sb.toString(); |
| 1439 |
} |
| 1440 |
int copies=calcCopies(ploidy); |
| 1441 |
if(ploidy==1){return copies==0 ? "0" : "1";} |
| 1442 |
if(ploidy==2){ |
| 1443 |
if(copies==0){return "0/0";} |
| 1444 |
if(copies==1){return "0/1";} |
| 1445 |
return "1/1"; |
| 1446 |
} |
| 1447 |
StringBuilder sb=new StringBuilder(ploidy*2); |
| 1448 |
int refCopies=ploidy-copies; |
| 1449 |
|
| 1450 |
for(int i=0; i<refCopies; i++){ |
| 1451 |
sb.append(0).append('/'); |
| 1452 |
} |
| 1453 |
for(int i=0; i<copies; i++){ |
| 1454 |
sb.append(1).append('/'); |
| 1455 |
} |
| 1456 |
sb.setLength(sb.length()-1); |
| 1457 |
return sb.toString(); |
| 1458 |
} |
| 1459 |
|
| 1460 |
private int hash(){ |
| 1461 |
return scafnum^Integer.rotateLeft(start, 9)^Integer.rotateRight(stop, 9)^hash(allele); |
| 1462 |
} |
| 1463 |
|
| 1464 |
public static final int hash(byte[] a){ |
| 1465 |
int code=123456789; |
| 1466 |
for(byte b : a){ |
| 1467 |
code=Integer.rotateLeft(code, 3)^codes[b]; |
| 1468 |
} |
| 1469 |
return code&Integer.MAX_VALUE; |
| 1470 |
} |
| 1471 |
|
| 1472 |
public int calcCoverage(ScafMap map){ |
| 1473 |
if(coverage>=0){return coverage;} |
| 1474 |
|
| 1475 |
Scaffold scaf=map.getScaffold(scafnum); |
| 1476 |
coverage=scaf.calcCoverage(this); |
| 1477 |
if(Scaffold.trackStrand()){minusCoverage=scaf.minusCoverage(this);} |
| 1478 |
return coverage; |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
public static double toPhredScore(double score){ |
| 1486 |
if(score==0){return 0;} |
| 1487 |
score=score*0.998; |
| 1488 |
return 2.5*QualityTools.probErrorToPhredDouble(1-score); |
| 1489 |
} |
| 1490 |
|
| 1491 |
public double phredScore(double properPairRate, double totalQualityAvg, double totalMapqAvg, double readLengthAvg, double rarity, int ploidy, ScafMap map){ |
| 1492 |
double score=score(properPairRate, totalQualityAvg, totalMapqAvg, readLengthAvg, rarity, ploidy, map); |
| 1493 |
return toPhredScore(score); |
| 1494 |
} |
| 1495 |
|
| 1496 |
public double score(double properPairRate, double totalQualityAvg, double totalMapqAvg, double readLengthAvg, double rarity, int ploidy, ScafMap map){ |
| 1497 |
int scafEndDist=(map==null ? start : contigEndDist(map)); |
| 1498 |
|
| 1499 |
double cs=coverageScore(ploidy, rarity, readLengthAvg); |
| 1500 |
if(cs==0){return 0;} |
| 1501 |
double es=(useEdist ? edistScore() : 1); |
| 1502 |
double qs=qualityScore(totalQualityAvg, totalMapqAvg); |
| 1503 |
double ps=(usePairing ? pairedScore(properPairRate, scafEndDist) : 1); |
| 1504 |
double bs=(useBias ? biasScore(properPairRate, scafEndDist) : 1); |
| 1505 |
double is=(useIdentity ? identityScore() : 1); |
| 1506 |
double hs=(useHomopolymer ? homopolymerScore(map) : 1); |
| 1507 |
return Math.pow(es*qs*ps*bs*cs*is*hs, 0.2); |
| 1508 |
} |
| 1509 |
|
| 1510 |
public double edistScore(){ |
| 1511 |
double lengthAvg=lengthAvg(); |
| 1512 |
double edistAvg=((edistAvg()*2+endDistMax))*0.333333333333; |
| 1513 |
double constant=5+Tools.min(20, lengthAvg*0.1)+lengthAvg*0.01; |
| 1514 |
double weighted=Tools.max(0.05, edistAvg-Tools.min(constant, edistAvg*0.95)); |
| 1515 |
weighted=weighted*weighted; |
| 1516 |
return weighted/(weighted+4); |
| 1517 |
} |
| 1518 |
|
| 1519 |
public double identityScore(){ |
| 1520 |
double lengthAvg=lengthAvg(); |
| 1521 |
double idAvg=0.001f*(((identityAvg()+idMax))*0.5f); |
| 1522 |
double weighted=Tools.min(1, (idAvg*lengthAvg+(0.65f*Tools.max(1, readlen())))/lengthAvg); |
| 1523 |
weighted=0.75f+0.25f*weighted; |
| 1524 |
return weighted; |
| 1525 |
} |
| 1526 |
|
| 1527 |
public double qualityScore(double totalBaseqAvg, double totalMapqAvg){ |
| 1528 |
return baseQualityScore(totalBaseqAvg)*mapQualityScore(totalMapqAvg); |
| 1529 |
} |
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
public double baseQualityScore(double totalBaseqAvg){ |
| 1537 |
double bqAvg=baseQAvg(); |
| 1538 |
|
| 1539 |
if(totalBaseqAvg<32 && bqAvg<32){ |
| 1540 |
|
| 1541 |
double fudgeFactor1=0.75*(32-totalBaseqAvg); |
| 1542 |
double fudgeFactor2=0.75*(32-bqAvg); |
| 1543 |
totalBaseqAvg+=fudgeFactor1; |
| 1544 |
bqAvg+=Tools.min(fudgeFactor1, fudgeFactor2); |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
final double delta=totalBaseqAvg-bqAvg; |
| 1550 |
if(delta>0){ |
| 1551 |
bqAvg=Tools.max(bqAvg*0.5, bqAvg-0.5*delta); |
| 1552 |
} |
| 1553 |
|
| 1554 |
double mult=0.25; |
| 1555 |
double thresh=12; |
| 1556 |
if(bqAvg>thresh){ |
| 1557 |
bqAvg=bqAvg-thresh+(thresh*mult); |
| 1558 |
}else{ |
| 1559 |
bqAvg=bqAvg*mult; |
| 1560 |
} |
| 1561 |
|
| 1562 |
double baseProbAvg=1-Math.pow(10, 0-.1*bqAvg); |
| 1563 |
double d=baseProbAvg*baseProbAvg; |
| 1564 |
return d; |
| 1565 |
} |
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
public double mapQualityScore(double totalMapqAvg){ |
| 1580 |
double mqAvg=0.5f*(mapQAvg()+mapQMax); |
| 1581 |
|
| 1582 |
double mapProbAvg=1-Math.pow(10, 0-.1*(mqAvg+2)); |
| 1583 |
double d=mapProbAvg; |
| 1584 |
return d; |
| 1585 |
} |
| 1586 |
|
| 1587 |
public double pairedScore(double properPairRate, int scafEndDist){ |
| 1588 |
if(properPairRate<0.5){return 0.98;} |
| 1589 |
final double count=alleleCount(); |
| 1590 |
if(count==0){return 0;} |
| 1591 |
double rate=properPairCount/count; |
| 1592 |
rate=rate*(count/(0.1+count)); |
| 1593 |
if(rate*1.05>=properPairRate){return Tools.max(rate, 1-0.001*properPairRate);} |
| 1594 |
double score=((rate*1.05)/properPairRate)*0.5+0.5; |
| 1595 |
score=Tools.max(0.1, score); |
| 1596 |
return modifyByEndDist(score, scafEndDist); |
| 1597 |
} |
| 1598 |
|
| 1599 |
public double modifyByEndDist(double x, int scafEndDist){ |
| 1600 |
if(x>=0.99 || !doNscan || scafEndDist>=nScan){return x;} |
| 1601 |
if(scafEndDist<minEndDistForBias){return Tools.max(x, 0.98+0.02*x);} |
| 1602 |
double delta=1-x; |
| 1603 |
delta=delta*(scafEndDist*scafEndDist)/(nScan*nScan); |
| 1604 |
return 1-delta; |
| 1605 |
} |
| 1606 |
|
| 1607 |
public double coverageScore(int ploidy, double rarity, double readLengthAvg){ |
| 1608 |
int count=alleleCount(); |
| 1609 |
if(count==0){return 0;} |
| 1610 |
double rawScore=count/(lowCoveragePenalty+count); |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
double ratio=0.98; |
| 1615 |
if(coverage>0){ |
| 1616 |
double dif=coverage-count; |
| 1617 |
if(dif>0){ |
| 1618 |
dif=dif-coverage*.01f-Tools.min(0.5f, coverage*.1f); |
| 1619 |
dif=Tools.max(0.1f, dif); |
| 1620 |
} |
| 1621 |
ratio=(coverage-dif)/coverage; |
| 1622 |
if(type()==SUB && revisedAlleleFraction!=-1 && revisedAlleleFraction<ratio){ratio=revisedAlleleFraction;} |
| 1623 |
else{ |
| 1624 |
ratio=adjustForInsertionLength(ratio, readLengthAvg); |
| 1625 |
} |
| 1626 |
if(rarity<1 && ratio>rarity){ |
| 1627 |
double minExpected=1f/ploidy; |
| 1628 |
if(ratio<minExpected){ |
| 1629 |
ratio=minExpected-((minExpected-ratio)*0.1); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
} |
| 1633 |
|
| 1634 |
double ratio2=Tools.min(1, ploidy*ratio); |
| 1635 |
return rawScore*ratio2; |
| 1636 |
} |
| 1637 |
|
| 1638 |
public void reviseAlleleFraction(double readLengthAvg, Scaffold scaffold, VarMap map){ |
| 1639 |
assert(type()==INS); |
| 1640 |
final int ilen=readlen(); |
| 1641 |
if(ilen<3 || start<1 || start>=scaffold.length-2){return;} |
| 1642 |
final byte[] bases=scaffold.bases; |
| 1643 |
|
| 1644 |
final double afIns=alleleFraction(); |
| 1645 |
final double rafIns=revisedAlleleFraction(afIns, readLengthAvg); |
| 1646 |
final double revisedDif=0.55*(rafIns-afIns); |
| 1647 |
final double mult=revisedDif/allele.length; |
| 1648 |
|
| 1649 |
for(int i=0, j=start; i<allele.length && j<scaffold.bases.length; i++, j++){ |
| 1650 |
final byte b=allele[i]; |
| 1651 |
if(b!=bases[j]){ |
| 1652 |
Var key=new Var(scaffold.number, j, j+1, b, SUB); |
| 1653 |
Var affectedSub=map.get(key); |
| 1654 |
|
| 1655 |
if(affectedSub!=null){ |
| 1656 |
assert(key.type()==SUB); |
| 1657 |
|
| 1658 |
final double subModifier=revisedDif-mult*i; |
| 1659 |
synchronized(affectedSub){ |
| 1660 |
double afSub=affectedSub.alleleFraction(); |
| 1661 |
double rafSub=affectedSub.revisedAlleleFraction; |
| 1662 |
double modified=afSub-subModifier; |
| 1663 |
if(rafSub==-1){ |
| 1664 |
|
| 1665 |
affectedSub.revisedAlleleFraction=Tools.max(afSub*0.05, modified); |
| 1666 |
}else{ |
| 1667 |
affectedSub.revisedAlleleFraction=Tools.min(rafSub, Tools.max(afSub*0.05, modified)); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
} |
| 1671 |
} |
| 1672 |
} |
| 1673 |
|
| 1674 |
for(int i=0, j=start-1; i<allele.length && j>=0; i++, j--){ |
| 1675 |
final byte b=allele[allele.length-1-i]; |
| 1676 |
if(b!=bases[j]){ |
| 1677 |
Var key=new Var(scaffold.number, j, j+1, b, SUB); |
| 1678 |
Var affectedSub=map.get(key); |
| 1679 |
if(affectedSub!=null){ |
| 1680 |
assert(key.type()==SUB); |
| 1681 |
|
| 1682 |
final double subModifier=revisedDif-mult*i; |
| 1683 |
synchronized(affectedSub){ |
| 1684 |
double afSub=affectedSub.alleleFraction(); |
| 1685 |
double rafSub=affectedSub.revisedAlleleFraction; |
| 1686 |
double modified=afSub-subModifier; |
| 1687 |
if(rafSub==-1){ |
| 1688 |
|
| 1689 |
affectedSub.revisedAlleleFraction=Tools.max(afSub*0.05, modified); |
| 1690 |
}else{ |
| 1691 |
affectedSub.revisedAlleleFraction=Tools.min(rafSub, Tools.max(afSub*0.05, modified)); |
| 1692 |
} |
| 1693 |
} |
| 1694 |
} |
| 1695 |
} |
| 1696 |
} |
| 1697 |
} |
| 1698 |
|
| 1699 |
public double revisedAlleleFraction(double af, double readLengthAvg){ |
| 1700 |
if(revisedAlleleFraction!=-1){ |
| 1701 |
return revisedAlleleFraction; |
| 1702 |
}else if(type()==INS){ |
| 1703 |
return revisedAlleleFraction=adjustForInsertionLength(af, readLengthAvg); |
| 1704 |
} |
| 1705 |
return af; |
| 1706 |
} |
| 1707 |
|
| 1708 |
public double adjustForInsertionLength(final double ratio, final double rlen0){ |
| 1709 |
if(type()!=INS){return ratio;} |
| 1710 |
final int ilen=readlen(); |
| 1711 |
if(ilen<2){return ratio;} |
| 1712 |
|
| 1713 |
final double rlen=Tools.max(ilen*1.2+6, rlen0); |
| 1714 |
final double sites=rlen+ilen-1; |
| 1715 |
final double goodSites=rlen-ilen*1.1-6; |
| 1716 |
|
| 1717 |
final double expectedFraction=goodSites/sites; |
| 1718 |
final double revisedRatio=Tools.min(ratio/expectedFraction, 1-(1-ratio)*0.1); |
| 1719 |
return revisedRatio; |
| 1720 |
} |
| 1721 |
|
| 1722 |
public double homopolymerScore(ScafMap map){ |
| 1723 |
if(map==null){return 1;} |
| 1724 |
|
| 1725 |
int count=homopolymerCount(map); |
| 1726 |
|
| 1727 |
if(count<2){return 1;} |
| 1728 |
return 1f-(count*0.1f/9); |
| 1729 |
} |
| 1730 |
|
| 1731 |
public int homopolymerCount(ScafMap map){ |
| 1732 |
if(map==null){return 0;} |
| 1733 |
final byte[] bases=map.getScaffold(scafnum).bases; |
| 1734 |
if(bases==null){return 0;} |
| 1735 |
|
| 1736 |
final int type=type(); |
| 1737 |
if(type==SUB){ |
| 1738 |
assert(start==stop-1) : start+", "+stop; |
| 1739 |
final byte base=allele[0]; |
| 1740 |
int x=homopolymerCountSub(bases, start, base); |
| 1741 |
|
| 1742 |
return x; |
| 1743 |
}else if(type==INS){ |
| 1744 |
final byte base1=allele[0], base2=allele[allele.length-1]; |
| 1745 |
int i=0; |
| 1746 |
while(i<allele.length && allele[i]==base1){i++;} |
| 1747 |
while(i<allele.length && allele[i]==base2){i++;} |
| 1748 |
if(i<bases.length){return 0;} |
| 1749 |
int left=homopolymerCountLeft(bases, start, base1); |
| 1750 |
int right=homopolymerCountRight(bases, stop+1, base2); |
| 1751 |
|
| 1752 |
return left+right+1; |
| 1753 |
}else if(type==DEL){ |
| 1754 |
if(start<0 || start+1>=bases.length || stop<=0 || stop>=bases.length){return 0;} |
| 1755 |
final byte base1=bases[start+1], base2=bases[stop-1]; |
| 1756 |
int pos=start+1; |
| 1757 |
while(pos<=stop && bases[pos]==base1){pos++;} |
| 1758 |
while(pos<=stop && bases[pos]==base2){pos++;} |
| 1759 |
if(pos<=stop){return 0;} |
| 1760 |
int left=homopolymerCountLeft(bases, start, base1); |
| 1761 |
int right=homopolymerCountRight(bases, stop, base2); |
| 1762 |
|
| 1763 |
return left+right+1; |
| 1764 |
}else{ |
| 1765 |
|
| 1766 |
return 0; |
| 1767 |
} |
| 1768 |
} |
| 1769 |
|
| 1770 |
public static int homopolymerCountSub(final byte[] bases, final int pos, final byte base){ |
| 1771 |
|
| 1772 |
if(pos<0 || pos>=bases.length){return 0;} |
| 1773 |
if(!AminoAcid.isFullyDefined(base)){return 0;} |
| 1774 |
|
| 1775 |
|
| 1776 |
int count1=0; |
| 1777 |
for(int i=pos-1, lim=Tools.max(0, pos-4); i>=lim; i--){ |
| 1778 |
if(bases[i]==base){count1++;} |
| 1779 |
else{break;} |
| 1780 |
} |
| 1781 |
|
| 1782 |
|
| 1783 |
int count2=0; |
| 1784 |
for(int i=pos+1, lim=Tools.min(bases.length, pos+5); i<lim; i++){ |
| 1785 |
if(bases[i]==base){count2++;} |
| 1786 |
else{break;} |
| 1787 |
} |
| 1788 |
|
| 1789 |
|
| 1790 |
assert(count1+count2<=8) : count1+", "+count2; |
| 1791 |
|
| 1792 |
return count1+count2+(count1>0 && count2>0 ? 1 : 0); |
| 1793 |
} |
| 1794 |
|
| 1795 |
public static int homopolymerCountLeft(final byte[] bases, final int pos, final byte base){ |
| 1796 |
if(pos<0 || bases[pos]!=base){return 0;} |
| 1797 |
if(!AminoAcid.isFullyDefined(base)){return 0;} |
| 1798 |
|
| 1799 |
int count=0; |
| 1800 |
for(int i=pos, lim=Tools.max(0, pos-3); i>=lim; i--){ |
| 1801 |
if(bases[i]==base){count++;} |
| 1802 |
else{break;} |
| 1803 |
} |
| 1804 |
return count; |
| 1805 |
} |
| 1806 |
|
| 1807 |
public static int homopolymerCountRight(final byte[] bases, final int pos, final byte base){ |
| 1808 |
if(pos<0 || bases[pos]!=base){return 0;} |
| 1809 |
if(!AminoAcid.isFullyDefined(base)){return 0;} |
| 1810 |
|
| 1811 |
int count=0; |
| 1812 |
for(int i=pos, lim=Tools.min(bases.length, pos+4); i<lim; i++){ |
| 1813 |
if(bases[i]==base){count++;} |
| 1814 |
else{break;} |
| 1815 |
} |
| 1816 |
return count; |
| 1817 |
} |
| 1818 |
|
| 1819 |
public double biasScore(double properPairRate, int scafEndDist){ |
| 1820 |
double strandBias=strandBiasScore(scafEndDist); |
| 1821 |
double readBias=readBiasScore(properPairRate); |
| 1822 |
return Math.sqrt(strandBias*readBias); |
| 1823 |
} |
| 1824 |
|
| 1825 |
public double strandBiasScore(int scafEndDist){ |
| 1826 |
int plus=allelePlusCount(); |
| 1827 |
int minus=alleleMinusCount(); |
| 1828 |
final double x=eventProb(plus, minus); |
| 1829 |
final double x2=modifyByEndDist(x, scafEndDist); |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
double result=x2; |
| 1836 |
if(plus+minus>=20 && x2<0.9){ |
| 1837 |
int min=Tools.min(plus, minus); |
| 1838 |
int max=Tools.max(plus, minus); |
| 1839 |
if(min>1 && min>0.06f*max){ |
| 1840 |
double y=0.15+(0.2*min)/max; |
| 1841 |
result=y+(1-y)*x2; |
| 1842 |
} |
| 1843 |
} |
| 1844 |
|
| 1845 |
return result; |
| 1846 |
} |
| 1847 |
|
| 1848 |
|
| 1849 |
public double readBiasScore(double properPairRate){ |
| 1850 |
if(properPairRate<0.5){return 0.95f;} |
| 1851 |
final int r1=r1AlleleCount(), r2=r2AlleleCount(); |
| 1852 |
final double x=eventProb(r1, r2); |
| 1853 |
|
| 1854 |
|
| 1855 |
final double x2=0.10+0.90*x; |
| 1856 |
double result=x2; |
| 1857 |
if(r1+r2>=20 && x2<0.9){ |
| 1858 |
int min=Tools.min(r1, r2); |
| 1859 |
int max=Tools.max(r1, r2); |
| 1860 |
if(min>1 && min>0.07f*max){ |
| 1861 |
double y=0.15+(0.2*min)/max; |
| 1862 |
result=y+(1-y)*x2; |
| 1863 |
} |
| 1864 |
} |
| 1865 |
return result; |
| 1866 |
} |
| 1867 |
|
| 1868 |
|
| 1869 |
public static double eventProb(int a, int b){ |
| 1870 |
|
| 1871 |
double allowedBias=0.75; |
| 1872 |
double slopMult=0.95; |
| 1873 |
|
| 1874 |
double n=a+b; |
| 1875 |
double k=Tools.min(a, b); |
| 1876 |
|
| 1877 |
double slop=n*(allowedBias*0.5); |
| 1878 |
double dif=n-k*2; |
| 1879 |
dif=dif-(Tools.min(slop, dif)*slopMult); |
| 1880 |
n=k*2+dif; |
| 1881 |
|
| 1882 |
assert(k<=n*0.5) : a+", "+b+", "+n+", "+k+", "+slop+", "+dif; |
| 1883 |
|
| 1884 |
if(n>PROBLEN){ |
| 1885 |
double mult=PROBLEN/n; |
| 1886 |
n=PROBLEN; |
| 1887 |
k=(int)(k*mult); |
| 1888 |
} |
| 1889 |
|
| 1890 |
int n2=(int)Math.round(n); |
| 1891 |
int k2=Tools.min(n2/2, (int)(k+1)); |
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
double result=prob[n2][k2]; |
| 1899 |
if(result<1 || a==b || a+1==b || a==b+1){return result;} |
| 1900 |
|
| 1901 |
double slope=Tools.min(a, b)/(double)Tools.max(a, b); |
| 1902 |
return (0.998+slope*0.002); |
| 1903 |
} |
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
public int allelePlusCount(){return r1plus+r2plus;} |
| 1910 |
public int alleleMinusCount(){return r1minus+r2minus;} |
| 1911 |
public int r1AlleleCount(){return r1plus+r1minus;} |
| 1912 |
public int r2AlleleCount(){return r2plus+r2minus;} |
| 1913 |
public int alleleCount(){return r1plus+r1minus+r2plus+r2minus;} |
| 1914 |
|
| 1915 |
public double alleleFraction(){ |
| 1916 |
int count=alleleCount(); |
| 1917 |
int cov=Tools.max(count, coverage, 1); |
| 1918 |
return count/(double)cov; |
| 1919 |
} |
| 1920 |
|
| 1921 |
public double strandRatio(){ |
| 1922 |
int plus=allelePlusCount(); |
| 1923 |
int minus=alleleMinusCount(); |
| 1924 |
if(plus==minus){return 1;} |
| 1925 |
return (Tools.min(plus, minus)+1)/(double)Tools.max(plus, minus); |
| 1926 |
} |
| 1927 |
public double baseQAvg(){return baseQSum/(double)alleleCount();} |
| 1928 |
public double mapQAvg(){return mapQSum/(double)alleleCount();} |
| 1929 |
public double edistAvg(){return endDistSum/(double)alleleCount();} |
| 1930 |
public double identityAvg(){return idSum/(double)alleleCount();} |
| 1931 |
public double lengthAvg(){return lengthSum/(double)alleleCount();} |
| 1932 |
public double properPairRate(){return properPairCount/(double)alleleCount();} |
| 1933 |
|
| 1934 |
|
| 1935 |
public void setCoverage(int coverage_, int minusCoverage_){ |
| 1936 |
coverage=coverage_; |
| 1937 |
minusCoverage=minusCoverage_; |
| 1938 |
} |
| 1939 |
|
| 1940 |
public int coverage(){ |
| 1941 |
assert(coverage>-1) : coverage+", "+this; |
| 1942 |
return coverage; |
| 1943 |
} |
| 1944 |
|
| 1945 |
public boolean hasCoverage(){ |
| 1946 |
return coverage>-1; |
| 1947 |
} |
| 1948 |
|
| 1949 |
public int contigEndDist(ScafMap map){ |
| 1950 |
Scaffold scaf=map.getScaffold(scafnum); |
| 1951 |
int len=scaf.length; |
| 1952 |
byte[] bases=scaf.bases; |
| 1953 |
|
| 1954 |
int scafEndDist=Tools.max(0, Tools.min(start, len-stop)); |
| 1955 |
if(bases==null || nScan<1){return scafEndDist;} |
| 1956 |
int limit=Tools.min(nScan, scafEndDist); |
| 1957 |
int contigEndDist=leftContigEndDist(bases, limit); |
| 1958 |
limit=Tools.min(limit, contigEndDist); |
| 1959 |
contigEndDist=rightContigEndDist(bases, limit); |
| 1960 |
return Tools.min(scafEndDist, contigEndDist); |
| 1961 |
} |
| 1962 |
|
| 1963 |
public int leftContigEndDist(byte[] bases, int maxDist){ |
| 1964 |
if(start>=bases.length){return Tools.min(bases.length, maxDist+1);} |
| 1965 |
int ns=0; |
| 1966 |
for(int i=start, lim=Tools.max(0, start-maxDist); i>=lim; i--){ |
| 1967 |
if(AminoAcid.isFullyDefined(bases[i])){ |
| 1968 |
ns=0; |
| 1969 |
}else{ |
| 1970 |
ns++; |
| 1971 |
if(ns>=10){ |
| 1972 |
int x=start-i-ns+1; |
| 1973 |
assert(x>=0); |
| 1974 |
return x; |
| 1975 |
} |
| 1976 |
} |
| 1977 |
} |
| 1978 |
return maxDist+1; |
| 1979 |
} |
| 1980 |
|
| 1981 |
public int rightContigEndDist(byte[] bases, int maxDist){ |
| 1982 |
if(stop<0){return Tools.min(bases.length, maxDist+1);} |
| 1983 |
int ns=0; |
| 1984 |
for(int i=stop, lim=Tools.min(bases.length-1, stop+maxDist); i<=lim; i++){ |
| 1985 |
if(AminoAcid.isFullyDefined(bases[i])){ |
| 1986 |
ns=0; |
| 1987 |
}else{ |
| 1988 |
ns++; |
| 1989 |
if(ns>=10){ |
| 1990 |
int x=i-stop-ns+1; |
| 1991 |
assert(x>=0); |
| 1992 |
return x; |
| 1993 |
} |
| 1994 |
} |
| 1995 |
} |
| 1996 |
return maxDist+1; |
| 1997 |
} |
| 1998 |
|
| 1999 |
public String scafName(){ |
| 2000 |
return scafName(ScafMap.defaultScafMap()); |
| 2001 |
} |
| 2002 |
|
| 2003 |
public String scafName(ScafMap map){ |
| 2004 |
return map.getScaffold(scafnum).name; |
| 2005 |
} |
| 2006 |
|
| 2007 |
public Var setForced(boolean b){forced=b; return this;} |
| 2008 |
public boolean forced(){return forced;} |
| 2009 |
|
| 2010 |
public Var setFlagged(boolean b){flagged=b; return this;} |
| 2011 |
public boolean flagged(){return flagged;} |
| 2012 |
|
| 2013 |
|
| 2014 |
public final boolean ins(){return type==INS;} |
| 2015 |
public final boolean del(){return type==DEL;} |
| 2016 |
public final boolean indel(){return type==INS || type==DEL;} |
| 2017 |
public final boolean sub(){return type==SUB;} |
| 2018 |
public final boolean complex(){return type==COMPLEX;} |
| 2019 |
public final boolean frameshift(){ |
| 2020 |
int delta=Tools.absdif(reflen(), readlen()); |
| 2021 |
return delta%3!=0; |
| 2022 |
} |
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
public final int scafnum; |
| 2029 |
public final int start; |
| 2030 |
public final int stop; |
| 2031 |
public final byte[] allele; |
| 2032 |
public final int hashcode; |
| 2033 |
public final int type; |
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
private int coverage=-1; |
| 2040 |
private int minusCoverage=-1; |
| 2041 |
|
| 2042 |
int r1plus; |
| 2043 |
int r1minus; |
| 2044 |
int r2plus; |
| 2045 |
int r2minus; |
| 2046 |
int properPairCount; |
| 2047 |
|
| 2048 |
long mapQSum; |
| 2049 |
public int mapQMax; |
| 2050 |
|
| 2051 |
long baseQSum; |
| 2052 |
public int baseQMax; |
| 2053 |
|
| 2054 |
long endDistSum; |
| 2055 |
public int endDistMax; |
| 2056 |
|
| 2057 |
long idSum; |
| 2058 |
int idMax; |
| 2059 |
|
| 2060 |
long lengthSum; |
| 2061 |
|
| 2062 |
int nearbyVarCount=-1; |
| 2063 |
|
| 2064 |
double revisedAlleleFraction=-1; |
| 2065 |
private boolean forced=false; |
| 2066 |
private boolean flagged=false; |
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
public static boolean CALL_INS=true; |
| 2073 |
public static boolean CALL_DEL=true; |
| 2074 |
public static boolean CALL_SUB=true; |
| 2075 |
public static boolean CALL_NOCALL=false; |
| 2076 |
public static boolean CALL_JUNCTION=false; |
| 2077 |
|
| 2078 |
public static boolean extendedText=true; |
| 2079 |
|
| 2080 |
public static boolean noPassDotGenotype=false; |
| 2081 |
|
| 2082 |
public static boolean useHomopolymer=true; |
| 2083 |
public static boolean useIdentity=true; |
| 2084 |
public static boolean usePairing=true; |
| 2085 |
public static boolean useBias=true; |
| 2086 |
public static boolean useEdist=true; |
| 2087 |
public static boolean doNscan=true; |
| 2088 |
public static double lowCoveragePenalty=0.8; |
| 2089 |
public static int nScan=600; |
| 2090 |
public static int minEndDistForBias=200; |
| 2091 |
|
| 2092 |
public static int MIN_VAR_COPIES=0; |
| 2093 |
|
| 2094 |
final static int PROBLEN=100; |
| 2095 |
public static final boolean UPPER_CASE_ALLELES=true; |
| 2096 |
|
| 2097 |
private static final boolean TEST_REF_VARIANTS=false; |
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
private static final byte colon=';'; |
| 2103 |
private static final byte tab='\t'; |
| 2104 |
public static final String[] typeArray=new String[] {"INS","NOCALL","SUB","DEL","LJUNCT","RJUNCT","BJUNCT","MULTI","COMPLEX"}; |
| 2105 |
|
| 2106 |
public static final int INS=0; |
| 2107 |
|
| 2108 |
public static final int NOCALL=1; |
| 2109 |
|
| 2110 |
public static final int SUB=2; |
| 2111 |
|
| 2112 |
public static final int DEL=3; |
| 2113 |
|
| 2114 |
public static final int LJUNCT=4; |
| 2115 |
|
| 2116 |
public static final int RJUNCT=5; |
| 2117 |
|
| 2118 |
public static final int BJUNCT=6; |
| 2119 |
|
| 2120 |
public static final int MULTI=7; |
| 2121 |
|
| 2122 |
public static final int COMPLEX=8; |
| 2123 |
|
| 2124 |
|
| 2125 |
public static final int VAR_TYPES=COMPLEX+1; |
| 2126 |
|
| 2127 |
|
| 2128 |
static final byte[] typeInitialArray=new byte[128]; |
| 2129 |
|
| 2130 |
static final byte[] AL_0=new byte[0]; |
| 2131 |
static final byte[] AL_A=new byte[] {(byte)'A'}; |
| 2132 |
static final byte[] AL_C=new byte[] {(byte)'C'}; |
| 2133 |
static final byte[] AL_G=new byte[] {(byte)'G'}; |
| 2134 |
static final byte[] AL_T=new byte[] {(byte)'T'}; |
| 2135 |
static final byte[] AL_N=new byte[] {(byte)'N'}; |
| 2136 |
static final byte[][] AL_MAP=makeMap(); |
| 2137 |
static final int[] codes=makeCodes(); |
| 2138 |
|
| 2139 |
|
| 2140 |
private static final int typeBits=2; |
| 2141 |
private static final int alleleBits=6; |
| 2142 |
private static final int scafBits=16; |
| 2143 |
private static final int lenBits=8; |
| 2144 |
|
| 2145 |
private static final int alleleShift=typeBits; |
| 2146 |
private static final int scafShift=alleleShift+alleleBits; |
| 2147 |
private static final int lenShift=scafShift+scafBits; |
| 2148 |
private static final int startShift=lenShift+lenBits; |
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
static final int[] makeCodes(){ |
| 2154 |
Random randy=new Random(1); |
| 2155 |
int[] array=new int[256]; |
| 2156 |
for(int i=0; i<array.length; i++){ |
| 2157 |
array[i]=randy.nextInt(); |
| 2158 |
} |
| 2159 |
return array; |
| 2160 |
} |
| 2161 |
|
| 2162 |
static final byte[][] makeMap(){ |
| 2163 |
byte[][] map=new byte[128][]; |
| 2164 |
map[0]=map['.']=map['\t']=AL_0; |
| 2165 |
map['A']=map['a']=AL_A; |
| 2166 |
map['C']=map['c']=AL_C; |
| 2167 |
map['G']=map['g']=AL_G; |
| 2168 |
map['T']=map['t']=AL_T; |
| 2169 |
map['N']=map['n']=AL_N; |
| 2170 |
for(int i=0; i<map.length; i++){ |
| 2171 |
if(map[i]==null){map[i]=new byte[(byte)i];} |
| 2172 |
} |
| 2173 |
return map; |
| 2174 |
} |
| 2175 |
|
| 2176 |
|
| 2177 |
private static final double[] factorial=makeFactorialArray(PROBLEN+1); |
| 2178 |
|
| 2179 |
private static final double[][] binomial=makeBinomialMatrix(PROBLEN+1); |
| 2180 |
|
| 2181 |
private static final double[][] prob=makeProbMatrix(PROBLEN+1); |
| 2182 |
|
| 2183 |
private static double[] makeFactorialArray(int len) { |
| 2184 |
double[] x=new double[len]; |
| 2185 |
x[0]=1; |
| 2186 |
for(int i=1; i<len; i++){ |
| 2187 |
x[i]=x[i-1]*i; |
| 2188 |
} |
| 2189 |
return x; |
| 2190 |
} |
| 2191 |
|
| 2192 |
private static double[][] makeBinomialMatrix(int len) { |
| 2193 |
double[][] matrix=new double[len][]; |
| 2194 |
for(int n=0; n<len; n++){ |
| 2195 |
final int kmax=n/2; |
| 2196 |
final double nf=factorial[n]; |
| 2197 |
matrix[n]=new double[kmax+1]; |
| 2198 |
for(int k=0; k<=kmax; k++){ |
| 2199 |
final double kf=factorial[k]; |
| 2200 |
final double nmkf=factorial[n-k]; |
| 2201 |
double combinations=nf/kf; |
| 2202 |
combinations=combinations/nmkf; |
| 2203 |
matrix[n][k]=combinations; |
| 2204 |
} |
| 2205 |
} |
| 2206 |
return matrix; |
| 2207 |
} |
| 2208 |
|
| 2209 |
|
| 2210 |
private static double bigBinomial(int n, int k){ |
| 2211 |
|
| 2212 |
double combinations=1; |
| 2213 |
for(int a=-1, b=-1, c=-1; a<=n || b<=k || c<=n-k; ) { |
| 2214 |
double mult=1; |
| 2215 |
if(combinations<1000000000 && a<=n){ |
| 2216 |
a++; |
| 2217 |
mult=Tools.max(1, a); |
| 2218 |
}else if(b<=k){ |
| 2219 |
b++; |
| 2220 |
mult=1.0/Tools.max(1, a); |
| 2221 |
}else if(c<=n-k){ |
| 2222 |
c++; |
| 2223 |
mult=1.0/Tools.max(1, a); |
| 2224 |
}else{ |
| 2225 |
assert(false) : a+", "+b+", "+c+", "+n+", "+k+", "+(n-k)+", "+combinations; |
| 2226 |
} |
| 2227 |
combinations*=mult; |
| 2228 |
assert(combinations<Double.MAX_VALUE) : a+", "+b+", "+c+", "+n+", "+k+", "+(n-k)+", "+combinations; |
| 2229 |
} |
| 2230 |
|
| 2231 |
return combinations; |
| 2232 |
} |
| 2233 |
|
| 2234 |
private static double[][] makeProbMatrix(int len) { |
| 2235 |
double[][] matrix=new double[len][]; |
| 2236 |
double mult=2; |
| 2237 |
for(int n=0; n<len; n++){ |
| 2238 |
final int kmax=n/2; |
| 2239 |
final double[] array=matrix[n]=new double[kmax+1]; |
| 2240 |
for(int k=0; k<=kmax; k++){ |
| 2241 |
final double combinations=binomial[n][k]; |
| 2242 |
array[k]=combinations*mult; |
| 2243 |
} |
| 2244 |
|
| 2245 |
for(int k=0; k<=kmax; k++){ |
| 2246 |
array[k]=Tools.min(1, (k==0 ? 0 : array[k-1])+array[k]); |
| 2247 |
} |
| 2248 |
|
| 2249 |
|
| 2250 |
mult*=0.5; |
| 2251 |
|
| 2252 |
} |
| 2253 |
return matrix; |
| 2254 |
} |
| 2255 |
|
| 2256 |
static { |
| 2257 |
Arrays.fill(typeInitialArray, (byte)-1); |
| 2258 |
typeInitialArray['I']=INS; |
| 2259 |
typeInitialArray['N']=NOCALL; |
| 2260 |
typeInitialArray['S']=SUB; |
| 2261 |
typeInitialArray['D']=DEL; |
| 2262 |
typeInitialArray['L']=LJUNCT; |
| 2263 |
typeInitialArray['R']=RJUNCT; |
| 2264 |
typeInitialArray['B']=BJUNCT; |
| 2265 |
typeInitialArray['M']=MULTI; |
| 2266 |
typeInitialArray['C']=COMPLEX; |
| 2267 |
} |
| 2268 |
|
| 2269 |
public static final String varFormat="1.3"; |
| 2270 |
} |
| 2271 |
|