-
Notifications
You must be signed in to change notification settings - Fork 0
/
protein_translation.exs
68 lines (64 loc) · 1.6 KB
/
protein_translation.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule ProteinTranslation do
@codons %{
"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"
}
@invalid_codon "invalid codon"
@invalid_rna "invalid RNA"
@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: of_rna(rna, [])
defp of_rna("", []), do: { :error, @invalid_rna }
defp of_rna("", proteins), do: { :ok, proteins }
defp of_rna(<<codon::binary-size(3), tail::binary>>, proteins) do
case of_codon(codon) do
{ :ok, "STOP" } -> of_rna("", proteins)
{ :ok, protein } -> of_rna(tail, proteins ++ [protein])
{ :error, _ } -> of_rna("", [])
end
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
case @codons[codon] do
nil -> { :error, @invalid_codon }
protein -> { :ok, protein }
end
end
end