LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 35 lessons

Lesson 6 of 8

Module 1 · Python basics

Sequences with Biopython

Sequences with Biopython

Sequences with Biopython

Biopython reads the file formats biology uses and does the standard sequence operations for you. This lesson reads a FASTA file and checks your lesson 2 functions against it.

⏱ 10 min readModule 1 · Python basicsFree
Lesson at a glance3 steps · about 10 min
  1. The FASTA format
  2. Read it and measure each sequence
  3. Reverse complement and translation

Before you start. This lesson uses pandas and the gc_content function from Strings, lists and functions. If you open a new notebook, run this setup cell first.

Python · setup
import pandas as pd

def gc_content(seq):
    seq = seq.upper()
    return (seq.count("G") + seq.count("C")) / len(seq)
Files for this lessonSave each file under the name shown here, next to your notebook, or upload it to Colab (folder icon › Upload). Your browser may give the download a different name; rename it before you run the code.
practice_sequences.fastaFour short practice sequences (not real genes)

The FASTA format

FASTA is the simplest sequence format: a header line starting with >, then the sequence. The course file holds four short practice sequences, made up for this lesson.

A FASTA file can hold one sequence or millions, one after another; each new > starts a new record, meaning one header together with its sequence. By convention the first word of the header, up to the first space, is the sequence's identifier, and anything after it is a free-text description. Long sequences are usually wrapped over many lines of fixed width, often 60 or 80 letters, so a program must join the lines back together. The same format holds DNA, RNA or protein sequences; the letters tell you which.

>seq1 practice sequence, short coding region ATGGCTAGCAAAGGTGAAGAACTGTTTACCGGCTAA >seq2 practice sequence, AT-rich ATATTTAAATAAGCTTATAATTA …

Reading the file

These are the first two records of the file; the … marks where the other two continue in the same way. In the first header, seq1 is the identifier and practice sequence, short coding region the description. Each sequence here fits on one line.

Headers from public databases follow their own conventions within this free text. A protein downloaded from UniProt, for example, has a header that starts like >sp|P69905|HBA_HUMAN, with fields separated by vertical bars, so its identifier is that whole first word. Because the format is so loose, it is worth printing a few identifiers after reading any new file, to check that they are what you expect before you use them as keys in a table.

FASTA files carry several file endings, among them .fasta, .fa, .fna for nucleotides and .faa for amino acids. The ending is only a hint; the content is the same format. In Strings, lists and functions you stored sequences in a dictionary typed into the code. Reading them from a file is the step that lets the same code work on four sequences or four million.

Sequences with Biopython · LSMA free course The first record of the practice file. The header line starts with the greater-than sign; its first word, seq1, becomes record.id and the whole header after the sign becomes record.description. The sequence line of 36 letters becomes record.seq. 1 · ONE FASTA RECORD, AND WHAT BIOPYTHON MAKES OF IT >seq1 practice sequence, short coding region header line record.id seq1 · first word after > record.description the whole header after > ATGGCTAGCAAAGGTGAAGAACTGTTTACCGGCTAA sequence line(s) record.seq a Seq object · 36 letters
Biopython splits each record into named parts. The identifier is the first word of the header; the description keeps the whole header; the sequence becomes a Seq object. Schematic, not to scale.

The format takes its name from the FASTA sequence-comparison program of Pearson & Lipman (1988), "Improved tools for biological sequence comparison", PNAS 85, 2444–2448, whose input files used this simple layout. Biopython is described in Cock et al. (2009), "Biopython: freely available Python tools for computational molecular biology and bioinformatics", Bioinformatics 25(11), 1422–1423; its tutorial and documentation are at biopython.org. Biopython changes between releases: older tutorials use a function called GC(), which newer versions replaced with gc_fraction(), the one used here. Report the version you used (import Bio; print(Bio.__version__)). FASTA has no single official specification, so tools differ in small ways, such as which characters they accept.

Read it and measure each sequence

Python
from Bio import SeqIO
from Bio.SeqUtils import gc_fraction

