Input/Output

vcfpy.Reader

class vcfpy.Reader(stream: TextIOWrapper, path: Path | str | None = None, tabix_path: Path | str | None = None, record_checks: Iterable[Literal['FORMAT', 'INFO']] | None = None, parsed_samples: list[str] | None = 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 parsed_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: str, begin: int | None = None, end: int | None = 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(path: Path | str, tabix_path: Path | str | None = None, record_checks: list[Literal['INFO', 'FORMAT']] | None = None, parsed_samples: list[str] | None = None)[source]

Create new Reader from path

Note

If you use the parsed_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(stream: TextIOWrapper, path: Path | str | None = None, tabix_path: Path | str | None = None, record_checks: list[Literal['INFO', 'FORMAT']] | None = None, parsed_samples: list[str] | None = None)[source]

Create new Reader from file

Note

If you use the parsed_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’

  • parsed_samples (list) – list of str values with names of samples to parse call information for (for speedup); leave to None for ignoring

header

the Header

parsed_samples

if set, list of samples to parse for

parser

the parser to use

path

optional str with the path to the stream

record_checks

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

stream

stream (file-like object) to read from

tabix_file

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

tabix_path

optional str with path to tabix file

vcfpy.Writer

class vcfpy.Writer(stream: IO[str], header: Header, path: Path | str | None = 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(path: Path | str, header: 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(stream: IO[str] | IO[bytes], header: Header, path: Path | str | None = None, use_bgzf: bool | None = 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

the :py:class:~vcfpy.header.Header` to write out, will be deep-copied into the Writer on initialization

path

optional str with the path to the stream

stream

stream (file-like object) to read from

write_record(record: Record)[source]

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