Skip to content

Commit

Permalink
Sync tests for practice exercise protein translation (#2617)
Browse files Browse the repository at this point in the history
  • Loading branch information
manumafe98 authored Jan 2, 2024
1 parent 0081676 commit 816a934
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

class ProteinTranslator {

private static final String[] METHIONINE = {"AUG"};
private static final String[] PHENYLALANINE = {"UUU", "UUC"};
private static final String[] LEUCINE = {"UUA", "UUG"};
private static final String[] SERINE = {"UCU", "UCC", "UCA", "UCG"};
private static final String[] TYROSINE = {"UAU", "UAC"};
private static final String[] CYSTEINE = {"UGU", "UGC"};
private static final String[] TRYPTOPHAN = {"UGG"};
private static final String[] STOP = {"UAA", "UAG", "UGA"};
private final Map<String, String> aminoAcidsMap = new HashMap<>() {
{
put("AUG", "Methionine");
put("UUU", "Phenylalanine");
put("UUC", "Phenylalanine");
put("UUA", "Leucine");
put("UUG", "Leucine");
put("UCU", "Serine");
put("UCC", "Serine");
put("UCA", "Serine");
put("UCG", "Serine");
put("UAU", "Tyrosine");
put("UAC", "Tyrosine");
put("UGU", "Cysteine");
put("UGC", "Cysteine");
put("UGG", "Tryptophan");
put("UAA", "STOP");
put("UAG", "STOP");
put("UGA", "STOP");
}
};

List<String> translate(String rnaSequence) {
List<String> proteinList = new ArrayList<>();

List<String> proteinList = new ArrayList<String>();
if (rnaSequence.length() % 3 != 0
&& !(rnaSequence.contains("UAA") || rnaSequence.contains("UAG") || rnaSequence.contains("UGA"))) {
throw new IllegalArgumentException("Invalid codon");
}

for (int i = 0; i < rnaSequence.length(); i += 3) {
String codon = rnaSequence.substring(i, i + 3);

if (Arrays.asList(METHIONINE).contains(codon)) {
proteinList.add("Methionine");
} else if (Arrays.asList(PHENYLALANINE).contains(codon)) {
proteinList.add("Phenylalanine");
} else if (Arrays.asList(LEUCINE).contains(codon)) {
proteinList.add("Leucine");
} else if (Arrays.asList(SERINE).contains(codon)) {
proteinList.add("Serine");
} else if (Arrays.asList(TYROSINE).contains(codon)) {
proteinList.add("Tyrosine");
} else if (Arrays.asList(CYSTEINE).contains(codon)) {
proteinList.add("Cysteine");
} else if (Arrays.asList(TRYPTOPHAN).contains(codon)) {
proteinList.add("Tryptophan");
} else if (Arrays.asList(STOP).contains(codon)) {
break;
String currentSequence = rnaSequence.substring(i, i + 3);

if (aminoAcidsMap.containsKey(currentSequence)) {
if (aminoAcidsMap.get(currentSequence).equals("STOP")) {
return proteinList;
}

proteinList.add(aminoAcidsMap.get(currentSequence));
} else {
throw new IllegalArgumentException("Invalid input");
throw new IllegalArgumentException("Invalid codon");
}

}

return proteinList;
Expand Down
34 changes: 31 additions & 3 deletions exercises/practice/protein-translation/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[2c44f7bf-ba20-43f7-a3bf-f2219c0c3f98]
description = "Empty RNA sequence results in no proteins"

[96d3d44f-34a2-4db4-84cd-fff523e069be]
description = "Methionine RNA sequence"
Expand Down Expand Up @@ -53,6 +63,12 @@ description = "STOP codon RNA sequence 2"
[9c2ad527-ebc9-4ace-808b-2b6447cb54cb]
description = "STOP codon RNA sequence 3"

[f4d9d8ee-00a8-47bf-a1e3-1641d4428e54]
description = "Sequence of two protein codons translates into proteins"

[dd22eef3-b4f1-4ad6-bb0b-27093c090a9d]
description = "Sequence of two different protein codons translates into proteins"

[d0f295df-fb70-425c-946c-ec2ec185388e]
description = "Translate RNA strand into correct protein list"

Expand All @@ -70,3 +86,15 @@ description = "Translation stops if STOP codon in middle of three-codon sequence

[2c2a2a60-401f-4a80-b977-e0715b23b93d]
description = "Translation stops if STOP codon in middle of six-codon sequence"

[1e75ea2a-f907-4994-ae5c-118632a1cb0f]
description = "Non-existing codon can't translate"

[9eac93f3-627a-4c90-8653-6d0a0595bc6f]
description = "Unknown amino acids, not part of a codon, can't translate"

[9d73899f-e68e-4291-b1e2-7bf87c00f024]
description = "Incomplete RNA sequence can't translate"

[43945cf7-9968-402d-ab9f-b8a28750b050]
description = "Incomplete RNA sequence can translate if valid until a STOP codon"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class ProteinTranslatorTest {

Expand All @@ -13,6 +14,12 @@ public void setUp() {
proteinTranslator = new ProteinTranslator();
}

@Test
public void testEmptyRnaSequenceResultInNoproteins() {
assertThat(proteinTranslator.translate("")).isEmpty();
}

@Ignore("Remove to run test")
@Test
public void testMethionineRnaSequence() {
assertThat(proteinTranslator.translate("AUG")).containsExactly("Methionine");
Expand Down Expand Up @@ -114,11 +121,23 @@ public void testStopRnaSequence3() {
assertThat(proteinTranslator.translate("UGA")).isEmpty();
}

@Ignore("Remove to run test")
@Test
public void testSequenceOfTwoProteinCodonsTranslatesIntoProteins() {
assertThat(proteinTranslator.translate("UUUUUU")).containsExactly("Phenylalanine", "Phenylalanine");
}

@Ignore("Remove to run test")
@Test
public void testSequenceOfTwoDifferentProteinCodonsTranslatesIntoProteins() {
assertThat(proteinTranslator.translate("UUAUUG")).containsExactly("Leucine", "Leucine");
}

@Ignore("Remove to run test")
@Test
public void testTranslationOfRnaToProteinList() {
assertThat(proteinTranslator.translate("AUGUUUUGG"))
.containsExactly("Methionine", "Phenylalanine", "Tryptophan");
.containsExactly("Methionine", "Phenylalanine", "Tryptophan");
}

@Ignore("Remove to run test")
Expand Down Expand Up @@ -149,7 +168,36 @@ public void testTranslationStopsIfStopCodonInMiddle1() {
@Test
public void testTranslationStopsIfStopCodonInMiddle2() {
assertThat(proteinTranslator.translate("UGGUGUUAUUAAUGGUUU"))
.containsExactly("Tryptophan", "Cysteine", "Tyrosine");
.containsExactly("Tryptophan", "Cysteine", "Tyrosine");
}

@Ignore("Remove to run test")
@Test
public void testNonExistingCodonCantTranslate() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> proteinTranslator.translate("AAA"))
.withMessage("Invalid codon");
}

@Ignore("Remove to run test")
@Test
public void testUnknownAminoAcidsNotPartOfACodonCantTranslate() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> proteinTranslator.translate("XYZ"))
.withMessage("Invalid codon");
}

@Ignore("Remove to run test")
@Test
public void testIncompleteRnaSequenceCantTranslate() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> proteinTranslator.translate("AUGU"))
.withMessage("Invalid codon");
}

@Ignore("Remove to run test")
@Test
public void testIncompleteRnaSequenceCanTranslateIfValidUntilAStopCodon() {
assertThat(proteinTranslator.translate("UUCUUCUAAUGGU")).containsExactly("Phenylalanine", "Phenylalanine");
}
}

0 comments on commit 816a934

Please sign in to comment.