1

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_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_protein(self,phrase):     
        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):
        protein = request.POST.get('text','protein')
        return render(request, self.template_name, {'protein': self.translate_protein(protein)})

I probably have the problem in the dictionary and for loop, because to translate the protein, the forloop needs to get three letters because a codom (rna string) is grouped in three letters.

Example:

AAT should be translated into Asparagine. So the forloop shoul've got the three letters in the rna chain and translated it into Asparagine.

Needless to say, Idk how to do that. So It'd be necessary some help.

By the way, I hope I made myself clear with the explanation.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172

1 Answers1

2

You can try use the following method as your translate function in your view. No for loop is needed, you can check Python slice reference.

def translate_protein(self, phrase):     
    if not isinstance(phrase, str):
      raise ValueError('Phrase should be a string.')
    if len(phrase) < 3:
      raise ValueError('Phrase should be at least three chars.')
    index_str = phrase[:3].lower()
    # The following will return None if no protein is found based on the index str.
    return self.mapper_1.get(index_str, None)
Jun Zhou
  • 1,077
  • 1
  • 6
  • 19
  • When I input a protein that it should be found, it returns None. I know that it returns None if no protein is found. But in this case, it should be found – MarcJuegos_Yt Nov 28 '20 at 14:03
  • Then it will act as expected because the second argument is just in case the protein is not found if a user gives a wrong one. Here are the details for python dict get() method. https://stackoverflow.com/a/6130800/11607969 – Jun Zhou Nov 28 '20 at 14:09
  • Which input string(phase value) are you using? – Jun Zhou Nov 28 '20 at 14:23
  • As I am coding a web, the input string is a textarea coded in html. Is that what you were asking. I'm an amateur in coding – MarcJuegos_Yt Nov 28 '20 at 14:26
  • Yeah, I am just checking which good phase does not give you the expected value in your translate_protein method. Maybe you want to check all chars in phase rather than the first 3 characters of the phase? – Jun Zhou Nov 28 '20 at 14:31
  • Ok I found the error. The code works if I only input a codom (three letters). However, an rna chain has three codoms (twelve letters). How should I fix that? – MarcJuegos_Yt Nov 28 '20 at 14:37
  • Maybe you can split the RNA chain to individual one and get its respective final protein for each of them? – Jun Zhou Nov 28 '20 at 14:42
  • It's a solution, but my goal is to automatize it as much as possible – MarcJuegos_Yt Nov 28 '20 at 14:46