for record in SeqIO.parse("practice_sequences.fasta", "fasta"):
    print(record.id, len(record.seq),
          round(gc_fraction(record.seq), 3),          # Biopython
          round(gc_content(str(record.seq)), 3))      # your function from "Strings, lists and functions"
seq1 36 0.444 0.444 seq2 23 0.087 0.087 seq3 21 0.952 0.952 seq4 27 0.481 0.481

What this does

  • Biopython is installed as biopython but imported as Bio. from Bio import SeqIO loads its module for reading and writing sequence files; from Bio.SeqUtils import gc_fraction loads one ready-made function. The from … import … form lets you write gc_fraction instead of the full dotted name.
  • SeqIO.parse(file name, "fasta") reads the file one record at a time. The second argument names the format, so the same function reads other formats when given another name.
  • Each record has the parts shown in the figure above: record.id and record.seq, among others.
  • gc_fraction(record.seq) is Biopython's GC calculation. gc_content(str(record.seq)) is yours from Strings, lists and functions; str() converts Biopython's Seq object into a plain string first, which is what your function was written for. The calculation is long, so the print() call continues over three lines inside its brackets.

Reading the output

Each line is the identifier, the length, Biopython's GC content and yours. As fractions: seq1 has 16 G or C out of 36 (0.444), seq2 has 2 out of 23 (0.087), seq3 has 20 out of 21 (0.952) and seq4 has 13 out of 27 (0.481). The last two columns are identical on every line.

Your function agrees with Biopython on every sequence. Checking your own code against a trusted library is a habit worth keeping.

The agreement is not guaranteed in general. These sequences contain only A, C, G and T. Real sequences can contain other letters, such as N for an unknown base, and two tools may handle them differently: one might count N in the length and another leave it out. A check like this is most useful when you also include an awkward case on purpose. Your own toolkit: modules, files and errors does exactly that.

Printing is fine for four records, but for analysis you usually want a table. A common pattern is to collect one small dictionary per record in a list, then turn the list into a DataFrame, as in NumPy, pandas and files.

Python
rows = []
for record in SeqIO.parse("practice_sequences.fasta", "fasta"):
    rows.append({"id": record.id, "length": len(record.seq),
                 "gc": gc_fraction(record.seq)})
seq_table = pd.DataFrame(rows)

Each dictionary becomes one row, and its keys become the column names, so seq_table has four rows and the columns id, length and gc. From there, everything from NumPy, pandas and files, Cleaning and combining data and Figures with matplotlib and seaborn applies: filtering, sorting and plotting.

One behaviour of SeqIO.parse surprises people. It does not load the file into memory; it hands out records one by one, which is why it can read files far larger than your computer's memory. The cost is that it can be used only once. If you store records = SeqIO.parse(…) and loop over it twice, the second loop finds nothing and runs silently. Call SeqIO.parse again, or keep the records in a list with list(SeqIO.parse(…)) if the file is small. To look sequences up by identifier, SeqIO.to_dict(SeqIO.parse(…)) builds a dictionary keyed by record.id.

Reverse complement and translation

Translation is the step in which a ribosome reads an RNA message three letters at a time and joins one amino acid for each group of three. Biopython does the same lookup on the DNA letters, using the standard genetic code unless you ask for another.

Python
first = next(SeqIO.parse("practice_sequences.fasta", "fasta"))
print(first.description)
print(first.seq.reverse_complement())
print(first.seq.translate())
print(first.seq.translate(to_stop=True))
seq1 practice sequence, short coding region TTAGCCGGTAAACAGTTCTTCACCTTTGCTAGCCAT MASKGEELFTG* MASKGEELFTG

What this does

  • SeqIO.parse hands out records one at a time; next(…) takes only the first, without reading the rest of the file. That record is stored as first.
  • first.description is the whole header line without the >.
  • first.seq.reverse_complement() is Biopython's version of the function you wrote in Strings, lists and functions: it reverses the sequence and swaps each base for its partner.
  • first.seq.translate() turns the DNA into a protein sequence, writing each amino acid as a one-letter code.
  • translate(to_stop=True) stops at the first stop codon and leaves the stop out.

