Skip to main content

snputils logo

snputils: A Python Library for Processing Genetic Variation and Population Structure

License BSD-3 PyPI Python Version Test, Docs & Publish

snputils is a Python package designed to ease the processing and analysis of genomic datasets, while handling all the complexities of different genome formats and operations very efficiently. The library provides robust tools for handling sequencing and ancestry data, with a focus on performance, ease of use, and advanced visualization capabilities.

Developed in collaboration between Stanford University's Department of Biomedical Data Science, UC Santa Cruz Genomics Institute, and more collaborators worldwide.

snputils is stable and ready for production workflows. The core API is documented, tested, and suitable for day-to-day genomic analysis. The project is actively maintained: we ship regular releases, welcome contributions, and continue to extend format support, analyses, and performance.

Why snputils?

  • One API across genotype, local ancestry, global ancestry, phenotype, and IBD data
  • Fast readers and writers for common population-genetics formats
  • In-memory Python workflows and file-backed CLI workflows in the same package
  • Ancestry-aware analyses including PCA and advanced alternatives, admixture mapping, and ancestry-specific allele frequencies
  • Built-in plotting for embeddings, local ancestry, admixture, and association results

Quickstart

import snputils as su

snp = su.read_snp("cohort.vcf.gz")                    # VCF, BCF, BGEN, BED, PGEN
snp = snp.filter_biallelic_variants()
snp.save("cohort.pgen")                               # convert to PGEN

lai = su.read_lai("local_ancestry.msp")               # MSP, FLARE, or .lanc local ancestry
adm = su.read_admixture("admixture_prefix")           # ADMIXTURE-style global ancestry
pheno = su.read_pheno("phenotypes.tsv", col="trait")
ibd = su.read_ibd("segments.hapibd")

pcs = su.PCA(n_components=2).fit_transform(snp)
afr_af = snp.allele_freq(ancestry="AFR", laiobj=lai)
gwas = su.run_gwas(pheno, snp)
admix = su.run_admixture_mapping(pheno, lai)

su.viz.scatter(pcs, "labels.tsv", save_path="pca.png", show=False)
su.viz.chromosome_painting(lai, "chr_paintings/")
su.viz.qq_plot(gwas)
su.viz.manhattan_plot(admix)

Installation

Basic installation using pip:

pip install snputils

Optionally, for PyTorch-backed features, install with the [torch] extra:

pip install 'snputils[torch]'

Optional extras:

  • pip install 'snputils[tests]' for the test stack
  • pip install 'snputils[docs]' for local documentation builds
  • pip install 'snputils[demos]' for notebook demos

Key Features

File Format Support

snputils provides high-level dispatchers like read_snp, read_lai, read_admixture, read_pheno, and read_ibd, plus explicit reader and writer classes when you need finer control.

  • VCF: Support for .vcf and .vcf.gz files
  • BCF: Read support for .bcf files
  • BGEN: Support for .bgen files
  • PLINK1: Support for .bed, .bim, .fam filesets
  • PLINK2: Support for .pgen, .pvar, .psam filesets
  • GRG: Read and write graph-based genome representation files
  • Local Ancestry: Handle .msp, FLARE .anc.vcf.gz, and admix-kit .lanc local ancestry formats
  • Global Ancestry / ADMIXTURE: Read and write .Q and .P files
  • IBD: Read hap-IBD and ancIBD outputs into a unified object

Data Objects and Utilities

  • SNPObject for genotype data, including filtering, saving, allele-frequency helpers, and genotype QC
  • LocalAncestryObject and GlobalAncestryObject for ancestry-aware workflows
  • PhenotypeObject, MultiPhenotypeObject, and CovariateObject for trait data
  • IBDObject for segment filtering and ancestry-restricted trimming
  • Synthetic dataset builders for SNP, mdPCA, maasMDS, chromosome-painting, admixture, and GRG examples
  • Conversion helpers such as VCF-to-GRG workflows

