forked from RTXteam/RTX-KG2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrugcentral_json_to_kg_jsonl.py
executable file
·298 lines (259 loc) · 12.3 KB
/
drugcentral_json_to_kg_jsonl.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python3
''' drugcentral_json_to_kg_json.py: Converts the DrugCentral JSON
file into a KG JSON file
Usage: drugcentral_json_to_kg_json.py [--test] <inputFile.txt>
<outputNodesFile.json> <outputEdgesFile.json>
'''
import json
import argparse
import kg2_util
import datetime
__author__ = 'Erica Wood'
__copyright__ = 'Oregon State University'
__credits__ = ['Stephen Ramsey', 'Erica Wood']
__license__ = 'MIT'
__version__ = '0.1.0'
__maintainer__ = ''
__email__ = ''
__status__ = 'Prototype'
CURIE_PREFIX_DRUGCENTRAL = kg2_util.CURIE_PREFIX_DRUGCENTRAL
BASE_URL_DRUGCENTRAL = kg2_util.BASE_URL_DRUGCENTRAL
DRUGCENTRAL_SOURCE = kg2_util.CURIE_ID_DRUGCENTRAL_SOURCE
DRUGCENTRAL_RELATION_CURIE_PREFIX = kg2_util.CURIE_PREFIX_DRUGCENTRAL
TEST_MODE_EDGE_COUNT = 10000
def get_args():
description = "drugcentral_json_to_kg_json.py: converts the DrugCentral \
JSON file into a KG JSON file"
arg_parser = argparse.ArgumentParser(description=description)
arg_parser.add_argument('inputFile', type=str)
arg_parser.add_argument('outputNodesFile', type=str)
arg_parser.add_argument('outputEdgesFile', type=str)
arg_parser.add_argument('--test', dest='test',
action="store_true", default=False)
return arg_parser.parse_args()
def date():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def format_drugcentral_id(struct_id):
return CURIE_PREFIX_DRUGCENTRAL + ':' + struct_id
def format_edge(subject_id, object_id, predicate, update_date):
relation_curie = kg2_util.predicate_label_to_curie(predicate,
DRUGCENTRAL_RELATION_CURIE_PREFIX)
if predicate == kg2_util.EDGE_LABEL_BIOLINK_SAME_AS:
return kg2_util.make_edge_biolink(subject_id,
object_id,
predicate,
DRUGCENTRAL_SOURCE,
update_date)
else:
return kg2_util.make_edge(subject_id,
object_id,
relation_curie,
predicate,
DRUGCENTRAL_SOURCE,
update_date)
def process_external_ids(external_ids, edges_output, update_date, test_mode):
edge_count = 0
prefix_map = {'MESH_DESCRIPTOR_UI': kg2_util.CURIE_PREFIX_MESH,
'RXNORM': None,
'VANDF': kg2_util.CURIE_PREFIX_VANDF,
'IUPHAR_LIGAND_ID': None,
'PDB_CHEM_ID': None,
'INN_ID': None,
'SECONDARY_CAS_RN': None,
'SNOMEDCT_US': kg2_util.CURIE_PREFIX_SNOMED,
'CHEBI': kg2_util.CURIE_PREFIX_CHEBI,
'NDDF': None,
'NUI': None,
'UNII': None,
'MESH_SUPPLEMENTAL_RECORD_UI': kg2_util.CURIE_PREFIX_MESH,
'PUBCHEM_CID': None,
'VUID': None,
'KEGG_DRUG': kg2_util.CURIE_PREFIX_KEGG_DRUG,
'DRUGBANK_ID': kg2_util.CURIE_PREFIX_DRUGBANK,
'MMSL': None,
'UMLSCUI': kg2_util.CURIE_PREFIX_UMLS,
'ChEMBL_ID': kg2_util.CURIE_PREFIX_CHEMBL_COMPOUND}
for source_predicate in external_ids:
edge_count += 1
if test_mode and edge_count > TEST_MODE_EDGE_COUNT:
break
prefix = prefix_map.get(source_predicate['id_type'], None)
if prefix is None or \
len(source_predicate['struct_id']) < 1 or \
len(source_predicate['identifier']) < 1:
continue
drug_central_id = format_drugcentral_id(source_predicate['struct_id'])
external_id = prefix + ':' + source_predicate['identifier']
predicate = kg2_util.EDGE_LABEL_BIOLINK_SAME_AS
edge = format_edge(drug_central_id,
external_id,
predicate,
update_date)
edges_output.write(edge)
def process_omop_relations(omop_relations, edges_output, update_date, test_mode):
edge_count = 0
for source_predicate in omop_relations:
edge_count += 1
if test_mode and edge_count > TEST_MODE_EDGE_COUNT:
break
umls_id = source_predicate['umls_cui']
doid_id = source_predicate['doid']
drug_central_id = source_predicate['struct_id']
predicate = source_predicate['relationship_name'].replace(' ', '_')
if (len(doid_id) < 1 and len(umls_id) < 1) or \
len(drug_central_id) < 1 or \
len(predicate) < 1:
continue
object_id = ''
if len(doid_id) < 1:
object_id = kg2_util.CURIE_PREFIX_UMLS + ':' + umls_id
else:
object_id = kg2_util.CURIE_PREFIX_DOID + ':' + doid_id.replace('DOID:', '')
drug_central_id = format_drugcentral_id(drug_central_id)
edge = format_edge(drug_central_id, object_id, predicate, update_date)
edges_output.write(edge)
def process_faers_data(faers_edges, edges_output, update_date, test_mode):
edge_count = 0
for source_predicate in faers_edges:
edge_count += 1
if test_mode and edge_count > TEST_MODE_EDGE_COUNT:
break
meddra_id = source_predicate['meddra_code']
drug_central_id = source_predicate['struct_id']
predicate = 'has_faers'
likelihood = float(source_predicate['llr'])
likelihood_threshold = float(source_predicate['llr_threshold'])
if likelihood < likelihood_threshold:
continue
if len(meddra_id) < 1 or len(drug_central_id) < 1:
continue
meddra_id = kg2_util.CURIE_PREFIX_MEDDRA + ':' + meddra_id
drug_central_id = format_drugcentral_id(drug_central_id)
edge = format_edge(drug_central_id, meddra_id, predicate, update_date)
edges_output.write(edge)
def process_atc_codes(atc_codes, edges_output, update_date, test_mode):
edge_count = 0
for source_predicate in atc_codes:
edge_count += 1
if test_mode and edge_count > TEST_MODE_EDGE_COUNT:
break
atc_id = source_predicate['atc_code']
drug_central_id = source_predicate['struct_id']
if len(atc_id) < 1 or len(drug_central_id) < 1:
continue
atc_id = kg2_util.CURIE_PREFIX_ATC + ':' + atc_id
drug_central_id = format_drugcentral_id(drug_central_id)
predicate = 'struct2atc'
edge = format_edge(drug_central_id, atc_id, predicate, update_date)
edges_output.write(edge)
def format_publication(url):
pubmed_url = 'http://www.ncbi.nlm.nih.gov/pubmed/'
return kg2_util.CURIE_PREFIX_PMID + ':' + url.replace(pubmed_url, '')
def process_bioactivities(bioactivities, edges_output, update_date, test_mode):
edge_count = 0
for source_predicate in bioactivities:
edge_count += 1
if test_mode and edge_count > TEST_MODE_EDGE_COUNT:
break
publications = []
action_type = source_predicate['action_type'].lower().replace(' ', '_')
moa_source = source_predicate['moa_source']
moa_source_url = source_predicate['moa_source_url']
drug_central_id = format_drugcentral_id(source_predicate['struct_id'])
act_source = source_predicate['act_source']
act_source_url = source_predicate['act_source_url']
if len(act_source_url) > 0 and act_source == "SCIENTIFIC LITERATURE":
publications.append(format_publication(act_source_url))
if len(moa_source_url) > 0 and moa_source == "SCIENTIFIC LITERATURE":
publications.append(format_publication(moa_source_url))
if len(action_type) < 1:
continue
for accession in source_predicate['accession'].split('|'):
if len(accession) < 1:
continue
uniprot_id = kg2_util.CURIE_PREFIX_UNIPROT + ':' + accession
edge = format_edge(drug_central_id,
uniprot_id,
action_type,
update_date)
edge['publications'] = publications
edges_output.write(edge)
def process_pharmacologic_actions(pharm_acts, edges_output, update_date, test_mode):
prefix_map = {'CHEBI': kg2_util.CURIE_PREFIX_CHEBI,
'FDA': None,
'MeSH': kg2_util.CURIE_PREFIX_MESH}
edge_count = 0
for action in pharm_acts:
edge_count += 1
if test_mode and edge_count > TEST_MODE_EDGE_COUNT:
break
prefix = prefix_map.get(action['source'], None)
if prefix is None:
continue
predicate = action['type'].replace(' ', '_')
chebi_pr = kg2_util.CURIE_PREFIX_CHEBI + ':'
object_id = prefix + ':' + action['class_code'].replace(chebi_pr, '')
drug_central_id = format_drugcentral_id(action['struct_id'])
edge = format_edge(drug_central_id, object_id, predicate, update_date)
edges_output.write(edge)
def make_nodes(drugcentral_ids, nodes_output, update_date):
reformatted_json = dict()
category_label = kg2_util.BIOLINK_CATEGORY_CHEMICAL_ENTITY
for name_row in drugcentral_ids:
drug_central_id = name_row['id']
name = name_row['name']
if len(drug_central_id) < 1:
continue
drug_central_id = format_drugcentral_id(drug_central_id)
if drug_central_id not in reformatted_json:
reformatted_json[drug_central_id] = dict()
reformatted_json[drug_central_id]['synonyms'] = []
# I'm not sure what to do with the "preferred name" condition since there aren't any synonyms that I see
# if name_row['preferred_name'] == "1":
# reformatted_json[drug_central_id]['name'] = name
# else:
# reformatted_json[drug_central_id]['synonyms'].append(name)
reformatted_json[drug_central_id]['name'] = name
for node_id in reformatted_json:
synonyms = reformatted_json[node_id]['synonyms']
name = reformatted_json[node_id]['name']
iri = BASE_URL_DRUGCENTRAL + node_id.split(':')[1]
provided_by = DRUGCENTRAL_SOURCE
node = kg2_util.make_node(node_id,
iri,
name,
category_label,
update_date,
provided_by)
node['synonym'] = synonyms
nodes_output.write(node)
if __name__ == '__main__':
print("Start time: ", date())
args = get_args()
input_file_name = args.inputFile
output_nodes_file_name = args.outputNodesFile
output_edges_file_name = args.outputEdgesFile
test_mode = args.test
nodes_info, edges_info = kg2_util.create_kg2_jsonlines(test_mode)
nodes_output = nodes_info[0]
edges_output = edges_info[0]
with open(input_file_name, 'r') as input_file:
json_data = json.load(input_file)
update_date = json_data['version'][0]['dtime']
version_number = json_data['version'][0]['version as version_number']
process_external_ids(json_data['external_ids'], edges_output, update_date, test_mode)
process_omop_relations(json_data['omop_relations'], edges_output, update_date, test_mode)
process_faers_data(json_data['faers_data'], edges_output, update_date, test_mode)
process_atc_codes(json_data['atc_ids'], edges_output, update_date, test_mode)
process_bioactivities(json_data['bioactivities'], edges_output, update_date, test_mode)
process_pharmacologic_actions(json_data['pharmacologic_action'], edges_output, update_date, test_mode)
nodes = make_nodes(json_data['drugcentral_ids'], nodes_output, update_date)
kp_node = kg2_util.make_node(DRUGCENTRAL_SOURCE,
BASE_URL_DRUGCENTRAL,
'DrugCentral v' + version_number,
kg2_util.SOURCE_NODE_CATEGORY,
update_date,
DRUGCENTRAL_SOURCE)
nodes_output.write(kp_node)
kg2_util.close_kg2_jsonlines(nodes_info, edges_info, output_nodes_file_name, output_edges_file_name)
print("Finish time: ", date())