summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-03-08 21:51:43 -0600
committerJesse Luehrs <doy@tozt.net>2013-03-08 21:51:43 -0600
commit8026d2618d3b48bdcb1363ad4d651b37c41030a3 (patch)
tree3db380bf53827b1c3286728fb2e81c6571aedd48
parent241bad16f0dcc96403c697e13286d5444e67a9d2 (diff)
downloadrosalind-8026d2618d3b48bdcb1363ad4d651b37c41030a3.tar.gz
rosalind-8026d2618d3b48bdcb1363ad4d651b37c41030a3.zip
another rust solution
-rw-r--r--PROT.rs8
-rw-r--r--rosalind/mod.rc1
-rw-r--r--rosalind/protein.rs44
3 files changed, 53 insertions, 0 deletions
diff --git a/PROT.rs b/PROT.rs
new file mode 100644
index 0000000..40979e1
--- /dev/null
+++ b/PROT.rs
@@ -0,0 +1,8 @@
+extern mod rosalind;
+use rosalind::io::input_line;
+use rosalind::protein::translate;
+
+fn main() {
+ let rna = input_line();
+ io::println(translate(rna));
+}
diff --git a/rosalind/mod.rc b/rosalind/mod.rc
index 3908b83..99fa08f 100644
--- a/rosalind/mod.rc
+++ b/rosalind/mod.rc
@@ -4,5 +4,6 @@
mod dna;
mod fasta;
+mod protein;
mod io;
mod str;
diff --git a/rosalind/protein.rs b/rosalind/protein.rs
new file mode 100644
index 0000000..7d56093
--- /dev/null
+++ b/rosalind/protein.rs
@@ -0,0 +1,44 @@
+use str = core::str;
+
+const STOP: char = -1 as char;
+
+fn translate(rna: &str) -> ~str {
+ assert str::len(rna) % 3 == 0;
+
+ let codons = str::len(rna) / 3;
+ let mut protein = str::with_capacity(codons);
+
+ for uint::range(0, codons) |i| {
+ let codon = str::slice(rna, i * 3, i * 3 + 3);
+ let amino = translate_single(codon);
+ if (amino == STOP) {
+ break;
+ }
+ str::push_char(&mut protein, amino);
+ }
+
+ protein
+}
+
+priv fn translate_single(codon: &str) -> char {
+ assert str::len(codon) == 3;
+ match codon {
+ "UUU" => 'F', "CUU" => 'L', "AUU" => 'I', "GUU" => 'V',
+ "UUC" => 'F', "CUC" => 'L', "AUC" => 'I', "GUC" => 'V',
+ "UUA" => 'L', "CUA" => 'L', "AUA" => 'I', "GUA" => 'V',
+ "UUG" => 'L', "CUG" => 'L', "AUG" => 'M', "GUG" => 'V',
+ "UCU" => 'S', "CCU" => 'P', "ACU" => 'T', "GCU" => 'A',
+ "UCC" => 'S', "CCC" => 'P', "ACC" => 'T', "GCC" => 'A',
+ "UCA" => 'S', "CCA" => 'P', "ACA" => 'T', "GCA" => 'A',
+ "UCG" => 'S', "CCG" => 'P', "ACG" => 'T', "GCG" => 'A',
+ "UAU" => 'Y', "CAU" => 'H', "AAU" => 'N', "GAU" => 'D',
+ "UAC" => 'Y', "CAC" => 'H', "AAC" => 'N', "GAC" => 'D',
+ "UAA" => STOP, "CAA" => 'Q', "AAA" => 'K', "GAA" => 'E',
+ "UAG" => STOP, "CAG" => 'Q', "AAG" => 'K', "GAG" => 'E',
+ "UGU" => 'C', "CGU" => 'R', "AGU" => 'S', "GGU" => 'G',
+ "UGC" => 'C', "CGC" => 'R', "AGC" => 'S', "GGC" => 'G',
+ "UGA" => STOP, "CGA" => 'R', "AGA" => 'R', "GGA" => 'G',
+ "UGG" => 'W', "CGG" => 'R', "AGG" => 'R', "GGG" => 'G',
+ _ => fail ~"Unknown codon",
+ }
+}