Avatar of artemkorsakov

artemkorsakov's solution

to Protein Translation in the C# Track

Published at Feb 25 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

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit ProteinTranslation.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Tyler Long

Submitting Incomplete Solutions

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

ProteinTranslationTest.cs

// This file was auto-generated based on version 1.1.1 of the canonical data.

using Xunit;

public class ProteinTranslationTest
{
    [Fact]
    public void Methionine_rna_sequence()
    {
        Assert.Equal(new[] { "Methionine" }, ProteinTranslation.Proteins("AUG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Phenylalanine_rna_sequence_1()
    {
        Assert.Equal(new[] { "Phenylalanine" }, ProteinTranslation.Proteins("UUU"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Phenylalanine_rna_sequence_2()
    {
        Assert.Equal(new[] { "Phenylalanine" }, ProteinTranslation.Proteins("UUC"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Leucine_rna_sequence_1()
    {
        Assert.Equal(new[] { "Leucine" }, ProteinTranslation.Proteins("UUA"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Leucine_rna_sequence_2()
    {
        Assert.Equal(new[] { "Leucine" }, ProteinTranslation.Proteins("UUG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Serine_rna_sequence_1()
    {
        Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCU"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Serine_rna_sequence_2()
    {
        Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCC"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Serine_rna_sequence_3()
    {
        Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCA"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Serine_rna_sequence_4()
    {
        Assert.Equal(new[] { "Serine" }, ProteinTranslation.Proteins("UCG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Tyrosine_rna_sequence_1()
    {
        Assert.Equal(new[] { "Tyrosine" }, ProteinTranslation.Proteins("UAU"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Tyrosine_rna_sequence_2()
    {
        Assert.Equal(new[] { "Tyrosine" }, ProteinTranslation.Proteins("UAC"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Cysteine_rna_sequence_1()
    {
        Assert.Equal(new[] { "Cysteine" }, ProteinTranslation.Proteins("UGU"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Cysteine_rna_sequence_2()
    {
        Assert.Equal(new[] { "Cysteine" }, ProteinTranslation.Proteins("UGC"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Tryptophan_rna_sequence()
    {
        Assert.Equal(new[] { "Tryptophan" }, ProteinTranslation.Proteins("UGG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Stop_codon_rna_sequence_1()
    {
        Assert.Empty(ProteinTranslation.Proteins("UAA"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Stop_codon_rna_sequence_2()
    {
        Assert.Empty(ProteinTranslation.Proteins("UAG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Stop_codon_rna_sequence_3()
    {
        Assert.Empty(ProteinTranslation.Proteins("UGA"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Translate_rna_strand_into_correct_protein_list()
    {
        Assert.Equal(new[] { "Methionine", "Phenylalanine", "Tryptophan" }, ProteinTranslation.Proteins("AUGUUUUGG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Translation_stops_if_stop_codon_at_beginning_of_sequence()
    {
        Assert.Empty(ProteinTranslation.Proteins("UAGUGG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Translation_stops_if_stop_codon_at_end_of_two_codon_sequence()
    {
        Assert.Equal(new[] { "Tryptophan" }, ProteinTranslation.Proteins("UGGUAG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Translation_stops_if_stop_codon_at_end_of_three_codon_sequence()
    {
        Assert.Equal(new[] { "Methionine", "Phenylalanine" }, ProteinTranslation.Proteins("AUGUUUUAA"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Translation_stops_if_stop_codon_in_middle_of_three_codon_sequence()
    {
        Assert.Equal(new[] { "Tryptophan" }, ProteinTranslation.Proteins("UGGUAGUGG"));
    }

    [Fact(Skip = "Remove to run test")]
    public void Translation_stops_if_stop_codon_in_middle_of_six_codon_sequence()
    {
        Assert.Equal(new[] { "Tryptophan", "Cysteine", "Tyrosine" }, ProteinTranslation.Proteins("UGGUGUUAUUAAUGGUUU"));
    }
}
using System.Collections.Generic;
using System.Linq;

public static class ProteinTranslation
{
    private static readonly Dictionary<string, string[]> Codons = new Dictionary<string, string[]>
    {
        {"Methionine" , new []{"AUG"}},
        {"Phenylalanine" , new []{"UUU", "UUC"}},
        {"Leucine" , new []{"UUA", "UUG"}},
        {"Serine" , new []{"UCU", "UCC", "UCA", "UCG"}},
        {"Tyrosine" , new []{"UAU", "UAC"}},
        {"Cysteine" , new []{"UGU", "UGC"}},
        {"Tryptophan" , new []{"UGG"}},
        {"STOP" , new []{"UAA", "UAG", "UGA"}}
    };

    public static string[] Proteins(string strand)
    {
        var result = new List<string>();
        for (var i = 0; i < strand.Length / 3; i++)
        {
            var codon = strand.Substring(3 * i, 3);
            if (IsStop(codon))
            {
                return result.ToArray();
            }

            result.Add(GetProtein(codon));
        }

        return result.ToArray();
    }

    private static bool IsStop(string codon)
    {
        return GetProtein(codon).Equals("STOP");
    }

    private static string GetProtein(string codon)
    {
        return Codons.First(c => c.Value.Contains(codon)).Key;
    }
}

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?