Skip to content

Instantly share code, notes, and snippets.

@gavinmcgimpsey
Created January 14, 2016 21:46
Show Gist options
  • Select an option

  • Save gavinmcgimpsey/b18db122c091e80ca017 to your computer and use it in GitHub Desktop.

Select an option

Save gavinmcgimpsey/b18db122c091e80ca017 to your computer and use it in GitHub Desktop.

Revisions

  1. gavinmcgimpsey created this gist Jan 14, 2016.
    43 changes: 43 additions & 0 deletions protein_translation.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    class InvalidCodonError < ArgumentError; end

    module Translation
    def self.of_rna(string)
    result = []
    tris = string.chars
    .each_slice(3)
    .to_a
    .map(&:join)

    tris.each do |tri|
    next_codon = of_codon(tri)

    break if next_codon == 'STOP'
    result << next_codon
    end

    result
    end

    def self.of_codon(tri)
    case tri
    when 'AUG'
    'Methionine'
    when 'UUU', 'UUC'
    'Phenylalanine'
    when 'UUA', 'UUG'
    'Leucine'
    when 'UCU', 'UCC', 'UCA', 'UCG'
    'Serine'
    when 'UAU', 'UAC'
    'Tyrosine'
    when 'UGU', 'UGC'
    'Cysteine'
    when 'UGG'
    'Tryptophan'
    when 'UAA', 'UAG', 'UGA'
    'STOP'
    else
    fail InvalidCodonError
    end
    end
    end