Question: Computing The Reverse And Complement Of A Sequence With Biopython
gravatar for Question Bot
10.6 years ago by
Question Bot90
Question Bot90 wrote:

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

# 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'>
sequence biopython python • 1.7k views
ADD COMMENTlink modified 10.6 years ago by Wubin Qu0 • written 10.6 years ago by Question Bot90
gravatar for Giovanni M Dall'Olio
10.6 years ago by
Barcelona, Spain
Giovanni M Dall'Olio420 wrote:

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 COMMENTlink written 10.6 years ago by Giovanni M Dall'Olio420
gravatar for Etal
10.6 years ago by
Athens, GA
Etal70 wrote:

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 COMMENTlink written 10.6 years ago by Etal70
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 16.09
Traffic: 1 users visited in the last hour