-
Notifications
You must be signed in to change notification settings - Fork 0
/
doi2pmid_interactive_EuropePMC.py
executable file
·71 lines (56 loc) · 3 KB
/
doi2pmid_interactive_EuropePMC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" Find publication title, DOI or PMID based on either the DOI or the PMID as the query term by using the Europe PMC API (http://europepmc.org/RestfulWebService#!/Europe32PMC32Articles32RESTful32API/search). """
__author__ = "Ray Stefancsik"
__version__ = "0.01"
######################################################################
# Import libraries to help work with JSON.
import jmespath
import json
######################################################################
# Import libraries to make API requests
import requests
######################################################################
# Import command-line parsing module from the Python standard library.
######################################################################
import argparse
parser = argparse.ArgumentParser( description='You must provide either a Digital Object Identifier or a PMID to query Europe PMC in a format starting with either "DOI:" or "EXT_ID", respectively. Keyword parameters are case insensitive. For example "doi:10.1016/j.stem.2019.12.005" or "ext_id:31928944". For publication title queries, use syntax: \'TITLE:"Article title."\'', formatter_class=argparse.RawTextHelpFormatter )
parser.add_argument( 'doi_OR_pmid', help='Use "doi:" for Digital Object Identifier queries. Use "ext_id:" for PMID queries.' )
args = parser.parse_args()
#################################################################
### Global variables to construct API endpoint strings
#################################################################
endpoint = 'https://www.ebi.ac.uk/europepmc/webservices/rest/search'
# input argument
queryString = args.doi_OR_pmid
# endpoint example string: "https://api.ingest.archive.data.humancellatlas.org/submissionEnvelopes/5d1c67c688fa640008aff7cc/biomaterials"
def doi2pmid(query):
""" Query Europe PMC API using a DOI. Return the associated PMID if it exists in the JSON returned by the API."""
parameters = {
'query' : query,
'resultType' : 'lite', ## alternative: 'idlist',
'cursorMark' : '*',
'pageSize' : 25,
'format' : 'json'
}
response = requests.get(endpoint, params = parameters)
# print('endpoint:', response.url)
data = response.json()
foundTITLEs = jmespath.search('resultList.result[*].title', data)
foundPMIDs = jmespath.search('resultList.result[*].pmid', data)
foundDOIs = jmespath.search('resultList.result[*].doi', data)
if len(foundTITLEs) == 1: # check if there is a single hit or not
foundTitle = foundTITLEs[0]
else:
foundTitle = foundTITLEs
if len(foundPMIDs) == 1: # check if there is a single hit or not
foundPMID = foundPMIDs[0]
else:
foundPMID = foundPMIDs
if len(foundDOIs) == 1: # check if there is a single hit or not
foundDOI = foundDOIs[0]
else:
foundDOI = foundDOIs
results = { 'TITLE' : foundTitle, 'PMID' : foundPMID, 'DOI' : foundDOI }
return(results)
print ( 'Query:', queryString, 'Results:', doi2pmid(queryString))