Last active
May 29, 2017 03:08
-
-
Save itsanna/869136034c55af98d605614cf2d9945c to your computer and use it in GitHub Desktop.
Interview Question: Implement protein synthesis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
/* | |
Implement: Protein synthesis | |
Rules: | |
1. For an input string of ATGC, map A->T, T->A, C-G, G->C | |
2. If you see ATG in the mapped output start TRANSLATION (described below) | |
3. Stop when you see TAG | |
TRANSLATION: | |
1. Map triplets to the given protein output: | |
TTT -> PHE | |
TTC -> PHE | |
CGC -> ARG | |
Input: a long string of ATGC | |
Example: “TACAAAAAAATC” | |
Expected Out : PHE,PHE | |
*/ | |
type Encodings map[string]string | |
func getIndex(seq string) int { | |
for i := range seq { | |
// check seq[i:i+3] for ATG | |
if seq[i:i+3] == "ATG" { | |
return i + 3 | |
} | |
if i == len(seq)-3 { | |
// break before last codon | |
break | |
} | |
} | |
return -1 | |
} | |
func match(seq string, encodings Encodings) []string { | |
codons := []string{} | |
proteins := []string{} | |
index := getIndex(seq) | |
if index < 0 { | |
return proteins | |
} | |
for i := index; i < len(seq); i++ { | |
codon := seq[i : i+3] | |
if seq[i:i+3] == "TAG" { | |
break | |
} | |
if i == len(seq)-3 { | |
// break before last codon | |
break | |
} | |
// check seq[i:i+3] | |
if i%3 == 0 { | |
codons = append(codons, codon) | |
} | |
} | |
for _, codon := range codons { | |
protein, ok := encodings[codon] | |
if !ok { | |
fmt.Println("could not find protein for codon", codon) | |
break | |
} | |
proteins = append(proteins, protein) | |
} | |
return proteins | |
} | |
func Translation(str string) string { | |
encoding := map[string]string{ | |
"TTC": "PHE", | |
"TTT": "PHE", | |
"CGC": "ARG", | |
} | |
baseMap := map[string]string{ | |
"A": "T", | |
"T": "A", | |
"C": "G", | |
"G": "C", | |
} | |
baseArr := []string{} | |
for _, rune := range str { | |
base := baseMap[string(rune)] | |
baseArr = append(baseArr, base) | |
} | |
seq := strings.Join(baseArr, "") | |
fmt.Println(seq) | |
codons := match(seq, encoding) | |
return strings.Join(codons, ",") | |
} | |
func main() { | |
in := "AAATACAAAAAAATC" | |
out := Translation(in) | |
fmt.Println("Expect PHE, PHE; Got", out, "\n") | |
in = "AAATTTAAA" | |
out = Translation(in) | |
fmt.Println("Expect EMPTY; Got", out, "\n") | |
in = "TACGCGATC" | |
out = Translation(in) | |
fmt.Println("Expect ARG; Got", out, "\n") | |
in = "TACAAGATC" | |
out = Translation(in) | |
fmt.Println("Expect PHE; Got", out, "\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment