pysam
Package for reading, manipulating, and writing genomic data
What it is and what it does
pysam is a Python wrapper around HTSlib, the C library underlying samtools and bcftools, that lets you read and write genomic sequence data in standard formats (SAM, BAM, CRAM, VCF, BCF, BED, GFF, GTF, FASTA, FASTQ). It provides both low-level access to HTSlib's C API via Cython and a high-level Python interface for working with genomic data, including support for compression and random access through indexing.
The package is widely used in bioinformatics pipelines and genomic analysis workflows. It has no runtime dependencies beyond HTSlib itself (bundled in the wheel), making it straightforward to integrate into data processing scripts. Active maintenance and broad platform support (macOS, Linux) with prebuilt wheels for Python 3.10–3.13 mean installation is typically frictionless on supported systems.
Use it for:
- Parse and filter SAM/BAM files in a genomic analysis pipeline without calling samtools as a subprocess
- Extract specific genomic regions or read alignments from indexed VCF/BCF files for variant analysis
- Convert between genomic file formats (e.g., BAM to CRAM) programmatically within a Python workflow
- Access samtools and bcftools functionality directly from Python without shell subprocess overhead
- Build custom bioinformatics tools that need to read FASTA/FASTQ sequences and alignment data
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
pysam reads, manipulates, and writes genomic data files (SAM/BAM/CRAM/VCF/BCF/BED/GFF/GTF/FASTA/FASTQ) and provides access to samtools and bcftools command-line functionality through a Python wrapper around HTSlib.
Yes. pysam is actively maintained, has no runtime dependencies, carries a permissive MIT license, and is essential for any Python-based genomic data processing. Medium install friction is offset by prebuilt wheels for modern Python versions and the absence of external runtime requirements. No known vulnerabilities.
Install
pysam on PyPI
pip
pip install pysamuv
uv add pysampoetry
poetry add pysamInstalling pysam
Before you install
Medium install friction due to compiled C extensions via Cython wrapping HTSlib; prebuilt wheels available for common Python versions (3.10–3.13) and platforms (macOS, Linux, musl). Active maintenance with last commit 2026-08-10.
License in practice
MIT License (permissive) allows use in proprietary and open-source projects with minimal restrictions; attribution required.
Quickstart
import pysam
# Open and read a BAM file
bamfile = pysam.AlignmentFile('example.bam', 'rb')
for read in bamfile:
print(read.query_name, read.query_sequence)
bamfile.close()
Requires HTSlib C library and development headers; pysam is a compiled extension and cannot be used without a compatible binary wheel or build environment.
Verify before relying
- Whether the package works on Windows (classifiers list MacOS/POSIX/Unix but no Windows wheel evidence)
- Performance characteristics for large genomic datasets
- Exact HTSlib version bundled or required
Package facts
| License | MIT License (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 109 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,291,977/month — #4,103 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pysam-0.24.0-cp310-cp310-macosx_10_9_x86_64.whl; pysam-0.24.0-cp310-cp310-macosx_11_0_arm64.whl; pysam-0.24.0-cp310-cp310-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl; pysam-0.24.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl; pysam-0.24.0-cp310-cp310-musllinux_1_2_aarch64.whl; pysam-0.24.0-cp310-cp310-musllinux_1_2_x86_64.whl; pysam-0.24.0-cp311-cp311-macosx_10_9_x86_64.whl; pysam-0.24.0-cp311-cp311-macosx_11_0_arm64.whl; pysam-0.24.0-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl; pysam-0.24.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl; pysam-0.24.0-cp311-cp311-musllinux_1_2_aarch64.whl; pysam-0.24.0-cp311-cp311-musllinux_1_2_x86_64.whl; pysam-0.24.0-cp312-cp312-macosx_10_13_x86_64.whl; pysam-0.24.0-cp312-cp312-macosx_11_0_arm64.whl; pysam-0.24.0-cp312-cp312-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl; pysam-0.24.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl; pysam-0.24.0-cp312-cp312-musllinux_1_2_aarch64.whl; pysam-0.24.0-cp312-cp312-musllinux_1_2_x86_64.whl; pysam-0.24.0-cp313-cp313-macosx_10_13_x86_64.whl; pysam-0.24.0-cp313-cp313-macosx_11_0_arm64.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
cyvcf2cyvcf2 is a Cython wrapper around htslib that…
permissive · top 15,000 on PyPI
dnaiodnaio reads and writes FASTQ, FASTA, and uBAM…
permissive · top 15,000 on PyPI
NREL-PySAMPySAM wraps NREL's System Advisor Model (SAM)…
permissive · top 15,000 on PyPI
pyfaidxProvides fast random access to subsequences in…
permissive · top 15,000 on PyPI
pyensemblPyEnsembl provides a Python interface to query…
permissive · top 15,000 on PyPI
pybedtoolsWraps the BEDTools suite to perform genomic…
permissive · top 15,000 on PyPI
bioA command-line toolkit that simplifies common…
permissive · top 5,000 on PyPI
pyteomicsPyteomics provides Python tools for proteomics…
permissive · top 15,000 on PyPI
pysylphPysylph provides Python bindings to sylph,…
permissive · top 15,000 on PyPI
pybigtoolspybigtools is a Python wrapper around a Rust…
permissive · top 15,000 on PyPI