Skip to main content

Fast fuzzy search over biological sequences (C++ core, Python bindings)

Project description

seqtree

PyPI Python License CI Docs

Fast fuzzy search over biological sequences (amino-acid or nucleotide), as a C++ core with a minimal Python binding. Build an immutable index once, then search single queries or massive batches in parallel.

Two search engines over one trie:

  • seqtm — branch-and-bound enumeration. Exact per-type edit caps (max_subs / max_ins / max_dels) and a fast Hamming-only path. Best for small edit distances (UMI collapse, error correction, CDR3/epitope matching).
  • seqtrie — full-width edit-distance DP carried down the trie. Honours the max_penalty score budget only; it ignores the per-type edit caps. Use it when the budget is the whole specification.

engine="auto" always picks seqtm, because it is the only engine that enforces the caps you asked for. Results are payload-agnostic: (ref_id, score, n_subs, n_ins, n_dels). Downstream libraries map ref_id back to their own payloads (V gene, MHC, counts) and filter.

Beyond search, seqtree ships:

  • Substitution matrices — built-in identity, BLOSUM45, BLOSUM62, BLOSUM80, PAM250, PAM100, and structural — a Miyazawa–Jernigan interaction-strength matrix: each residue's strength q(a)=mean_b e(a,b) is read off the MJ contact potential, so substitutions between residues of like interaction strength are cheap. It separates strong (hydrophobic F W C L Y M I V) from weak (polar/charged S Q D E K) interactors — the strong/weak-interactor axis of TCR-recognition models (Košmrlj et al., PNAS 2008; MJ contact energies from Miyazawa & Jernigan, J Mol Biol 1996) — letting dissimilar-but-chemically-equivalent loops align. Plus custom matrices via SubstitutionMatrix.from_similarity (Gram penalty s(a,a)+s(b,b)−2·s(a,b)).
  • E-values / significance — calibrate hit counts against a background control repertoire (load_control + evalues), the TCRNET approach on a finite-sample footing. See the E-value guide.
  • Calibrated cutoffsthreshold_for_evalue inverts the E-value into the score cutoff that achieves it, per query. A fixed cutoff is not a calibrated one: a control repertoire is dense near germline and sparse among rare junctions, so the same threshold buys a common query far more chance neighbours than a rare one.
  • Gap-block alignmentgapblock restricts alignment to one contiguous indel, which is the right model for a V(D)J junction and, measured against unrestricted affine alignment, is exactly optimal on 98.8% of genuinely related pairs at a calibrated gap_open. A gap prior (central_prior, profile_prior, frame_prior) chooses where the block goes — a sequence score alone cannot. score_matrix scores a whole query set against a whole reference set in one GIL-released C++ call (532 M pairs/s on 16 cores; numpy.asarray wraps the result with no copy), the shape a prototype-distance embedding needs.
  • Pairwise alignment without BioPythonseqtree.pairwise is Needleman–Wunsch (mode="global") and Smith–Waterman (mode="local") with affine or linear gaps, on the raw log-odds scale. It is a drop-in for Bio.Align.PairwiseAligner — verified against it as an oracle across three matrices, ten gap/mode settings and sixty sequence shapes with zero disagreements — and 65–87× faster, since there is no Python in the per-pair loop. dist_matrix gives d = s(a,a) + s(b,b) − 2·s(a,b) directly. BioPython is a test-only dependency; seqtree still needs nothing at runtime.
  • Island profilesIslandProfile.fit builds a position weight matrix over a set of frame-aligned junctions (an island) and scores a query column by column against the island consensus, as a non-negative penalty that flows through threshold_for_evalue unchanged. At a repertoire-scale cutoff it recovers 48.5% of held-out members against 37.6% for min-over-members; at a loose cutoff the two are indistinguishable, so it earns its keep only where the cutoff is strict.

Install

pip install seqtree       # prebuilt wheels for CPython 3.10–3.13

Prebuilt wheels cover Linux x86-64, macOS arm64 (Apple Silicon), and Windows x86-64. There are no Intel/x86-64 macOS wheels — Intel Macs build from source (see below), which just needs a C++17 compiler and CMake (pulled in automatically by the build).

Build from source

bash setup.sh            # repo-local .venv + editable install
bash setup.sh --tests    # + pytest
bash setup.sh --bench     # + benchmark deps (huggingface_hub, psutil)

Quickstart

import seqtree

idx = seqtree.Index.build(["CASSLAPGATNEKLFF", "CASSLELGATNEKLFF"], alphabet="aa")

p = seqtree.SearchParams(max_subs=2, engine="seqtm")
for hit in idx.search("CASSLAPGATNEKLFF", p):
    print(hit.ref_id, hit.score, hit.n_subs)

# parallel batch (releases the GIL)
results = idx.search_batch(queries, p, threads=0)   # 0 = all cores

# matrix-weighted budget
pm = seqtree.SearchParams(matrix="BLOSUM62", max_penalty=12, engine="seqtrie")
top = idx.search_top("CASSLAPGATNEKLFF", pm, k=5)

# alignment on demand
aln = idx.align(0, "CASSLELGATNEKLFF", p)
print(aln.aligned_query, aln.aligned_ref, aln.ops)

# batch-vs-batch (auto-indexes the larger set)
pairs = seqtree.pairwise_batch(query_set, db_set, p, alphabet="aa")

# E-values against a background control repertoire (TCRNET-style significance)
control = seqtree.load_control("human_trb_aa", size=1_000_000)
target = seqtree.Index.build(vdjdb_cdr3s, alphabet="aa")
for q, r in zip(queries, seqtree.evalues(target, control, queries, p)):
    if r["p_enrichment"] < 1e-3:
        print(q, r["E"], r["n_target"], r["n_control"])

# ...and the cutoff that achieves a target E, per query (-1 = unreachable at this control size)
ceiling = seqtree.SearchParams(max_subs=14, max_penalty=50, matrix="BLOSUM62", engine="seqtm")
thetas = seqtree.threshold_for_evalue(target, control, queries, ceiling, e_target=0.05)

# one contiguous gap block, placed by a prior rather than by the score alone
from seqtree.gapblock import GapBlockIndex, central_prior, embed_in_frame

gbi = GapBlockIndex(cdr3s, "aa", d_max=2)
mat = seqtree.SubstitutionMatrix.blosum62()
for ref_id, score, block_len, block_pos in gbi.search(
        "CASSLGQAYEQYF", 40, mat, gap_open=2 * mat.scale(),
        gap_prior=central_prior(int(1.5 * mat.scale()))):
    ...

# a fixed frame column makes gap placement transitive -- and a column index, hence a PWM, possible
embed_in_frame("CASSGQAYEQYF", width=14, c=4)      # 'CASS--GQAYEQYF'

# a whole query set vs a whole reference set, in one GIL-released C++ call
from seqtree.gapblock import score_matrix, IslandProfile
sm = score_matrix(clonotypes, prototypes, mat, gap_open=2 * mat.scale(), threads=0)
import numpy as np
distances = np.asarray(sm)                          # (len(clonotypes), len(prototypes)) int32, zero-copy

# a position weight matrix over an island, still a non-negative penalty (feeds threshold_for_evalue)
profile = IslandProfile.fit(island_members)
profile.score("CASSLGQAYEQYF")                      # 0 on the consensus, > 0 for deviations

# ordinary pairwise alignment -- Needleman-Wunsch / Smith-Waterman, no BioPython
from seqtree.pairwise import align, score, dist_matrix
score("CASSLGQAYEQYF", "CASSPGQAYEQF", mat)                    # global, BLAST defaults (11/1)
score("WWWAAAWWW", "KKKAAAKKK", mat, mode="local")             # Smith-Waterman
score("AAA", "AAAAA", mat, gap_open=5, gap_extend=5)           # linear gaps: open == extend
aln = align("CASSLGQAYEQYF", "CASSPGQAYEQF", mat)              # + aligned strings and ops

d = np.asarray(dist_matrix(v_genes, v_genes, mat, threads=0))  # s(a,a)+s(b,b)-2s(a,b), zero diagonal

Tests

cmake -S . -B build -G Ninja -DSEQTREE_TESTS=ON
cmake --build build
ctest --test-dir build           # C++ unit tests
pytest tests/python              # Python tests

Benchmarks

python bench/bench_gnuplot.py        # throughput / scaling / matrix / collisions → SVG (needs gnuplot)
python bench/bench.py                # recall vs ground truth (real VDJdb data)
python bench/bench_evalue.py         # true E-value benchmark (target vs background control)
python bench/bench_evalue_matrix.py  # significance across reference/control/query/scope grid
python bench/bench_epitope.py        # epitope detection-complexity (GIL vs NLV)
python bench/bench_gapblock.py       # the gap-freedom ladder: fixed centre → prior → flat → affine
python bench/bench_score_matrix.py   # dense batch gap-block throughput (µs/pair, M pairs/s, RSS)

Figures (throughput, scaling, matrix-scoring overhead, collisions, E-value matrix, epitope detection) and the full methodology are in the benchmarks docs. Set RUN_BENCHMARK=1 for the large tiers.

Development

This repo follows git-flow:

  • master — stable, release-ready; CI + docs deploy run here.
  • dev — integration branch for day-to-day work.
  • feature branches branch off dev and merge back via PR; releases merge devmaster.

Roadmap (affine gaps, position-specific matrices, succinct memory packing) lives in docs/roadmap.rst. Control-set E-values already ship — see the E-value guide.

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

seqtree-0.4.0.tar.gz (1.8 MB view details)

Uploaded Source

Built Distributions

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

seqtree-0.4.0-cp313-cp313-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.13Windows x86-64

seqtree-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

seqtree-0.4.0-cp313-cp313-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

seqtree-0.4.0-cp312-cp312-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.12Windows x86-64

seqtree-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

seqtree-0.4.0-cp312-cp312-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

