Sha256: 0be2dff0e351ce06e511b24817d8b15bb8abe0596fc4bc65d13a60a9ac26483e

Contents?: true

Size: 1.57 KB

Versions: 69

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

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/protein-translation/example.exs