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)
--refine-cap 32 Max centroid comparisons per anchor in refinement reassignment (<=0 = no cap). Lower = faster
--no-merge off Skip the refinement centroid-merge step (much faster on many-cluster data)
--backend auto auto | rust | python
-q, --quiet Suppress progress output

Refinement (--refinement) is optional and can be slow on datasets with many clusters, because it widens each anchor's search to neighbouring blocks and tries to merge similar clusters. Two knobs make it fast:

  • --refine-cap N bounds how many candidate centroids each anchor is compared against during reassignment (examined own-block-first, largest-cluster-first). The default 32 is near-lossless because the best match is almost always in the same block.
  • --no-merge skips the centroid-merge step — the dominant cost when there are many clusters — at the price of leaving some greedy-split clusters separate.

Together they speed refinement up by ~90–370× on many-cluster data with essentially unchanged assignments. Example:

pepcluster -i peptides.fasta -o out -t 0.6 --refinement --refine-cap 32 --no-merge

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, representative_peptide, header, sequence, anchor (every peptide)
├── cluster_summary.tsv     # cluster_id, representative_anchor, representative_peptide, 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)

representative_peptide is the cluster's central member — the peptide with the least average distance (highest weighted average similarity) to every other peptide in the cluster. It is computed in linear time and is always a real member sequence, so it's a good label or seed for each cluster. representative_anchor is the anchor that originally seeded the cluster.


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.1.tar.gz (27.5 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.1-cp38-abi3-win_amd64.whl (165.5 kB view details)

Uploaded CPython 3.8+Windows x86-64

pepcluster-0.1.1-cp38-abi3-musllinux_1_2_x86_64.whl (508.7 kB view details)

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

pepcluster-0.1.1-cp38-abi3-musllinux_1_2_aarch64.whl (475.7 kB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

pepcluster-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (304.6 kB view details)

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

pepcluster-0.1.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (299.3 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ ARM64

pepcluster-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (268.0 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

pepcluster-0.1.1-cp38-abi3-macosx_10_12_x86_64.whl (273.1 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: pepcluster-0.1.1.tar.gz
  • Upload date:
  • Size: 27.5 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.1.tar.gz
Algorithm Hash digest
SHA256 55f798949a7489091708f8445d0e107782921594df0b62c78a8406679121e6ea
MD5 c9e888ace3e0aa3cae62e5069ce060a1
BLAKE2b-256 2c32f78a6a745d6a9e8ebf75e45316288e494dc453b4ab44fe976daa5b1c94bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1.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.1-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: pepcluster-0.1.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 165.5 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.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f129831eb432c41d7742bbb7c23d856eee6b11c809461e8d4e4bd3b61c0eed06
MD5 2f7f2aeafee75dbb7a7e85446808f4ee
BLAKE2b-256 fc94b5de1da6404d1190cc8d408ca1ea61e914db99b26ddf36d1b1c267074f14

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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.1-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.1-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 722741d37e3ee690eb5e83801aaeb4bf8ff32edb8512ce822b10e3b016d8d8e6
MD5 216950cee710b11fb504b283a3f8e2ec
BLAKE2b-256 0702ca242896c4be72a44296e12357200b3e379a6609cfd9673679b5847d385c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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.1-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.1-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 19f75b3ea38652334861904eeed7191205bb0fec6fb9350bc6c833d0603c8707
MD5 5c5fb8a1dc7cf7c302fe79e520d5a883
BLAKE2b-256 114d2bbd485297b4a46635ac273319b933edd28ab1aeb0d2575f78a35cd98911

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 89a35bada43919a101f4d5140a7247339cb69d4c691087dde5db220fc4ee2047
MD5 02b0d92dcd10bb23b9fba4de2e1716f9
BLAKE2b-256 2bc8b26847e6cd0388bb4f557e61b23d0fa03cf0806ea8b9fce443c39edbe9b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 babfbc17ec026416b0a5a2fb334f0eff23e9c579b4ad8c674f4f6a8a6b961708
MD5 1f08c1864ba0b008ab1136e87213dd9b
BLAKE2b-256 0caa2319053cdea5aca938bdb3fafd41bb20c2ba338f5b1c5c872422eed73e7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9572a14adca9b418e98b28bac96b429aeecf5456d3cb4d1ec46d36fd5e6abae2
MD5 9b19b851820fa389bf419c484d65fe64
BLAKE2b-256 d90acabeca4a033ce0a4c0a8a572fe65eba3466969e428ce1a9b7ff583d22cad

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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.1-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for pepcluster-0.1.1-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7432db2d00dacce69142105ed3c467e5358b9d79e0292da78fc6dbc001f4d0f4
MD5 9f98da5828aa200d9bfe3eb28ff57b1b
BLAKE2b-256 cdb1307d0d19bbdc345e64b009a058a4ce49770e4e732a394b6fc1e94e7dd47a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pepcluster-0.1.1-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