Input/Output

vcfpy.Reader

class vcfpy.Reader(stream, path=None, tabix_path=None, record_checks=None)[source]

Class for parsing of files from file-like objects

Instead of using the constructor, use the class methods from_stream() and from_path().

On construction, the header will be read from the file which can cause problems. After construction, Reader can be used as an iterable of Record.

Raises:InvalidHeaderException in the case of problems reading the header
close()[source]

Close underlying stream

fetch(chrom, begin, end)[source]

Jump to the start position of the given chromosomal position and limit iteration to the end position

Parameters:
  • chrom (str) – name of the chromosome to jump to
  • begin (int) – 0-based begin position (inclusive)
  • end (int) – 0-based end position (exclusive)
classmethod from_path(klass, path, tabix_path=None, record_checks=None)[source]

Create new Reader from path

Parameters:
  • path – the path to load from (converted to str for compatibility with path.py)
  • tabix_path – optional string with path to TBI index, automatic inferral from path will be tried on the fly if not given
  • record_checks (list) – record checks to perform, can contain ‘INFO’ and ‘FORMAT’
classmethod from_stream(klass, stream, path=None, tabix_path=None, record_checks=None)[source]

Create new Reader from file

Parameters:
  • streamfile-like object to read from
  • path – optional string with path to store (for display only)
  • record_checks (list) – record checks to perform, can contain ‘INFO’ and ‘FORMAT’
header = None

the Header

parser = None

the parser to use

path = None

optional str with the path to the stream

record_checks = None

checks to perform on records, can contain ‘FORMAT’ and ‘INFO’

samples = None

the vcfpy.header.SamplesInfos object with the sample name information

stream = None

stream (file-like object) to read from

tabix_file = None

the pysam.TabixFile used for reading from index bgzip-ed VCF; constructed on the fly

tabix_path = None

optional str with path to tabix file

vcfpy.Writer

class vcfpy.Writer(stream, header, samples, path=None)[source]

Class for writing VCF files to file-like objects

Instead of using the constructor, use the class methods from_stream() and from_path().

The writer has to be constructed with a Header object and the full VCF header will be written immediately on construction. This, of course, implies that modifying the header after construction is illegal.

close()[source]

Close underlying stream

classmethod from_path(klass, path, header)[source]

Create new Writer from path

Parameters:
  • path – the path to load from (converted to str for compatibility with path.py)
  • header – VCF header to use
  • samples – SamplesInfos to use
classmethod from_stream(klass, stream, header, path=None, use_bgzf=None)[source]

Create new Writer from file

Note that for getting bgzf support, you have to pass in a stream opened in binary mode. Further, you either have to provide a path ending in ".gz" or set use_bgzf=True. Otherwise, you will get the notorious “TypeError: ‘str’ does not support the buffer interface”.

Parameters:
  • streamfile-like object to write to
  • header – VCF header to use
  • path – optional string with path to store (for display only)
  • use_bgzf – indicator whether to write bgzf to stream if True, prevent if False, interpret path if None
header = None

the :py:class:~vcfpy.header.Header` written out

path = None

optional str with the path to the stream

stream = None

stream (file-like object) to read from

write_record(record)[source]

Write out the given vcfpy.record.Record to this Writer