Skip to main content

BLOSUM62-aware anchor-residue clustering for immunopeptides, with a Rust backend

Project description

PepCluster

BLOSUM62-aware anchor-residue clustering for immunopeptides — with a fast Rust backend.

PepCluster groups peptides by the similarity of their MHC-I anchor residues (the first 3 + last 3 amino acids) using a BLOSUM62-normalized similarity metric with double weight on the primary anchor positions P2 and PΩ. It is built for large immunopeptidomics datasets: a Rust extension does the heavy lifting (10–100× faster than pure Python), and a pure-Python fallback keeps it working everywhere.

Unlike general-purpose sequence tools (e.g. MMseqs2), PepCluster distinguishes anchor from non-anchor positions, which is what actually drives MHC-I binding specificity — producing biologically interpretable clusters on short (8–14 aa) peptides where full-length estimators break down.


Install

pip install pepcluster

Prebuilt wheels are published for Linux, macOS, and Windows, so no Rust toolchain is required for end users. If you install on a platform without a wheel, pip builds from source (needs a Rust compiler — see Building from source).


Quick start

Command line

pepcluster -i examples/peptides.fasta -o out -t 0.6

This writes cluster assignments and per-cluster FASTA files under out/ (see Output files).

Python

import pepcluster

# End-to-end: FASTA in → TSV + per-cluster FASTA out
stats = pepcluster.cluster_fasta("peptides.fasta", "out", threshold=0.6)
print(stats["n_clusters"], "clusters")

# Low-level: cluster a dict of unique 6-mer anchors → frequency
mapping, n_cmp, n_early = pepcluster.cluster_anchors(
    {"YLLAGV": 3, "YMLAGV": 1, "GYAWTK": 2}, 0.6)
# mapping: {anchor -> representative anchor}

# Optional Lloyd-style refinement on top of the greedy result
refined, refine_stats = pepcluster.refine_clusters(
    {"YLLAGV": 3, "YMLAGV": 1}, mapping, 0.6, iterations=3)

pepcluster.HAS_RUST tells you whether the compiled backend is active (cluster_anchors / refine_clusters automatically use Rust when available and fall back to identical pure-Python implementations otherwise).


CLI options

Flag Default Description
-i, --input required Input FASTA file
-o, --outdir anchor_clusters Output directory
-t, --threshold 0.6 BLOSUM similarity threshold (0.0–1.0)
--min-cluster-size 2 Min members for a per-cluster FASTA
--n-front 3 N-terminal anchor length
--n-back 3 C-terminal anchor length
--refinement off Apply Lloyd-style refinement after greedy clustering
--iterations 3 Max refinement passes (with --refinement)
--backend auto auto | rust | python
-q, --quiet Suppress progress output

Threshold guide:

Value Effect
0.8 Strict — mostly exact matches + very conservative substitutions
0.6 Moderate — allows 1–2 conservative substitutions (recommended)
0.4 Relaxed — broader groups for exploratory analysis

Output files

out/
├── clusters.tsv            # cluster_id, representative_anchor, header, sequence, anchor (every peptide)
├── cluster_summary.tsv     # cluster_id, representative_anchor, size (sorted by size)
├── summary.txt             # run statistics
└── fasta/
    ├── cluster_0.fasta     # per-cluster FASTA, ready for MSA (>= --min-cluster-size members)
    ├── cluster_1.fasta
    └── SHORT_peptides.fasta # peptides too short to form an anchor (if any)

How it works

  1. Anchor extraction. Each peptide is reduced to its 6-residue anchor: the first --n-front (3) and last --n-back (3) amino acids. Peptides shorter than that are set aside in SHORT_peptides.fasta.
  2. Deduplicate. Peptides are grouped by their exact anchor, so clustering operates on unique anchors weighted by frequency.
  3. Similarity metric. Two anchors are compared position-by-position with a BLOSUM62 score normalized to sim(a,b) = B(a,b) / sqrt(B(a,a)·B(b,b)). Positions P2 and PΩ carry 2× weight (the primary MHC-I anchors); the score is a weighted mean in [−…, 1].
  4. Blocking. Unique anchors are bucketed by a reduced 10-letter alphabet at P2 and PΩ (10×10 = 100 bins), so only plausibly-similar anchors are ever compared. High-weight positions are checked first with early termination.
  5. Greedy clustering. Within each block, anchors are processed most-frequent-first; each joins the first centroid above threshold or becomes a new centroid.
  6. Optional refinement (--refinement). A Lloyd-style pass iterates: medoid update → cross-block reassignment → centroid merging, until stable.

The Rust backend (pepcluster._core) and the pure-Python reference (pepcluster.clustering) implement identical logic and produce identical cluster assignments; the test suite asserts this parity.


Performance

