-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
493 lines (466 loc) · 16.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import dash
import dash_core_components as dcc
import dash_html_components as html
from quilt.data.hsiaoyi0504 import aeolus_top5drugs
from src.python import sd_data, sd_plot
app = dash.Dash()
app.config.supress_callback_exceptions = True
data = sd_data.file_connector(aeolus_top5drugs.aeolus_top5drugs._data())
uniq_drugs = data.unique_values("drug_concept_name")
uniq_drugs_list = []
for d in uniq_drugs:
tmp = {'label': d, 'value': d}
uniq_drugs_list.append(tmp)
drug_classes = [col for col in data.data.columns if 'atc' in col]
drug_classes.append('atc_5th')
drug_classes_list = []
for d in drug_classes:
tmp = {'label': d, 'value': d}
if d == 'atc_5th':
tmp = {'label': d, 'value': 'drug_concept_name'}
drug_classes_list.append(tmp)
drugs_in_drug_classes = {}
for i in range(len(drug_classes_list)):
col = drug_classes_list[i]['value']
drugs_in_drug_classes[col] = data.data[col].unique()
all_drugs = data.counts_by_feature("age_cat")
app.layout = html.Div(children=[
dcc.Location(id='url', refresh=True),
html.Div(id='page-content')
])
index_page = html.Div(children=[
html.H1(
'Visualizing Drug Safety Data in a Web Framework: Interacting with, understanding, and communicating using Python',
className='text-center'),
html.Div(children=[
dcc.Link(
'ADR-age relation Page',
href='/age_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate Drugs (under development)',
href='/feature_drugs_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate Adverse Reactions (under development)',
href='/feature_outcomes_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate ADRs (under development)',
href='/feature_drugs_and_outcomes_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br()
], className='text-center'),
html.Hr(),
html.Div([
html.P("The Food and Drug Administration's Adverse Event Reporting System provides population-level information on drug usage and adverse reactions to drugs (ADRs). We present a standardized, processed version of this ADR data for the public."),
html.P("Feel free to use the Jupyter notebooks in our github repository SafeDrugs (www.github/NCBI-Hackathons/SafeDrugs) for the data wrangling and code implementation."),
html.P("Check out the individual fveature links to investigate frequent drugs, adverse reactions, and ADRs. Post issues on our github SafeDrugs (www.github/NCBI-Hackathons/SafeDrugs) for additional features or bugs.")
])
], className='container')
age_page = html.Div(children=[
html.H1('ADR age relation', className='text-center'),
dcc.Link(
'Go back to Home Page',
href='/',
style={
'font-size': 16,
'color': 'blue',
}),
html.Br(),
html.Hr(),
html.Div(
style={
'border': '2px solid black',
'float': 'left'
},
className='col-md-4',
children=[
html.H1(
'How many patients are taking these drugs?',
style={
'text-align': 'center',
'font-size': 18
}),
dcc.Dropdown(
id='drug_count',
options=uniq_drugs_list,
value=uniq_drugs_list[0]['value']),
html.Div(id='drug_output', style={'text-align': 'center'})
]
),
html.Div(
className='col-md-8',
children=[
dcc.Graph(
id='drug-reports-at-ages',
)
]
)
])
# TODO: finish other pages
feature_drugs_page = html.Div(children=[
html.H1(
'Investigating Drug usage \nin ADR data',
style={'text-align': 'center'}),
dcc.Link(
'Go to Home Page', href='/', style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate Adverse Reactions',
href='/feature_outcomes_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate ADRs',
href='/feature_drugs_and_outcomes_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
html.Hr(),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Drug Class'),
html.
H4('The Anatomical Thearapeutic Classification (ATC) is a standard vocabulary that categorizes drugs within domains such as organ system, mechanism of action, etc. Please choose the ATC class of drugs you want to view.'
),
dcc.Dropdown(
id="chosen-drug-class-input",
options=drug_classes_list,
value=drug_classes_list[0]['label'],
clearable=False),
html.Br(),
dcc.Dropdown(
id='chosen-drug-class-output',
multi=True)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('How many reports?'),
dcc.Graph(id='drug-class-num-reports')
]),
# Frequency across years of reporting
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Frequency across years of reporting'),
html.
H4('Reports of ADRs were given in different years, where reporting may be more or less frequent for random reasons, or because of popular events such as news and drug releases.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[dcc.Graph(id='drug-class-num-reports-across-report-years')]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Breakdown of reporting based on sex'),
html.
H4('Reporting of ADRs can be by males or females, which can vary in reporting behavior as well as have different predisposition to drug use.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
dcc.Graph(id='drug-class-num-reports-across-report-years-by-sex')
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Breakdown of reporting based on sex and age'),
html.
H4('Reporting of ADRs can be by males or females, as well as at different ages. Patient demographics can influence reporting as well as predisposition to drug use.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
dcc.Graph(
id='drug-class-num-reports-across-report-years-by-sex-and-age')
])
]),
feature_outcomes_page = html.Div(children=[
html.H1(
'Investigating Adverse Reactions from drug usage \nin ADR data',
style={'text-align': 'center'}),
dcc.Link(
'Go to Home Page', href='/', style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate Drugs',
href='/feature_drugs_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate ADRs',
href='/feature_drugs_and_outcomes_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
html.Hr(),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Number of Adverse Reactions'),
html.
H4('Patients may report one or many adverse reactions to a drug, which can either be causal or anecdotal (i.e caused by many factors) as well as reported more frequently than others. Choose how many adverse reactions you want to view.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('How many reports?'),
dcc.Graph(id='outcomes-num-reports')
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Frequency across years of reporting'),
html.
H4('Reports of ADRs were given in different years, where reporting may be more or less frequent for random reasons, or because of popular events such as news and drug releases.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[dcc.Graph(id='outcomes-num-reports-across-report-years')]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Breakdown of reporting based on sex'),
html.
H4('Reporting of ADRs can be by males or females, which can vary in reporting behavior as well as have different predisposition to drug use.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
dcc.Graph(id='outcomes-num-reports-across-report-years-by-sex')
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Breakdown of reporting based on sex and age'),
html.
H4('Reporting of ADRs can be by males or females, as well as at different ages. Patient demographics can influence reporting as well as predisposition to drug use.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
dcc.Graph(
id='outcomes-num-reports-across-report-years-by-sex-and-age')
])
]),
feature_drugs_and_outcomes_page = html.Div(children=[
html.H1('Investigating ADRs', style={'text-align': 'center'}),
dcc.Link(
'Go to Home Page', href='/', style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate Drugs',
href='/feature_drugs_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
dcc.Link(
'Investigate Adverse Reactions',
href='/feature_outcomes_page',
style={
'font-size': 16,
'color': 'blue'
}),
html.Br(),
html.Hr(),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Adverse drug reactions'),
html.
H4('ADRs that are reported may be more or less frequent in the population. Choose the number of ADRs you would like to view, with the top being the most frequent ADR in the dataset.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('How many reports?'),
dcc.Graph(id='adr-num-reports')
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Frequency across years of reporting'),
html.
H4('Reports of ADRs were given in different years, where reporting may be more or less frequent for random reasons, or because of popular events such as news and drug releases.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[dcc.Graph(id='adr-num-reports-across-report-years')]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Breakdown of reporting based on sex'),
html.
H4('Reporting of ADRs can be by males or females, which can vary in reporting behavior as well as have different predisposition to drug use.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[dcc.Graph(id='adr-num-reports-across-report-years-by-sex')]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
html.H2('Breakdown of reporting based on sex and age'),
html.
H4('Reporting of ADRs can be by males or females, as well as at different ages. Patient demographics can influence reporting as well as predisposition to drug use.'
)
]),
html.Div(
className='col-md-6',
style={'text-align': 'center'},
children=[
dcc.Graph(id='adr-num-reports-across-report-years-by-sex-and-age')
])
]),
# Update the index
# This callback constantly looks at the page location with id="url",
# and gives the pathname to the function immediately following the callback.
# The function then gives what it returns, which is a new layout.
# That new layout itself has a url! So the new url is displayed
# Every time we click the link, the pathname changes (href) and we get returned the
# corresponding page.
@app.callback(
dash.dependencies.Output('page-content', 'children'),
[dash.dependencies.Input('url', 'pathname')])
def display_page(pathname):
if pathname == '/age_page':
return age_page
if pathname == '/feature_drugs_page':
return feature_drugs_page
if pathname == '/feature_outcomes_page':
return feature_outcomes_page
if pathname == '/feature_drugs_and_outcomes_page':
return feature_drugs_and_outcomes_page
else:
return index_page
# get list of drugs or drug categories based on drug class chosen
@app.callback(
dash.dependencies.Output('chosen-drug-class-output', 'options'),
[dash.dependencies.Input('chosen-drug-class-input', 'value')])
def set_chosen_drug_class_options(value):
return [{'label': c, 'value': c} for c in drugs_in_drug_classes[value]]
# get list of drugs or drug categories based on drug class chosen
@app.callback(
dash.dependencies.Output('chosen-drug-class-output', 'value'),
[dash.dependencies.Input('chosen-drug-class-input', 'value')])
def set_chosen_drug_class_value(available_options):
return available_options
@app.callback(
dash.dependencies.Output('drug-class-num-reports', 'options'), [
dash.dependencies.Input('chosen-drug-class-input', 'value'),
dash.dependencies.Input('chosen-drug-class-output', 'value')
])
def plot_drug_class_num_reports(col, classes):
title = ''
x_legend = "classes"
y_legend = "Number of reports"
dat = data.counts_by_feature("report_year", '{0} in {1}'.format(
col, classes))
return sd_plot.bar_plot(
title,
x_legend,
y_legend,
trace1=dat,
t1name="Drugs",
trace2=False,
t2name=False)
#Show number of patients taking selected drug in dataset
@app.callback(
dash.dependencies.Output('drug_output', 'children'), [
dash.dependencies.Input('drug_count', 'value'),
])
def callback_drug(value):
return 'There are {} patients that reported taking {}'.format(
data.count('drug_concept_name=="' + value + '"'), value)
#Update bar graph for drug
@app.callback(
dash.dependencies.Output('drug-reports-at-ages', 'figure'),
[dash.dependencies.Input('drug_count', 'value')])
def callback_drug_reports_at_ages_bars(value):
title = 'Patients taking {} at different age intervals'.format(value)
x_legend = "Age category"
y_legend = "Percentage of reports"
this_drug_title = '{}'.format(value)
all_drugs_title = "All drugs"
this_drug = data.counts_by_feature("age_cat",
'drug_concept_name == "' + value + '"')
return sd_plot.bar_plot(title, x_legend, y_legend, this_drug, all_drugs,
this_drug_title, all_drugs_title)
# enable bootstrap styling
external_css = [["https://bootswatch.com/3/paper/bootstrap.css"],
["docs/custom.css"]]
for css in external_css:
app.css.append_css({"external_url": css})
if __name__ == '__main__':
app.run_server(debug=True)