-
Notifications
You must be signed in to change notification settings - Fork 21
/
QueryDisont.py
114 lines (98 loc) · 4.26 KB
/
QueryDisont.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
""" This module is the definition of class QueryDisont. It is written to connect
with disease-ontology to query disease ontology and mesh id of given disont_id.
"""
__author__ = ""
__copyright__ = ""
__credits__ = []
__license__ = ""
__version__ = ""
__maintainer__ = ""
__email__ = ""
__status__ = "Prototype"
# import requests
import sys
from cache_control_helper import CacheControlHelper
class QueryDisont:
TIMEOUT_SEC = 120
API_BASE_URL = 'http://www.disease-ontology.org/api'
@staticmethod
def send_query_get(handler, url_suffix):
requests = CacheControlHelper()
url = QueryDisont.API_BASE_URL + "/" + handler + "/" + url_suffix
# print(url_str)
try:
res = requests.get(url, timeout=QueryDisont.TIMEOUT_SEC)
except requests.exceptions.Timeout:
print(url, file=sys.stderr)
print('Timeout in QueryDisont for URL: ' + url, file=sys.stderr)
return None
except BaseException as e:
print(url, file=sys.stderr)
print('%s received in QueryDisont for URL: %s' % (e, url), file=sys.stderr)
return None
status_code = res.status_code
if status_code != 200:
print('Status code ' + str(status_code) + ' for url: ' + url, file=sys.stderr)
return None
return res
@staticmethod
def query_disont_to_child_disonts(disont_id):
"""for a disease ontology ID (including prefix "DOID:", with zero padding), return child DOIDs
:param disont_id: string, like ``'DOID:14069'``
:returns: ``set`` with keys as DOIDs
"""
res = QueryDisont.send_query_get('metadata', disont_id)
ret_set = set()
if res is not None:
res_json = res.json()
# print(res_json)
disease_children_list = res_json.get("children", None)
if disease_children_list is not None:
ret_set |= set([int(disease_child_list[1].split(':')[1]) for disease_child_list in disease_children_list])
return ret_set
@staticmethod
def query_disont_to_label(disont_id):
res = QueryDisont.send_query_get('metadata', disont_id)
ret_label = ''
if res is not None:
res_json = res.json()
ret_label = res_json.get('name', '')
return ret_label
@staticmethod
def query_disont_to_child_disonts_desc(disont_id):
"""for a disease ontology ID (including prefix "DOID:", with zero padding), return child DOIDs
:param disont_id: string, like ``'DOID:14069'``
:returns: ``dict`` with keys as DOIDs and values as human-readable disease names
"""
res = QueryDisont.send_query_get('metadata', disont_id)
ret_dict = dict()
if res is not None:
res_json = res.json()
# print(res_json)
disease_children_list = res_json.get("children", None)
if disease_children_list is not None:
ret_dict = dict([[disease_child_list[1], disease_child_list[0]] for disease_child_list in disease_children_list])
return ret_dict
@staticmethod
def query_disont_to_mesh_id(disont_id):
"""convert a disease ontology ID (including prefix "DOID:", with zero padding) to MeSH ID
:param disont_id: string, like ``'DOID:14069'``
"""
res = QueryDisont.send_query_get('metadata', disont_id)
ret_set = set()
if res is not None:
res_json = res.json()
xref_strs = res_json.get("xrefs", None)
if xref_strs is not None:
ret_set |= set([xref_str.split('MESH:')[1] for xref_str in xref_strs if 'MESH:' in xref_str])
return ret_set
if __name__ == '__main__':
print(QueryDisont.query_disont_to_label("DOID:0050741"))
print(QueryDisont.query_disont_to_mesh_id("DOID:9352"))
print(QueryDisont.query_disont_to_mesh_id("DOID:1837"))
print(QueryDisont.query_disont_to_mesh_id("DOID:10182"))
print(QueryDisont.query_disont_to_mesh_id("DOID:11712"))
print(QueryDisont.query_disont_to_child_disonts_desc("DOID:9352"))
print(QueryDisont.query_disont_to_mesh_id("DOID:14069"))
print(QueryDisont.query_disont_to_child_disonts_desc("DOID:12365"))
print(QueryDisont.query_disont_to_mesh_id("DOID:0050741"))