// Package protein translates RNA sequences to proteins.
package protein
import "strconv"
type myerror int
func (m myerror) Error() string {
return strconv.Itoa(int(m))
}
// Error constants for the tests
const (
ErrStop myerror = iota
ErrInvalidBase
)
var codons = map[string]string{
"AUG": "Methionine",
"UUU": "Phenylalanine",
"UUC": "Phenylalanine",
"UUA": "Leucine",
"UUG": "Leucine",
"UCU": "Serine",
"UCC": "Serine",
"UCA": "Serine",
"UCG": "Serine",
"UAU": "Tyrosine",
"UAC": "Tyrosine",
"UGU": "Cysteine",
"UGC": "Cysteine",
"UGG": "Tryptophan",
"UAA": "STOP",
"UAG": "STOP",
"UGA": "STOP",
}
// FromCodon returns the protein of the given RNA codon or an error if sequence is STOP or invalid.
func FromCodon(sequence string) (string, error) {
ret, found := codons[sequence]
if !found {
return "", ErrInvalidBase
}
if ret == "STOP" {
return "", ErrStop
}
return ret, nil
}
// FromRNA returns the polypeptide of the input RNA.
func FromRNA(rna string) (peptide []string, err error) {
for len(rna) >= 3 {
sequence := rna[:3]
protein, err := FromCodon(sequence)
if err != nil {
if err == ErrStop {
return peptide, nil
}
return peptide, err
}
peptide = append(peptide, protein)
rna = rna[3:]
}
return peptide, nil
}