supriyoch_2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I am interested to randomize a DNA string in such a way that the number and the composition of elements in the DNA string remian the same but only the positions of elements are changed. For instance, I have a DNA string like "AATTGGCC" i.e. each base (A,T,G,C) has two copies. I want to get all possible combinations like ATATGGCC,AATGTGCC,ATATGCGC..etc. I am at a fix to find all the possible combinations from the string in perl. I looked for online resouces but I could not find the solution. I welcome suggestions from perlmonks. I have written a script which goes as follows. I have not written the code for combinations.

#!/usr/bin/perl use warnings; my $dna="aattggcc"; $dna=uc$dna; print "\n dna: $dna\n"; # code for all possible combinations ? exit;

Results should look like:

All possible combinations are: ATATGGCC AATGTGCC ATATGCGC ........

Replies are listed 'Best First'.
Re: How can one randomize a DNA string without changing the composition?
by QM (Parson) on Jan 16, 2015 at 10:46 UTC
    You might be interested in a small pure Perl solution, which I stole from a QOTW entry (which I can't locate).
    # Find and return the $n'th permutation # of the remaining arguments in some canonical order # (modified from QOTW solution) sub permutation_n { my $n = shift; my $result = ''; while (@_) { ($n, my $r) = (int($n/@_), $n % @_); $result .= splice @_, $r, 1; } return $result; }

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

        Thanks.

        There is a similar version, I recall, which just returns the next permutation, or can be turned into an iterator. For some reason I think I saw them on something MJD wrote, but it may originate from someone else through him.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: How can one randomize a DNA string without changing the composition?
by Anonymous Monk on Jan 16, 2015 at 02:32 UTC
Re: How can one randomize a DNA string without changing the composition?
by karlgoethebier (Abbot) on Jan 16, 2015 at 10:35 UTC

    My first bioperl:

    perl -MMath::Permute::List -e "permute {print qq(@_\n)}  qw(A A T T G G C C);"

    Best regards, Karl

    Update: I don't know if that is what you need because i'm no biologist :-(

    P.S.: Change ticks to ' if not on windoz.

    Update 2: Perhaps i missed something essential in your specs:

    "...the composition of elements in the DNA string remain the same"

    What does that mean?

    And the missing link: Math::Permute::List.

    «The Crux of the Biscuit is the Apostrophe»