generated from Wang-Bioinformatics-Lab/Template_Streamlit_App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtein_Dendrogram_Components.py
More file actions
425 lines (354 loc) · 17.2 KB
/
Protein_Dendrogram_Components.py
File metadata and controls
425 lines (354 loc) · 17.2 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
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
import streamlit as st
import numpy as np
import pandas as pd
import plotly
from plotly import figure_factory as ff
import requests
import scipy.cluster.hierarchy as sch
import scipy.spatial as scs
from collections import OrderedDict
from utils import format_proteins_as_strings
def draw_protein_heatmap(all_spectra_df, bin_size):
st.subheader("Protein Spectra m/z Heatmap")
# Options
all_options = format_proteins_as_strings(all_spectra_df)
selected_proteins = st.multiselect("Select proteins to display", all_options)
min_count = st.slider("Minimum m/z Count", min_value=0, max_value=max(1,len(selected_proteins)), step=1, value=int(len(selected_proteins) * 0.75),
help="The minimum number of times an m/z value must be present \
in the selected proteins to be displayed.")
min_intensity = st.slider("Minimum Relative Intensity", min_value=0.0, max_value=1.0, step=0.01, value=0.75,
help="The minimum relative intensity value to display.")
# Remove "KB Result - " from the selected proteins
selected_proteins = [x.replace("KB Result - ", "") for x in selected_proteins]
# Set index to filename
all_spectra_df = all_spectra_df.set_index("filename")
all_spectra_df = all_spectra_df.loc[selected_proteins, :]
bin_columns = [col for col in all_spectra_df.columns if col.startswith("BIN_")]
bin_columns = sorted(bin_columns, key=lambda x: int(x.split("_")[-1]))
all_spectra_df = all_spectra_df.loc[:, bin_columns]
# Normalize Intensity (Normalize Across Row)
all_spectra_df = all_spectra_df.div(all_spectra_df.max(axis=1), axis=0)
# Set zeros to nan
all_spectra_df = all_spectra_df.replace(0, np.nan)
# Set all values less than min_intensity to nan
all_spectra_df = all_spectra_df.where(all_spectra_df > min_intensity)
# Filter bins by count
bin_columns = [col for col in bin_columns if all_spectra_df[col].notna().sum() >= min_count]
all_spectra_df = all_spectra_df.loc[:, bin_columns]
def _convert_bin_to_mz(bin_name):
bin = int(bin_name.split("_")[-1])
return f"[{bin * bin_size}, {(bin + 1) * bin_size})"
# Remove rows with all nan
all_spectra_df = all_spectra_df.dropna(how='all', axis='columns')
if len(all_spectra_df.columns) != 0:
# Note: We transpose the dataframe so that the proteins are on the x-axis
st.markdown("Common m/z values between selected proteins and their relative intensities.")
# Draw Heatmap
dynamic_height = max(500, len(all_spectra_df.columns) * 24) # Dyanmic height based on number of m/z values
# If we're suppled a dendrogram, use it to reorder the heatmap
x = None
if False: # The below code is copied from the small molecule heatmap, but it doesn't work for proteins.
# I've left it here in case we want to try to get it working in the future.
# Remove any rows where the filename is not currently selected
all_filenames = st.session_state['query_only_spectra_df'].filename.values
all_data = st.session_state['query_spectra_numpy_data']
# Get the indices of the selected proteins
selected_indices = [i for i, filename in enumerate(all_filenames) if filename in st.session_state["sma_selected_proteins"]]
# Get the data for the selected proteins
numpy_data = all_data[selected_indices]
# Unfortunately, we have to recalculate the dendrogram, because things may cluster differently
# depending on the selected proteins.
# Note though, that we share parameters with the above dendrogram.
dendro = ff.create_dendrogram(numpy_data,
orientation='bottom',
labels=st.session_state["sma_selected_proteins"],
distfun=st.session_state['distance_measure'],
linkagefun=lambda x: linkage(x, method=st.session_state["sma_clustering_method"],),
color_threshold=st.session_state["sma_coloring_threshold"])
# Reorder the dataframe based on the dendrogram
reordered_df = all_spectra_df.reindex(index=dendro.layout.xaxis.ticktext)
reordered_df = reordered_df.reindex(columns=dendro.layout.yaxis.ticktext)
all_spectra_df = reordered_df
# Also us the X values from the dendrogram
x = dendro.layout.xaxis.tickvals
heatmap = plotly.express.imshow(all_spectra_df.T.values, # Transpose so m/zs are rows
x=x,
aspect ='auto',
width=1500,
height=dynamic_height,
color_continuous_scale='Bluered',)
# Update axis text (we do this here otherwise spacing is not even)
heatmap.update_layout(
xaxis=dict(title="Protein", ticktext=list(all_spectra_df.index.values), tickvals=list(range(len(all_spectra_df.index))), side='top'),
yaxis=dict(title="m/z", ticktext=[_convert_bin_to_mz(x) for x in all_spectra_df.columns], tickvals=list(range(len(all_spectra_df.columns)))),
margin=dict(t=5, pad=0),
)
heatmap.update_coloraxes(cmin=0.0, cmax=1.0, cmid=0.5)
if False: # The below code is copied from the small molecule heatmap, but it doesn't work for proteins.
# I've left it here in case we want to try to get it working in the future.
dendrogram_height = 200
dendrogram_height_as_percent = dendrogram_height / (dynamic_height + dendrogram_height)
fig = plotly.subplots.make_subplots(rows=2, cols=1,
shared_xaxes=True,
vertical_spacing=0.02,
row_heights=[dendrogram_height_as_percent, 1-dendrogram_height_as_percent])
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0, pad=0), width=1500, height=dynamic_height + dendrogram_height)
for trace in dendro.data:
fig.add_trace(trace, row=1, col=1)
# Add x-axis labels from dendrogram
print(dendro.layout.xaxis.ticktext, flush=True)
fig.update_xaxes(ticktext=dendro.layout.xaxis.ticktext, tickvals=dendro.layout.xaxis.tickvals, row=1, col=1)
fig.update_xaxes(ticktext=dendro.layout.xaxis.ticktext, tickvals=dendro.layout.xaxis.tickvals, row=2, col=1)
# Add y labels to dendrogram
fig.update_yaxes(ticktext=dendro.layout.yaxis.ticktext, tickvals=dendro.layout.yaxis.tickvals, row=1, col=1, title="Dendrogram Distance")
# Add y labels to heatmap
fig.update_yaxes(ticktext=heatmap.layout.yaxis.ticktext, tickvals=heatmap.layout.yaxis.tickvals, row=2, col=1,title="m/z")
for trace in heatmap.data:
fig.add_trace(trace, row=2, col=1)
else:
fig = heatmap
fig.update_layout(showlegend=False,
coloraxis_colorbar=dict(title="Relative Intensity",
len=min(500, dynamic_height),
lenmode="pixels",
y=0.75)
)
fig.update_coloraxes(cmin=0.0, cmax=1.0, cmid=0.5,colorscale='Bluered')
st.plotly_chart(fig,use_container_width=True)
print(all_spectra_df, flush=True)
# Rename the indices to be more human readable using _convert_bin_to_mz
all_spectra_df.columns = [_convert_bin_to_mz(x) for x in all_spectra_df.columns]
# Add a button to download the heatmap
st.download_button("Download Current Heatmap Data", all_spectra_df.T.to_csv(), "protein_heatmap.csv", help="Download the data used to generate the heatmap.")
class _Dendrogram(object):
"""
Refactored Dendrogram class to support pre-computed distance matrices.
Unless otherwise noted, it is the same as the implementation from plotly figure_factory.
"""
def __init__(
self,
X,
orientation="bottom",
labels=None,
colorscale=None,
width=np.inf,
height=np.inf,
xaxis="xaxis",
yaxis="yaxis",
distfun=None,
linkagefun=lambda x: sch.linkage(x, "complete"),
hovertext=None,
color_threshold=None,
):
self.orientation = orientation
self.labels = labels
self.xaxis = xaxis
self.yaxis = yaxis
self.data = []
self.leaves = []
self.sign = {self.xaxis: 1, self.yaxis: 1}
self.layout = {self.xaxis: {}, self.yaxis: {}}
if self.orientation in ["left", "bottom"]:
self.sign[self.xaxis] = 1
else:
self.sign[self.xaxis] = -1
if self.orientation in ["right", "bottom"]:
self.sign[self.yaxis] = 1
else:
self.sign[self.yaxis] = -1
# Logic Change: If distfun is None, we assume X is a distance matrix.
# Otherwise, we use the provided/default distfun.
(dd_traces, xvals, yvals, ordered_labels, leaves) = self.get_dendrogram_traces(
X, colorscale, distfun, linkagefun, hovertext, color_threshold
)
self.labels = ordered_labels
self.leaves = leaves
yvals_flat = yvals.flatten()
xvals_flat = xvals.flatten()
self.zero_vals = []
for i in range(len(yvals_flat)):
if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals:
self.zero_vals.append(xvals_flat[i])
if len(self.zero_vals) > len(yvals) + 1:
l_border = int(min(self.zero_vals))
r_border = int(max(self.zero_vals))
correct_leaves_pos = range(
l_border, r_border + 1, int((r_border - l_border) / len(yvals))
)
self.zero_vals = [v for v in correct_leaves_pos]
self.zero_vals.sort()
self.layout = self.set_figure_layout(width, height)
self.data = dd_traces
def get_color_dict(self, colorscale):
"""
Returns colorscale used for dendrogram tree clusters.
:param (list) colorscale: Colors to use for the plot in rgb format.
:rtype (dict): A dict of default colors mapped to the user colorscale.
"""
# These are the color codes returned for dendrograms
# We're replacing them with nicer colors
# This list is the colors that can be used by dendrogram, which were
# determined as the combination of the default above_threshold_color and
# the default color palette (see scipy/cluster/hierarchy.py)
d = {
"r": "red",
"g": "green",
"b": "blue",
"c": "cyan",
"m": "magenta",
"y": "yellow",
"k": "black",
# TODO: 'w' doesn't seem to be in the default color
# palette in scipy/cluster/hierarchy.py
"w": "white",
}
default_colors = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
if colorscale is None:
rgb_colorscale = [
"rgb(0,116,217)", # blue
"rgb(35,205,205)", # cyan
"rgb(61,153,112)", # green
"rgb(40,35,35)", # black
"rgb(133,20,75)", # magenta
"rgb(255,65,54)", # red
"rgb(255,255,255)", # white
"rgb(255,220,0)", # yellow
]
else:
rgb_colorscale = colorscale
for i in range(len(default_colors.keys())):
k = list(default_colors.keys())[i] # PY3 won't index keys
if i < len(rgb_colorscale):
default_colors[k] = rgb_colorscale[i]
# add support for cyclic format colors as introduced in scipy===1.5.0
# before this, the colors were named 'r', 'b', 'y' etc., now they are
# named 'C0', 'C1', etc. To keep the colors consistent regardless of the
# scipy version, we try as much as possible to map the new colors to the
# old colors
# this mapping was found by inpecting scipy/cluster/hierarchy.py (see
# comment above).
new_old_color_map = [
("C0", "b"),
("C1", "g"),
("C2", "r"),
("C3", "c"),
("C4", "m"),
("C5", "y"),
("C6", "k"),
("C7", "g"),
("C8", "r"),
("C9", "c"),
]
for nc, oc in new_old_color_map:
try:
default_colors[nc] = default_colors[oc]
except KeyError:
# it could happen that the old color isn't found (if a custom
# colorscale was specified), in this case we set it to an
# arbitrary default.
default_colors[n] = "rgb(0,116,217)"
return default_colors
def set_axis_layout(self, axis_key):
"""
Sets and returns default axis object for dendrogram figure.
:param (str) axis_key: E.g., 'xaxis', 'xaxis1', 'yaxis', yaxis1', etc.
:rtype (dict): An axis_key dictionary with set parameters.
"""
axis_defaults = {
"type": "linear",
"ticks": "outside",
"mirror": "allticks",
"rangemode": "tozero",
"showticklabels": True,
"zeroline": False,
"showgrid": False,
"showline": True,
}
if len(self.labels) != 0:
axis_key_labels = self.xaxis
if self.orientation in ["left", "right"]:
axis_key_labels = self.yaxis
if axis_key_labels not in self.layout:
self.layout[axis_key_labels] = {}
self.layout[axis_key_labels]["tickvals"] = [
zv * self.sign[axis_key] for zv in self.zero_vals
]
self.layout[axis_key_labels]["ticktext"] = self.labels
self.layout[axis_key_labels]["tickmode"] = "array"
self.layout[axis_key].update(axis_defaults)
return self.layout[axis_key]
def set_figure_layout(self, width, height):
"""
Sets and returns default layout object for dendrogram figure.
"""
self.layout.update(
{
"showlegend": False,
"autosize": False,
"hovermode": "closest",
"width": width,
"height": height,
}
)
self.set_axis_layout(self.xaxis)
self.set_axis_layout(self.yaxis)
return self.layout
def get_dendrogram_traces(
self, X, colorscale, distfun, linkagefun, hovertext, color_threshold
):
"""
Calculates all the elements needed for plotting a dendrogram.
"""
# --- REFACTORED LOGIC ---
if distfun is None:
# If no distance function is provided, we assume X is already
# a condensed distance matrix.
print("Assuming x is a distance matrix.", flush=True)
# Validate that this is a condensed distance matrix
if X.ndim != 1:
raise ValueError("Input distance matrix must be condensed (1D array).")
d = X
else:
# Otherwise, calculate distance from observation matrix X.
print("Calculating distance matrix from observation matrix.", flush=True)
d = distfun(X)
Z = linkagefun(d)
# -----------------------
P = sch.dendrogram(
Z,
orientation=self.orientation,
labels=self.labels,
no_plot=True,
color_threshold=color_threshold,
)
icoord = np.array(P["icoord"])
dcoord = np.array(P["dcoord"])
ordered_labels = np.array(P["ivl"])
color_list = np.array(P["color_list"])
colors = self.get_color_dict(colorscale)
trace_list = []
for i in range(len(icoord)):
if self.orientation in ["top", "bottom"]:
xs = icoord[i]
ys = dcoord[i]
else:
xs = dcoord[i]
ys = icoord[i]
color_key = color_list[i]
hovertext_label = hovertext[i] if hovertext else None
trace = dict(
type="scatter",
x=np.multiply(self.sign[self.xaxis], xs),
y=np.multiply(self.sign[self.yaxis], ys),
mode="lines",
marker=dict(color=colors[color_key]),
text=hovertext_label,
hoverinfo="text",
)
# Handle axis indexing
x_index = self.xaxis[-1] if self.xaxis[-1].isdigit() else ""
y_index = self.yaxis[-1] if self.yaxis[-1].isdigit() else ""
trace["xaxis"] = f"x{x_index}"
trace["yaxis"] = f"y{y_index}"
trace_list.append(trace)
return trace_list, icoord, dcoord, ordered_labels, P["leaves"]