Sha256: ab5dc814d2ae8ca34acb0120e9418dd04bf6e103d2cf81f5b864ebcc769b1636

Contents?: true

Size: 1.57 KB

Versions: 207

Compression:

Stored size: 1.57 KB

Contents

defmodule ProteinTranslation do
  @doc """
  Given an RNA string, return a list of proteins specified by codons, in order.
  """
  @spec of_rna(String.t()) :: { atom, list(String.t()) }
  def of_rna(rna) do
    translate_rna(rna, [])
  end

  @doc """
  Given a codon, return the corresponding protein

  UGU -> Cysteine
  UGC -> Cysteine
  UUA -> Leucine
  UUG -> Leucine
  AUG -> Methionine
  UUU -> Phenylalanine
  UUC -> Phenylalanine
  UCU -> Serine
  UCC -> Serine
  UCA -> Serine
  UCG -> Serine
  UGG -> Tryptophan
  UAU -> Tyrosine
  UAC -> Tyrosine
  UAA -> STOP
  UAG -> STOP
  UGA -> STOP
  """
  @spec of_codon(String.t()) :: { atom, String.t() }
  def of_codon(codon) do
    translate_codon(codon)
  end

  @codons %{
    "Methionine" => ~w(AUG),
    "Phenylalanine" => ~w(UUU UUC),
    "Leucine" => ~w(UUA UUG),
    "Serine" => ~w(UCU UCC UCA UCG),
    "Tyrosine" => ~w(UAU UAC),
    "Cysteine" => ~w(UGU UGC),
    "Tryptophan" => ~w(UGG),
  }
  @stop ~w(UAA UAG UGA)

  defp translate_rna("", results), do: { :ok, Enum.reverse(results) }

  for codon <- @stop do
    defp translate_rna(unquote(codon) <> _rest, results), do: { :ok, Enum.reverse(results) }
    defp translate_codon(unquote(codon)), do: { :ok, "STOP" }
  end

  for { protein, codons } <- @codons, codon <- codons do
    defp translate_rna(unquote(codon) <> rest, results), do: translate_rna(rest, [unquote(protein) | results])
    defp translate_codon(unquote(codon)), do: { :ok, unquote(protein) }
  end

  defp translate_rna(_, _), do: { :error, "invalid RNA"}
  defp translate_codon(_), do: { :error, "invalid codon"}
end

Version data entries

207 entries across 207 versions & 1 rubygems

Version Path
trackler-2.2.1.109 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.108 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.107 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.106 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.105 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.104 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.103 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.102 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.101 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.100 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.99 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.98 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.97 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.96 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.95 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.94 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.93 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.92 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.91 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.90 tracks/elixir/exercises/protein-translation/example.exs