What changes do I need to make in this python code to convert DNA sequences to protein?

158 views Asked by At

I have to first find codon "ATG" then once it finds the codon it should translate using the dictionary. I am currently getting no output. I am still inexperienced in python so have trouble writing code with proper syntax

def translate(line, table):
    protein = ""
    for i in range (0, len(line), 3):
        codon = line[i:i+3]
        protein += table[codon]
        print(protein)
    return protein

def find_start(sequence):
    for line in sequence:
        if line == "ATG":
            translate(line)
            return 

table = { 
        'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M', 
        'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T', 
        'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K', 
        'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',                  
        'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L', 
        'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P', 
        'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q', 
        'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R', 
        'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V', 
        'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A', 
        'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E', 
        'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G', 
        'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S', 
        'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L', 
        'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_', 
        'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W', 
    } 


sequence = "ggatcatagtcttgttgcattgtaggtgtttctttacagtatccttcttaatgaatcatgattatgttctaagtagaccagatcgattcttacgactacaatattttcttttgagccatcatagattttttttagtttcaaaccagccttgcattgtgttctcctcgatgatgtgttacggattctaggattactagctaacctttctgtatttctaccctccattgaacaatttaac"

sequence = sequence.upper()


find_start(sequence)
2

There are 2 answers

0
Mudasir Habib On

update your find start function as

def find_start(sequence):
    # index = sequence.find('ATG')    # if yow want start index of codon
    if ('ATG' in sequence):
        # do some thing
0
David_Zizu On

I am still not 100% what you are trying to achieve but hope it helps

for i in range(sequence.find("ATG"), len(sequence) - 2):
    possible_codon = sequence[i:i + 3]
    if possible_codon not in table:
        continue
    print(f"key: {possible_codon}, val: {table[possible_codon]}")