Avatar of JaeHyoLee

JaeHyoLee's solution

to Nucleotide Count in the Lua Track

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

Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.

Here is an analogy:

  • twigs are to birds nests as
  • nucleotides are to DNA as
  • legos are to lego houses as
  • words are to sentences as...

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

The Calculating DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/

Submitting Incomplete Solutions

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

nucleotide-count_spec.lua

local DNA = require('nucleotide-count')

describe('nucleotide-count', function()
  it('has no nucleotides', function()
    local expected =  { A = 0, T = 0, C = 0, G = 0 }
    dna = DNA:new('')
    local result = dna.nucleotideCounts
    assert.are.same(expected, result)
  end)

  it('has no adenosine', function()
    local dna = DNA:new('')
    local expected = 0
    result = dna:count('A')
    assert.are.same(expected, result)
 end)

  it('repetitive cytosine gets counts', function()
    local dna = DNA:new('CCCCC')
    local expected = 5
    result = dna:count('C')
    assert.are.same(expected, result)
  end)

  it('repetitive sequence has only guanosine', function()
    local dna = DNA:new('GGGGGGGG')
    expected = { A = 0, T = 0, C = 0, G = 8 }
    result = dna.nucleotideCounts
    assert.are.same(expected, result)
  end)

  it('counts only thymine', function()
    local dna = DNA:new('GGGGTAACCCGG')
    expected = 1
    result = dna:count('T')
    assert.are.same(expected, result)
  end)

  it('counts a nucleotide only once', function()
    local dna = DNA:new('GGTTGG')
    expected = 2
    result = dna:count('T')
    assert.are.same(expected, result)
  end)

  it('validates nucleotides', function()
    local dna = DNA:new('GGTTGG')
     assert.has.errors(function() dna:count('X') end)
     assert.has_error(function() dna:count('X') end, 'Invalid Nucleotide')
  end)

  it('counts all nucleotides', function()
    local dna = DNA:new('AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC')
    expected = { A = 20, T = 21, G = 17, C = 12 }
    result = dna.nucleotideCounts
    assert.are.same(expected, result)
  end)
end)
local n_count = {}

function n_count:count(base)
  assert(self.nucleotideCounts[base], 'Invalid Nucleotide')
  return self.nucleotideCounts[base]
end

function n_count:new(dna)
  local o = {}
  setmetatable(o, self)
  self.__index = self

  o.nucleotideCounts = { A = 0, T = 0, C = 0, G = 0 }

  for i = 1, #dna do
    local c = dna:sub(i,i)
    assert(o.nucleotideCounts[c], 'Invalid Nucleotide')
    o.nucleotideCounts[c] = o.nucleotideCounts[c] + 1
  end
  return o
end

return n_count

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?