Python for Biologists

Lesson 10 of 14 · 10 min

Getting Started with Biopython

Biopython is a free, community-built library for biology. A library is a collection of ready-made code that other people wrote and shared, so you can use their work instead of writing everything yourself. In this lesson you will install Biopython, load a short piece of DNA into it, and watch it handle common biology tasks for you.

Installing Biopython

bash
pip install biopython
Collecting biopython
  Downloading biopython-1.85-cp312-cp312-macosx_10_13_universal2.whl (2.8 MB)
Requirement already satisfied: numpy in ./venv/lib/python3.12/site-packages (from biopython) (2.2.1)
Installing collected packages: biopython
Successfully installed biopython-1.85

The command pip install biopython uses pip, Python's package installer, to download Biopython onto your computer. You only run it once. To use the library in a program, you write an import statement, which loads code from a library so your program can reach it. Bio.Seq is the part of Biopython that deals with sequences, and Seq is a special kind of object it provides for holding a DNA, RNA, or protein sequence. An object is a bundle of data (here, the letters) plus the actions you can run on it.

python
from Bio.Seq import Seq

dna = Seq("ATGGCCATTGTAATGGGCCGC")

print(dna)            # show the letters, just like plain text
print(len(dna))       # len() counts the bases
print(dna[0:3])       # a slice: bases at positions 0, 1, 2
print(dna.count("G")) # how many times G appears
ATGGCCATTGTAATGGGCCGC
21
ATG
7

Biology-aware methods

python
print(dna.complement())         # pair each base with its partner
print(dna.reverse_complement()) # the other strand: complemented and reversed
print(dna.transcribe())         # DNA to messenger RNA (every T becomes U)
print(dna.translate())          # read codons into a protein
TACCGGTAACATTACCCGGCG
GCGGCCCATTACAATGGCCAT
AUGGCCAUUGUAAUGGGCCGC
MAIVMGR

A Seq behaves much like an ordinary text string, so len(), slicing, and count() all work on it. What sets it apart are the biology-aware methods: complement flips each base to its partner, reverse_complement gives the opposite strand (because DNA's two strands run in opposite directions, it both swaps each base for its partner and reverses the order), transcribe turns DNA into RNA, and translate reads the letters three at a time (each group of three is a codon) into amino acids, which is why the 21-base sequence becomes the 7-letter protein MAIVMGR. You could hand-write this logic, but it is easy to get the base-pairing rules or the genetic code slightly wrong. Biopython has been tested by thousands of scientists and gives one clear method per task, so reaching for a purpose-built library beats rolling your own.

Try it yourself: Create a new Seq from the DNA string TTTAAACCCGGG. Print its length, its reverse_complement(), and its translate() result. Which amino acids do you get, and why does a 12-base sequence turn into a protein of only 4 letters?