forked from mwang87/NP-Classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
330 lines (269 loc) · 11.6 KB
/
app.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
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_table
import plotly.express as px
from dash.dependencies import Input, Output
import os
from zipfile import ZipFile
import urllib.parse
from flask import Flask, request
import json
import pandas as pd
import requests
import numpy as np
import urllib.parse
import sys
sys.path.insert(0, "Classifier")
import fingerprint_handler
import prediction_voting
server = Flask(__name__)
app = dash.Dash(__name__, server=server, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = 'NP Classifier'
server = app.server
from models import ClassifyEntity
ontology_dictionary = json.loads(open("Classifier/dict/index_v1.json").read())
NAVBAR = dbc.Navbar(
children=[
dbc.NavbarBrand(
html.Img(src="https://gnps-cytoscape.ucsd.edu/static/img/GNPS_logo.png", width="120px"),
href="https://gnps.ucsd.edu"
),
dbc.Nav(
[
dbc.NavItem(dbc.NavLink("NP Classifier", href="#")),
dbc.NavItem(dbc.NavLink("Report Feedback", href="https://docs.google.com/forms/d/e/1FAIpQLSf1-sw-P0SQGokyeaOpEmLda0UPJW93qkrI8rfp7D46fHVi6g/viewform?usp=sf_link")),
dbc.NavItem(dbc.NavLink("Preprint Publication", href="https://chemrxiv.org/articles/preprint/NPClassifier_A_Deep_Neural_Network-Based_Structural_Classification_Tool_for_Natural_Products/12885494/1")),
dbc.NavItem(dbc.NavLink("API", href="https://ccms-ucsd.github.io/GNPSDocumentation/api/#structure-np-classifier")),
],
navbar=True)
],
color="light",
dark=False,
sticky="top",
)
DASHBOARD = [
dcc.Location(id='url', refresh=False),
dbc.CardHeader(html.H5("NP Classifier")),
dbc.CardBody(
[
html.Div(id='version', children="Version - 1.5"),
html.Br(),
dbc.Textarea(className="mb-3", id='smiles_string', placeholder="Smiles Structure"),
dcc.Loading(
id="structure",
children=[html.Div([html.Div(id="loading-output-5")])],
type="default",
),
dcc.Loading(
id="classification_table",
children=[html.Div([html.Div(id="loading-output-3")])],
type="default",
),
html.Hr(),
dcc.Loading(
id="usage_summary",
children=[html.Div([html.Div(id="loading-output-323")])],
type="default",
)
]
)
]
CONTRIBUTORS_DASHBOARD = [
dbc.CardHeader(html.H5("Software Contributors")),
dbc.CardBody(
[
"Hyunwoo Kim PhD - UC San Diego",
html.Br(),
"Mingxun Wang PhD - UC Riverside",
html.Br(),
html.Br(),
html.H5("Citation"),
html.A('Kim HW, Wang M, Leber CA, Nothias LF, Reher R, Kang KB, van der Hooft JJJ, Dorrestein PC, Gerwick WH, Cottrell GW. NPClassifier: A Deep Neural Network-Based Structural Classification Tool for Natural Products. J Nat Prod. 2021 Oct 18. doi: 10.1021/acs.jnatprod.1c00399. Epub ahead of print. PMID: 34662515.',
href="https://pubmed.ncbi.nlm.nih.gov/34662515/"),
html.Br(),
html.Br(),
html.A('Checkout our other work!',
href="https://www.cs.ucr.edu/~mingxunw/")
]
)
]
BODY = dbc.Container(
[
dbc.Row([dbc.Col(dbc.Card(DASHBOARD)),], style={"marginTop": 30}),
html.Br(),
dbc.Row([dbc.Col(dbc.Card(CONTRIBUTORS_DASHBOARD)),]),
],
className="mt-12",
)
app.layout = html.Div(children=[NAVBAR, BODY])
# This enables parsing the URL to shove a task into the qemistree id
@app.callback(Output('smiles_string', 'value'),
[Input('url', 'pathname')])
def display_page(pathname):
# Otherwise, lets use the url
if len(pathname) > 1:
return pathname[1:]
else:
return "CC1C(O)CC2C1C(OC1OC(COC(C)=O)C(O)C(O)C1O)OC=C2C(O)=O"
# This function will rerun at any
@app.callback(
[Output('classification_table', 'children'), Output('structure', 'children')],
[Input('smiles_string', 'value')],
)
def handle_smiles(smiles_string):
classification_dict = _process_full_classification(smiles_string)
#isglycoside, class_results, superclass_results, pathway_results, path_from_class, path_from_superclass, n_path, fp1, fp2 = classify_structure(smiles_string)
output_list = []
for result in classification_dict["pathway_results"]:
output_dict = {}
output_dict["type"] = "pathway"
output_dict["entry"] = result
output_list.append(output_dict)
for result in classification_dict["superclass_results"]:
output_dict = {}
output_dict["type"] = "superclass"
output_dict["entry"] = result
output_list.append(output_dict)
for result in classification_dict["class_results"]:
output_dict = {}
output_dict["type"] = "class"
output_dict["entry"] = result
output_list.append(output_dict)
if classification_dict["isglycoside"]:
output_dict = {}
output_dict["type"] = "is glycoside"
output_dict["entry"] = "glycoside"
output_list.append(output_dict)
#Creating Table
white_list_columns = ["type", "entry"]
table_fig = dash_table.DataTable(
columns=[
{"name": i, "id": i, "deletable": False, "selectable": True} for i in white_list_columns
],
data=output_list,
editable=False,
filter_action="native",
sort_action="native",
sort_mode="multi",
row_deletable=False,
selected_columns=[],
selected_rows=[],
page_action="native",
page_current= 0,
page_size= 10,
)
# Creating Structure Image
img_obj = html.Img(id='image', src="https://gnps-structure.ucsd.edu/structureimg?smiles={}".format(urllib.parse.quote(smiles_string)))
return [table_fig, img_obj]
# This function will rerun at any
@app.callback(
[Output('usage_summary', 'children')],
[Input('url', 'pathname')],
)
def usage_summary(pathname):
number_entries = ClassifyEntity.select().count()
return ["Total Unique SMILES Classified - {}".format(number_entries)]
def classify_structure(smiles):
isglycoside = fingerprint_handler._isglycoside(smiles)
fp = fingerprint_handler.calculate_fingerprint(smiles, 2)
fp1 = fp[0].tolist()[0]
fp2 = fp[1].tolist()[0]
query_dict = {}
query_dict["input_2048"] = fp1
query_dict["input_4096"] = fp2
# Handling SUPERCLASS
fp_pred_url = "http://npclassifier-tf-server:8501/v1/models/SUPERCLASS:predict"
payload = json.dumps({"instances": [ query_dict ]})
headers = {"content-type": "application/json"}
json_response = requests.post(fp_pred_url, data=payload, headers=headers)
pred_super = np.asarray(json.loads(json_response.text)['predictions'])[0]
n_super = list(np.where(pred_super>=0.3)[0])
path_from_superclass = []
for j in n_super:
path_from_superclass += ontology_dictionary['Super_hierarchy'][str(j)]['Pathway']
path_from_superclass = list(set(path_from_superclass))
query_dict = {}
query_dict["input_2048"] = fp1
query_dict["input_4096"] = fp2
# Handling CLASS
fp_pred_url = "http://npclassifier-tf-server:8501/v1/models/CLASS:predict"
payload = json.dumps({"instances": [ query_dict ]})
headers = {"content-type": "application/json"}
json_response = requests.post(fp_pred_url, data=payload, headers=headers)
pred_class = np.asarray(json.loads(json_response.text)['predictions'])[0]
n_class = list(np.where(pred_class>=0.1)[0])
path_from_class = []
for j in n_class:
path_from_class += ontology_dictionary['Class_hierarchy'][str(j)]['Pathway']
path_from_class = list(set(path_from_class))
query_dict = {}
query_dict["input_2048"] = fp1
query_dict["input_4096"] = fp2
# Handling PATHWAY
fp_pred_url = "http://npclassifier-tf-server:8501/v1/models/PATHWAY:predict"
payload = json.dumps({"instances": [ query_dict ]})
headers = {"content-type": "application/json"}
json_response = requests.post(fp_pred_url, data=payload, headers=headers)
pred_path = np.asarray(json.loads(json_response.text)['predictions'])[0]
n_path = list(np.where(pred_path>=0.5)[0])
class_result = []
superclass_result = []
pathway_result = []
# Voting on Answer
pathway_result, superclass_result, class_result, isglycoside = prediction_voting.vote_classification(n_path,
n_class,
n_super,
pred_class,
pred_super,
path_from_class,
path_from_superclass,
isglycoside,
ontology_dictionary)
return isglycoside, class_result, superclass_result, pathway_result, path_from_class, path_from_superclass, n_path, fp1, fp2
def _process_full_classification(smiles_string):
try:
db_record = ClassifyEntity.get(ClassifyEntity.smiles == smiles_string)
return json.loads(db_record.classification_json)
except:
pass
isglycoside, class_results, superclass_results, pathway_results, path_from_class, path_from_superclass, n_path, fp1, fp2 = classify_structure(smiles_string)
respond_dict = {}
respond_dict["class_results"] = class_results
respond_dict["superclass_results"] = superclass_results
respond_dict["pathway_results"] = pathway_results
respond_dict["isglycoside"] = isglycoside
# Not including this extra information to slim down the caching database
#respond_dict["fp1"] = fp1
#respond_dict["fp2"] = fp2
# Lets save the result here, we should also check if its changed, and if so, we overwrite
try:
# Save it out
ClassifyEntity.create(
smiles=smiles_string,
classification_json=json.dumps(respond_dict)
)
except:
pass
return respond_dict
@server.route("/classify")
def classify():
smiles_string = request.values.get("smiles")
respond_dict = _process_full_classification(smiles_string)
return json.dumps(respond_dict)
# This gets you the model metadata
@server.route("/model/metadata")
def metadata():
"""Serve a file from the upload directory."""
all_metadata = {}
pathway_metadata = json.loads(requests.get("http://npclassifier-tf-server:8501/v1/models/PATHWAY/metadata").text)
class_metadata = json.loads(requests.get("http://npclassifier-tf-server:8501/v1/models/CLASS/metadata").text)
superclass_metadata = json.loads(requests.get("http://npclassifier-tf-server:8501/v1/models/SUPERCLASS/metadata").text)
all_metadata["pathway"] = pathway_metadata
all_metadata["class"] = class_metadata
all_metadata["superclass"] = superclass_metadata
return json.dumps(all_metadata)
if __name__ == "__main__":
app.run_server(debug=True, port=5000, host="0.0.0.0")