-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
1029 lines (828 loc) · 32.2 KB
/
utils.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""Utils files with all functions relevant to generation of KG."""
import logging
import pickle
from collections import defaultdict
import pybel
import pandas as pd
import pubchempy as pcp
import requests
from chembl_webresource_client.http_errors import HttpBadRequest, HttpApplicationError
from chembl_webresource_client.new_client import new_client
from pybel import BELGraph
from pybel.dsl import Protein, Abundance, Pathology, BiologicalProcess
from tqdm import tqdm
logger = logging.getLogger("__name__")
def RetMech(chemblIds) -> dict:
"""Function to retrieve mechanism of actions and target proteins from ChEMBL
:param chemblIds:
:return:
"""
getMech = new_client.mechanism
mechList = []
for chemblid in tqdm(chemblIds, desc='Retrieving mechanisms from ChEMBL'):
mechs = getMech.filter(
molecule_chembl_id=chemblid
).only(['mechanism_of_action', 'target_chembl_id'])
mechList.append(list(mechs))
named_mechList = dict(zip(chemblIds, mechList))
named_mechList = {
k: v
for k, v in named_mechList.items()
if v
}
return named_mechList
def RetDrugInd(chemblIDs) -> dict:
"""Function to retrieve associated diseases from ChEMBL
:param chemblIDs:
:return:
"""
getDrugInd = new_client.drug_indication
drugIndList = []
for chemblid in tqdm(chemblIDs, desc='Retrieving diseases from ChEMBL'):
drugInd = getDrugInd.filter(
molecule_chembl_id=chemblid
).only('mesh_heading')
drugIndList.append(list(drugInd))
named_drugIndList = dict(zip(chemblIDs, drugIndList))
named_drugIndList = {
k: v
for k, v in named_drugIndList.items()
if v
}
return named_drugIndList
def RetAct(chemblIds) -> dict:
"""Function to retrieve associated assays from ChEMBL
:param chemblIds:
:return:
"""
GetAct = new_client.activity
ActList = []
filtered_list=['assay_chembl_id','assay_type','pchembl_value','target_chembl_id',
'target_organism','bao_label','target_type']
# filtered_list = [
# 'pchembl_value',
# 'target_chembl_id',
# 'target_type',
# 'bao_label'
# ]
for chembl in tqdm(chemblIds, desc='Retrieving bioassays from ChEMBL'):
#for i in range(len(chemblIds)):
acts = GetAct.filter(
molecule_chembl_id=chembl,
pchembl_value__isnull=False,
assay_type_iregex='(B|F)',
target_organism='Homo sapiens'
).only(filtered_list)
#print(chemblIds[i])
data = []
for d in acts:
if float(d.get('pchembl_value')) < 6:
continue
# try:
# if d['target_type'] in ('CELL-LINE', 'UNCHECKED'):
# continue
# except KeyError:
# continue
if (d.get('bao_label') != 'single protein format'):
continue
#uprot_id = d['target_components'][0]['accession']
#print(uprot_id)
#print(d)
data.append(d)
acts = acts[:10]
ActList.append(list(data))
named_ActList = dict(zip(chemblIds, ActList))
named_ActList = {
k: v
for k, v in named_ActList.items()
if v
}
return named_ActList
# def RetAct(chemblIds) -> dict:
# """Function to retrieve associated assays from ChEMBL
# :param chemblIds:
# :return:
# """
# GetAct = new_client.activity
# ActList = []
# filtered_list = [
# 'pchembl_value',
# 'target_chembl_id',
# 'target_type'
# ]
# for chembl in tqdm(chemblIds, desc='Retrieving bioassays from ChEMBL'):
# acts = GetAct.filter(
# molecule_chembl_id=chembl,
# pchembl_value__isnull=False,
# assay_type_iregex='(B|F)',
# target_organism='Homo sapiens'
# ).only(filtered_list)
# data = []
# for d in acts:
# if float(d.get('pchembl_value')) < 6:
# continue
# try:
# if d['target_type'] in ('CELL-LINE', 'UNCHECKED'):
# continue
# except IndexError:
# continue
# uprot_id = d['target_components'][0]['accession']
# data.append(uprot_id)
# # acts = acts[:5]
# ActList.append(list(data))
# named_ActList = dict(zip(chemblIds, ActList))
# named_ActList = {
# k: v
# for k, v in named_ActList.items()
# if v
# }
# return named_ActList
def Ret_chembl_protein(sourceList) -> list:
"""Method to retrieve ChEMBL ids which are proteins/targets
:param sourceList:
:return:
"""
protein_List = []
for item in sourceList:
for j in range(len(sourceList[item])):
protein_List.append(sourceList[item][j]['target_chembl_id'])
protein_List = set(protein_List)
protein_List = list(filter(None, protein_List))
return protein_List
def chembl2uniprot(chemblIDs) -> dict:
"""Method to convert ChEMBL id to UNIPROT and get associated REACTOME pathways
:param chemblIDs:
:return:
"""
getTarget = new_client.target
chem2Gene2path = []
chemHasNoPath = set()
chemNotprotein = set()
chem2path = defaultdict(list)
# Loop to ensure it is a protein
for chemblid in tqdm(chemblIDs, desc='Filtering UniProt proteins from ChEMBL'):
chem = getTarget.filter(
chembl_id=chemblid
).only('target_components')
try:
uprot_id = chem[0]['target_components'][0]['accession']
if not uprot_id:
chemHasNoPath.add(chemblid)
except IndexError:
chemHasNoPath.add(chemblid)
logger.info(f'No UniProt information available for {len(chemHasNoPath)} proteins.')
chemblIDs_filtered = [
item
for item in chemblIDs
if item not in chemHasNoPath
]
# Get gene symbol from ChEMBL and filtering the list for human proteins only
for chemblid in tqdm(chemblIDs_filtered, desc='Filtering human proteins from ChEMBL'):
chem = getTarget.filter(chembl_id=chemblid).only('target_components')
getGene = chem[0]['target_components'][0]['target_component_synonyms']
try:
getGene = [item for item in getGene if item["syn_type"] == "GENE_SYMBOL"][0]
if not getGene:
chemNotprotein.add(chemblid)
except IndexError:
chemNotprotein.add(chemblid)
chemblIDs_filtered = [
item
for item in chemblIDs_filtered
if item not in chemNotprotein
]
# Extracting data for valid proteins only
for chemblid in tqdm(chemblIDs_filtered, desc='Populating ChEMBL data for human proteins'):
chem = getTarget.filter(
chembl_id=chemblid
).only('target_components')
# UniProt data
uprot_id = chem[0]['target_components'][0]['accession']
# Gene symbol
getGene = chem[0]['target_components'][0]['target_component_synonyms']
getGene = [item for item in getGene if item["syn_type"] == "GENE_SYMBOL"][0]
# Pathway data
chem2path = [item for item in chem[0]['target_components'][0]['target_component_xrefs'] if
item["xref_src_db"] == "Reactome"]
uprot = {'accession': uprot_id}
chem2path.append(uprot)
chem2path.append(getGene)
chem2Gene2path.append(chem2path)
named_chem2Gene2path = dict(zip(chemblIDs_filtered, chem2Gene2path))
named_chem2Gene2path = {
k: v
for k, v in named_chem2Gene2path.items()
if v
}
return named_chem2Gene2path
def chembl2gene2path(
chem2geneList,
ActList
):
"""Method for updating chembl protein nodes with gene symbol.
:param chem2geneList:
:param ActList:
:return:
"""
for item in chem2geneList:
sizeOfitem = len(chem2geneList[item])
gene = chem2geneList[item][sizeOfitem - 1]['component_synonym']
for jtem in ActList:
for i in range(len(ActList[jtem])):
if item == ActList.get(jtem)[i]['target_chembl_id']:
newkey = {'Protein': gene}
ActList[jtem][i].update(newkey)
return ActList
def Ret_uprotid(chembl2uprot) -> list:
"""Method to get UniProt ids from dict of chembl2uniprot
:param chembl2uprot:
:return:
"""
chemblProt = []
for item in chembl2uprot:
for j in range(len(chembl2uprot[item])):
if 'accession' in chembl2uprot[item][j]:
chemblProt.append(
chembl2uprot[item][j]['accession']
)
return chemblProt
def ExtractFromUniProt(uniprot_id) -> dict:
"""Uniprot parser to retrieve information about OMIM disease, reactome pathway, biological process,
and molecular functions.
:param uniprot_id:
:return:
"""
Uniprot_Dict = []
mapped_uprot = []
for id in tqdm(uniprot_id,desc='reading uprot ids'):
# Retrieve data for id in text format if found in uniprot
ret_uprot = requests.get(
'https://www.uniprot.org/uniprot/' + id + '.txt'
).text.split('\n')
if ret_uprot == ['']:
continue
id_copy = id
mapped_uprot.append(id_copy)
i = 0
j = 0
k = 0
id = {}
id['Disease'] = {}
id['Reactome'] = {}
id['Function'] = {}
id['BioProcess'] = {}
id['Gene'] = {}
# parse each line looking for info about disease, pathway, funcn, bp and so on
for line in ret_uprot:
# parse lines with disease and extract disease names and omim ids
if '-!- DISEASE:' in line:
if ('[MIM:' in line):
dis = line.split(':')
id['Disease'].update({dis[1][1:-5]: dis[2][:-1]})
# extract reactome ids and names
if 'Reactome;' in line:
ract = line.split(';')
id['Reactome'].update({ract[2][1:-2]: ract[1][1:]})
# look for functions
if ' F:' in line:
if j < 5:
fn = line.split(';')
id['Function'].update({fn[2][3:]: fn[1][1:]})
j += 1
# look for biological processes
if ' P:' in line:
if i < 5:
bp = line.split(';')
# bp returns list with GO ids and names
id['BioProcess'].update({bp[2][3:]: bp[1][1:]})
i += 1
if 'GN Name' in line:
if k == 0:
gene = line.split('=')
gene = gene[1].split(' ')
if ';' in gene[0]:
gene = gene[0].split(';')
gene = {'Gene': gene[0]}
else:
gene = {'Gene': gene[0]}
id.update(gene)
k += 1
Uniprot_Dict.append(id)
Uniprot_Dict = dict(zip(mapped_uprot, Uniprot_Dict))
return Uniprot_Dict
def chem2moa_rel(
named_mechList,
org,
graph: BELGraph
) -> BELGraph:
"""Method to create the monkeypox graph
:param named_mechList:
:param org:
:param graph: BEL graph of Monkeypox
:return:
"""
for chembl_name, chembl_entries in tqdm(named_mechList.items(), desc='Populating Chemical-MoA edges'):
for info in chembl_entries:
graph.add_association(
Abundance(namespace='ChEMBL', name=chembl_name),
BiologicalProcess(namespace='MOA', name=info['mechanism_of_action']),
citation='ChEMBL database',
evidence='ChEMBL query'
)
if not info['target_chembl_id']:
continue
if 'Protein' in info:
graph.add_association(
Abundance(namespace='ChEMBL', name=chembl_name),
Protein(namespace=org, name=info['Protein']),
citation='ChEMBL database',
evidence='ChEMBL query'
)
else:
graph.add_association(
Abundance(namespace='ChEMBL', name=chembl_name),
Protein(namespace=org, name=info['target_chembl_id']),
citation='ChEMBL database',
evidence='ChEMBL query'
)
return graph
def chem2dis_rel(
named_drugIndList,
graph: BELGraph
) -> BELGraph:
"""Method to add drug indication edges to the KG.
:param named_drugIndList:
:param graph:
:return:
"""
for chembl_id, drug_entries in tqdm(named_drugIndList.items(), desc='Populating Drug-Indication edges'):
for drug_data in drug_entries:
graph.add_association(
Abundance(namespace='ChEMBL', name=chembl_id),
Pathology(namespace='Disease', name=drug_data['mesh_heading']),
citation='ChEMBL database',
evidence='ChEMBL query'
)
return graph
def chem2act_rel(
named_ActList,
org,
graph: BELGraph
) -> BELGraph:
"""Method to add bioassay edges to the KG.
:param named_ActList:
:param org:
:param graph:
:return:
"""
for chemical, chem_entries in tqdm(named_ActList.items(), desc='Adding bioassay edges to BEL'):
for chem_data in chem_entries:
if chem_data['target_chembl_id']:
if 'Protein' in chem_data:
graph.add_association(
Abundance(namespace='ChEMBLAssay', name=chem_data['assay_chembl_id']),
Protein(namespace=org, name=chem_data['Protein']),
citation='ChEMBL database',
evidence='ChEMBL query'
)
else:
graph.add_association(
Abundance(namespace='ChEMBLAssay', name=chem_data['assay_chembl_id']),
Protein(namespace=org, name=chem_data['target_chembl_id']),
citation='ChEMBL database',
evidence='ChEMBL query'
)
graph.add_association(
Abundance(namespace='ChEMBL', name=chemical),
Abundance(namespace='ChEMBLAssay', name=chem_data['assay_chembl_id']),
citation='ChEMBL database',
evidence='ChEMBL query',
annotation={
'assayType': chem_data['assay_type'],
'pChEMBL': chem_data['pchembl_value']
}
)
return graph
def gene2path_rel(
named_chem2geneList,
org,
graph
) -> BELGraph:
"""Method to add protein and reactome data to KG
:param named_chem2geneList:
:param org:
:param graph:
:return:
"""
for item in named_chem2geneList:
itemLen = len(named_chem2geneList[item]) - 1
for j in range(itemLen - 1):
graph.add_association(
Protein(namespace=org, name=named_chem2geneList[item][itemLen]['component_synonym']),
BiologicalProcess(namespace='Reactome', name=named_chem2geneList[item][j]['xref_name']),
citation='ChEMBL database',
evidence='ChEMBL query',
annotation={
'Reactome': named_chem2geneList[item][j]['xref_id']
}
)
return graph
def uniprot_rel(
named_uprotList,
org,
graph
) -> BELGraph:
"""Method to add UniProt related edges
:param named_uprotList:
:param org:
:param graph:
:return:
"""
for item in named_uprotList:
fun = list(named_uprotList[item]['Function'].keys())
bp = list(named_uprotList[item]['BioProcess'].keys())
for f in fun:
if str(named_uprotList[item]['Gene']) != 'nan' and not isinstance(named_uprotList[item]['Gene'], dict):
graph.add_association(
Protein(namespace=org, name=named_uprotList[item]['Gene']),
BiologicalProcess(namespace='GOMF', name=f),
citation='UniProt database',
evidence='UniProt query'
)
else:
graph.add_association(
Protein(namespace=org, name=item),
BiologicalProcess(namespace='GOMF', name=f),
citation='UniProt database',
evidence='UniProt query'
)
for b in bp:
if str(named_uprotList[item]['Gene']) != 'nan' and not isinstance(named_uprotList[item]['Gene'], dict):
graph.add_association(
Protein(namespace=org, name=named_uprotList[item]['Gene']),
BiologicalProcess(namespace='GOBP', name=b),
citation='UniProt database',
evidence='UniProt query'
)
else:
graph.add_association(
Protein(namespace=org, name=item),
BiologicalProcess(namespace='GOBP', name=b),
citation='UniProt database',
evidence='UniProt query'
)
return graph
def _get_target_data(
protein_list: list,
organism: str
) -> pd.DataFrame:
"""Get chemical for target data from ChEMBL.
:param protein_list:
:param organism:
:return:
"""
df_data = []
target = new_client.target
activity = new_client.activity
for protein in tqdm(protein_list, desc='Retrieving chemicals for proteins'):
if pd.isna(protein):
continue
try:
prot_data = [target.search(protein)[0]]
# Search for protein with same synonym
if prot_data == [None]:
prot_data = target.filter(
target_synonym__icontains=protein, target_organism__istartswith=organism
).only(['target_chembl_id', 'target_pref_name', 'molecule_chembl_id', 'molecule_pref_name'])
except HttpBadRequest:
print(f'Cannot search for {protein} due to chembl error')
continue
# No results found
if not prot_data:
continue
for prot in prot_data:
# Absence of chembl id
if not prot['target_chembl_id']:
continue
prot_activity_data = activity.filter(
target_chembl_id=prot['target_chembl_id'],
assay_type_iregex='(B|F)',
).only([
'pchembl_value', 'molecule_chembl_id', 'activity_id', 'target_pref_name', 'molecule_pref_name'
])
if len(prot_activity_data) < 1:
continue
for i in prot_activity_data:
tmp = {}
try:
if i['pchembl_value'] is None:
continue
except HttpApplicationError:
continue
pchembl_val = i['pchembl_value']
if float(pchembl_val) < 6:
tmp['activity'] = 'inhibitor'
else:
tmp['activity'] = 'activator'
tmp.update({
'protein_symbol': protein,
'protein_name': i['target_pref_name'],
'aid': str(i['activity_id']),
'chembl_id': i['molecule_chembl_id'],
'compound_name': i['molecule_pref_name'].capitalize() if i['molecule_pref_name'] else ''
})
df_data.append(tmp)
time.sleep(1)
print('1 seconds of break')
# Merge duplicated protein-chemical entries into one
df = pd.DataFrame()
for idx, row in tqdm(enumerate(df_data), total=len(df_data), desc='Preparing data'):
if idx == 0:
df = df.append(row, ignore_index=True)
else:
_in_df = df.loc[
(df['protein_symbol'] == row['protein_symbol']) & (df['chembl_id'] == row['chembl_id'])
]
if _in_df.empty:
df = df.append(row, ignore_index=True)
else:
row_index = _in_df.index
# Check existing citations
existing_assays = set(df.loc[row_index, 'aid'].values[0].split(' | '))
old_count = len(existing_assays)
existing_assays.add(row['aid'])
new_count = len(existing_assays)
# Check if new citation added, if yes - add respective data
if old_count < new_count:
df.loc[row_index, 'aid'] = ' | '.join(existing_assays)
df = df[['activity', 'protein_symbol', 'protein_name', 'aid', 'chembl_id', 'compound_name']]
return df
def target_list_to_chemical(
proteins: list,
organism: str = 'Homo sapiens',
) -> pd.DataFrame:
"""Extract chemical information on list of targets
Usage:
>> target_list_to_chemical(proteins=['RIPK'])
"""
df = _get_target_data(protein_list=proteins, organism=organism)
return df
def getNodeList(nodeName,itmpGraph):
node_list = []
for node in itmpGraph.nodes():
if isinstance(node,pybel.dsl.Abundance):
if node.namespace == nodeName:
node_list.append(node.name)
return(node_list)
def chembl2rxn_rel(
chemblid_list,
graph: BELGraph
) -> BELGraph:
"""
:param chemblid_list:
:param graph:
:return:
"""
infile = open('data/normalized_data/drugReactions.pkl', 'rb')
rxn_df = pickle.load(infile)
infile.close()
chembl_id_rxn = rxn_df[rxn_df['chembl_id'].isin(chemblid_list)]
chembl_id_rxn = chembl_id_rxn.reset_index(drop=True)
for i in range(len(chembl_id_rxn)):
graph.add_association(
Abundance(namespace='ChEMBL', name=chembl_id_rxn['chembl_id'][i]),
Pathology(namespace='SideEffect', name=chembl_id_rxn['event'][i]), # TODO: Fix namespace
citation="OpenTargets Platform",
evidence='DrugReactions'
)
return graph
def cid2chembl(cidList) -> list:
"""Method to convert Pubchem CIDs to ChEMBL ids
:param cidList:
:return:
"""
cid2chembl_list = []
for id in tqdm(cidList, desc='Converting PubChem ids to ChEMBL ids'):
c = pcp.Compound.from_cid(id)
for synonym in c.synonyms:
if synonym.startswith('CHEMBL'):
cid2chembl_list.append(synonym)
return cid2chembl_list
# def chembl2rxn_rel(itmpGraph):
# infile = open('data/normalized_data/drugReactions.pkl','rb')
# rxn_df = pickle.load(infile)
# infile.close()
# chembl_id = []
# for node in itmpGraph.nodes():
# if isinstance(node,pybel.dsl.Abundance):
# if node.namespace == 'ChEMBL':
# chembl_id.append(node.name)
# chembl_id_rxn = rxn_df[rxn_df['chembl_id'].isin(chembl_id)]
# chembl_id_rxn = chembl_id_rxn.reset_index(drop=True)
# for i in range(len(chembl_id_rxn)):
# itmpGraph.add_association(Abundance(namespace='ChEMBL',name = chembl_id_rxn['chembl_id'][i]),
# Pathology(namespace='SideEffect',name = chembl_id_rxn['event'][i]),
# citation = "OpenTargets Platform",evidence = 'DrugReactions')
# return(itmpGraph)
#import time
# def _get_target_data(
# protein_list: list,
# organism: str
# ) -> pd.DataFrame:
# """Get chemical for target data from ChEMBL.
# :param protein_list:
# :param organism:
# :return:
# """
# df_data = []
# target = new_client.target
# activity = new_client.activity
# for protein in tqdm(protein_list, desc='Retrieving chemicals for proteins'):
# if pd.isna(protein):
# continue
# try:
# prot_data = [target.search(protein)[0]]
# # Search for protein with same synonym
# if prot_data == [None]:
# prot_data = target.filter(
# target_synonym__icontains=protein, target_organism__istartswith=organism
# ).only(['target_chembl_id', 'target_pref_name', 'molecule_chembl_id', 'molecule_pref_name'])
# except HttpBadRequest:
# print(f'Cannot search for {protein} due to chembl error')
# continue
# # No results found
# if not prot_data:
# continue
# for prot in prot_data:
# # Absence of chembl id
# if not prot['target_chembl_id']:
# continue
# prot_activity_data = activity.filter(
# target_chembl_id=prot['target_chembl_id'],
# assay_type_iregex='(B|F)',
# ).only([
# 'pchembl_value', 'molecule_chembl_id', 'activity_id', 'target_pref_name', 'molecule_pref_name'
# ])
# if len(prot_activity_data) < 1:
# continue
# for i in prot_activity_data:
# tmp = {}
# try:
# if i['pchembl_value'] is None:
# continue
# except HttpApplicationError:
# continue
# pchembl_val = i['pchembl_value']
# if float(pchembl_val) < 6:
# tmp['activity'] = 'inhibitor'
# else:
# tmp['activity'] = 'activator'
# tmp.update({
# 'protein_symbol': protein,
# 'protein_name': i['target_pref_name'],
# 'aid': str(i['activity_id']),
# 'chembl_id': i['molecule_chembl_id'],
# 'compound_name': i['molecule_pref_name'].capitalize() if i['molecule_pref_name'] else ''
# })
# df_data.append(tmp)
# time.sleep(5)
# print('5 seconds of break')
# # Merge duplicated protein-chemical entries into one
# df = pd.DataFrame()
# if len(df_data)==0:
# return df
# for idx, row in tqdm(enumerate(df_data), total=len(df_data), desc='Preparing data'):
# if idx == 0:
# df = df.append(row, ignore_index=True)
# else:
# _in_df = df.loc[
# (df['protein_symbol'] == row['protein_symbol']) & (df['chembl_id'] == row['chembl_id'])
# ]
# if _in_df.empty:
# df = df.append(row, ignore_index=True)
# else:
# row_index = _in_df.index
# # Check existing citations
# existing_assays = set(df.loc[row_index, 'aid'].values[0].split(' | '))
# old_count = len(existing_assays)
# existing_assays.add(row['aid'])
# new_count = len(existing_assays)
# # Check if new citation added, if yes - add respective data
# if old_count < new_count:
# df.loc[row_index, 'aid'] = ' | '.join(existing_assays)
# print(df)
# print(df_data)
# df = df[['activity', 'protein_symbol', 'protein_name', 'aid', 'chembl_id', 'compound_name']]
# return df
# def target_list_to_chemical(
# proteins: list,
# organism: str = 'Homo sapiens',
# ) -> pd.DataFrame:
# """Extract chemical information on list of targets
# Usage:
# >> target_list_to_chemical(proteins=['RIPK'])
# """
# df = _get_target_data(protein_list=proteins, organism=organism)
# return df
#def _get_target_data(protein_list: list, organism: str):
# """Get chemical for target data from ChEMBL"""
# df_data = []
# target = new_client.target
# activity = new_client.activity
# for protein in protein_list:
# if pd.isna(protein):
# continue
# try:
# prot_data = [target.search(protein)[0]]
# # Search for protein with same synonym
# if prot_data == [None]:
# prot_data = target.filter(
# target_synonym__icontains=protein, target_organism__istartswith=organism
# ).only(['target_chembl_id', 'target_pref_name', 'molecule_chembl_id', 'molecule_pref_name'])
# except HttpBadRequest:
# print(f'Cannot search for {protein} due to chembl error')
# continue
# # No results found
# if not prot_data:
# continue
# for prot in tqdm(prot_data, f'Analying data for {protein}'):
# # Absence of chembl id
# if not prot['target_chembl_id']:
# continue
# prot_activity_data = activity.filter(
# target_chembl_id=prot['target_chembl_id'],
# assay_type_iregex='(B|F)',
# ).only([
# 'pchembl_value', 'molecule_chembl_id', 'activity_id', 'target_pref_name', 'molecule_pref_name'
# ])
# if len(prot_activity_data) < 1:
# continue
# for i in prot_activity_data:
# tmp = {}
# if i['pchembl_value'] is None:
# continue
# pchembl_val = i['pchembl_value']
# if float(pchembl_val) < 6:
# tmp['activity'] = 'inhibitor'
# else:
# tmp['activity'] = 'activator'
# tmp['protein_symbol'] = protein
# tmp['protein_name'] = i['target_pref_name']
# tmp['aid'] = str(i['activity_id'])
# tmp['chembl_id'] = i['molecule_chembl_id']
# tmp['compound_name'] = i['molecule_pref_name'].capitalize() if i['molecule_pref_name'] else ''
# df_data.append(tmp)
# time.sleep(10)
# # Merge duplicated protein-chemical entries into one
# df = pd.DataFrame()
# for idx, row in tqdm(enumerate(df_data), total=len(df_data), desc='Preparing data'):
# if idx == 0:
# df = df.append(row, ignore_index=True)
# else:
# _in_df = df.loc[
# (df['protein_symbol'] == row['protein_symbol']) & (df['chembl_id'] == row['chembl_id'])
# ]
# if _in_df.empty:
# df = df.append(row, ignore_index=True)
# else:
# row_index = _in_df.index
# # Check existing citations
# existing_assays = set(df.loc[row_index, 'aid'].values[0].split(' | '))
# old_count = len(existing_assays)
# existing_assays.add(row['aid'])
# new_count = len(existing_assays)
# # Check if new citation added, if yes - add respective data
# if old_count < new_count:
# df.loc[row_index, 'aid'] = ' | '.join(existing_assays)
# df = df[['activity', 'protein_symbol', 'protein_name', 'aid', 'chembl_id', 'compound_name']]
# return df
# def target_list_to_chemical(
# proteins: list,
# organism: str = 'Homo sapiens',
# output_dir: str = ''
# ) -> None:
# """Extract chemical information on list of targets
# Usage: