Skip to content

Instantly share code, notes, and snippets.

@bobthecat
Created May 15, 2012 22:21
Show Gist options
  • Save bobthecat/2705578 to your computer and use it in GitHub Desktop.
Save bobthecat/2705578 to your computer and use it in GitHub Desktop.
PubMed trend
#! /usr/bin/perl -w
#
# pubmed_trend.pl
#
# Created by David Ruau on 2011-02-17.
# Department of Pediatrics/Div. System Medicine Stanford University.
#
##################### USAGE #########################
#
# Query PubMed with Eutils tools
#
#####################################################
use Bio::TGen::EUtils;
use strict;
my $queryString = $ARGV[0];
## query info
my $eu = Bio::TGen::EUtils->new( 'tool' => 'pubmed_trend.pl',
'email' => '[email protected]' );
## EFetch
my $query = $eu->esearch( db => 'pubmed',
term => $queryString,
usehistory => 'n' );
$query->write_raw( file => 'tempfile.xml' );
if (-z 'tempfile.xml') {
# one more time
my $query = $eu->esearch( db => 'pubmed',
term => $queryString,
usehistory => 'n' );
$query->write_raw( file => 'tempfile.xml' );
if (-z 'tempfile.xml') {
open (FILE, '>', 'tempfile.xml') or die 'Could not open file, $!';
print FILE "<begin>hello world</begin>";
close (FILE);
}
}
pubmed_trend <- function(search.str = 'Sex+Characteristics[mh] AND Pain[mh]', year.span=1970:2011) {
require(XML)
require(RCurl)
results <- NULL
tmpf <- "./tempfile.xml"
## clean before
system(paste("rm", tmpf))
for(i in year.span){
queryString <- paste(search.str, ' AND ', i, '[dp]', sep="")
print(paste('queryString:', queryString))
sysString <- paste('./pubmed_trend.pl "', queryString,'"', sep="")
system(sysString)
xml <- xmlTreeParse(tmpf, useInternalNodes=TRUE)
pubTerm <- as.numeric(xmlValue(getNodeSet(xml, "//Count")[[1]]))
print(paste("#______num pub for",i,":",pubTerm))
rm(xml)
results <- append(results, pubTerm)
## avoid being kicked out!
Sys.sleep(1)
}
names(results) <- year.span
## clean after
system(paste("rm", tmpf))
return(results)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment