0
$\begingroup$

I'm trying to create a python program that follows the codon translation and turn mRNA sequence into amino acids (see the dictionary). The system has to return an error of the string is not a multiplier of 3, and the the function should not produce any more amino acids once a stop codon (keys with a value of X in the dictionary) is found, and should not display 'X' in the output. I do not know how to incorporate the stop codon to stop the programming entirely, as well as the multiplier of 3 aspect in the code. Can someone give me any advice? Thank you so much! example: protfromRNA('GCCAACGCACCCACUUUUUUUUAGAAA') should return ANAPTFF

def protfromRNA(Seq):
    GeneticCode = {'AAA':'K', 'AAC':'N', 'AAG':'K', 'AAU':'N',
               'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACU':'T',
               'AGA':'R', 'AGC':'S', 'AGG':'R', 'AGU':'S',
               'AUA':'I', 'AUC':'I', 'AUG':'M', 'AUU':'I',
               'CAA':'Q', 'CAC':'H', 'CAG':'Q', 'CAU':'H',
               'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCU':'P',
               'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGU':'R',
               'CUA':'L', 'CUC':'L', 'CUG':'L', 'CUU':'L',
               'GAA':'E', 'GAC':'D', 'GAG':'E', 'GAU':'D',
               'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCU':'A',
               'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGU':'G',
               'GUA':'V', 'GUC':'V', 'GUG':'V', 'GUU':'V',
               'UAA':'X', 'UAC':'Y', 'UAG':'X', 'UAU':'Y',
               'UCA':'S', 'UCC':'S', 'UCG':'S', 'UCU':'S',
               'UGA':'X', 'UGC':'C', 'UGG':'W', 'UGU':'C',
               'UUA':'L', 'UUC':'F', 'UUG':'L', 'UUU':'F'}
    return ''.join(GeneticCode[i] if GeneticCode[i]!=X else '' for i in Seq)
$\endgroup$

1 Answer 1

1
$\begingroup$

You need to iterate over the sequence 3 characters at a time and you need to stop when you hit the stop codon (or before for an invalid codon, e.g. of length 2). Here is a memory efficient implementation using iterators (requires python >= 3.8):

from itertools import islice

def protfromRNA(Seq):
    it = iter(Seq)
    lst = []
    while codon := "".join(islice(it, 3)):
        aa = GeneticCode.get(codon)
        if aa is None:
            raise ValueError(f"invalid codon: {codon}")
        if aa == "X":
            break
        lst.append(aa)
    return "".join(lst)

print(protfromRNA('GCCAACGCACCCACUUUUUUUUAGAAA'))
# ANAPTFF
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.