class vcf.Reader(fsock=None, filename=None, compressed=None, prepend_chr=False, strict_whitespace=False, encoding='ascii')[source]

Reader for a VCF v 4.0 file, an iterator returning _Record objects

alts = None

ALT fields from header

contigs = None

contig fields from header

fetch(chrom, start=None, end=None)[source]

Fetches records from a tabix-indexed VCF file and returns an iterable of _Record instances

chrom must be specified.

The start and end coordinates are in the zero-based, half-open coordinate system, similar to _Record.start and _Record.end. The very first base of a chromosome is index 0, and the the region includes bases up to, but not including the base at the end coordinate. For example fetch('4', 10, 20) would include all variants overlapping a 10 base pair region from the 11th base of through the 20th base (which is at index 19) of chromosome 4. It would not include the 21st base (at index 20). See http://genomewiki.ucsc.edu/index.php/Coordinate_Transforms for more information on the zero-based, half-open coordinate system.

If end is omitted, all variants from start until the end of the chromosome chrom will be included.

If start and end are omitted, all variants on chrom will be returned.

requires pysam

filters = None

FILTER fields from header

formats = None

FORMAT fields from header

infos = None

INFO fields from header

metadata = None

metadata fields from header (string or hash, depending)


Return the next record in the file.


class vcf.Writer(stream, template, lineterminator='n')[source]

VCF Writer. On Windows Python 2, open stream with ‘wb’.


Close the writer


Flush the writer


write a record to the file


class vcf.model._Record(CHROM, POS, ID, REF, ALT, QUAL, FILTER, INFO, FORMAT, sample_indexes, samples=None)[source]

A set of calls at a site. Equivalent to a row in a VCF file.

The standard VCF fields CHROM, POS, ID, REF, ALT, QUAL, FILTER, INFO and FORMAT are available as properties.

The list of genotype calls is in the samples property.

Regarding the coordinates associated with each instance:

  • POS, per VCF specification, is the one-based index (the first base of the contig has an index of 1) of the first base of the REF sequence.
  • The start and end denote the coordinates of the entire REF sequence in the zero-based, half-open coordinate system (see http://genomewiki.ucsc.edu/index.php/Coordinate_Transforms), where the first base of the contig has an index of 0, and the interval runs up to, but does not include, the base at the end index. This indexing scheme is analagous to Python slice notation.
  • The affected_start and affected_end coordinates are also in the zero-based, half-open coordinate system. These coordinates indicate the precise region of the reference genome actually affected by the events denoted in ALT (i.e., the minimum affected_start and maximum affected_end).
    • For SNPs and structural variants, the affected region includes all bases of REF, including the first base (i.e., affected_start = start = POS - 1).
    • For deletions, the region includes all bases of REF except the first base, which flanks upstream the actual deletion event, per VCF specification.
    • For insertions, the affected_start and affected_end coordinates represent a 0 bp-length region between the two flanking bases (i.e., affected_start = affected_end). This is analagous to Python slice notation (see http://stackoverflow.com/a/2947881/38140). Neither the upstream nor downstream flanking bases are included in the region.
POS = None

the one-based coordinate of the first nucleotide in REF


A list of allele frequencies of alternate alleles. NOTE: Denominator calc’ed from _called_ genotypes.

affected_end = None

zero-based, half-open end coordinate of affected region of reference genome (not included in the region)

affected_start = None

zero-based, half-open start coordinate of affected region of reference genome

alleles = None

list of alleles. [0] = REF, [1:] = ALTS


The fraction of genotypes that were actually called.

end = None

zero-based, half-open end coordinate of REF


Lookup a _Call for the sample given in name


The list of het genotypes


The list of hom alt genotypes


The list of hom ref genotypes


The list of unknown genotypes


Heterozygosity of a site. Heterozygosity gives the probability that two randomly chosen chromosomes from the population have different alleles, giving a measure of the degree of polymorphism in a population.

If there are i alleles with frequency p_i, H=1-sum_i(p_i^2)


Return whether or not the INDEL is a deletion


Return True if a variant has been filtered


Return whether or not the variant is an INDEL


Return True for reference calls


Return whether or not the variant is a SNP


Return whether or not the variant is a structural variant


Return whether the SV cordinates are mapped to 1 b.p. resolution.


Return whether or not the SNP is a transition


pi_hat (estimation of nucleotide diversity) for the site. This metric can be summed across multiple sites to compute regional nucleotide diversity estimates. For example, pi_hat for all variants in a given gene.

Derived from: “Population Genetics: A Concise Guide, 2nd ed., p.45” John Gillespie.


The number of called samples


The number of heterozygous genotypes


The number of homozygous for alt allele genotypes


The number of homozygous for ref allele genotypes


The number of unknown genotypes

samples = None

list of _Calls for each sample ordered as in source VCF

start = None

zero-based, half-open start coordinate of REF


Return the end position for the SV


Return the subtype of variant.

  • For SNPs and INDELs, yeild one of: [ts, tv, ins, del]

  • For SVs yield either “complex” or the SV type defined in the ALT fields (removing the brackets). E.g.:

    <DEL>       -> DEL
    <INS:ME:L1> -> INS:ME:L1
    <DUP>       -> DUP

The logic is meant to follow the rules outlined in the following paragraph at:


“For precisely known variants, the REF and ALT fields should contain the full sequences for the alleles, following the usual VCF conventions. For imprecise variants, the REF field may contain a single base and the ALT fields should contain symbolic alleles (e.g. <ID>), described in more detail below. Imprecise variants should also be marked by the presence of an IMPRECISE flag in the INFO field.”


Return the type of variant [snp, indel, unknown] TO DO: support SVs


class vcf.model._Call(site, sample, data)[source]

A genotype call, a cell entry in a VCF file


Namedtuple of data from the VCF file


The actual genotype alleles. E.g. if VCF genotype is 0/1, return A/G


The type of genotype. hom_ref = 0 het = 1 hom_alt = 2 (we don;t track _which+ ALT) uncalled = None


Return True for filtered calls


Return True for heterozygous calls


Return True if not a reference call


A boolean indicating whether or not the genotype is phased for this sample


The sample name


The _Record for this _Call


class vcf.model._AltRecord(type, **kwargs)[source]

An alternative allele record: either replacement string, SV placeholder, or breakend

type = None

String to describe the type of variant, by default “SNV” or “MNV”, but can be extended to any of the types described in the ALT lines of the header (e.g. “DUP”, “DEL”, “INS”...)


class vcf.model._Substitution(nucleotides, **kwargs)[source]

A basic ALT record, where a REF sequence is replaced by an ALT sequence

sequence = None

Alternate sequence


class vcf.model._SV(type, **kwargs)[source]

An SV placeholder


class vcf.model._SingleBreakend(orientation, connectingSequence, **kwargs)[source]

A single breakend


class vcf.parser._Breakend(chr, pos, orientation, remoteOrientation, connectingSequence, withinMainAssembly, **kwargs)[source]

A breakend which is paired to a remote location on or off the genome

connectingSequence = None

The breakpoint’s connecting sequence.

orientation = None

The orientation of breakend. If the sequence 3’ of the breakend is connected, True, else if the sequence 5’ of the breakend is connected, False.

remoteOrientation = None

The orientation of breakend’s mate. If the sequence 3’ of the breakend’s mate is connected, True, else if the sequence 5’ of the breakend’s mate is connected, False.

withinMainAssembly = None

If the breakend mate is within the assembly, True, else False if the breakend mate is on a contig in an ancillary assembly file.