I want to create a function that reads DNA symbols from a txt file and converts it to relevant amino acids.

I have created a function that takes in user input and converts the input into the relevant amino acids. Now I want the function to take the inputs from a txt file inside my directory and convert it into the amino acids.

# 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()

Instead of the user manually typing each letter, I want the program to read an external txt file and run the program inside a new function called txtTranslate which is based on the earlier translate() function.

0 Answers