| 1 |
package var2; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Arrays; |
| 5 |
import java.util.Collections; |
| 6 |
import java.util.HashMap; |
| 7 |
|
| 8 |
import shared.Parse; |
| 9 |
import shared.Tools; |
| 10 |
import structures.ByteBuilder; |
| 11 |
|
| 12 |
public class VCFLine implements Comparable<VCFLine>, Cloneable { |
| 13 |
|
| 14 |
public VCFLine(String scaf_, int pos_, byte[] id_, byte[] ref_, byte[] alt_, double qual_, |
| 15 |
byte[] filter_, byte[] info_, byte[] format_, int type_, ArrayList<byte[]> samples_) { |
| 16 |
scaf=scaf_; |
| 17 |
pos=pos_; |
| 18 |
id=id_; |
| 19 |
ref=ref_; |
| 20 |
reflen=ref[0]=='.' ? 0 : ref.length; |
| 21 |
alt=alt_; |
| 22 |
qual=qual_; |
| 23 |
filter=filter_; |
| 24 |
info=info_; |
| 25 |
format=format_; |
| 26 |
type=type_; |
| 27 |
if(samples_!=null) { |
| 28 |
samples.addAll(samples_); |
| 29 |
} |
| 30 |
hashcode=hash(); |
| 31 |
} |
| 32 |
|
| 33 |
public VCFLine(byte[] line) { |
| 34 |
int a=0, b=0; |
| 35 |
|
| 36 |
while(b<line.length && line[b]!='\t'){b++;} |
| 37 |
assert(b>a) : "Missing field 0: "+new String(line); |
| 38 |
scaf=new String(line, a, b-a); |
| 39 |
b++; |
| 40 |
a=b; |
| 41 |
|
| 42 |
while(b<line.length && line[b]!='\t'){b++;} |
| 43 |
assert(b>a) : "Missing field 1: "+new String(line); |
| 44 |
pos=Parse.parseInt(line, a, b); |
| 45 |
b++; |
| 46 |
a=b; |
| 47 |
|
| 48 |
while(b<line.length && line[b]!='\t'){b++;} |
| 49 |
assert(b>a) : "Missing field 2: "+new String(line); |
| 50 |
id=line[a]=='.' ? DOT : Arrays.copyOfRange(line, a, b); |
| 51 |
b++; |
| 52 |
a=b; |
| 53 |
|
| 54 |
while(b<line.length && line[b]!='\t'){b++;} |
| 55 |
assert(b>a) : "Missing field 3: "+new String(line); |
| 56 |
if(b<=a+1){ref=Var.AL_MAP[line[a]];} |
| 57 |
else{ref=Arrays.copyOfRange(line, a, b);} |
| 58 |
|
| 59 |
reflen=line[a]=='.' ? 0 : b-a; |
| 60 |
b++; |
| 61 |
a=b; |
| 62 |
|
| 63 |
while(b<line.length && line[b]!='\t'){b++;} |
| 64 |
assert(b>a) : "Missing field 4: "+new String(line); |
| 65 |
if(b<=a+1){alt=Var.AL_MAP[line[a]];} |
| 66 |
else{alt=Arrays.copyOfRange(line, a, b);} |
| 67 |
b++; |
| 68 |
a=b; |
| 69 |
|
| 70 |
while(b<line.length && line[b]!='\t'){b++;} |
| 71 |
assert(b>a) : "Missing field 5: "+new String(line); |
| 72 |
qual=(line[a]=='.' && b==a+1 ? 40 : Parse.parseDouble(line, a, b)); |
| 73 |
b++; |
| 74 |
a=b; |
| 75 |
|
| 76 |
while(b<line.length && line[b]!='\t'){b++;} |
| 77 |
assert(b>a) : "Missing field 6: "+new String(line); |
| 78 |
filter=Arrays.copyOfRange(line, a, b); |
| 79 |
b++; |
| 80 |
a=b; |
| 81 |
|
| 82 |
while(b<line.length && line[b]!='\t'){b++;} |
| 83 |
assert(b>a) : "Missing field 7: "+new String(line); |
| 84 |
info=Arrays.copyOfRange(line, a, b); |
| 85 |
b++; |
| 86 |
a=b; |
| 87 |
|
| 88 |
int TYP=Tools.indexOfDelimited(info, "TYP=", 0, (byte)';'); |
| 89 |
type=(TYP>=0 ? Var.typeInitialArray[info[TYP+4]] : type_old()); |
| 90 |
assert(type>=0) : type+", "+TYP+"\n"+new String(info); |
| 91 |
|
| 92 |
while(b<line.length && line[b]!='\t'){b++;} |
| 93 |
|
| 94 |
if(b>a){ |
| 95 |
format=Arrays.copyOfRange(line, a, b); |
| 96 |
b++; |
| 97 |
a=b; |
| 98 |
} |
| 99 |
|
| 100 |
while(b<line.length){ |
| 101 |
while(b<line.length && line[b]!='\t'){b++;} |
| 102 |
if(b<=a){ |
| 103 |
break; |
| 104 |
} |
| 105 |
byte[] sample=Arrays.copyOfRange(line, a, b); |
| 106 |
samples.add(sample); |
| 107 |
b++; |
| 108 |
a=b; |
| 109 |
} |
| 110 |
|
| 111 |
if(TRIM_TO_CANONICAL){trimToCanonical();} |
| 112 |
|
| 113 |
|
| 114 |
hashcode=hash(); |
| 115 |
if(AUTOCACHE){cache();} |
| 116 |
} |
| 117 |
|
| 118 |
public Var toVar(){ |
| 119 |
return makeVar(info, alt); |
| 120 |
} |
| 121 |
|
| 122 |
public static Var makeVar(byte[] info, byte[] alt){ |
| 123 |
int a=0, b=0; |
| 124 |
|
| 125 |
|
| 126 |
assert(Tools.startsWith(info, "SN", a)); |
| 127 |
while(b<info.length && info[b]!='='){b++;} |
| 128 |
a=b+1; |
| 129 |
while(b<info.length && info[b]!=';'){b++;} |
| 130 |
assert(b>a) : "Missing field 0: "+new String(info); |
| 131 |
int scaf=Parse.parseInt(info, a, b); |
| 132 |
b++; |
| 133 |
a=b; |
| 134 |
|
| 135 |
while(b<info.length && info[b]!='='){b++;} |
| 136 |
a=b+1; |
| 137 |
while(b<info.length && info[b]!=';'){b++;} |
| 138 |
assert(b>a) : "Missing field 1: "+new String(info); |
| 139 |
int start=Parse.parseInt(info, a, b); |
| 140 |
b++; |
| 141 |
a=b; |
| 142 |
|
| 143 |
while(b<info.length && info[b]!='='){b++;} |
| 144 |
a=b+1; |
| 145 |
while(b<info.length && info[b]!=';'){b++;} |
| 146 |
assert(b>a) : "Missing field 2: "+new String(info); |
| 147 |
int stop=Parse.parseInt(info, a, b); |
| 148 |
b++; |
| 149 |
a=b; |
| 150 |
|
| 151 |
|
| 152 |
while(b<info.length && info[b]!='='){b++;} |
| 153 |
a=b+1; |
| 154 |
while(b<info.length && info[b]!=';'){b++;} |
| 155 |
assert(b>a) : "Missing field 3: "+new String(info); |
| 156 |
final int type=Var.typeInitialArray[info[a]]; |
| 157 |
assert(type>=0) : type+new String(info); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
b++; |
| 164 |
a=b; |
| 165 |
|
| 166 |
|
| 167 |
assert(Tools.startsWith(info, "R1P", a)); |
| 168 |
while(b<info.length && info[b]!='='){b++;} |
| 169 |
a=b+1; |
| 170 |
while(b<info.length && info[b]!=';'){b++;} |
| 171 |
assert(b>a) : "Missing field 4: "+new String(info); |
| 172 |
int r1p=Parse.parseInt(info, a, b); |
| 173 |
b++; |
| 174 |
a=b; |
| 175 |
|
| 176 |
while(b<info.length && info[b]!='='){b++;} |
| 177 |
a=b+1; |
| 178 |
while(b<info.length && info[b]!=';'){b++;} |
| 179 |
assert(b>a) : "Missing field 5: "+new String(info); |
| 180 |
int r1m=Parse.parseInt(info, a, b); |
| 181 |
b++; |
| 182 |
a=b; |
| 183 |
|
| 184 |
while(b<info.length && info[b]!='='){b++;} |
| 185 |
a=b+1; |
| 186 |
while(b<info.length && info[b]!=';'){b++;} |
| 187 |
assert(b>a) : "Missing field 6: "+new String(info); |
| 188 |
int r2p=Parse.parseInt(info, a, b); |
| 189 |
b++; |
| 190 |
a=b; |
| 191 |
|
| 192 |
while(b<info.length && info[b]!='='){b++;} |
| 193 |
a=b+1; |
| 194 |
while(b<info.length && info[b]!=';'){b++;} |
| 195 |
assert(b>a) : "Missing field 7: "+new String(info); |
| 196 |
int r2m=Parse.parseInt(info, a, b); |
| 197 |
b++; |
| 198 |
a=b; |
| 199 |
|
| 200 |
|
| 201 |
assert(Tools.startsWith(info, "AD=", a)); |
| 202 |
while(b<info.length && info[b]!='='){b++;} |
| 203 |
a=b+1; |
| 204 |
while(b<info.length && info[b]!=';'){b++;} |
| 205 |
assert(b>a) : "Missing field 8: "+new String(info); |
| 206 |
|
| 207 |
b++; |
| 208 |
a=b; |
| 209 |
|
| 210 |
while(b<info.length && info[b]!='='){b++;} |
| 211 |
a=b+1; |
| 212 |
while(b<info.length && info[b]!=';'){b++;} |
| 213 |
assert(b>a) : "Missing field 9: "+new String(info); |
| 214 |
int cov=Parse.parseInt(info, a, b); |
| 215 |
b++; |
| 216 |
a=b; |
| 217 |
|
| 218 |
while(b<info.length && info[b]!='='){b++;} |
| 219 |
a=b+1; |
| 220 |
while(b<info.length && info[b]!=';'){b++;} |
| 221 |
assert(b>a) : "Missing field 10: "+new String(info); |
| 222 |
int mcov=Parse.parseInt(info, a, b); |
| 223 |
b++; |
| 224 |
a=b; |
| 225 |
|
| 226 |
assert(Tools.startsWith(info, "PPC", a)); |
| 227 |
while(b<info.length && info[b]!='='){b++;} |
| 228 |
a=b+1; |
| 229 |
while(b<info.length && info[b]!=';'){b++;} |
| 230 |
assert(b>a) : "Missing field 11: "+new String(info); |
| 231 |
int pc=Parse.parseInt(info, a, b); |
| 232 |
b++; |
| 233 |
a=b; |
| 234 |
|
| 235 |
|
| 236 |
while(b<info.length && info[b]!='='){b++;} |
| 237 |
a=b+1; |
| 238 |
while(b<info.length && info[b]!=';'){b++;} |
| 239 |
assert(b>a) : "Missing field 12: "+new String(info); |
| 240 |
|
| 241 |
b++; |
| 242 |
a=b; |
| 243 |
|
| 244 |
assert(Tools.startsWith(info, "RAF", a)); |
| 245 |
while(b<info.length && info[b]!='='){b++;} |
| 246 |
a=b+1; |
| 247 |
while(b<info.length && info[b]!=';'){b++;} |
| 248 |
assert(b>a) : "Missing field 13: "+new String(info); |
| 249 |
double raf=Parse.parseDouble(info, a, b); |
| 250 |
b++; |
| 251 |
a=b; |
| 252 |
|
| 253 |
while(b<info.length && info[b]!='='){b++;} |
| 254 |
a=b+1; |
| 255 |
while(b<info.length && info[b]!=';'){b++;} |
| 256 |
assert(b>a) : "Missing field 14: "+new String(info); |
| 257 |
long ls=Parse.parseLong(info, a, b); |
| 258 |
b++; |
| 259 |
a=b; |
| 260 |
|
| 261 |
|
| 262 |
assert(Tools.startsWith(info, "MQS", a)); |
| 263 |
while(b<info.length && info[b]!='='){b++;} |
| 264 |
a=b+1; |
| 265 |
while(b<info.length && info[b]!=';'){b++;} |
| 266 |
assert(b>a) : "Missing field 15: "+new String(info); |
| 267 |
long mqs=Parse.parseLong(info, a, b); |
| 268 |
b++; |
| 269 |
a=b; |
| 270 |
|
| 271 |
while(b<info.length && info[b]!='='){b++;} |
| 272 |
a=b+1; |
| 273 |
while(b<info.length && info[b]!=';'){b++;} |
| 274 |
assert(b>a) : "Missing field 16: "+new String(info); |
| 275 |
int mqm=Parse.parseInt(info, a, b); |
| 276 |
b++; |
| 277 |
a=b; |
| 278 |
|
| 279 |
assert(Tools.startsWith(info, "BQS", a)); |
| 280 |
while(b<info.length && info[b]!='='){b++;} |
| 281 |
a=b+1; |
| 282 |
while(b<info.length && info[b]!=';'){b++;} |
| 283 |
assert(b>a) : "Missing field 17: "+new String(info); |
| 284 |
long bqs=Parse.parseLong(info, a, b); |
| 285 |
b++; |
| 286 |
a=b; |
| 287 |
|
| 288 |
while(b<info.length && info[b]!='='){b++;} |
| 289 |
a=b+1; |
| 290 |
while(b<info.length && info[b]!=';'){b++;} |
| 291 |
assert(b>a) : "Missing field 18: "+new String(info); |
| 292 |
int bqm=Parse.parseInt(info, a, b); |
| 293 |
b++; |
| 294 |
a=b; |
| 295 |
|
| 296 |
|
| 297 |
while(b<info.length && info[b]!='='){b++;} |
| 298 |
a=b+1; |
| 299 |
while(b<info.length && info[b]!=';'){b++;} |
| 300 |
assert(b>a) : "Missing field 19: "+new String(info); |
| 301 |
long eds=Parse.parseLong(info, a, b); |
| 302 |
b++; |
| 303 |
a=b; |
| 304 |
|
| 305 |
while(b<info.length && info[b]!='='){b++;} |
| 306 |
a=b+1; |
| 307 |
while(b<info.length && info[b]!=';'){b++;} |
| 308 |
assert(b>a) : "Missing field 20: "+new String(info); |
| 309 |
int edm=Parse.parseInt(info, a, b); |
| 310 |
b++; |
| 311 |
a=b; |
| 312 |
|
| 313 |
assert(Tools.startsWith(info, "IDS", a)); |
| 314 |
while(b<info.length && info[b]!='='){b++;} |
| 315 |
a=b+1; |
| 316 |
while(b<info.length && info[b]!=';'){b++;} |
| 317 |
assert(b>a) : "Missing field 21: "+new String(info); |
| 318 |
long ids=Parse.parseLong(info, a, b); |
| 319 |
b++; |
| 320 |
a=b; |
| 321 |
|
| 322 |
while(b<info.length && info[b]!='='){b++;} |
| 323 |
a=b+1; |
| 324 |
while(b<info.length && info[b]!=';'){b++;} |
| 325 |
assert(b>a) : "Missing field 22: "+new String(info); |
| 326 |
int idm=Parse.parseInt(info, a, b); |
| 327 |
b++; |
| 328 |
a=b; |
| 329 |
|
| 330 |
assert(Tools.startsWith(info, "NVC", a)); |
| 331 |
while(b<info.length && info[b]!='='){b++;} |
| 332 |
a=b+1; |
| 333 |
while(b<info.length && info[b]!=';'){b++;} |
| 334 |
assert(b>a) : "Missing field 23: "+new String(info); |
| 335 |
int nvc=Parse.parseInt(info, a, b); |
| 336 |
b++; |
| 337 |
a=b; |
| 338 |
|
| 339 |
assert(Tools.startsWith(info, "FLG", a)); |
| 340 |
while(b<info.length && info[b]!='='){b++;} |
| 341 |
a=b+1; |
| 342 |
while(b<info.length && info[b]!=';'){b++;} |
| 343 |
assert(b>a) : "Missing field 24: "+new String(info); |
| 344 |
int flg=Parse.parseInt(info, a, b); |
| 345 |
b++; |
| 346 |
a=b; |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
if(type==Var.DEL || type==Var.INS){ |
| 385 |
if(alt.length<=1){alt=Var.AL_0;} |
| 386 |
else if(alt.length==2){alt=Var.AL_MAP[alt[1]];} |
| 387 |
else{alt=Arrays.copyOfRange(alt, 1, alt.length);} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
Var v=new Var(scaf, start, stop, alt, type); |
| 393 |
v.r1plus=r1p; |
| 394 |
v.r1minus=r1m; |
| 395 |
v.r2plus=r2p; |
| 396 |
v.r2minus=r2m; |
| 397 |
v.properPairCount=pc; |
| 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.setFlagged(flg>0); |
| 409 |
v.revisedAlleleFraction=raf; |
| 410 |
v.setCoverage(cov, mcov); |
| 411 |
|
| 412 |
|
| 413 |
return v; |
| 414 |
} |
| 415 |
|
| 416 |
public ArrayList<VCFLine> split(boolean splitAlleles, boolean splitComplex, boolean splitSubs){ |
| 417 |
assert(splitAlleles || splitComplex || splitSubs); |
| 418 |
if(isSimple()){return null;} |
| 419 |
if(isJunction()) {assert(false) : "TODO";} |
| 420 |
|
| 421 |
if(!isMulti()){splitAlleles=false;} |
| 422 |
|
| 423 |
|
| 424 |
assert(!splitComplex) : "splitComplex function is written, but not integrated."; |
| 425 |
|
| 426 |
if(splitAlleles){ |
| 427 |
ArrayList<VCFLine> list0=splitAlleles(); |
| 428 |
if(!splitSubs || list0==null){ |
| 429 |
if(SORT && list0!=null){Collections.sort(list0);} |
| 430 |
if(CONDENSE){condense(list0);} |
| 431 |
return list0; |
| 432 |
} |
| 433 |
ArrayList<VCFLine> list2=new ArrayList<VCFLine>(); |
| 434 |
for(VCFLine line0 : list0){ |
| 435 |
ArrayList<VCFLine> list1=null; |
| 436 |
if(line0.type==Var.SUB && line0.alt.length>1){ |
| 437 |
list1=line0.splitSubs(); |
| 438 |
assert(list1!=null) : "\n"+this+"\n"+line0+"\n"; |
| 439 |
} |
| 440 |
if(list1!=null){list2.addAll(list1);} |
| 441 |
else{list2.add(line0);} |
| 442 |
} |
| 443 |
if(SORT && list2!=null){Collections.sort(list2);} |
| 444 |
if(CONDENSE){condense(list2);} |
| 445 |
return list2; |
| 446 |
} |
| 447 |
|
| 448 |
if(type!=Var.SUB || alt.length<2){splitSubs=false;} |
| 449 |
if(!splitSubs || isMulti()){return null;} |
| 450 |
|
| 451 |
ArrayList<VCFLine> list=splitSubs(); |
| 452 |
return list; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
private ArrayList<VCFLine> splitAlleles(){ |
| 457 |
assert(isMulti()) : this; |
| 458 |
if(Tools.indexOf(alt, ',')<0){ |
| 459 |
assert(false); |
| 460 |
return null; |
| 461 |
} |
| 462 |
String alleles=new String(alt); |
| 463 |
String[] split=alleles.split(","); |
| 464 |
ArrayList<VCFLine> list=new ArrayList<VCFLine>(split.length); |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
final String[] splitInfo=(info==null || !SPLIT_INFO ? null : new String(info).split(";")); |
| 470 |
|
| 471 |
for(int i=0; i<split.length; i++){ |
| 472 |
final String s=split[i]; |
| 473 |
VCFLine line=this.clone(); |
| 474 |
line.alt=s.getBytes(); |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
line.recalc(); |
| 479 |
|
| 480 |
|
| 481 |
if(!line.isRef()){ |
| 482 |
if(TRIM_TO_CANONICAL){line.trimToCanonical();} |
| 483 |
if(SPLIT_INFO){line.info=splitInfo(splitInfo, i, split.length);} |
| 484 |
|
| 485 |
list.add(line); |
| 486 |
} |
| 487 |
} |
| 488 |
return list.size()==0 ? null : list; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
private ArrayList<VCFLine> splitSubs(){ |
| 494 |
assert(type==Var.SUB); |
| 495 |
assert(alt.length>1); |
| 496 |
assert(Tools.indexOf(alt, ',')<0) : toString(); |
| 497 |
assert(alt.length==ref.length) : this; |
| 498 |
ArrayList<VCFLine> list=new ArrayList<VCFLine>(alt.length); |
| 499 |
for(int i=0; i<alt.length; i++){ |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
VCFLine line=new VCFLine(scaf, pos+i, id, Var.AL_MAP[ref[i]], Var.AL_MAP[alt[i]], qual, filter, info, format, type, samples); |
| 507 |
if(!line.isRef() && (Var.CALL_NOCALL || !line.isNocall())){ |
| 508 |
if(TRIM_TO_CANONICAL){line.trimToCanonical();} |
| 509 |
list.add(line); |
| 510 |
} |
| 511 |
} |
| 512 |
return list.size()==0 ? null : list; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
private ArrayList<VCFLine> splitComplex(){ |
| 521 |
assert(type==Var.COMPLEX); |
| 522 |
assert(alt.length>1); |
| 523 |
assert(Tools.indexOf(alt, ',')<0) : toString(); |
| 524 |
assert(alt.length!=ref.length && alt.length>1 && ref.length>1) : this; |
| 525 |
|
| 526 |
if(TRIM_TO_CANONICAL){this.trimToCanonical();} |
| 527 |
ArrayList<VCFLine> list=new ArrayList<VCFLine>(2); |
| 528 |
|
| 529 |
final int prefixLen=Tools.min(alt.length, ref.length)-1; |
| 530 |
final byte[] prefixA=prefix(alt, prefixLen); |
| 531 |
final byte[] prefixR=prefix(ref, prefixLen); |
| 532 |
final byte[] suffixA=suffix(alt, alt.length-prefixLen); |
| 533 |
final byte[] suffixR=suffix(ref, ref.length-prefixLen); |
| 534 |
VCFLine sub=new VCFLine(scaf, pos, id, prefixR, prefixA, qual, filter, info, format, type, samples); |
| 535 |
VCFLine indel=new VCFLine(scaf, pos, id, suffixR, suffixA, qual, filter, info, format, type, samples); |
| 536 |
|
| 537 |
assert(sub.isSub()); |
| 538 |
if(alt.length<ref.length){ |
| 539 |
assert(indel.isDel()); |
| 540 |
}else if(alt.length>ref.length){ |
| 541 |
assert(indel.isIns()); |
| 542 |
}else{ |
| 543 |
assert(false) : this; |
| 544 |
throw new RuntimeException("Unreachable"); |
| 545 |
} |
| 546 |
|
| 547 |
if(!sub.isRef() && (Var.CALL_NOCALL || !sub.isNocall())){list.add(sub);} |
| 548 |
list.add(indel); |
| 549 |
return list; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
private byte[] splitInfo(String[] splitInfo, int alleleNum, int alleles){ |
| 555 |
if(splitInfo==null){return null;} |
| 556 |
assert(alleles>1); |
| 557 |
assert(alleleNum>=0 && alleleNum<alleles); |
| 558 |
ByteBuilder bb=new ByteBuilder(); |
| 559 |
for(String part : splitInfo){ |
| 560 |
if(bb.length()>0){bb.append(';');} |
| 561 |
if(part.indexOf(',')<0){ |
| 562 |
bb.append(part); |
| 563 |
}else{ |
| 564 |
String[] splitEquals=part.split("="); |
| 565 |
String[] splitComma=splitEquals[1].split(","); |
| 566 |
if(splitComma.length==alleles){ |
| 567 |
bb.append(splitEquals[0]).append('=').append(splitComma[alleleNum]); |
| 568 |
}else{ |
| 569 |
bb.append(part); |
| 570 |
} |
| 571 |
} |
| 572 |
} |
| 573 |
return bb.toBytes(); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
private static int condense(ArrayList<VCFLine> list){ |
| 578 |
if(list==null || list.size()<2){return 0;} |
| 579 |
int removed=0; |
| 580 |
VCFLine prev=list.get(0); |
| 581 |
for(int i=1; i<list.size(); i++){ |
| 582 |
VCFLine line=list.get(i); |
| 583 |
if(prev.equals(line)){ |
| 584 |
list.set(i, null); |
| 585 |
removed++; |
| 586 |
}else{ |
| 587 |
prev=line; |
| 588 |
} |
| 589 |
} |
| 590 |
if(removed>0){Tools.condenseStrict(list);} |
| 591 |
return removed; |
| 592 |
} |
| 593 |
|
| 594 |
@Override |
| 595 |
public VCFLine clone(){ |
| 596 |
try { |
| 597 |
return (VCFLine)super.clone(); |
| 598 |
} catch (CloneNotSupportedException e) { |
| 599 |
|
| 600 |
e.printStackTrace(); |
| 601 |
throw new RuntimeException(); |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
private int trimToCanonical(){ |
| 608 |
if(isMulti() || alt.length<2 || ref.length<2 ){return 0;} |
| 609 |
assert(Tools.indexOf(alt, ',')<0) : this; |
| 610 |
|
| 611 |
int s=trimSuffix(); |
| 612 |
int p=trimPrefix(); |
| 613 |
int delta=s+p; |
| 614 |
if(delta>0){recalc();} |
| 615 |
assert(!isRef()) : this; |
| 616 |
return delta; |
| 617 |
} |
| 618 |
|
| 619 |
private int trimPrefix(){ |
| 620 |
int prefix=0; |
| 621 |
for(int rpos=0, apos=0; rpos<ref.length-1 && apos<alt.length-1 && ref[rpos]==alt[apos]; rpos++, apos++){ |
| 622 |
prefix++; |
| 623 |
} |
| 624 |
if(prefix>0){ |
| 625 |
pos+=prefix; |
| 626 |
reflen=ref.length-prefix; |
| 627 |
int altlen=alt.length-prefix; |
| 628 |
ref=(reflen==1 ? Var.AL_MAP[ref[ref.length-1]] : Arrays.copyOfRange(ref, prefix, ref.length)); |
| 629 |
alt=(altlen==1 ? Var.AL_MAP[alt[alt.length-1]] : Arrays.copyOfRange(alt, prefix, alt.length)); |
| 630 |
assert(alt.length>0 && ref.length>0) : this; |
| 631 |
} |
| 632 |
return prefix; |
| 633 |
} |
| 634 |
|
| 635 |
private int trimSuffix(){ |
| 636 |
int suffix=0; |
| 637 |
for(int rpos=ref.length-1, apos=alt.length-1; rpos>0 && apos>0 && ref[rpos]==alt[apos]; rpos--, apos--){ |
| 638 |
suffix++; |
| 639 |
} |
| 640 |
if(suffix>0){ |
| 641 |
reflen=ref.length-suffix; |
| 642 |
int altlen=alt.length-suffix; |
| 643 |
ref=(reflen==1 ? Var.AL_MAP[ref[0]] : Arrays.copyOf(ref, reflen)); |
| 644 |
alt=(altlen==1 ? Var.AL_MAP[alt[0]] : Arrays.copyOf(alt, altlen)); |
| 645 |
assert(alt.length>0 && ref.length>0) : this; |
| 646 |
} |
| 647 |
return suffix; |
| 648 |
} |
| 649 |
|
| 650 |
private byte[] prefix(byte[] array, int len){ |
| 651 |
assert(len>0); |
| 652 |
assert(array.length>len); |
| 653 |
byte[] prefix=(len==1 ? Var.AL_MAP[array[0]] : len==array.length ? array : Arrays.copyOf(array, len)); |
| 654 |
return prefix; |
| 655 |
} |
| 656 |
|
| 657 |
private byte[] suffix(byte[] array, int len){ |
| 658 |
assert(len>0); |
| 659 |
assert(array.length>len); |
| 660 |
byte[] suffix=(len==1 ? Var.AL_MAP[array[array.length-1]] : len==array.length ? array : Arrays.copyOfRange(array, array.length-len, array.length)); |
| 661 |
return suffix; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
@Override |
| 669 |
public boolean equals(Object b){ |
| 670 |
return equals((VCFLine)b); |
| 671 |
} |
| 672 |
|
| 673 |
public boolean equals(VCFLine b){ |
| 674 |
return hashcode==b.hashcode && compareTo(b)==0; |
| 675 |
} |
| 676 |
|
| 677 |
private int hash(){ |
| 678 |
return scaf.hashCode()^Integer.rotateLeft(pos, 9)^Integer.rotateRight(pos+ref.length, 9)^Var.hash(alt); |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
@Override |
| 686 |
public int hashCode(){ |
| 687 |
return hashcode; |
| 688 |
} |
| 689 |
|
| 690 |
public long toKey() { |
| 691 |
long key=Long.rotateLeft(pos, 31)^Long.rotateRight(hashcode, 10)^scaf.hashCode(); |
| 692 |
return key&0x3FFFFFFFFFFFFFFFL; |
| 693 |
} |
| 694 |
|
| 695 |
@Override |
| 696 |
public int compareTo(VCFLine v){ |
| 697 |
ScafMap map=ScafMap.defaultScafMap(); |
| 698 |
assert(map!=null); |
| 699 |
int scafnum1=map.getScaffold(scaf).number; |
| 700 |
int scafnum2=map.getScaffold(v.scaf).number; |
| 701 |
if(scafnum1!=scafnum2){return scafnum1-scafnum2;} |
| 702 |
if(pos!=v.pos){return pos-v.pos;} |
| 703 |
final int typeA=type(), typeB=v.type(); |
| 704 |
if(typeA!=typeB){return typeA-typeB;} |
| 705 |
int stop1=pos+reflen(), stop2=v.pos+reflen(); |
| 706 |
if(stop1!=stop2){return stop1-stop2;} |
| 707 |
return compare(alt, v.alt); |
| 708 |
} |
| 709 |
|
| 710 |
public int compare(byte[] a, byte[] b){ |
| 711 |
if(a==b){return 0;} |
| 712 |
if(a.length!=b.length){return b.length-a.length;} |
| 713 |
for(int i=0; i<a.length; i++){ |
| 714 |
byte ca=a[i], cb=b[i]; |
| 715 |
if(ca!=cb){return ca-cb;} |
| 716 |
} |
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
@Override |
| 721 |
public String toString(){ |
| 722 |
ByteBuilder bb=new ByteBuilder(); |
| 723 |
return toText(bb).toString(); |
| 724 |
} |
| 725 |
|
| 726 |
public ByteBuilder toText(ByteBuilder bb){ |
| 727 |
bb.append(scaf).append('\t'); |
| 728 |
bb.append(pos).append('\t'); |
| 729 |
bb.append(id).append('\t'); |
| 730 |
bb.append(ref).append('\t'); |
| 731 |
bb.append(alt).append('\t'); |
| 732 |
bb.append(qual, 2).append('\t'); |
| 733 |
bb.append(filter).append('\t'); |
| 734 |
bb.append(info).append('\t'); |
| 735 |
if(format!=null){ |
| 736 |
bb.append(format); |
| 737 |
} |
| 738 |
for(byte[] sample : samples){ |
| 739 |
bb.tab().append(sample); |
| 740 |
} |
| 741 |
return bb; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public int reflen(){return ref.length;} |
| 749 |
public int readlen(){return alt.length;} |
| 750 |
|
| 751 |
public int type_old(){ |
| 752 |
if(alt!=null && Tools.indexOf(alt, ',')>=0){return Var.MULTI;} |
| 753 |
final int reflen=reflen(), readlen=readlen(); |
| 754 |
if(readlen!=reflen && readlen!=0 && reflen!=0){return Var.COMPLEX;} |
| 755 |
if(reflen<readlen){return Var.INS;} |
| 756 |
if(reflen>readlen){return Var.DEL;} |
| 757 |
for(byte b : alt){ |
| 758 |
if(b!='N'){return Var.SUB;} |
| 759 |
} |
| 760 |
return Var.NOCALL; |
| 761 |
} |
| 762 |
|
| 763 |
public int type(){return type;} |
| 764 |
|
| 765 |
public boolean isRef(){ |
| 766 |
return Tools.equals(alt, ref); |
| 767 |
} |
| 768 |
|
| 769 |
public boolean isJunction(){ |
| 770 |
return type==Var.LJUNCT || type==Var.RJUNCT || type==Var.BJUNCT; |
| 771 |
} |
| 772 |
|
| 773 |
public boolean isIndel(){ |
| 774 |
return type==Var.INS || type==Var.DEL; |
| 775 |
} |
| 776 |
|
| 777 |
public boolean isSub(){ |
| 778 |
return type==Var.SUB; |
| 779 |
} |
| 780 |
|
| 781 |
public boolean isDel(){ |
| 782 |
return type==Var.DEL; |
| 783 |
} |
| 784 |
|
| 785 |
public boolean isIns(){ |
| 786 |
return type==Var.INS; |
| 787 |
} |
| 788 |
|
| 789 |
public boolean isNocall(){ |
| 790 |
return type==Var.NOCALL; |
| 791 |
} |
| 792 |
|
| 793 |
public boolean isMulti(){ |
| 794 |
return type==Var.MULTI; |
| 795 |
} |
| 796 |
|
| 797 |
public boolean isComplex(){ |
| 798 |
return type==Var.COMPLEX; |
| 799 |
} |
| 800 |
|
| 801 |
public boolean isSimple(){ |
| 802 |
|
| 803 |
return type==Var.SUB || type==Var.DEL || type==Var.INS || type==Var.NOCALL; |
| 804 |
} |
| 805 |
|
| 806 |
void cache(){ |
| 807 |
|
| 808 |
id=cache(id); |
| 809 |
if(ref.length<5){ref=cache(ref);} |
| 810 |
if(alt.length<5){alt=cache(alt);} |
| 811 |
filter=cache(filter); |
| 812 |
ref=cache(ref); |
| 813 |
format=cache(format); |
| 814 |
} |
| 815 |
|
| 816 |
static byte[] cache(byte[] line){ |
| 817 |
if(line==null){return line;} |
| 818 |
String s=new String(line); |
| 819 |
byte[] old=cache.get(s); |
| 820 |
if(old!=null){return old;} |
| 821 |
if(cache.size()>20000){return line;} |
| 822 |
synchronized(cache){ |
| 823 |
old=cache.get(s); |
| 824 |
if(old!=null){return old;} |
| 825 |
cache.put(s, line); |
| 826 |
return line; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
static byte[] cache(String s){ |
| 831 |
if(s==null){return null;} |
| 832 |
byte[] old=cache.get(s); |
| 833 |
if(old!=null){return old;} |
| 834 |
synchronized(cache){ |
| 835 |
old=cache.get(s); |
| 836 |
if(old!=null){return old;} |
| 837 |
old=s.getBytes(); |
| 838 |
cache.put(s, old); |
| 839 |
return old; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
public int start(){ |
| 845 |
return pos-1; |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
public int stop(){ |
| 850 |
return Tools.max(start(), pos+reflen-2); |
| 851 |
} |
| 852 |
|
| 853 |
private void recalc(){ |
| 854 |
type=type_old(); |
| 855 |
hashcode=hash(); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
public final String scaf; |
| 863 |
public int pos; |
| 864 |
public byte[] id; |
| 865 |
public byte[] ref; |
| 866 |
public int reflen; |
| 867 |
public byte[] alt; |
| 868 |
public double qual; |
| 869 |
public byte[] filter; |
| 870 |
public byte[] info; |
| 871 |
public byte[] format; |
| 872 |
public int hashcode; |
| 873 |
public int type; |
| 874 |
public ArrayList<byte[]> samples=new ArrayList<byte[]>(); |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
public static HashMap<String, byte[]> cache=new HashMap<String, byte[]>(99997); |
| 881 |
|
| 882 |
static boolean AUTOCACHE=false; |
| 883 |
static boolean TRIM_TO_CANONICAL=true; |
| 884 |
static boolean SORT=true; |
| 885 |
static boolean CONDENSE=true; |
| 886 |
static boolean SPLIT_INFO=true; |
| 887 |
|
| 888 |
private static final byte[] NOCALL=cache("NOCALL"); |
| 889 |
private static final byte[] SUB=cache("SUB"); |
| 890 |
private static final byte[] DEL=cache("DEL"); |
| 891 |
private static final byte[] INS=cache("INS"); |
| 892 |
private static final byte[] LJUNCT=cache("LJUNCT"); |
| 893 |
private static final byte[] RJUNCT=cache("RJUNCT"); |
| 894 |
private static final byte[] BJUNCT=cache("BJUNCT"); |
| 895 |
private static final byte[] MULTI=cache("MULTI"); |
| 896 |
private static final byte[] COMPLEX=cache("COMPLEX"); |
| 897 |
private static final byte[] DOT=cache("."); |
| 898 |
private static final byte[] PASS=cache("PASS"); |
| 899 |
private static final byte[] FAIL=cache("FAIL"); |
| 900 |
private static final byte[] FORMAT=cache("GT:DP:AD:AF:SC:PF"); |
| 901 |
|
| 902 |
static{ |
| 903 |
cache(Var.AL_0); |
| 904 |
cache(Var.AL_A); |
| 905 |
cache(Var.AL_C); |
| 906 |
cache(Var.AL_G); |
| 907 |
cache(Var.AL_T); |
| 908 |
cache(Var.AL_N); |
| 909 |
} |
| 910 |
|
| 911 |
} |