Input/Output

vcfpy.Reader

class vcfpy.Reader(stream, path=None, tabix_path=None, record_checks=None, parse_samples=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

Note

It is important to note that the header member is used during the parsing of the file. If you need a modified version then create a copy, e.g., using :py:method:`~vcfpy.header.Header.copy`.

Note

If you use the parse_samples feature and you write out records then you must not change the FORMAT of the record.

close()[source]

Close underlying stream

fetch(chrom_or_region, begin=None, end=None)[source]

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

Parameters:
  • chrom_or_region (str) – name of the chromosome to jump to if begin and end are given and a samtools region string otherwise (e.g. “chr1:123,456-123,900”).
  • 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, parse_samples=None)[source]

Create new Reader from path

Note

If you use the parse_samples feature and you write out records then you must not change the FORMAT of the record.

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, parse_samples=None)[source]

Create new Reader from file

Note

If you use the parse_samples feature and you write out records then you must not change the FORMAT of the record.

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’
  • parse_samples (list) – list of str values with names of samples to parse call information for (for speedup); leave to None for ignoring
header = None

the Header

parse_samples = None

if set, list of samples to parse for

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’

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, 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, lines and samples are deep-copied
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, lines and samples are deep-copied
  • 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` to write out, will be deep-copied into the Writer on initialization

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