5

I have this equation for reverse complementing DNA in python:

def complement(s): 
    basecomplement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'} 
    letters = list(s) 
    letters = [basecomplement[base] for base in letters] 
    return ''.join(letters)
def revcom(s):
    complement(s[::-1])
print("ACGTAAA")
print(complement("ACGTAAA"[::-1]))
print(revcom("ACGTAAA"))

however the lines:

print(complement("ACGTAAA"[::-1]))
print(revcom("ACGTAAA"))

do not equal one another. only the top line gives an answer. the bottom just prints "NONE"

any help why this is?

Christian Ternus
  • 8,406
  • 24
  • 39
Michael Ridley
  • 53
  • 1
  • 1
  • 8
  • 3
    I bet it prints `None`, not `"NONE"`. That's actually an important distinction. Remember, when asking for help with debugging, it is important to report the error *exactly* as it occurs. – SethMMorton Oct 24 '13 at 17:24

5 Answers5

7

You forgot the return statement in revcom. Try this:

def revcom(s):
    return complement(s[::-1])

If you don't explicitly return a value from a function in Python, then the function returns None.

mdml
  • 22,442
  • 8
  • 58
  • 66
6

Python3, including whole IUPAC alphabet for nucleotides:

def revcomp(seq):
    return seq.translate(str.maketrans('ACGTacgtRYMKrymkVBHDvbhd', 'TGCAtgcaYRKMyrkmBVDHbvdh'))[::-1]

In Python2:

from string import maketrans

def revcomp(seq):
    return seq.translate(maketrans('ACGTacgtRYMKrymkVBHDvbhd', 'TGCAtgcaYRKMyrkmBVDHbvdh'))[::-1]
corinna
  • 629
  • 7
  • 18
4

A pretty one liner to reverse complement a DNA sequence:

revcompl = lambda x: ''.join([{'A':'T','C':'G','G':'C','T':'A'}[B] for B in x][::-1])
print revcompl("AGTCAGCAT")
xApple
  • 6,150
  • 9
  • 48
  • 49
1

You need to return the result from revcom().

Christian Ternus
  • 8,406
  • 24
  • 39
0
`a = "AAAACCCGGT"
a= a[::-1]
print("reverse: "+a)
a = a.replace('A','a')
a = a.replace('T','A')
a = a.replace('a','T')
a = a.replace('C','c')
a = a.replace('G','C')
a = a.replace('c','G')
print("complement : "+a)`
Shahadat Hossain
  • 533
  • 7
  • 20