Skip to main content

pubmed-types (v0.2.0)

Built with: xsdata-pydantic

Introduction

A complete implementation of the XML schema for PMC Open Access articles and Pubmed article sets (citations).

This package helps to parse PubMed XML data into Pydantic models. This validates the input xml data and provides typehints for working with the complex XML structures present in PubMed data.

Most Recent Changes

  • Breaking Change: The parse_pubmed_xml is replaced by pmc_article and pubmed_article_set.
  • More test coverage
  • Pubmed Articles can now parse MathML
  • Restructured code to separate out jats (pmc open access articles) and pubmed (pubmed article set)
  • One unit test with 99% coverage
  • Added CHANGELOG.md

Why do I need this?

PubMed keeps track of 10s of millions of research data, and a complex XML structure is used to store it. Parsing XML on its own is challenging enough. Add to it the feature rich data inside of each citation, and you will find yourself with hours or days of navigating the XML structure.

The approach here was to autogenerate Pydantic classes to parse the XML using the xsdata-pydantic tool. This approach has the benefit of making sure every piece of data is parsed properly, and an error is thrown is something is missing or incorrect. Instead of using dictionaries to hold the data, Pydantic classes have the benefit of providing type hints with tab completion for IDEs, making it easier to navigate the complex structure of the citation data.

How do I use it?

It is possible to use xsdata-pydantic and the autogenerated classes directly to parse an XML file, but we provide a convenience function to easily open PubMed XMl citations and PMC open access articles.

Example 1: A PMC Open Access Article

import tarfile
import urllib.request as request
from contextlib import closing
from pathlib import Path

from pubmed_types import pmc_article

# Input file source and output file destination
source = (
    "ftp://ftp.ncbi.nlm.nih.gov"
    + "/pub/pmc/oa_bulk/oa_comm/xml"
    + "/oa_comm_xml.incr.2023-03-21.tar.gz"
)
destination = Path("downloads")
destination.mkdir(exist_ok=True)

# 1. Get an open access article dataset from the FTP server
with closing(request.urlopen(source)) as url:
    with tarfile.open(fileobj=url, mode="r:gz") as fr:
        fr.extractall(destination)

# 2. Parse the file
file_path = destination.joinpath("PMC009xxxxxx").joinpath("PMC9970662.xml")
full_text = pmc_article(file_path)

# 3. Print out the article title
print(f"Title: {full_text.front.article_meta.title_group.article_title.content[0]}")

Output:

Title: Lactate as a myokine and exerkine: drivers and signals of physiology and metabolism

Example 2: A Pubmed baseline citation file

import gzip
import urllib.request as request
from contextlib import closing
from pathlib import Path

from pubmed_types import pubmed_article_set

# Input file source and output file destination
source = "ftp://ftp.ncbi.nlm.nih.gov" + "/pubmed/updatefiles" + "/pubmed23n1168.xml.gz"
destination = Path("downloads").joinpath("pubmed23n1168.xml")
destination.parent.mkdir(exist_ok=True)

# 1. Get a pubmed citation daily update file from the FTP server
with closing(request.urlopen(source)) as url:
    with gzip.GzipFile(fileobj=url, mode="rb") as fr:
        with open(destination, mode="wb") as fw:
            fw.write(fr.read())

# 2. Parse the file
article_set = pubmed_article_set(destination)

# 3. Get the number of citations in the file
print(f"Number of citations: {len(article_set.pubmed_article)}")
print(
    f"{article_set.pubmed_article[0].medline_citation.article.article_title.content[0]}"
)

Output:

Number of citations: 2543
A Patent and Pattern Mother.

FAQ

Why does it take so long to parse a pubmed citation set

There is a lot of data, and the schema is deep and complex.

Why are the return structures so complicated?

The return structures are a direct reflection of the XML format defined by the NLM. In the future some utility classes might be made for common components (title, authors, etc), but for now this is intended to be an unbiased way of parsing the XML.

Metadata

Release files for pubmed-types 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pubmed-types 0.2.0
File Size Uploaded Provenance
pubmed_types-0.2.0.tar.gz 211.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pubmed-types 0.2.0
File Interpreter ABI Platform
pubmed_types-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 692.1 kB

Release files / pubmed_types-0.2.0.tar.gz

Download URL pubmed_types-0.2.0.tar.gz
Size 211.0 kB
Tags Source
SHA-256 checksum
How to use checksums
56b9e53128f43eddb83118ed4e5e2c3dee23d03813277f3ec6b6951b9f522130
BLAKE2b-256 checksum
How to use checksums
5849412119a8d07431b75c961a4630a48252b03d798cc4b783374ab883783959
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1038-azure

Release files / pubmed_types-0.2.0-py3-none-any.whl

Download URL pubmed_types-0.2.0-py3-none-any.whl
Size 481.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
936f85f5011f294dcc01389375f4c5e21622441d8b54b273cced31bfbf5f3d39
BLAKE2b-256 checksum
How to use checksums
c8f61a8d035939c9432975eecf87305af7c4cd31466aefdf6a574e4171dc12d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.0 Linux/5.15.0-1038-azure

Security for pubmed-types 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page