Translate will return an amino acid sequence which the given DNA will yield
(dnaSeq string)
| 254 | |
| 255 | // Translate will return an amino acid sequence which the given DNA will yield |
| 256 | func (table *TranslationTable) Translate(dnaSeq string) (string, error) { |
| 257 | if dnaSeq == "" { |
| 258 | return "", errEmptySequenceString |
| 259 | } |
| 260 | |
| 261 | var aminoAcids strings.Builder |
| 262 | var currentCodon strings.Builder |
| 263 | translationTable := table.TranslationMap |
| 264 | |
| 265 | for _, letter := range dnaSeq { |
| 266 | // add current nucleotide to currentCodon |
| 267 | currentCodon.WriteRune(letter) |
| 268 | |
| 269 | // if current nucleotide is the third in a codon, translate to amino acid, write to aminoAcids, and reset currentCodon |
| 270 | if currentCodon.Len() == 3 { |
| 271 | aminoAcids.WriteString(translationTable[strings.ToUpper(currentCodon.String())]) |
| 272 | |
| 273 | // reset codon string builder for the next codon |
| 274 | currentCodon.Reset() |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return aminoAcids.String(), nil |
| 279 | } |
| 280 | |
| 281 | // weightAminoAcids weights each codon in a codon table according to input string codon frequency, adding weight to |
| 282 | // the given NCBI base codon table |