seqtree-0.4.0-cp311-cp311-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.11Windows x86-64

seqtree-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

seqtree-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

seqtree-0.4.0-cp310-cp310-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.10Windows x86-64

seqtree-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

seqtree-0.4.0-cp310-cp310-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file seqtree-0.4.0.tar.gz.

File metadata

  • Download URL: seqtree-0.4.0.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for seqtree-0.4.0.tar.gz
Algorithm Hash digest
SHA256 6ca79a117148bdfd807802559145af76d2a4f86a0bf949e74c1823987c8e8891
MD5 73569f5b97ebba87893e1d6aa6ff5c4f
BLAKE2b-256 ac5ff2a623093140d472ce170adb977f99867e6e77e07450696972d62a5e9bd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0.tar.gz:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: seqtree-0.4.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for seqtree-0.4.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 41c0b9a938c07ab80350dfc802c964637ae08e3353929f83584621c9db59a711
MD5 15c6e2ea2434cd31d4c6d90577887363
BLAKE2b-256 a1c8d1b9e7c275ef6cfcac3ec04a1ee15f3733b7289c4a27082bdb3de6eef118

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f1b9f458ed21a208e3d41aeb70ec63555bdb104f30f09f02c3cc177df4375639
MD5 8b33c247f2ff4e222ec67a2180d3ecbe
BLAKE2b-256 19e21ec3709432f88f918576407f62f4da99e782bffb5e3f55b8e16619c6ac24

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cf171840a6ee740de429033045dc3e7ab9dc1fd17edecf31c35fc3d21a4c40b8
MD5 8ee1513d48fb7b0ca7883b6417270490
BLAKE2b-256 bd29935e2f7798c3c3122c14a5ffac7ca20121aaa60ec4fa75db4273daf0739a

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: seqtree-0.4.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for seqtree-0.4.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1c55856059ecb10cee0a0db5981c1c4773e61bf5e0b78e5d716abd0bdb2ae133
MD5 e995b7883bc26d02ce514ce66520d3bf
BLAKE2b-256 25a6157327ab4feef9b973683caae649dc3ba7c251f648cca2701f410326fe48

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 93f4027cc9419e0260b533cb0301accb18c1ed257ef65080cf2301f737d67582
MD5 0a885889a003372ca81d349121b12fa6
BLAKE2b-256 d865751e6538c7dcda35deb9e4ec8d8b46c65b09d28e01ba3a8af6bc2d02a088

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 639d64929cb09590d5788c995c1be912327345051c62ab6c1e4e02ef7c2245f2
MD5 56651cf440f785782a71572308425ce0
BLAKE2b-256 da3d11b6ad37884aa8829f50e49a67797812a2be9b19d27dc20a26402c937b74

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: seqtree-0.4.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for seqtree-0.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ff8a18b294b471d32839911d0789b68fbef43bb5dab45d2dc8383f2b9e0c8769
MD5 2ab148e5545d45e5fa1932f5501765e7
BLAKE2b-256 b896a6d5a6f87b89325ba24960ad1c980e8fd4bc9704e7302b98c4d65faf3ca1

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp311-cp311-win_amd64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4fc02296d4437fa57fd6322d84e186f4ea5d221dfac091a65df05777b3b86e89
MD5 b802791b926e0e4f37de528c2222bbd3
BLAKE2b-256 ca0296b13ffdd93781e7417396e8edbe0c65334b70239a539950ae67d63e13f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 777106268332e1a176c45144534553005f21e44d93a000f6e4e1d2960231f4d2
MD5 df696e2b7ff1a89203753fc54d7a3bd3
BLAKE2b-256 7a195a9484b72f2012e8b4a5dfc3bd71f1adcfb055786698d651771aee074ef5

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: seqtree-0.4.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for seqtree-0.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e87cd75c311d1e6af4569c34ac0fa9bdd7ce1ce95ac6cd872b8ec0db12bdf394
MD5 75e673752d4d74982841b3ebcbd18b04
BLAKE2b-256 bff28ef831e448dbda989c02b6b72d80c20ba02664f84f9cd33ab71db6b04c23

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp310-cp310-win_amd64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3729488e6ead4f2623423fd27a719fab292156e214ff5b4da39e9c3c4fe0e152
MD5 3e82291baa9ef8783d7504d2f4855bcc
BLAKE2b-256 7f8e0c776f542583bf634e92751875385822d7db24d5394e279bbf5e10891b36

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on antigenomics/seqtree

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

File details

Details for the file seqtree-0.4.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for seqtree-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 14c552313bdd5bf3785b0a74ccbde1b7b82cd35108d52619a2bced183f90f229
MD5 e99affd1198cb73b52fc3dd17d746b60
BLAKE2b-256 8f57e8db039ef0abb342ceeae82522491a58c7354701dc58cf24cb1a63700924

See more details on using hashes here.

Provenance

The following attestation bundles were made for seqtree-0.4.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: publish.yml on antigenomics/seqtree

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