Online Biopython Compiler

Run Biopython code in your browser. Reverse-complement and translate DNA, parse FASTA and score pairwise alignments.

Python
from Bio import Align
aligner = Align.PairwiseAligner()
seq1 = "ATTCCTAAGT"
seq2 = "TCGT"
alignments = aligner.align(seq1, seq2)
print(alignments[0])
target            0 ATTCCTAAGT 10
                  0 -|-|----|| 10
query             0 -T-C----GT  4

Biopython is a set of Python tools for biological data. It handles DNA, RNA and protein sequences, reads and writes file formats such as FASTA, FASTQ and GenBank, aligns sequences and queries NCBI's databases. It suits small scripted jobs like checking a sequence file or translating a gene. This page is an online Biopython compiler: the code runs in your browser, so you can try it without installing anything. Run the example first, then paste any snippet below into a new cell to try it.

What the example does

Align.PairwiseAligner() creates an aligner with default settings: global mode, which aligns both sequences end to end, a match score of 1, a mismatch score of 0 and free gaps. aligner.align(seq1, seq2) returns the best-scoring alignments. Here four of them tie at a score of 4, one point per matched base, and print(alignments[0]) shows the first. The target, seq1, sits above the query, seq2. A | marks an identical base, - a gap and . a mismatch. The numbers are start and end positions.

Work with DNA sequences

A Seq behaves like a Python string with biology methods added. translate() uses the standard genetic code and writes a stop codon as *:

from Bio.Seq import Seq
from Bio.SeqUtils import gc_fraction

dna = Seq("ATGGCCATTGTAATGGGCCGCTGA")
print("Reverse complement:", dna.reverse_complement())
print("mRNA:", dna.transcribe())
print("Protein:", dna.translate())
print("Protein to first stop:", dna.translate(to_stop=True))
print("GC fraction:", round(gc_fraction(dna), 3))
print("Count of G:", dna.count("G"), "| ATT at index", dna.find("ATT"))

Like a string, a Seq cannot be changed in place: use MutableSeq to edit bases. translate(table=2) switches to the vertebrate mitochondrial code.

Read FASTA records

SeqIO.parse() reads a file or any file-like object, so io.StringIO works for text pasted into a cell. Each record has an id, a description and a seq:

import io
from Bio import SeqIO
from Bio.SeqUtils import gc_fraction

fasta = """>gene1 example coding sequence
ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG
>gene2 another example
ATGAAACGCATTAGCACCACCATTACCACCACCATCACCATTACCACAGGTAACGGTGCGGGCTGA
"""

records = list(SeqIO.parse(io.StringIO(fasta), "fasta"))
for record in records:
    print(record.id, len(record), round(gc_fraction(record.seq), 2), record.description)

count = SeqIO.write(records, "genes.fasta", "fasta")
print("Wrote", count, "records to genes.fasta")

To work on your own file, click Mount folder in the sidebar's Data tab (Chrome or Edge on a computer), choose the folder it is in, and pass its name to SeqIO.parse(). For a file with exactly one record, SeqIO.read() returns that record directly.

Change the alignment scores

These settings give 2 points for a match, take 1 for a mismatch, and charge 2 to open a gap plus 0.5 for each further position in it:

from Bio import Align

aligner = Align.PairwiseAligner()
aligner.match_score = 2
aligner.mismatch_score = -1
aligner.open_gap_score = -2
aligner.extend_gap_score = -0.5

alignments = aligner.align("ATTCCTAAGT", "TCGT")
print("Score:", alignments.score, "| best alignments:", len(alignments))
print(alignments[0])
print(alignments[0].counts())

With gaps penalized, the example's four-way tie becomes one best alignment, with its gaps in two runs. counts() reports identities, mismatches and gaps.

Align protein sequences

Protein alignments score each pair of amino acids with a substitution matrix such as BLOSUM62. Local mode finds the best-matching region instead of aligning the full length:

from Bio import Align
from Bio.Align import substitution_matrices

aligner = Align.PairwiseAligner()
aligner.substitution_matrix = substitution_matrices.load("BLOSUM62")
aligner.open_gap_score = -10
aligner.extend_gap_score = -0.5
aligner.mode = "local"

alignment = aligner.align("HEAGAWGHEE", "PAWHEAE")[0]
print("Score:", alignment.score)
print(alignment)

substitution_matrices.load() with no argument lists the available matrices.

Good to know

  • Bio.pairwise2 is deprecated: importing it shows a BiopythonDeprecationWarning. Use PairwiseAligner instead.
  • from Bio.SeqUtils import GC fails in Biopython 1.84, the version on this page. Use gc_fraction(), which returns a fraction such as 0.54, not a percentage.
  • Bio.Entrez and Bio.Blast send requests to NCBI's servers rather than running locally. Set Entrez.email first, or Biopython warns that NCBI requires an address.