Avatar of artemkorsakov

artemkorsakov's solution

to Protein Translation in the Java Track

Published at Feb 04 2019 · 0 comments
Instructions
Test suite
Solution

Translate RNA sequences into proteins.

RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so:

RNA: "AUGUUUUCU" => translates to

Codons: "AUG", "UUU", "UCU" => which become a polypeptide with the following sequence =>

Protein: "Methionine", "Phenylalanine", "Serine"

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. However, feel free to expand the list in the test suite to include them all.

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.

All subsequent codons after are ignored, like this:

RNA: "AUGUUUUCUUAAAUG" =>

Codons: "AUG", "UUU", "UCU", "UAA", "AUG" =>

Protein: "Methionine", "Phenylalanine", "Serine"

Note the stop codon "UAA" terminates the translation and the final methionine is not translated into the protein sequence.

Below are the codons and resulting Amino Acids needed for the exercise.

Codon Protein
AUG Methionine
UUU, UUC Phenylalanine
UUA, UUG Leucine
UCU, UCC, UCA, UCG Serine
UAU, UAC Tyrosine
UGU, UGC Cysteine
UGG Tryptophan
UAA, UAG, UGA STOP

Learn more about protein translation on Wikipedia

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

Tyler Long

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

ProteinTranslatorTest.java

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class ProteinTranslatorTest {

    private ProteinTranslator proteinTranslator;

    @Before
    public void setUp() {
        proteinTranslator = new ProteinTranslator();
    }

    @Test
    public void testMethionineRnaSequence() {
        List<String> expected = Arrays.asList("Methionine");
        assertEquals(expected, proteinTranslator.translate("AUG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testPhenylalanineRnaSequence1() {
        List<String> expected = Arrays.asList("Phenylalanine");
        assertEquals(expected, proteinTranslator.translate("UUU"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testPhenylalanineRnaSequence2() {
        List<String> expected = Arrays.asList("Phenylalanine");
        assertEquals(expected, proteinTranslator.translate("UUC"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testLeucineRnaSequence1() {
        List<String> expected = Arrays.asList("Leucine");
        assertEquals(expected, proteinTranslator.translate("UUA"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testLeucineRnaSequence2() {
        List<String> expected = Arrays.asList("Leucine");
        assertEquals(expected, proteinTranslator.translate("UUG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSerineRnaSequence1() {
        List<String> expected = Arrays.asList("Serine");
        assertEquals(expected, proteinTranslator.translate("UCU"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSerineRnaSequence2() {
        List<String> expected = Arrays.asList("Serine");
        assertEquals(expected, proteinTranslator.translate("UCC"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSerineRnaSequence3() {
        List<String> expected = Arrays.asList("Serine");
        assertEquals(expected, proteinTranslator.translate("UCA"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSerineRnaSequence4() {
        List<String> expected = Arrays.asList("Serine");
        assertEquals(expected, proteinTranslator.translate("UCG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTyrosineRnaSequence1() {
        List<String> expected = Arrays.asList("Tyrosine");
        assertEquals(expected, proteinTranslator.translate("UAU"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTyrosineRnaSequence2() {
        List<String> expected = Arrays.asList("Tyrosine");
        assertEquals(expected, proteinTranslator.translate("UAC"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testCysteineRnaSequence1() {
        List<String> expected = Arrays.asList("Cysteine");
        assertEquals(expected, proteinTranslator.translate("UGU"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testCysteineRnaSequence2() {
        List<String> expected = Arrays.asList("Cysteine");
        assertEquals(expected, proteinTranslator.translate("UGC"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTryptophanRnaSequence1() {
        List<String> expected = Arrays.asList("Tryptophan");
        assertEquals(expected, proteinTranslator.translate("UGG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testStopRnaSequence1() {
        List<String> expected = Arrays.asList();
        assertEquals(expected, proteinTranslator.translate("UAA"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testStopRnaSequence2() {
        List<String> expected = Arrays.asList();
        assertEquals(expected, proteinTranslator.translate("UAG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testStopRnaSequence3() {
        List<String> expected = Arrays.asList();
        assertEquals(expected, proteinTranslator.translate("UGA"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTranslationOfRnaToProteinList() {
        List<String> expected = Arrays.asList("Methionine", "Phenylalanine", "Tryptophan");
        assertEquals(expected, proteinTranslator.translate("AUGUUUUGG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTranslationStopsIfStopCodonAtBeginning() {
        List<String> expected = Arrays.asList();
        assertEquals(expected, proteinTranslator.translate("UAGUGG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTranslationStopsIfStopCodonAtEnd1() {
        List<String> expected = Arrays.asList("Tryptophan");
        assertEquals(expected, proteinTranslator.translate("UGGUAG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTranslationStopsIfStopCodonAtEnd2() {
        List<String> expected = Arrays.asList("Methionine", "Phenylalanine");
        assertEquals(expected, proteinTranslator.translate("AUGUUUUAA"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTranslationStopsIfStopCodonInMiddle1() {
        List<String> expected = Arrays.asList("Tryptophan");
        assertEquals(expected, proteinTranslator.translate("UGGUAGUGG"));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTranslationStopsIfStopCodonInMiddle2() {
        List<String> expected = Arrays.asList("Tryptophan", "Cysteine", "Tyrosine");
        assertEquals(expected, proteinTranslator.translate("UGGUGUUAUUAAUGGUUU"));
    }

}
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

class ProteinTranslator {
    private static final Pattern STOP_PATTERN = Pattern.compile("^(UAA|UAG|UGA)$");
    private static final Pattern METHIONINE_PATTERN = Pattern.compile("^AUG$");
    private static final Pattern PHENYLALANINE_PATTERN = Pattern.compile("^(UUU|UUC)$");
    private static final Pattern LEUCINE_PATTERN = Pattern.compile("^(UUA|UUG)$");
    private static final Pattern SERINE_PATTERN = Pattern.compile("^(UCU|UCC|UCA|UCG)$");
    private static final Pattern TYROSINE_PATTERN = Pattern.compile("^(UAU|UAC)$");
    private static final Pattern CYSTEINE_PATTERN = Pattern.compile("^(UGU|UGC)$");
    private static final Pattern TRYPTOPHAN_PATTERN = Pattern.compile("^(UGG)$");

    List<String> translate(String rnaSequence) {
        List<String> result = new ArrayList<>();
        for (int i = 0; i < (rnaSequence.length() / 3); i++) {
            String rna = rnaSequence.substring(3 * i, 3 * i + 3);
            if (STOP_PATTERN.matcher(rna).find()) {
                return result;
            }
            if (METHIONINE_PATTERN.matcher(rna).find()) {
                result.add("Methionine");
            }
            if (PHENYLALANINE_PATTERN.matcher(rna).find()) {
                result.add("Phenylalanine");
            }
            if (LEUCINE_PATTERN.matcher(rna).find()) {
                result.add("Leucine");
            }
            if (SERINE_PATTERN.matcher(rna).find()) {
                result.add("Serine");
            }
            if (TYROSINE_PATTERN.matcher(rna).find()) {
                result.add("Tyrosine");
            }
            if (CYSTEINE_PATTERN.matcher(rna).find()) {
                result.add("Cysteine");
            }
            if (TRYPTOPHAN_PATTERN.matcher(rna).find()) {
                result.add("Tryptophan");
            }
        }
        return result;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?