-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrug_combinations_graph.py
171 lines (148 loc) · 4.47 KB
/
drug_combinations_graph.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
import json
import dash
import dash_cytoscape as cyto
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import csv
import networkx as nx
app = dash.Dash(__name__)
edge_list = []
nx_edge_list = []
node_list = []
with open('drug_combinations.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# print(row)
source = row['source']
target = row['target']
label = int(row['weight'])
# print(type(label)) should be int
if source not in node_list:
node_list.append(source)
if target not in node_list:
node_list.append(target)
source_target_label = tuple(
(source, target, label))
edge_for_nx = [source, target]
nx_edge_list.append(edge_for_nx)
edge_list.append(source_target_label)
# create NetworkX object for shortest path
G = nx.Graph()
G.add_nodes_from(node_list)
G.add_edges_from(nx_edge_list)
neighbors_dict = {}
for node in G.nodes:
neighbors_dict[node] = [n for n in G.neighbors(node)]
# print(G.nodes)
neighbors_list = list(neighbors_dict.values())
new_list = []
for neighbor in neighbors_list:
new_list.append(','.join(neighbor))
# print(new_list)
# p = nx.shortest_path(G, source='metformin', target='glyburide')
# print(p)
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
}
}
# print(node_list)
# for node in node_list:
# print([n for n in G.predecessors(node)])
# print(edge_list)
nodes = [
{
'data': {'id': label, 'label': str(label), 'neighbor': neighbor}
}
for label in (
node_list
)
for neighbor in (
new_list
)
]
# print(nodes)
edges = [
{'data': {'id': source+'--'+target+'--'+str(label), 'source': source,
'target': target, 'label': label}}
for source, target, label in (
edge_list
)
]
default_stylesheet = [
{
'selector': 'node',
'style': {
'background-color': '#BFD7B5',
'label': 'data(label)'
}
},
{
'selector': 'edge',
'style': {
'curve-style': 'bezier',
'width': '2px',
# 'label': 'data(label)',
'font-size': '10px',
'font-opacity': 1,
# 'target-arrow-color': 'black',
# 'target-arrow-shape': 'triangle',
'line-color': 'green'
}
},
{
'selector': ':selected',
'style': {
'border-color': 'black',
'border-opacity': '1',
'border-width': '0.075px',
'label': 'data(label)'
}
},
{
'selector': '[label > 1000]',
'style': {
'line-color': 'red'
}
}
]
cyto.load_extra_layouts()
app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape-event-callbacks-2',
layout={'name': 'cola', 'nodeSpacing': 70},
elements=edges+nodes,
stylesheet=default_stylesheet,
style={'width': '100%', 'height': '450px'}
),
# html.P(id='cytoscape-tapNodeData-output'),
# html.P(id='cytoscape-tapEdgeData-output'),
html.P(id='cytoscape-mouseoverNodeData-output'),
html.P(id='cytoscape-mouseoverEdgeData-output')
])
# @app.callback(Output('cytoscape-tapNodeData-output', 'children'),
# Input('cytoscape-event-callbacks-2', 'tapNodeData'))
# def displayTapNodeData(data):
# if data:
# return "You recently clicked/tapped the node: " + data['label']
# @app.callback(Output('cytoscape-tapEdgeData-output', 'children'),
# Input('cytoscape-event-callbacks-2', 'tapEdgeData'))
# def displayTapEdgeData(data):
# if data:
# return "Use " + data['label'] + " to get from " + \
# data['source'].upper() + " to " + data['target'].upper()
@app.callback(Output('cytoscape-mouseoverNodeData-output', 'children'),
Input('cytoscape-event-callbacks-2', 'mouseoverNodeData'))
def displayTapNodeData(data):
if data:
return "Taken with: " + str(neighbors_dict[data['label']])
@app.callback(Output('cytoscape-mouseoverEdgeData-output', 'children'),
Input('cytoscape-event-callbacks-2', 'mouseoverEdgeData'))
def displayTapEdgeData(data):
if data:
return str(data['label']) + " patients have taken " + \
data['source'].upper() + " with " + data['target'].upper()
if __name__ == '__main__':
app.run_server(debug=True)