Removal of clusters with few number of cells

Hi Everyone,

After Leiden clustering, I got in total 50 clusters. But few of the clusters have less than 10 cells.
I want to remove them from UMAP plot in order to have better visualizations. How should I remove
these clusters?

Thanks in advance.

Hello,

There’s a number of ways this could be accomplished. A more readable way would be to use the pandas dataframe within the anndata object to get cluster counts with value_counts function and then mask your threshold of 10.

cluster_counts = adata.obs['leiden'].value_counts()
adata[adata.obs['leiden'].isin( cluster_counts[cluster_counts>10].index)]

You can also use pandas filter and anndata obs subset selection.

adata._inplace_subset_obs( adata.obs.groupby( 'leiden').filter( lambda x : len(x)>10).index)

Hope that helps

Thanks Herrinca … It was really helpful :slight_smile: