-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBigGIM_Parser.py
587 lines (487 loc) · 21 KB
/
BigGIM_Parser.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# Author: Guangrong Qin
# Institute for Systems Biology
# Aug 16, 2022
# reference of biolink model:
# https://github.com/biolink/biolink-model/blob/master/biolink-model.yaml
# infores CURIE: https://docs.google.com/spreadsheets/d/1Ak1hRqlTLr1qa-7O0s5bqeTHukj9gSLQML1-lg6xIHM/edit#gid=293462374
# KG standarization: https://docs.google.com/document/d/1SlqN7M1LTgwBcoIuEAlUnxkJ3xzTmKJJ327zO9Vw5uQ/edit
import os
import validators
import pandas as pd
############# Part I: KG overview ####################
def graph_stat(G_G_KG_formated):
num_subject_ids = len(set(G_G_KG_formated['subject_id']))
num_object_ids = len(set(G_G_KG_formated['object_id']))
num_edges = G_G_KG_formated.shape[0]
num_unique_nodes = len(set(list(G_G_KG_formated['subject_id']) + list(G_G_KG_formated['object_id'])))
num_unique_association = len(set(G_G_KG_formated['predicate']))
dic_node_degree = {}
node_degree_list = []
node_list = list(set(list(set(G_G_KG_formated['subject_id'])) + list(set(G_G_KG_formated['object_id']))))
for node in node_list:
x1 = list(G_G_KG_formated.loc[G_G_KG_formated['subject_id'] == node].index)
x2 = list(G_G_KG_formated.loc[G_G_KG_formated['object_id'] == node].index)
x = set(x1 + x2)
node_degree_list.append(len(x))
dic_node_degree[node] = len(x)
node_degree_max = max(node_degree_list)
print("max degree: " + str(node_degree_max))
print("num of subject ids:" + str(num_subject_ids))
print("num of object ids:" + str(num_object_ids))
print("number of edges:" + str(num_edges))
print("number of unique nodes: " + str(num_unique_nodes))
print("number of association categories: " + str(num_unique_association))
############# Part II: KG parser ####################
# Pre-define associations
CORRELATION_STATISTIC = {
"T_test": {
# Correlation Test -- http://purl.obolibrary.org/obo/NCIT_C53236
"attribute_type_id": "NCIT:C53236",
"description": "t-test was used to compute the p-value for the association",
"value": "NCIT:C53231", # t-Test -- http://purl.obolibrary.org/obo/NCIT_C53231
"value_type_id": "biolink:id"
},
"Spearman_correlation": {
# Correlation Test -- http://purl.obolibrary.org/obo/NCIT_C53236
"attribute_type_id": "NCIT:C53236",
"description": "Spearman Correlation Test was used to extract the association",
# Spearman Correlation Test -- http://purl.obolibrary.org/obo/NCIT_C53249
"value": "NCIT:C53249",
"value_type_id": "biolink:id",
},
"Wilcoxon-test": {
"attribute_type_id": "NCIT:C53246",
"description": "Wilcoxon-test was used to compute the p-value for the association",
"value": "NCIT:C53246",
"value_type_id": "biolink:id",
},
"Pearson_correlation": {
"attribute_type_id": "NCIT:C53244",
"description": "Pearson correlation was used to extract the association",
# Pearson correlation test -- https://ontobee.org/ontology/NCIT?iri=http://purl.obolibrary.org/obo/NCIT_C53244
"value": "NCIT:C53246",
"value_type_id": "biolink:id",
}
}
DIC_XREF = {
'NCBIGene': "https://www.ncbi.nlm.nih.gov/gene/",
'Pubchem.compound': "https://pubchem.ncbi.nlm.nih.gov/compound/",
'PUBCHEM.COMPOUND': "https://pubchem.ncbi.nlm.nih.gov/compound/",
'pubchem.compound': "https://pubchem.ncbi.nlm.nih.gov/compound/",
'pubchem.cid': "https://pubchem.ncbi.nlm.nih.gov/compound/",
'MONDO': "http://purl.obolibrary.org/obo/MONDO_",
"CHEBI": "https://www.ebi.ac.uk/chebi/chebiOntology.do?chebiId=CHEBI:",
"CHEMBL": "https://www.ebi.ac.uk/chembl/compound_report_card/CHEMBL",
"ENSEMBL": "https://uswest.ensembl.org/Homo_sapiens/Gene/Summary?db=core;g=",
"NCIT": "https://ontobee.org/ontology/NCIT?iri=http://purl.obolibrary.org/obo/NCIT_",
"CL":"http://purl.obolibrary.org/obo/"
}
def get_xref(id_prefix, _id):
if id_prefix in DIC_XREF:
if id_prefix == "MONDO":
return DIC_XREF[id_prefix] + str(_id).split(":")[1] #MONDO ID in the table is labeled as MONDO:0019391
else:
return DIC_XREF[id_prefix] + str(_id)
else:
print("id_prefix not recognized!")
return None
def validate_xref(validated_xrefs, xref):
if xref not in validated_xrefs:
if validators.url(xref):
validated_xrefs.add(xref)
else:
print(f"{xref} is not valid. ")
def header_check(file_formated):
file_columns = set(file_formated.columns.values.tolist())
# minumus columns:
# 'subject_id', 'subject_category', 'subject_name', 'subject_id_prefix',
# 'object_id', 'object_category', 'object_name', 'object_id_prefix',
# 'predicate', 'Knowledge_source', 'P_value'
essential_columns = [
"subject_id",
"subject_category",
"subject_name",
"subject_id_prefix",
"object_id",
"object_category",
"object_name",
"object_id_prefix",
"predicate",
# "knowledge_source"
]
for col in essential_columns:
if col not in file_columns:
print(f"Missing essential column {col}")
return False
return True
def _parse_party(row, party):
"""For given input row and party, generate the subject/object json
Args:
row: a row of a dataframe
party: either "object" or "subject" (no error handling)
"""
prefix_column = f"{party}_id_prefix"
id_column = f"{party}_id"
name_column = f"{party}_name"
type_column = f"{party}_category"
prefix = row[prefix_column]
prefix_field_name = '_'.join(prefix.split('.'))
raw_id = row[id_column]
if type(raw_id) == float:
raw_id = int(raw_id)
raw_id = str(raw_id)
if prefix == "MONDO" or prefix == "CL":
_id = raw_id
else:
_id = f"{prefix}:{raw_id}"
xref = get_xref(prefix, raw_id)
if prefix == "CL":
_id = f"{prefix}"+":"+f"{raw_id}".split('_')[1]
json = {
"id": _id,
prefix_field_name: _id,
"name": row[name_column],
"type": row[type_column],
"xref": xref
}
return json
def parse_subject(row):
return _parse_party(row, "subject")
def parse_object(row):
return _parse_party(row, "object")
def parse_sub_attribute(source, infores_dict):
"""Parse sub-attributes for column "knowledge_source" or "Data_set"
Args:
source (str): the content of column "knowledge_source" or "Data_set"
infores_dict (dict): a dict of infores keys (to match against source) and values (as part of the returned attributes). E.g.
{"GTEx": "infores:gtex", "HuRI": "infores:HuRI"}
"""
def _parse_publication(source):
if 'PMID' in source:
new_values = source.split(":")
new_value_PMID = new_values[0].strip() + ":" + new_values[1].strip()
attribute = {
# "description": "Publication describing the dataset used to compute the association",
# "value_type_id": "biolink:id"
"attribute_type_id": "biolink:Publication",
"value": new_value_PMID,
}
return attribute
return None
def _parse_source_url(source):
url = None
if validators.url(source):
url = source
elif source.startswith("www."):
for candidate_url in [f"https://{source}", f"http://{source}"]:
if validators.url(candidate_url):
url = candidate_url
break
if url is not None:
attribute = {
# "description": "source url describing the dataset used to compute the association",
# "value_type_id": "biolink:id"
"attribute_type_id": "biolink:source_url",
"value": url
}
return attribute
return None
def _parse_source_infores(source, infores_dict):
for infores_key, infores_value in infores_dict.items():
if source == infores_key:
attribute = {
# "description": "source infores describing association",
# "value_type_id": "biolink:id"
"attribute_type_id": "biolink:source_infores",
"value": infores_value,
}
return attribute
return None
def _default_attribute(source):
return {"attribute_type_id": "Biolink:Dataset", "value": source}
# main function body starts
# return the first non-None value
attribute = _parse_publication(source) or _parse_source_url(source) or \
_parse_source_infores(source, infores_dict) or _default_attribute(source)
return attribute
def parse_edge_attributes(row, column_names):
edge_attributes = []
# aggregator_knowledge_source
# edge_attributes.append({
# "attribute_type_id": "biolink:aggregator_knowledge_source",
# "value": "infores:biothings-multiomics-biggim-drugresponse"
# })
# creation_date
# edge_attributes.append({"attribute_type_id": "biolink:creation_date","value": Date})
# resource_url
# edge_attributes.append({"attribute_type_id": "biolink:supporting_study_method_description",
# "value": "https://github.com/NCATSTranslator/Translator-All/wiki/MultiomicsBigGIM_KP"})
# P_value
if "statistics_method" in column_names and "P_value" in column_names:
edge_attributes.append({
# statistical estimate score -- http://edamontology.org/data_0951
"attribute_type_id": "EDAM:data_0951",
# "description": "Confidence metric for the association",
"value": float(row["P_value"]),
"value_type_id": "EDAM:data_1669", # P-value -- http://edamontology.org/data_1669
# sub-attributes should be a list per TRAPI standard
"attributes": [CORRELATION_STATISTIC[row['statistics_method']]]
})
# Sample size
if "supporting_study_size" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:supporting_study_size",
# "description": "The sample size used in a study that provided evidence for the association",
"value": int(row['supporting_study_size']),
})
# Datasets for extracting the knowledge graphs
if "Data_set" in column_names:
source = row["Data_set"]
# infores_dict = {"GTEx": "infores:gtex"}
# attribute = parse_sub_attribute(source, infores_dict)
edge_attributes.append({
# "description": "Dataset used to compute the association",
"attribute_type_id": "biolink:dataset",
# "attribute_type_id": "biolink:Data_source",
"value": source,
# "attributes": [attribute] # sub-attributes should be a list per TRAPI standard
})
# publications
if "publications" in column_names:
publications = row["publications"]
if 'PMID' in publications:
new_values = publications.split(":")
new_value_PMID = new_values[0].strip() + ":" + new_values[1].strip()
edge_attributes.append({
"attribute_type_id": "biolink:publications",
"value": new_value_PMID,
})
else:
edge_attributes.append({
"attribute_type_id": "biolink:publications",
"value": publications,
})
# knowledge graphs for extract the association
if "knowledge_source" in column_names:
source = row["knowledge_source"]
infores_dict = {
"Biogrid": "infores:biogrid",
"HuRI": "infores:huri",
"DrugCentral": "infores:drugcentral",
"http://www.interactome-atlas.org/download": "infores:huri",
"TTD_2021": "infores:ttd",
"CellMarker": "infores:cellmarker2.0",
}
attribute = parse_sub_attribute(source, infores_dict)
if source in infores_dict:
edge_attributes.append({
"attribute_type_id": "biolink:primary_knowledge_source",
# "value": source,
"value": infores_dict[source],
# "value_type_id": None,
"attributes": [attribute] # sub-attributes should be a list per TRAPI standard
})
edge_attributes.append({
"attribute_type_id": "biolink:aggregator_knowledge_source",
"value": "infores:biothings-multiomics-biggim-drugresponse"
})
elif source == "BigGIM_DRUGRESPONSE":
edge_attributes.append({
"attribute_type_id": "biolink:primary_knowledge_source",
"value": "infores:biothings-multiomics-biggim-drugresponse"
})
elif source == "BigGIM":
edge_attributes.append({
"attribute_type_id": "biolink:primary_knowledge_source",
"value": "infores:biggim"
})
elif source == "CellMarker":
edge_attributes.append({
"attribute_type_id": "biolink:primary_knowledge_source",
"value": "infores:cellmarker2.0"
})
elif "PMID" in source:
edge_attributes.append({
"attribute_type_id": "biolink:primary_knowledge_source",
"value": "infores:pubmed"
})
edge_attributes.append({
"attribute_type_id": "biolink:publications",
"value": source
})
# add more optional associations
# Qualifiers
if "subject_aspect_qualifier" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:subject_aspect_qualifier", # biolink version 3.0.0
"value": row["subject_aspect_qualifier"]
})
if "object_aspect_qualifier" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:object_aspect_qualifier", # biolink version 3.0.0
"value": row["object_aspect_qualifier"]
})
# disease context
if "context_qualifier" in column_names:
edge_attributes.append({
"attribute_source": "infores:biothings-multiomics-biggim-drugresponse",
"attribute_type_id": "biolink:context_qualifier", # biolink version 3.0.0
"value": row['context_qualifier'],
# "value_type_id": "biolink:id",
})
# anatomical context qualifier
if "anatomical_context_qualifier" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:anatomical_context_qualifier", # biolink version 3.0.0
"value": row['anatomical_context_qualifier'],
})
# frequence qualifier
if "frequency_qualifier" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:frequency_qualifier", # biolink version 3.0.0
"value": row['frequency_qualifier'],
})
# supporting_study_cohort
if "supporting_study_cohort" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:supporting_study_cohort", # biolink version 3.0.0
"value": row['supporting_study_cohort'],
})
# has_count
if "has_count" in column_names:
edge_attributes.append({
"attribute_type_id": "biolink:has_count", # biolink version 3.0.0
"value": row['has_count'],
})
return edge_attributes
def parse_source_attribute(row, column_names):
source_attributes = []
infores_dict = {
"Biogrid": "infores:biogrid",
"HuRI": "infores:huri",
"DrugCentral": "infores:drugcentral",
"http://www.interactome-atlas.org/download": "infores:huri",
"TTD_2021": "infores:ttd",
"TCGA": "infores:tcga",
"GDSC": "infores:gdsc",
"GTEx": "infores:gtex",
"CellMarker": "infores:cellmarker2.0"
}
if "knowledge_source" in column_names:
source = row["knowledge_source"]
# attribute = parse_sub_attribute(source, infores_dict)
if source in infores_dict:
source_attributes.append({
"resource_id": infores_dict[source],
"resource_role": "primary_knowledge_source"
})
source_attributes.append({
"resource_id": "infores:biothings-multiomics-biggim-drugresponse",
"resource_role": "aggregator_knowledge_source",
"upstream_resource_ids": [infores_dict[source]]
})
elif source == "BigGIM_DRUGRESPONSE":
source_attributes.append({
"resource_id": "biolink:primary_knowledge_source",
"resource_role": "primary_knowledge_source"
})
elif source == "BigGIM":
source_attributes.append({
"resource_id": "infores:biothings-multiomics-biggim-drugresponse",
"resource_role": "primary_knowledge_source"
})
elif "PMID" in source:
source_attributes.append({
"resource_id": "infores:pubmed",
"resource_role": "primary_knowledge_source"
})
source_attributes.append({
"resource_id": "infores:biothings-multiomics-biggim-drugresponse",
"resource_role": "aggregator_knowledge_source",
"upstream_resource_ids": [source]
})
else:
source_attributes.append({
"resource_id": "infores:biothings-multiomics-biggim-drugresponse",
"resource_role": "primary_knowledge_source"
})
else:
source_attributes.append({
"resource_id": "infores:biothings-multiomics-biggim-drugresponse",
"resource_role": "primary_knowledge_source"
})
if "Data_set" in column_names:
source = row["Data_set"]
# attribute = parse_sub_attribute(source, infores_dict)
if source in infores_dict:
source_attributes.append({
"resource_id": infores_dict[source],
"resource_role": "supporting_data_source"
})
elif "PMID" in source:
source_attributes.append({
"resource_id": "infores:pubmed",
"resource_role": "supporting_data_source",
# "resource_name": source
})
if "supporting_study_cohort" in column_names:
if row['supporting_study_cohort'] in infores_dict:
source_attributes.append({
"resource_id": infores_dict[row['supporting_study_cohort']],
"resource_role": "supporting_data_set"} ) # biolink version 3.0.0
else:
print("supporting_study_cohort not in infores_dict")
return source_attributes
def load_tsv_data(file_path):
file_formated = pd.read_csv(file_path)
file_formated = file_formated.dropna()
if not header_check(file_formated):
print(f"file {file_path} misses essential headers.")
return
column_names = file_formated.columns.values.tolist()
validated_xrefs = set()
for index, row in file_formated.iterrows():
# Get the dictionary of subject, object and associations
subject_json = parse_subject(row)
validate_xref(validated_xrefs, subject_json["xref"])
object_json = parse_object(row)
validate_xref(validated_xrefs, object_json["xref"])
predicates = "biolink:" + '_'.join(row['predicate'].split(' '))
edge_attributes = parse_edge_attributes(row, column_names)
source_attributes = parse_source_attribute(row, column_names)
association_json = {
"edge_label": predicates,
"edge_attributes": edge_attributes,
"source_attributes": source_attributes
}
json = {
# "_id":'-'.join(unique_id_list),
"_id": subject_json["type"] + "_" + predicates + "_" + object_json["type"] + "_" + file_path.split("/")[-1] + "_" + str(index),
"subject": subject_json,
"object": object_json,
"predicate": association_json["edge_label"],
"attributes": association_json["edge_attributes"],
"sources": association_json["source_attributes"]
}
yield json
def load_data(data_folder):
file_names = [
"Input_DrugResponse_expr_auc_gdsc_08312022.csv",
"Input_DrugResponse_mut_IC50_gdsc_08312022.csv",
"TCGA_driver_mut_freq.csv",
"FA_mut.csv",
"GDSC_cancer_specific_signatures.csv",
"Drug_target_with_primary_source.csv",
"Biogrid_formated.csv",
"H-I-05_formated.csv",
"HI-II-14_formated.csv",
"HuRI_formated.csv",
"Yang-16_formated.csv",
"GTEX_liver_negative_correlated_formated.csv",
"GTEX_liver_positively_correlated_formated.csv",
"cellmarker.csv"
]
file_paths = [os.path.join(data_folder, fn) for fn in file_names]
for file_path in file_paths:
for json in load_tsv_data(file_path):
yield json