简体   繁体   中英

rna to protein translator

I am currently working on my first website, which is a dna translator that you can translate your dna into a certain protein. To do that, I've created a class in views that goes like this:

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

The template goes like this:

{% 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%}

If I input a chain like atc, the protein doesn't appear, because the for loop in translate protein is probably wrong.

Here's an example: 在此处输入图像描述

As you can see, the rna codon appears, but not the protein.

If anybody knows where the error is, It'd be amazing if you told me.

if letter.lower() in self.mapper_1: will never return True as the single letter can't be equal to the string (keys in the dictionary) you are looping through, which you guessed correctly.

To translate a single amino acid:

def translate_amino(self, codon):
        return self.mapper_1.get(phrase, "")

I then suggest having 1 more function to actually return the protein, and since I'm not sure how you're supposed to write out the protein i'll just return a list of the amino acids in order:

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

For me, running this:

test = TranslatorView()
print(test.build_protein('tcgtgtgtcagg'))

Gives: ['Serine', 'Cysteine', 'Valine', 'Arginine']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM