nucleotide_count.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Nucleotide counter
class Nucleotide
  EMPTY_MAP = { 'A' => 0, 'C' => 0, 'G' => 0, 'T' => 0 }.freeze

  # Create a new nucleotide counter from a DNA sequence
  def self.from_dna(sequence)
    new(sequence)
  end

  # Get count of given nucleotide
  def count(nucleotide)
    @histogram[nucleotide]
  end

  attr_reader :histogram

  private

  def initialize(sequence)
    raise ArgumentError, 'Invalid DNA' unless /^[AGCT]*$/ =~ sequence

    @histogram = EMPTY_MAP.dup
    count_nucleotides(sequence)
  end

  def count_nucleotides(sequence)
    sequence.each_char { |nuc| @histogram[nuc] += 1 }
  end
end

@remcopeereboom thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment