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 objectsInstead of using the constructor, use the class methods
from_stream()andfrom_path().On construction, the header will be read from the file which can cause problems. After construction,
Readercan be used as an iterable ofRecord.- Raises:
InvalidHeaderExceptionin the case of problems reading the header
Note
It is important to note that the
headermember 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_samplesfeature and you write out records then you must not change theFORMATof the record.- 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
Readerfrom pathNote
If you use the
parsed_samplesfeature and you write out records then you must not change theFORMATof the record.- Parameters:
path – the path to load from (converted to
strfor compatibility withpath.py)tabix_path – optional string with path to TBI index, automatic inferral from
pathwill be tried on the fly if not givenrecord_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
Readerfrom fileNote
If you use the
parsed_samplesfeature and you write out records then you must not change theFORMATof the record.- Parameters:
stream –
file-like object to read frompath – optional string with path to store (for display only)
record_checks (list) – record checks to perform, can contain ‘INFO’ and ‘FORMAT’
parsed_samples (list) –
listofstrvalues with names of samples to parse call information for (for speedup); leave toNonefor ignoring
- header
the Header
- parsed_samples
if set, list of samples to parse for
- parser
the parser to use
- path
optional
strwith 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.TabixFileused for reading from index bgzip-ed VCF; constructed on the fly
- tabix_path
optional
strwith 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 objectsInstead of using the constructor, use the class methods
from_stream()andfrom_path().The writer has to be constructed with a
Headerobject and the full VCF header will be written immediately on construction. This, of course, implies that modifying the header after construction is illegal.- classmethod from_path(path: Path | str, header: Header)[source]
Create new
Writerfrom path- Parameters:
path – the path to load from (converted to
strfor compatibility withpath.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
Writerfrom fileNote that for getting bgzf support, you have to pass in a stream opened in binary mode. Further, you either have to provide a
pathending in".gz"or setuse_bgzf=True. Otherwise, you will get the notorious “TypeError: ‘str’ does not support the buffer interface”.- Parameters:
stream –
file-like object to write toheader – 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
streamifTrue, prevent ifFalse, interpretpathifNone
- header
the :py:class:~vcfpy.header.Header` to write out, will be deep-copied into the
Writeron initialization
- path
optional
strwith the path to the stream
- stream
stream (
file-like object) to read from
- write_record(record: Record)[source]
Write out the given
vcfpy.record.Recordto this Writer