๐Ÿ“˜ DNA-to-RNA transcription using Perl 6

๐Ÿ“˜ DNA-to-RNA transcription using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

Convert the given DNA sequence to a compliment RNA.

Weโ€™ll not dig deep into the biology aspect of the problem. For us, it is important that the DNA is a string containing the four letters A, C, G, and T, and the RNA is a string of A, C, G, and U. The transformation from DNA to RNA happens according to the following table:


In the Str class in Perl 6, the trans method is defined; it takes pairs โ€˜old characterโ€”new characterโ€™. So, to translate from DNA to RNA, letโ€™s write the above table as a hash and use it with the transmethod:

my %transcription =
    A => 'U', C => 'G', G => 'C', T => 'A';

my $dna = 'ACCATCAGTC';
my $rna = $dna.trans(%transcription);
say $rna; # UGGUAGUCAG

The trans method accepts a few attributes that alternate its behaviour. For example, the :squash attribute removes the duplicated characters. This probably does not make sense in biology, but works for us an exercise:

say $dna.trans(%transcription, :squash); # UGUAGUCAG

It is also possible to replace the sequences of more than one character. For example:

say $dna.trans('ACCA' => 'UGGU', 'TCA' => 'AGU',
               'GTC' => 'CAG');

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code