Skip to content

Commit 8da517c

Browse files
author
L. Grondin
committed
(rosalind) SUFF
1 parent ac897af commit 8da517c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

rosalind/suff-grondilu.pl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use v6;
2+
3+
my $dna = get;
4+
sub suffixes(Str $str) { map *.flip, [\~] $str.flip.comb }
5+
sub suffix-tree(@a) {
6+
if (@a == 0) { [] }
7+
elsif (@a == 1) { return hash @a[0] => [] }
8+
else {
9+
return hash gather for @a.classify(*.substr(0, 1)) {
10+
my $subtree = suffix-tree([ grep *.chars, map *.substr(1), .value[] ]);
11+
if $subtree.elems == 1 {
12+
my $pair = $subtree.pick;
13+
take .key ~ $pair.key => $pair.value;
14+
} else {
15+
take .key => $subtree;
16+
}
17+
}
18+
}
19+
}
20+
21+
sub show-edges($tree) {
22+
return if $tree.elems == 0;
23+
for $tree[] {
24+
say .key;
25+
show-edges .value;
26+
}
27+
}
28+
29+
my $tree = suffix-tree(suffixes($dna));
30+
31+
show-edges $tree;
32+
33+
34+
# vim: ft=perl6

0 commit comments

Comments
 (0)