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.29 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.28 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.27 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.26 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.25 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.24 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.23 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.22 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.21 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.20 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.19 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.18 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.17 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.16 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.15 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.14 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.13 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.12 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.11 tracks/elixir/exercises/protein-translation/example.exs
trackler-2.2.1.10 tracks/elixir/exercises/protein-translation/example.exs