Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have used the Math::Combinatorics to work out the combinations and permutations needed. The aim is to work out all of the ways in which a string of a certain hamming distance could be created rather than create all the possible permutations of the string and test them.

use strict; use warnings; use Data::Dumper; use Math::Combinatorics; my $str = "TTTCGG"; my @str = split //, $str; my $hammingDistance = 2; # Calculate the total number of ways of getting the bases my %bases; my @baseopts = split //, 'ACGT' x $hammingDistance; my $basecomb = Math::Combinatorics->new(count => $hammingDistance, dat +a => [@baseopts], ); while(my @combo = $basecomb->next_combination){ my $permu = Math::Combinatorics->new(data => [@combo], ); while(my @permu = $permu->next_permutation){ $bases{join '', @permu}++; } } # Make the list unique my @baseperms; push @baseperms, [split //, $_] foreach (keys %bases); my %results; my @n = (0 .. $#str); # Calculate all the permutations of position that could give a change # and work through the base combinations my $poscomb = Math::Combinatorics->new(count => $hammingDistance, data + => [@n], ); while(my @combo = $poscomb->next_combination){ foreach my $bases (@baseperms){ my @newstr = @str; @newstr[@combo] = @$bases; $results{ join('', @newstr) }++; } } print "$_ ", hd($_, $str), "\n" foreach (sort keys %results); sub hd { return ( $_[0] ^ $_[1] ) =~ tr/\001-\255//; }

In reply to Re: Finding Neighbours of a String by inman
in thread Finding Neighbours of a String by monkfan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-18 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found