ReproducibleResearchTutorial / pubmedR / R / pubmed.R

pubmedQuery = function(search) {
  library(XML)
   return(xmlRoot(xmlTreeParse(sprintf("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=%s&tool=R",search))))
}

getQueryCounts = function(genes,terms,baseline=NULL) {
  retmat = matrix(rep(0,length(terms)*length(genes)),nc=length(terms))
  colnames(retmat)=terms
  rownames(retmat)=genes
  pb = txtProgressBar(max=length(terms)*length(genes),style=3)
  i = 0
  for(gene in genes) {
    for(term in terms) {
      i=i+1
      setTxtProgressBar(pb,i)
      search = sprintf("%s AND %s",term,gene)
      if(!is.null(baseline)) {
        search = sprintf("%s AND %s",search,baseline)
      }
      doc=pubmedQuery(search)
      if(length(getNodeSet(doc,'//ErrorList'))>0) {
        # If we are here, an error or "not found" occurred.
        retmat[gene,term]=0
      } else {
        retmat[gene,term]=as.integer(xmlValue(getNodeSet(doc,"/eSearchResult/Count")[[1]]))
      }
    }
    Sys.sleep(0.33)
  }
  return(retmat)
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.