aertslab / pySCENIC

pySCENIC is a lightning-fast python implementation of the SCENIC pipeline (Single-Cell rEgulatory Network Inference and Clustering) which enables biologists to infer transcription factors, gene regulatory networks and cell types from single-cell RNA-seq data.
http://scenic.aertslab.org
GNU General Public License v3.0
440 stars 182 forks source link

Is it possible to sort auc_mtx heatmap by cell type colors? #354

Open hyjforesight opened 2 years ago

hyjforesight commented 2 years ago

Hello pyscenic, For the heatmap (coding below), is it possible to sort it by cell type colors? I mean the cells from the same cell types aggregate together to make the color bar in an ordered pattern. Thanks! Best, YJ

sns.set()
sns.set(font_scale=1.0)
sns.set_style("ticks", {"xtick.minor.size": 1, "ytick.minor.size": 0.1})
g = sns.clustermap(bin_mtx.T, 
               col_colors=auc_mtx.index.map(cell_id2cell_type_lut).map(cell_type_color_lut),
               cmap=bw_palette, figsize=(20,20))
g.ax_heatmap.set_xticklabels([])
g.ax_heatmap.set_xticks([])
g.ax_heatmap.set_xlabel('Cells')
g.ax_heatmap.set_ylabel('Regulons')
g.ax_col_colors.set_yticks([0.5])
g.ax_col_colors.set_yticklabels(['Cell Type'])
g.cax.set_visible(False)
g.fig.savefig(os.path.join(FIGURES_FOLDERNAME, 'clustermap - GSE115978.png'), format='png')

image Expecting to see that, after sorting heatmap by cell type colors, the color bar of heatmap becomes like below, same colors aggregating together image

hyjforesight commented 2 years ago

I think the distribution of the color bar is determined by the cell order in the auc_mtx and bin_mtx. So I sorted the auc_mtx and bin_mtx by the leiden cell clusters (adata.obs['leiden'], cells in the same leiden cluster have the same leiden color), but the heapmap color bar is still not color-aggregtated.

auc_mtx_unsorted = pd.read_csv(AUCELL_MTX_FNAME, index_col=0)
auc_mtx_unsorted['pca_leiden']=adata.obs['leiden']
auc_mtx_unsorted.sort_values(by='pca_leiden', inplace=True)
auc_mtx=auc_mtx_unsorted.drop('pca_leiden', axis=1)    # auc_mtx sorted by 'leiden'

binary_mtx, thresholds = binarize(auc_mtx)    # generate bin_mtx from the sorted auc_mtx
binary_mtx.to_csv(BINARY_MTX_FNAME)
thresholds.to_frame().rename(columns={0:'threshold'}).to_csv(THR_FNAME)
binary_mtx = pd.read_csv(BINARY_MTX_FNAME, index_col=0)
thresholds = pd.read_csv(THR_FNAME, index_col=0).threshold

sns.set()
sns.set(font_scale=1.0)
sns.set_style("ticks", {"xtick.minor.size": 1, "ytick.minor.size": 0.1})
g = sns.clustermap(binary_mtx.T,
               col_colors=auc_mtx.index.map(CellID2pca_leiden_lut).map(pca_leiden_color_lut), 
               cmap=bw_palette, figsize=(20,20))    # redo heatmap by the sorted auc_mtx and col_mtx
g.ax_heatmap.set_xticklabels([])
g.ax_heatmap.set_xticks([])
g.ax_heatmap.set_xlabel('Cells')
g.ax_heatmap.set_ylabel('Regulons')
g.ax_col_colors.set_yticks([0.5])
g.ax_col_colors.set_yticklabels(['Cell clusters'])
g.cax.set_visible(False)
g.fig.savefig(os.path.join(FIGURES_FOLDERNAME, 'clustermap - ACT_sub2.png'), format='png')

the heatmap is still not color-aggregrated. image

MubasherMohammed commented 2 years ago

@hyjforesight Hi! hope you have managed to sort the colours? I have the same issue where the cell type color not aggregated.