Skip to content

Instantly share code, notes, and snippets.

@produnis
Created May 26, 2012 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save produnis/2794312 to your computer and use it in GitHub Desktop.
Save produnis/2794312 to your computer and use it in GitHub Desktop.
Pubmed Trend via R
plot_bar <- function(x=sex.pub, linecol="royalblue", cols, addArg=TRUE) {
bp <- barplot(x, col=cols, add=addArg)
fit <- stats::lowess(x, f=1/3)
lines(x=bp, fit$y, col=linecol, lwd=3)
}
#! /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' => 'produnis@googlemail.com' );
## 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 = 'Family+Nursing[mh] AND intervention', year.span=1990: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)
}
setwd('/home/produnis/Dokumente/Programmierung/R/PubMedTrend')# edit to fit your system
source('pubmed_trend.r')
source('plot_bar.r')
library("RColorBrewer")
#-------------------------------------------------------------
search.for1 <- "schnepp w[Author]"
#----------
search.pub1 <- pubmed_trend(search.str = search.for1, year.span=1990:2012)
#--------------------------------------------------------------
pdf(file='output-one-string.pdf', height=8, width=8)
par(las=1)
colorfunction = colorRampPalette(brewer.pal(9, "Blues"))
mycolors = colorfunction(length(search.pub1))
plot_bar(x=search.pub1, linecol="#525252", cols=mycolors, addArg=FALSE)
title('Number of publication per year')
legend('topleft',
legend=c(search.for1),
fill=c("blue"),
bty="n",
cex=1.1
)
dev.off()
#END OF FILE
setwd('/home/produnis/Dokumente/Programmierung/R/PubMedTrend') # edit to fit your system
source('pubmed_trend.r')
source('plot_bar.r')
library("RColorBrewer")
#-------------------------------------------------------------
search.for1 <- "Midwifery[mh]"
search.for2 <- "Family+Nursing[mh]"
#----------
search.pub1 <- pubmed_trend(search.str = search.for1, year.span=1960:2012)
search.pub2 <- pubmed_trend(search.str = search.for2, year.span=1960:2012)
#--------------------------------------------------------------
pdf(file='output-two-string.pdf', height=8, width=8)
par(las=1)
colorfunction = colorRampPalette(brewer.pal(9, "Reds"))
mycolors = colorfunction(length(search.pub1))
plot_bar(x=search.pub1, linecol="#525252", cols=mycolors, addArg=FALSE)
colorfunction = colorRampPalette(brewer.pal(9, "Blues"))
mycolors = colorfunction(length(search.pub2))
plot_bar(x=search.pub2, linecol='black', cols=mycolors, addArg=TRUE)
title('Number of publication per year')
legend('topleft',
legend=c(search.for1, search.for2),
fill=c("red", "blue"),
bty="n",
cex=1.1
)
dev.off()
#END OF FILE
@produnis
Copy link
Author

On Ubuntu, install XML and Curl support.
Otherwise, installation of R-Packages will fail
On OSX, skip this step

sudo apt-get install libxml2-dev curl libcurl4-openssl-dev

the perl script needs the TGEN-EUtils
download package TGen-EUtils-0.xxx.tar.gz from http://bioinformatics.tgen.org/brunit/downloads/tgen-eutils/
extract it to some place you like, e.g. ~/Downloads/
in terminal go to the extracted folder
type in:

perl Makefile.PL

and then

make

and then

make test

and finally

sudo make install

Start R and install additionally packages 'XML', 'RCurl', 'RColorBrewer'

install.packages(c('XML', 'RCurl', 'RColorBrewer'),dependencies=T)

don't forget to make the perl-script executable
open terminal
go to the folder where you saved these pubmed-scripts

cd /path/to/pubmedscripts/

chmod +x pubmed_trend.pl

enjoy

thx to bobthecat who originally postet this hack at http://brainchronicle.blogspot.com/2012/05/using-r-to-graph-subject-trend-in.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment