-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_app.py
257 lines (225 loc) · 8.9 KB
/
run_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
"""
MetOncoFit Interactive explorer
This application was built with a Flask framework using Dash and Plotly. The explorer shows three heatmaps that shows the top 10 features predicted by MetOncoFit on the y-axis, the genes corresponding to the feature predictions on the x-axis, and each cell is the corresponding gene-feature value.
@author: Scott Campit
"""
import pandas as pd
import numpy as np
import json, os
from plotly import tools
import chart_studio.plotly as py
import plotly.graph_objs as go
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import flask
import metoncofit.static
import metoncofit.functions
#import callbacks
import metoncofit.col
# Intialize the Flask/Dash application
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = 'MetOncoFit'
# Read in data
base = os.path.dirname(os.path.abspath(__file__))
df = pd.read_json(base+'/data/db.json')
# Create dataframes to parse data into three heat maps
up = df.loc[(df["Type"] == "UPREGULATED") | (df["Type"] == "GAIN")]
up = up.sort_values(by="Gini", ascending=False)
neut = df.loc[(df["Type"] == "NEUTRAL") | (df["Type"] == "NEUT")]
neut = neut.sort_values(by="Gini", ascending=False)
down = df.loc[(df["Type"] == "DOWNREGULATED") | (df["Type"] == "LOSS")]
down = down.sort_values(by="Gini", ascending=False)
colormap = metoncofit.col.Choose_Gradient('red')
# Text describing MetOncoFit
_body = metoncofit.static.header()
# Create dynamic parts that will allow client to interact with data
_widgets = metoncofit.functions.widget(data=df)
up_heatmap = metoncofit.functions.make_struct(
hm_id='up-heatmap', data=up, nam='up', cmap=colormap)
neut_heatmap = metoncofit.functions.make_struct(
hm_id='neut-heatmap', data=neut, nam='neut', cmap=colormap)
down_heatmap = metoncofit.functions.make_struct(
hm_id='down-heatmap', data=down, nam='down', cmap=colormap)
# Initialize the application
app.layout = html.Div(
[_body,
_widgets,
up_heatmap,
neut_heatmap,
down_heatmap
]
)
@app.callback(
dash.dependencies.Output('up-heatmap', 'figure'),
[dash.dependencies.Input('cancer-type', 'value'),
dash.dependencies.Input('prediction-type', 'value'),
dash.dependencies.Input('gene-slider', 'value')])
def update_up(cancer_choice, prediction_choice, slider_choice):
up_df = up[up['Target'] == prediction_choice]
up_df = up_df[up_df['Cancer'] == cancer_choice]
up_tmp = up_df['Gene'].unique().tolist()
up_tmp = up_tmp[0:slider_choice]
up_df = up_df.loc[up_df['Gene'].isin(up_tmp)]
up_df = up_df.sort_values(['Gini', 'Value'], ascending=False)
custom_hover = []
for _, i in up_df.iterrows():
dat = 'Gene: '+'{}'.format(i['Gene'])+'<br>'+'Feature: '+'{}'.format(i['Feature']) + \
'<br>'+'Value: ' + \
'{0:.2f}'.format(
i['Value'])+'<br>'+'R: '+'{0:.2f}'.format(i['R'])
custom_hover.append(dat)
return {
'data': [(
go.Heatmap(
x=up_df['Gene'],
y=up_df['Feature'],
z=up_df['Value'],
name='up-heatmap',
colorscale=colormap,
text=custom_hover,
hoverinfo='text')
)],
'layout': go.Layout(
title=go.layout.Title(
text=('<b>Target label: '+up_df['Type'].iloc[0]+'</b>'),
xanchor='left',
yanchor='bottom',
x=0.47,
font=dict(
family='Arial',
size=16,
color='black'
)
),
autosize=False,
yaxis=dict(
automargin=True,
autorange='reversed',
tickfont=dict(
family='Arial, sans-serif',
size=14,
color='black',
)
)
)
}
@app.callback(
dash.dependencies.Output('neut-heatmap', 'figure'),
[dash.dependencies.Input('cancer-type', 'value'),
dash.dependencies.Input('prediction-type', 'value'),
dash.dependencies.Input('gene-slider', 'value')])
def update_neut(cancer_choice, prediction_choice, slider_choice):
neut_df = neut[neut['Target'] == prediction_choice]
neut_df = neut_df[neut_df['Cancer'] == cancer_choice]
neut_tmp = neut_df['Gene'].unique().tolist()
neut_tmp = neut_tmp[0:slider_choice]
neut_df = neut_df.loc[neut_df['Gene'].isin(neut_tmp)]
neut_df = neut_df.sort_values(['Gini', 'Value'], ascending=False)
custom_hover = []
for _, i in neut_df.iterrows():
dat = 'Gene: '+'{}'.format(i['Gene'])+'<br>'+'Feature: '+'{}'.format(i['Feature']) + \
'<br>'+'Value: ' + \
'{0:.2f}'.format(
i['Value'])+'<br>'+'R: '+'{0:.2f}'.format(i['R'])
custom_hover.append(dat)
return {
'data': [(
go.Heatmap(
x=neut_df['Gene'],
y=neut_df['Feature'],
z=neut_df['Value'],
name='neut-heatmap',
colorscale=colormap,
text=custom_hover,
hoverinfo='text')
)],
'layout': go.Layout(
title=go.layout.Title(
text=('<b>Target label: '+neut_df['Type'].iloc[0]+'</b>'),
xanchor='left',
yanchor='bottom',
x=0.47,
font=dict(
family='Arial',
size=16,
color='black'
)
),
autosize=False,
yaxis=dict(
automargin=True,
autorange='reversed',
tickfont=dict(
family='Arial, sans-serif',
size=14,
color='black',
)
)
)
}
@app.callback(
dash.dependencies.Output('down-heatmap', 'figure'),
[dash.dependencies.Input('cancer-type', 'value'),
dash.dependencies.Input('prediction-type', 'value'),
dash.dependencies.Input('gene-slider', 'value')])
def update_down(cancer_choice, prediction_choice, slider_choice):
down_df = down[down['Target'] == prediction_choice]
down_df = down_df[down_df['Cancer'] == cancer_choice]
down_tmp = down_df['Gene'].unique().tolist()
down_tmp = down_tmp[0:slider_choice]
down_df = down_df.loc[down_df['Gene'].isin(down_tmp)]
down_df = down_df.sort_values(['Gini', 'Value'], ascending=False)
custom_hover = []
for _, i in down_df.iterrows():
dat = 'Gene: '+'{}'.format(i['Gene'])+'<br>'+'Feature: '+'{}'.format(i['Feature']) + \
'<br>'+'Value: ' + \
'{0:.2f}'.format(
i['Value'])+'<br>'+'R: '+'{0:.2f}'.format(i['R'])
custom_hover.append(dat)
return {
'data': [(
go.Heatmap(
x=down_df['Gene'],
y=down_df['Feature'],
z=down_df['Value'],
name='down-heatmap',
colorscale=colormap,
text=custom_hover,
hoverinfo='text')
)],
'layout': go.Layout(
title=go.layout.Title(
text=('<b>Target label: '+down_df['Type'].iloc[0]+'</b>'),
xanchor='left',
yanchor='bottom',
x=0.47,
font=dict(
family='Arial',
size=16,
color='black'
)
),
autosize=False,
yaxis=dict(
automargin=True,
autorange='reversed',
tickfont=dict(
family='Arial, sans-serif',
size=14,
color='black',
)
)
)
}
# Callback for the slider
@app.callback(Output('updatemode-output-container', 'children'),
[dash.dependencies.Input('gene-slider', 'value')])
def display_value(value):
return 'Number of genes displayed: {}'.format(value, value)
# For server
#server = app.server
if __name__ == '__main__':
app.run_server(debug=True)