Skip to main content

Download and parse pubmed publication data

Project description

PyPI - Version PyPI - Python Version

Read XML files and pull out selected values. Values to collect are determined by paths found in a structure file. The structure file also includes a key which associates the values with a parent element and names, which determine which file to place the elements in.

Files can be passed as either gzipped or uncompressed XML files or from standard in.

For more info on Pubmed's XML files see: pubmed_190101_.dtd.

Usage:

import pubmedparser
import pubmedparser.ftp

# Download data
files = pubmedparser.ftp.download(range(1, 6))

# Read XML files using a YAML file to describe what data to collect.
data_dir = "file_example"
structure_file = "example/structure.yml"
results = pubmedparser.read_xml(files, structure_file, data_dir)

See the example file for more options.

In python, the structure file can be replaced with a dictionary of dictionaries as well.

Or, as a CLI:

xml_read --cache-dir=cache --structure-file=structure.yml \
    data/*.xml.gz

Installing with pip

pip install pubmedparser2

Building python package

Requires zlib.

Clone the repository and cd into the directory. Then use poetry to build and install the package.

make python

Structure file

The structure file is a YAML file containing key-value pairs for different tags and paths. There are two required keys: root and key. Root provide the top-level tag, in the case of the pubmed files this will be PubmedArticleSet.

root: "/PubmedArticleSet"

The / is not strictly required as the program will ignore them, but they are used to conform to the xpath syntax (although this program does not handle all cases for xpath).

Only tags below the root tag will be considered and the parsing will terminate once the program has left the root of the tree.

Key is a reference tag. In the pubmed case, all data is with respect to a publication, so the key should identify the publication the values are linked to. The PMID tag is a suitable candidate.

key: "/PubmedArticle/MedlineCitation/PMID"

After root, all paths are taken as relative to the root node.

The other name-pairs in the file determine what other items to collect. These can either be a simple name and path, like the key, such as:

Language: "/PubmedArticle/MedlineCitation/Article/Language"
Keywords: "/PubmedArticle/MedlineCitation/KeywordList/Keyword"

Or they can use a hierarchical representation to get multiple values below a child. This is mainly used to handle lists of items where there is an indefinite number of items below the list.

Author: {
  root: "/PubmedArticle/MedlineCitation/Article/AuthorList",
  key: "/Author/auto_index",
  LastName: "/Author/LastName",
  ForeName: "/Author/ForeName",
  Affiliation: "/Author/AffiliationInfo/Affiliation",
  Orcid: "/Author/Identifier/[@Source='ORCID']"
}

Here, all paths are relative to the sub-structures root path, which is in turn relative to the parent structure's root. This sub-structure uses the same rules as the parent structure, so it needs both a root and key name-value pair. The results of searching each path are written to separate files. Each file gets a column for the parent and child key. So in this case, each element of the author is linked by an author key and that is related to the publication they authored through the parent key.

The main parser is called recursively to parse this structure so it's worth thinking about what the root should be under the context that the parser will be called with that root. This means if, instead of stopping at /AuthorList, /Author was added to the end of the root, the parser would be called for each individual author, instead of once per author list, leading to all author's getting the index 0.

There are a number of additional syntax constructs to note in the above example. The key uses the special name auto_index, since there is no author ID in the XML data, an index is used to count the authors in the order they appear. This resets for each publication and starts at 0. Treating the auto_index as the tail of a path allows you to control when the indexing occurs—the index is incremented whenever it hits a /Author tag.

In addition to the auto_index key, there is a second special index name, condensed.

Reference: {
  root: "/PubmedArticle/PubmedData/ReferenceList/Reference/ArticleIdList"
  key: "/condensed"
  PMID: "/ArticleId/[@IdType='pubmed']"
  DOI: "/ArticleId/[@IdType='doi']"
}

In the case of condensed, instead of writing the results to separate files, they will printed as columns in the same file, and therefore do not need an additional key for the sub-structure. If any of the elements are missing, they will be left blank, for example, if the parser does not find a pubmed ID for a given reference, the row will look like "%s\t\t%s" where the first string will contain the parent key (the PMID of the publication citing this reference) and the second string will contain the reference's DOI.

The /[@attribute='value'] syntax at the end of a path tells the parser to only collect an element if it has an attribute and the attribute's value matches the supplied value. Similarly the /@attribute syntax, tells the parser to collect the value of the attribute attribute along with the element's value. Then both values will be written to the output file. Currently only a single attribute can be specified.

Lastly, there is a special syntax for writing condensed sub-structures:

Date: "/PubmedArticle/MedlineCitation/Article/Journal/JournalIssue/PubDate/{Year,Month,Day}"

The {child,child,child} syntax allows you to select multiple children at the same level to be printed to a single file. This is useful when multiple children make up a single piece of information (i.e. the publication date).

A similar example structure file can be found in the example directory of this project at: file:./example/structure.yml.

Structure dictionary

The structure of the xml data to read can also be described as a python dictionary of dictionaries.

The form is similar to the file:

structure = {
    "root": "//PubmedArticleSet",
    "key": "/PubmedArticle/MedlineCitation/PMID",
    "DOI": "/PubmedArticle/PubmedData/ArticleIdList/ArticleId/[@IdType='doi']",
    "Date": "/PubmedArticle/MedlineCitation/Article/Journal/JournalIssue/PubDate/{Year,Month,Day}",
    "Journal": "/PubmedArticle/MedlineCitation/Article/Journal/{Title,ISOAbbreviation}",
    "Language": "/PubmedArticle/MedlineCitation/Article/Language",
    "Author": {
        "root": "/PubmedArticle/MedlineCitation/Article/AuthorList",
        "key": "/Author/auto_index",
        "LastName": "/Author/LastName",
        "ForName": "/Author/ForeName",
        "Affiliation": "/Author/AffiliationInfo/Affiliation",
        "Orcid": "/Author/Identifier/[@Source='ORCID']",
    },
    "Grant": {
        "root": "/PubmedArticle/MedlineCitation/Article/GrantList",
        "key": "/Grant/auto_index",
        "ID": "/Grant/GrantID",
        "Agency": "/Grant/Agency",
    },
    "Chemical": "/PubmedArticle/MedlineCitation/ChemicalList/Chemical/NameOfSubstance/@UI",
    "Qualifier": "/PubmedArticle/MedlineCitation/MeshHeadingList/MeshHeading/QualifierName/@UI",
    "Descriptor": "/PubmedArticle/MedlineCitation/MeshHeadingList/MeshHeading/DescriptorName/@UI",
    "Keywords": "/PubmedArticle/MedlineCitation/KeywordList/Keyword",
    "Reference": {
        "root": (
            "/PubmedArticle/PubmedData/ReferenceList/Reference/ArticleIdList"
        ),
        "key": "/condensed",
        "PMID": "/ArticleId/[@IdType='pubmed']",
        "DOI": "/ArticleId/[@IdType='doi']",
    },
}

This can then be passed to pubmedparser.read_xml in place of the structure file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pubmedparser2-2.1.3.tar.gz (41.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pubmedparser2-2.1.3-cp313-cp313-manylinux_2_39_x86_64.whl (113.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_x86_64.whl (61.2 kB view details)

Uploaded CPython 3.13macOS 15.0+ x86-64

pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_arm64.whl (61.2 kB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

pubmedparser2-2.1.3-cp312-cp312-manylinux_2_39_x86_64.whl (113.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_x86_64.whl (61.2 kB view details)

Uploaded CPython 3.12macOS 15.0+ x86-64

pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_arm64.whl (61.2 kB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

pubmedparser2-2.1.3-cp311-cp311-manylinux_2_39_x86_64.whl (113.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_x86_64.whl (61.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ x86-64

pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_arm64.whl (61.2 kB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

pubmedparser2-2.1.3-cp310-cp310-manylinux_2_39_x86_64.whl (112.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_x86_64.whl (42.8 kB view details)

Uploaded CPython 3.10macOS 15.0+ x86-64

pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_arm64.whl (61.2 kB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file pubmedparser2-2.1.3.tar.gz.

File metadata

  • Download URL: pubmedparser2-2.1.3.tar.gz
  • Upload date:
  • Size: 41.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pubmedparser2-2.1.3.tar.gz
Algorithm Hash digest
SHA256 10be135d1cc4ff35c771538d54414527c37b28b1c92a9b34de0a240c9df684db
MD5 608f2523137ca2a35dce7060d709138c
BLAKE2b-256 7f8e552f18f96599df19b6af0dea031cef2b5a0c08c68df7ef775fab537f5d79

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3.tar.gz:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 178cde639a91bacf020cf70b5c15a0188fdbaf3e749861127a7b07865f4f5ed0
MD5 d4df236f2ca05e06f9186b4a9872c731
BLAKE2b-256 09fde447766ad84a19d35efa5a5ace4c540ac64449201044df33e3e24ae08686

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp313-cp313-manylinux_2_39_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 0c9573a93e98cc9b5da7d2a8f3cb464da6743780cd94e2a66638aa5a0616dfb5
MD5 ad96a422870ce61fe3de4abacaddd8d5
BLAKE2b-256 1a917dabb36b15d8805b94ec5c587b25496ea14228a936367dcbb1fc464db3fa

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a72ff4e967cc45018a6642e866c64cfd62242b9bc764c83a5387aa3b8eb750cc
MD5 dfa76ccd25f9a6847a2606b1ced54cef
BLAKE2b-256 9cdb3956f567cf6a8da1b9310483b19e37e257b5ef405388b8de0a3f2cfb99f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp313-cp313-macosx_15_0_arm64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 e3d44b50754956b80bd9a3ca2f883b7e803f5c48288ffde64e49158ac9ebdcf2
MD5 8235120ac537d5b0d319effa84291f60
BLAKE2b-256 846b6be2ebe60cca4f25c840547cef09e48ee46f20b26c2abef28716a5f09d12

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp312-cp312-manylinux_2_39_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 6b883984434d032d94fae07a88dd8c3c04b8ed86edabfaf0f3a72b6fb1b766c6
MD5 0f75acf62cdb86197d681e73feb6e592
BLAKE2b-256 dd25e66f5f84b1be779b3eda7cd03e3126fd381d973c349ba70612c7ceb704e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 669abd91d1a77ae8cf111fbe8b252906fd28819c0164ac0981674ff3eb5221f3
MD5 d86bd078836c77297fc34347ad2e367e
BLAKE2b-256 81440fccf936808be6049f66f80c26a6dad839aee1b5cd8746200aad5ea9fc64

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp312-cp312-macosx_15_0_arm64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3b60e02797feceee6b41094495014f2cb23cb171d872fbb581ec4e4df4253e5f
MD5 3862e008b7d45ac31e26ed1462a05ba7
BLAKE2b-256 e24b0e8052eaa7f8a1c3748e6ada0c16ebcce6ce4b78fee4eac3484b7bedc7a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp311-cp311-manylinux_2_39_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 496b91dd8ab47dc880c27bb5c521e86212ed9ac8edeaed5c17cdc9f538eb2a61
MD5 935d1c5bf3b336e1fdbe04d3a36a37a9
BLAKE2b-256 d1c9cee72bb0401a0ecc7340200aef45e44f05e50c631340db093ffafe44ea61

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 90f516ee53e0841c0801b802b194da9cdbe9ccd9327ce5e4402ad20036d8fdbe
MD5 36d987ff7a07f2b45ccc053d5ffdb234
BLAKE2b-256 b9ed4c6df4543b7b6e6deea4dff8a4bd5020ad270da2722ee951069bd72a1fcc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp311-cp311-macosx_15_0_arm64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 257888f6df1ec3c18687657e3963e7c207815599c8b08a32202dcb4ee32c5704
MD5 8a0ee8c2c4e179d3f011a65be45ac2f5
BLAKE2b-256 5763d99feb0d67fe2327c21461bec8698537decd75329473ae79973a9b01f87c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp310-cp310-manylinux_2_39_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_x86_64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_x86_64.whl
Algorithm Hash digest
SHA256 b541374e26bb181d2cd5df8d490aa3e3a6a2b930a09e515db4bff6dfc4428cb9
MD5 de98040a2553f94cc435b606af552557
BLAKE2b-256 e1316005d1037ef9c1a38e1a5838c88189ee4c97b4b8465249910aab83f9a593

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_x86_64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 a121a70f21fff227628f4365580a59a0fc850c9250b64188ecd8e4a135d8b197
MD5 20ea49d22fc9fa4944ca6e7f2a32f963
BLAKE2b-256 33b42c4d5bd553f3bc0d09190ff264bdb847b631e08330153cf715470da11bb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pubmedparser2-2.1.3-cp310-cp310-macosx_15_0_arm64.whl:

Publisher: release.yaml on net-synergy/pubmedparser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page