The Bio.Seq module in Biopython provides a few sequence related classes, the Seq object and the MutableSeq object, plus some general purpose sequence functions. In addition to this wiki page, there is a whole chapter in the Tutorial (PDF) on this topic.
In Biopython, sequences are usually held as Seq objects, which hold the sequence string and an associated alphabet.
If you need to store additional information like a sequence identifer or name, or even more details like a description or annotation, then we use a SeqRecord object instead. These are the sequence records used by the SeqIO module for reading and writing sequence files.
The Seq Object
The Seq object essentially combines a Python string with an (optional) biological alphabet. For example:
>>> from Bio.Seq import Seq >>> my_seq = Seq("AGTACACTGGT") >>> my_seq Seq('AGTACACTGGT', Alphabet()) >>> my_seq.alphabet Alphabet()
In the above example, we haven't specified an alphabet so we end up with a default generic alphabet. Biopython doesn't know if this is a nucleotide sequence or a protein rich in alanines, glycines, cysteines and threonines. If you know, you should supply this information:
>>> from Bio.Seq import Seq >>> from Bio.Alphabet import generic_dna, generic_protein >>> my_seq = Seq("AGTACACTGGT") >>> my_seq Seq('AGTACACTGGT', Alphabet()) >>> my_dna = Seq("AGTACACTGGT", generic_dna) >>> my_dna Seq('AGTACACTGGT', DNAAlphabet()) >>> my_protein = Seq("AGTACACTGGT", generic_protein) >>> my_protein Seq('AGTACACTGGT', ProteinAlphabet())
Why is this important? Well it can catch some errors for you - you wouldn't want to accidentally try and combine a DNA sequence with a protein would you:
>>> my_protein + my_dna Traceback (most recent call last): ... TypeError: Incompatable alphabets ProteinAlphabet() and DNAAlphabet()
Biopython will also catch things like trying to use nucleotide only methods like translation (see below) on a protein sequence.
If you have a nucleotide sequence (or a sequence with a generic alphabet) you may want to do things like take the reverse complement, or do a translation.
Complement and reverse complement
These are very simple - the methods return a new Seq object with the appropriate sequence and the same alphabet:
>>> my_dna Seq('AGTACACTGGT', DNAAlphabet()) >>> my_dna.complement() Seq('TCATGTGACCA', DNAAlphabet()) >>> my_dna.reverse_complement() Seq('ACCAGTGTACT', DNAAlphabet())
Transcription and back transcription
If you have a DNA sequence, you may want to turn it into RNA. In bioinformatics we normally assume the DNA is the coding strand (not the template strand) so this is a simple matter of replacing all the thymines with uracil:
>>> my_dna Seq('AGTACACTGGT', DNAAlphabet()) >>> my_dna.transcribe() Seq('AGUACACUGGU', RNAAlphabet())
Naturally, given some RNA, you might want the associated DNA - and again Biopython does a simple U/T substitution:
>>> my_rna = my_dna.transcribe() >>> my_rna Seq('AGUACACUGGU', RNAAlphabet()) >>> my_rna.back_transcribe() Seq('AGTACACTGGT', DNAAlphabet())
If you actually do want the template strand, you'd have to do a reverse complement on top:
>>> my_rna Seq('AGUACACUGGU', RNAAlphabet()) >>> my_rna.back_transcribe().reverse_complement() Seq('ACCAGTGTACT', DNAAlphabet())
Using nucleotide methods on a protein
None of this operations apply to a protein sequence and trying this will raise an exception:
>>> my_protein.complement() Traceback (most recent call last): ... ValueError: Proteins do not have complements!
You can use them on Seq objects with a generic alphabet:
>>> my_seq.complement() Seq('AGTACACTGGT', Alphabet()) >>> my_seq.complement() Seq('TCATGTGACCA', Alphabet())