RNA to PROTEIN计划问题

时间:2015-05-12 17:40:12

标签: python bioinformatics

我的代码存在一些问题,我很感激帮助。

该计划的第一部分旨在验证用户的输入;因此除了A U G C T(或小写)之外,他们不能输入任何其他内容。但是,如果我输入任何其他内容,我会收到一条非常长的错误消息,但我想要的是重启函数验证check()的程序。

此外,如果用户确实输入了有效序列,由于某种原因,我的代码没有将有效的RNA序列翻译成蛋白质序列。我想也许它与块函数有关,它将input_rna中的str分成3个字母的块。

import re

input_rna = input("Type RNA sequence: ")

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i+n]

def translate():
    amino_acids = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
        "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
        "UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
        "UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
        "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
        "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
        "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
        "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
        "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
        "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
        "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
        "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
        "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
        "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
        "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
        "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G",}

    translated = "".join(amino_acids[i] for i in chunks("".join(input_rna), 3))

def validation_check():
    global input_rna
    if re.match(r"[A, U, G, C, T, a, u, g, c, t]", input_rna):
        print("Correct! That is a valid sequence.")
        translate()
    else:
        print("That is not a valid RNA sequence, please try again.")
        validation_check()
validation_check()

2 个答案:

答案 0 :(得分:3)

正则表达式错误,请尝试:

if re.match(r"^[AUGCT]+$", input_rna, re.IGNORECASE):

以下情况更好,因为在RNA中尿嘧啶代替了噻嗪......

if re.match(r"^[AUGC]+$", input_rna, re.IGNORECASE):

注意:算法翻译有问题,

list(chunks("".join(input_rna), 3))

你得到:

['ACG', 'AUG', 'AGU', 'CAU', 'GCU', 'U']

最后的问题" ACGAUGAGUCAUGCUU",如果长度不是3的多个

溶液:

"".join(amino_acids[i] for i in chunks("".join(input_rna), 3) if len(i)==3)

答案 1 :(得分:3)

除了指出的其他问题,您的validation_check函数不允许用户再次输入字符串。这意味着您将不断尝试一遍又一遍地验证它,而无需更改它。

你可能想做的更像是:

def validation_check():
    input_rna = raw_input("Type RNA sequence: ").upper()
    if re.match(r"^[AUGCT]+$", input_rna):
        print("Correct! That is a valid sequence.")
        print translate(input_rna)
    else:
        print("That is not a valid RNA sequence, please try again.")
        validation_check()

这避免了使用全局,允许用户重新输入,并且不会自动导致无限循环。

(即便如此,在这里使用递归可能很糟糕,所以你应该考虑将其实现为while循环。)

您会注意到其他一些事情:

  • raw_input代替input,因为后者具有隐式eval。除非你绝对需要,否则你要避开它。
  • .upper()所以你有标准化的字符串来验证和关闭。由于你的基础词典只使用大写字符串,这比使用其他地方推荐的re.I更有意义。
  • 我让translate返回翻译的蛋白质,然后打印出来。你可能想做点什么。

我还在字典查找中添加了一个默认值:

translated = "".join(amino_acids.get(i, '!') for i in chunks("".join(rna), 3)) 

这样,你可以尝试继续处理,如果你得到一些奇怪的东西,而不是必须处理KeyError(如果用户输入你没有密钥的序列,将会引发for,like 'CUT'

我也注意到你允许,但不要翻译基地'T'。你可能想看一下。

无论如何,我收到的完整代码是:

import re

def chunks(l, n): 
    for i in range(0, len(l), n): 
        # print i
        chunk = l[i:i+n]
        # print chunk
        yield l[i:i+n]

def translate(rna):
    amino_acids = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
        "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
        "UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
        "UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
        "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
        "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
        "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
        "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
        "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
        "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
        "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
        "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
        "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
        "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
        "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
        "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G",}
    translated = "".join(amino_acids.get(i, '!') for i in chunks("".join(rna), 3)) 
    return translated

def validation_check():
    input_rna = raw_input("Type RNA sequence: ").upper()
    if re.match(r"^[AUGCT]+$", input_rna):
        print("Correct! That is a valid sequence.")
        print translate(input_rna)
    else:
        print("That is not a valid RNA sequence, please try again.")
        validation_check()

# in case you ever need to import this, don't always call validation_check
if __name__ == "__main__":
     validation_check()