Skip to content

Instantly share code, notes, and snippets.

@MattOates
Last active January 1, 2016 08:19
Show Gist options
  • Save MattOates/8117240 to your computer and use it in GitHub Desktop.
Save MattOates/8117240 to your computer and use it in GitHub Desktop.
Snippet for translating DNA to amino acid sequences, trying to use the most Perl6ish looking code.
use v6;
class BioInfo::Sequence {
has Str $.seq;
has Str @.residues;
submethod BUILD (Str :$seq, Str :@residues) {
die X::BioInfo::UnknownResidue.new(seq => $seq, residues => @residues);
$!seq := $seq;
@!residues := @residues;
}
}
class BioInfo::NucleotideSequence is BioInfo::Sequence {
method translate (:$table='standard') {
#Get all the combinations of bases that map to the @aminos ordering
#my @codons = map *~*~*, (@!bases X @!bases X @!bases);
my @codons = [X~] @.residues.item xx 3;
#Translation table
#TODO add in the weirdy beardy translation tables
my %aminos = {standard => <F F L L S S S S
Y Y * * C C * W
L L L L P P P P
H H Q Q R R R R
I I I M T T T T
N N K K S S R R
V V V V A A A A
D D E E G G G G>};
#Create a map of the codons to amino acids
my %codon_table = zip @codons, %aminos{$table};
#Take all of the codons mapped to aminos and join them together
return %codon_table{map *~*~*, $.seq.uc.comb}.join;
}
}
class BioInfo::DNASequence is BioInfo::NucleotideSequence {
has Str @.residues = ('T','C','A','G');
}
class BioInfo::RNASequence is BioInfo::NucleotideSequence {
has Str @.residues = ('U','C','A','G');
}
class X::BioInfo::UnknownResidue is Exception {
has Str $!seq;
has Str @!residues;
method message {
return "Valid residue " ~ @!residues ~ " was not recognised in " ~ $!seq;
}
}
my $dna = BioInfo::DNASequence.new(seq => 'GTCATAGTCATAGTCATAGTCATA');
my $rna = BioInfo::DNASequence.new(seq => 'GUCAUAGUCAUAGUCAUAGUCAUA');
say $dna.translate;
say $rna.translate;
@grondilu
Copy link

my @codons = [X~] $@!bases xx 3

@grondilu
Copy link

return %codon_table{map *~*~*, $!seq.uc.comb}.join

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment