Exercise: Count A, C, G, T, and other
Goal
Write a function
fn base_counts(seq: &[u8]) -> (usize, usize, usize, usize, usize)that returns (a, c, g, t, other) — the number of A, C, G, T, and “anything else” bases in a DNA sequence, treating upper and lower case as the same letter. “Other” counts every byte that is not A/C/G/T: N, ambiguity codes like R/Y/S, gap characters, and any garbage.
Background
Per-base composition is the first thing every QC tool reports (FastQC, seqkit, NanoStat, …). You are building a tiny version. By the end of the week your tool will handle gigabyte FASTQ files in seconds; for now we feed it a string on the command line.
A note on tuples
This is the first exercise that returns more than one value. Rust has tuples for bundling several values of (possibly different) types into one return value:
let (a, c, g, t, other) = base_counts(seq);The pattern (a, c, g, t, other) on the left is destructuring — it pulls the five fields apart into separate local variables in one step.
On day 3 we will replace the bare tuple with a proper BaseCounts struct so the fields have names. For now, the position-based tuple is enough.
The exercise
cd day1/ex-base-countsMake all six tests pass without changing the signature, main, or the tests.
How to run
cargo test
cargo run -- ACGTacgtNNXExpected output for ACGTacgtNNX:
A: 2
C: 2
G: 2
T: 2
other: 3
Hints
You can combine alternatives in a single match arm with |:
match b {
b'A' | b'a' => a += 1,
// ...
_ => other += 1,
}_ is the wildcard pattern — it matches anything you have not already handled. Use it for the “other” arm.
At the end of the function, write the tuple as the last expression (no ;):
(a, c, g, t, other)That is the function’s return value.
Solution
fn base_counts(seq: &[u8]) -> (usize, usize, usize, usize, usize) {
let mut a: usize = 0;
let mut c: usize = 0;
let mut g: usize = 0;
let mut t: usize = 0;
let mut other: usize = 0;
for &b in seq {
match b {
b'A' | b'a' => a += 1,
b'C' | b'c' => c += 1,
b'G' | b'g' => g += 1,
b'T' | b't' => t += 1,
_ => other += 1,
}
}
(a, c, g, t, other)
}Full reference implementation in day1/ex-base-counts-solution/src/main.rs.
Where this leads
- Day 3: replace the five-tuple return with a named
BaseCountsstruct, rewrite the loop as an iteratorfold, and decide formally how to treat IUPAC ambiguity codes (R, Y, S, W, K, M, …). - Day 4: apply this to every record of a real
.fastq.gzstreamed throughnoodles-fastq.