Skip to content

Commit

Permalink
Ensure Non-Met start codons map to Met on translation
Browse files Browse the repository at this point in the history
Also makes Translate() return an error if an invalid start codon is received
  • Loading branch information
carreter committed Aug 2, 2023
1 parent f587e72 commit c90d5b3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
26 changes: 25 additions & 1 deletion synthesis/codon/codon.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type AminoAcid struct {
type Table interface {
Chooser() (map[string]weightedRand.Chooser, error)
GenerateTranslationTable() map[string]string
GenerateStartCodonTable() map[string]string
GetAminoAcids() []AminoAcid
GetStartCodons() []string
GetStopCodons() []string
Expand All @@ -113,15 +114,27 @@ func Translate(sequence string, codonTable Table) (string, error) {
var aminoAcids strings.Builder
var currentCodon strings.Builder
translationTable := codonTable.GenerateTranslationTable()
startCodonTable := codonTable.GenerateStartCodonTable()

startCodonReached := false
for _, letter := range sequence {

// add current nucleotide to currentCodon
currentCodon.WriteRune(letter)

// if current nucleotide is the third in a codon translate to aminoAcid write to aminoAcids and reset currentCodon.
// use start codon table for the first codon only, erroring out if an invalid start codon is provided
if currentCodon.Len() == 3 {
aminoAcids.WriteString(translationTable[strings.ToUpper(currentCodon.String())])
if startCodonReached {
aminoAcids.WriteString(translationTable[strings.ToUpper(currentCodon.String())])
} else {
aminoAcid, ok := startCodonTable[strings.ToUpper(currentCodon.String())]
if !ok {
return "", fmt.Errorf("start codon %q is not in start codon table %v", currentCodon.String(), startCodonTable)
}
aminoAcids.WriteString(aminoAcid)
startCodonReached = true
}

// reset codon string builder for next codon.
currentCodon.Reset()
Expand Down Expand Up @@ -183,6 +196,17 @@ func (table codonTable) OptimizeTable(sequence string) Table {
return table
}

// GenerateStartCodonTable returns a mapping from the start codons of a Table to their associated amino acids.
// For our codonTable implementation, assumes that we always map to Met.
func (table codonTable) GenerateStartCodonTable() map[string]string {
result := make(map[string]string)
for _, codon := range table.StartCodons {
result[codon] = "M"
}

return result
}

// getCodonFrequency takes a DNA sequence and returns a hashmap of its codons and their frequencies.
func getCodonFrequency(sequence string) map[string]int {

Expand Down
20 changes: 20 additions & 0 deletions synthesis/codon/codon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ func TestTranslation(t *testing.T) {
}

}

// Non-Met start codons should still map to Met for our standard codon tables.
// See https://github.com/TimothyStiles/poly/issues/305.
func TestTranslationAlwaysMapsStartCodonToMet(t *testing.T) {
gfpTranslation := "MASKGEELFTGVVPILVELDGDVNGHKFSVSGEGEGDATYGKLTLKFICTTGKLPVPWPTLVTTFSYGVQCFSRYPDHMKRHDFFKSAMPEGYVQERTISFKDDGNYKTRAEVKFEGDTLVNRIELKGIDFKEDGNILGHKLEYNYNSHNVYITADKQKNGIKANFKIRHNIEDGSVQLADHYQQNTPIGDGPVLLPDNHYLSTQSALSKDPNEKRDHMVLLEFVTAAGITHGMDELYK*"
gfpDnaSequence := "TTGGCTAGCAAAGGAGAAGAACTTTTCACTGGAGTTGTCCCAATTCTTGTTGAATTAGATGGTGATGTTAATGGGCACAAATTTTCTGTCAGTGGAGAGGGTGAAGGTGATGCTACATACGGAAAGCTTACCCTTAAATTTATTTGCACTACTGGAAAACTACCTGTTCCATGGCCAACACTTGTCACTACTTTCTCTTATGGTGTTCAATGCTTTTCCCGTTATCCGGATCATATGAAACGGCATGACTTTTTCAAGAGTGCCATGCCCGAAGGTTATGTACAGGAACGCACTATATCTTTCAAAGATGACGGGAACTACAAGACGCGTGCTGAAGTCAAGTTTGAAGGTGATACCCTTGTTAATCGTATCGAGTTAAAAGGTATTGATTTTAAAGAAGATGGAAACATTCTCGGACACAAACTCGAGTACAACTATAACTCACACAATGTATACATCACGGCAGACAAACAAAAGAATGGAATCAAAGCTAACTTCAAAATTCGCCACAACATTGAAGATGGATCCGTTCAACTAGCAGACCATTATCAACAAAATACTCCAATTGGCGATGGCCCTGTCCTTTTACCAGACAACCATTACCTGTCGACACAATCTGCCCTTTCGAAAGATCCCAACGAAAAGCGTGACCACATGGTCCTTCTTGAGTTTGTAACTGCTGCTGGGATTACACATGGCATGGATGAGCTCTACAAATAA"

if got, _ := Translate(gfpDnaSequence, GetCodonTable(11)); got != gfpTranslation {
t.Errorf("TestTranslation has failed. Translate has returned %q, want %q", got, gfpTranslation)
}
}

func TestTranslationErrorsOnIncorrectStartCodon(t *testing.T) {
badSequence := "GGG"

if _, gotErr := Translate(badSequence, GetCodonTable(11)); gotErr == nil {
t.Errorf("Translation should return an error if given an incorrect start codon")
}
}

func TestTranslationErrorsOnEmptyCodonTable(t *testing.T) {
emtpyCodonTable := codonTable{}
_, err := Translate("A", emtpyCodonTable)
Expand Down

0 comments on commit c90d5b3

Please sign in to comment.