Skip to content

Commit

Permalink
Handle invalid amino acids in codon optimization (#228)
Browse files Browse the repository at this point in the history
* Handle invalid amino acids in codon optimization

* add comment
  • Loading branch information
fialhopm authored Dec 7, 2021
1 parent 9eaa943 commit f35062c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
16 changes: 14 additions & 2 deletions synthesis/codon/codon.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ var errEmtpyCodonTable error = errors.New("empty codon table")
var errEmtpyAminoAcidString error = errors.New("empty amino acid string")
var errEmtpySequenceString error = errors.New("empty sequence string")

// InvalidAminoAcidError is returned when an input protein sequence contains an invalid amino acid.
type InvalidAminoAcidError struct {
AminoAcid rune
}

func (e InvalidAminoAcidError) Error() string {
return fmt.Sprintf("amino acid %q is missing from codon table", e.AminoAcid)
}

// Codon holds information for a codon triplet in a struct
type Codon struct {
Triplet string `json:"triplet"`
Expand Down Expand Up @@ -126,8 +135,11 @@ func Optimize(aminoAcids string, codonTable Table) (string, error) {
}

for _, aminoAcid := range aminoAcids {
aminoAcidString := string(aminoAcid)
codons.WriteString(codonChooser[aminoAcidString].Pick().(string))
chooser, ok := codonChooser[string(aminoAcid)]
if !ok {
return "", InvalidAminoAcidError{aminoAcid}
}
codons.WriteString(chooser.Pick().(string))
}
return codons.String(), nil
}
Expand Down
14 changes: 14 additions & 0 deletions synthesis/codon/codon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ func TestOptimizeErrorsOnEmptyAminoAcidString(t *testing.T) {
t.Error("Optimize should return an error if given an empty amino acid string")
}
}
func TestOptimizeErrorsOnInvalidAminoAcid(t *testing.T) {
aminoAcids := "TOP"
table := GetCodonTable(1) // does not contain 'O'
_, err := Optimize(aminoAcids, table)

want := InvalidAminoAcidError{'O'}
got, isInvalidAminoAcidError := err.(InvalidAminoAcidError)
if !isInvalidAminoAcidError {
t.Errorf("Optimize should return an InvalidAminoAcidError, got %T", got)
}
if got != want {
t.Errorf("Optimize should return an InvalidAminoAcidError for %q, got %q", want.AminoAcid, got.AminoAcid)
}
}

func TestGetCodonFrequency(t *testing.T) {

Expand Down

0 comments on commit f35062c

Please sign in to comment.