Commit 0425c5db authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Changed arg parsing

parent 7ff8ef5d
...@@ -5,6 +5,7 @@ import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder ...@@ -5,6 +5,7 @@ import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder
import htsjdk.variant.vcf.VCFFileReader import htsjdk.variant.vcf.VCFFileReader
import java.io.File import java.io.File
import nl.lumc.sasc.biopet.core.BiopetJavaCommandLineFunction import nl.lumc.sasc.biopet.core.BiopetJavaCommandLineFunction
import nl.lumc.sasc.biopet.core.ToolCommand
import nl.lumc.sasc.biopet.core.config.Configurable import nl.lumc.sasc.biopet.core.config.Configurable
import org.broadinstitute.gatk.utils.commandline.{ Output, Input } import org.broadinstitute.gatk.utils.commandline.{ Output, Input }
import scala.collection.JavaConversions._ import scala.collection.JavaConversions._
...@@ -45,44 +46,45 @@ class VcfFilter(val root: Configurable) extends BiopetJavaCommandLineFunction { ...@@ -45,44 +46,45 @@ class VcfFilter(val root: Configurable) extends BiopetJavaCommandLineFunction {
conditional(filterRefCalls, "-filterRefCalls") conditional(filterRefCalls, "-filterRefCalls")
} }
object VcfFilter { object VcfFilter extends ToolCommand {
var inputVcf: File = _ case class Args (inputVcf:File = null, outputVcf:File = null, minSampleDepth: Int = -1, minTotalDepth: Int = -1,
var outputVcf: File = _ minAlternateDepth: Int = -1, minSamplesPass: Int = 0, filterRefCalls: Boolean = false) extends AbstractArgs
var minSampleDepth = -1
var minTotalDepth = -1 class OptParser extends AbstractOptParser {
var minAlternateDepth = -1 opt[File]('I', "inputVcf") required() maxOccurs(1) valueName("<file>") action { (x, c) =>
var minSamplesPass = 0 c.copy(inputVcf = x) }
var filterRefCalls = false opt[File]('o', "outputVcf") required() maxOccurs(1) valueName("<file>") action { (x, c) =>
c.copy(outputVcf = x) } text("output file, default to stdout")
opt[Int]("minSampleDepth") unbounded() action { (x, c) =>
c.copy(minSampleDepth = x ) }
opt[Int]("minTotalDepth") unbounded() action { (x, c) =>
c.copy(minTotalDepth = x ) }
opt[Int]("minAlternateDepth") unbounded() action { (x, c) =>
c.copy(minAlternateDepth = x) }
opt[Int]("minSamplesPass") unbounded() action { (x, c) =>
c.copy(minSamplesPass = x) }
opt[Boolean]("filterRefCalls") unbounded() action { (x, c) =>
c.copy(filterRefCalls = x) }
}
/** /**
* @param args the command line arguments * @param args the command line arguments
*/ */
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
for (t <- 0 until args.size) { val argsParser = new OptParser
args(t) match { val commandArgs: Args = argsParser.parse(args, Args()) getOrElse sys.exit(1)
case "-I" => inputVcf = new File(args(t+1))
case "-o" => outputVcf = new File(args(t+1))
case "-minSampleDepth" => minSampleDepth = args(t+1).toInt
case "-minTotalDepth" => minTotalDepth = args(t+1).toInt
case "-minAlternateDepth" => minAlternateDepth = args(t+1).toInt
case "-minSamplesPass" => minSamplesPass = args(t+1).toInt
case "-filterRefCalls" => filterRefCalls = true
case _ =>
}
}
if (inputVcf == null) throw new IllegalStateException("No inputVcf, use -I")
if (outputVcf == null) throw new IllegalStateException("No outputVcf, use -o")
val reader = new VCFFileReader(inputVcf, false) val reader = new VCFFileReader(commandArgs.inputVcf, false)
val writer = new AsyncVariantContextWriter(new VariantContextWriterBuilder().setOutputFile(outputVcf).build) val writer = new AsyncVariantContextWriter(new VariantContextWriterBuilder().setOutputFile(commandArgs.outputVcf).build)
writer.writeHeader(reader.getFileHeader) writer.writeHeader(reader.getFileHeader)
for (record <- reader) { for (record <- reader) {
val genotypes = for (genotype <- record.getGenotypes) yield { val genotypes = for (genotype <- record.getGenotypes) yield {
genotype.getDP >= minSampleDepth && genotype.getDP >= commandArgs.minSampleDepth &&
List(genotype.getAD:_*).tail.count(_ >= minAlternateDepth) > 0 && List(genotype.getAD:_*).tail.count(_ >= commandArgs.minAlternateDepth) > 0 &&
!(filterRefCalls && genotype.isHomRef) !(commandArgs.filterRefCalls && genotype.isHomRef)
} }
if (record.getAttributeAsInt("DP", -1) >= minTotalDepth && genotypes.count(_ == true) >= minSamplesPass) if (record.getAttributeAsInt("DP", -1) >= commandArgs.minTotalDepth && genotypes.count(_ == true) >= commandArgs.minSamplesPass)
writer.add(record) writer.add(record)
} }
reader.close reader.close
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment