Skip to main content

Fast Bayesian hierarchical clustering of bacterial populations

Project description

fastbaps

Fast Bayesian hierarchical clustering of bacterial populations from SNP alignments.

fastbaps is a Python+Rust port of the R fastbaps package. Given a whole-genome alignment of bacterial isolates in FASTA format, it infers population structure by running Bayesian hierarchical clustering (BHC) over single-nucleotide polymorphisms and returns a cluster label for each isolate. The Python layer handles orchestration and I/O; the numerically intensive kernels run in compiled Rust via the companion fastbaps-core crate (available separately on crates.io for Rust users).

Installation

pip install fastbaps

Prebuilt abi3 wheels are available for Linux and macOS (x86_64 and arm64). Windows is not supported because the pysam runtime dependency wraps htslib, a Unix-only C library. Python 3.9 or newer is required.

You do not need a Rust toolchain to install from PyPI; all wheels include the compiled extension.

Quickstart

from fastbaps import import_fasta_sparse_nt, fast_baps, best_baps_partition

# Load a whole-genome SNP alignment
sparse_data = import_fasta_sparse_nt("alignment.fa")          # prior="baps" (default)

# Run two-phase Bayesian hierarchical clustering
hc = fast_baps(sparse_data, n_cores=4)
# hc keys: "merge", "height", "order", "labels"

# Cut the dendrogram to find the best partition
clusters = best_baps_partition(sparse_data, hc)
# clusters: integer array, one 1-indexed label per isolate

for name, label in zip(hc["labels"], clusters):
    print(f"{name}\t{label}")

import_fasta_sparse_nt takes a required fasta_path and an optional prior argument ("baps" or "mean", default "baps"). fast_baps accepts k_init (initial partition count; auto-selected if omitted), n_cores (default 1), and quiet (default False).

Command line

fastbaps --input alignment.fa --output clusters.csv --threads 4

Full options:

fastbaps --input INPUT --output OUTPUT
         [--prior {baps,mean,optimise.symmetric,optimise.baps}]
         [--levels N] [--threads N] [--quiet]

The output CSV has columns Isolates and Clusters.

How it works

  • Phase 1 (coarse) — Ward's D2 hierarchical clustering divides isolates into k_init initial partitions, reducing the search space cheaply.
  • Phase 2 (BHC refinement) — the partitions are agglomerated greedily using a Dirichlet-multinomial marginal log-likelihood, building a full merge tree with heights and posterior merge ratios (rk), parallelized with Rayon.
  • Best partition — the tree is cut wherever the posterior favors two subtrees being separate (threshold ln(0.5)), yielding one cluster label per isolate.
  • The heavy kernels (py_bhier_parallel, py_calc_ddk, py_tree_llk, py_summarise_clusters) live in Rust; everything else is Python.

For a full technical walk-through see FOR-DEVELOPERS.md. The standalone Rust core is published as fastbaps-core on crates.io.

License

Licensed under the MIT License. See LICENSE-MIT.

Credits

This package ports the algorithm from the original R fastbaps package. Please cite the original work when using fastbaps in research.

Project details


Download files

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

Source Distribution

fastbaps-0.1.1.tar.gz (31.0 kB view details)

Uploaded Source

Built Distributions

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

fastbaps-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (426.4 kB view details)

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

fastbaps-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (414.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

fastbaps-0.1.1-cp39-abi3-macosx_11_0_arm64.whl (367.8 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

fastbaps-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl (372.8 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file fastbaps-0.1.1.tar.gz.

File metadata

  • Download URL: fastbaps-0.1.1.tar.gz
  • Upload date:
  • Size: 31.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastbaps-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2105633e4b41055ea2b91c3fc5d320245594ffbd630dc67e413fd3d92b6db8d5
MD5 56ee55a080ce6d86aa47d2f27ddf02b4
BLAKE2b-256 6391c79320c8b54522394d5302b5ab983441ce90faa6309dd9349d34ff5bb30e

See more details on using hashes here.

File details

Details for the file fastbaps-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

  • Download URL: fastbaps-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 426.4 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastbaps-0.1.1-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bcd73baeb04753c2e776032e2547ac40fe6b404e9c946b0af4f5836d4f25fe82
MD5 03aab57edbf637e9f14c9c4a56b49740
BLAKE2b-256 8acd7b6f5f5c61aff61ed0afc7b9a28185f6531ae54aa7e325d532fac3b1bb84

See more details on using hashes here.

File details

Details for the file fastbaps-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

  • Download URL: fastbaps-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 414.6 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastbaps-0.1.1-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 32781540f0f8fc2dc0c3d6af9cea8d39537c06a1a6aa71938dc9aa4e1af47531
MD5 e365cf26c99cc992d77f8e2c31354bb6
BLAKE2b-256 bef0d43a1a471faabf2565fa5953e75045a19290378f9af1e0adfe69979bfa82

See more details on using hashes here.

File details

Details for the file fastbaps-0.1.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: fastbaps-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 367.8 kB
  • Tags: CPython 3.9+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastbaps-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 82b0b7d7f31360d76affb2cde24f8cac1154bf610bd0c27ad1962bfabedd40cb
MD5 4dc1e129cf4ea59fbe661e557454553a
BLAKE2b-256 7c786ba2e909d1fe7d55f7d0fe051861dbcc60c01360285ab8b677293a61456e

See more details on using hashes here.

File details

Details for the file fastbaps-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: fastbaps-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 372.8 kB
  • Tags: CPython 3.9+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastbaps-0.1.1-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e734bd8adb1e934d412d6fabf4ec3f43756a37ccc6394d1f2d3d38e5ea375b23
MD5 4fba8922feab391060ecdbf506d0a922
BLAKE2b-256 86825c74f6f4153bed444f641cb4193ab834a2f1042ef2b2c77479be50a56dcf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page