I have a subroutine that generates the d-neighbors of an input sequence, which I was given help with here. My subroutine looks like:
#subroutine for generating d-neighbors
sub generate_d_neighbors{
# $sequence is the sequence to generate d-neighbors from
my ($sequence) = @_;
my @temp;
my %returnHash;
my @tempSeq = @$sequence;
for(my $i = 0; $i <= 3; $i++){
#print "-------------------\n";
#reset back to original sequence
@$sequence = @tempSeq;
my @l = qw(A T G C); #list of possible bases
my $baseToRemove = @$sequence[$i]; #get base from sequence at current index
@temp = grep {$_ ne $baseToRemove} @l; #remove base
for (my $j = 0; $j <= 2; $j++){
#replace sequence[i] with temp[j]
@$sequence[$i] = $temp[$j];
#add sequences to hash
$returnHash{@$sequence} = $i;
}
}
return %returnHash;
}
I want to return a hash of the sequences. However, when I test it with
my @testSeq = ("A","T","C","G");
my %neighborhood = generate_d_neighbors(\@testSeq);
for my $neighbor (keys %neighborhood){
print "key: $neighbor\t value: $neighborhood{$neighbor}\n";
}
I get an output of
key:4 value:3
I would like the output to be the generated sequences as the keys (since they cannot have duplicates) , and the starting position in the sequence as the value. What am I doing wrong?