Wednesday, June 6, 2012

Searching Pubmed Using Regular Expression Based Pattern Matching

This is a script that I wrote a number of years ago that provides a way of searching Pubmed abstracts using regular expression based pattern matching and, hence, it provides a way to pull out mutation data and other types of biomedical data that can be fit to a text pattern but not easily represented with keywords.  Publications describing some sample bioinformatics usages of the script for mining mutation data, gathering links to biomedical resources, and profiling developmental factors that play a role in the inner ear can be found in the links below.  I am reposting this code here because it seems the original host ( no longer makes a copy of the script available and I have recently received a number of requests for copies of the script. 

Sample Publications:


# PREP (Perl RegExps for Pubmed) is a script that allows the use of 
# Perl regexs in the searching of Pubmed records, providing the ability to search 
# records for textual patterns as well as keywords

# Copyright 2005- Christopher M. Frenz
# This script is free sofware it may be used, copied, redistributed, and/or modified
# under the terms laid forth in the Perl Artisic License 

# Please cite this script in any publication in which literature cited within the
# publication was located using the script.  

# Usage: perl PubmedQueryTerms

# Usage of this script requires the LWP and XML::LibXML modules are installed
use LWP;
use XML::LibXML; #Version 1.58 used for development and testing

# Change the variable below to set the text pattern that Perl 
# will seek to match in the returned results

my $request;
my $response;
my $query;

# Concatenates arguments passed to script to form Pubmed query
$query=join(" ", @ARGV);

# Creates the URL to search Pubmed
my $baseurl="";
my $url=$baseurl . "db=Pubmed&retmax=1&usehistory=y&term=" . $query;

# Searches Pubmed and Returns the number of results
# as well as the session information needed for results retrieval
my $results= $response->content;
die unless $response->is_success;
print "PubMed Search Results \n";
   my $NumAbstracts=$1;
   my $QueryKey=$1;
   my $WebEnv=$1;
print "$NumAbstracts are Available \n";
print "Query Key= $QueryKey \n";
print "WebEnv= $WebEnv \n";

# Opens a file for output
open(OFile, ">PREPout.html");

my $parser=XML::LibXML->new;

my $retmax=500; #Number of records to be retrieved per request-Max 500
my $retstart=0; #Record number to start retreival from

# Creates the URL needed to retrieve results
my $url2="";

my $Count=0;
# Retreives results in XML format
   print "Processing record # $retstart \n";
   $url=$baseurl . "rettype=abstract&retmode=xml&retstart=$retstart&retmax=$retmax&db=Pubmed&query_key=$QueryKey&WebEnv=$WebEnv";

   die unless $response->is_success;

   # Uses a DOM based XML parser to process returned results
   my $domtree=$parser->parse_string($results);
   my $i=0;
# Extracts element data for regex processing and output formatting

       # Processes title and abstract for pattern match and if a match occurs
       # data is written to output
       if($titles=~/($regex)/ or $abstracts=~/($regex)/){
           print OFile "<h1>Pattern Match: $1 </h1>\n";
           print OFile "<h3><a href=\"$url2$IDS\">$titles </a></h3> \n";
           print OFile "<p>$journals $volumes, $pgnums </p>\n";
           print OFile "<p>$abstracts </p>\n\n";
close OFile;
print "$Count records matched the pattern";

No comments: