Before you start
- Finish the earlier Python lessons first, ideally Python: Level 2 (Intermediate).
- Any term new? The Glossary has it.
Learning objectives
By the end of this lesson you will be able to: fetch a real gene from NCBI; calculate base composition and GC content; translate a gene into its protein; and produce a chart in Python.
This is where it gets real. You'll pull an actual gene sequence from NCBI - the same database working scientists use every day - and analyze it with the Python you just learned. The gene is human insulin (accession NM_000207): the molecule that lets your body use sugar, and the first medicine made with recombinant DNA technology (approved in 1982).
โถ Code along
Use a Jupyter notebook in your bionexus environment, or open a free Colab. Run each block as you go.
1Fetch a real gene from NCBI
Biopython's Entrez module talks to NCBI for you. (NCBI asks you to say who you are with an email address. Use your real one, so NCBI can contact you if something goes wrong; please don't make one up.) In Colab, run !pip install biopython first.
from Bio import Entrez, SeqIO Entrez.email = "your.email@example.com" # use your own email # Fetch the human insulin mRNA as a FASTA record handle = Entrez.efetch(db="nucleotide", id="NM_000207", rettype="fasta", retmode="text") record = SeqIO.read(handle, "fasta") handle.close() seq = str(record.seq) print(record.description) print("Length:", len(seq), "bases")
ncbi.nlm.nih.gov/nuccore/NM_000207 (Send to โ File โ FASTA), then load it with record = SeqIO.read("NM_000207.fasta", "fasta").2Base composition
Use the loop pattern from the basics lesson to count each base:
tally = {}
for base in seq:
tally[base] = tally.get(base, 0) + 1
print(tally)
for base, n in tally.items():
print(base, n, round(n/len(seq)*100, 1), "%")
3GC content - reuse your function
def gc_content(s): return round((s.count("G") + s.count("C")) / len(s) * 100, 1) print("GC content:", gc_content(seq), "%")
Why GC content is interesting
A G-C pair is held by three hydrogen bonds and an A-T pair by two, and GC-rich DNA is generally more stable. GC content varies a lot between genes and between genomes. Comparing GC content across genes and organisms is a real, everyday bioinformatics task.
4From gene to protein
The whole point of insulin's gene is the protein it builds. But an mRNA record starts with a stretch that is not translated (the 5' untranslated region), so first find the start codon, ATG, then let Biopython translate from there:
from Bio.Seq import Seq start = seq.find("ATG") # first start codon print("Start codon at position", start) cds = seq[start:] cds = cds[:len(cds) // 3 * 3] # keep whole codons only protein = Seq(cds).translate(to_stop=True) print("Protein length:", len(protein), "amino acids") print("First 20:", protein[:20])
You should see 110 amino acids starting MALWMRLLPLL.... That is preproinsulin, the protein the ribosome actually makes; the cell then trims it into the 51-amino-acid insulin hormone. You just decoded a gene.
Why not translate the whole record?
If you translate from base 1, Biopython reads the untranslated region in the wrong frame and gives you nonsense. For insulin the first ATG is the real start, but that is not true for every gene. The reliable way is to use the coding region (CDS) that NCBI annotates in the GenBank record.
5Visualize it
import matplotlib.pyplot as plt bases = ["A", "C", "G", "T"] heights = [tally.get(b, 0) for b in bases] plt.bar(bases, heights, color=["#5b9cf8","#a855f7","#10b981","#f59e0b"]) plt.title("Base composition of human insulin (NM_000207)") plt.ylabel("Count") plt.show()
That bar chart is a small but complete piece of analysis - and it's yours. You fetched real data, processed it, and visualized a result.
๐ Make it your own
The best way to cement a skill is to push past the tutorial. Try one (or all) of these:
- Fetch a different gene - try
NM_000518(beta-globin, the blood gene behind sickle-cell). Compare its GC content to insulin. - Write a function that returns the reverse complement of a sequence (hint: Biopython's
Seq(seq).reverse_complement()). - Count the most common codon in the sequence using a loop over
seq[i:i+3]. - Portfolio move: save your notebook to GitHub with a short README. That's your first public bioinformatics project.
What you just did
Step back and notice the shape of what happened: you connected to a real scientific database, retrieved real data, applied your own code to it, and produced a result and a figure. That loop - get data โ analyze โ visualize - is the core of nearly every bioinformatics project, no matter how advanced. You now own the whole pattern.
I got a "Partial codon" warning when translating. Is that bad?
Not for this project. It means the piece you translated was not a whole number of codons, because the end of the mRNA (the 3' untranslated region) is included. The line cds[:len(cds) // 3 * 3] trims the leftover bases so the warning goes away, and to_stop=True stops at the first stop codon anyway.
Check your understanding
Sources & further reading
- Cock PJA, Antao T, Chang JT, et al. Biopython: freely available Python tools for computational molecular biology and bioinformatics. Bioinformatics 25: 1422-1423, 2009. doi:10.1093/bioinformatics/btp163
- Biopython Tutorial and Cookbook, chapter "Accessing NCBI's Entrez databases". biopython.org
- NCBI Nucleotide record NM_000207 (Homo sapiens insulin, INS, mRNA). ncbi.nlm.nih.gov
- Perkel JM. Programming: Pick up Python. Nature 518: 125-126, 2015. doi:10.1038/518125a
Last reviewed: October 2026.
