Exercise: A codon table with Option

Goal

Write translate_codon(codon: [u8; 3]) -> Option<u8>: given three DNA bases, return the corresponding amino acid as a single ASCII letter, or None if the codon is unrecognised (typically because it contains an N or other ambiguity character).

Stop codons (TAA, TAG, TGA) return Some(b'*') — they are recognised, they just mean “end translation here”.

Background

The standard genetic code maps each of the 64 possible DNA triplets to one of 20 amino acids or a stop. Translation is the inner loop of every protein-coding-gene analysis. A robust translator returns:

  • Some(amino_acid) for the 61 sense codons
  • Some(b'*') for the 3 stop codons
  • None for everything else (anything containing N, IUPAC ambiguity codes, lower-case characters mixed with garbage, etc.)

Option<T> is Rust’s “this might be there or not” type. Unlike null in many other languages, the compiler forces every caller to handle both cases — there is no way to accidentally use a None as if it were a value.

A 4 by 4 grid of outer cells indexed by first base (rows T, C, A, G) and second base (columns T, C, A, G). Each outer cell contains four sub-rows for the third base T, C, A, G. Every sub-row shows a codon on the left and its one-letter amino acid on the right. Cells are background-coloured: cream for hydrophobic amino acids, light blue for polar uncharged, light pink for acidic, light green for basic, and dark grey with white text for stop codons. The codon ATG is annotated as the start codon. A legend at the bottom names the colour categories.
Figure 1: The standard genetic code: every cell is indexed by its first base (row band), second base (column band), and third base (sub-row). Background colour groups amino acids by chemistry — hydrophobic, polar uncharged, acidic, basic, or stop. ATG is the universal start codon.

The exercise

cd day3/ex-codon-table

Implement the full standard codon table. Make all nine tests pass.

How to run

cargo test
cargo run -- ATG    # M
cargo run -- TAA    # * (stop)
cargo run -- ATN    # None  (contains an N)
cargo run -- atg    # M     (case-insensitive)

Hints

The standard codon table is given in upper case. Convert each byte with u8::to_ascii_uppercase at the start so the match itself only handles upper case:

let upper = [
    codon[0].to_ascii_uppercase(),
    codon[1].to_ascii_uppercase(),
    codon[2].to_ascii_uppercase(),
];

Byte string literals b"ATG" have type &[u8; 3]. Inside match patterns, they match an array of bytes — so you can write:

match upper {
    b"ATG" => Some(b'M'),
    // ...
    _ => None,
}

Wait — upper has type [u8; 3] and the pattern b"ATG" is &[u8; 3]. Won’t the types mismatch? In modern Rust they do not: match patterns auto-dereference. (If you ever get a type error here, you can always write match &upper { b"ATG" => ... } to be explicit.) See the Rust reference on match ergonomics.

Most amino acids correspond to several codons (this is the genetic code’s redundancy). Use | alternatives to group them on a single arm:

b"TTA" | b"TTG" | b"CTT" | b"CTC" | b"CTA" | b"CTG" => Some(b'L'),  // Leucine
b"TAA" | b"TAG" | b"TGA" => Some(b'*'),                              // Stop

The catch-all _ => None at the end handles every codon containing an N (or other non-ACGT character) — anything we did not list explicitly.

The 20 standard amino acids (each followed by its codon set):

AA Codons
F TTT TTC
L TTA TTG CTT CTC CTA CTG
I ATT ATC ATA
M ATG
V GTT GTC GTA GTG
S TCT TCC TCA TCG AGT AGC
P CCT CCC CCA CCG
T ACT ACC ACA ACG
A GCT GCC GCA GCG
Y TAT TAC
* TAA TAG TGA
H CAT CAC
Q CAA CAG
N AAT AAC
K AAA AAG
D GAT GAC
E GAA GAG
C TGT TGC
W TGG
R CGT CGC CGA CGG AGA AGG
G GGT GGC GGA GGG

That is 64 codons total — full coverage of every plain ACGT triplet.

Solution

The full match is long but mechanical:

fn translate_codon(codon: [u8; 3]) -> Option<u8> {
    let upper = [
        codon[0].to_ascii_uppercase(),
        codon[1].to_ascii_uppercase(),
        codon[2].to_ascii_uppercase(),
    ];
    match &upper {
        b"TTT" | b"TTC" => Some(b'F'),
        b"TTA" | b"TTG" | b"CTT" | b"CTC" | b"CTA" | b"CTG" => Some(b'L'),
        b"ATT" | b"ATC" | b"ATA" => Some(b'I'),
        b"ATG" => Some(b'M'),
        b"GTT" | b"GTC" | b"GTA" | b"GTG" => Some(b'V'),
        b"TCT" | b"TCC" | b"TCA" | b"TCG" | b"AGT" | b"AGC" => Some(b'S'),
        b"CCT" | b"CCC" | b"CCA" | b"CCG" => Some(b'P'),
        b"ACT" | b"ACC" | b"ACA" | b"ACG" => Some(b'T'),
        b"GCT" | b"GCC" | b"GCA" | b"GCG" => Some(b'A'),
        b"TAT" | b"TAC" => Some(b'Y'),
        b"TAA" | b"TAG" | b"TGA" => Some(b'*'),
        b"CAT" | b"CAC" => Some(b'H'),
        b"CAA" | b"CAG" => Some(b'Q'),
        b"AAT" | b"AAC" => Some(b'N'),
        b"AAA" | b"AAG" => Some(b'K'),
        b"GAT" | b"GAC" => Some(b'D'),
        b"GAA" | b"GAG" => Some(b'E'),
        b"TGT" | b"TGC" => Some(b'C'),
        b"TGG" => Some(b'W'),
        b"CGT" | b"CGC" | b"CGA" | b"CGG" | b"AGA" | b"AGG" => Some(b'R'),
        b"GGT" | b"GGC" | b"GGA" | b"GGG" => Some(b'G'),
        _ => None,
    }
}

Full reference in day3/ex-codon-table-solution/src/main.rs.

Where this leads

  • Day 4: translate every record of a FASTA file using this function, returning the protein sequence (truncated at the first stop codon).
  • A nice property to think about: stops are encoded as a regular Some(b'*') value rather than as None. This separates “unknown codon” (None) from “end of translation” (Some(b'*')) — two different semantics that the type system now keeps straight for you.