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

Dual-licensed under MIT or Apache-2.0, at your option. See LICENSE-MIT and LICENSE-APACHE.

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.0.tar.gz (36.3 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.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (430.6 kB view details)

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

fastbaps-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (418.5 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

fastbaps-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (372.1 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

fastbaps-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (376.9 kB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: fastbaps-0.1.0.tar.gz
  • Upload date:
  • Size: 36.3 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.0.tar.gz
Algorithm Hash digest
SHA256 b7848742f5d5a1428356d951395fbcb63f0b6e02e13a8188bcaf2968ff926aa2
MD5 b9334156d6d766a322b29447f0f7f798
BLAKE2b-256 bf0d209256922c133f2c0634d07e14241d37d17ca5eaf7539757361359aa104f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastbaps-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
  • Upload date:
  • Size: 430.6 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.17+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","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":true}

File hashes

Hashes for fastbaps-0.1.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 798951bcb390ec7460be23d7a1e90166655fd1d2729ae902c435e4153b9f1b6d
MD5 a0512d7ca4608b36c15c08c1f231f210
BLAKE2b-256 d20dab7e6480bd1503891a8d9bb6c2c850a8fe51d4946fb04394e1246cc89216

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastbaps-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
  • Upload date:
  • Size: 418.5 kB
  • Tags: CPython 3.9+, manylinux: glibc 2.17+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","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":true}

File hashes

Hashes for fastbaps-0.1.0-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5ded7b0791ef1cdd8c67c5afadfd142863be58624099a652468436e16f08deab
MD5 50d578c6e7fee743269ebf364f58d7f8
BLAKE2b-256 577f77aef88e496a0b6e0475ce5dbf4b84d03745628face7d3a2663083f74217

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastbaps-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 372.1 kB
  • Tags: CPython 3.9+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","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":true}

File hashes

Hashes for fastbaps-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6f77c2e82f743968323d7fdacbdf3fb50a1385edfc63a6021e82a3c9bd061882
MD5 7da9c15ad9585b54178da6babebfc341
BLAKE2b-256 5e89878f3c3826fac2484700c7a0ce23252f21126dec7bdc6e3eb881779896fc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastbaps-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 376.9 kB
  • Tags: CPython 3.9+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","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":true}

File hashes

Hashes for fastbaps-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3c0735c2f75f9cbf74061c634ab166d8e9c513b9d40d199ea6afd2cb01f26916
MD5 edf13155b52ab661ab17234e5ec69491
BLAKE2b-256 8b168633dc322355535c4e132da6a38a31bb1115e90de29edb1aa3a442c59c48

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