DNA K-mer Counting with scikit-bio
When to Use
- Count k-mer occurrences in DNA sequences
- Analyze genomic sequence composition
- Use scikit-bio for sequence analysis
Input
/root/input.txt: Single DNA sequence
- Read
/root/input.txtbefore computing anything, and apply.strip()so surrounding whitespace or trailing newlines do not affect the sequence.
K-mer Counting
- k = 3 (3-mers)
- Count all possible 3-mer combinations
- Only output k-mers with count > 0
- Sort alphabetically by k-mer
- Generate overlapping 3-mers across the full sequence.
- Prefer
DNA(...).kmer_frequencies(k=3)over manual sliding-window counting when using scikit-bio. - Only report observed k-mers with count > 0, and sort the reported k-mers alphabetically before writing.
Output Format
To /root/output.txt:
AXXXXXXXXXX: count
One line per k-mer, sorted alphabetically.
- Write results directly to
/root/output.txtin exactKMER: countformat, one entry per line. - Enforce output constraints while writing: include only 3-mers with count > 0 and sort k-mers alphabetically.
- After writing, read
/root/output.txtback once to verify formatting and alphabetical order.
Using scikit-bio
from skbio import DNA
from skbio.sequence import KmerGenerator
# Generate and count kmers
K-mers
64 possible 3-mers: AAA, AAC, AAG, AAT, ACA, ..., TTG, TTT
Tips
- Use
skbio.DNAfor sequence handling. - Prefer
DNA(sequence).kmer_frequencies(k=3)overKmerGeneratoror a purely manual implementation. - Prefer one short script that reads
/root/input.txt, counts 3-mers, and writes/root/output.txt. - When writing output, iterate over
sorted(counts)and emit only observed/nonzero k-mers asKMER: count. - After running the script, confirm
/root/output.txtexists and sanity-check its contents against the input sequence.