A modern Python toolkit for classical sequence machine learning
Project description
kmer-learn
A modern Python toolkit for classical sequence machine learning.
⚠ API stability: This package is in early development (v0.x). The public API is not yet stable — breaking changes may be introduced between minor versions until v1.0. Pin your dependency to an exact version (e.g.
kmer-learn==0.1.0) if reproducibility matters.
Install
pip install kmer-learn
From source (requires a C compiler):
git clone https://github.com/synbioml/kmer-learn.git
cd kmer-learn
pip install -e .
Optional backends (strongly recommended for speed):
pip install rapidfuzz parasail # 10-100x faster edit distances and alignments
pip install pyfastx # fast FASTA parsing for examples
Features
Kernels
- GKMKernel family — gapped k-mer kernels from gkmSVM / LS-GKM (Ghandi et al. 2014; Lee 2016). C-backed, OpenMP-parallel, with full/estimated/truncated mismatch schemes, 6 post-transforms (RBF, poly, sigmoid, exponential, …), 5 positional weighting kernels (triangular, Epanechnikov, Gaussian, Laplacian, Cauchy), reverse-complement indexing, sliding-window scan, and 3D windowed tensors.
- DistanceKernel — turns any distance into a kernel via a post-transform.
Encoders (CSR output)
- SpectrumEncoder — plain k-mer counts via rolling hash (k ≤ 12).
- GappyEncoder — gappy k-mer counts with explicit masks (
"*--*") or gap ranges (L=6, g_min=2, g_max=3). - MismatchEncoder — mismatch-tolerant k-mer counts (Leslie, Eskin, Noble 2004).
All encoders support canonical_rc=True for reverse-complement collapsing.
Distances
- Hamming, Levenshtein (rapidfuzz backend + Python fallback).
- NeedlemanWunsch, SmithWaterman (parasail backend + Python fallback), with custom substitution matrices (NUC4.4, BLOSUM62, …).
Models
- DifferentialKmerScorer — Multinomial Naive Bayes on k-mer features, with auto-generated negatives via Shuffler/Chunker.
- KernelSVM — SVM with a precomputed kernel (works with GKMKernel and DistanceKernel).
- LinearSVM — Linear SVM on encoder features.
- KNNClassifier — k-Nearest Neighbors with a sequence distance.
Sequence perturbation
- KmerShuffler — k-mer-preserving shuffle via random Eulerian paths in the De Bruijn graph. Three endpoint modes (preserve / free / crop). Philox4×32-10 RNG, reproducible across
n_jobs. - Chunker — block-level perturbation: split into chunks of size
[min, max], optionally reverse-complement each, shuffle, concatenate. Five residual-handling modes, two algorithms (random / backtrack). - BaseBackgroundModel — ABC for custom background models.
Utilities
kmer.utils— bit-packed k-mer helpers (kmer_to_code,code_to_kmer,reverse_complement,canonical_code).
Quick start
from kmer.kernels import GKMKernel
from kmer.models import KernelSVM
# Train a gkm-SVM
clf = KernelSVM(GKMKernel(L=10, k=6, d=3, kernel_type="truncated", use_rc=True), C=1.0)
clf.fit(positives + negatives, [1]*len(positives) + [0]*len(negatives))
preds = clf.predict(test_seqs)
from kmer.encoders import SpectrumEncoder
from kmer.models import DifferentialKmerScorer
from kmer.perturb import KmerShuffler
# Differential k-mer scoring with dinucleotide-shuffled background
scorer = DifferentialKmerScorer(
featurizer=SpectrumEncoder(k=6, canonical_rc=True),
background=KmerShuffler(k=2, seed=42),
)
scorer.fit(positives)
top_motifs = scorer.kmer_scores_.sort_values(ascending=False).head(20)
from kmer.distance import Levenshtein, DistanceKernel
from kmer.models import KNNClassifier
# KNN with edit distance
clf = KNNClassifier(Levenshtein(), n_neighbors=5)
clf.fit(train_seqs, y_train)
Examples (Vignettes)
The examples/ directory (top-level, next to kmer/) contains a series of cross-linked Jupyter notebooks. Each notebook starts with a vignette index linking to all others.
| # | Notebook | Topic |
|---|---|---|
| 01 | 01_basic_kernel_matrix.ipynb |
GKMKernel: build, inspect, verify invariants |
| 02 | 02_distance_metrics_and_kernels.ipynb |
Distance metrics (Hamming, Levenshtein, NW, SW) + DistanceKernel (RBF, PSD, KernelSVM) |
| 03 | 03_svc_with_kernel.ipynb |
Train a gkm-SVM with KernelSVM |
| 04 | 04_clustering_sequences.ipynb |
Hierarchical clustering with kernel distances |
| 05 | 05_score_long_sequence.ipynb |
Sliding-window scan of a long sequence |
| 06 | 06_weighted_kernel.ipynb |
WGKMKernel positional weighting (centered motif) |
| 07 | 07_transform_and_comparison.ipynb |
All 3 schemes × 6 transforms, GKM vs WGKM |
| 08 | 08_windowed_3d_tensors.ipynb |
WindowedGKMKernel 3D output (line plot) |
| 09 | 09_spectrum_encoder_and_differential.ipynb |
SpectrumEncoder + DifferentialKmerScorer |
| 10 | 10_gappy_encoder.ipynb |
GappyEncoder with masks, gap ranges, RC collapse |
| 11 | 11_mismatch_encoder.ipynb |
MismatchEncoder and comparison to spectrum |
| 12 | 12_shuffler_and_chunker.ipynb |
KmerShuffler + Chunker for negative-set generation |
Citation
An article describing this package is in preparation. Until it is published, please cite the package as:
Zinkevich A. kmer-learn: Classical machine learning primitives for nucleotide sequences. (in preparation).
For the mean time, if you use the package in your research, please cite the relevant foundational works listed below.
References
The package builds on the following foundational works:
- gkmSVM — Ghandi M, Lee D, Mohammad-Noori M, Beer MA. Enhanced regulatory sequence prediction using gapped k-mer features. PLoS Comput Biol. 2014;10(7):e1003711.
- LS-GKM — Lee D. LS-GKM: a new gkm-SVM for large-scale datasets. Bioinformatics. 2016;32(14):2196–8.
- Mismatch kernel — Leslie CS, Eskin E, Cohen A, Weston J, Noble WS. Mismatch string kernels for discriminative protein classification. Bioinformatics. 2004;20 Suppl 1:i467–76.
- Needleman-Wunsch — Needleman SB, Wunsch CD. A general method applicable to the search for similarities in the amino acid sequence of two proteins. J Mol Biol. 1970;48(3):443–53.
- Smith-Waterman — Smith TF, Waterman MS. Identification of common molecular subsequences. J Mol Biol. 1981;147(1):195–7.
Third-party libraries used as optional backends:
- rapidfuzz — fast Levenshtein and Hamming distances.
- parasail — SIMD-accelerated sequence alignment (Daily, 2016).
- scikit-learn — SVM, Naive Bayes, KNN.
- NumPy / SciPy — array and sparse-matrix infrastructure.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kmer_learn-0.1.1.tar.gz.
File metadata
- Download URL: kmer_learn-0.1.1.tar.gz
- Upload date:
- Size: 354.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
120fe1c82dc7c5266901f84bf38ff75c0a1ba47b8c1f146d80125b59d3c414e5
|
|
| MD5 |
d1d7f32271740cee3d8902eee8581999
|
|
| BLAKE2b-256 |
ac0470f7a6ae7b9106466c674b9e2a5a70f9122082e32c96f00cee5239c90ac2
|
File details
Details for the file kmer_learn-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 673.8 kB
- Tags: CPython 3.14t, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
864b32c3764c1a76350beec3a59a08627acfc3e80626944e157a1d2157d6694f
|
|
| MD5 |
383fde7089f58b53d9aab95f39d779e6
|
|
| BLAKE2b-256 |
b39c751c4db4b6af6ffe928747bfea36e06aa885952d1b34b140b1df400cad65
|
File details
Details for the file kmer_learn-0.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 638.0 kB
- Tags: CPython 3.14t, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96c90195e189cbcb63a9c850436529ee48102edefea96237a717ded35e16c50a
|
|
| MD5 |
4252ac81584b32a76c0a6712900fef67
|
|
| BLAKE2b-256 |
2e3f89b3ec197c42135658e261e48a272dfd362381c49eb7cd37c02c8f1240ce
|
File details
Details for the file kmer_learn-0.1.1-cp314-cp314-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp314-cp314-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 656.0 kB
- Tags: CPython 3.14, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1797d4c9e22a192dc97675b848022dcb94e8bbb23bbfb4c7bbbfdfc883da5616
|
|
| MD5 |
82634df0050395066d0951a7841dbc96
|
|
| BLAKE2b-256 |
781cc3c41c8d6b9ad9834204d97acbe7eca70d927830b027a4e975a7705e5896
|
File details
Details for the file kmer_learn-0.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 619.9 kB
- Tags: CPython 3.14, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e2dafd4aec19c6f10f452af4bd8cc5b2fe9c694aa7412dd0e0f479d194c4071
|
|
| MD5 |
7c4b8cf999bfa98927f8f344ffe932a0
|
|
| BLAKE2b-256 |
53ec97305bce2c2610ac0e9b1ef381cc78cd6a4084154b673670f5dc4f59050f
|
File details
Details for the file kmer_learn-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 655.6 kB
- Tags: CPython 3.13, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
381d58270eafb509c02554e1af47c446a4bb8fb5f27e427f7571db5ddaff2f25
|
|
| MD5 |
be41faed99c2b05f17b89db3334b4125
|
|
| BLAKE2b-256 |
a72329d10173abe6d760867434a19be07e254faa19cc325bef83378e25c60e27
|
File details
Details for the file kmer_learn-0.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 619.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbae9b3f6bc4dec225ccc2d3826c00501ad5f2208c0c09d0bd6987c9e4175ca1
|
|
| MD5 |
070402ce7f67e1efeaecff27a0a25fbb
|
|
| BLAKE2b-256 |
616946369076710373bfd07de349a1716277ea64d925d6caec0abddce364bd0f
|
File details
Details for the file kmer_learn-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 655.5 kB
- Tags: CPython 3.12, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bae49bb9fdea7b36ba3f6040f4aa4a1fe841dec236d151e9578b60f5585c180
|
|
| MD5 |
acf25f478e7f94043bcdb36e1434b210
|
|
| BLAKE2b-256 |
835361b256b01df7cf2f40f04a7b75fe6d271f7427aa7f456c2486293b857e5e
|
File details
Details for the file kmer_learn-0.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 619.3 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3c04661c08340386aa568b8bd909f10ec2952d27e3248fbf513bc2b9c20c7de
|
|
| MD5 |
bfa5f6b0abf5347319b76c4c8614ece6
|
|
| BLAKE2b-256 |
57fcd0a73e67dcabb2d21074e1f2b8e306a1b158104995b065213d56329e7a6e
|
File details
Details for the file kmer_learn-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 652.2 kB
- Tags: CPython 3.11, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbda5f2e7148e5308485e50ca6bf2062eb2cff91ca78320f38fef974422ea1e1
|
|
| MD5 |
b0bd4b06d4b6021bfed87e65dbfcf0ae
|
|
| BLAKE2b-256 |
79d038d2511f356fe5db7a819379b8d16bc10ffcb38885d1b053f20d717d7bd7
|
File details
Details for the file kmer_learn-0.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 615.6 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e02516cf0a2df3a6389f21c073225851c5325e0190550714484929f0a26a13c
|
|
| MD5 |
92f86c1d943e4b4fef02963fe82eca8d
|
|
| BLAKE2b-256 |
c746ede2fa55a87249678de136519bc8da8e563fa10dd6a0722fcbbba5d43f61
|
File details
Details for the file kmer_learn-0.1.1-cp310-cp310-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp310-cp310-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 646.6 kB
- Tags: CPython 3.10, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcd9c3ecf51df53c7f04ea35dcb00598aa32774103e6f0c1d2dbc49939d606e8
|
|
| MD5 |
13ff2a69fe91004f770608b0fc366e57
|
|
| BLAKE2b-256 |
8ad1a26df585e3c2472b0a0b35e9f916f25329822663ea5e34e02e9a2f264204
|
File details
Details for the file kmer_learn-0.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 609.7 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6d7e77e3cc0dde3869c55263d1e92b55634c0b4781667701fe29ed74167a23a
|
|
| MD5 |
e839297672e6ffb53cdbdba0377cfa2f
|
|
| BLAKE2b-256 |
6067d7cd86b059e9955b130545ecc6a570c8092cd82e1daa014e61e0bcd59909
|
File details
Details for the file kmer_learn-0.1.1-cp39-cp39-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp39-cp39-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 645.2 kB
- Tags: CPython 3.9, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a671add773d8a86a4ee2683afaa6cebc37494bba1835cb07ad7be98db638a5d6
|
|
| MD5 |
59d970888499f6f780c1b2aedeaa3528
|
|
| BLAKE2b-256 |
b0ed27ed22cfbd465ab965f2743160c9b6439f2e72668d948686d215affc72bd
|
File details
Details for the file kmer_learn-0.1.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.
File metadata
- Download URL: kmer_learn-0.1.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
- Upload date:
- Size: 608.5 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a19c3b9b4ce1c126a83e3fa6838e245355ac8f19f126bf418a74a54fb1958f71
|
|
| MD5 |
ad34cd4b02985b5cdcb1ff1753a48780
|
|
| BLAKE2b-256 |
fe6de3b933c17fdaa89aac4a2b37ab65c4c09fc036ac3a45665e60ddb95d2f57
|