Skip to content

Instantly share code, notes, and snippets.

@MattOates
Last active June 25, 2018 17:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattOates/b2ccabc6201daf9ddbd2cba8a29c5b80 to your computer and use it in GitHub Desktop.
Save MattOates/b2ccabc6201daf9ddbd2cba8a29c5b80 to your computer and use it in GitHub Desktop.
Tools to encode and decode files as DNA sequence text assuming 2bit style byte encodings.
#!/usr/bin/env perl6
use v6;
sub MAIN ($file) {
my %bases = (T => '00', C => '01', A => '10', G => '11');
my %bytes = (0..255).map({ sprintf('%08d', $_.base(2)) }) Z=> (0..255);
my $io = $file.IO.open;
while (my $hunk = $io.readchars(4)) {
last unless $hunk.chars == 4;
print %bytes{$hunk.comb.map({%bases{$_}}).join}.chr;
}
}
#!/usr/bin/env perl6
use v6;
sub MAIN ($file) {
my %bases = (T => '00', C => '01', A => '10', G => '11').invert;
my %bytes = (0..255) Z=> (0..255).map({ sprintf('%08d', $_.base(2)) });
my $io = $file.IO.open;
while (my $byte = $io.getc) {
print %bytes{$byte.ord}.comb.rotor(2).map({ %bases{$_.join} }).join;
}
}
@MattOates
Copy link
Author

An example run of the tools

$ perl6 dnaencode.p6 <(echo "hello world")
CAATCACCCAGTCAGTCAGGTATTCGCGCAGGCGTACAGTCACTTTAA

Then to decode the DNA looking string back to the original plain text

$ perl6 dnadecode.p6 <(echo "CAATCACCCAGTCAGTCAGGTATTCGCGCAGGCGTACAGTCACTTTAA")
hello world

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