Skip to content

Instantly share code, notes, and snippets.

@wflynny
Last active October 13, 2023 17:42
Show Gist options
  • Save wflynny/79c5266cc39a4a884958d696f84f85df to your computer and use it in GitHub Desktop.
Save wflynny/79c5266cc39a4a884958d696f84f85df to your computer and use it in GitHub Desktop.
Stacked barplot of scRNA-seq cluster proportions per sample
import scanpy.api as sc
import matplotlib.pyplot as plt
import seaborn as sns
def get_cluster_proportions(adata,
cluster_key="cluster_final",
sample_key="replicate",
drop_values=None):
"""
Input
=====
adata : AnnData object
cluster_key : key of `adata.obs` storing cluster info
sample_key : key of `adata.obs` storing sample/replicate info
drop_values : list/iterable of possible values of `sample_key` that you don't want
Returns
=======
pd.DataFrame with samples as the index and clusters as the columns and 0-100 floats
as values
"""
adata_tmp = adata.copy()
sizes = adata_tmp.obs.groupby([cluster_key, sample_key]).size()
props = sizes.groupby(level=1).apply(lambda x: 100 * x / x.sum()).reset_index()
props = props.pivot(columns=sample_key, index=cluster_key).T
props.index = props.index.droplevel(0)
props.fillna(0, inplace=True)
if drop_values is not None:
for drop_value in drop_values:
props.drop(drop_value, axis=0, inplace=True)
return props
def plot_cluster_proportions(cluster_props,
cluster_palette=None,
xlabel_rotation=0):
fig, ax = plt.subplots(dpi=300)
fig.patch.set_facecolor("white")
cmap = None
if cluster_palette is not None:
cmap = sns.palettes.blend_palette(
cluster_palette,
n_colors=len(cluster_palette),
as_cmap=True)
cluster_props.plot(
kind="bar",
stacked=True,
ax=ax,
legend=None,
colormap=cmap
)
ax.legend(bbox_to_anchor=(1.01, 1), frameon=False, title="Cluster")
sns.despine(fig, ax)
ax.tick_params(axis="x", rotation=xlabel_rotation)
ax.set_xlabel(cluster_props.index.name.capitalize())
ax.set_ylabel("Proportion")
fig.tight_layout()
return fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment