Exercise: Reverse complement

Goal

Write reverse_complement(seq: &[u8]) -> Vec<u8>: given a DNA sequence as a byte slice, return a newly-allocated Vec<u8> containing the complement of each base in reverse order.

This is the first exercise where the function owns the data it returns. The caller is given a fresh Vec<u8> they can mutate, store, or drop as they wish — the function keeps nothing.

Background

The reverse complement is one of the most common operations in bioinformatics. To compute it, complement each base (A↔︎T, C↔︎G) and reverse the order. Aliasing on day 1: ATGCATGC → complemented TACGTACG → reversed GCATGCAT. We will reuse the complement_base function from day 1.

Three rows of five monospace boxes. Row 1: A T T G C (input). A left-pointing arrow labelled 'reverse' connects to row 2: C G T T A. A down arrow labelled 'complement (A↔T, C↔G)' connects to row 3: G C A A T, highlighted in light yellow with an orange border to mark the final output.
Figure 1: Reverse complement of ATTGC: reverse the order, then complement each base. Doing it in the opposite order — complement first, then reverse — produces the same result, because the two operations commute.

The exercise

cd day2/ex-reverse-complement

Make all seven tests pass.

How to run

cargo test
cargo run -- ATGCATGC    # expected: GCATGCAT
cargo run -- AAAA        # expected: TTTT
cargo run -- ACGT        # expected: ACGT  (this DNA sequence is a palindrome)

Hints

Vec::with_capacity(n) creates an empty vector that has already reserved space for n items. Compared to Vec::new(), this avoids the vector having to grow (and reallocate) as you push into it:

let mut out: Vec<u8> = Vec::with_capacity(seq.len());

You know exactly how big the result will be — same length as the input — so allocate exactly that much.

A range like 0..seq.len() walks indices forward. To walk them backward, call .rev() on it:

for i in (0..seq.len()).rev() {
    // i goes from seq.len()-1 down to 0
}

On day 3 we will write the same loop more compactly with seq.iter().rev().map(...).collect(), but the indexed loop is fine for today and easier to read while learning.

Vec::push appends a single element:

out.push(complement_base(seq[i]));

When the loop ends, the bare name out (no ;) is your return value.

Solution

fn reverse_complement(seq: &[u8]) -> Vec<u8> {
    let mut out: Vec<u8> = Vec::with_capacity(seq.len());
    for i in (0..seq.len()).rev() {
        out.push(complement_base(seq[i]));
    }
    out
}

Full reference in day2/ex-reverse-complement-solution/src/main.rs.

Where this leads

  • Next exercise (02): rewrite this so the caller passes in a buffer to fill, instead of allocating a fresh Vec on every call. That is how every fast bioinformatics tool actually does it in its inner loop.
  • Day 3: rewrite the body as one iterator chain — seq.iter().rev().map(|&b| complement_base(b)).collect().
  • Day 12 exercise 4: rewrite this as a wgpu compute shader — same complement table, now applied by thousands of GPU threads in parallel. Side-by-side with this CPU version, the GPU wins on multi-megabyte inputs once the data is already on the device.