Skip to content

Instantly share code, notes, and snippets.

@egonw
Last active December 14, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egonw/7f510fc51070414e0fe10e84dbf8c603 to your computer and use it in GitHub Desktop.
Save egonw/7f510fc51070414e0fe10e84dbf8c603 to your computer and use it in GitHub Desktop.
Tool that takes a DOI and PubMed ID and creates Quickstatements for Wikidata
# (C) 2019 Andra Waagmeester
# 2019 Egon Willighagen
#
# If you used this code, please cite this paper: https://www.biorxiv.org/content/10.1101/799684v1
#
# Install first (see https://pypi.org/project/wikidataintegrator/):
#
# pip install wikidataintegrator
#
# MIT License
from wikidataintegrator import wdi_core, wdi_login, wdi_helpers
import os
import sys
if "WDUSER" in os.environ and "WDPASS" in os.environ:
WDUSER = os.environ['WDUSER']
WDPASS = os.environ['WDPASS']
else:
raise ValueError("WDUSER and WDPASS must be specified in local.py or as environment variables")
if not sys.argv[1]:
raise ValueError("An input identifier (DOI or PubMed) should be provided")
login = wdi_login.WDLogin(WDUSER, WDPASS)
def processIdentifier(arg):
if arg.startswith("10."):
doi = arg
qid = wdi_helpers.PublicationHelper(
doi, id_type="doi",
source="crossref").get_or_create(login if True else None)
print(doi + ": " + str(qid))
else:
pmid = arg
qid = wdi_helpers.PublicationHelper(
pmid, id_type="pmid",
source="europepmc").get_or_create(login if True else None)
print(pmid + ": " + str(qid))
if (len(sys.argv)==2 and sys.argv[1] == "--"):
print("reading from stdin\n")
for line in sys.stdin:
processIdentifier(line.rstrip())
elif len(sys.argv)>1:
for x in sys.argv:
processIdentifier(str(x))
@egonw
Copy link
Author

egonw commented Dec 14, 2019

Example use:

cat dois.txt | WDPASS=$YOURCODE WDUSER="Your WikidataAccount" python3 pubmed.py --

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment