| 1 |
package var2; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import fileIO.ByteFile; |
| 7 |
import fileIO.ByteFile1; |
| 8 |
import fileIO.ByteFile2; |
| 9 |
import fileIO.ByteStreamWriter; |
| 10 |
import fileIO.FileFormat; |
| 11 |
import fileIO.ReadWrite; |
| 12 |
import shared.Parse; |
| 13 |
import shared.Parser; |
| 14 |
import shared.PreParser; |
| 15 |
import shared.Shared; |
| 16 |
import shared.Timer; |
| 17 |
import shared.Tools; |
| 18 |
import stream.ConcurrentGenericReadInputStream; |
| 19 |
import stream.FastaReadInputStream; |
| 20 |
import structures.ByteBuilder; |
| 21 |
import structures.ListNum; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class FilterVCF { |
| 29 |
|
| 30 |
public static void main(String[] args){ |
| 31 |
Timer t=new Timer(); |
| 32 |
FilterVCF x=new FilterVCF(args); |
| 33 |
x.process(t); |
| 34 |
|
| 35 |
|
| 36 |
Shared.closeStream(x.outstream); |
| 37 |
} |
| 38 |
|
| 39 |
public FilterVCF(String[] args){ |
| 40 |
|
| 41 |
{ |
| 42 |
PreParser pp=new PreParser(args, getClass(), false); |
| 43 |
args=pp.args; |
| 44 |
outstream=pp.outstream; |
| 45 |
} |
| 46 |
|
| 47 |
ReadWrite.USE_PIGZ=ReadWrite.USE_UNPIGZ=true; |
| 48 |
ReadWrite.setZipThreads(Shared.threads()); |
| 49 |
|
| 50 |
varFilter.clear(); |
| 51 |
|
| 52 |
boolean setSamFilter=false; |
| 53 |
boolean setVarFilter=false; |
| 54 |
|
| 55 |
Parser parser=new Parser(); |
| 56 |
for(int i=0; i<args.length; i++){ |
| 57 |
String arg=args[i]; |
| 58 |
String[] split=arg.split("="); |
| 59 |
String a=split[0].toLowerCase(); |
| 60 |
String b=split.length>1 ? split[1] : null; |
| 61 |
|
| 62 |
if(a.equals("lines")){ |
| 63 |
maxLines=Long.parseLong(b); |
| 64 |
if(maxLines<0){maxLines=Long.MAX_VALUE;} |
| 65 |
}else if(a.equals("verbose")){ |
| 66 |
verbose=Parse.parseBoolean(b); |
| 67 |
ByteFile1.verbose=verbose; |
| 68 |
ByteFile2.verbose=verbose; |
| 69 |
stream.FastaReadInputStream.verbose=verbose; |
| 70 |
ConcurrentGenericReadInputStream.verbose=verbose; |
| 71 |
stream.FastqReadInputStream.verbose=verbose; |
| 72 |
ReadWrite.verbose=verbose; |
| 73 |
}else if(a.equals("multithreaded") || a.equals("mt")){ |
| 74 |
multithreaded=Parse.parseBoolean(b); |
| 75 |
}else if(a.equals("singlethreaded") || a.equals("st")){ |
| 76 |
multithreaded=!Parse.parseBoolean(b); |
| 77 |
}else if(a.equals("ref")){ |
| 78 |
ref=b; |
| 79 |
}else if(a.equals("ploidy")){ |
| 80 |
ploidy=Integer.parseInt(b); |
| 81 |
}else if(a.equals("sub") || a.equals("subs")){ |
| 82 |
Var.CALL_SUB=Parse.parseBoolean(b); |
| 83 |
}else if(a.equals("del") || a.equals("dels")){ |
| 84 |
Var.CALL_DEL=Parse.parseBoolean(b); |
| 85 |
}else if(a.equals("ins") || a.equals("inss")){ |
| 86 |
Var.CALL_INS=Parse.parseBoolean(b); |
| 87 |
}else if(a.equals("indel") || a.equals("indels")){ |
| 88 |
Var.CALL_INS=Var.CALL_DEL=Parse.parseBoolean(b); |
| 89 |
}else if(a.equals("junction") || a.equals("junctions")){ |
| 90 |
Var.CALL_JUNCTION=Parse.parseBoolean(b); |
| 91 |
}else if(a.equals("minscore")){ |
| 92 |
minScore=Double.parseDouble(b); |
| 93 |
}else if(a.equals("splitalleles")) { |
| 94 |
splitAlleles=Parse.parseBoolean(b); |
| 95 |
}else if(a.equals("splitsubs") || a.equals("splitsnps")) { |
| 96 |
splitSubs=Parse.parseBoolean(b); |
| 97 |
}else if(a.equals("splitcomplex")) { |
| 98 |
splitComplex=Parse.parseBoolean(b); |
| 99 |
}else if(a.equals("sass") || a.equals("split")) { |
| 100 |
splitAlleles=splitSubs=Parse.parseBoolean(b); |
| 101 |
}else if(a.equals("splitall") || a.equals("sascsss")) { |
| 102 |
splitAlleles=splitComplex=splitSubs=Parse.parseBoolean(b); |
| 103 |
}else if(a.equals("clearfilters")){ |
| 104 |
if(Parse.parseBoolean(b)){ |
| 105 |
varFilter.clear(); |
| 106 |
samFilter.clear(); |
| 107 |
} |
| 108 |
}else if(samFilter.parse(arg, a, b)){ |
| 109 |
setSamFilter=true; |
| 110 |
}else if(varFilter.parse(a, b, arg)){ |
| 111 |
setVarFilter=true; |
| 112 |
}else if(a.equals("scorehist") || a.equals("qualhist") || a.equals("qhist") || a.equals("shist")){ |
| 113 |
scoreHistFile=b; |
| 114 |
}else if(a.equals("trimtocanonical") || a.equals("canonicalize") || a.equals("canonicize") || a.equals("canonize")){ |
| 115 |
VCFLine.TRIM_TO_CANONICAL=Parse.parseBoolean(b); |
| 116 |
} |
| 117 |
|
| 118 |
else if(a.equalsIgnoreCase("countNearbyVars")){ |
| 119 |
countNearby=Parse.parseBoolean(b); |
| 120 |
} |
| 121 |
|
| 122 |
else if(parser.parse(arg, a, b)){ |
| 123 |
|
| 124 |
}else{ |
| 125 |
outstream.println("Unknown parameter "+args[i]); |
| 126 |
assert(false) : "Unknown parameter "+args[i]; |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
{ |
| 132 |
in1=parser.in1; |
| 133 |
out1=parser.out1; |
| 134 |
overwrite=parser.overwrite; |
| 135 |
append=parser.append; |
| 136 |
} |
| 137 |
|
| 138 |
if(!setSamFilter){samFilter=null;} |
| 139 |
if(!setVarFilter){varFilter=null;} |
| 140 |
|
| 141 |
assert(FastaReadInputStream.settingsOK()); |
| 142 |
|
| 143 |
if(in1==null){throw new RuntimeException("Error - at least two input files are required.");} |
| 144 |
|
| 145 |
if(!ByteFile.FORCE_MODE_BF2){ |
| 146 |
ByteFile.FORCE_MODE_BF2=false; |
| 147 |
ByteFile.FORCE_MODE_BF1=true; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if(!Tools.testOutputFiles(overwrite, append, false, out1)){ |
| 154 |
outstream.println((out1==null)+", "+out1); |
| 155 |
throw new RuntimeException("\n\noverwrite="+overwrite+"; Can't write to output file "+out1+"\n"); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
if(!Tools.testInputFiles(false, true, in1, ref)){ |
| 160 |
throw new RuntimeException("\nCan't read some input files.\n"); |
| 161 |
} |
| 162 |
|
| 163 |
ffin1=FileFormat.testInput(in1, FileFormat.TXT, null, true, true); |
| 164 |
|
| 165 |
ffout1=FileFormat.testOutput(out1, FileFormat.TXT, null, true, overwrite, append, multithreaded); |
| 166 |
|
| 167 |
if(ref!=null){ScafMap.loadReference(ref, scafMap, samFilter, true);} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
threads=Tools.min(8, Shared.threads()); |
| 172 |
} |
| 173 |
|
| 174 |
public void loadHeaderInScafMap(){ |
| 175 |
for(byte[] line : header){ |
| 176 |
if(Tools.startsWith(line, "##contig=<ID=")){ |
| 177 |
scafMap.addFromVcf(line); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public String headerToString(){ |
| 183 |
StringBuilder sb=new StringBuilder(); |
| 184 |
for(byte[] line : header){ |
| 185 |
for(byte b : line){ |
| 186 |
sb.append((char)b); |
| 187 |
} |
| 188 |
sb.append('\n'); |
| 189 |
} |
| 190 |
return sb.toString(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
private ArrayList<ProcessThread> spawnThreads(ByteFile bf, ByteStreamWriter bsw){ |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
ArrayList<ProcessThread> alpt=new ArrayList<ProcessThread>(threads); |
| 200 |
for(int i=0; i<threads; i++){ |
| 201 |
alpt.add(new ProcessThread(bf, bsw, jobIDOffset)); |
| 202 |
} |
| 203 |
if(verbose){outstream.println("Spawned threads.");} |
| 204 |
|
| 205 |
|
| 206 |
for(ProcessThread pt : alpt){ |
| 207 |
pt.start(); |
| 208 |
} |
| 209 |
if(verbose){outstream.println("Started threads.");} |
| 210 |
|
| 211 |
|
| 212 |
return alpt; |
| 213 |
} |
| 214 |
|
| 215 |
private void waitForFinish(ArrayList<ProcessThread> alpt){ |
| 216 |
|
| 217 |
boolean allSuccess=true; |
| 218 |
for(ProcessThread pt : alpt){ |
| 219 |
while(pt.getState()!=Thread.State.TERMINATED){ |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
try { |
| 226 |
|
| 227 |
pt.join(); |
| 228 |
} catch (InterruptedException e) { |
| 229 |
|
| 230 |
e.printStackTrace(); |
| 231 |
} |
| 232 |
} |
| 233 |
linesProcessed+=pt.linesProcessedT; |
| 234 |
headerLinesProcessed+=pt.headerLinesProcessedT; |
| 235 |
variantLinesProcessed+=pt.variantLinesProcessedT; |
| 236 |
variantLinesOut+=pt.variantLinesOutT; |
| 237 |
bytesProcessed+=pt.bytesProcessedT; |
| 238 |
Tools.add(scoreHist, pt.scoreHistT); |
| 239 |
|
| 240 |
allSuccess&=pt.success; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if(!allSuccess){errorState=true;} |
| 245 |
} |
| 246 |
|
| 247 |
private void processVcfHeader(ByteFile bf, ByteStreamWriter bsw){ |
| 248 |
byte[] line=bf.nextLine(); |
| 249 |
|
| 250 |
if(ScafMap.defaultScafMap()==null){ |
| 251 |
ScafMap.setDefaultScafMap(new ScafMap(), bf.name()); |
| 252 |
} |
| 253 |
ByteBuilder bb=new ByteBuilder(); |
| 254 |
while(line!=null && (line.length==0 || line[0]=='#')){ |
| 255 |
if(line.length>0){ |
| 256 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 257 |
linesProcessed++; |
| 258 |
headerLinesProcessed++; |
| 259 |
bytesProcessed+=line.length; |
| 260 |
bb.append(line).append('\n'); |
| 261 |
headerLinesOut++; |
| 262 |
header.add(line); |
| 263 |
if(Tools.startsWith(line, VCFFile.CHROM_POS)){ |
| 264 |
String[] split=new String(line).split("\t"); |
| 265 |
for(int i=9; i<split.length; i++){ |
| 266 |
samples.add(split[i]); |
| 267 |
} |
| 268 |
}else if(Tools.startsWith(line, "##contig=<ID=")){ |
| 269 |
ScafMap.defaultScafMap().addFromVcf(line); |
| 270 |
}else{ |
| 271 |
String[] split=new String(line).split("="); |
| 272 |
if(split.length==2){ |
| 273 |
String a=split[0], b=split[1]; |
| 274 |
if(a.equalsIgnoreCase("##ploidy")){ |
| 275 |
ploidy=Integer.parseInt(b); |
| 276 |
}else if(a.equalsIgnoreCase("##properPairRate")){ |
| 277 |
properPairRate=(float) Double.parseDouble(b); |
| 278 |
}else if(a.equalsIgnoreCase("##totalQualityAvg")){ |
| 279 |
totalQualityAvg=(float) Double.parseDouble(b); |
| 280 |
}else if(a.equalsIgnoreCase("##mapqAvg")){ |
| 281 |
totalMapqAvg=(float) Double.parseDouble(b); |
| 282 |
}else if(a.equalsIgnoreCase("##readLengthAvg")){ |
| 283 |
readLengthAvg=(float) Double.parseDouble(b); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
line=bf.nextLine(); |
| 289 |
} |
| 290 |
if(line!=null && line.length>0){bf.pushBack(line);} |
| 291 |
if(bb.length()>0 && bsw!=null){ |
| 292 |
bsw.add(bb, jobIDOffset); |
| 293 |
jobIDOffset++; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
private void processVcfVarsST(ByteFile bf, ByteStreamWriter bsw){ |
| 298 |
boolean varFormatOK=true; |
| 299 |
byte[] line=bf.nextLine(); |
| 300 |
while(line!=null){ |
| 301 |
if(line.length>0){ |
| 302 |
if(maxLines>0 && linesProcessed>=maxLines){break;} |
| 303 |
linesProcessed++; |
| 304 |
bytesProcessed+=line.length; |
| 305 |
|
| 306 |
final boolean isHeader=(line[0]=='#'); |
| 307 |
|
| 308 |
if(isHeader){ |
| 309 |
assert(false) : "Encountered intermediate header."; |
| 310 |
headerLinesProcessed++; |
| 311 |
if(bsw!=null){bsw.println(line);} |
| 312 |
header.add(line); |
| 313 |
if(Tools.startsWith(line, VCFFile.CHROM_POS)){ |
| 314 |
String[] split=new String(line).split("\t"); |
| 315 |
for(int i=9; i<split.length; i++){ |
| 316 |
samples.add(split[i]); |
| 317 |
} |
| 318 |
} |
| 319 |
}else{ |
| 320 |
variantLinesProcessed++; |
| 321 |
VCFLine vline=new VCFLine(line); |
| 322 |
boolean pass=true; |
| 323 |
|
| 324 |
if(!Var.CALL_DEL && vline.type()==Var.DEL){pass=false;} |
| 325 |
else if(!Var.CALL_INS && vline.type()==Var.INS){pass=false;} |
| 326 |
else if(!Var.CALL_SUB && vline.type()==Var.SUB){pass=false;} |
| 327 |
else if(!Var.CALL_JUNCTION && vline.isJunction()){pass=false;} |
| 328 |
|
| 329 |
if(pass && samFilter!=null){pass&=samFilter.passesFilter(vline);} |
| 330 |
if(pass && varFilter!=null){ |
| 331 |
Var v=null; |
| 332 |
|
| 333 |
if(varFormatOK){ |
| 334 |
try { |
| 335 |
v=vline.toVar(); |
| 336 |
} catch (Throwable e) { |
| 337 |
System.err.println("WARNING: This VCF file does not support Var format.\n" |
| 338 |
+ "Filtering can only be done on location and quality score.\n"); |
| 339 |
varFormatOK=false; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
if(v!=null){ |
| 344 |
pass&=varFilter.passesFilter(v, properPairRate, totalQualityAvg, totalMapqAvg, |
| 345 |
readLengthAvg, ploidy, scafMap, countNearby); |
| 346 |
}else{ |
| 347 |
pass&=vline.qual>=varFilter.minScore; |
| 348 |
} |
| 349 |
} |
| 350 |
if(pass){ |
| 351 |
|
| 352 |
ArrayList<VCFLine> split=(splitAlleles || splitComplex || splitSubs) ? vline.split(splitAlleles, splitComplex, splitSubs) : null; |
| 353 |
|
| 354 |
if(split==null){ |
| 355 |
if(bsw!=null){bsw.println(line);} |
| 356 |
variantLinesOut++; |
| 357 |
int q=(int)(vline.qual); |
| 358 |
scoreHist[Tools.min(scoreHist.length-1, q)]++; |
| 359 |
}else{ |
| 360 |
for(VCFLine vline2 : split){ |
| 361 |
if(bsw!=null){bsw.print(vline2.toText(new ByteBuilder(64)).nl());} |
| 362 |
variantLinesOut++; |
| 363 |
int q=(int)(vline2.qual); |
| 364 |
scoreHist[Tools.min(scoreHist.length-1, q)]++; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
} |
| 391 |
} |
| 392 |
} |
| 393 |
line=bf.nextLine(); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
private void processVcfVarsMT (ByteFile bf, ByteStreamWriter bsw){ |
| 398 |
ArrayList<ProcessThread> alpt=spawnThreads(bf, bsw); |
| 399 |
|
| 400 |
waitForFinish(alpt); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public void filter(FileFormat ff, ByteStreamWriter bsw){ |
| 413 |
|
| 414 |
ByteFile bf=ByteFile.makeByteFile(ff); |
| 415 |
|
| 416 |
processVcfHeader(bf, bsw); |
| 417 |
|
| 418 |
loadHeaderInScafMap(); |
| 419 |
assert(ScafMap.defaultScafMap().size()>0) : ScafMap.defaultScafMap()+"\n"+headerToString(); |
| 420 |
|
| 421 |
if(multithreaded){ |
| 422 |
processVcfVarsMT(bf, bsw); |
| 423 |
}else{ |
| 424 |
processVcfVarsST(bf, bsw); |
| 425 |
} |
| 426 |
|
| 427 |
if(scoreHistFile!=null){ |
| 428 |
CallVariants.writeScoreHist(scoreHistFile, scoreHist); |
| 429 |
} |
| 430 |
|
| 431 |
errorState|=bf.close(); |
| 432 |
if(bsw!=null){errorState|=bsw.poisonAndWait();} |
| 433 |
} |
| 434 |
|
| 435 |
void process(Timer t){ |
| 436 |
|
| 437 |
ByteStreamWriter bsw; |
| 438 |
if(ffout1!=null){ |
| 439 |
bsw=new ByteStreamWriter(ffout1); |
| 440 |
bsw.start(); |
| 441 |
}else{bsw=null;} |
| 442 |
|
| 443 |
filter(ffin1, bsw); |
| 444 |
|
| 445 |
t.stop(); |
| 446 |
outstream.println(Tools.timeLinesBytesProcessed(t, linesProcessed, bytesProcessed, 8)); |
| 447 |
outstream.println(); |
| 448 |
outstream.println("Header Lines In: \t"+headerLinesProcessed); |
| 449 |
outstream.println("Variant Lines In: \t"+variantLinesProcessed); |
| 450 |
outstream.println("Header Lines Out: \t"+headerLinesOut); |
| 451 |
outstream.println("Variant Lines Out: \t"+variantLinesOut); |
| 452 |
|
| 453 |
if(errorState){ |
| 454 |
throw new RuntimeException(getClass().getName()+" terminated in an error state; the output may be corrupt."); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
private class ProcessThread extends Thread { |
| 486 |
|
| 487 |
ProcessThread(ByteFile bf_, ByteStreamWriter bsw_, long jobIDOffset_){ |
| 488 |
bf=bf_; |
| 489 |
bsw=bsw_; |
| 490 |
offset=jobIDOffset_; |
| 491 |
} |
| 492 |
|
| 493 |
@Override |
| 494 |
public void run(){ |
| 495 |
ListNum<byte[]> ln=bf.nextList(); |
| 496 |
|
| 497 |
while(ln!=null && ln!=POISON_BYTES){ |
| 498 |
ByteBuilder bb=new ByteBuilder(4096); |
| 499 |
for(byte[] line : ln){ |
| 500 |
linesProcessedT++; |
| 501 |
processLine(line, bb); |
| 502 |
} |
| 503 |
if(bsw!=null){bsw.add(bb, ln.id+offset);} |
| 504 |
ln=bf.nextList(); |
| 505 |
|
| 506 |
} |
| 507 |
|
| 508 |
success=true; |
| 509 |
synchronized(this){notify();} |
| 510 |
} |
| 511 |
|
| 512 |
void processLine(byte[] line, ByteBuilder bb){ |
| 513 |
linesProcessedT++; |
| 514 |
bytesProcessedT+=line.length; |
| 515 |
if(line.length<1){return;} |
| 516 |
final boolean isHeader=(line[0]=='#'); |
| 517 |
if(isHeader){ |
| 518 |
assert(false) : "Encountered intermediate header."; |
| 519 |
headerLinesProcessedT++; |
| 520 |
bb.append(line).append('\n'); |
| 521 |
synchronized(header){ |
| 522 |
header.add(line); |
| 523 |
} |
| 524 |
if(Tools.startsWith(line, VCFFile.CHROM_POS)){ |
| 525 |
String[] split=new String(line).split("\t"); |
| 526 |
synchronized(samples){ |
| 527 |
for(int i=9; i<split.length; i++){ |
| 528 |
samples.add(split[i]); |
| 529 |
} |
| 530 |
} |
| 531 |
} |
| 532 |
}else{ |
| 533 |
variantLinesProcessedT++; |
| 534 |
boolean pass=true; |
| 535 |
|
| 536 |
VCFLine vline=new VCFLine(line); |
| 537 |
pass&=vline.qual>=minScore; |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
{ |
| 574 |
if(pass){ |
| 575 |
if(!Var.CALL_DEL && vline.type()==Var.DEL){pass=false;} |
| 576 |
else if(!Var.CALL_INS && vline.type()==Var.INS){pass=false;} |
| 577 |
else if(!Var.CALL_SUB && vline.type()==Var.SUB){pass=false;} |
| 578 |
else if(!Var.CALL_JUNCTION && vline.isJunction()){pass=false;} |
| 579 |
} |
| 580 |
|
| 581 |
if(pass && samFilter!=null){pass&=samFilter.passesFilter(vline);} |
| 582 |
|
| 583 |
if(pass && varFilter!=null){ |
| 584 |
if(varFormatOK){ |
| 585 |
try { |
| 586 |
|
| 587 |
final Var v; |
| 588 |
if(threads>1){ |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
v=Var.fromVCF(line, scafMap, true, true); |
| 593 |
}else{ |
| 594 |
|
| 595 |
|
| 596 |
v=vline.toVar(); |
| 597 |
} |
| 598 |
|
| 599 |
pass&=varFilter.passesFilter(v, properPairRate, totalQualityAvg, totalMapqAvg, |
| 600 |
readLengthAvg, ploidy, scafMap, countNearby); |
| 601 |
} catch (Throwable e) { |
| 602 |
System.err.println("WARNING: This VCF file does not support Var format.\n" |
| 603 |
+ "Filtering can only be done on location and quality score.\n"+e); |
| 604 |
e.printStackTrace(); |
| 605 |
varFormatOK=false; |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
if(pass){ |
| 612 |
|
| 613 |
|
| 614 |
if(splitAlleles && vline.alt!=null && Tools.indexOf(vline.alt, ',')>0){ |
| 615 |
String alleles=new String(vline.alt); |
| 616 |
String[] split=alleles.split(","); |
| 617 |
for(String allele : split){ |
| 618 |
vline.alt=allele.getBytes(); |
| 619 |
vline.toText(bb).nl(); |
| 620 |
variantLinesOutT++; |
| 621 |
int q=(int)(vline.qual); |
| 622 |
scoreHistT[Tools.min(scoreHistT.length-1, q)]++; |
| 623 |
} |
| 624 |
}else{ |
| 625 |
bb.append(line).append('\n'); |
| 626 |
variantLinesOutT++; |
| 627 |
int q=(int)(vline.qual); |
| 628 |
scoreHistT[Tools.min(scoreHistT.length-1, q)]++; |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
final ByteFile bf; |
| 635 |
final ByteStreamWriter bsw; |
| 636 |
final long offset; |
| 637 |
boolean varFormatOK=true; |
| 638 |
|
| 639 |
long linesProcessedT=0; |
| 640 |
long headerLinesProcessedT=0; |
| 641 |
long variantLinesProcessedT=0; |
| 642 |
long variantLinesOutT=0; |
| 643 |
long bytesProcessedT=0; |
| 644 |
private long[] scoreHistT=new long[scoreHist.length]; |
| 645 |
|
| 646 |
boolean success=false; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
private long linesProcessed=0; |
| 652 |
private long headerLinesProcessed=0; |
| 653 |
private long variantLinesProcessed=0; |
| 654 |
private long headerLinesOut=0; |
| 655 |
private long variantLinesOut=0; |
| 656 |
private long bytesProcessed=0; |
| 657 |
private long[] scoreHist=new long[1000]; |
| 658 |
|
| 659 |
private long maxLines=Long.MAX_VALUE; |
| 660 |
|
| 661 |
public ArrayList<byte[]> header=new ArrayList<byte[]>(); |
| 662 |
public ArrayList<String> samples=new ArrayList<String>(); |
| 663 |
|
| 664 |
SamFilter samFilter=new SamFilter(); |
| 665 |
VarFilter varFilter=new VarFilter(); |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
double minScore=0; |
| 670 |
|
| 671 |
public int ploidy=1; |
| 672 |
public float properPairRate=0; |
| 673 |
public float totalQualityAvg=30; |
| 674 |
public float totalMapqAvg=30; |
| 675 |
public float readLengthAvg=150; |
| 676 |
|
| 677 |
final int threads; |
| 678 |
public boolean multithreaded=false; |
| 679 |
private long jobIDOffset=0; |
| 680 |
boolean splitAlleles=false; |
| 681 |
boolean splitSubs=false; |
| 682 |
boolean splitComplex=false; |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
boolean countNearby=false; |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
private String in1=null; |
| 693 |
private String out1=null; |
| 694 |
private String ref=null; |
| 695 |
private String scoreHistFile=null; |
| 696 |
|
| 697 |
private final FileFormat ffin1; |
| 698 |
private final FileFormat ffout1; |
| 699 |
|
| 700 |
public final ScafMap scafMap=new ScafMap(); |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
static final ListNum<byte[]> POISON_BYTES=new ListNum<byte[]>(null, -1); |
| 705 |
private PrintStream outstream=System.err; |
| 706 |
public static boolean verbose=false; |
| 707 |
public boolean errorState=false; |
| 708 |
private boolean overwrite=true; |
| 709 |
private boolean append=false; |
| 710 |
|
| 711 |
} |