Test-drive the GATK tools and Best Practices pipelines on Terra


Check out this blog post to learn how you can get started with GATK and try out the pipelines in preconfigured workspaces (with a user-friendly interface!) without having to install anything.

NullPointerException while writing VCF

OliverROliverR Broad InstituteMember, Broadie

Hello,

I'm getting this error while writing a VCF file:

Exception in thread "main" java.lang.NullPointerException
        at htsjdk.tribble.index.tabix.TabixIndexCreator.advanceToReference(TabixIndexCreator.java:116)
        at htsjdk.tribble.index.tabix.TabixIndexCreator.addFeature(TabixIndexCreator.java:96)
        at htsjdk.variant.variantcontext.writer.IndexingVariantContextWriter.add(IndexingVariantContextWriter.java:203)
        at htsjdk.variant.variantcontext.writer.VCFWriter.add(VCFWriter.java:240)
        at chowser.execute.VcfUtils$.$anonfun$transformVcf$1(VcfUtils.scala:18)
        at chowser.execute.VcfUtils$.$anonfun$transformVcf$1$adapted(VcfUtils.scala:18)
        at scala.collection.Iterator.foreach(Iterator.scala:941)
        at scala.collection.Iterator.foreach$(Iterator.scala:941)
        at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
        at chowser.execute.VcfUtils$.transformVcf(VcfUtils.scala:18)
        at chowser.execute.VariantsCanonicalizeVcfExecuter$.execute(VariantsCanonicalizeVcfExecuter.scala:11)
        at chowser.execute.ChowserExecuter$.execute(ChowserExecuter.scala:24)
        at chowser.app.ChowserApp$.main(ChowserApp.scala:15)
        at chowser.app.ChowserApp.main(ChowserApp.scala)

My code is this:

package chowser.execute

import better.files.File
import htsjdk.variant.variantcontext.VariantContext
import htsjdk.variant.variantcontext.writer.VariantContextWriterBuilder
import htsjdk.variant.vcf.VCFFileReader
import scala.collection.JavaConverters.asScalaIteratorConverter

object VcfUtils {
  def transformVcf(inFile: File, outFile: File)
                  (transformation: Iterator[VariantContext] => Iterator[VariantContext]): Unit = {
    val reader = new VCFFileReader(inFile.path, false)
    val header = reader.getFileHeader
    val dict = header.getSequenceDictionary
    val writer = new VariantContextWriterBuilder().setOutputPath(outFile.path).setReferenceDictionary(dict).build
    writer.writeHeader(header)
    val variantContextIter = reader.iterator().asScala
    transformation(variantContextIter).foreach(writer.add)
    writer.close()
  }
}

which is invoked like this:

package chowser.execute

import chowser.cmd.VariantsCanonicalizeVcfCommand
import chowser.genomics.VariantGroupId
import htsjdk.variant.variantcontext.VariantContextBuilder

object VariantsCanonicalizeVcfExecuter extends ChowserExecuter[VariantsCanonicalizeVcfCommand] {

  def execute(command: VariantsCanonicalizeVcfCommand): Result = {
    import command.{inFile, outFile}
    VcfUtils.transformVcf(inFile, outFile) { variantContextIter =>
      variantContextIter.flatMap { context =>
        VariantGroupId.fromVariantContext(context) match {
          case Right(newId) =>
            Some(new VariantContextBuilder(context).id(newId.toString).make)
          case Left(message) =>
            println(message)
            None
        }
      }
    }
    Result(command, success = true)
  }

  case class Result(command: VariantsCanonicalizeVcfCommand, success: Boolean)
    extends ChowserExecuter.Result[VariantsCanonicalizeVcfCommand]

}

All I'm trying to do is read a VCF file and write a new VCF file that looks identical to the original file, except that the variant ids have been replaced by canonical ones.

Thanks!

Best, Oliver

Tagged:

Answers

  • bhanuGandhambhanuGandham Cambridge MAMember, Administrator, Broadie, Moderator admin
    edited May 24

    Hi @OliverR

    I am not very familiar with scala, but if I understand correctly you are seeing this error in the steps using the vcfutils tool from samtools? As it stands we do not support errors with samtools, we only support errors that arise from GATK tools.

    This question is better suited for www.biostars.org or www.seqanswers.com

Sign In or Register to comment.