Entering edit mode
3.3 years ago
ThulasiS
▴
90
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
print translate_DNA(seq)
will print the output, or you canprint protein
inside the function.Besides your code can be improved on many levels and may have many more issues look at this part:
It should be
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:
Because you are returning something you need to 'capture' the return value like:
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