Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Count multiple pattern matches

by bobf (Monsignor)
on Dec 07, 2004 at 00:58 UTC ( #412834=note: print w/replies, xml ) Need Help??


in reply to Count multiple pattern matches

If the 'pretty long' string is 'long enough' and you have a lot of keywords, you might be better off using a non-regex solution. The example below creates an index of $string, so finding exact matches is fast. Using very short substrings for the index will not be very efficient, though, so if your keywords are only a few characters a regex might be better.

This example uses a snippet of DNA sequence for $string and an arbitrary substring length of 3 (but that could be set based on the length of the shortest element in @keywords instead). The location of all the matches are stored in %matches, but you could speed things up by just incrementing a counter if you didn't need them for anything later.

use strict; use warnings; my $string = 'ccaaactcagtggggtgaatggggcttctctgtgctctgatagcttccctaccctt +tcccttctccagctcccgtcccttctgactgtgagcagccccctcctctccactgttcccctcctgttg +tcagaggagggcccagctgaggcagggactggaccaccggctggggtgtccctaggggtcttgggtggc +tggcagtagtggagcctggggctgagaggggaagcaaaataagattgtcctccaacttagccatcctca +ggcctgctggggctatttaactggctgggcctgcatggcgacagggcccctacagcctccctgggaaca +aggggtgaagggttcagggggaagggggtcacagagtgatggagaaacctcttgagaacaaactaggct +ccctcatgctggagtccaaggctgagtacctcccttctctgaaacagagcaacaaccccactcccaccc +cgagtctgtc'; my @keywords = qw( ttc gctg ccaac ggggct ccc ); my $substrlen = 3; # or use length of shortest element in @keywords # create an index of all substrings of length $string my %substrings; for( my $i = 0; $i <= length($string) - $substrlen; $i++ ) { my $substring = substr( $string, $i, $substrlen ); push( @{ $substrings{$substring}{hits} }, $i ); } # search the index for all elements of @keywords my %matches; foreach my $keyword ( @keywords ) { my $subkey = substr( $keyword, 0, $substrlen ); if( exists $substrings{$subkey} ) { foreach my $hit ( @{ $substrings{$subkey}{hits} } ) { if( $keyword eq substr($string, $hit, length($keyword)) ) { push( @{ $matches{$keyword} }, $hit ); } } } } foreach my $keyword ( keys %matches ) { print "Found $keyword ", scalar @{ $matches{$keyword} }; printf " time%s at position%s: ", scalar @{ $matches{$keyword} } > 1 ? 's' : '', scalar @{ $matches{$keyword} } > 1 ? 's' : ''; print join( ', ', @{ $matches{$keyword} } ), "\n"; } ** OUTPUT ** Found gctg 8 times at positions: 140, 164, 192, 214, 268, 286, 408, 42 +1 Found ttc 8 times at positions: 25, 44, 55, 60, 78, 111, 344, 435 Found ccaac 1 time at position: 245 Found ccc 22 times at positions: 46, 51, 57, 70, 75, 94, 95, 96, 113, +114, 136, 174, 309, 310, 321, 401, 432, 456, 457, 463, 467, 468 Found ggggct 3 times at positions: 20, 211, 271

I just thought a different approach might be interesting. YMMV. I'm sure there is a break even point between creating a regex with a lot of alternation and the time spent creating the index. I'd recommend benchmarking a few methods with some of your actual data to see what works best.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://412834]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2019-11-13 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (74 votes). Check out past polls.

    Notices?