-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash_app.py
350 lines (307 loc) · 12.4 KB
/
dash_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
import os
import dash
import dash_bootstrap_components as dbc
import pandas as pd
import plotly.graph_objects as go
from dash import dcc, html
from dash.dependencies import Input, Output
# Step 1: Load the compound list
def load_compound_list(compound_file: str) -> tuple[pd.DataFrame, list[str]]:
"""
Load the list of compounds from a CSV file.
Args:
compound_file (str): The path to the CSV file containing the compound list.
Returns:
tuple[pd.DataFrame, list[str]]: A tuple containing the DataFrame of compounds and a list of compound names.
"""
print(f"Loading compound list from {compound_file}...")
compounds_df = pd.read_csv(compound_file)
compounds = compounds_df["compound_name"].tolist()
print(f"Loaded {len(compounds)} compounds.")
return compounds_df, compounds
# Step 2: Load and filter data for specific metrics
def load_data(
data_directory: str, compounds: list[str]
) -> tuple[dict[str, pd.DataFrame], dict[str, dict]]:
"""
Load and filter the activity data for specific metrics (e.g., ED50, TD50) for each compound.
Args:
data_directory (str): The directory where the data files are located.
compounds (list[str]): The list of compound names.
Returns:
tuple[dict[str, pd.DataFrame], dict[str, dict]]:
A tuple containing two dictionaries:
- The first dictionary maps compound names to their filtered data.
- The second dictionary maps compound names to their properties.
"""
print("Starting to load data for each compound...")
metrics = ["ED50", "TD50"]
data = {}
properties = {}
for compound in compounds:
filepath = os.path.join(data_directory, f"{compound}_activity_data.csv")
info_filepath = os.path.join("drug_info", f"{compound}_info.csv")
if os.path.exists(filepath):
print(f"Loading data for {compound} from {filepath}...")
try:
df = pd.read_csv(filepath)
print(f"Data for {compound} loaded successfully, shape: {df.shape}")
df_filtered = df[df["standard_type"].isin(metrics)]
print(
f"Filtered data for {compound}, remaining rows: {len(df_filtered)}"
)
if not df_filtered.empty:
data[compound] = df_filtered
if os.path.exists(info_filepath):
properties[compound] = (
pd.read_csv(info_filepath).iloc[0].to_dict()
)
except Exception as e:
print(f"Error loading data for {compound}: {e}")
else:
print(f"File not found: {filepath}")
print("Data loading completed.")
return data, properties
# Create visualizations using Plotly Core Library
def create_visualizations(data: dict[str, pd.DataFrame]) -> dict[str, go.Figure]:
"""
Create visualizations (scatter plots and box plots) for each compound's ED50 and TD50 data.
Args:
data (dict[str, pd.DataFrame]): The dictionary mapping compound names to their filtered data.
Returns:
dict[str, go.Figure]: A dictionary mapping compound names to their corresponding Plotly figures.
"""
print("Starting to create visualizations...")
figures = {}
for compound, df in data.items():
# Check if both ED50 and TD50 are present
if all(metric in df["standard_type"].values for metric in ["ED50", "TD50"]):
# Scatter plot of ED50 vs TD50 using Plotly core
scatter_fig = go.Figure()
for metric in ["ED50", "TD50"]:
filtered_df = df[df["standard_type"] == metric]
scatter_fig.add_trace(
go.Scatter(
x=filtered_df["standard_value"],
y=[metric] * len(filtered_df),
mode="markers",
marker=dict(
size=10,
color=(
"rgba(135, 206, 250, .9)"
if metric == "ED50"
else "rgba(255, 99, 71, .9)"
),
),
name=metric,
text=filtered_df[
"assay_description"
], # Include assay_description in hover text
hoverinfo="text+x+y",
)
)
scatter_fig.update_layout(
title=f"{compound.capitalize()}: ED50 vs TD50 by Target Organism",
xaxis_title="Standard Value (mg/kg)",
yaxis_title="Measurement Type",
yaxis=dict(tickmode="array", tickvals=["ED50", "TD50"]),
hovermode="closest",
)
figures[f"{compound}_ed_td"] = scatter_fig
print(f"ED50/TD50 scatter plot created for {compound}.")
# Distribution plot for ED50 and TD50 using Plotly core
box_fig = go.Figure()
for metric in ["ED50", "TD50"]:
filtered_df = df[df["standard_type"] == metric]
box_fig.add_trace(
go.Box(
y=filtered_df["standard_value"],
x=[metric] * len(filtered_df),
name=metric,
boxpoints="all", # show all points
jitter=0.3,
pointpos=-1.8,
marker=dict(
color=(
"rgba(135, 206, 250, .9)"
if metric == "ED50"
else "rgba(255, 99, 71, .9)"
)
),
text=filtered_df[
"assay_description"
], # Include assay_description in hover text
hoverinfo="text+x+y",
)
)
box_fig.update_layout(
title=f"{compound.capitalize()}: Distribution of ED50 and TD50",
yaxis_title="Standard Value (mg/kg)",
xaxis_title="Measurement Type",
hovermode="closest",
)
figures[f"{compound}_distribution"] = box_fig
print(f"ED50/TD50 distribution plot created for {compound}.")
print("Visualizations created successfully.")
return figures
# Create a Dash app
def create_dash_app(
figures: dict[str, go.Figure],
properties: dict[str, dict],
data: dict[str, pd.DataFrame],
) -> dash.Dash:
"""
Create a Dash app for visualizing compound data and their properties.
Args:
figures (dict[str, go.Figure]): A dictionary of figures for each compound.
properties (dict[str, dict]): A dictionary of properties for each compound.
data (dict[str, pd.DataFrame]): A dictionary of the compound data for the new page.
Returns:
dash.Dash: The Dash app instance.
"""
app = dash.Dash(__name__, suppress_callback_exceptions=True)
print("Initializing Dash app...")
# Home page layout
home_layout = html.Div(
[
html.H1(
"Effective dose vs toxic dose for anticonvulsant drugs",
style={"textAlign": "center", "margin-bottom": "20px"},
),
dcc.Dropdown(
id="compound-dropdown",
options=[
{"label": f"{compound.capitalize()} - ED50 vs TD50", "value": key}
for key, compound in [
(key, key.split("_")[0]) for key in figures.keys()
]
],
value=list(figures.keys())[0] if figures else None,
clearable=False,
style={"width": "50%", "margin": "auto"},
),
html.Div(
id="drug-info", style={"textAlign": "center", "margin-top": "20px"}
),
html.Hr(), # Horizontal line separator
html.Div(
dcc.Link(
"Go to Organism Data",
href="/organisms",
style={
"display": "block",
"textAlign": "center",
"fontWeight": "bold",
"margin-top": "20px",
},
)
),
dcc.Graph(id="compound-graph"),
]
)
# New page layout for organism data
organism_layout = html.Div(
[
html.H1(
"Organisms and Test Frequencies by Drug",
style={"textAlign": "center", "margin-bottom": "20px"},
),
dcc.Link(
"Go to Home Page",
href="/",
style={
"display": "block",
"textAlign": "center",
"margin-bottom": "20px",
"fontWeight": "bold",
},
),
html.Div(id="organism-data"),
dcc.Graph(id="organism-bar-chart"),
]
)
app.layout = html.Div(
[
dcc.Location(id="url", refresh=False),
html.Div(id="page-content"),
]
)
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def display_page(pathname):
if pathname == "/organisms":
return organism_layout
else:
return home_layout
@app.callback(
[Output("compound-graph", "figure"), Output("drug-info", "children")],
[Input("compound-dropdown", "value")],
)
def update_graph(selected_compound: str):
print(f"Updating graph for {selected_compound}")
fig = figures[selected_compound]
compound_name = selected_compound.split("_")[0]
info = properties.get(compound_name, {})
info_text = html.Div(
[
html.P(f"Drug: {info.get('Drug', 'N/A')}"),
html.P(f"Molecular Formula: {info.get('Molecular Formula', 'N/A')}"),
html.P(
f"Molecular Weight: {info.get('Molecular Weight', 'N/A')} g/mol"
),
html.P(f"XLogP: {info.get('XLogP', 'N/A')}"),
html.P(f"H-Bond Donor Count: {info.get('H-Bond Donor Count', 'N/A')}"),
html.P(
f"H-Bond Acceptor Count: {info.get('H-Bond Acceptor Count', 'N/A')}"
),
html.P(f"Exact Mass: {info.get('Exact Mass', 'N/A')} g/mol"),
html.P(
f"Topological Polar Surface Area (TPSA): {info.get('Topological Polar Surface Area (TPSA)', 'N/A')} Ų"
),
]
)
return fig, info_text
@app.callback(
[Output("organism-data", "children"), Output("organism-bar-chart", "figure")],
Input("url", "pathname"),
)
def display_organism_data(_):
organism_data = []
for compound, df in data.items():
organism_counts = df["target_organism"].dropna().value_counts().to_dict()
for organism, count in organism_counts.items():
organism_data.append(
{
"Compound": compound.capitalize(),
"Organism": organism,
"Test Count": count,
}
)
organism_df = pd.DataFrame(organism_data)
organism_table = dbc.Table.from_dataframe(
organism_df,
striped=True,
bordered=True,
hover=True,
responsive=True,
)
# Create a bar chart visualization
bar_chart = go.Figure()
for compound in organism_df["Compound"].unique():
compound_df = organism_df[organism_df["Compound"] == compound]
bar_chart.add_trace(
go.Bar(
x=compound_df["Organism"],
y=compound_df["Test Count"],
name=compound,
)
)
bar_chart.update_layout(
title="Test Counts by Organism and Drug",
xaxis_title="Organism",
yaxis_title="Test Count",
barmode="group",
hovermode="closest",
)
return organism_table, bar_chart
print("Dash app initialized successfully.")
return app