Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to RNA Transcription in the PHP Track

Instructions
Test suite
Solution

Given a DNA strand, return its RNA complement (per RNA transcription).

Both DNA and RNA strands are a sequence of nucleotides.

The four nucleotides found in DNA are adenine (A), cytosine (C), guanine (G) and thymine (T).

The four nucleotides found in RNA are adenine (A), cytosine (C), guanine (G) and uracil (U).

Given a DNA strand, its transcribed RNA strand is formed by replacing each nucleotide with its complement:

  • G -> C
  • C -> G
  • T -> A
  • A -> U

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
    
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
    
  3. Execute the tests:

     % ./phpunit.phar rna-transcription/rna-transcription_test.php
    

Source

Rosalind http://rosalind.info/problems/rna

Submitting Incomplete Solutions

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

rna-transcription_test.php

<?php

require "rna-transcription.php";

class ComplementTest extends PHPUnit\Framework\TestCase
{
    public function testTranscribesGuanineToCytosine()
    {
        $this->assertSame('G', toRna('C'));
    }

    public function testTranscribesCytosineToGuanine()
    {
        $this->markTestSkipped();
        $this->assertSame('C', toRna('G'));
    }

    public function testTranscribesThymineToAdenine()
    {
        $this->markTestSkipped();
        $this->assertSame('A', toRna('T'));
    }

    public function testTranscribesAdenineToUracil()
    {
        $this->markTestSkipped();
        $this->assertSame('U', toRna('A'));
    }

    public function testTranscribesAllOccurencesOne()
    {
        $this->markTestSkipped();
        $this->assertSame('UGCACCAGAAUU', toRna('ACGTGGTCTTAA'));
    }
}
<?php

function toRna($dna)
{
    static $nucleotide_complements = [
        'G' => 'C',
        'C' => 'G',
        'T' => 'A',
        'A' => 'U',
    ];
    if (!preg_match('/[GCTA]+/', $dna)) {
        throw new InvalidArgumentException('Invalid DNA strand, only G, C, T, A characters allowed.');
    }
    return strtr($dna, $nucleotide_complements);
}

What can you learn from this solution?

A huge amount can be learnt 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 I could read more about to develop my understanding?