pboennig
pboennig

Reputation: 13

Function and if/elif statement always returns first if statement

Ok, so I'm trying to create a program that takes three DNA bases and gives you the complimentary amino acid. I can effectively transcribe DNA to complimentary RNA, but my translation function isn't working.

Here is my code: 
def basematch(base):
if base == "A":
    return "U"
elif base == "T":
    return "A"
elif base == "G":
    return "C"
elif base == "C":
    return "G"
else:
    return "invalid"




rawdna = raw_input("Enter a raw DNA sequence: ")
comp = list(rawdna)
length = len(comp)

n = 0


codonlist = []


while n + 1 <= length:
    final = comp[n]
    finalbase = basematch(final)
    codonlist.append(finalbase)
    n = n + 1


rawRNA = "".join(codonlist)

def translate(codon):
    if codon == "GCU" or "GCC" or "GCA" or "GCG":
       print 'Ala'
    elif codon == "UUU" or "UUC":
       print "Phe"
    elif codon == "UUA" or "UUG":
       print "Leu" 
    elif codon == "UGU" or "UGC": 
       print "Cys"
    elif codon == "UGA" or "UAA" or "UAG": 
       print "Stop"
    elif codon == "UGG" or "UAU" or "UAC": 
       print "Tyr"
    elif codon == "UCU" or "UCC" or "UCA" or "UCG":
       print "Ser"
    elif codon == "CUU" or "CUC" or "CUA" or "CUG":
       print "Leu"
    elif codon == "CCU" or "CCC" or "CCA" or "CCG":
       print "Pro"
    elif codon == "CAU" or "CAC":
       print "His"
    elif codon == "CAA" or "CAG":
       print "Gln"
    elif codon == "CGU" or "CGC" or "CGA" or "CGG":
       print "Arg"

translate(rawRNA) 

Whenever I translate rawRNA, it alway returns 'Ala'.

Sorry for the long code. Thanks for any help.

Upvotes: 0

Views: 163

Answers (4)

wwii
wwii

Reputation: 23753

Another approach would be to make translate and basematch dictionaries:

basematch = {'A' : 'U', 'T' : 'A', 'G' : 'C', 'C' : 'G'}

try:
    print basematch['A']
    print basematch['foo']
except KeyError:
    print 'invalid'

translate = {"GCU" : 'Ala', "GCC" : 'Ala', "GCA" : 'Ala', "GCG" : 'Ala',
             "UUU" : 'Phe', "UUC" : 'Phe', "UUA" : 'Leu', "UUG" : 'Leu',
             "UGU" : 'Cys', "UGC" : 'Cys',
             "UGA" : 'Stop', "UAA" : 'Stop', "UAG" : 'Stop',
             "UGG" : 'Tyr', "UAU" : 'Tyr', "UAC" : 'Tyr',
             "UCU" : 'Ser', "UCC" : 'Ser', "UCA" : 'Ser', "UCG" : 'Ser',
             "CUU" : 'Leu', "CUC" : 'Leu',"CUA" : 'Leu',"CUG" : 'Leu',
             "CCU" : 'Pro', "CCC" : 'Pro', "CCA" : 'Pro', "CCG" : 'Pro',
             "CAU" : 'His', "CAC" : 'His', "CAA" : 'Gln', "CAG" : 'Gln',
             "CGU" : 'Arg', "CGC" : 'Arg', "CGA" : 'Arg', "CGG" : 'Arg'}

print translate["UUU"]

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881293

You have your answer re the correct form of the if condition:

if codon in ["GCU", "GCC", "GCA", "GCG"]:

although you could shorten some of the cases, that one particularly (assuming the input sequences were valid of course):

if codon[:2] == "GC":

But I'd just like to point out that, if you're using Python 3.1 or above, there's a much easier way to do the base translations:

>>> import string
>>> s = 'GATTACA'
>>> s.translate(s.maketrans('ATGC','UACG'))
'CUAAUGU'

Any real cells doing your (sequence to list, individual base translate appending to new list, new list to sequence with join) would have evolution quickly selecting against them :-)

Upvotes: 0

Leigh
Leigh

Reputation: 12506

The line isn't checking it properly.

if codon == "GCU" or "GCC" or "GCA" or "GCG":

The or doesn't extend to the ==; it's going to check if "GCC":, which is always True, and so it ends there.

Upvotes: 3

kojiro
kojiro

Reputation: 77089

You're misunderstanding how 'or' works here.

if codon == "GCU" or "GCC" or "GCA" or "GCG":

means

if (codon == "GCU") or "GCC" or "GCA" or "GCG":

It's always True because "GCC" is a non-empty string.

What you probably want is:

if codon in ["GCU", "GCC", "GCA", "GCG"]:

Upvotes: 9

Related Questions