The Open Access subset of Pubmed Central (PMC) includes 2.5 million articles from biomedical and life sciences journals. The full text XML files are freely available for text mining from the REST service or FTP site but can be challenging to parse. For example, section tags are nested to arbitrary depths, formulas and tables may return incomprehensible text blobs and superscripted references are pasted at the end of words. The functions in the tidypmc package are intended to return readable text and maintain the document structure, so gene names and other terms can be associated with specific sections, paragraphs, sentences or table rows.


Use remotes to install the package.


Load XML

Download a single XML document like PMC2231364 from the REST service using the pmc_xml function.

The europepmc package includes additional functions to search PMC and download full text. Be sure to include the OPEN_ACCESS field in the search since these are the only articles with full text XML available.

Save all 19 results to a list of XML documents using the epmc_ftxt or pmc_xml function.

See the PMC FTP vignette for details on parsing the large XML files on the FTP site with 10,000 articles each.

Parse XML

The package includes five functions to parse the xml_document.

R function Description
pmc_text Split section paragraphs into sentences with full path to subsection titles
pmc_caption Split figure, table and supplementary material captions into sentences
pmc_table Convert table nodes into a list of tibbles
pmc_reference Format references cited into a tibble
pmc_metadata List journal and article metadata in front node

The pmc_text function uses the tokenizers package to split section paragraphs into sentences. The function also removes any tables, figures or formulas that are nested within paragraph tags, replaces superscripted references with brackets, adds carets and underscores to other superscripts and subscripts and includes the full path to the subsection title.

txt <- pmc_text(doc)
#  Note: removing disp-formula nested in sec/p tag
#  # A tibble: 194 x 4
#     section    paragraph sentence text                                                                         
#     <chr>          <int>    <int> <chr>                                                                        
#   1 Title              1        1 Comparative transcriptomics in Yersinia pestis: a global view of environment…
#   2 Abstract           1        1 Environmental modulation of gene expression in Yersinia pestis is critical f…
#   3 Abstract           1        2 Using cDNA microarray technology, we have analyzed the global gene expressio…
#   4 Abstract           2        1 To provide us with a comprehensive view of environmental modulation of globa…
#   5 Abstract           2        2 Almost all known virulence genes of Y. pestis were differentially regulated …
#   6 Abstract           2        3 Clustering enabled us to functionally classify co-expressed genes, including…
#   7 Abstract           2        4 Collections of operons were predicted from the microarray data, and some of …
#   8 Abstract           2        5 Several regulatory DNA motifs, probably recognized by the regulatory protein…
#   9 Abstract           3        1 The comparative transcriptomics analysis we present here not only benefits o…
#  10 Background         1        1 Yersinia pestis is the etiological agent of plague, alternatively growing in…
#  # … with 184 more rows
count(txt, section, sort=TRUE)
#  # A tibble: 21 x 2
#     section                                                                                                   n
#     <chr>                                                                                                 <int>
#   1 Results and Discussion; Clustering analysis and functional classification of co-expressed gene clust…    22
#   2 Background                                                                                               20
#   3 Results and Discussion; Virulence genes in response to multiple environmental stresses                   20
#   4 Methods; Collection of microarray expression data                                                        17
#   5 Results and Discussion; Computational discovery of regulatory DNA motifs                                 16
#   6 Methods; Gel mobility shift analysis of Fur binding                                                      13
#   7 Results and Discussion; Verification of predicted operons by RT-PCR                                      10
#   8 Abstract                                                                                                  8
#   9 Methods; Discovery of regulatory DNA motifs                                                               8
#  10 Methods; Clustering analysis                                                                              7
#  # … with 11 more rows

Load the tidytext package for further text processing.

The pmc_table function formats tables by collapsing multiline headers, expanding rowspan and colspan attributes and adding subheadings into a new column.

Use collapse_rows to join column names and cell values in a semi-colon delimited string (and then search using functions in the next section).

The other three pmc functions are described in the package vignette.

Searching text

There are a few functions to search within the pmc_text or collapsed pmc_table output. separate_text uses the stringr package to extract any regular expression or vector of words.

A few wrappers search pre-defined patterns and add an extra step to expand matched ranges. separate_refs matches references within brackets using \\[[0-9, -]+\\] and expands ranges like [7-9].

separate_genes will find microbial genes like tauD (with a capitalized 4th letter) and expand operons like tauABCD into four genes. separate_tags will find and expand locus tag ranges below.

See the vignette for more details including code to parse XML documents using the xml2 package. The PMC FTP vignette has details on parsing XML files at the Europe PMC FTP site.

Community Guidelines

This project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. Feedback, bug reports, and feature requests are welcome here.