为什么我不能在python中将DNA转换成mRNA?

2024-05-19 18:41:38 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

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

发生了什么:

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

我想发生什么:

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

为什么我要得到A的输出,如何将其修复为我想要的输出?我知道我没有做RNA = RNA.replace('G', 'C'),但是当我做了那件事,输出就变成了

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

或者类似的事情,但肯定不是我所做的。请帮忙?你知道吗


Tags: ofthetoinforyouristhis
3条回答

好的,有一些语法错误,但使它启动并运行。。。你知道吗

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)

您正在通过“a”循环,但它应该是RNA。 阅读评论以了解其他更改。。。你知道吗

我为另一个人写了一个解决方案:

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']

据我所知,对于遇到问题的替换,您可以执行以下操作;可以使用1个翻译调用或2个取决于您的首选项:

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)

输出为:

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

关于氨基酸的印刷:

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

注意

该表不是DNA表,而是RNA(您有U),因此您需要在循环中使用RNA,输出为:

Glycine Isoleucine Valine Glutamine

相关问题 更多 >