Python for Bioinformatics: Getting Started with Biopython
Reading sequence files, computing GC content, and querying NCBI - the Biopython basics that cover most day-to-day scripting.
Biopython is the standard Python library for working with biological data - reading sequence files, running alignments, and querying public databases without hand-rolling parsers for every file format.
Install
pip install biopythonReading a FASTA file
from Bio import SeqIO
for record in SeqIO.parse("genes.fasta", "fasta"):
print(record.id, len(record.seq))SeqIO.parse handles the format-specific parsing (FASTA, FASTQ, GenBank, and more all share the same
interface), returning SeqRecord objects with the id, description, and sequence.
Basic sequence operations
from Bio.Seq import Seq
seq = Seq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG")
print(seq.complement()) # TACCGGTAACATTACCCGGCGACTTTCCCACGGGCTATC
print(seq.reverse_complement())
print(seq.translate()) # translates codons to amino acidsComputing GC content
GC content (the percentage of G and C bases) is a common quick-look statistic - it affects everything from PCR primer design to sequencing bias.
from Bio.SeqUtils import gc_fraction
for record in SeqIO.parse("genes.fasta", "fasta"):
gc = gc_fraction(record.seq) * 100
print(f"{record.id}: {gc:.1f}% GC")Querying NCBI directly
Biopython's Entrez module lets you pull sequences straight from NCBI without leaving Python -
useful for quickly grabbing a reference sequence by accession number.
from Bio import Entrez, SeqIO
Entrez.email = "you@example.com" # NCBI requires this
handle = Entrez.efetch(db="nucleotide", id="NM_000546", rettype="fasta", retmode="text")
record = SeqIO.read(handle, "fasta")
print(record.description)Where this fits into a real pipeline
In practice, Biopython is rarely the whole pipeline - it's the glue: parsing intermediate files, reformatting outputs between tools, pulling reference sequences, or writing small custom filters that a general-purpose tool doesn't quite cover. Heavier lifting (alignment, variant calling) usually goes to dedicated command-line tools like BWA or GATK, called from a script or workflow manager.
Bioinformatics with Biopython - Full Course