Skip to content

Commit 74ee985

Browse files
sjwarner-bpFridaTveit
authored andcommitted
Protein Translation: Implement problem in Java language stream. (exercism#985)
* Protein Translation: Implemented problem in Java language stream. * Protein-translation: Update implementation with FridaTveit's suggestions. * Fix formatting and typesafety issues.
1 parent a686f51 commit 74ee985

7 files changed

Lines changed: 324 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@
166166
"unlocked_by": "difference-of-squares",
167167
"uuid": "d0dcc898-ec38-4822-9e3c-1a1829c9b2d9"
168168
},
169+
{
170+
"core": false,
171+
"difficulty": 3,
172+
"slug": "protein-translation",
173+
"topics": [
174+
"arrays",
175+
"conditionals",
176+
"loops",
177+
"strings"
178+
],
179+
"unlocked_by": "rna-transcription",
180+
"uuid": "1bea45f6-021e-0a80-1e53-afcd9f9a8df08c01126"
181+
},
169182
{
170183
"core": false,
171184
"difficulty": 4,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
class ProteinTranslator {
6+
7+
private static final String[] METHIONINE = {"AUG"};
8+
private static final String[] PHENYLALANINE = {"UUU", "UUC"};
9+
private static final String[] LEUCINE = {"UUA", "UUG"};
10+
private static final String[] SERINE = {"UCU", "UCC", "UCA", "UCG"};
11+
private static final String[] TYROSINE = {"UAU", "UAC"};
12+
private static final String[] CYSTEINE = {"UGU", "UGC"};
13+
private static final String[] TRYPTOPHAN = {"UGG"};
14+
private static final String[] STOP = {"UAA", "UAG", "UGA"};
15+
16+
List<String> translate(String rnaSequence) {
17+
18+
List<String> proteinList = new ArrayList<String>();
19+
20+
for (int i = 0; i < rnaSequence.length(); i += 3) {
21+
String codon = rnaSequence.substring(i, i+3);
22+
23+
if (Arrays.asList(METHIONINE).contains(codon)) {
24+
proteinList.add("Methionine");
25+
} else if (Arrays.asList(PHENYLALANINE).contains(codon)) {
26+
proteinList.add("Phenylalanine");
27+
} else if (Arrays.asList(LEUCINE).contains(codon)) {
28+
proteinList.add("Leucine");
29+
} else if (Arrays.asList(SERINE).contains(codon)) {
30+
proteinList.add("Serine");
31+
} else if (Arrays.asList(TYROSINE).contains(codon)) {
32+
proteinList.add("Tyrosine");
33+
} else if (Arrays.asList(CYSTEINE).contains(codon)) {
34+
proteinList.add("Cysteine");
35+
} else if (Arrays.asList(TRYPTOPHAN).contains(codon)) {
36+
proteinList.add("Tryptophan");
37+
} else if (Arrays.asList(STOP).contains(codon)) {
38+
break;
39+
} else {
40+
throw new IllegalArgumentException("Invalid input");
41+
}
42+
43+
}
44+
45+
return proteinList;
46+
}
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#Translate RNA sequences into proteins.
2+
3+
RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so:
4+
5+
RNA: `"AUGUUUUCU"` => translates to
6+
7+
Codons: `"AUG", "UUU", "UCU"`
8+
=> which become a polypeptide with the following sequence =>
9+
10+
Protein: `"Methionine", "Phenylalanine", "Serine"`
11+
12+
There are 64 codons which in turn correspond to 20 amino acids; however, all of the codon sequences and resulting amino acids are not important in this exercise. If it works for one codon, the program should work for all of them.
13+
However, feel free to expand the list in the test suite to include them all.
14+
15+
There are also three terminating codons (also known as 'STOP' codons); if any of these codons are encountered (by the ribosome), all translation ends and the protein is terminated.
16+
17+
All subsequent codons after are ignored, like this:
18+
19+
RNA: `"AUGUUUUCUUAAAUG"` =>
20+
21+
Codons: `"AUG", "UUU", "UCU", "UAG", "AUG"` =>
22+
23+
Protein: `"Methionine", "Phenylalanine", "Serine"`
24+
25+
Note the stop codon terminates the translation and the final methionine is not translated into the protein sequence.
26+
27+
Below are the codons and resulting Amino Acids needed for the exercise.
28+
29+
Codon | Protein
30+
:--- | :---
31+
AUG | Methionine
32+
UUU, UUC | Phenylalanine
33+
UUA, UUG | Leucine
34+
UCU, UCC, UCA, UCG | Serine
35+
UAU, UAC | Tyrosine
36+
UGU, UGC | Cysteine
37+
UGG | Tryptophan
38+
UAA, UAG, UGA | STOP
39+
40+
Learn more about [protein translation on Wikipedia](http://en.wikipedia.org/wiki/Translation_(biology))
41+
42+
# Running the tests
43+
44+
You can run all the tests for an exercise by entering
45+
46+
```sh
47+
$ gradle test
48+
```
49+
50+
in your terminal.
51+
52+
## Source
53+
54+
Tyler Long
55+
56+
## Submitting Incomplete Solutions
57+
58+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import java.util.List;
2+
3+
class ProteinTranslator {
4+
5+
List<String> translate(String rnaSequence) {
6+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
7+
}
8+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import org.junit.Before;
2+
import org.junit.Ignore;
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class ProteinTranslatorTest {
11+
12+
private ProteinTranslator proteinTranslator;
13+
14+
@Before
15+
public void setUp() {
16+
proteinTranslator = new ProteinTranslator();
17+
}
18+
19+
@Test
20+
public void testMethionineRnaSequence() {
21+
List<String> expected = Arrays.asList("Methionine");
22+
assertEquals(expected, proteinTranslator.translate("AUG"));
23+
}
24+
25+
@Ignore("Remove to run test")
26+
@Test
27+
public void testPhenylalanineRnaSequence1() {
28+
List<String> expected = Arrays.asList("Phenylalanine");
29+
assertEquals(expected, proteinTranslator.translate("UUU"));
30+
}
31+
32+
@Ignore("Remove to run test")
33+
@Test
34+
public void testPhenylalanineRnaSequence2() {
35+
List<String> expected = Arrays.asList("Phenylalanine");
36+
assertEquals(expected, proteinTranslator.translate("UUC"));
37+
}
38+
39+
@Ignore("Remove to run test")
40+
@Test
41+
public void testLeucineRnaSequence1() {
42+
List<String> expected = Arrays.asList("Leucine");
43+
assertEquals(expected, proteinTranslator.translate("UUA"));
44+
}
45+
46+
@Ignore("Remove to run test")
47+
@Test
48+
public void testLeucineRnaSequence2() {
49+
List<String> expected = Arrays.asList("Leucine");
50+
assertEquals(expected, proteinTranslator.translate("UUG"));
51+
}
52+
53+
@Ignore("Remove to run test")
54+
@Test
55+
public void testSerineRnaSequence1() {
56+
List<String> expected = Arrays.asList("Serine");
57+
assertEquals(expected, proteinTranslator.translate("UCU"));
58+
}
59+
60+
@Ignore("Remove to run test")
61+
@Test
62+
public void testSerineRnaSequence2() {
63+
List<String> expected = Arrays.asList("Serine");
64+
assertEquals(expected, proteinTranslator.translate("UCC"));
65+
}
66+
67+
@Ignore("Remove to run test")
68+
@Test
69+
public void testSerineRnaSequence3() {
70+
List<String> expected = Arrays.asList("Serine");
71+
assertEquals(expected, proteinTranslator.translate("UCA"));
72+
}
73+
74+
@Ignore("Remove to run test")
75+
@Test
76+
public void testSerineRnaSequence4() {
77+
List<String> expected = Arrays.asList("Serine");
78+
assertEquals(expected, proteinTranslator.translate("UCG"));
79+
}
80+
81+
@Ignore("Remove to run test")
82+
@Test
83+
public void testTyrosineRnaSequence1() {
84+
List<String> expected = Arrays.asList("Tyrosine");
85+
assertEquals(expected, proteinTranslator.translate("UAU"));
86+
}
87+
88+
@Ignore("Remove to run test")
89+
@Test
90+
public void testTyrosineRnaSequence2() {
91+
List<String> expected = Arrays.asList("Tyrosine");
92+
assertEquals(expected, proteinTranslator.translate("UAC"));
93+
}
94+
95+
@Ignore("Remove to run test")
96+
@Test
97+
public void testCysteineRnaSequence1() {
98+
List<String> expected = Arrays.asList("Cysteine");
99+
assertEquals(expected, proteinTranslator.translate("UGU"));
100+
}
101+
102+
@Ignore("Remove to run test")
103+
@Test
104+
public void testCysteineRnaSequence2() {
105+
List<String> expected = Arrays.asList("Cysteine");
106+
assertEquals(expected, proteinTranslator.translate("UGC"));
107+
}
108+
109+
@Ignore("Remove to run test")
110+
@Test
111+
public void testTryptophanRnaSequence1() {
112+
List<String> expected = Arrays.asList("Tryptophan");
113+
assertEquals(expected, proteinTranslator.translate("UGG"));
114+
}
115+
116+
@Ignore("Remove to run test")
117+
@Test
118+
public void testStopRnaSequence1() {
119+
List<String> expected = Arrays.asList();
120+
assertEquals(expected, proteinTranslator.translate("UAA"));
121+
}
122+
123+
@Ignore("Remove to run test")
124+
@Test
125+
public void testStopRnaSequence2() {
126+
List<String> expected = Arrays.asList();
127+
assertEquals(expected, proteinTranslator.translate("UAG"));
128+
}
129+
130+
@Ignore("Remove to run test")
131+
@Test
132+
public void testStopRnaSequence3() {
133+
List<String> expected = Arrays.asList();
134+
assertEquals(expected, proteinTranslator.translate("UGA"));
135+
}
136+
137+
@Ignore("Remove to run test")
138+
@Test
139+
public void testTranslationOfRnaToProteinList() {
140+
List<String> expected = Arrays.asList("Methionine","Phenylalanine","Tryptophan");
141+
assertEquals(expected, proteinTranslator.translate("AUGUUUUGG"));
142+
}
143+
144+
@Ignore("Remove to run test")
145+
@Test
146+
public void testTranslationStopsIfStopCodonAtBeginning() {
147+
List<String> expected = Arrays.asList();
148+
assertEquals(expected, proteinTranslator.translate("UAGUGG"));
149+
}
150+
151+
@Ignore("Remove to run test")
152+
@Test
153+
public void testTranslationStopsIfStopCodonAtEnd1() {
154+
List<String> expected = Arrays.asList("Tryptophan");
155+
assertEquals(expected, proteinTranslator.translate("UGGUAG"));
156+
}
157+
158+
@Ignore("Remove to run test")
159+
@Test
160+
public void testTranslationStopsIfStopCodonAtEnd2() {
161+
List<String> expected = Arrays.asList("Methionine", "Phenylalanine");
162+
assertEquals(expected, proteinTranslator.translate("AUGUUUUAA"));
163+
}
164+
165+
@Ignore("Remove to run test")
166+
@Test
167+
public void testTranslationStopsIfStopCodonInMiddle1() {
168+
List<String> expected = Arrays.asList("Tryptophan");
169+
assertEquals(expected, proteinTranslator.translate("UGGUAGUGG"));
170+
}
171+
172+
@Ignore("Remove to run test")
173+
@Test
174+
public void testTranslationStopsIfStopCodonInMiddle2() {
175+
List<String> expected = Arrays.asList("Tryptophan","Cysteine","Tyrosine");
176+
assertEquals(expected, proteinTranslator.translate("UGGUGUUAUUAAUGGUUU"));
177+
}
178+
179+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ include 'phone-number'
5353
include 'pig-latin'
5454
include 'poker'
5555
include 'prime-factors'
56+
include 'protein-translation'
5657
include 'pythagorean-triplet'
5758
include 'queen-attack'
5859
include 'raindrops'

0 commit comments

Comments
 (0)