Lesson 12 of 14 · 11 min
Transcription and Translation
The central dogma of molecular biology describes how information flows inside a cell: DNA is copied into messenger RNA (mRNA) in a step called transcription, and that mRNA is then read three letters at a time to build a protein in a step called translation. In this lesson you will run each of these steps in code using Biopython, a free Python library built for biology. If you have not installed it yet, run pip install biopython in your terminal once. Biopython is imported under the short name Bio, so every import we write begins with the word Bio.
Creating a Seq object
from Bio.Seq import Seq
dna = Seq("ATGGCCTAA")
print(dna)
print(type(dna))ATGGCCTAA
<class 'Bio.Seq.Seq'>
The first line, from Bio.Seq import Seq, reaches into Biopython and pulls out the Seq tool so we can use it by name. A Seq (short for sequence) is a special kind of text that knows it represents biological letters, so it comes with built-in methods for biology that an ordinary Python string does not have. We build one by passing a string of DNA letters to Seq(...) and storing the result in a variable called dna. Printing dna shows the letters back to us, and printing type(dna) confirms Python is treating it as a Bio.Seq.Seq object rather than a plain string. A method is simply a function that belongs to an object; you call it by writing the object, then a dot, then the method name followed by parentheses. Now that we have a Seq object, we can call four of its most useful methods.
print(dna.transcribe())
print(dna.translate())
print(dna.complement())
print(dna.reverse_complement())AUGGCCUAA
MA*
TACCGGATT
TTAGGCCAT
Each line calls one method on our dna object. transcribe() performs transcription, copying DNA into mRNA; the only change is that every T (thymine) becomes a U (uracil), so ATGGCCTAA becomes AUGGCCUAA. translate() performs translation, reading the sequence in groups of three letters called codons and converting each codon into one amino acid (or, for a stop codon, an end-of-protein signal) using the standard codon table: ATG becomes M (methionine), GCC becomes A (alanine), and TAA is a stop codon, which Biopython writes as an asterisk, giving the protein MA*. The asterisk marks where the ribosome would stop building the protein. complement() uses the base-pairing rule that A pairs with T and G pairs with C, swapping every letter for its partner in place to give TACCGGATT. reverse_complement() does that same partner swap and then reads the result backwards, which is how the opposite strand of the double helix actually runs, giving TTAGGCCAT.
codon_table = {
"TTT": "F", "TTC": "F", "TTA": "L", "TTG": "L",
"CTT": "L", "CTC": "L", "CTA": "L", "CTG": "L",
"ATT": "I", "ATC": "I", "ATA": "I", "ATG": "M",
"GTT": "V", "GTC": "V", "GTA": "V", "GTG": "V",
"TCT": "S", "TCC": "S", "TCA": "S", "TCG": "S",
"CCT": "P", "CCC": "P", "CCA": "P", "CCG": "P",
"ACT": "T", "ACC": "T", "ACA": "T", "ACG": "T",
"GCT": "A", "GCC": "A", "GCA": "A", "GCG": "A",
"TAT": "Y", "TAC": "Y", "TAA": "*", "TAG": "*",
"CAT": "H", "CAC": "H", "CAA": "Q", "CAG": "Q",
"AAT": "N", "AAC": "N", "AAA": "K", "AAG": "K",
"GAT": "D", "GAC": "D", "GAA": "E", "GAG": "E",
"TGT": "C", "TGC": "C", "TGA": "*", "TGG": "W",
"CGT": "R", "CGC": "R", "CGA": "R", "CGG": "R",
"AGT": "S", "AGC": "S", "AGA": "R", "AGG": "R",
"GGT": "G", "GGC": "G", "GGA": "G", "GGG": "G",
}
sequence = "ATGGCCTAA"
manual_protein = ""
for i in range(0, len(sequence), 3):
codon = sequence[i:i + 3]
manual_protein += codon_table[codon]
print(manual_protein)
print(str(dna.translate()))
print(manual_protein == str(dna.translate()))MA*
MA*
True
Biopython gave us MA*, but how do we know that is correct? Here we translate the same sequence by hand and confirm the two answers match. The dictionary codon_table maps all 64 possible codons to their amino acid letters, with the three stop codons TAA, TAG, and TGA mapped to an asterisk. The loop walks through the DNA three letters at a time: range(0, len(sequence), 3) produces the starting positions 0, 3, and 6, and sequence[i:i + 3] slices out one codon at each position. We look each codon up in the dictionary and glue the amino acid letters onto manual_protein. Comparing our by-hand result to Biopython with == prints True, which proves that both approaches produce the identical protein MA*.
Try it yourself: Create a new Seq object from the DNA string "ATGTTTGGATAG" and, in order, print its transcribe(), translate(), complement(), and reverse_complement() results. Before you run translate(), predict the protein by hand: split the sequence into the codons ATG, TTT, GGA, and TAG, look each one up, and check that your prediction matches what Biopython returns. Hint: TAG is another stop codon, so it should appear as an asterisk at the end.