Skip to main content

Pure Tabix

Build Status PyPI version

This is a pure-python Tabix index parser. Useful as an alternative to PySAM and PyTabix for rapid read access by position to Tabix indexed block gzipped files such as VCFs and other common bioinfomatics formats.

See https://samtools.github.io/hts-specs/tabix.pdf and https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3042176 for information about Tabix and the detailed file format specification.

from puretabix import TabixIndexedFile

tabix_indexed_file = TabixIndexedFile.from_files(open('somefile.vcf.gz', 'rb'), open('somefile.vcf.gz.tbi', 'rb'))
tabix_indexed_file.fetch("1", 1000, 5000)

Documentation is supported via Python built-in module PyDoc: python3 -m pydoc -b puretabix

VCF

Included in this package is tooling for reading and writing VCF lines.

To read a file:

from puretabix.vcf import read_vcf_lines

with open("source.vcf") as input:
    for vcfline in read_vcf_lines(input):
        if vcfline.is_comment:
            # its a comment or meta-information
            pass
        else:
            # access the parsed information
            if "PASS" not in vcfline._filter:
                print(f"{vcfline.chrom} {vcfline.pos} {vcfline.get_genotype()}")

To write some lines:

from puretabix.vcf import VCFLine

with open("output.vcf") as output:
    output.write(str(VCFLine.as_comment_key_dict("fileformat", "VCFv4.2")))
    output.write("\n")
    output.write(
        str(
            VCFLine.as_comment_raw(
                "\t".join(
                    (
                        "CHROM",
                        "POS",
                        "ID",
                        "REF",
                        "ALT",
                        "QUAL",
                        "FILTER",
                        "INFO",
                        "FORMAT",
                        "SAMPLE",
                    )
                )
            )
        )
    )
    output.write("\n")
    output.write(
        str(
            VCFLine.as_data(
                "chr1",
                123,
                ("rs123",),
                "A",
                ("C",),
                ".",
                ("PASS",),
                {},
                ({"GT": "1/0"},),
            )
        )
    )
    output.write("\n")

VCF with index

If there is a tabix index for a block gzipped VCF file, that index can be used for fast random access

import puretabix

with open("input.vcf.gz", "rb") as vcf:
    with open("input.vcf.gz.tbi", "rb") as vcf_tbi:
        indexed = puretabix.TabixIndexedVCFFile.from_files(vcf, vcf_tbi)
        vcfline = tuple(indexed.fetch_vcf_lines("chr1", 1108138))
        assert vcfline.chrom == "chr1"
        assert vcfline.pos == 1108138
        print(f"gt = {vcfline.get_genotype()}")

development

TL;DR: pip install -e '.[dev]' && pre-commit install

pip install -e '.[dev]'  # Install using pip including development extras
pre-commit install  # Enable pre-commit hooks
pre-commit run --all-files  # Run pre-commit hooks without committing
# Note pre-commit is configured to use:
# - seed-isort-config to better categorise third party imports
# - isort to sort imports
# - black to format code
pip-compile  # Freeze dependencies
pytest  # Run tests
coverage run --source=puretabix -m pytest && coverage report -m  # Run tests, print coverage
mypy .  # Type checking
pipdeptree  # Print dependencies
scalene --outfile tests/perf_test.txt --profile-all --cpu-sampling-rate 0.0001 tests/perf_test.py  # performance measurements

Global git ignores per https://help.github.com/en/github/using-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer

For release to PyPI see https://packaging.python.org/tutorials/packaging-projects/

For information about packaging wheels see https://realpython.com/python-wheels/

git checkout master
git pull
git add setup.py CHANGES.txt
git commit -m"prepare for x.x.x"
git push
git tag x.x.x
git push origin x.x.x
python3 setup.py sdist bdist_wheel && python3 -m twine upload dist/*

acknowledgements

Inspired by @yangmqglobe code in https://github.com/cggh/scikit-allel/pull/297

Metadata

Release files for puretabix 5.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for puretabix 5.4.0
File Size Uploaded Provenance Actions
puretabix-5.4.0.tar.gz 22.0 kB Without provenance Details for puretabix-5.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for puretabix 5.4.0
File Interpreter ABI Platform Actions
puretabix-5.4.0-py3-none-any.whl Python 3 none any Details for puretabix-5.4.0-py3-none-any.whl

Total release size: 43.8 kB

Release files / puretabix-5.4.0.tar.gz

Download URL puretabix-5.4.0.tar.gz
Size 22.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9c6d307ccb97fb55498e4e82f532afb2635e576454b6479da0c74af12681749d
BLAKE2b-256 checksum
How to use checksums
21b4946c81ccf63fbac991184d146ad6f4c3715987770f9c6443fc4384552180
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.18

Release files / puretabix-5.4.0-py3-none-any.whl

Download URL puretabix-5.4.0-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ef9067e8dd0f8d91a9f4eff6fd482347dd664f01a51ebf45c4b184a283f5882
BLAKE2b-256 checksum
How to use checksums
5960593949da3a0f288873b8d5c83142158033a2194ec8744884424aebcc1797
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.18

Security for puretabix 5.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page