-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
336 lines (262 loc) · 10.9 KB
/
app.py
File metadata and controls
336 lines (262 loc) · 10.9 KB
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
# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import pickle
import math
import dash_cytoscape as cyto
import dash_table
##############
# texts
markdown_text_1 = '''
Explore the relationships and the diversity of Natural Products (NP) datasets.
Data available at [Zenodo](https://zenodo.org/record/3547718#.XeZCMZJKiu4).
Created and maintained by M. Sorokina @ [SteinbeckLab](https://cheminf.uni-jena.de/)
'''
markdown_text_2 = '''
The desired molecular descriptor, among 19 available, to visualise can be selected in the dropdown menu.
Only few databases are displayed by default, others can be selected by scrolling and clicking on the database menu on the right of the plot.
'''
markdown_text_3 = '''
This network reflects the overlap between the Natural Product open databases in terms of content similarity.
There is a directed edge between database A and database B if more than the selected percent of the unique molecules from database A are present in database B.
The node size is proportional to the number of molecules in the database.
The slider on the top of the network allows to select the desired percentage of similarity.
'''
##############
# functions
# get table data
df = pd.read_csv('NP_databases_review_open_only.csv')
# get network edges
def read_network_data(edges_file="percent_biway_sim2_10.csv", nodes_file="databases.csv"):
nodes = set()
cy_edges, cy_nodes = [], []
elements = []
fn = open(nodes_file, "r")
for line in fn:
if not line.startswith("NP_database,size,logsize"):
dbname, dbsize, dblogsize = line.split(",")
nodes.add(dbname)
cy_nodes.append({"data": {"id": dbname, 'label': dbname, "size": dbsize }}) #'label': dbname,
elements.append({"data": {"id": dbname, 'label': dbname, "size": math.log(int(dbsize))*10 }})
fe = open(edges_file, "r")
for line in fe:
if not line.startswith("db1,db2,si"):
source, target, score = line.split(",")
if source in nodes and target in nodes:
cy_edges.append({ # Add the Edge Node
'data': {'source': source, 'target': target, 'score': score, 'label': score}
})
elements.append({'data': {'source': source, 'target': target, 'score': score, 'label': score}})
return elements
def create_cytoscape_stylesheet(min_score_display=60):
style = []
style.append(
# Group selectors
{
'selector': 'node',
'style': {
'content': 'data(label)',
'width': 'data(size)',
'height': 'data(size)',
"font-size": 18,
"text-valign": "center",
"text-halign": "center",
'shape': 'ellipse',
'background-color': 'floralWhite',
'border-color': 'black',
'border-width': '1px',
}
})
style.append(
{
'selector': 'edge',
'style': {
'label': 'data(label)',
'curve-style': 'segments',
'target-arrow-shape': 'vee'
}
}
)
style.append(
{
'selector': '[score < ' + str(min_score_display) + ']',
'style': {
'display': 'none'
}
}
)
return style
# load hist scatterplots for npl_score:
def load_data(filename):
infile = open(filename, "rb")
dict_hist = pickle.load(infile)
infile.close()
return dict_hist
def create_hist_fig(file_with_path):
dict_hists = load_data(file_with_path)
figs = []
for dbname in dict_hists.keys():
if dbname not in ["supernatural2", "cmaup", "zinc_np", "unpd"]:
one_line = dict(
x=dict_hists[dbname][1],
y=dict_hists[dbname][0],
text=dbname,
name=dbname,
mode='lines',
visible= 'legendonly',
line={'shape': 'spline', 'smoothing': 2}
#opacity=0.7,
)
figs.append(one_line)
else:
one_line = dict(
x=dict_hists[dbname][1],
y=dict_hists[dbname][0],
text=dbname,
name=dbname,
mode='lines',
line={'shape': 'spline', 'smoothing': 1.7}
# opacity=0.7,
)
figs.append(one_line)
return figs
def create_dropdown_data():
dropdown_data = [
{'label': 'NP-likeness score', 'value': 'archive/npl_score_hist_backup'},
#{'label': 'AlogP', 'value': 'archive/alogp_hist_backup'},
{'label': 'Apolarity', 'value': 'archive/apol_hist_backup'},
{'label': 'Eccentric Connectivity Index', 'value': 'archive/eccentricConnectivityIndexDescriptor_hist_backup'},
{'label': 'FMF Descriptor', 'value': 'archive/fmfDescriptor_hist_backup'},
{'label': 'Fragment Complexity Descriptor', 'value': 'archive/fragmentComplexityDescriptor_hist_backup'},
{'label': 'fsp3', 'value': 'archive/fsp3_hist_backup'},
{'label': 'Hybridization Ratio Descriptor', 'value': 'archive/hybridizationRatioDescriptor_hist_backup'},
{'label': 'Kappa Shape Index', 'value': 'archive/kappaShapeIndex1_hist_backup'},
{'label': 'Manhold logP', 'value': 'archive/manholdlogp_hist_backup'},
{'label': 'Molecular Weight', 'value': 'archive/molecular_weight_hist_backup'},
{'label': 'Petitjean Number', 'value': 'archive/petitjeanNumber_hist_backup'},
{'label': 'Topological PSA', 'value': 'archive/topoPSA_hist_backup'},
{'label': 'TPSA Efficiency', 'value': 'archive/tpsaEfficiency_hist_backup'},
{'label': 'Vertex Adjacency Magnitude', 'value': 'archive/vertexAdjMagnitude_hist_backup'},
{'label': 'Wiener Path Number', 'value': 'archive/weinerPathNumber_hist_backup'},
{'label': 'Wiener Polarity Number', 'value': 'archive/weinerPolarityNumber_hist_backup'},
{'label': 'XlogP', 'value': 'archive/xlogp_hist_backup'},
{'label': 'Zagreb Index', 'value': 'archive/zagrebIndex_hist_backup'}
]
return dropdown_data
dropdown_data_pretty = {
'archive/npl_score_hist_backup': 'NP-likeness score',
'archive/alogp_hist_backup': 'AlogP',
'archive/apol_hist_backup': 'Apolarity',
'archive/eccentricConnectivityIndexDescriptor_hist_backup': 'Eccentric Connectivity Index',
'archive/fmfDescriptor_hist_backup': 'FMF Descriptor',
'archive/fragmentComplexityDescriptor_hist_backup': 'Fragment Complexity Descriptor',
'archive/fsp3_hist_backup': 'fsp3',
'archive/hybridizationRatioDescriptor_hist_backup': 'Hybridization Ratio Descriptor',
'archive/kappaShapeIndex1_hist_backup': 'Kappa Shape Index',
'archive/manholdlogp_hist_backup': 'Manhold logP',
'archive/molecular_weight_hist_backup': 'Molecular Weight',
'archive/petitjeanNumber_hist_backup': 'Petitjean Number',
'archive/topoPSA_hist_backup': 'Topological PSA',
'archive/tpsaEfficiency_hist_backup': 'TPSA Efficiency',
'archive/vertexAdjMagnitude_hist_backup': 'Vertex Adjacency Magnitude',
'archive/weinerPathNumber_hist_backup': 'Wiener Path Number',
'archive/weinerPolarityNumber_hist_backup': 'Wiener Polarity Number',
'archive/xlogp_hist_backup': 'XlogP',
'archive/zagrebIndex_hist_backup': 'Zagreb Index'
}
#############
# build the app
#external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__)
# Load extra layouts
# cyto.load_extra_layouts()
app.title = "Natural Product Databases Comparative Analysis"
app.layout = html.Div(children=[
html.Div(className="pretty_container", children=[
html.H1(children='Natural Product Databases: diversity in 2020'),
dcc.Markdown(children=markdown_text_1),
]),
# Histograms
html.Div(className="pretty_container",children=[
html.H4(children='Distributions of molecular descriptors for NPs in databases'),
dcc.Markdown(children=markdown_text_2),
#dcc.Input(id='my-id', value='archive/npl_score_hist_backup', type='text'),
dcc.Dropdown(
id='hist_dropdown',
options=create_dropdown_data(),
value='archive/npl_score_hist_backup'
),
dcc.Graph(
id='distributions-descriptors-graph',
),
]),
html.Div(className="pretty_container", children=[
html.H4(children='Overlap network between the NP datasets'),
dcc.Markdown(children=markdown_text_3),
dcc.Slider(
id='slider-for-cytoscape',
min=10,
max=100,
marks={i: '{} %'.format(i) for i in range(10, 100, 5)},
value=50,
),
cyto.Cytoscape(
id='cytoscape-all-db',
layout={
'name': 'random'
},
style={'width': '100%', 'height': '800px'},
stylesheet=create_cytoscape_stylesheet(),
elements=read_network_data()
),
]),
html.Div(className="pretty_container", children=[
html.H4(children='List of NP datasets and their characteristics'),
dash_table.DataTable(
id='np-db-table',
style_table={
'width': '100%',
'minWidth': '400px',
'overflowX': 'scroll',
'overflowY': 'scroll',
'height': '100%',
},
style_cell={
'minWidth': '80px', 'maxWidth': '200px', 'width': 'auto',
'whiteSpace': 'normal',
'height': 'auto',
},
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict('records'),
fixed_rows={'headers': True, 'data': 0},
)
]),
])
@app.callback(
Output('distributions-descriptors-graph', 'figure'),
[Input('hist_dropdown', 'value')])
def update_distribution_histogram(selected_descriptor):
return {'data': create_hist_fig(selected_descriptor),
'layout': {
'title': dropdown_data_pretty[selected_descriptor] + " distribution in NP databases",
#(scroll the databases list and click on them to display the distribution)
'xaxis': {
'title': dropdown_data_pretty[selected_descriptor],
'showgrid': False,
},
'yaxis': {
'title': "Density",
'showgrid': False,
},
'colorway': 'Rainbow'
}}
@app.callback(
Output('cytoscape-all-db','stylesheet'),
[Input('slider-for-cytoscape', 'value')])
def update_cytoscape_visible_edges(selected_value):
return create_cytoscape_stylesheet(selected_value)
if __name__ == '__main__':
app.run_server(debug=True, host="0.0.0.0", port=8000)