Computing The Reverse And Complement Of A Sequence With Biopython
4
0
Entering edit mode
14.8 years ago
Biostar User ★ 1.0k

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 • 27k views
ADD COMMENT
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
6
Entering edit mode
14.8 years ago
Eric T. ★ 2.8k

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.

ADD COMMENT
1
Entering edit mode

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).

ADD REPLY
0
Entering edit mode

good observation will do that from now on

ADD REPLY
4
Entering edit mode
14.8 years ago

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..

ADD COMMENT
1
Entering edit mode

I concur with giovanni.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
14.8 years ago
Peter 6.0k

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.

ADD COMMENT
0
Entering edit mode
13.1 years ago
User 4133 ▴ 150

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!!

ADD COMMENT
3
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 2592 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6