Dear Aristotle,
Sorry slight glitches here. I was working on
your last modified code below.
It works 99% fine except when the given string
is in bracketed format.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Carp;
use Algorithm::Combinatorics qw( combinations );
use Set::CrossProduct;
my $str1 = '[TA]TTCGG';
my $e = 2;
find_nb($str1,$e);
sub find_nb {
my ( $str, $d ) = @_;
my @base = $str =~ /\G ( \[ [^][]+ \] | [^][] ) /xg;
#my @base = split //, $str;
for my $exact_distance ( 1 .. $d ) {
my $change_idx_iter
= combinations( [ 0 .. $#base ], $exact_distance );
while ( my $change_idx = $change_idx_iter->next ) {
my @base_combo = map {
my $i = $_;
[ grep { $base[$i] !~ $_ } qw( A T C G ) ];
#[ grep { $base[$i] ne $_ } qw( A T C G ) ];
} @$change_idx;
push @base_combo, [0] if $exact_distance == 1;
my $bases_iter = Set::CrossProduct->new( \@base_combo );
my @neighbour = @base;
while ( my $new_bases = $bases_iter->get ) {
@neighbour[@$change_idx] = @$new_bases;
#$_ = "[$_]" for @neighbour[@$change_idx];
my $str = join( "", @neighbour );
print "$str\n";
}
}
}
return;
}
Why my modification above it doesnt' produce this:
So the output should be always without bracket.
Currently one of the entry appear
like this: [TA]TTTTG.
Instead this kind of string would need
to be represented separately into:
TTTTTG
ATTTTG
Is there anything I can do to fix it?
I really hope to hear from you again. Since
your solution is very important to me.
Here is my brute-force code that generate the result above.
|