Using an established function I need to call it to interpret an external txt file and output the amino acid codes into the console

translate function interprets input DNA.

txtTranslate is supposed to interpret txt file DNA.

External file contains: ACATTTGCTTCTGACACAACTGTGTTCACTAGCAACCTCAAACAGACACCATGGTGCATCTGACTCCTGA GGAGAAGTCTGCCGTTACTGCCCTGTGGGGCAAGGTGAACGTGGATGAAGTTGGTGGTGAGGCCCTGGGC AGGCTGCTGGTGGTCTACCCTTGGACCCAGAGGTTCTTTGAGTCCTTTGGGGATCTGTCCACTCCTGATG CTGTTATGGGCAACCCTAAGGTGAAGGCTCATGGCAAGAAAGTGCTCGGTGCCTTTAGTGATGGCCTGGC TCACCTGGACAACCTCAAGGGCACCTTTGCCACACTGAGTGAGCTGCACTGTGACAAGCTGCACGTGGAT CCTGAGAACTTCAGGCTCCTGGGCAACGTGCTGGTCTGTGTGCTGGCCCATCACTTTGGCAAAGAATTCA CCCCACCAGTGCAGGCTGCCTATCAGAAAGTGGTGGCTGGTGTGGCTAATGCCCTGGCCCACAAGTATCA CTAAGCTCGCTTTCTTGCTGTCCAATTTCTATTAAAGGTTCCTTTGTTCCCTAAGTCCAACTACTAAACT GGGGGATATTATGAAGGGCCTTGAGCATCTGGATTCTGCCTAATAAAAAACATTTATTTTCATTGC

dna_ = input("Enter the DNA sequence to translate: ")
def translate():
  translate()
for i in range(0,len(dna_),3):
    dna = dna_[i:i+3]
    if dna == "ATA" or dna == "ATC" or dna == "ATT":
      print ("I")
    elif dna == "CTA" or dna == "CTC" or dna == "CTG" or dna == "CTT" or dna == "TAA" or dna =="TTG":
      print ("L")
    elif dna == "GTA" or dna == "GTC" or dna == "GTG" or dna == "GTT":
      print ("V")
    elif dna == "TTC" or dna == "TTT":
      print ("F")
    elif dna == "ATG":
      print ("M")
    else:
      print ("X")

def txtTranslate():
  translate()
  with open("normalDNA.txt", "r") as f:
    normalDNAoutput = f.readlines(translate)
    print (f.read())
  return (normalDNAoutput)

I would like it to output the interpreted code but it gives nothing.

2 Answers

1
Quang Nguyen Van On Best Solutions

Your external text file:

ACATTTGCTTCTGACACAACTGTGTTCACTAGCAACCTCAAACAGACACCATGGTGCATCTGACTCCTGA
GGAGAAGTCTGCCGTTACTGCCCTGTGGGGCAAGGTGAACGTGGATGAAGTTGGTGGTGAGGCCCTGGGC
AGGCTGCTGGTGGTCTACCCTTGGACCCAGAGGTTCTTTGAGTCCTTTGGGGATCTGTCCACTCCTGATG
CTGTTATGGGCAACCCTAAGGTGAAGGCTCATGGCAAGAAAGTGCTCGGTGCCTTTAGTGATGGCCTGGC

Here is the code, looks prettier and clean:

def translate(dna_):
    codes = []
    for i in range(0,len(dna_),3):
        dna = dna_[i:i+3]
        if dna == "ATA" or dna == "ATC" or dna == "ATT":
            codes.append("I")
        elif dna == "CTA" or dna == "CTC" or dna == "CTG" or dna == "CTT" or dna == "TAA" or dna =="TTG":
            codes.append('L')

        elif dna == "GTA" or dna == "GTC" or dna == "GTG" or dna == "GTT":
            codes.append('V')
        elif dna == "TTC" or dna == "TTT":
            codes.append('F')
        elif dna == "ATG":
            codes.append('M')
        else:
            codes.append('X')

    print('-'.join(codes))

with open("normalDNA.txt", "r") as f:
    dna_seqs = f.readlines(translate)

for dna_ in dna_seqs:
    translate(dna_)

It should output like this:

X-F-X-X-X-X-X-V-F-X-X-X-L-X-X-X-X-X-X-I-X-L-L-X
X-X-V-X-X-X-X-X-V-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X
X-L-L-V-V-X-X-X-X-X-X-F-F-X-X-F-X-X-L-X-X-X-X-X
L-X-X-X-X-L-X-X-X-L-M-X-X-X-X-X-V-X-X-V-M-X-X-X
1
alec_djinn On

The preferred way to translate DNA is by using a translation table, i.e. a dictionary of codon:aminoacid pairs.

Here is one example:

def translate(seq, table):
    result=''
    for i in range(0,len(seq),3):
        codon = seq[i:i+3].upper()
        if codon in table:
            result += table[codon]
        else:
            result += 'X'
    return result

s = 'ACATTTGCTTCTGACACAACTGTGTTCACTAGCAACCTCAAACAGACACCATGGTGCATCTGACTCCTGAGGAGAAGTCTGCCGTTACTGCCCTGTGGGGCAAGGTGAACGTGGATGAAGTTGGTGGTGAGGCCCTGGGCAGGCTGCTGGTGGTCTACCCTTGGACCCAGAGGTTCTTTGAGTCCTTTGGGGATCTGTCCACTCCTGATGCTGTTATGGGCAACCCTAAGGTGAAGGCTCATGGCAAGAAAGTGCTCGGTGCCTTTAGTGATGGCCTGGC'
table = {"ATA":"I", "ATC":"I", "ATT":"I", "CTA":"I",
         "CTC":"L", "CTG":"L", "CTT":"L", "TAA":"L", "TTG":"L",
         "GTA":"V", "GTC":"V", "GTG":"V", "GTT":"V",
         "TTC":"F", "TTT":"F",
         "ATG":"M"}

translate(s,table)

Output:

'XFXXXXXVFXXXLXXXXXXIXLLXXXLXXLXXXXXXXXMXLVVXXXXXXXXXXLXXXXXLXXLXIXXLLMLXXXXIXXXLMXXXXXVXXVMXXX'