## Modules ##
use strict;
use lib '.'; # This looks for modules in the same folder as the script
use NCBI_PowerScripting; # NCBI eutils module
use XML::Twig; # Handle the complicated handlers, and parse xml
use XML::Simple; # Use to insert parsed xml data into file
use CGI;
use CGI::Carp qw(fatalsToBrowser);
## End Modules ##
# Global Constants
my $pCGI = new CGI;
print $pCGI->header;
my $sTerm = $pCGI->param('search');
# End
print "Searching for $sTerm...
\n";
# Now going to parse all "spaces" into "+" signs
$sTerm =~s/ /+/g;
$sTerm =~s/\+{2,}/\+/g; #just in case the user was smart and wrote in "+" signs
print "Converting to $sTerm...
\n";
# Information needed for ESearch
# Information passed from webbrower is $sTerm
my $PH_ESEARCH_PARAMS = {
'db' => 'pubmed',
'term' => "$sTerm",
'rettype' => 'uilist',
'retmode' => 'xml',
'outfile' => 'pdfids.xml',
'tool' => 'vbipatric'
};
select(STDOUT); $| = 1; # make STDOUT unbuffered (so prints are done imediately)
# Fetch Datas and create XML File
buildPMIDXMLFile($PH_ESEARCH_PARAMS);
# Strip file of unnecessary crap
# strip();
my $parse = "pdfids.xml";
# Building the Twig Object and parse it using the insertCitations Method
my $pTwig = new XML::Twig('TwigRoots' => {'IdList' => \&insertCitations});
if( !defined($pTwig) )
{
print "Err: Can't get XML::Twig Object !\n";
exit -2;
}
$pTwig->parsefile($parse);
print "All done \:\) \n";
exit;
sub buildPMIDXMLFile
{
my $p_phESearchParams = shift;
my $p_sTerm = shift;
my %hESearchParams = esearch(('db' => $p_phESearchParams->{'db'}, 'term' => $p_phESearchParams->{'term'}));
# Complete the %hESearchParams hashtable using required params from original $p_phESearchParams
foreach my $sKey (keys %{$p_phESearchParams})
{
$hESearchParams{$sKey} = $p_phESearchParams->{$sKey};
}
$hESearchParams{'term'} = $p_sTerm;
efetch_batch(%hESearchParams);
print "
Successfully built pdfids.xml file
\n";
}
sub getPMID
{
my $p_phXML = shift;
my $paXMLPMID = getInfosFromXML($p_phXML, 'Id');
return join(',', @{$paXMLPMID});
print "Successfully grabbed all PMIDS and inserted into an array....proceeding \n"
}
sub getInfosFromXML
{
my $p_phXML = shift;
my @p_aKeyList = @_;
my $r_paXMLInfos;
my $phTmpXML = $p_phXML;
my @aKeyListSaved = @p_aKeyList;
my $bReturnTmpXML = 1;
foreach my $sKey (@p_aKeyList)
{
shift @aKeyListSaved;
if( defined($phTmpXML) )
{
if( ref($phTmpXML) eq 'HASH' && defined($phTmpXML->{$sKey}) )
{
if( ref($phTmpXML->{$sKey}) eq 'HASH' )
{
$phTmpXML = $phTmpXML->{$sKey};
}
elsif( ref($phTmpXML->{$sKey}) eq 'ARRAY' )
{
foreach my $puTmp (@{$phTmpXML->{$sKey}})
{
if( !defined($r_paXMLInfos) )
{
$r_paXMLInfos = [];
}
my $paXMLInfos = getInfosFromXML($puTmp, @aKeyListSaved);
if( defined($paXMLInfos) )
{
push @{$r_paXMLInfos}, @{$paXMLInfos};
}
}
}
else
{
if( !defined($r_paXMLInfos) )
{
$r_paXMLInfos = [];
}
push @{$r_paXMLInfos}, $phTmpXML->{$sKey};
$bReturnTmpXML = 0;
last;
}
}
elsif( ref($phTmpXML) eq 'HASH' && !defined($phTmpXML->{$sKey}) )
{
$bReturnTmpXML = 0;
}
}
}
if( !defined($r_paXMLInfos) )
{
if( $bReturnTmpXML eq 1 )
{
$r_paXMLInfos = [$phTmpXML];
}
else
{
$r_paXMLInfos = [];
}
}
return $r_paXMLInfos;
print "Done grabbing all pmids.... \n";
}
sub insertCitations
{
my $p_pTwig = shift;
my $p_pIdList = shift;
# Save XML IdList into string
my $sIdList = $p_pIdList->sprint;
# release/purge Twig Datas (no more needed as we work on the $sIdList XML directly using XML::Simple
$p_pTwig->purge;
# Getting an XML::Simple Object from the IdList XML (using sprint), which makes things simple to parse...
my $pXS = new XML::Simple();
my $phXML = eval{$pXS->XMLin($sIdList, 'forcearray' => 0)};
if( $@ )
{
print "Err: $@\n";
return;
}
if( !defined($phXML) )
{
return;
}
# Getting PMID
my $nPMID = getPMID($phXML);
# 1112,111244,121423,1214314
my @pmid = split (",",$nPMID);
my $pmid;
print "Grabbing all pmids .... \n";
#if( !defined($nPMID) || $nPMID !~ /^\d+$/ )
#{
# return;
#}
print "Writing list of PDF PMIDS that you'll need.... \n";
open (PDFLIST,">/opt/lampp/htdocs/pdflist/pdflist-$sTerm.html");
print PDFLIST "";
print PDFLIST "