Exercise: Counting k-mers with a HashMap
Goal
Write kmer_counts(seq: &[u8], k: usize) -> HashMap<Vec<u8>, usize>: return a map from every distinct k-mer in seq to the number of times it appears.
This combines three things that are new today: the standard library’s HashMap, the slice windows iterator, and the Entry API for “increment-or-insert”.
Background
K-mer counting is a foundational operation in bioinformatics. Spectrum-based assembly (de Bruijn graphs), classifier features, sequence sketches, error correction, contamination screening — they all start with a k-mer histogram. Doing this well requires the right data structure: a hash map keyed by the k-mer itself.
On day 2 you wrote kmers(seq, k) -> Vec<&[u8]> which produced the k-mers as slice views. That is great if you want to iterate k-mers without copying, but it does not group duplicates — and a hash map cannot key on a borrowed &[u8] whose lifetime is tied to the input sequence (the entries need to outlive the map, which would outlive the sequence). So we promote each k-mer to a Vec<u8> (an owned copy) when we insert it.
ATGATGCAT. Seven windows of length 3 slide across the sequence; the resulting six distinct k-mers are shown sorted alphabetically, with ATG standing out at count 2 because it occurs at positions 0 and 3.
The exercise
cd day3/ex-kmer-countsMake all five tests pass.
How to run
cargo test
cargo run -- ATGATG 3
# Expected output (order may differ — HashMaps do not preserve insertion order):
# ATG: 2
# TGA: 1
# GAT: 1Hints
HashMap is in std::collections
The HashMap type is not in the prelude — you have to bring it into scope:
use std::collections::HashMap;Then construct an empty one with HashMap::new:
let mut counts: HashMap<Vec<u8>, usize> = HashMap::new();<[T]>::windows does the sliding loop for free
<[T]>::windows(k) returns an iterator over every contiguous window of length k — exactly the loop you wrote by hand on day 2, but as one method call:
for window in seq.windows(k) {
// window: &[u8] of length k
}Edge cases are already handled: if k > seq.len() the iterator simply yields nothing. (If k == 0 it panics; check for that explicitly.)
Entry API for increment-or-insert
You want: “if this key is already in the map, increment its count; otherwise, insert it with count 1”. Doing this with two lookups (contains_key then get_mut) would work but is wasteful. The Entry API does it in one lookup:
*counts.entry(window.to_vec()).or_insert(0) += 1;Read that line as: get the entry for this key, insert 0 if it isn’t there, dereference the resulting &mut usize, and add 1. It is the idiomatic Rust pattern for histogram building.
<[T]>::to_vec copies the slice into a new Vec. This is the one allocation per insertion; for a k-mer counter on a real genome you would normally key on packed integers instead (each base in 2 bits), but for today the simple Vec<u8> keys are fine.
Solution
use std::collections::HashMap;
fn kmer_counts(seq: &[u8], k: usize) -> HashMap<Vec<u8>, usize> {
let mut counts: HashMap<Vec<u8>, usize> = HashMap::new();
if k == 0 {
return counts;
}
for window in seq.windows(k) {
*counts.entry(window.to_vec()).or_insert(0) += 1;
}
counts
}Full reference in day3/ex-kmer-counts-solution/src/main.rs.
Where this leads
- Day 4: stream a whole
.fa.gzwithnoodles-fastaand accumulate k-mer counts across millions of records. - Day 5: parallelise the same computation with
rayon— each chunk of records contributes its own localHashMap, then we merge them at the end. - For real production k-mer counting, look at crates like
needletail(FASTA/FASTQ) and the literature on minimisers and 2-bit packing. Outside the scope of this course.