Sequence Objects
The Seq class is Biopython's sequence object: a string-like container that knows its molecular type (DNA, RNA, protein) and can perform biology-aware operations - translation, transcription, reverse complement - that plain strings cannot.
Creating Sequences
from Bio.Seq import Seq
dna = Seq("ATGCGTACGT")
rna = Seq("AUGCGUACGU")
protein = Seq("MAVT")
print(len(dna)) # 10
print(dna[0:4]) # ATGC
print(dna + Seq("AA")) # ATGCGTACGTAA - concatenation
print(Seq("AT") * 3) # ATATAT
Molecular Type
Seq infers its type from the alphabet of characters: nucleotides (ACGTU) versus amino acids. The type drives which operations are valid:
print(dna.transcribe()) # DNA -> RNA: ATGCGTACGT -> AUGCGUACGU
print(dna.reverse_complement()) # ACGTACGCAT
print(dna.translate(table=1)) # protein: M R T ... (depending on frame)
Counting and Comparing
print(dna.count("GC")) # occurrences of substring GC
print(Seq("ATGC").count("A")) # 1
print(Seq("AAAA") == Seq("AAAA")) # True - value equality
print(Seq("ATGC")[:2].upper()) # AT
Iterating Bases
for base in dna:
print(base, end=" ")
# A T G C G T A C G T
print(list(dna[::3])) # every third base: a quick frame sampler
Seq vs SeqRecord
- Seq - just the sequence and its biology; the workhorse for operations.
- SeqRecord - Seq plus id, description, annotations, and features; what file parsers return and what you store per entity.
- When you load a file with SeqIO.parse you get SeqRecord objects; rec.seq is the Seq.
Summary
Seq is a biology-aware string: it supports normal string operations plus transcribe, translate, reverse_complement, and type-based behavior. Use Seq for the sequence itself and SeqRecord when you need identity, annotations, or features - which is what file formats give you.