-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the protein-translation exercise.
- Loading branch information
1 parent
cd1ca26
commit 37f36b1
Showing
6 changed files
with
377 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
exercises/practice/protein-translation/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Instructions | ||
|
||
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](http://en.wikipedia.org/wiki/Translation_(biology)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"blurb": "Translate RNA sequences into proteins.", | ||
"authors": ["MichaelBunker"], | ||
"contributors": [], | ||
"files": { | ||
"solution": ["ProteinTranslation.php"], | ||
"test": ["ProteinTranslationTest.php"], | ||
"example": [".meta/example.php"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
class ProteinTranslation | ||
{ | ||
public static function getProteins(string $rnaSequence): array | ||
{ | ||
if (!$rnaSequence) { | ||
return []; | ||
} | ||
|
||
$translatedProteins = []; | ||
|
||
foreach (str_split($rnaSequence, 3) as $rna) { | ||
$protein = static::translateRna($rna); | ||
|
||
if (!$protein) { | ||
throw new InvalidArgumentException('Invalid codon'); | ||
} | ||
|
||
if ($protein === 'STOP') { | ||
break; | ||
} | ||
|
||
$translatedProteins[] = $protein; | ||
} | ||
|
||
return $translatedProteins; | ||
} | ||
|
||
private static function translateRna(string $rna): ?string | ||
{ | ||
switch ($rna) { | ||
case 'AUG': | ||
return 'Methionine'; | ||
case 'UUU': | ||
case 'UUC': | ||
return 'Phenylalanine'; | ||
case 'UUA': | ||
case 'UUG': | ||
return 'Leucine'; | ||
case 'UCU': | ||
case 'UCC': | ||
case 'UCA': | ||
case 'UCG': | ||
return 'Serine'; | ||
case 'UAU': | ||
case 'UAC': | ||
return 'Tyrosine'; | ||
case 'UGU': | ||
case 'UGC': | ||
return 'Cysteine'; | ||
case 'UGG': | ||
return 'Tryptophan'; | ||
case 'UAA': | ||
case 'UAG': | ||
case 'UGA': | ||
return 'STOP'; | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
exercises/practice/protein-translation/ProteinTranslation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
class ProteinTranslation | ||
{ | ||
public static function getProteins() | ||
{ | ||
throw new \Exception(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
} |
193 changes: 193 additions & 0 deletions
193
exercises/practice/protein-translation/ProteinTranslationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
class ProteinTranslationTest extends PHPUnit\Framework\TestCase | ||
{ | ||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once 'ProteinTranslation.php'; | ||
} | ||
|
||
public function testEmptyRnaSequence(): void | ||
{ | ||
$this->assertEquals([], ProteinTranslation::getProteins('')); | ||
} | ||
|
||
public function testMethionineRnaSequence(): void | ||
{ | ||
$this->assertEquals(['Methionine'], ProteinTranslation::getProteins('AUG')); | ||
} | ||
|
||
public function testPhenylalanineRnaSequenceOne(): void | ||
{ | ||
$this->assertEquals(['Phenylalanine'], ProteinTranslation::getProteins('UUU')); | ||
} | ||
|
||
public function testPhenylalanineRnaSequenceTwo(): void | ||
{ | ||
$this->assertEquals(['Phenylalanine'], ProteinTranslation::getProteins('UUC')); | ||
} | ||
|
||
public function testLeucineRnaSequenceOne(): void | ||
{ | ||
$this->assertEquals(['Leucine'], ProteinTranslation::getProteins('UUA')); | ||
} | ||
|
||
public function testLeucineRnaSequenceTwo(): void | ||
{ | ||
$this->assertEquals(['Leucine'], ProteinTranslation::getProteins('UUG')); | ||
} | ||
|
||
public function testSerineRnaSequenceOne(): void | ||
{ | ||
$this->assertEquals(['Serine'], ProteinTranslation::getProteins('UCU')); | ||
} | ||
|
||
public function testSerineRnaSequenceTwo(): void | ||
{ | ||
$this->assertEquals(['Serine'], ProteinTranslation::getProteins('UCC')); | ||
} | ||
|
||
public function testSerineRnaSequenceThree(): void | ||
{ | ||
$this->assertEquals(['Serine'], ProteinTranslation::getProteins('UCA')); | ||
} | ||
|
||
public function testSerineRnaSequenceFour(): void | ||
{ | ||
$this->assertEquals(['Serine'], ProteinTranslation::getProteins('UCG')); | ||
} | ||
|
||
public function testTyrosineRnaSequenceOne(): void | ||
{ | ||
$this->assertEquals(['Tyrosine'], ProteinTranslation::getProteins('UAU')); | ||
} | ||
|
||
public function testTyrosineRnaSequenceTwo(): void | ||
{ | ||
$this->assertEquals(['Tyrosine'], ProteinTranslation::getProteins('UAC')); | ||
} | ||
|
||
public function testCysteineRnaSequenceOne(): void | ||
{ | ||
$this->assertEquals(['Cysteine'], ProteinTranslation::getProteins('UGU')); | ||
} | ||
|
||
public function testCysteineRnaSequenceTwo(): void | ||
{ | ||
$this->assertEquals(['Cysteine'], ProteinTranslation::getProteins('UGC')); | ||
} | ||
|
||
public function testTryptophanRnaSequence(): void | ||
{ | ||
$this->assertEquals(['Tryptophan'], ProteinTranslation::getProteins('UGG')); | ||
} | ||
|
||
public function testStopCodonRnaSequenceOne(): void | ||
{ | ||
$this->assertEquals([], ProteinTranslation::getProteins('UAA')); | ||
} | ||
|
||
public function testStopCodonRnaSequenceTwo(): void | ||
{ | ||
$this->assertEquals([], ProteinTranslation::getProteins('UAG')); | ||
} | ||
|
||
public function testStopCodonRnaSequenceThree(): void | ||
{ | ||
$this->assertEquals([], ProteinTranslation::getProteins('UGA')); | ||
} | ||
|
||
public function testToCodonsTranslateToProteins(): void | ||
{ | ||
$this->assertEquals(['Phenylalanine', 'Phenylalanine'], ProteinTranslation::getProteins('UUUUUU')); | ||
} | ||
|
||
public function testToDifferentCodonsTranslateToProteins(): void | ||
{ | ||
$this->assertEquals(['Leucine', 'Leucine'], ProteinTranslation::getProteins('UUAUUG')); | ||
} | ||
|
||
public function testTranslateRnaStrandToCorrectProteinList(): void | ||
{ | ||
$this->assertEquals( | ||
['Methionine', 'Phenylalanine', 'Tryptophan'], | ||
ProteinTranslation::getProteins('AUGUUUUGG') | ||
); | ||
} | ||
|
||
public function testTranslationStopsIfStopCodonAtBeginningOfSequence(): void | ||
{ | ||
$this->assertEquals([], ProteinTranslation::getProteins('UAGUGG')); | ||
} | ||
|
||
public function testTranslationStopsIfStopCodonAtEndOfTwoCodonSequence(): void | ||
{ | ||
$this->assertEquals(['Tryptophan'], ProteinTranslation::getProteins('UGGUAG')); | ||
} | ||
|
||
public function testTranslationStopsIfStopCodonAtEndOfThreeCodonSequence(): void | ||
{ | ||
$this->assertEquals(['Methionine', 'Phenylalanine'], ProteinTranslation::getProteins('AUGUUUUAA')); | ||
} | ||
|
||
public function testTranslationStopsIfStopCodonInMiddleOfThreeCodonSequence(): void | ||
{ | ||
$this->assertEquals(['Tryptophan'], ProteinTranslation::getProteins('UGGUAGUGG')); | ||
} | ||
|
||
public function testTranslationStopsIfStopCodonInMiddleOfSixCodonSequence(): void | ||
{ | ||
$this->assertEquals( | ||
['Tryptophan', 'Cysteine', 'Tyrosine'], | ||
ProteinTranslation::getProteins('UGGUGUUAUUAAUGGUUU') | ||
); | ||
} | ||
|
||
public function invalidCodonDataProvider(): array | ||
{ | ||
return [ | ||
'Non-existing' => ['AAA'], | ||
'Unknown' => ['XYZ'], | ||
'Incomplete' => ['AUGU'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider invalidCodonDataProvider | ||
*/ | ||
public function testTranslateFailsForInvalidCodons(string $rna): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Invalid codon'); | ||
ProteinTranslation::getProteins($rna); | ||
} | ||
|
||
public function testTranslatePassesIfStopCodeBeforeIncompleteSequence(): void | ||
{ | ||
$this->assertEquals(['Phenylalanine', 'Phenylalanine'], ProteinTranslation::getProteins('UUCUUCUAAUGGU')); | ||
} | ||
} |