forked from dsi-bdi/biokg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile_biokg.py
1322 lines (1076 loc) · 42 KB
/
compile_biokg.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
from biodblinker import SiderLinker, MESHLinker
from os import makedirs, listdir, remove, walk
from os.path import join, isdir
from shutil import copy
import gzip
sider_linker = SiderLinker()
mesh_linker = MESHLinker()
data_root = "data/preprocessed"
output_root = "data/output"
core_root = "data/biokg"
links_root = join(output_root, "links")
meta_root = join(output_root, "metadata")
properties_root = join(output_root, "properties")
drug_properties_root = join(properties_root, "drug")
protein_properties_root = join(properties_root, "protein")
pathway_properties_root = join(properties_root, "pathway")
disease_properties_root = join(properties_root, "disease")
cell_properties_root = join(properties_root, "cell")
mim_properties_root = join(properties_root, "genetic_disorders")
other_root = join(output_root, "other")
makedirs(output_root) if not isdir(output_root) else None
makedirs(links_root) if not isdir(links_root) else None
makedirs(meta_root) if not isdir(meta_root) else None
makedirs(properties_root) if not isdir(properties_root) else None
makedirs(drug_properties_root) if not isdir(drug_properties_root) else None
makedirs(protein_properties_root) if not isdir(protein_properties_root) else None
makedirs(pathway_properties_root) if not isdir(pathway_properties_root) else None
makedirs(disease_properties_root) if not isdir(disease_properties_root) else None
makedirs(cell_properties_root) if not isdir(cell_properties_root) else None
makedirs(mim_properties_root) if not isdir(mim_properties_root) else None
makedirs(other_root) if not isdir(other_root) else None
makedirs(core_root) if not isdir(core_root) else None
def get_all_proteins():
"""
Get the set of uniprot proteins to include in the kg
Parameters
----------
Returns
-------
set
the set of proteins to include in the kg
"""
uniprot_meta = join(data_root, "uniprot", "uniprot_metadata.txt")
protein_set = set()
with open(uniprot_meta, "r") as fd:
for line in fd:
protein = line.split("\t")[0]
protein_set.add(protein)
return protein_set
def get_proteins_by_metadata(filter_field, accpeted_values):
"""
Get the set of uniprot proteins for which the metadata value of field
is ont of the accpeted_values
Parameters
----------
filter_field: str
The metadata field to filter by
accepted_values: list
The set of accpetable values for the metadata field
Returns
-------
set
the set of proteins to include in the kg
"""
uniprot_meta = join(data_root, "uniprot", "uniprot_metadata.txt")
protein_set = set()
with open(uniprot_meta, "r") as fd:
for line in fd:
protein, field, value = line.strip().split("\t")
if field == filter_field and value in accpeted_values:
protein_set.add(protein)
return protein_set
def get_all_drugs():
"""
Get the set of drugs to include in the kg
Parameters
----------
Returns
-------
set
the set of drugs to include in the kg
"""
db_meta = join(data_root, "drugbank", "db_meta.txt")
db_drugs = set()
with open(db_meta, "r") as fd:
for line in fd:
drug = line.split("\t")[0]
db_drugs.add(drug)
return db_drugs
def get_all_mesh_diseases():
"""
Get the set of mesh diseases to include in the kg
Parameters
----------
Returns
-------
set
the set of mesh diseases to include in the kg
"""
files = [
join(data_root, "mesh", "mesh_disease_meta.txt"),
join(data_root, "mesh", "mesh_scr_disease_meta.txt"),
]
mesh_diseases = set()
for fp in files:
with open(fp, "r") as fd:
for line in fd:
disease, meta, value = line.strip().split("\t")
if meta == "TYPE":
mesh_diseases.add(disease)
return mesh_diseases
def get_all_genetic_disorders():
"""
Get the set of uniprot proteins for which the metadata value of field
is ont of the accpeted_values
Parameters
----------
filter_field: str
The metadata field to filter by
accepted_values: list
The set of accpetable values for the metadata field
Returns
-------
set
the set of proteins to include in the kg
"""
uniprot_meta = join(data_root, "uniprot", "uniprot_facts.txt")
genetic_disorders = set()
with open(uniprot_meta, "r") as fd:
for line in fd:
protein, field, value = line.strip().split("\t")
if field == "RELATED_GENETIC_DISORDER":
genetic_disorders.add(value)
return genetic_disorders
def get_all_unique_ppi(protein_set):
"""
Get the set of protein proteins interactions to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the ppis
Returns
-------
list
the list of unique protein protein interactions
"""
files = [
join(data_root, "uniprot", "uniprot_ppi.txt"),
join(data_root, "reactome", "reactome_ppi.txt"),
join(data_root, "intact", "intact_ppi.txt"),
]
ppis = set()
for fp in files:
with open(fp, "r") as fd:
for line in fd:
s, _, o = line.strip().split("\t")[:3]
if s > o:
ppis.add((o, s))
else:
ppis.add((s, o))
unique_triples = []
for s, o in ppis:
if s in protein_set and o in protein_set:
unique_triples.append((s, "PPI", o))
return unique_triples
def get_species_map():
species_map = {}
with open(join(data_root, "uniprot", "uniprot_metadata.txt"), "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "SPECIES":
species_map[s] = o
return species_map
def write_ppi_by_species():
"""
Get the set of protein proteins interactions to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the ppis
Returns
-------
list
the list of unique protein protein interactions
"""
species_map = get_species_map()
unique_species = list(set(species_map.values()))
species_root = join(links_root, "PPI_SPECIES")
makedirs(species_root) if not isdir(species_root) else None
species_file_names = {}
for species in set(species_map.values()):
species_file_names[species] = join(species_root, f"{species}_ppi.txt")
# species_files['OTHER'] = open(join(species_root, 'INTERSPECIES_ppi.txt'), 'w')
files = [
join(data_root, "uniprot", "uniprot_ppi.txt"),
join(data_root, "reactome", "reactome_ppi.txt"),
join(data_root, "intact", "intact_ppi.txt"),
]
ppis = set()
for fp in files:
with open(fp, "r") as fd:
for line in fd:
s, _, o = line.strip().split("\t")[:3]
if s > o:
ppis.add((o, s))
else:
ppis.add((s, o))
unique_triples = []
species_files = {}
for s, o in ppis:
if s not in species_map or o not in species_map:
continue
s_species = species_map[s]
o_species = species_map[o]
if s_species == o_species:
if s_species not in species_files:
species_files[s_species] = open(species_file_names[s_species], "w")
species_files[s_species].write(f"{s}\tPPI\t{o}\n")
else:
if "OTHER" not in species_files:
species_files["OTHER"] = open(
join(species_root, "INTERSPECIES_ppi.txt"), "w"
)
species_files["OTHER"].write(f"{s}\tPPI\t{o}\n")
for f in species_files.values():
f.close()
def get_all_protein_sequence_annotations(protein_set):
"""
Get the set of protein sequence annotations to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the ppis
Returns
-------
list
the list of interpro protein sequence annotations
list
the list of prosite protein sequence annotations
"""
seq_ann_root = join(protein_properties_root, "sequence_annotations")
makedirs(seq_ann_root) if not isdir(seq_ann_root) else None
annotation_output_files = {
"ACTIVE_SITE": open(join(seq_ann_root, "protein_active_site.txt"), "w"),
"BINDING_SITE": open(join(seq_ann_root, "protein_binding_site.txt"), "w"),
"CONSERVED_SITE": open(join(seq_ann_root, "protein_conserved_site.txt"), "w"),
"DOMAIN": open(join(seq_ann_root, "protein_domain.txt"), "w"),
"FAMILY": open(join(seq_ann_root, "protein_family.txt"), "w"),
"HOMOLOGOUS_SUPERFAMILY": open(
join(seq_ann_root, "protein_homologous_superfamily.txt"), "w"
),
"PTM": open(join(seq_ann_root, "protein_ptm.txt"), "w"),
"REPEAT": open(join(seq_ann_root, "protein_repeat.txt"), "w"),
"GO_BP": open(
join(protein_properties_root, "protein_go_biological_process.txt"), "w"
),
"GO_CC": open(
join(protein_properties_root, "protein_go_cellular_component.txt"), "w"
),
"GO_MF": open(
join(protein_properties_root, "protein_go_molecular_function.txt"), "w"
),
"RELATED_GENETIC_DISORDER": open(
join(links_root, "protein_genetic_disorders.txt"), "w"
),
}
with open(join(data_root, "uniprot", "uniprot_facts.txt"), "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p in annotation_output_files and s in protein_set:
annotation_output_files[p].write(line)
for fd in annotation_output_files.values():
fd.close()
def get_all_protein_drug_interactions(protein_set, drug_set):
"""
Get the set of protein drug interactions to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the protein drug interactions
drug_set: set
the set of drugs used to filter the protein drug interactions
Returns
-------
list
the list of unique protein drug interactions
"""
uniprot_facts = join(data_root, "uniprot", "uniprot_facts.txt")
db_targets = join(data_root, "drugbank", "db_targets.txt")
# ctd_drug_protein = join(data_root, 'ctd', 'ctd_drug_protein_interactions.txt')
pdis = set()
carriers = set()
enzymes = set()
transporters = set()
targets = set()
with open(uniprot_facts, "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "TARGET_OF_DRUG":
pdis.add((s, o))
with open(db_targets, "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")[:3]
pdis.add((o, s))
if p == "DRUG_CARRIER":
carriers.add((o, s))
elif p == "DRUG_ENZYME":
enzymes.add((o, s))
elif p == "DRUG_TRANSPORTER":
transporters.add((o, s))
else:
targets.add((o, s))
unique_triples = []
function_triples = []
for protein, drug in pdis:
if protein in protein_set and drug in drug_set:
unique_triples.append((drug, "DPI", protein))
for protein, drug in targets:
if protein in protein_set and drug in drug_set:
function_triples.append((drug, "DRUG_TARGET", protein))
for protein, drug in carriers:
if protein in protein_set and drug in drug_set:
function_triples.append((drug, "DRUG_CARRIER", protein))
for protein, drug in enzymes:
if protein in protein_set and drug in drug_set:
function_triples.append((drug, "DRUG_ENZYME", protein))
for protein, drug in transporters:
if protein in protein_set and drug in drug_set:
function_triples.append((drug, "DRUG_TRANSPORTER", protein))
return unique_triples, function_triples
def get_all_protein_disease_associations(protein_set, disease_set):
"""
Get the set of protein disease associations to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the protein disease associations
disease_set: set
the set of mesh diseases used to filter the protein disease associations
Returns
-------
list
the list of unique protein disease associations
"""
uniprot_facts = join(data_root, "uniprot", "uniprot_facts.txt")
ctd_protein_disease = join(data_root, "ctd", "ctd_protein_disease_association.txt")
pdis = set()
with open(ctd_protein_disease, "r") as fd:
for line in fd:
s, p, o, prov = line.strip().split("\t")[:4]
if prov == "CURATED":
pdis.add((s, o))
unique_triples = []
for protein, disease in pdis:
if protein in protein_set and disease in disease_set:
unique_triples.append((protein, "PROTEIN_DISEASE_ASSOCIATION", disease))
return unique_triples
def get_all_protein_pathway_associations(protein_set):
"""
Get the set of protein pathway associations to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the protein pathway associations
Returns
-------
list
the list of unique protein pathway associations
"""
uniprot_facts = join(data_root, "uniprot", "uniprot_facts.txt")
ctd_pathways = [
join(data_root, "ctd", "ctd_protein_reactome_pathway_association.txt"),
]
db_pathways = join(data_root, "drugbank", "db_pathways.txt")
ppis = set()
with open(uniprot_facts, "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "RELATED_PATHWAY":
ppis.add((s, o))
with open(db_pathways, "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "PATHWAY_ENZYME":
ppis.add((o, s))
for f in ctd_pathways:
with open(f, "r") as fd:
for line in fd:
s, p, o, prov = line.strip().split("\t")
if prov == "CURATED":
ppis.add((s, o))
unique_triples = []
for protein, pathway in ppis:
if protein in protein_set:
unique_triples.append((protein, "PROTEIN_PATHWAY_ASSOCIATION", pathway))
return unique_triples
def get_all_drug_drug_interactions(drug_set):
"""
Get the set of drug drug interactions to include in the kg
Parameters
----------
drug_set: set
the set of drugs used to filter the drug drug interactions
Returns
-------
list
the list of unique drug drug interactions
"""
db_interactions = join(data_root, "drugbank", "db_ddi.txt")
ddis = set()
with open(db_interactions, "r") as fd:
for line in fd:
s, _, o = line.strip().split("\t")[:3]
if s > o:
ddis.add((o, s))
else:
ddis.add((s, o))
unique_triples = []
for d1, d2 in ddis:
if d1 in drug_set and d2 in drug_set:
unique_triples.append((d1, "DDI", d2))
return unique_triples
def get_all_drug_pathway_associations(drug_set):
"""
Get the set of drug pathway associations to include in the kg
Parameters
----------
drug_set: set
the set of drugs used to filter the drug pathway associations
Returns
-------
list
the list of unique drug pathway associations
"""
ctd_pathways = [
join(data_root, "ctd", "ctd_drug_reactome_pathway_association.txt"),
]
db_pathways = join(data_root, "drugbank", "db_pathways.txt")
dpas = set()
with open(db_pathways, "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "DRUG_PATHWAY":
dpas.add((s, o))
for f in ctd_pathways:
with open(f, "r") as fd:
for line in fd:
s, p, o, prov = line.strip().split("\t")
if prov == "CURATED":
dpas.add((s, o))
unique_triples = []
for drug, pathway in dpas:
if drug in drug_set:
unique_triples.append((drug, "DRUG_PATHWAY_ASSOCIATION", pathway))
return unique_triples
def get_all_drug_disease_associations(drug_set, disease_set):
"""
Get the set of drug pathway associations to include in the kg
Parameters
----------
drug_set: set
the set of drugs used to filter the drug pathway associations
Returns
-------
list
the list of unique drug pathway associations
"""
ctd_links = join(data_root, "ctd", "ctd_drug_diseases.txt")
ddis = set()
with open(ctd_links, "r") as fd:
for line in fd:
s, _, o, prov = line.strip().split("\t")[:4]
if prov == "CURATED":
ddis.add((s, o))
unique_triples = []
for drug, disease in ddis:
if drug in drug_set and disease in disease_set:
unique_triples.append((drug, "DRUG_DISEASE_ASSOCIATION", disease))
return unique_triples
def get_all_drug_side_effects(drug_set):
"""
Get the set of drug side effect associations to include in the kg
Parameters
----------
drug_set: set
the set of drugs used to filter the drug side effect associations
Returns
-------
list
the list of unique drug side effect associations
"""
sider_effects = join(data_root, "sider", "sider_effects.txt")
sider_indications = join(data_root, "sider", "sider_indications.txt")
side_effects = set()
indications = set()
with open(sider_effects, "r") as fd:
for line in fd:
d, _, o = line.strip().split("\t")
db_ids = sider_linker.convert_drugs_to_drugbank([d])
if len(db_ids[0]) == 0:
continue
for db_id in db_ids[0]:
side_effects.add((db_id, o))
with open(sider_indications, "r") as fd:
for line in fd:
d, _, o = line.strip().split("\t")
db_ids = sider_linker.convert_drugs_to_drugbank([d])
if len(db_ids[0]) == 0:
continue
for db_id in db_ids[0]:
indications.add((db_id, o))
unique_triples = []
for drug, effect in side_effects:
if drug in drug_set:
unique_triples.append((drug, "DRUG_SIDEEFFECT_ASSOCIATION", effect))
unique_indications = []
for drug, indication in indications:
if drug in drug_set:
unique_indications.append((drug, "DRUG_INDICATION_ASSOCIATION", indication))
return unique_triples, unique_indications
def get_all_drug_atc_codes(drug_set):
"""
Get the set of drug atc codes to include in the kg
Parameters
----------
drug_set: set
the set of drugs used to filter the drug drug interactions
Returns
-------
list
the list of unique drug atc codes
"""
db_interactions = join(data_root, "drugbank", "db_atc.txt")
datc = set()
with open(db_interactions, "r") as fd:
for line in fd:
s, _, o = line.strip().split("\t")[:3]
datc.add((s, o))
unique_triples = []
for drug, atc in datc:
if drug in drug_set:
unique_triples.append((drug, "DRUG_ATC_CODE", atc))
return unique_triples
def get_all_disease_pathway_associations(disease_set):
"""
Get the set of disease pathway associations to include in the kg
Parameters
----------
Returns
-------
list
the list of unique disease pathway associations
"""
ddis = set()
unique_triples = []
for disease, pathway in ddis:
if disease in disease_set:
unique_triples.append((disease, "DISEASE_PATHWAY_ASSOCIATION", pathway))
return unique_triples
def get_disease_tree(disease_set):
"""
Get the set of disease pathway associations to include in the kg
Parameters
----------
Returns
-------
list
the list of unique disease pathway associations
"""
disease_tree = join(data_root, "mesh", "mesh_disease_tree.txt")
distree = set()
with open(disease_tree, "r") as fd:
for line in fd:
s, _, o = line.strip().split("\t")
distree.add((s, o))
unique_triples = []
for disease, category in distree:
if disease in disease_set:
unique_triples.append((disease, "DISEASE_SUPERGRP", category))
return unique_triples
def get_pathway_rels():
pathway_rels = set()
with open(join(data_root, "reactome", "reactome_pathway_rels.txt"), "r") as fd:
for line in fd:
s, _, o = line.strip().split("\t")
pathway_rels.add((s, o))
unique_triples = []
for pathway, parent in pathway_rels:
unique_triples.append((pathway, "HAS_PARENT_PATHWAY", parent))
return unique_triples
def get_complex_pathway_rels():
top_level_pathways = set()
complex_pathways = set()
with open(
join(data_root, "reactome", "reactome_complex_pathway_rels.txt"), "r"
) as fd:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "COMPLEX_PATHWAY":
complex_pathways.add((s, o))
else:
top_level_pathways.add((s, o))
unique_triples = []
for _complex, pathway in complex_pathways:
unique_triples.append((_complex, "COMPLEX_IN_PATHWAY", pathway))
tl_triples = []
for _complex, pathway in top_level_pathways:
tl_triples.append((_complex, "COMPLEX_TOP_LEVEL_PATHWAY", pathway))
return unique_triples, tl_triples
def get_protein_complex_rels(protein_set):
protein_complex = set()
with open(
join(data_root, "reactome", "reactome_protein_complex_rels.txt"), "r"
) as fd:
for line in fd:
s, _, o = line.strip().split("\t")[:3]
protein_complex.add((s, o))
unique_triples = []
for protein, _complex in protein_complex:
if protein in protein_set:
unique_triples.append((protein, "MEMBER_OF_COMPLEX", _complex))
return unique_triples
def get_all_protein_expressions(protein_set):
"""
Get the set of protein tissue expressions to include in the kg
Parameters
----------
protein_set: set
the set of proteins used to filter the protein tissue expressions
Returns
-------
list
the list of unique protein tissue expressions
"""
hpa_tissue_expression = join(data_root, "hpa", "hpa_tissues_exp.txt")
high_expression = set()
medium_expression = set()
low_expression = set()
expression = set()
tissue_structure = set()
with open(hpa_tissue_expression, "r") as fd:
for line in fd:
p, t, level = line.strip().split("\t")
parts = t.split("__")
if len(parts) == 2:
tissue = parts[0]
cell = parts[1]
tissue_structure.add((cell, tissue))
if level in ["low", "medium", "high"]:
expression.add((p, t))
if level == "low":
low_expression.add((p, t))
elif level == "medium":
medium_expression.add((p, t))
elif level == "high":
high_expression.add((p, t))
unique_triples = []
# for protein, tissue in expression:
# if protein in protein_set:
# unique_triples.append((protein, 'Protein_Expression', tissue))
hpa_other = join(other_root, "hpa")
makedirs(hpa_other) if not isdir(hpa_other) else None
level_output = open(join(hpa_other, "protein_expression_level.txt"), "w")
for protein, tissue in low_expression:
if protein in protein_set:
unique_triples.append((protein, "PROTEIN_EXPRESSED_IN", tissue))
level_output.write(f"{protein}\tPROTEIN_EXPRESSED_IN\t{tissue}\tLOW\n")
for protein, tissue in medium_expression:
if protein in protein_set:
unique_triples.append((protein, "PROTEIN_EXPRESSED_IN", tissue))
level_output.write(f"{protein}\tPROTEIN_EXPRESSED_IN\t{tissue}\tMEDIUM\n")
for protein, tissue in high_expression:
if protein in protein_set:
unique_triples.append((protein, "PROTEIN_EXPRESSED_IN", tissue))
level_output.write(f"{protein}\tPROTEIN_EXPRESSED_IN\t{tissue}\tHIGH\n")
structure_triples = []
for cell, tissue in tissue_structure:
structure_triples.append((cell, "PART_OF_TISSUE", tissue))
return unique_triples, structure_triples
def get_all_disease_genetic_disorder(disease_set, genetic_disorder_set):
dgd = set()
for disease in disease_set:
mapped_mim = mesh_linker.convert_disease_to_omim([disease])
for mim in mapped_mim[0]:
title = f"MIM:{mim}"
if title in genetic_disorder_set:
dgd.add((disease, title))
unique_triples = []
for disease, disorder in dgd:
unique_triples.append((disease, "DISEASE_GENETIC_DISORDER", disorder))
return unique_triples
def write_protein_cellline_expressions(protein_set):
pcl = set()
hpa_other = join(other_root, "hpa")
makedirs(hpa_other) if not isdir(hpa_other) else None
with open(join(data_root, "hpa", "hpa_cellines_exp.txt"), "r") as fd:
for line in fd:
pro, _, cl_tissue, exp = line.strip().split("\t")
cl = cl_tissue.split("#")[1]
if cl == "NA":
continue
exp = exp.split(":")[1]
pcl.add((pro, cl, exp))
with gzip.open(join(hpa_other, "protein_cellline_expression.txt.gz"), "wt") as fd:
for protein, cellline, expression in pcl:
if protein in protein_set:
fd.write(f"{protein}\t{cellline}\t{expression}\n")
def write_triples(triples, output_fp):
with open(output_fp, "w") as output:
for s, p, o in triples:
output.write(f"{s}\t{p}\t{o}\n")
def filter_ctd_drug_protein(protein_set):
ctd_drug_protein = join(data_root, "ctd", "ctd_drug_protein_interactions.txt")
ctd_drug_protein_filtered = join(
links_root, "ctd_drug_protein_interactions_SWISSPORT.txt"
)
with open(ctd_drug_protein_filtered, "w") as output_fd:
with open(ctd_drug_protein, "r") as fd:
for line in fd:
s, p, o, prov = line.strip().split("\t")[:4]
if o in protein_set:
output_fd.write(line)
def write_uniprot_metadata():
uniprot_meta_dp = join(meta_root, "protein")
makedirs(uniprot_meta_dp) if not isdir(uniprot_meta_dp) else None
meta_output_files = {
"NAME": open(join(uniprot_meta_dp, "uniprot_name.txt"), "w"),
"SHORT_NAME": open(join(uniprot_meta_dp, "uniprot_shortname.txt"), "w"),
"FULL_NAME": open(join(uniprot_meta_dp, "uniprot_fullname.txt"), "w"),
"ORGANISM_CLASS": open(
join(uniprot_meta_dp, "uniprot_organism_class.txt"), "w"
),
"OTHER_ID": open(join(uniprot_meta_dp, "uniprot_other_ids.txt"), "w"),
"RELATED_KEYWORD": open(
join(uniprot_meta_dp, "uniprot_related_keywords.txt"), "w"
),
"RELATED_PUBMED_ID": open(
join(uniprot_meta_dp, "uniprot_related_pubmed_ids.txt"), "w"
),
"SPECIES": open(join(uniprot_meta_dp, "uniprot_species.txt"), "w"),
}
with open(join(data_root, "uniprot", "uniprot_metadata.txt"), "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
# Fail if metadata type is not in the map
if p not in meta_output_files:
raise Exception(f"Predicate not recognized {p}")
meta_output_files[p].write(line)
for fd in meta_output_files.values():
fd.close()
def write_drugbank_metadata():
drugbank_meta_dp = join(meta_root, "drug")
makedirs(drugbank_meta_dp) if not isdir(drugbank_meta_dp) else None
meta_output_files = {
"NAME": open(join(drugbank_meta_dp, "drugbank_name.txt"), "w"),
"SYNONYM": open(join(drugbank_meta_dp, "drugbank_synonym.txt"), "w"),
"TYPE": open(join(drugbank_meta_dp, "drugbank_type.txt"), "w"),
"PRODUCT": open(join(drugbank_meta_dp, "drugbank_product.txt"), "w"),
"PUBMED_ARTICLE": open(
join(drugbank_meta_dp, "drugbank_related_pubmed_ids.txt"), "w"
),
"DIRECT_PARENT": open(
join(drugbank_meta_dp, "drugbank_direct_parent.txt"), "w"
),
"KINGDOM": open(join(drugbank_meta_dp, "drugbank_kingdom.txt"), "w"),
"SUPERCLASS": open(join(drugbank_meta_dp, "drugbank_superclass.txt"), "w"),
"CLASS": open(join(drugbank_meta_dp, "drugbank_class.txt"), "w"),
"SUBCLASS": open(join(drugbank_meta_dp, "drugbank_subclass.txt"), "w"),
"ALTERNATIVE_PARENT": open(
join(drugbank_meta_dp, "drugbank_alternative_parent.txt"), "w"
),
"SUBSTITUENT": open(join(drugbank_meta_dp, "drugbank_substituent.txt"), "w"),
"PRODUCT_STAGE": open(
join(drugbank_meta_dp, "drugbank_product_stage.txt"), "w"
),
}
files = [
join(data_root, "drugbank", "db_meta.txt"),
join(data_root, "drugbank", "db_product_stage.txt"),
join(data_root, "drugbank", "db_classification.txt"),
]
for f in files:
with open(f, "r") as fd:
for line in fd:
s, p, o = line.strip().split("\t")
# Fail if metadata type is not in the map
if p not in meta_output_files:
raise Exception(f"Predicate not recognized {p}")
meta_output_files[p].write(line)
for fd in meta_output_files.values():
fd.close()
with open(join(data_root, "drugbank", "db_pathways.txt"), "r") as fd:
with open(join(pathway_properties_root, "pathway_category.txt"), "w") as output:
for line in fd:
s, p, o = line.strip().split("\t")
if p == "PATHWAY_CATEGORY":
output.write(line)
def write_pathway_go_annotations():
pred_file_map = {
"GO_BP": open(
join(pathway_properties_root, "pathway_go_biological_processes.txt"), "w"
),
"GO_CC": open(
join(pathway_properties_root, "pathway_go_cellular_components.txt"), "w"
),
"GO_MF": open(
join(pathway_properties_root, "pathway_go_molecular_functions.txt"), "w"
),
}
with open(join(data_root, "reactome", "reactome_go_mapping.txt"), "r") as fd:
for line in fd:
protein, map_type, goid, pathway_id, species = line.strip().split("\t")
pred_file_map[map_type].write(f"{pathway_id}\tPATHWAY_{map_type}\t{goid}\n")
for f in pred_file_map.values():
f.close()
def write_mesh_metadata():
mesh_meta_dp = join(meta_root, "disease")
makedirs(mesh_meta_dp) if not isdir(mesh_meta_dp) else None