MarcJuegos_Yt
MarcJuegos_Yt

Reputation: 127

For loops in a view

I am currently working on a rna to protein translator. To do that, I've created a class that does two things.

Firstly, you input a dna chain and it gets translated into rna (this part works perfectly)

Secondly, you get the rna chain translated into a protein (this part doesn't work).

Here's the code for the class:

class TranslatorView(View):
    template_name = 'main/translated.html'

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

    amino_mapper={
        "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.rna_mapper:
                translation += self.rna_mapper[letter.lower()].upper() if letter.isupper() else self.rna_mapper[letter]
        return translation

    def translate_amino(self, phrase): #Here's where the error might be
        return self.amino_mapper.get(phrase, "")
    
    def build_protein(self, phrase): #Here's where the error might be
        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

    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_amino(protein)})

If anyone needs the template where the protein should get translated, here it is:

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

Now I'm going to show an example of how the class should work.

If I input a dna chain (such as atc) , I should get the dna translated into rna and the protein.

enter image description here

As you can see, the rna chain appears, but the protein doesn't.

Upvotes: 0

Views: 63

Answers (1)

Ogulcan Olguner
Ogulcan Olguner

Reputation: 581

I guess the post method should return the build_protein method instead of the translate_amino method. Shouldn't the 'protein' key be as follows?

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.build_protein(protein)})

Upvotes: 2

Related Questions