Adding protein-translation to Raku stream

Hello,

would it make sense to add the task protein-translation (as it is featured in the very good 48in24 thing) to the raku track? Here a first proposal for the solution.

my class X::InvalidCodon is Exception {
    method message {
        'Invalid codon'
    }
}

our %PROTEIN is default(X::InvalidCodon) = {
    Methionine => <AUG>,
    Phenylalanine => <UUU UUC>,
    Leucine => <UUA UUG>,
    Serine => <UCU UCC UCA UCG>,
    Tyrosine => <UAU UAC>,
    Cysteine => <UGU UGC>,
    Tryptophan => <UGG>,
    STOP => <UAA UAG UGA> }.invert;

sub protein_translation(Str $rna --> Seq) {
    $rna.comb(3).map: { given %PROTEIN{$_} {
        when 'STOP' { last }
        when X::InvalidCodon { $_.new.throw }
        default { $_ or [] }
    }}
}

use Test;

throws-like protein_translation("AAA"), X::InvalidCodon;
throws-like protein_translation("AUGU"), X::InvalidCodon;

my @test-data =
        AUG => <Methionine>,
        UUU => <Phenylalanine>,
        UUC => <Phenylalanine>,
        UUA => <Leucine>,
        UUG => <Leucine>,
        UCU => <Serine>,
        UCC => <Serine>,
        UCA => <Serine>,
        UCG => <Serine>,
        UAU => <Tyrosine>,
        UAC => <Tyrosine>,
        UGU => <Cysteine>,
        UGC => <Cysteine>,
        UGG => <Tryptophan>,
        UAA => [],
        UAG => [],
        UGA => [],
        UUUUUU => <Phenylalanine Phenylalanine>,
        UUAUUG => <Leucine Leucine>,
        AUGUUUUGG => <Methionine Phenylalanine Tryptophan>,
        UAGUGG => [],
        UGGUAG => <Tryptophan>,
        AUGUUUUAA => <Methionine Phenylalanine>,
        UGGUAGUGG => <Tryptophan>,
        UGGUGUUAUUAAUGGUUU => <Tryptophan Cysteine Tyrosine>;

is protein_translation(.key), .value for @test-data;
done-testing

Ping @m-dango :grinning:

Please could you encourage @rcmlz over the line ? :pray:

Thanks :+1:

Exercise should now be implemented

2 Likes

Please add is export to example stub ?

===SORRY!=== Error while compiling /mnt/exercism-iteration/t/protein-translation.rakutest
Undeclared routine:
    protein-translation used at lines 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 204, 189, 194, 199

Thanks !

Fixed in Add is export trait to protein-translation stub by m-dango · Pull Request #701 · exercism/raku · GitHub

1 Like

Approved, just needs to be brought in.