Transcription and translation: DNA to protein, as a pipeline
Life's central process — DNA to RNA to protein — is really a data pipeline. Each stage transforms the sequence, and you can build the whole thing in a few lines of Python.
The big idea
Transcription copies DNA into RNA; translation reads RNA codons into a chain of amino acids, stopping at a stop codon.
See it in code
Stage one is transcription, and it's a single string operation: copy the DNA into RNA by swapping every T for a U:
dna = "ATGGCTTAA"
rna = dna.replace("T", "U")
print("DNA:", dna)
print("RNA:", rna)DNA: ATGGCTTAA RNA: AUGGCUUAA
.replace("T", "U") is the whole of transcription. The RNA it produces is what the next stage reads.
Stage two is translation. Walk that RNA in steps of three, and look up each codon in the genetic-code dictionary to get its amino acid:
rna = "AUGGCUUAA"
table = {"AUG": "Met", "GCU": "Ala", "UAA": "Stop"}
for i in range(0, len(rna), 3):
codon = rna[i:i+3]
print(codon, "->", table[codon])AUG -> Met GCU -> Ala UAA -> Stop
Each triplet becomes an amino acid — until UAA, which reads Stop. Next we chain both stages and let that stop actually end the protein.
Now chain both stages into one flow. Transcribe (swap T for U), then translate: walk the RNA in codons, look up each amino acid, and stop the moment we hit a stop codon:
dna = "ATGGCTTAA"
rna = dna.replace("T", "U")
table = {"AUG": "Met", "GCU": "Ala", "UAA": "Stop"}
protein = []
for i in range(0, len(rna), 3):
amino = table[rna[i:i+3]]
if amino == "Stop":
break
protein.append(amino)
print("RNA:", rna)
print("Protein:", protein)RNA: AUGGCUUAA Protein: ['Met', 'Ala']
The break at the stop codon is the cell's period at the end of a sentence — translation halts, and UAA never becomes an amino acid. Chaining transcribe then translate is exactly the 'central dogma' of biology, expressed as code.
A pipeline — output of one stage feeds the next — is everywhere in computing: parse then compile then run, extract then transform then load, read then filter then summarize. Modeling biology this way makes each step testable and swappable on its own.
Try it yourself
Extend the DNA with more codons before the stop and watch the protein grow. Then wrap the whole thing in a function translate(dna) that returns the protein list, so you can run it on any strand.
The common mistake
Forgetting to stop. Without the stop-codon break, translation runs off the end of meaningful data (or crashes on an unknown codon). Biology has explicit start and stop signals for exactly this reason — your code needs to honor them too.
What it unlocks
The pipeline combines codons, the genetic code, and proteins, driven by for loops and conditionals.