Skip to content

Commit 2ea8df8

Browse files
authored
Merge pull request #153 from ncats/ab_adjust_plotting
Unsupervised clustering: changed aspect ratios and added normalization options for the plots
2 parents 8e780e9 + 5bbb1ea commit 2ea8df8

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

pages2/Pheno_Cluster_a.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,10 @@ def phenocluster__plotly_umaps(adata, umap_cur_col, umap_cur_groups, umap_color_
12791279
y=-0.2,
12801280
xanchor="right",
12811281
x=1
1282-
)
1282+
),
1283+
xaxis=dict(
1284+
scaleanchor="y",
1285+
scaleratio=1)
12831286
)
12841287
if i % 2 == 0:
12851288
subcol1.plotly_chart(fig, use_container_width=True)

pages2/Pheno_Cluster_b.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,21 @@ def phenocluster__plot_diff_intensity(adata, groups, method, n_genes, cur_col):
9696
# ncols=3, show=True,
9797
# wspace = 0.2 ,save = False), use_container_width = True , clear_figure = True)
9898

99-
def phenocluster__plot_diff_intensity_2(adata, groups, method, n_genes, plot_column):
99+
def phenocluster__plot_diff_intensity_2(adata, groups, method, n_genes,
100+
plot_column, normalize_total,
101+
log_normalize, z_normalize,
102+
cluster_group):
100103
if "All" in groups:
101104
cur_groups = list(pd.unique(st.session_state['phenocluster__de_results']["group"]))
102105
else:
103106
cur_groups = groups
104107
adata_norm = adata.copy()
105-
sc.pp.normalize_total(adata_norm)
106-
sc.pp.log1p(adata_norm)
107-
sc.pp.scale(adata_norm)
108-
if "Edit_Cluster" in adata.obs.columns:
109-
cluster_group = "Edit_Cluster"
110-
else:
111-
cluster_group = "Cluster"
108+
if normalize_total:
109+
sc.pp.normalize_total(adata_norm)
110+
if log_normalize:
111+
sc.pp.log1p(adata_norm)
112+
if z_normalize:
113+
sc.pp.scale(adata_norm)
112114

113115
adata_sub = adata_norm[adata_norm.obs[cluster_group].isin(cur_groups)]
114116
top_names = pd.unique(st.session_state['phenocluster__de_results'].groupby('group')['names'].apply(lambda x: x.head(n_genes)))
@@ -122,18 +124,18 @@ def phenocluster__plot_diff_intensity_2(adata, groups, method, n_genes, plot_col
122124
matrix_avg.columns = [cluster_group, "Marker", "Intensity"]
123125
plot_mat = matrix_avg[matrix_avg["Marker"].isin(top_names)].reset_index(drop=True)
124126
plot_mat["Marker"] = pd.Categorical(plot_mat["Marker"], categories=top_names[::-1], ordered=True)
125-
plotnine.options.figure_size = (10, 10)
127+
plotnine.options.figure_size = (5, 4)
126128
plot = (
127129
ggplot(plot_mat, aes(cluster_group, "Marker"))
128130
+ geom_tile(mapping = aes(fill = "Intensity"))
129131
+ scale_fill_distiller(type = 'div', palette = 'RdYlBu')
130-
+ theme(axis_text_x=element_text(rotation=0, hjust=0.5, size=28))
131-
+ theme(axis_text_y=element_text(rotation=0, hjust=1, size=16))
132+
+ theme(axis_text_x=element_text(rotation=0, hjust=0.5, size=8))
133+
+ theme(axis_text_y=element_text(rotation=0, hjust=1, size=8))
132134
+ theme(axis_title_x = element_blank(), axis_title_y = element_text(angle=90))
133-
+ theme(text=element_text(size=16))
135+
+ theme(text=element_text(size=8))
134136
)
135137

136-
st.pyplot(ggplot.draw(plot), use_container_width=True)
138+
st.pyplot(ggplot.draw(plot), use_container_width=False)
137139

138140
elif method == "UMAP":
139141
obs_df = adata_norm[:, top_names].to_df().reset_index(drop=True)
@@ -151,9 +153,10 @@ def phenocluster__plot_diff_intensity_2(adata, groups, method, n_genes, plot_col
151153
axs = axs.flatten()
152154
for ax, col in zip(axs, columns):
153155
plot = sns.scatterplot(data=obs_df, x='UMAP_1', y='UMAP_2', hue=col,
154-
palette='viridis', ax=ax, s=8)
156+
palette='viridis', ax=ax, s=3)
155157
ax.set_title(col)
156158
plot.legend(loc='upper left', bbox_to_anchor=(1, 1))
159+
ax.set_aspect('equal', 'box')
157160
# Remove any unused subplots
158161
for ax in axs[len(columns):]:
159162
ax.remove()
@@ -264,7 +267,10 @@ def phenocluster__plotly_umaps_b(adata, umap_cur_col, umap_cur_groups, umap_colo
264267
y=-0.2,
265268
xanchor="right",
266269
x=1
267-
)
270+
),
271+
xaxis=dict(
272+
scaleanchor="y",
273+
scaleratio=1)
268274
)
269275
if i % 2 == 0:
270276
subcol1.plotly_chart(fig, use_container_width=True)
@@ -305,7 +311,10 @@ def spatial_plots_cust_2b(adata, umap_cur_col, umap_cur_groups, umap_color_col,
305311
y=-0.2,
306312
xanchor="right",
307313
x=1
308-
)
314+
),
315+
xaxis=dict(
316+
scaleanchor="y",
317+
scaleratio=1)
309318
)
310319
if i % 2 == 0:
311320
subcol3.plotly_chart(fig, use_container_width=True)
@@ -368,6 +377,9 @@ def main():
368377
st.number_input(label = "Number of markers to plot",
369378
key = 'phenocluster__plot_diff_intensity_n_genes',
370379
step = 1)
380+
st.toggle("Normalize total intensity", key='phenocluster__plot_normalize_total_intensity')
381+
st.toggle("Log normalize", key='phenocluster__plot_log_normalize')
382+
st.toggle("Z-score normalize columns", key='phenocluster__plot_zscore_normalize')
371383

372384
# phenocluster__plot_diff_intensity(st.session_state['phenocluster__clustering_adata'],
373385
# st.session_state['phenocluster__de_sel_groups'],
@@ -378,7 +390,11 @@ def main():
378390
st.session_state['phenocluster__de_sel_groups'],
379391
st.session_state['phenocluster__plot_diff_intensity_method'],
380392
st.session_state['phenocluster__plot_diff_intensity_n_genes'],
381-
phenocluster__col4b
393+
phenocluster__col4b,
394+
st.session_state['phenocluster__plot_normalize_total_intensity'],
395+
st.session_state['phenocluster__plot_log_normalize'],
396+
st.session_state['phenocluster__plot_zscore_normalize'],
397+
st.session_state['phenocluster__de_col']
382398
])
383399

384400
# make plots for differential intensity markers

0 commit comments

Comments
 (0)