如何编辑python函数的某些部分

时间:2019-05-16 14:13:49

标签: python

我想创建一个从txt文件读取DNA符号并将其转换为相关氨基酸的函数。

我创建了一个函数,该函数接收用户输入并将输入转换为相关的氨基酸。现在,我希望该函数从目录中的txt文件获取输入并将其转换为氨基酸。

# this is a program to simulate the effects of the simulate the effects of the Single Nucleotide Polymorphism that leads to this genetic disease
# we create a function called translate that, when given a DNA sequence of arbitrary length, the program identifies and returns the amino acid sequence of the DNA using the amino acid SLC code found in that table.

# we create a variable called DNA to store the user's input for their dna sequence
# we also create a variable called codons to convert the entered DNA sequence into a list of 3 characters per element
# furthermore, we create an if structure which matches each codon to its appropriate amino acid

def translate(dna_file):
    dna = input("please enter the DNA sequence in CAPS: ")
    codons = [dna[start:start+3] for start in range (0,len(dna),3)]
    if "ATA" in codons or "ATC" in codons or "ATT" in codons:
        print("Isoleucine")
    if "CTT" in codons or "CTC" in codons or "CTA" in codons or "CTG" in codons or "TTA" in codons or "TTG" in codons:
        print("Leucine")
    if "GTT" in codons or "GTC" in codons or "GTA" in codons or "GTG" in codons:
        print("Valine")
    if "TTT" in codons or "TTC" in codons:
        print("Phenylalanine")
    if "ATG" in codons:
        print("Methionine")
        return

# we now create a new function called mutate which must read the contents of the file DNA.txt
# the function must identify the first occurence of the lowercase letter "a"

# we first open the file in read mode, and then remove hidden characters "\n" and "\r" from the file
file = open("DNA.txt", "r") 

sequence = file.read() 
sequence = sequence.replace("\n", "")  
sequence = sequence.replace("\r", "")

file.close()

# we now create a new txt file called normal.txt which changes the lowercase a into caps
# we first create a variable that contains the contents of DNA.txt in all uppercase
dna_upper = sequence.upper()

# we now write to the txt file inside file object "n_file" with the n standing for normalDNA
n_file = open("normalDNA.txt", "w")
n_file.write(dna_upper)

# closing the file
n_file.close()

# we now create a txt file called mutatedDNA.txt which changes the lowercase a into a capital T
# we first create a variable that replaces the "a" DNA.txt to "T"
dna_T = sequence.replace("a", "T")

# we now write to the txt file inside file object "m_file" with the m standing for mutatedDNA 
m_file = open("mutatedDNA.txt", "w")
m_file.write(dna_T)

# closing the file
m_file.close()

# creating the txtTranslate function that calls the translate function above
def txtTranslate():
        translate()

我希望程序无需用户手动键入每个字母,而是读取外部txt文件并在名为txtTranslate的新函数(基于早期translate()函数的新函数)中运行该程序。 / p>

0 个答案:

没有答案