mastarr has asked for the wisdom of the Perl Monks concerning the following question:
Hello everybody,
I'm searching a file of annotations for a select list of terms in a second file. It seems to work fine but I'd like to learn alternative ways to do it or learn how to improve it. Specifically, is there is a way I can improve the regular expressions in this line:
/(\b($term)\b|$term-)\b|(-$term)\b/i) {Thanks!
Here is the full script:
#!/usr/bin/perl # Read search terms into array @terms open (SRC, $searchTermsFile) || die "can't open search terms file"; @terms = <SRC>; chomp (@terms); @terms = sort @terms; my @annotationlist; #------------------------------------------------------- # Search each line in annotations file for match with search terms %nTermHits = (); $nTotalHits = 0; open (ANN, $annotationsFile) || die "$annotationsFile won't open\n"; while ($line = <ANN>) { #chomp($line); @fields = split(/\t/, $line); $annotation = $fields [2]; foreach $term (@terms) { if($annotation =~ /(\b($term)\b|$term-)\b|(-$term)\b/i) { $nTermHits {$term}++; $nTotalHits++; push(@annotationlist, "$annotation\n"); } }} #@annotationlist = sort @annotationlist; #print "@annotationlist\n"; #--------------------------------------- foreach $term (@terms) { if ($nTermHits {$term} > 0) { printf "%s\t%d\n", $term, $nTermHits {$term}; } } print "Total hits: $nTotalHits\n"; #-----------------------------
This is the output :
Ras GTPase 3 catalase 2 cysteine proteinase 4 Total hits: 9
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help to improve script that searches a file for key words in a second file.
by kschwab (Vicar) on Mar 29, 2013 at 16:45 UTC | |
by mastarr (Novice) on Apr 01, 2013 at 18:06 UTC | |
|
Re: Help to improve script that searches a file for key words in a second file.
by jwkrahn (Abbot) on Mar 29, 2013 at 16:51 UTC | |
by mastarr (Novice) on Apr 01, 2013 at 18:31 UTC | |
|
Re: Help to improve script that searches a file for key words in a second file.
by space_monk (Chaplain) on Mar 29, 2013 at 17:13 UTC | |
by mastarr (Novice) on Apr 01, 2013 at 19:29 UTC |