Avatar of JaeHyoLee

JaeHyoLee's solution

to Protein Translation in the Lua Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Translate RNA sequences into proteins.

RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so:

RNA: "AUGUUUUCU" => translates to

Codons: "AUG", "UUU", "UCU" => which become a polypeptide with the following sequence =>

Protein: "Methionine", "Phenylalanine", "Serine"

There are 64 codons which in turn correspond to 20 amino acids; however, all of the codon sequences and resulting amino acids are not important in this exercise. If it works for one codon, the program should work for all of them. However, feel free to expand the list in the test suite to include them all.

There are also three terminating codons (also known as 'STOP' codons); if any of these codons are encountered (by the ribosome), all translation ends and the protein is terminated.

All subsequent codons after are ignored, like this:

RNA: "AUGUUUUCUUAAAUG" =>

Codons: "AUG", "UUU", "UCU", "UAA", "AUG" =>

Protein: "Methionine", "Phenylalanine", "Serine"

Note the stop codon "UAA" terminates the translation and the final methionine is not translated into the protein sequence.

Below are the codons and resulting Amino Acids needed for the exercise.

Codon Protein
AUG Methionine
UUU, UUC Phenylalanine
UUA, UUG Leucine
UCU, UCC, UCA, UCG Serine
UAU, UAC Tyrosine
UGU, UGC Cysteine
UGG Tryptophan
UAA, UAG, UGA STOP

Learn more about protein translation on Wikipedia

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Tyler Long

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

protein-translation_spec.lua

local translate = require('protein-translation')

describe('protein-translation', function()
  describe('translate.codon', function()
    it('should translate AUG to Methionine', function()
      assert.equal('Methionine', translate.codon('AUG'))
    end)

    it('should translate UUU and UUC to Phenylalanine', function()
      assert.equal('Phenylalanine', translate.codon('UUU'))
      assert.equal('Phenylalanine', translate.codon('UUC'))
    end)

    it('should translate UUA and UUG to Leucine', function()
      assert.equal('Leucine', translate.codon('UUA'))
      assert.equal('Leucine', translate.codon('UUG'))
    end)

    it('should translate UCU, UCC, UCA, and UCG to Serine', function()
      assert.equal('Serine', translate.codon('UCU'))
      assert.equal('Serine', translate.codon('UCC'))
      assert.equal('Serine', translate.codon('UCA'))
      assert.equal('Serine', translate.codon('UCG'))
    end)

    it('should translate UAU and UAC to Tyrosine', function()
      assert.equal('Tyrosine', translate.codon('UAU'))
      assert.equal('Tyrosine', translate.codon('UAC'))
    end)

    it('should translate UGU and UGC to Cysteine', function()
      assert.equal('Cysteine', translate.codon('UGU'))
      assert.equal('Cysteine', translate.codon('UGC'))
    end)

    it('should translate UGG to Tryptophan', function()
      assert.equal('Tryptophan', translate.codon('UGG'))
    end)

    it('should translate UAA, UAG, and UGA to STOP codons', function()
      assert.equal('STOP', translate.codon('UAA'))
      assert.equal('STOP', translate.codon('UAG'))
      assert.equal('STOP', translate.codon('UGA'))
    end)

    it('should raise an error when an unknown codon is translated', function()
      assert.has_error(function() translate.codon('MIA') end)
    end)
  end)

  describe('translate.rna_strand', function()
    it('should translate each codon in a strand into the corresponding protein', function()
      assert.same({ 'Methionine', 'Phenylalanine', 'Tryptophan' }, translate.rna_strand('AUGUUUUGG'))
    end)

    it('should stop translation when a STOP codon is encountered', function()
      assert.same({ 'Methionine', 'Phenylalanine' }, translate.rna_strand('AUGUUUUAA'))
      assert.same({ 'Tryptophan', 'Cysteine', 'Tyrosine' }, translate.rna_strand('UGGUGUUAUUAAUGGUUU'))
    end)

    it('should raise an error when an unknown codon is encountered', function()
      assert.has_error(function() translate.rna_strand('UGUCARROT') end)
    end)

    it('should not raise an error when an unknown codon occurs after a STOP codon', function()
      assert.same({ 'Cysteine' }, translate.rna_strand('UGUUGACARROT'))
    end)
  end)
end)
local codon_pairs = {
  AUG = 'Methionine',
  UUU = 'Phenylalanine',
  UUC = 'Phenylalanine',
  UUA = 'Leucine',
  UUG = 'Leucine',
  UCU = 'Serine',
  UCC = 'Serine',
  UCA = 'Serine',
  UCG = 'Serine',
  UAU = 'Tyrosine',
  UAC = 'Tyrosine',
  UGU = 'Cysteine',
  UGC = 'Cysteine',
  UGG = 'Tryptophan',
  UAA = 'STOP',
  UAG = 'STOP',
  UGA = 'STOP',
}

local function codon(candidate)
  return (codon_pairs[candidate] or assert(false, 'unknown codon'))
end

local function rna_strand(sequence)
  local protein = {}
  for i=1, #sequence, 3 do
    local translated = codon(sequence:sub(i, i+2))
    if translated == 'STOP' then break end
    table.insert(protein, translated)
  end
  return protein
end

return{
  codon = codon,
  rna_strand = rna_strand
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?