If you happen to see a question you know the answer to, please do chime in and help your fellow community members. We encourage our fourm members to be more involved, jump in and help out your fellow researchers with their questions. GATK forum is a community forum and helping each other with using GATK tools and research is the cornerstone of our success as a genomics research community.We appreciate your help!

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


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

Exception in thread "main" java.lang.NullPointerException
        at htsjdk.tribble.index.tabix.TabixIndexCreator.advanceToReference(
        at htsjdk.tribble.index.tabix.TabixIndexCreator.addFeature(
        at htsjdk.variant.variantcontext.writer.IndexingVariantContextWriter.add(
        at htsjdk.variant.variantcontext.writer.VCFWriter.add(
        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)

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
    val variantContextIter = reader.iterator().asScala

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) =>
    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.


Best, Oliver



  • 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 or

Sign In or Register to comment.