varona.fake_vcf

Module for fake VCF file creation.

The classes in this module provide quick VCF file creation for testing purposes. In one case, the file mimics a Platypus VCF file, which has an older VCF format (v4.0). In the other case, the file is a generic VCF file with a v4.2 format.

Note

As of pysam v0.22.1 (April 23, 2024), the VCF header doesn’t capture the fileformat line correctly sometimes, so when mimicking a Platypus VCF file, the file is rewritten with a Platypus-like header and the fileformat is set to v4.0. There is a regression test for pysam in case that behvaior changes in the future, in which case the rewriting won’t be needed.

class FakePlatypusVcfFile(path: Path, samples: list[str] = ['sample'], records: list[dict] = [])[source]

Bases: FakeVcfFile

Make a fake Platypus VCF file for testing.

Platypus VCFs have a different header format than generic VCFs.

add_records_from_lines(lines)[source]

Add records to the VCF file from a list of lines.

These lines are expected to be in the format of a Platypus VCF file and may be copy/pasted from a real file.

One note is that the this method writes a temporary file for the sake of parsing the records into memory. This is a bit silly, but it works.

make_header()[source]

Add a Platypus header to the VCF file.

write_vcf(rewrite=True)[source]

Write the VCF file with the header and records.

The Platypus VCF file has a slightly different header format than generic VCF files written by pysam. The VCFv4.0 format doesn’t have contig information mandatory.

In the Platypus VCF, pysam seems to ignore the fileformat line, and otherwise writes out in a different order as the test file.

Parameters:

rewrite – If True, rewrite the file to ensure the header is correct.

class FakeVcfFile(path: Path, samples: list[str] = ['sample'], records: list[dict] = [])[source]

Bases: object

Make a minimal fake VCF files for testing.

Unlike the pysam.AlignedSegment class, the pysam.VariantRecord class lacks from_dict and fromstring convenience methods. This class provides a way to create and add to minimal VCF files for testing.

add_records(records: list[dict])[source]

Add records to the VCF file.

make_header()[source]

Add a basic header to the VCF file.

write_vcf()[source]

Write the VCF file with the header and records.

class TestWithTempDir(methodName='runTest')[source]

Bases: TestCase

Boilerplate for tests that need a temporary directory.

setUp()[source]

Hook method for setting up the test fixture before exercising it.

tearDown()[source]

Hook method for deconstructing the test fixture after testing it.