From dc68b60713c95ec6209f1376a8ac5c5a98e8c787 Mon Sep 17 00:00:00 2001 From: HardikPurohit Date: Thu, 19 Jan 2017 14:26:52 +0530 Subject: [PATCH] Protein Translation --- protein_translation.rb | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 protein_translation.rb diff --git a/protein_translation.rb b/protein_translation.rb new file mode 100644 index 0000000..4107afa --- /dev/null +++ b/protein_translation.rb @@ -0,0 +1,66 @@ +class Translation + def self.of_rna(codon) + Translation.of_codon(codon) + end + def self.of_codon(codon) + @@result = [] + if codon.class == String + @codon_array = codon.split("") + @codon_string="" + char = 0 + @codon_array.each do |i| + if char != 3 + @codon_string += i + char += 1 + end + if char == 3 + return_proteins = self.new.proteins(@codon_string) + raise InvalidCodonError if return_proteins == "invalid" + if @@result == [] + @@result << return_proteins + elsif return_proteins == "STOP" + break + else + @@result << return_proteins + end + @codon_string = "" + char = 0 + end + end + else + codon.each do |i| + return_proteins = self.new.proteins(i) + raise InvalidCodonError if return_proteins == "invalid" + if @@result == [] + @@result << return_proteins + elsif return_proteins == "STOP" + break + else + @@result << return_proteins + end + @codon_string = "" + char = 0 + end + end + if @@result.uniq.length == 1 + return @@result[0] + else + return @@result + end + end + def proteins(codon) + case codon + when "AUG" then return "Methionine" + when "UUU", "UUC" then return "Phenylalanine" + when "UUA", "UUG" then return "Leucine" + when "UCU", "UCC", "UCA", "UCG" then return "Serine" + when "UAU", "UAC" then return "Tyrosine" + when "UGU", "UGC" then return "Cysteine" + when "UGG" then return "Tryptophan" + when "UAA", "UAG", "UGA" then return "STOP" + else + return "invalid" + end + end +end +InvalidCodonError = Class.new(StandardError)