Skip to main content

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 = sorted(scorer.kmer_scores_.items(), key=lambda x: -x[1])[: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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kmer_learn-0.1.2.tar.gz (494.5 kB view details)

Uploaded Source

Built Distributions

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

kmer_learn-0.1.2-cp314-cp314t-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

kmer_learn-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

kmer_learn-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

kmer_learn-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

kmer_learn-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

kmer_learn-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

kmer_learn-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

kmer_learn-0.1.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file kmer_learn-0.1.2.tar.gz.

File metadata

  • Download URL: kmer_learn-0.1.2.tar.gz
  • Upload date:
  • Size: 494.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for kmer_learn-0.1.2.tar.gz
Algorithm Hash digest
SHA256 59d15edbe5fcc22e1b68edcccfd5e5692a5e0631b2e473982abdff0be490453f
MD5 ab77ae1459a6fba6b0ba636a0b21d95b
BLAKE2b-256 073b4ced9dad00440fa46944c676743f970dc1cb6c88ffc6918ff3aa6a4a1d04

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2fa3826e0277145525ba943cd0ffe842943ce11b78fe3ae7d43d237b6e667ab3
MD5 a8cda2bc61ee305de79203fc902d3ceb
BLAKE2b-256 0a81e4dc7eda3ebe202c9e3e148203f032166c0393278cf12dffba37cf6762b5

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 2d17f2a4ef2ec908705d88b425f287fe2b79ee61ed9773540229e4d66b78d7da
MD5 aef689fa314b7caa4f7524b6d5eb1eda
BLAKE2b-256 c22507588e3fff04bcf6df18767bdbb9cf25822ba226a5c9f8d1c2ad7ea02a38

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 82ba02f7c78326fef42fe63b6bf6300b64136dce312c24140dabc919f0651476
MD5 a31c64a22b527b67623abdd62b7fdf75
BLAKE2b-256 8c04fdf2f886a4de1ea8bcd7754bdcf964a7f62b47e4e7fcb3e3c902c99fed47

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c8863d4b717e4c41f53e37e7cae35caf49bc212611f4b8cfd10c7767634a6109
MD5 93a939847b5e9e30f369b3f8ea501461
BLAKE2b-256 d2da11a4d2f474f3d6d4a2db923e271bdb01cdc4a22a1998dc6324527c77c7ff

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7bccd254e547678619ba1616b3d48c4786d519484588d10901c952146f2ac035
MD5 536aeba644880615c20cc64cb1419dbe
BLAKE2b-256 afd63cff0c38f57166465c9ff4d5731955c28f3f9444d9d236bb928527e291ed

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5461e79c1ae97068e2858129b19d600d848a1c585ba8b529266096a4f7219571
MD5 31254990ae897a084f4107ae8fa62f84
BLAKE2b-256 6d3731fd138e39d978dcd9102641d911db4437d2847270ec996bed432a80c7e2

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 946a63ec9f9340c67c72ee40f45ca5ad2e812643551b072c031b2a0379e113d5
MD5 692181ae414b320b25465d2fa0133933
BLAKE2b-256 464bb1bb4096b61958a7ceec39537ac3702aede9883d25a716668e8a767e257b

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 996f8a793d0c705b830b32305812f560e1de1a4aeeda47ae1044ddfab79ccbf4
MD5 a52e280ac3746edb825e5179fb034917
BLAKE2b-256 d251a897cba8d02c3e466a957beac84f082f4c366d7bc5f474117b9af6228820

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 00e19434fd4e25f264df80cadb397bc24db098e61179629313ca46351932ba87
MD5 851b98901ca396429eb95ef950ac5014
BLAKE2b-256 76337a6587075049f4e62878c7f95bc7532e0e5605458b2a682890c6c145f910

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f091a694eabd7c920dd75c6ab5ad8c7e4ec66192f0d08d1c456dafea1ea20a24
MD5 8f5faa3e727dc3b7f91cc0041c8ca73e
BLAKE2b-256 0bc6b46191dd8e5c12e981af0f36321566297ba5689ef6f0cf5b3fab1757b20d

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1d0a3193032d37497101778004357002195bf230d92be0b16b10c07d0c5283a1
MD5 2d2a538ea5d25757f6fa7755e7ecea9a
BLAKE2b-256 6cbef2407bc93f879db7fa6e686e85cc9961b5573b0681d853276162a156e3e9

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5172958b65b087af04e90f7d94e96b38f832bb4e988f7d5fd41472935f9abed7
MD5 d7246f3df29ed2d62597e74899f6b1ad
BLAKE2b-256 92a3f79e30b6b2e1c9027a6644b4a1474ca9a191fcd15abacc45341ec087bd52

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e4c9bb1cacf9254f81aba359b0a30351421f10ad035cd5e9ec56029366b47ca2
MD5 8e82b7a1283d7adf7e33ca76781d23f7
BLAKE2b-256 981b68b4068d2a534c6af89156bdb2cc322accd1988e6051d2e63461e33172ac

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.2-cp39-cp39-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9b11c686836a56ea08c7c98cf171b561cf437da3072bbf383198f98abbf708bd
MD5 45f19de4418f15309d74a7a030ed5247
BLAKE2b-256 28a04cb2e0e3b2e9ea422d77dd0425307ea636b3a882762e889cdb031bef1ee0

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