-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph_builder.py
92 lines (56 loc) · 2.67 KB
/
graph_builder.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
from builtins import dict
from platform import win32_edition
import matplotlib.pyplot as plt
import json
from common.dataset_class import Dataset
from common.cograph_class import Cograph
import networkx as nx
import common.stats
from collections import Counter
import numpy as np
def load_settings():
settings = {}
with open('zttk_case_study/settings.json', 'r') as f:
cont = f.read()
settings = json.loads(cont)
return settings
def get_inverse_thresaurs(thresaurs : dict):
inverse_thresaurus = {}
for (k, v) in thresaurs.items():
for i in v:
inverse_thresaurus[i] = k
return inverse_thresaurus
def main():
settings = load_settings() # loaded ad a dict, TODO: use settingsclass
inverse_thresaurus = get_inverse_thresaurs(settings.get('thesaurus'))
dataset = Dataset()
for k, v in settings.get('dataset').items():
if v:
dataset.add_from_path(k)
print('Normalization')
dataset.normalize(inverse_thresaurus)
work_graph = Cograph()
print('Graph building')
work_graph.add_dataset(dataset, norm_type=settings.get('normalization_type'))
print('Graph saving')
work_graph.save_nodes_to_path("./zttk_case_study/results/nodes.txt")
work_graph.save_edges_to_path("./zttk_case_study/results/edges.txt")
print('Disease ranking')
work_graph.disease_rank(source=settings.get('rank_source'), rank_type=settings.get('rank_type'), algorithm=settings.get('rank_algorithm'), path_to_save="./results/disease_rank.txt")
widest_set = work_graph.widest_set(settings.get('widest_set'), bbent_types = settings.get('bioBERT_entity_types_widest_set') )# widest set with only selected types of entities
neighbors = work_graph.get_neighbors(widest_set, bbent_types = settings.get('bioBERT_entity_types_neighbors'), max_for_node = settings.get('max_neighbors_for_node')) # second layer with only selected types of entities
showing_nodes = widest_set + neighbors
showing_nodes += work_graph.get_main_nodes(max=settings.get('num_other_relevant_nodes'))
nodes_layer = {}
for n in showing_nodes:
if n in widest_set:
nodes_layer[n] = 'first'
elif n in neighbors:
nodes_layer[n] = 'second'
else:
nodes_layer[n] = 'third'
showing_graph : Cograph = work_graph.draw( showing_nodes=showing_nodes, layout=settings.get('layout'), nodes_layer=nodes_layer, percentage = settings.get('percentage_of_showing_edges'))
showing_graph.export_cytoscape_data("./zttk_case_study/results/cytoskape_format.json")
plt.show()
if __name__ == "__main__":
main()