This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Vcf Programming Language Api'S / Parsers Available?

Are there any programming language APIs available yet for working with VCF files? The only tool I know of is vcf tools which as far as I know only has a command line interface and no application interface.

What I am looking for is the vcf equivalent of Picards, Samtools or Bamtools. A library that you can use from your own code to parse trough VCF files.

Preferentially a object oriented library in Java, but anything in Python or Perl will also do.

vcf api programming

4 answers

see the latest post on my blog:

"Reading/Writing a VCF file with the GATK-API. "

http://plindenbaum.blogspot.fr/2012/11/readingwriting-vcf-file-with-gatk-api.html

Nice GATK ofcourse has a VCF reader and writer in their source. I will have a look at this.

I copied your code and it seems to work. But I get an exception when closing the vcf writer. Did you run into this? Exception in thread "main" java.lang.NullPointerException at org.broadinstitute.sting.gatk.refdata.tracks.IndexDictionaryUtils.setIndexSequenceDictionary(IndexDictionaryUtils.java:85) at org.broadinstitute.sting.utils.variantcontext.writer.IndexingVariantContextWriter.close(IndexingVariantContextWriter.java:100) at org.broadinstitute.sting.utils.variantcontext.writer.VCFWriter.close(VCFWriter.java:147)

I am using the latest GATK lite jar file as a library.

strange. I cannot help you with this only stacktrace.

Well the writer seems to write all the output before crashing on closing so I'll just look into it later.

C++: vcflib

The design was meant to be similar to bamtools, which I found very easy to work with. You open a VCF file and iterate through record, populating an object with the VCF record on each iteration. You can write out VCF records using the << operator. Documentation is sparse, because I continuously develop the library as a way of dealing with edge cases not handled by by other systems such as vcf-tools or the GATK. Variant.h should provide ample self-documentation. Function names are meant to be self-explanatory.

Example usage, counting the number of alternate alleles in a VCF file (taken from vcfcountalleles):

#include "Variant.h"

using namespace std;
using namespace vcf;

int main(int argc, char** argv) {

    VariantCallFile variantFile;
    string filename = argv[1];
    variantFile.open(filename);

    int uniqueAlleles = 0;

    Variant var(variantFile);
    while (variantFile.getNextVariant(var)) {
        uniqueAlleles += var.alleles.size();
    }

    cout << uniqueAlleles << endl;

    return 0;

}

There are a lot of existing functions in the library for doing everything from haplotype-based allele intersection to filtering to genotype annotation. The stock programs in the library are almost entirely designed to read and write VCF, so, for instance, whenever possible annotations are added to records rather than dumped in a separate format, allowing the streaming of the results of one into the next and so on.

I intend to document them and will post a news item here on BioStar and also seqanswers when I do.

Python: https://github.com/jamescasbon/PyVCF

Have a look at the Perl API vcf.pm) from the VCFtools.

"VCFtools contains a Perl API Vcf.pm) and a number of Perl scripts that can be used to perform common tasks with VCF files such as file validation, file merging, intersecting, complements, etc."

Log in to answer this question.