-
Notifications
You must be signed in to change notification settings - Fork 33
/
burrows-wheeler_transform.pl
176 lines (139 loc) · 4.29 KB
/
burrows-wheeler_transform.pl
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/perl
# Author: Trizen
# Date: 14 June 2023
# https://github.com/trizen
# Implementation of the Burrows–Wheeler transform, with fast inversion.
# https://rosettacode.org/wiki/Burrows–Wheeler_transform
# References:
# Data Compression (Summer 2023) - Lecture 12 - The Burrows-Wheeler Transform (BWT)
# https://youtube.com/watch?v=rQ7wwh4HRZM
#
# Data Compression (Summer 2023) - Lecture 13 - BZip2
# https://youtube.com/watch?v=cvoZbBZ3M2A
use 5.036;
use constant {
LOOKAHEAD_LEN => 512, # lower values are faster (on average)
};
sub bwt_quadratic ($s) { # O(n^2) space (impractical)
[map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [substr($s, $_) . substr($s, 0, $_), $_] } 0 .. length($s) - 1];
}
sub bwt_simple ($s) { # O(n) space (very slow)
[sort { (substr($s, $a) . substr($s, 0, $a)) cmp(substr($s, $b) . substr($s, 0, $b)) } 0 .. length($s) - 1];
}
sub bwt_cyclic ($s) { # O(n) space (slow)
my @cyclic = split(//, $s);
my $len = scalar(@cyclic);
my $rle = 1;
foreach my $i (1 .. $len - 1) {
if ($cyclic[$i] ne $cyclic[$i - 1]) {
$rle = 0;
last;
}
}
$rle && return [0 .. $len - 1];
[
sort {
my ($i, $j) = ($a, $b);
while ($cyclic[$i] eq $cyclic[$j]) {
$i %= $len if (++$i >= $len);
$j %= $len if (++$j >= $len);
}
$cyclic[$i] cmp $cyclic[$j];
} 0 .. $len - 1
];
}
sub bwt_lookahead ($s) { # O(n) space (moderately fast)
[
sort {
my $t = substr($s, $a, LOOKAHEAD_LEN);
my $u = substr($s, $b, LOOKAHEAD_LEN);
if (length($t) < LOOKAHEAD_LEN) {
$t .= substr($s, 0, ($a < LOOKAHEAD_LEN) ? $a : (LOOKAHEAD_LEN - length($t)));
}
if (length($u) < LOOKAHEAD_LEN) {
$u .= substr($s, 0, ($b < LOOKAHEAD_LEN) ? $b : (LOOKAHEAD_LEN - length($u)));
}
($t cmp $u) || ((substr($s, $a) . substr($s, 0, $a)) cmp(substr($s, $b) . substr($s, 0, $b)))
} 0 .. length($s) - 1
];
}
sub bwt_balanced ($s) { # O(n * LOOKAHEAD_LEN) space (fast)
#<<<
[
map { $_->[1] } sort {
($a->[0] cmp $b->[0])
|| ((substr($s, $a->[1]) . substr($s, 0, $a->[1])) cmp(substr($s, $b->[1]) . substr($s, 0, $b->[1])))
}
map {
my $t = substr($s, $_, LOOKAHEAD_LEN);
if (length($t) < LOOKAHEAD_LEN) {
$t .= substr($s, 0, ($_ < LOOKAHEAD_LEN) ? $_ : (LOOKAHEAD_LEN - length($t)));
}
[$t, $_]
} 0 .. length($s) - 1
];
#>>>
}
sub bwt_encode ($s) {
#my $bwt = bwt_simple($s);
#my $bwt = bwt_quadratic($s);
#my $bwt = bwt_cyclic($s);
#my $bwt = bwt_lookahead($s);
my $bwt = bwt_balanced($s);
my $ret = join('', map { substr($s, $_ - 1, 1) } @$bwt);
my $idx = 0;
foreach my $i (@$bwt) {
$i || last;
++$idx;
}
return ($ret, $idx);
}
sub bwt_decode ($bwt, $idx) { # fast inversion
my @tail = split(//, $bwt);
my @head = sort @tail;
my %indices;
foreach my $i (0 .. $#tail) {
push @{$indices{$tail[$i]}}, $i;
}
my @table;
foreach my $v (@head) {
push @table, shift(@{$indices{$v}});
}
my $dec = '';
my $i = $idx;
for (1 .. scalar(@head)) {
$dec .= $head[$i];
$i = $table[$i];
}
return $dec;
}
#<<<
my @tests = (
"banana", "appellee", "dogwood", "TOBEORNOTTOBEORTOBEORNOT",
"SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES", "PINEAPPLE",
"","a","aa","aabb","aaaaaaaaaaaa","aaaaaaaaaaaab",
"baaaaaaaaaaaa","aaaaaabaaaaaa","aaaaaaabaaaaa",
);
#>>>
foreach my $file (__FILE__, $^X) {
push @tests, do {
open my $fh, '<:raw', $file;
local $/;
<$fh>;
};
}
foreach my $str (@tests) {
my ($enc, $idx) = bwt_encode($str);
my $dec = bwt_decode($enc, $idx);
if (length($str) < 1024) {
say "BWT($dec) = ($enc, $idx)";
}
$dec eq $str or die "error: <<$dec>> != <<$str>>";
}
__END__
BWT(banana) = (nnbaaa, 3)
BWT(appellee) = (eelplepa, 0)
BWT(dogwood) = (odoodwg, 1)
BWT(TOBEORNOTTOBEORTOBEORNOT) = (OOOBBBRRTTTEEENNOOORTTOO, 20)
BWT(SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES) = (TEXYDST.E.IXIXIXXSSMPPS.B..E.S.EUSFXDIIOIIIT, 29)
BWT(PINEAPPLE) = (ENLPPIEPA, 6)