Reading the output

The first line is the description. The second is the reverse complement, 36 letters, which starts TTA: the reverse complement of the final stop codon TAA. It ends CAT, the reverse complement of the start codon ATG. The third line is the protein, eleven amino acids followed by * for the stop. The fourth line is the same protein without the *. When you only want the protein itself, for example to search a protein database, this is the version to use.

If a sequence's length is not a multiple of three, translate() still works but gives a warning about a partial codon, and the leftover one or two letters are ignored. Take the warning seriously: it often means the sequence was cut in the wrong place, or that you are reading in the wrong frame.

To read a different frame, slice before translating: first.seq[1:].translate() starts at the second letter and first.seq[2:].translate() at the third. The other strand has three frames of its own, reached with first.seq.reverse_complement().translate() and the same slices, so a DNA sequence has six possible reading frames in all. Some organisms and organelles use a slightly different genetic code; vertebrate mitochondria, for example, read TGA as tryptophan rather than stop. Biopython follows the numbered NCBI tables, so translate(table=2) uses the vertebrate mitochondrial code.

Worked example · reading the translation

ATG GCT AGC AAA GGT GAA GAA CTG TTT ACC GGC TAA
 M   A   S   K   G   E   E   L   F   T   G   *

Each three-letter codon codes for one amino acid. ATG (methionine, M) starts the protein; TAA is a stop codon, shown as *. to_stop=True ends the translation there.

Step by step. Split the 36 letters into groups of three from the first letter: 36 ÷ 3 = 12 codons. Look each one up in the genetic code: ATG is methionine (M), GCT alanine (A), AGC serine (S), AAA lysine (K), GGT glycine (G), GAA glutamic acid (E), GAA again (E), CTG leucine (L), TTT phenylalanine (F), ACC threonine (T), GGC glycine (G) and TAA stop. Eleven codons give amino acids; the twelfth ends the protein.

What the answer means. The genetic code has 64 codons: 61 name one of 20 amino acids and three, TAA, TAG and TGA, mean stop. Because there are more codons than amino acids, several codons can name the same one; here GGT and GGC both give glycine. Where you start reading matters: starting one letter later would group the letters differently and give an entirely different protein. Each of the three possible starting points is called a reading frame, and you will search all three in the Module 1 project.

Go deeper: not every genome uses the standard code

A few genomes read some codons differently from the standard code. NCBI gives each variant a number, and Biopython's translate() takes that number with table=. In the vertebrate mitochondrial code, table 2, TGA means tryptophan instead of stop, while AGA and AGG become stops. So, after from Bio.Seq import Seq, Seq("ATGTGA").translate() gives M*, but Seq("ATGTGA").translate(table=2) gives MW. Translating human mitochondrial genes with the default table would cut their proteins short at every TGA.

Bacteria use table 11, which gives the same amino acids as the standard code but allows more start codons, such as GTG. A second pitfall: if a sequence's length is not a multiple of three, translate() warns about a partial codon. Trimming first, with s[:len(s) // 3 * 3], keeps only whole codons. This matters when you translate the second or third reading frame, which starts one or two letters in.

Key point

Biopython also reads GenBank, FASTQ and many other formats, and can query NCBI databases. When a task involves sequences, check whether Biopython already does it before writing your own.

Take FASTQ, the format sequencing machines produce. Each read takes four lines: a header starting with @, the sequence, a separator line starting with +, and a line of quality scores, one character per base, saying how confident the machine was in each letter. Writing a correct reader for that by hand takes care; with Biopython, it is the same SeqIO.parse call with "fastq" as the format name, and each record then carries its quality scores as well. GenBank files, in turn, add annotation: where each gene starts and ends, and what it codes for.

Further reading · Module 1The Python Tutorial: the official tutorial, for anything you want explained a second way. · All reading for this course

Check your understanding

1. What does a FASTA header line start with?

2. What does * mean in a translated protein?

3. How many amino acids does a 36-base coding sequence (including its stop codon) produce?

Up next

Lesson 7

Your own toolkit: modules, files and errors

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.