Сначала вам нужна подпрограмма merge_strings
:
sub merge_strings {
my ($x, $y, $k) = @_;
return sprintf '%s%s', $x, substr $y, $k;
}
Подпрограмма предполагает, что совпадают последние $k
символов $x
и первые $k
символов $y
.
use strict; use warnings;
use Data::Dumper;
my $k = 2;
my %readlist = (
"read1" => "ACTGA",
"read2" => "ACAAA",
"read3" => "CTCGC",
"read4" => "GAGGG",
"read5" => "TTTCC",
);
my %prefix_hash = (
"AC" => ["read1","read2"],
"AG" => ["read3"],
"GA" => ["read4"],
"TT" => ["read5"]
);
my $seed = "AAAAC";
my @newreads = extend_seed($seed, $k, \%prefix_hash, \%readlist);
print Dumper \@newreads;
sub merge_strings {
my ($x, $y, $k) = @_;
return sprintf '%s%s', $x, substr $y, $k;
}
sub extend_seed {
my ($x, $k, $prefix, $reads) = @_;
my $key = substr $x, -$k;
return unless exists $prefix->{$key};
my @ret = map merge_strings($x, $_, $k),
@{$reads}{@{ $prefix->{$key} }};
push @ret, map extend_seed($_, $k, $prefix, $reads), @ret;
return @ret;
}
Выход:
$VAR1 = [
'AAAACTGA',
'AAAACAAA',
'AAAACTGAGGG'
];