No out put from python function
0
0
Entering edit mode
8 weeks ago
ThulasiS ▴ 70

I am trying to define a function like translating DNA to protein

I am not getting the output but it is printing header. No error info as well. What might be the reason? I am trying to stop the program if stop codon found.

Here is the code:

import sys
dna_file = sys.argv[1]
f = open(dna_file, "r")
seq = f.read()

seq = seq.replace("\n","") #new lines
seq = seq.replace("\r", "") #carriages or hidden characters
seq = seq.upper()

def translate_DNA(seq):
codon_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',
}


start_codon = seq.find('ATG')
start_codon = seq.find('GTG')
protein = ''
i = start_codon
while i < len(seq)-2:
    codon = seq[i:i+3]
    aa = codon_table[codon]
    if aa == '*':
        break
        print "Stop Codon Found!!!"
    i = i+3
    protein = protein + aa
return protein

header = ''
seq = ''
for line in open(dna_file):
if line[0] == '>':
    if header != '':
        print header
        translate_DNA(seq)

    header = line.strip()
    seq = ''
else:
    seq =seq + line.strip()

print header
translate_DNA(seq)

Thank you

Sincerely

Python • 294 views
ADD COMMENT
1
Entering edit mode

print translate_DNA(seq) will print the output, or you can print protein inside the function.

ADD REPLY
1
Entering edit mode

Besides your code can be improved on many levels and may have many more issues look at this part:

    if aa == '*':
        break
        print "Stop Codon Found!!!"

It should be

    if aa == '*':    
        print "Stop Codon Found!!!"
        break

I did not looked very detailed at the code, maybe I can do that a later time. Also take a look at the biopython package.

EDIT:

I read now that you also want to stop the program. Change your code to:

while i < len(seq)-2:
    codon = seq[i:i+3]
    aa = codon_table[codon]
    protein = protein + aa
    if aa == '*':
        print "Stop Codon Found!!!"
        return protein
    i = i+3

Because you are returning something you need to 'capture' the return value like:

protein = translate_DNA(seq)
ADD REPLY
0
Entering edit mode

Thank You!!.. I changed that print part but it in the output we are getting "Stop Codon Found" but with earlier it is showing fine. I just missed the print option here. Others looking fine

ADD REPLY

Login before adding your answer.

Traffic: 2421 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6