Why Can't I Convert Dna to mRNA in Python?

Why Can't I Convert Dna to mRNA in Python?

Code:

n = 3

DNA-Sequence = { #dictionary of DNA
    "Phenylalanine": ["UUU", "UUC"],
    "Leucine": ["UUA", "CUU", "CUC", "CUA", "CUG", "UUG"],
    "Isoleucine": ["AUU", "AUC", "AUA"],
    "Methionine": "AUG",
    "Valine": ["GUU", "GUC", "GUA", "GUG"],
    "Serine": ["UCU", "UCC", "UCA", "UCG"],
    "Proline": ["CCU", "CCC", "CCA", "CCG"],
    "Threonine": ["ACU", "ACC", "ACA", "ACG"],
    "Alanine": ["GCU", "GCC", "GCA", "GCG"],
    "Tyrosine": ["UAU", "UAC"],
    "Histidine": ["CAU", "CAC"],
    "Glutamine": ["CAA", "CAG"],
    "Asparagine": ["AAU", "AAC"],
    "Lysine": ["AAA", "AAG"],
    "Asparatic Acid": ["GAU", "GAC"],
    "Glutamic Acid": ["GAA", "GAG"],
    "Cysteine": ["UGU", "UGC"],
    "Trytophan": "UGG",
    "Arginine": ["CGU", "CGC", "CGA", "CGG", "AGG", "AGA"],
    "Serine": ["AGU", "AGC"],
    "Glycine": ["GGU", "GGC", "GGA", "GGG"]
}

lookup_dict = {k: key for key, values in DNA-Sequence.items() for k in values} #this is used to find the values in the dictionary using the inputDNA
inputDNA = input("Enter your DNA sequence: ")
inputDNA = inputDNA.upper()
print("Your DNA sequence is", inputDNA)
str(inputDNA)
RNA = inputDNA.replace('C', 'G') #this is me trying to convert DNA sequence to RNA
RNA = RNA.replace('A', "U") #this is me trying to convert DNA sequence to RNA
RNA = RNA.replace('T', 'A') #this is me trying to convert DNA sequence to RNA
print(RNA)

b = len(inputDNA)

if b % 3 == 0: #if the length of inputDNA is a multiple of 3
  for k in (inputDNA[i:i + n] for i in range(0, len(inputDNA), n)):
    for _, values in DNA-Sequence.items():
      if k in values:
        print(lookup_dict[k], end=" ")
        break
    else: #if the length of inputDNA is not a multiple of 3
      print("I hate u")

What happened:

Enter your DNA sequence: CCATAGCACGTT
Your DNA sequence is: CCATAGCACGTT
GGUAUGGUGGAA
Proline I hate u
Histidine I hate u

What I want to happen:

Enter your DNA sequence: CCATAGCACGTT
Your DNA sequence is: CCATAGCACGTT #this is because I need to convert DNA sequence to RNA but I am not sure of the formula and how to do it in python
GGUAUCGUGCAA
Your amino acids chain is: Glycine, Isoleucine, Valine, Glutamine

Why do I get the output of A, and how do I fix it to become the output I want? I know that I am not doing RNA = RNA.replace('G', 'C') but when I do that, the output becomes

Enter your DNA sequence: CAACAUGCU
Your DNA sequence is CAACAUGCU
A
Glutamine Histidine Alanine 

Or something along those lines, but definitely not what I what do happen. Please help?

8

3 Answers

You can do the following regarding the replacement where you encounter you problems, as far as I've seen; It can be done with 1 translate call or 2 depend on you preferences:

a = input("Enter your DNA sequence: ")
a = a.upper()
print("Your DNA sequence is", a)
# RNA = a.translate(str.maketrans({'G': 'C', 'C': 'G'}))
# RNA = RNA.translate(str.maketrans({'A': 'U', 'T': 'A'}))
RNA = a.translate(str.maketrans({'G': 'C', 'C': 'G', 'A': 'U', 'T': 'A'}))
print(RNA)

The output is:

Enter your DNA sequence: CCATAGCACGTT
Your DNA sequence is CCATAGCACGTT
GGUAUCGUGCAA

Regarding to the printing of the amino acids:

b = len(RNA)

if b % 3 == 0: #if the length of inputDNA is a multiple of 3
  for k in (RNA[i:i + n] for i in range(0, len(RNA), n)):
    for kk, val in DNA_Sequence.items():
      if k in val:
        print(kk, end=" ")
        break
else: #if the length of inputDNA is not a multiple of 3
    print("I hate u")

Notice

The table is not a DNA table but RNA (you have U) there thus you need to use the RNA in the loops and the output is:

Glycine Isoleucine Valine Glutamine

3

I wrote a pandas solution for this for another person:

import pandas as pd
df =  pd.DataFrame(list(xdict.items()))

import re
def lookupKeys(df, key):
  name = []
  matches = re.findall(r'...', key)
  for match in matches:
      name.append(df[df[1].apply(lambda x: True if key in x else x) == True][0].reset_index()[0][0])
  return name


lookupKeys(df, 'GGUAUCGUGCAA')                                                                                                                                                      
# ['Glycine', 'Isoleucine', 'Valine', 'Glutamine']

Ok there were some syntax errors, but made it to up and running...

n = 3

xdict = {
    "Phenylalanine": ["UUU", "UUC"],
    "Leucine": ["UUA", "CUU", "CUC", "CUA", "CUG", "UUG"],
    "Isoleucine": ["AUU", "AUC", "AUA"],
    # Put the 'AUG' on brackets []
    "Methionine": ["AUG"],
    "Valine": ["GUU", "GUC", "GUA", "GUG"],
    "Serine": ["UCU", "UCC", "UCA", "UCG"],
    "Proline": ["CCU", "CCC", "CCA", "CCG"],
    "Threonine": ["ACU", "ACC", "ACA", "ACG"],
    "Alanine": ["GCU", "GCC", "GCA", "GCG"],
    "Tyrosine": ["UAU", "UAC"],
    "Histidine": ["CAU", "CAC"],
    "Glutamine": ["CAA", "CAG"],
    "Asparagine": ["AAU", "AAC"],
    "Lysine": ["AAA", "AAG"],
    "Asparatic Acid": ["GAU", "GAC"],
    "Glutamic Acid": ["GAA", "GAG"],
    "Cysteine": ["UGU", "UGC"],
    "Trytophan": "UGG",
    "Arginine": ["CGU", "CGC", "CGA", "CGG", "AGG", "AGA"],
    "Serine": ["AGU", "AGC"],
    "Glycine": ["GGU", "GGC", "GGA", "GGG"]
}

lookup_dict = {k: key for key, values in xdict.items() for k in values}
a = input("Enter your DNA sequence: ")
a = a.upper()
print("Your DNA sequence is", a)
str(a)
RNA = a.replace('C', 'G')
RNA = RNA.replace('A', "U")
RNA = RNA.replace('T', 'A')
print(RNA)

b = len(a)

# Introduced a new flag variable
val = ''

if b % 3 == 0:
    # a replaced with RNA
  for k in (RNA[i:i + n] for i in range(0, len(a), n)):
      val += lookup_dict[k] + ' '

elif b % 3 != 0:
  print("Try again.")


print('Name', val)

You were looping via 'a' but it should be RNA. Read the comments to know the other changes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.