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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |