Hi,
I'm trying to generate all the possible DNA sequences that fall between two defined lengths.
The code below works but is not very extensible. I could keep adding loops for each additional base and as the maximum length that I'm interested in is around 10 bases its quite doable, but its not very efficient programing. It should be possible to do this with dynamic references or something but I don't really know where to start. Can anyone give me some clues?

Thanks

my @Bases = qw(A C G T); my $SequenceMinLenght = 1; my $SequenceMaxLength = 3; my @SequenceLenghts = ($SequenceMinLenght...$SequenceMaxLength); #print "@SequenceLenghts\n"; my $SequenceListLength = $#SequenceLenghts; $SequenceListLength ++; print "Number of Sequence lenghts to permute is $SequenceListLength\n" +; my @allSequence = (); my $sequence; my $currLenght; foreach $currLenght (@SequenceLenghts){ foreach my $base1 (@Bases) { if ($currLenght == 1){ $sequence = $base1; push (@allSequence, $sequence); } else{ foreach my $base2 (@Bases) { if ($currLenght == 2){ $sequence = $base1 . $base2; push (@allSequence, $sequence); } else{ foreach my $base3 (@Bases) { if ($currLenght == 3){ $sequence = $base1 . $base2. $base3; push (@allSequence, $sequence); } } } } } } } #Prints sequences to screen #- Too many entries after lenght of sequence is grater than 4. my $i=0; foreach my $finalsequence (@allSequence){ $i ++; if ($i == 4){ print "$finalsequence\n"; $i= 0; } else{ print "$finalsequence\t"; } }

In reply to Generate all possibilities by lostcause

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.