christopherbelter / pubmedxml Goto Github PK
View Code? Open in Web Editor NEWParse PubMed XML into a data frame in R
License: MIT License
Parse PubMed XML into a data frame in R
License: MIT License
Hi, a quick question. How should I write the query to download the full database.
Amazing work. Finding these functions saved me a lot of time and effort - thanks so much for making these available.
Just one small issue, using efetch, my xml results have a declaration that includes the encoding, eg
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
When running clean_api_xml
on results, this xml declaration is missed. This then results in the return from the function having two xml declarations.
The following adds another gsub
to handle cases where encoding is included in the xml declaration. It also allows clean_api_xml
to work on xml results in memory without needing to read/write to disk.
## clean pubmed XML returned from either the reutils or rentrez packages and save the cleaned XML to a new file
clean_api_xml <- function(infile, outfile = NULL, usedisk = FALSE) {
if (usedisk == TRUE){
theData <- readChar(infile, file.info(infile)$size, useBytes = TRUE)
} else {
theData <- as.character(infile)
}
theData <- gsub("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "", theData, fixed = TRUE) # efetch xml results sometimes have encoding in the xml declaration
theData <- gsub("<?xml version=\"1.0\" ?>", "", theData, fixed = TRUE)
theData <- gsub("<!DOCTYPE PubmedArticleSet PUBLIC \"-//NLM//DTD PubMedArticle, 1st January 2019//EN\" \"https://dtd.nlm.nih.gov/ncbi/pubmed/out/pubmed_190101.dtd\">", "", theData, fixed = TRUE, useBytes = TRUE)
theData <- gsub("<PubmedArticleSet>", "", theData, fixed = TRUE)
theData <- gsub("</PubmedArticleSet>", "", theData, fixed = TRUE)
theData <- gsub("<U\\+\\w{4}>", "", theData) ## note: with some files this doesn't catch everything; potential issue with <OtherAbstract> tags especially
theData <- paste("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<!DOCTYPE PubmedArticleSet PUBLIC \"-//NLM//DTD PubMedArticle, 1st January 2019//EN\" \"https://dtd.nlm.nih.gov/ncbi/pubmed/out/pubmed_190101.dtd\">", "<PubmedArticleSet>", theData, "</PubmedArticleSet>", sep = "\n")
#theData <- paste(theData, "</PubmedArticleSet>")
theData <- iconv(theData, to = "UTF-8", sub = "")
if (usedisk == TRUE){
writeLines(theData, outfile, sep = " ")
}
return(theData)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.