-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp[FINAL].py
189 lines (165 loc) · 8.22 KB
/
app[FINAL].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
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__)
server = app.server
edge_list = []
nx_edge_list = []
node_list = []
with open('reaction_map.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# print(row)
source = row['\ufeffSource']
target = row['End']
label = row['Label']
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.DiGraph()
G.add_nodes_from(node_list)
G.add_edges_from(nx_edge_list)
predecessor_dict = {}
for node in G.nodes:
predecessor_dict[node] = [n for n in G.predecessors(node)]
# print(G.nodes)
predecessor_list = list(predecessor_dict.values())
new_list = []
for predecessor in predecessor_list:
new_list.append(','.join(predecessor))
# print(new_list)
# p = nx.shortest_path(G, source='1° Alcohol', target='1° Amine')
# 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)
node_images = ['https://orgosynthesissolver.s3.amazonaws.com/assets/acid_chloride.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/primary_alcohol.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/keton3.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/tert_alcohol.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/acid_azide.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/amine_prim.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/sec_amide.png',
'https://orgosynthesissolver.s3.amazonaws.com/assets/tert_amide.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/prim_amide.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/carb_acid.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alde.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/est.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/cyanohy.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/anhydride.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/nitrile1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alkylazide1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/ammoniumsalt1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/quatammoniumsalt2.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/sec_amine.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/tert_amine2.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/phosge.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/urea1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/chloroform.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/carbamate.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/benzen.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/chlorobenzen.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/nitrobenzen.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/aniline1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alkylbenzene.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/benzoicacid.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alkylhalide.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/isocya.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alkene.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/carboxylate.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/sec_alcohol.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/oxime.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/iminium1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/amine_4.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/hydrazone1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/enamine.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/imine1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alkylhalide.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/grignard.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/carbonate.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alphahaloketone1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alphahalohalide.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/alphahalocarboacid1.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/methylketone.png', 'https://orgosynthesissolver.s3.amazonaws.com/assets/haloform.png']
zipped_node_list = [list(l) for l in zip(node_list, node_images)]
nodes = [
{'classes': 'images',
'data': {'id': label, 'label': label, 'predecessor': predecessor, 'url': url}
}
# 'predecessors': predecessors
# for label in (
# node_list
# )
for label, url in (
zipped_node_list
)
for predecessor in (
new_list
)
]
# print(nodes)
edges = [
{'data': {'id': source+'--'+target+'--'+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',
'arrow-scale': 2,
'line-color': 'green'
}
},
{
'selector': ':selected',
'style': {
'border-color': 'black',
'border-opacity': '1',
'border-width': '0.075px',
'label': 'data(label)'
}
},
{
'selector': '.images',
'style': {
'width': 120,
'height': 80,
'background-fit': 'cover',
'background-image': 'data(url)'
}
}
]
cyto.load_extra_layouts()
app.layout = html.Div([
cyto.Cytoscape(
id='cytoscape-event-callbacks-2',
layout={'name': 'cose', 'nodeSpacing': 100},
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 "How to get here: " + str(predecessor_dict[data['label']])
@app.callback(Output('cytoscape-mouseoverEdgeData-output', 'children'),
Input('cytoscape-event-callbacks-2', 'mouseoverEdgeData'))
def displayTapEdgeData(data):
if data:
return "Use " + data['label'] + " to get from " + \
data['source'].upper() + " to " + data['target'].upper()
if __name__ == '__main__':
app.run_server(debug=True)