Dataset Python Rust
7K peptides <1 s <1 s
2.5M peptides ~3 min ~15 s

Speed comes from anchor deduplication, coarse-alphabet blocking, and weighted early-termination in the similarity check.


Building from source

Requires a Rust toolchain and maturin.

# one-time
pip install maturin

# build + install into the current environment (editable-ish)
maturin develop --release

# or build a wheel
maturin build --release      # wheel lands in target/wheels/

The project uses maturin's mixed layout: Rust lives in src/lib.rs (compiled to pepcluster._core), Python in python/pepcluster/.

Run the tests with:

pip install pytest
pytest

Releasing (maintainers)

Wheels are built for Linux / macOS / Windows by .github/workflows/CI.yml and published to PyPI on version tags via PyPI Trusted Publishing (OpenID Connect — no API token or stored secret).

  1. One-time: on https://pypi.org/manage/account/publishing/ add a pending publisher — Owner AmirAsgary, Repository PepCluster, Workflow CI.yml (leave the environment blank).

  2. Bump the version in pyproject.toml and Cargo.toml.

  3. Tag and push:

    git tag v0.1.0
    git push origin v0.1.0
    

The release job then builds all wheels + an sdist and uploads them to PyPI.


License

MIT © 2026 Amir Asgary


Citation

If you use PepCluster in your research, please cite this repository:

Asgary, A. PepCluster: BLOSUM62-aware anchor-residue clustering for immunopeptides. https://github.com/AmirAsgary/PepCluster

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

pepcluster-0.1.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distributions

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

pepcluster-0.1.0-cp38-abi3-win_amd64.whl (148.7 kB view details)

Uploaded CPython 3.8+Windows x86-64

pepcluster-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl (490.9 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ x86-64

pepcluster-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl (457.1 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

pepcluster-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (286.7 kB view details)

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

pepcluster-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (280.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

pepcluster-0.1.0-cp38-abi3-macosx_11_0_arm64.whl (250.1 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

pepcluster-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl (254.8 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pepcluster-0.1.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pepcluster-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7978368cb3f26ba31426599091f3163ac61cdc0b6a0202c93ededc883905c493
MD5 1c20b380453c487e7d905d4086047b7b
BLAKE2b-256 3daf611ec7b0428462964a50fb50b94e74a5aa72cd5b1a4f1f40b042f3201d46

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0.tar.gz:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: pepcluster-0.1.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 148.7 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 ab8ce831e8c91162e0acd7fc132e795cb3e9493298e97d7bb7b4b3d67deee1a4
MD5 4d6fa2ad3381ea9c1a62bb4a7053096e
BLAKE2b-256 a404ab12c930810400915df5a824e6cc8efb6422415a6f34c852bebfe39d54df

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-win_amd64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ee573d52859b866fe9ec7ea27fcf67147743851fafbddf0425903dc5f04d2b69
MD5 53543a0f5e0ab8e091fb80f94237da10
BLAKE2b-256 d4eacc4a265c078ab6731248dab8e31019f5995e98977bdb2a8ade7762c2f3d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5d8fc47b5e5f86cf079e0bc6b17dd3a5e0c5f9fbbae77b339736b9d722088c58
MD5 0243d677689a02a7b8a418ce3db8f19e
BLAKE2b-256 00b9b196cc51d2b079fae6e4a8753fefb964320a8fd677ddc2a4785ba663e84b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-musllinux_1_2_aarch64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 15babfe893761f6e627b89242a2919412ea4f87962fd263d617c6cbb04a96915
MD5 2258345d462a49cac06c1f6f8528c817
BLAKE2b-256 acb016262c9ede7d602596198da883c494725368be065448b22c8255f810dd47

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e745f4c4a6c60963fab2ff81f931d969b63c427133abcac2cd9baec1903bb803
MD5 966172cfdebf8007f7a2eee67378c2af
BLAKE2b-256 f2ab1930c3cdaa97f806c079848f9daa505c1179b52116871213633acad06b96

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 284ed068d84148804869b071a5692d4f0aa0b2d4fb4e20c03e9f3e8f3c744a50
MD5 aa672dcfd1d7548f03d73f9dd1fc3df0
BLAKE2b-256 001cf61cfefc928f01ff93336181904150f17b898e4f2f2161cafd85d07ff8da

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pepcluster-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ce7da729f46d3bcd461e2610730a7f9df0875a96c2fd94244c30e0bf0839d310
MD5 0d9db64b50f2dd8cbda304ea4cb00c5c
BLAKE2b-256 6ec8af2c5ace6b550c2c34cd5e88b5fed1bb65d140d77a18919c560723991c68

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.0-cp38-abi3-macosx_10_12_x86_64.whl:

Publisher: CI.yml on AmirAsgary/PepCluster

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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