Processing & Analysis Tools

  • Basic manipulation

    • Filter variants and samples, correct SNP flips, and filter ambiguous SNPs
    • Compute cohort and ancestry-specific allele frequencies via SNPObject.allele_freq(...)
    • Stream allele frequencies with snputils.stats.allele_freq_stream(...) for memory efficiency
    • Run common genotype QC from SNPObject: duplicate sample/variant checks, call-rate filters, MAF/MAC filters, HWE, LD pruning, heterozygosity/inbreeding reports, imputation quality filters, relatedness pruning, and differential missingness by phenotype, cohort, or batch
  • Dimensionality reduction

    • Standard PCA with optional PyTorch acceleration
    • Missing-data PCA (mdPCA)
    • Multi-array ancestry-specific MDS (maasMDS)
  • Population-genetic statistics

    • Compute $D$, $f_2$, $f_3$, $f_4$, the $f_4$-ratio, and $F_{ST}$ (Hudson, Weir-Cockerham, and Tsallis $F_{q}$)
    • Block jackknife standard errors where applicable
    • Optional ancestry masking in relevant workflows
  • Association analysis

    • GWAS on SNP dosages for binary and quantitative traits
    • Admixture mapping from local ancestry dosage
    • Built-in Manhattan and Q–Q plotting utilities
  • IBD and ancestry-aware trimming

    • Unified IBD ingestion from common upstream tools
    • Segment filtering and ancestry-restricted trimming using local ancestry
  • Simulation

    • Lightweight haplotype-based simulation of admixed mosaics from founder haplotypes

Visualization

  • Scatter plots for PCA, mdPCA, and maasMDS embeddings
  • Global ancestry bar plots
  • Local ancestry visualization
    • Chromosome painting
    • Dataset-level cohort summaries
  • Association plots
    • Manhattan plots
    • Q–Q plots

Performance

  • Fast file I/O through built-in methods or optimized wrappers (e.g., Pgenlib for PLINK files)
  • Memory-efficient operations using NumPy and Polars, including streaming workflows
  • Optional GPU acceleration via PyTorch for computationally intensive tasks
  • Support for large-scale genomic datasets through efficient memory management

Our benchmark demonstrates superior performance compared to existing tools:

Reading time and peak-memory comparison for chromosome 22 data across different tools. See the benchmark directory for detailed methodology and results.

Command-Line Interface

Installing the package provides a snputils command for common file-backed workflows:

snputils --help
snputils --version

Available subcommands include:

  • pca: run standard PCA and save coordinates/components and a scatter plot.
  • mdpca: run missing-data PCA and save an embedding table.
  • maasmds: run ancestry-specific MDS and save an embedding table.
  • admixture-map: run admixture mapping from phenotype and local ancestry files.
  • gwas: run variant-level association testing from phenotype and genotype files.
  • simulate: simulate admixed haplotype batches from phased founder haplotypes.
  • plot-manhattan and plot-qq: render association result visualizations.

The Python API remains the full surface for low-level readers/writers, object manipulation, IBD filtering and trimming, f-statistics, allele-frequency helpers, custom visualizations, and notebook-oriented workflows. Use the CLI when a workflow naturally starts from files and produces files; use Python when you need programmatic composition or in-memory objects.

Documentation and Examples

  • Documentation: docs.snputils.org
  • Quickstart: Quickstart guide
  • Tutorials: PCA, mdPCA, maasMDS, SNP objects, allele frequency, local ancestry visualization, admixture mapping, and GRG workflows
  • API Reference: Readers, writers, data objects, processing classes, statistics, datasets, and visualization helpers
  • Issues and feature requests: GitHub Issues

Public API Highlights

Top-level imports include:

  • Readers and objects: read_snp, read_lai, read_admixture, read_ibd, read_pheno, SNPObject, LocalAncestryObject, GlobalAncestryObject, IBDObject
  • Analysis: PCA, mdPCA, maasMDS, run_gwas, run_admixture_mapping, allele_freq_stream
  • Datasets: load_dataset, available_datasets_list, build_synthetic_*
  • Visualization namespace: snputils.viz

Citation

If you use snputils in your research, please cite our paper:

