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

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s