Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
(rosalind) SUFF
  • Loading branch information
grondilu committed May 10, 2013
1 parent ac897af commit 8da517c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions rosalind/suff-grondilu.pl
@@ -0,0 +1,34 @@
use v6;

my $dna = get;
sub suffixes(Str $str) { map *.flip, [\~] $str.flip.comb }
sub suffix-tree(@a) {
if (@a == 0) { [] }
elsif (@a == 1) { return hash @a[0] => [] }
else {
return hash gather for @a.classify(*.substr(0, 1)) {
my $subtree = suffix-tree([ grep *.chars, map *.substr(1), .value[] ]);
if $subtree.elems == 1 {
my $pair = $subtree.pick;
take .key ~ $pair.key => $pair.value;
} else {
take .key => $subtree;
}
}
}
}

sub show-edges($tree) {
return if $tree.elems == 0;
for $tree[] {
say .key;
show-edges .value;
}
}

my $tree = suffix-tree(suffixes($dna));

show-edges $tree;


# vim: ft=perl6

0 comments on commit 8da517c

Please sign in to comment.