-
Notifications
You must be signed in to change notification settings - Fork 9
/
gencode
71 lines (53 loc) · 1.28 KB
/
gencode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
=head1 NAME
gencode - encode your file as base pairs
=cut
my @bases = qw(A C G T);
sub ncode {
my ($n, @chars) = @_;
my $s = '';
for (3,2,1,0) {
$s .= $chars [ int($n / @chars**$_) ];
$n %= @chars**$_ if $_;
}
return $s;
}
sub dcode {
my ($s, @chars) = @_;
my %chars = map { $chars[$_] => $_ } (0 .. $#chars);
die "invalid quad $s" if length $s > 4;
my @digits = split //, $s;
my $n = 0;
for (0 .. 3) {
$n += $chars{$digits[$_]} * (@chars ** (3-$_));
}
return $n;
}
sub ncode_file {
my ($filename) = @_;
open my $fh, "<", $filename
or die "can't open input file: $!";
my $string = join '',
map { ncode(ord($_), @bases) }
map { split //, $_ } <$fh>;
print "$_\n" for chunk_by_n(60, $string);
}
sub chunk_by_n {
my ($n, $string) = @_;
my @chunks;
while ($string =~ /(.{$n})/og) { push @chunks, $1 }
return @chunks;
}
sub dcode_file {
my ($filename) = @_;
open my $fh, "<", $filename
or die "can't open input file: $!";
for my $line (<$fh>) {
chomp $line;
print chr dcode($_, @bases) for chunk_by_n(4, $line)
}
}
my ($opt, $filename) = @_;
if ($opt eq '-e') { ncode_file($filename); exit; }
if ($opt eq '-d') { dcode_file($filename); exit; }
else { print "usage: gencode [ -e | -d ] filename\n"; exit; }