sub align { my($hash, $seq) = @_; my $best = { totlen => 0 }; my @match; my $re = join '(.*?)', ($seq =~ /(\w)/g); for my $key (sort keys %$hash) { my $string = join '', @{ $hash->{$key} }; next unless $string =~ $re; my $offset = $-[0]; my $length = $+[0] - $offset; my $match = { key => $key, matched => substr($string, $offset, $length), totlen => $length, inserts => [ map $_ - $offset, @+[1 .. $#-] ], lengths => [ map $+[$_] - $-[$_], 1 .. $#+ ], }; push @match, $match; $best = $match if $match->{totlen} > $best->{totlen}; } return [ map { my $match = $_; my $string = $match->{matched}; for (reverse 0 .. $#{ $match->{lengths} }) { my $bestlen = $best->{lengths}[$_]; my $curlen = $match->{lengths}[$_]; if ($bestlen > $curlen) { substr($string, $match->{inserts}[$_], 0) = "-" x ($bestlen - $curlen); } } $string; } @match ]; }