This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Computing The Reverse And Complement Of A Sequence With Biopython

An example that computes the reverse complement of a sequence with BioPython

#
# Reverse complement example with BioPython
#

from Bio.Seq import Seq

# a separate function to reverse strings (or other iterables)
def rev(it):
    "Reverses an interable and returns it as a string"
    return ''.join(reversed(it))

# create a Seq class instance
dna = Seq("ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG")

# original DNA
print type(dna)
print dna

# reverse complement DNA, returns a new sequence
print dna.reverse_complement()

# currently there is no direct way to just reverse a sequence
# we need to do a little extra work

rseq = rev(str(dna))
rdna = Seq(rseq)

# reversed sequence
print rdna

# to complement DNA, returns a new sequence
print dna.complement()

Produces the following output:

<class 'Bio.Seq.Seq'>
ATGGCCATTGTAATGGGCCGCTGAAAGGGTGCCCGATAG
CTATCGGGCACCCTTTCAGCGGCCCATTACAATGGCCAT
GATAGCCCGTGGGAAAGTCGCCGGGTAATGTTACCGGTA
TACCGGTAACATTACCCGGCGACTTTCCCACGGGCTATC
biopython python sequence

Another tip: you can transform a Seq class instance into a string with the str() function.

This is obviously not a question. It should therefore be labeled as 'community wiki' :)

4 answers

The Bio.Seq module provides two easy ways to get the complement and reverse complement from a sequence:

  • If you have a string, use the functions complement(dna) and reverse_complement(dna)
  • If you have a Seq object, use its methods with the same names: dna.complement() and dna.reverse_complement

To reverse a sequence, there is a function in the Bio.SeqUtils module called reverse which does what you would expect.


(Sorry for going meta, but I don't have commenting privileges yet. This can be deleted if the original post is edited.)

According to Meta Stack Overflow, if you want to share the answer to a difficult question that's poorly documented elsewhere online, you should post the question as a genuine one, and then submit your own answer separately. In theory, someone else may have an answer that's better than yours, and this allows it to be voted to the top properly.

good observation will do that from now on

Don't use the Bio.SeqUtils.reverse() function, it is deprecated. You can just do seq[::-1] with either a string or a Seq object (see my answer below).

Wouldn't it better to have a single question titled 'How to compute the reverse complement with python' and put all the examples as different answers? Otherwise it seems a bit confusing..

good point but it seems like that would generate way too many small questions

If you want to reverse a string in Python, you can use a slice with a step of minus one -1,

rev_str = str[::-1]

It should not surprise you that you can do the same with a Biopython Seq object:

rev_seq = seq[::-1]

I guess the Biopython Tutorial you be more explicit but it does cover reversing a sequence like this.

In my opinion, for a very simple problem we need a very simple script. Why use biopython or complicated functions??

Try this:

http://basicbioinformatics.blogspot.com/2011/10/reverse-strand.html

Bye!!

Too simple - it doesn't handle IUPAC ambiguity codes or mixed case.

Log in to answer this question.