使用函数解释外部txt文件

时间:2019-04-30 09:54:11

标签: python function

使用已建立的函数,我需要调用它来解释外部txt文件并将氨基酸代码输出到控制台中

translate函数解释输入的DNA。

txtTranslate应该可以解释txt文件的DNA。

  

外部文件包含:   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)

我希望它输出解释后的代码,但它什么也没提供。

2 个答案:

答案 0 :(得分:1)

您的外部文本文件:

ACATTTGCTTCTGACACAACTGTGTTCACTAGCAACCTCAAACAGACACCATGGTGCATCTGACTCCTGA
GGAGAAGTCTGCCGTTACTGCCCTGTGGGGCAAGGTGAACGTGGATGAAGTTGGTGGTGAGGCCCTGGGC
AGGCTGCTGGTGGTCTACCCTTGGACCCAGAGGTTCTTTGAGTCCTTTGGGGATCTGTCCACTCCTGATG
CTGTTATGGGCAACCCTAAGGTGAAGGCTCATGGCAAGAAAGTGCTCGGTGCCTTTAGTGATGGCCTGGC

这是代码,看上去更漂亮,更干净:

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_)

它应该这样输出:

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 :(得分:1)

翻译DNA的首选方法是使用翻译表,即密码子:氨基酸对的字典。

这里是一个例子:

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)

输出:

'XFXXXXXVFXXXLXXXXXXIXLLXXXLXXLXXXXXXXXMXLVVXXXXXXXXXXLXXXXXLXXLXIXXLLMLXXXXIXXXLMXXXXXVXXVMXXX'