Sha256: ee492c1d5edf5e9308aec799d3afc99a016952cf1d3e19158823590d3644e1c6

Contents?: true

Size: 1.27 KB

Versions: 484

Compression:

Stored size: 1.27 KB

Contents

use std::collections::HashMap;

pub struct CodonInfo<'a> {
    actual_codons: HashMap<&'a str, &'a str>
}

pub fn parse<'a>(pairs: Vec<(&'a str, &'a str)>) -> CodonInfo<'a> {
    CodonInfo{
        actual_codons: pairs.into_iter().collect()
    }
}

impl<'a> CodonInfo<'a> {
    pub fn name_for(&self, codon: &str) -> Result<&'a str, &'static str> {
        if codon.len() != 3 {
            return Err("invalid length")
        }

        let mut valid = true;
        let lookup: String = codon.chars().map(|l| {
            // Get an example of a "letter" represented by the possibly encoded letter.
            // Since every codon represented by the compressed notation has to be of
            // the desired amino acid just picking one at random will do.
            match l {
                'A' | 'W' | 'M' | 'R' | 'D' | 'H' | 'V' | 'N' => 'A',
                'C' | 'S' | 'Y' | 'B' => 'C',
                'G' | 'K' => 'G',
                'T' => 'T',
                _ => { valid = false; ' ' }
            }
        }).collect();
        if !valid {
            return Err("invalid char")
        }

        // If the input table is correct (which it is) every valid codon is in it
        // so unwrap() shouldn't panic.
        Ok(self.actual_codons.get(&lookup.as_ref()).unwrap())
    }
}

Version data entries

484 entries across 395 versions & 1 rubygems

Version Path
trackler-2.2.1.179 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.178 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.177 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.176 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.175 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.174 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.173 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.172 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.171 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.170 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.169 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.167 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.166 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.165 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.164 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.163 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.162 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.161 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.160 tracks/rust/exercises/nucleotide-codons/example.rs
trackler-2.2.1.159 tracks/rust/exercises/nucleotide-codons/example.rs