Skip to main content

A modern Python toolkit for classical sequence machine learning

Project description

kmer-learn

Classical machine learning primitives for nucleotide sequences — kernels, encoders, distances, models, and sequence perturbation, all in one composable Python package.

⚠ 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:

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.
  • Spectrum / gappy kernel — Leslie CS, Eskin E, Weston J, Noble WS. The spectrum kernel: a string kernel for SVM protein classification. Pacific Symposium on Biocomputing. 2002:564–75.
  • Dinucleotide shuffle — Clote P. Efficient calculation of the number of native states of a protein. (2003, unpublished note); Altschul SF, Erickson BW. Significance of nucleotide sequence alignments: a method for random sequence permutation. Bull Math Biol. 1985;47(4):541–51.
  • Philox4×32 RNG — Salmon JK, Moraes MA, Dror RO, Shaw DE. Parallel random numbers: as easy as 1, 2, 3. SC '11.
  • Multinomial Naive Bayes — Manning CD, Raghavan P, Schütze H. Introduction to Information Retrieval. Cambridge University Press, 2008.
  • 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.
  • NUC4.4 matrix — NCBI standard DNA scoring matrix.

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.

License

To be specified.

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.0.tar.gz (322.0 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.0-cp314-cp314t-musllinux_1_2_x86_64.whl (680.1 kB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (644.5 kB view details)

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

kmer_learn-0.1.0-cp314-cp314-musllinux_1_2_x86_64.whl (661.0 kB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (624.9 kB view details)

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

kmer_learn-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl (660.7 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (624.5 kB view details)

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

kmer_learn-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl (660.5 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (624.4 kB view details)

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

kmer_learn-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl (656.9 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (620.2 kB view details)

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

kmer_learn-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl (651.3 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (614.3 kB view details)

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

kmer_learn-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl (649.9 kB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

kmer_learn-0.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (613.1 kB view details)

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

File details

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

File metadata

  • Download URL: kmer_learn-0.1.0.tar.gz
  • Upload date:
  • Size: 322.0 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.0.tar.gz
Algorithm Hash digest
SHA256 3cf700cfaea5105d6761bda9f852217d90e64b5bc9dc6f941ef5b43c955d3db5
MD5 0e5c061d46bfa4000bc7aa405002707d
BLAKE2b-256 742cbc4476860cd3c524c31564c2679de1e729d2defdec387d6d5b75351cf808

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d754427530c17538e9f732fbc1002ac056d4bdb4f89430b5a814513f274a9dbf
MD5 c81d26e21a2244144da538e1d6e31576
BLAKE2b-256 e1ae9eb4cc8a5fbab118ad1dfc73f50d4357a2e056d8712b0884eab30915fc2a

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 64a06bf2041d3f99f283c804b859b06c8b427c358770b3b397f523638d814f38
MD5 eed5955e3083758a9eab985acb21e839
BLAKE2b-256 4664714257e3e222c746a70fe17e4c4abf2b54a604cfdfb3944b57ae4ad6330b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e2d5108ed6d2507fff8f23f5ee3147b8cc00b9bb2cd8c6977f238619b0b74896
MD5 16fd156fc881ec4510065b5747099dca
BLAKE2b-256 38026bc130442a682ddc7f12de5e7453b8d6d02efee52c9b32fdd2317618131f

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 76ad88adac7c97628d0f18c8c33c2e8a41c6d71b8d49bff00c4dbc3eb4824360
MD5 48035c30da44fafe00bb545a81fc3618
BLAKE2b-256 a8eeeb88d937bcff4f733976bcef07499f19291fb381f58d2f9f8653e66a8261

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 205984c30008e45a4a5d42c8b3e4c4d98961b06d457c25110a543cb8fd5861c4
MD5 cc71a7df9deb1eea394d2fe6d08e4c29
BLAKE2b-256 ee33ef6833054275c0e31baaba6cdeed3102258113de4544e6dc31a492752226

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ce3079c1dd1d4452272994b119199b8bf6b3269e18b388f8e9c59efa8759e80c
MD5 b9f0e111397090d1b9925567e5ee48ff
BLAKE2b-256 a19ebba5b6e199e05e43fc9cc94b4fb9b543b3ffb9acc2fe5d643d58fae1009c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6bb73eb1fdd419f2efdfdedbeb7df65c1f637386c291f12af1f767fb17c5b54b
MD5 7dc58d31a0429c6ed614e9efd8994f25
BLAKE2b-256 76947c7b046fa0506e1e165b7c6bc3cee2e013c89182c86cddf2b6c7bbecce7a

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ad7c1c49264d9c68e7325716aaa680be11ad758cddecbabd8604c2da3d414b70
MD5 26b325e42aa13cbaa4a7502009b41515
BLAKE2b-256 e41fab53401c78d688ecc0f8431587ab452e80d7bfa612cca50b56b7a3c4a324

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 09c086f952e84bb1bc29e4cc5e6a304a43f60530710c3ce07f779316f284c058
MD5 47d6233d3e730bac591290794428e8c5
BLAKE2b-256 66d30d89490a205cbd8c6483cca326228b0182dce50ecfe36be9e0617395526a

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 76b6a07d20b7a043ad8e08113971bdee02a18cee59a00ea793bb19b4bc846982
MD5 a8e002fbc7ec588677744faf8f7ff105
BLAKE2b-256 253b7ca34627aafa8a5fe8861b7e5bee21e826f06065638b0503a50173211721

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 61f90813c4a8b6053f55ad9f23448649ef34105e357ffe471a2f5dbee11d3f0a
MD5 c61d6f49fa12c3296ca1d230510bb6ea
BLAKE2b-256 977d32ad54250fe57d470f308ba1f3c2ea6f76198b2d6005d45f4a464f4e065a

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0640b1d4523c4265b461e5162bfd2017562bce5e03e880f63af799c05f9765c3
MD5 eaeb982b8e02a38755b8d05c6532f913
BLAKE2b-256 13731a219f35e96b72819c0efbac5b892dc9237f64c144a18b07aa73df119ef1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 85b73fb6847b83e9d9616668cf8f366ea27e28d0ed6961d6d08535a695da31a1
MD5 8502f58d5986eb4fdb2d2606de903c87
BLAKE2b-256 cb9a0cbaf3e45f35986fe36ba38f0d812b26496e49ed484266a5faf6374e0685

See more details on using hashes here.

File details

Details for the file kmer_learn-0.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for kmer_learn-0.1.0-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5601720b8d26913dfa3ebb2ccdd5f28723549d0c63de8cfad98669325838c8b6
MD5 0d0316cdcfc4e26ac408be485b6d8d7e
BLAKE2b-256 0a8250b38939b3c66f31f945b47efaf9d18f836cec6ed5bfeee246a3b3cf3cbe

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