I would suggest something like this as it only requires a single pass, uses index which is faster than an RE and will accomodate non adjacent matching.

my $dna = 'CATCATCAT_____CATCATCATCAT____CAT_CAT___CAT__CAT___'; my $find = 'CAT'; my $fudge = 1; # the fudge factor allow detection of sequences that are close # but not immediately adjacent to one another. # set to 0 no separation is allowed, # set to 1 there can be 0-1 bases between the find pattern etc. my $index = 0; my $last_index = -999999; my $start_index = 0; my $num = 0; my $cur_offset = 0; my $len = length $find; my $hash; while ( ($index = index($dna, $find,$cur_offset)) != -1 ) { if ( $index <= ($last_index+$len+$fudge) ) { $num++; } else { if ( $num ) { print "$num\n"; push @{$hash->{$num}}, [$start_index, $last_index+$len-1]; } print "Found at $index, repeats "; $start_index = $index; $num = 1; } $cur_offset = $index+$len; $last_index = $index; } # get the last match, if it exists if ( $num ) { print "$num\n"; push @{$hash->{$num}}, [$start_index, $last_index+$len-1]; } for $num( sort { $a<=>$b } keys %$hash ) { printf "%d repeat\n\t%d found\n", $num, scalar(@{$hash->{$num}}); printf "\t\tOffset %d - %d (%d)\n", @$_, ($_->[1]-$_->[0]+1) for @ +{$hash->{$num}}; } __DATA__ Found at 0, repeats 3 Found at 14, repeats 4 Found at 30, repeats 2 Found at 40, repeats 1 Found at 45, repeats 1 1 repeat 2 found Offset 40 - 42 (3) Offset 45 - 47 (3) 2 repeat 1 found Offset 30 - 36 (7) 3 repeat 1 found Offset 0 - 8 (9) 4 repeat 1 found Offset 14 - 25 (12)

cheers

tachyon


In reply to Re: Regexps for microsatellites by tachyon
in thread Regexps for microsatellites by knirirr

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.