@article{snputils2026,
    author    = {Bonet, David and Comajoan Cara, Marçal and Barrabés, Míriam and Smeriglio, Riccardo and Agrawal, Devang and Aounallah, Khaled and Geleta, Margarita and Dominguez Mantes, Albert and Thomassin, Christophe and Shanks, Cole and Huang, Edward C. and Franquesa Monés, Marc and Luis, Aina and Saurina, Joan and Perera, Maria and López, Cayetana and Sabat, Benet Oriol and Abante, Jordi and Moreno-Grau, Sonia and Mas Montserrat, Daniel and Ioannidis, Alexander G.},
    title     = {{snputils}: A High-Performance {Python} Library for Genetic Variation and Population Structure},
    year      = {2026},
    doi       = {10.64898/2026.02.28.708618},
    url       = {https://www.biorxiv.org/content/10.64898/2026.02.28.708618},
    journal   = {bioRxiv},
    publisher = {Cold Spring Harbor Laboratory},
}

Acknowledgments

We would like to thank the open-source packages that make snputils possible.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snputils-1.0.7.tar.gz (4.4 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

snputils-1.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (689.3 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

snputils-1.0.7-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (689.2 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

snputils-1.0.7-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (689.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

snputils-1.0.7-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (686.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

snputils-1.0.7-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (683.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

snputils-1.0.7-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (683.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file snputils-1.0.7.tar.gz.

File metadata

  • Download URL: snputils-1.0.7.tar.gz
  • Upload date:
  • Size: 4.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for snputils-1.0.7.tar.gz
Algorithm Hash digest
SHA256 28c447a65b0d7bbb6a45b423b9b5d6beb4ee9573f88ff592321cd0cb51e31cf5
MD5 8b8f5380733d8ad8b6705f1e188e49f0
BLAKE2b-256 f8b613559452f0d76f84b1a5f3cd95367c56217dabcb1eac839707a862d16de5

See more details on using hashes here.

File details

Details for the file snputils-1.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for snputils-1.0.7-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ec4da7ee957d97236751c08bc1d97779f22ee9b23b7717c134b97ba18892f733
MD5 2ace7940fbd309ee6277d66d56906ffc
BLAKE2b-256 97bf6339131076ef996776052e8c7b3eb420aad570900f18b68df5ce263bab85

See more details on using hashes here.

File details

Details for the file snputils-1.0.7-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for snputils-1.0.7-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2656cf0586a41227a6c6ecd09779eab6bf92c994ed20f85d99086bbeedd2471f
MD5 8ecb210b124d4114c35f49b74598eb93
BLAKE2b-256 2146b63ad00caee35de84e66252b973eba86d9c82b9165ff266c6e5ef3cfa4a2

See more details on using hashes here.

File details

Details for the file snputils-1.0.7-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for snputils-1.0.7-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ca3937df17d6e8b6e4bf04bf4e5479b0b6095a4ef4f7e032bc74d92c3d139ea4
MD5 ca268f8aceb009879fdc7af306cbee0a
BLAKE2b-256 0b2b87875a61432ee9f2d035f9a6717f800f34e44526ca6383dd6f7cd5a52420

See more details on using hashes here.

File details

Details for the file snputils-1.0.7-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for snputils-1.0.7-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a55485e879676a9a354c48a5d1c9860ed860974cff11c3893997f554def2c757
MD5 efb2e5756cbd796d699ac98c665c1e1e
BLAKE2b-256 f782decd7c26eb87c5a66cf55b962bdf8222912e704f9828dce3fe651d43105e

See more details on using hashes here.

File details

Details for the file snputils-1.0.7-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for snputils-1.0.7-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 406e95cd5ff16abc47b25a7e4d6361026d3cebefa46b64238b432c8fc8c4f4f0
MD5 2136a7677998264b8c37b1e831433a99
BLAKE2b-256 f039422fd8afc5e1538c46d87fc86de85e665cffcdc897f0ca2de56a5cd9a382

See more details on using hashes here.

File details

Details for the file snputils-1.0.7-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for snputils-1.0.7-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e994a711fbe1f19215c66d8a081e7400f2cadd6be831a44f8306e1d891a66305
MD5 e02478468cb77738d4d066a9d522c511
BLAKE2b-256 d0f43b8508360aa0b34a234ffc27926bb156b7083bd1176d18233017b80b249c

See more details on using hashes here.

Release history Release notifications | RSS feed

1.1.0

7 files

This release

1.0.7 This release

7 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.1

2 files

1.0.0

2 files

0.2.40

2 files

0.2.39

2 files

0.2.38

2 files

0.2.37

2 files

0.2.36

2 files

0.2.35

2 files

0.2.34

2 files

0.2.32

2 files

0.2.31

2 files

0.2.30

2 files

0.2.29

2 files

0.2.28

2 files

0.2.27

2 files

0.2.26

2 files

0.2.25

2 files

0.2.24

2 files

0.2.23

2 files

0.2.22

2 files

0.2.21

2 files

0.2.20

2 files

0.2.19

2 files

0.2.18

2 files

0.2.17

2 files

0.2.16

2 files

0.2.15

2 files

0.2.14

2 files

0.2.13

2 files

0.2.12

2 files

0.2.11

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

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