in reply to Finding Neighbours of a String
use Algorithm::Combinatorics qw( combinations ); use Set::CrossProduct; my @base = split //, $str; for my $exact_distance ( 1 .. $d ) { my $change_idx_iter = combinations( [ 0 .. $#base ], $exact_distan +ce ); while( my $change_idx = $change_idx_iter->next ) { my @base_combo = map { my $i = $_; [ grep { $base[ $i ] ne $_ } qw( A T C G ) ]; } @$change_idx; # HACK: Set::CrossProduct doesn’t work with a 1-dimensional ma +trix 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; print @neighbour, "\n"; } } }
Updates: changed to use combinations vs variations and to grep out non-changes, so that it will produce no duplicates.
To see what’s going on, add the following line before the print:
$_ = "[$_]" for @neighbour[ @$change_idx ];
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding Neighbours of a String
by monkfan (Curate) on Mar 01, 2006 at 11:25 UTC | |
by Aristotle (Chancellor) on Mar 01, 2006 at 11:57 UTC | |
by monkfan (Curate) on Mar 02, 2006 at 09:25 UTC | |
by Aristotle (Chancellor) on Mar 03, 2006 at 20:15 UTC | |
by Aristotle (Chancellor) on Mar 01, 2006 at 11:35 UTC | |
by monkfan (Curate) on Mar 01, 2006 at 11:41 UTC |