Hi to all Monks,

I am trying to find the common patterns in different strings. I used the KMP algorithm. It works best for a single string but show almost nothing for the same string in next iterator of loop, Here is my code

use CGI qw(:standard); use strict; print header; BEGIN{ push @INC,'C:/src/String-1.5','C:/src/bioperl-live','C:/src/ens +embl/modules','C:/src/ensembl-compara/modules','C:/src/ensembl-variat +ion/modules','C:/src/ensembl-functgenomics/modules','C:/src/DBD-mysql +-4.021';}; use strict; use warnings; use string; my $T_one='GAATTCACGGATAGCCWGGTACGATGATAGATGAATTCGACTAGAATGCCWGGAAGAAT +gGAATTC'; my $Ta='GAATTCACGGATAGCCWGGTACGATGATAGATGAATTCGACTAGAATGCCWGGAAGAATgGA +ATTC'; my $rr='GAATTCACGGATAGCCWGGTACGATGATAGATGAATTCGACTAGAATGCCWGGAAGAATgGA +ATTC'; my @P=('GAATTC','CCWGG'); my @next; my @loc; my $m; my $l=0; my $lt;my $T; my @text=(0); my @tt=($T_one,$Ta,$rr); for(my $i=0;$i<3;$i++){ $T=$tt[$i]; @loc=(); print "<\br>"; @text=(); my $str = new String($T); # USE OF STRING MODULE TO FETCH +THE SEQUENCE print "length of gene sequence array ", $lt= $str->length +, "\n"; # LENGTH OF THE SEQUENCE $lt= $str->length; my $z=0; # FOR ARRAY INCREMENT for(my $k=0; $k<$lt;$k++){ $text[$z]=$str->charAt($k); #ASSIGNING THE ARRAY +OBJECT VALUES TO AN ARRAY $z++; } #END OF FOR LOOP foreach my $P(@P){ print knuth_morris_pratt($T,$P); print"\n"; } @loc = sort {$a <=> $b} @loc; print"</br>";print "@loc";print "</br>"; my $i=0; for(my $k=0;$k<$lt;$k++){ print $i; if ($k == $loc[$i]){ print "<span style=background-color:re +d;>" . $text[$k] . "</span>"; $i++; } else { print $text[$k]; } } # end of $k for loop #print"\n";print "@loc";print "<\n>"; # FOR ARRAY INCREMENT print "</br>"; } #end of $T for each loop sub knuth_morris_pratt_next { my ( $P ) = @_; # The pattern. use integer; $m=length $P; my ($i, $j ) = ( 0, -1 ); for ($next[0] = -1; $i < $m; ) { # Note that this while() is skipped during the first for() pas +s. while ( $j > -1 && substr( $P, $i, 1 ) ne substr( $P, $j, 1 ) ) { $j = $next[ $j ]; } $i++; $j++; $next[ $i ] = substr( $P, $j, 1 ) eq substr( $P, $i, 1 ) ? $next[ $j ] : $j; } return ( $m, @next ); # Length of pattern and prefix function. } ####################################### sub knuth_morris_pratt { my ( $T, $P ) = @_; # Text and pattern. use integer; my( $m, @next) = knuth_morris_pratt_next( $P ); my ( $n, $i, $j ) = ( length($T), 0, 0 ); while ( $i < $n ) { while ( $j > -1 && substr( $P, $j, 1 ) ne substr( $T, $i, 1 ) +) { $j = $next[ $j ]; } $i++; $j++; if($j >= $m){ my $a=$i-$j; # $j=$j-1;print "\n"; $j=$next[$j]; for(my $z=0;$z<$m;$z++){ $loc[$l]=$a; $l++; $a++; } } } return ; # Mismatch. } #################################

I am new here so apology for any mistake. I am a biologist doing a project in perl.Please Monks help me


In reply to Pattern Searching by aseee

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.