首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rna到蛋白质翻译器

rna到蛋白质翻译器
EN

Stack Overflow用户
提问于 2020-11-28 18:45:17
回答 1查看 99关注 0票数 1

我目前正在开发我的第一个网站,这是一个dna翻译器,你可以将你的dna翻译成某种蛋白质。为此,我在视图中创建了一个类,如下所示:

代码语言:javascript
复制
class TranslatorView(View):
    template_name = 'main/translated.html'

    mapper = {
        "a": "u",
        "t": "a",
        "c": "g",
        "g": "c"
    }

    mapper_1={                           #Here's the protein dictionary
        "aat": "Asparagine",
        "aac": "Asparagine",
        "aaa": "Lysine",
        "aag": "Lysine",
        "act": "Threonine",
        "acc": "Threonine",
        "aca": "Threonine",
        "acg": "Threonine",
        "agt": "Serine",
        "agc": "Serine",
        "aga": "Arginine",
        "agg": "Arginine",
        "att": "Isoleucine",
        "atc": "Isoleucine",
        "ata": "Isoleucine",
        "atg": "Methionine",
        "cat": "Histidine",
        "cac": "Histidine",
        "caa": "Glutamine",
        "cag": "Glutamine",
        "cct": "Proline",
        "ccc": "Proline",
        "cca": "Proline",
        "ccg": "Proline",
        "cgt": "Arginine",
        "cgc": "Arginine",
        "cga": "Arginine",
        "cgg": "Arginine",
        "ctt": "Leucine",
        "ctc": "Leucine",
        "cta": "Leucine",
        "ctg": "Leucine",
        "gat": "Aspartic",
        "gac": "Aspartic",
        "gaa": "Glutamic",
        "gag": "Glutamic",
        "gct": "Alanine",
        "gcc": "Alanine",
        "gca": "Alanine",
        "gcg": "Alanine",
        "ggt": "Glycine",
        "ggc": "Glycine", 
        "gga": "Glycine",
        "ggg": "Glycine",
        "gtt": "Valine",
        "gtc": "Valine",
        "gta": "Valine",
        "gtg": "Valine",
        "tat": "Tyrosine",
        "tac": "Tyrosine",
        "taa": "Stop",
        "tag": "Stop",
        "tct": "Serine",
        "tcc": "Serine",
        "tca": "Serine",
        "tcg": "Serine",
        "tgt": "Cysteine",
        "tgc": "Cysteine",
        "tga": "Stop",
        "tgg": "Tryptophan",
        "ttt": "Phenylalanine",
        "ttc": "Phenylalanine",
        "tta": "Leucine",
        "ttg": "Leucine",

    }

    def translate(self, phrase):
        translation = ""
        for letter in phrase:
            if letter.lower() in self.mapper:
                translation += self.mapper[letter.lower()].upper() if letter.isupper() else self.mapper[letter]
        return translation

    def translate_protein(self,phrase):     #Here's where I think the error is, The for loop should get the letters in groups of three, but Idk how to do it.
        protein = ""
        for letter in phrase:
            if letter.lower() in self.mapper_1:
                protein += self.mapper_1[letter.lower()].upper() if letter.isupper() else self.mapper_1[letter]
        return protein

    def get(self, request, *args, **kwargs):
        return render(request, 'main/translator.html')

    def post(self, request, *args, **kwargs):
        phrase = request.POST.get('text', 'translation')
        protein = request.POST.get('text','protein')
        return render(request, self.template_name, {'translation': self.translate(phrase), 'protein': self.translate_protein(protein)})

模板是这样的:

代码语言:javascript
复制
{% extends "base.html"%}

{% block content%}

<div >
    
    <h2 class = "display-3">DNA TRANSLATED SUCCESFULLY </h2>
    <br>
    <br>
    <br>

  
    <h2>
        {{ translation }}
    </h2>

    <br>
    <br>
    <br>
   
    <h2 class = "display-4">YOUR PROTEIN IS</h2>

    <div class = "protein_image"></div>

    <br>
    <br>

    <h2>
        {{ protein }}
    </h2>





    <button class= "button_with_image_save" value="Back" onclick="window.history.back()" ></button>


    
</div>   

{% endblock content%}

如果我输入一个像atc这样的链,蛋白质就不会出现,因为翻译蛋白质中的for循环可能是错误的。

下面是一个例子:

如你所见,rna密码子出现了,但蛋白质没有。

如果有人知道错误在哪里,如果你能告诉我那就太好了。

EN

回答 1

Stack Overflow用户

发布于 2020-11-28 20:02:09

if letter.lower() in self.mapper_1:永远不会返回True,因为单个字母不能等于您所遍历的字符串(字典中的键),您猜对了。

翻译一种氨基酸:

代码语言:javascript
复制
def translate_amino(self, codon):
        return self.mapper_1.get(phrase, "")

然后我建议再多一个函数来实际返回蛋白质,因为我不确定你应该如何写出蛋白质,所以我将按顺序返回一个氨基酸列表:

代码语言:javascript
复制
def build_protein(self, phrase):
    """Accepts RNA sequence and returns corresponding sequence of amino acids."""

    protein = []
    i = 0
    while i < len(phrase):
        codon = phrase[i: i + 3]
        amino = self.translate_amino(codon)
        if amino:
            protein.append(amino)
        else:
            print(f"The codon {codon} is not in self.mapper_1")

        i += 3
    return protein

对我来说,运行以下代码:

代码语言:javascript
复制
test = TranslatorView()
print(test.build_protein('tcgtgtgtcagg'))

给予:['Serine', 'Cysteine', 'Valine', 'Arginine']

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65048743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档