AAA => 0
AAC => 1
AAG => 2
AAT => 3
ACA => 4
...
TTT => 63
####
#! /usr/bin/perl -w
use strict;
# build our encoder and decoder
my $offset = 0;
my %encode;
my %decode;
for my $x( qw/A C G T/ ) {
for my $y( qw/A C G T/ ) {
for my $z( qw/A C G T/ ) {
$encode{"$x$y$z"} = $offset;
$decode{$offset} = "$x$y$z";
$offset++;
}
}
}
# encode the string
my $in = shift || 'AAAACTGACCGTTTT';
my $enc = '';
while( $in =~ /(...)/g ) {
$enc .= chr( 48 + $encode{$1} );
}
print "encoded: $enc\n";
# and back again
my $dec = '';
for( split //, $enc ) {
$dec .= $decode{ ord($_) - 48 };
}
print "decoded: $dec\n";
####
#! /usr/bin/perl -w
use strict;
# build our encoder and decoder
my @char = ( 0..9, 'a'..'z', 'A'..'Z', qw/- _/ );
my %encode;
my %decode;
for my $x( qw/A C G T/ ) {
for my $y( qw/A C G T/ ) {
for my $z( qw/A C G T/ ) {
my $ch = shift @char;
$encode{"$x$y$z"} = $ch;
$decode{$ch} = "$x$y$z";
}
}
}
# encode the string
my $in = shift || 'AAAACTGACCGTTTT';
my $enc = '';
my $chunk;
my $chunk_len;
while( defined( $chunk = substr( $in, 0, 3, '' ))) {
$chunk_len = length( $chunk );
last if $chunk_len < 3;
$enc .= $encode{$chunk};
}
# deal with leftover chunk
if( $chunk_len ) {
my $pad = 3 - $chunk_len;
$enc = $pad . $enc . $encode{$chunk . 'A' x $pad};
}
else {
$enc = "0$enc";
}
print "encoded: $enc\n";
# and back again
my $dec = '';
my $pad = substr( $enc, 0, 1, '' );
$dec .= $decode{$_} for split //, $enc;
$dec = substr( $dec, 0, -$pad ) if $pad;
print "decoded: $dec\n";