def figure_S2a(): neuronal_final = load_adata("neuronal") neuronal_cluster_props = get_cluster_proportions( neuronal_final, "cluster_revised", sample_key="sample_name" ) fig1 = plot_celltype_proportions(neuronal_cluster_props, neuronal_palette) save_figure(fig1, "figure_S02", "figS2a_neuronal_cluster_props")
def scatter_1by2( adata, genes, filename=None, figdir=None, selection=None, add_marker_edges=True, label_va="top", label_ha="left", markersize=3, ): gs = plt.GridSpec(1, 2, wspace=0, hspace=0) fig = plt.figure(figsize=(4, 4)) ax1 = fig.add_subplot(gs[0, 0]) ax2 = fig.add_subplot(gs[0, 1]) axs = (ax1, ax2) plot_data = adata plot_params = dict(use_raw=False, show=False, title="", size=markersize, color_map=red_colormap) if selection is not None: plot_data = adata[selection] plot_params["size"] = 12 x = dict(left=0.05, right=0.95)[label_ha] y = dict(bottom=0.05, top=0.95)[label_va] for ax, gene in zip(axs, genes): sc.pl.umap(plot_data, color=gene, ax=ax, **plot_params) ax.text(x, y, gene, ha=label_ha, va=label_va, size="small", transform=ax.transAxes) filtered_children = list( filter(lambda c: isinstance(c, matplotlib.axes.Axes), fig.get_children())) for k, child in enumerate(filtered_children[len(axs):]): fig.delaxes(child) for ax in axs: ax.set_xlabel("") ax.set_ylabel("") fix_aspect_scatter_with_legend(fig) if add_marker_edges: add_scatter_borders(ax) ax1.set_ylabel("UMAP2") ax1.set_xlabel("UMAP1") ax2.set_xlabel("UMAP1") fig.tight_layout() fig.subplots_adjust(wspace=0.0) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) if (filename is not None) and (figdir is not None): save_figure(fig, figdir, filename)
def figure_S2c(): nonneuronal_final = load_adata("nonneuronal") nonneuronal_cluster_props = get_cluster_proportions( nonneuronal_final, "cluster_final", sample_key="sample_name" ) fig1 = plot_celltype_proportions(nonneuronal_cluster_props, nonneuronal_palette) save_figure(fig1, "figure_S02", "figS2c_nonneuronal_cluster_props")
def figure_S8(): neuronal_final = load_adata("neuronal") genes = pd.Index([ "Rprm", "Pitx2", "Cdh11", "Cpne9", "Gpr83", "Sim1", "Ctxn3", "Lhx1", "Sim2", "Fam19a1", "Lhx5", "Nr4a2", "Nkx2-1", ]) for gene in genes: fig, ax = plt.subplots(figsize=(2, 2)) sc.pl.umap( neuronal_final, color=gene, size=3, cmap=red_colormap, title="", ax=ax, show=False, use_raw=False ) fig.delaxes(fig.get_children()[-1]) ax.text(0.025, 0.975, gene, ha="left", va="top", size="small", style="italic", transform=ax.transAxes) ax.set_xlabel("") ax.set_ylabel("") fix_aspect_scatter_with_legend(fig) save_figure(fig, "figure_S08", f"fig_S8_neuronal-UMAP_{gene}") del fig
def figure_2c(): neuronal_final = load_adata("neuronal") neuronal_marker_gene_list = load_markers("neuronal") neuronal_heatmap_data, neuronal_col_colors, neuronal_row_colors = get_heatmap_data( neuronal_final, neuronal_marker_gene_list, "cluster_revised", neuronal_palette ) cg = sns.clustermap( neuronal_heatmap_data, z_score=0, vmin=-3, vmax=3, cmap=heatmap_cmap, xticklabels=False, yticklabels=False, row_cluster=False, col_cluster=False, col_colors=neuronal_col_colors, row_colors=neuronal_row_colors, robust=True, figsize=(4, 4), cbar_kws=dict(use_gridspec=True), ) cg.ax_row_colors.set_ylabel("Genes", size="small") cg.ax_col_colors.set_title("Cells", size="small", zorder=100) cg.ax_col_colors.xaxis.tick_top() fix_heatmap_colorbar(cg, neuronal_final, neuronal_heatmap_data, heatmap_cmap) # cg.fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) fix_heatmap_annotations(cg) save_figure(cg.fig, "figure_02", "fig2_neuronal-heatmap", dpi=600, ext="png")
def figure_1e(): full_no_dub = load_adata("global_no_dub") full_markers = load_markers("global") full_heatmap_data, full_col_colors, full_row_colors = get_heatmap_data( full_no_dub, full_markers, "cluster_revised", main_palette ) cg = sns.clustermap( full_heatmap_data, z_score=0, vmin=-3, vmax=3, cmap=heatmap_cmap, xticklabels=False, yticklabels=False, row_cluster=False, col_cluster=False, col_colors=full_col_colors, row_colors=full_row_colors, robust=True, figsize=(4, 4), cbar_kws=dict(use_gridspec=True), ) cg.ax_row_colors.set_ylabel("Genes", size="small") cg.ax_col_colors.set_title("Cells", size="small", zorder=100) fix_heatmap_colorbar(cg, full_no_dub, full_heatmap_data, heatmap_cmap) cg.ax_col_colors.xaxis.tick_top() # cg.fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) fix_heatmap_annotations(cg) save_figure(cg.fig, "figure_01", "fig1_main-heatmap", dpi=600, ext="png")
def figure_S2d(): nonneuronal_final = load_adata("nonneuronal") nonneuronal_sample_props = get_sample_proportions( nonneuronal_final, "cluster_final", "sample_name" ) fig2 = plot_celltype_proportions( nonneuronal_sample_props, sns.mpl_palette("tab20", 4)[::-1] ) save_figure(fig2, "figure_S02", "figS2d_nonneuronal_sample_props")
def figure_1d(): full_no_dub = load_adata("global_no_dub") fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4)) axs = (ax1, ax2) params = dict( norm_hist=False, kde=False, hist_kws=dict(color=sns.xkcd_rgb["cerulean"], alpha=0.9), ) sns.distplot( full_no_dub.obs.total_counts, ax=ax1, bins=np.linspace(2000, 35000, 50), **params ) sns.distplot(full_no_dub.obs.n_genes_by_counts, ax=ax2, **params) median1 = full_no_dub.obs.total_counts.median().astype(int) median2 = full_no_dub.obs.n_genes_by_counts.median().astype(int) ymax = 0.85 ax1.axvline( median1, ymax=ymax, lw=1, ls="--", color="k", ) ax2.axvline(median2, ymax=ymax, lw=1, ls="--", color="k") ax1.text( median1, ymax * 1.02 * ax1.get_ylim()[1], str(median1), transform=ax1.transData, ha="center", size="small", ) ax2.text( median2, ymax * 1.02 * ax2.get_ylim()[1], str(median2), transform=ax2.transData, ha="center", size="small", ) ax1.set_xlim(0, 35000) ax2.set_xlim(0, 10000) ax1.set_xlabel("No. of transcripts") ax2.set_xlabel("No. of genes") ax1.set_ylabel("No. of cells") ax2.set_ylabel("No. of cells") for ax in axs: sns.despine(fig, ax) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_01", "fig1_qc-histograms")
def genes_umi_violins(adata, cluster_key, palette, filename=None, figdir=None, ylims=[50000, 10000]): obs_to_plot = ["total_counts", "n_genes_by_counts"] obs_names = ["UIMs", "Genes"] yticks = [np.linspace(0, ylims[0], 6), np.linspace(0, ylims[1], 6)] L = len(obs_names) fig, axs = plt.subplots(2, 1, figsize=(4, 2)) violin_data = pd.DataFrame(adata.obs[obs_to_plot].values, columns=obs_names) violin_data["Cluster"] = adata.obs[cluster_key].values for k, (ax, obs, ylim, ytick) in enumerate(zip(axs.flat, obs_names, ylims, yticks)): ax.grid(axis="y", lw=0.25, color="0.6") sns.violinplot( data=violin_data, x="Cluster", y=obs, palette=palette, ax=ax, inner=None, linewidth=0.4, scale="width", cut=0, ) ax.set_ylabel(obs, rotation=90, ha="center", va="center", size="medium") ax.set_yticks(ytick) ax.set_ylim(0, ylim) ax.yaxis.set_tick_params(labelsize="small") if k < (L - 1): ax.set_xticks([]) ax.set_xlabel("") else: ax.xaxis.set_tick_params(labelsize="small") ax.set_xlabel("Cluster", size="medium") sns.despine(ax=ax, left=False) ax.set_axisbelow(True) fig.tight_layout() fig.subplots_adjust(hspace=0.25, wspace=0) if (filename is not None) and (figdir is not None): save_figure(fig, figdir, filename)
def marker_violins( adata, genes, cluster_key, palette, cluster_name="Cluster", filename=None, figdir=None, ): L = len(genes) N = len(adata.obs[cluster_key].unique()) fig, axs = plt.subplots(L, 1, figsize=(0.5 + 0.25 * N, 0.32 * L)) violin_data = pd.DataFrame(adata[:, genes].X.toarray(), columns=genes) violin_data[cluster_name] = adata.obs[cluster_key].values for k, (ax, gene) in enumerate(zip(axs.flat, genes)): sns.violinplot( data=violin_data, x=cluster_name, y=gene, palette=palette, ax=ax, inner=None, linewidth=0.4, scale="width", cut=0, ) ax.set_ylabel(gene, rotation=0, ha="right", va="top", size="small", style="italic") ax.set_yticks([]) ax.set_ylim(0, ax.get_ylim()[1]) if k < (L - 1): ax.set_xticks([]) ax.set_xlabel(None) ax.xaxis.label.set_visible(False) else: ax.xaxis.set_tick_params(labelsize="small") sns.despine(ax=ax, left=True) fig.tight_layout() fig.subplots_adjust(hspace=0, wspace=0) if (filename is not None) and (figdir is not None): save_figure(fig, figdir, filename)
def figure_S4a(): nonneuronal_final = load_adata("nonneuronal") fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sc.pl.umap( nonneuronal_final[nonneuronal_final.obs.cluster_final.isin(list("123456"))], color="cluster_final", show=False, ax=ax, title="", palette=nonneuronal_palette, ) ax.legend(bbox_to_anchor=(1.0, 0.5), loc="center left", ncol=1, frameon=False) fix_aspect_scatter_with_legend(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_S04", "figS4a_oligo-lineage")
def figure_S3a(): nonneuronal_final = load_adata("nonneuronal") fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sc.pl.umap( nonneuronal_final, color="cluster_final", show=False, ax=ax, title="Nonneuronal clusters", palette=nonneuronal_palette, ) ax.legend(bbox_to_anchor=(1.0, 0.5), loc="center left", ncol=1, frameon=False) fix_aspect_scatter_with_legend(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_S03", "fig3a_nonneuronal-clusters")
def figure_7a(): neuronal_final = load_adata("neuronal") fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sc.pl.umap( neuronal_final[neuronal_final.obs.cluster_revised.isin(list("12345"))], color="cluster_revised", show=False, ax=ax, title="Mammillary clusters", palette=neuronal_palette, ) ax.legend(bbox_to_anchor=(1.0, 0.5), loc="center left", ncol=1, frameon=False) fix_aspect_scatter_with_legend(fig) # add_scatter_borders(ax) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_07", "fig7_mammillary-umap-clusters")
def figure_S1c(): full_no_dub = load_adata("global_no_dub") fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sc.pl.umap( full_no_dub, color="classification_cluster", palette=neuronal_nonneuronal_palette, title="Neuronal Classification", show=False, ax=ax, ) ax.legend(bbox_to_anchor=(0.5, -0.2), ncol=2, frameon=False, loc="lower center") sns.despine(fig) fix_aspect_scatter_with_legend(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_S01", "figS1c_neuronal-classification")
def figure_2b(): neuronal_final = load_adata("neuronal") fig, ax = plt.subplots(1, 1, figsize=(8, 8)) sc.pl.umap( neuronal_final, color="cluster_revised", show=False, ax=ax, size=40, title="Neuronal clusters", palette=neuronal_palette, ) ax.legend(bbox_to_anchor=(1.0, 0.5), loc="center left", ncol=1, frameon=False) fix_aspect_scatter_with_legend(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_02", "fig2_neuronal-clusters")
def figure_1c(): full_no_dub = load_adata("global_no_dub") fig, axarr = plt.subplots( 1, 2, gridspec_kw=dict(hspace=0, wspace=0), sharex=True, sharey=True, figsize=(7, 4), ) params = dict(size=3, show=False, alpha=1.0, legend_fontsize=8) legend_params = dict( loc="upper center", bbox_to_anchor=(0.5, 0.0), ncol=2, frameon=False, fontsize="small", ) # by sex colors = sns.xkcd_palette(["electric blue", "bright red"]) sc.pl.umap( full_no_dub, color="sex", title="", ax=axarr[0], palette=colors, **params ) axarr[0].legend(**legend_params) # by 10x chemistry colors = sns.xkcd_palette(["orange", "grass green"]) sc.pl.umap( full_no_dub, color="chemistry", title="", ax=axarr[1], palette=colors, **params ) axarr[1].legend(**legend_params) for ax in axarr.flat: ax.set_xlabel("") ax.set_ylabel("") ax.set_aspect(1) fix_aspect_scatter_with_legend(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_01", "fig1_umap-sex-chemistry")
def figure_S6b(): supramammillary = load_adata("supramammillary") fig, ax = plt.subplots(1, 1, figsize=(2, 2)) sc.pl.umap( supramammillary, color="cluster_revised", show=False, ax=ax, title="", size=16 ) leg = ax.legend( bbox_to_anchor=(1.0, 0.5), loc="center left", ncol=1, frameon=False, fontsize="xx-small", ) for handle in leg.legendHandles: handle._sizes = [16] fix_aspect_scatter_with_legend(fig) # add_scatter_borders(ax) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_S06", "fig6b_cluster8_subclusters")
def figure_1f(): full_no_dub = load_adata("global_no_dub") fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sc.pl.umap( full_no_dub, color="cluster_revised", palette=main_palette, title="", show=False, ax=ax, ) legend = ax.legend( bbox_to_anchor=(1, 1), ncol=1, frameon=False, loc="upper left", fontsize=7.6 ) for leg in legend.legendHandles: leg._sizes = [20] sns.despine(fig) fix_aspect_scatter_with_legend(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_01", "fig1f_all-clusters")
def figure_S1b(): full_no_dub = load_adata("global_no_dub") neuronal_markers = ["Snap25", "Tubb3", "Elavl2", "Syp"] full_no_dub.obs["neuronal_average_exp"] = full_no_dub[:, neuronal_markers].X.mean( axis=1 ) fig, ax = plt.subplots(1, 1, figsize=(4, 4)) sc.pl.umap( full_no_dub, color="neuronal_average_exp", palette=neuronal_nonneuronal_palette, title="Neuronal marker mean expression", show=False, color_map=red_colormap, ax=ax, ) sns.despine(fig) fig.get_children()[-1].set_ylabel("Log-normalized expression") fix_aspect_scatter_with_cbar(fig) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_S01", "figS1b_neuronal-expression")
def figure_S1a(): full_no_dub = load_adata("global_with_dub") full_comb = full_no_dub.copy() sc.pp.normalize_per_cell(full_comb) sc.pp.log1p(full_comb) sc.pp.highly_variable_genes(full_comb, n_top_genes=2500, flavor="cell_ranger") sc.pp.pca(full_comb, n_comps=25, svd_solver="arpack", use_highly_variable=True) sc.pp.neighbors(full_comb, n_neighbors=15, metric="correlation") sc.tl.umap(full_comb, min_dist=0.5) full_comb.obs["chemistry"] = full_comb.obs.sampleid.map( {"AJ18003": "v2", "AJ18004": "v2", "AJ19001": "v3", "AJ19002": "v3",} ) full_comb.obsm["X_umap"] = full_comb.obsm["X_umap"].dot(-np.eye(2)) fig, axarr = plt.subplots(2, 3, figsize=(6, 4)) params = dict(show=False, size=2) sc.pl.umap( full_comb, color="chemistry", ax=axarr[0, 0], palette=sns.xkcd_palette(["orange", "grass green"]), legend_loc=None, title="10X Chemistry", **params, ) sc.pl.umap( full_no_dub, color="chemistry", ax=axarr[1, 0], palette=sns.xkcd_palette(["orange", "grass green"]), title="", **params, ) sc.pl.umap( full_comb, color="sex", ax=axarr[0, 1], palette=sns.xkcd_palette(["electric blue", "bright red"]), legend_loc=None, title="Sex", **params, ) sc.pl.umap( full_no_dub, color="sex", ax=axarr[1, 1], palette=sns.xkcd_palette(["electric blue", "bright red"]), title="", **params, ) sc.pl.umap( full_comb, color="sample_name", ax=axarr[0, 2], # palette=sns.xkcd_palette(["orange", "grass green"]), legend_loc=None, title="Sample", **params, ) sc.pl.umap( full_no_dub, color="sample_name", ax=axarr[1, 2], # palette=sns.xkcd_palette(["orange", "grass green"]), title="", **params, ) legend_params = dict( bbox_to_anchor=(0.5, -0.0), loc="upper center", ncol=2, frameon=False, columnspacing=0.9, fontsize=7, markerscale=0.5, ) for k in range(3): leg = axarr[1, k].legend(**legend_params) for ax in axarr.flat: ax.set_xlabel("") ax.set_ylabel("") axarr[0, 0].set_ylabel("No batch correction", size=8) axarr[1, 0].set_ylabel("With batch correction", size=8) axarr[0, 0].set_title("10X Chemistry", size=9) axarr[0, 1].set_title("Sex", size=9) axarr[0, 2].set_title("Sample", size=9) fix_aspect_scatter_with_legend(fig) fig.subplots_adjust(wspace=0.0, hspace=0.0) fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) save_figure(fig, "figure_S01", "figS1a_batch-correction-comparison")
def figure_7b(): def get_heatmap_data_tmp(adata, markers, cluster_key, palette): grpd = markers.groupby(cluster_key).head(100) genes = grpd.gene_name # .unique() duplicated = genes.duplicated() clusters = grpd[cluster_key].astype(int).values - 1 genes = genes[~duplicated] clusters = clusters[~duplicated] # heatmap_data = nonneuronal_final[nonneuronal_final.obs.sort_values("cluster_final").index, :] data = adata[:, genes].X.toarray() sort_order = adata.obs[cluster_key].reset_index(drop=True).sort_values().index data = data[sort_order, :].T col_colors = np.array(palette)[adata.obs[cluster_key].cat.codes[sort_order]] row_colors = np.array(palette)[clusters] weights = np.array( [ [0, 0, 1, 0, 0], [0, 2, 4, 2, 0], [1, 4, 8, 4, 1], [0, 2, 4, 2, 0], [0, 0, 1, 0, 0], ], dtype=np.float, ) weights = weights / np.sum(weights[:]) smoothed = convolve(data, weights, mode="constant") return smoothed, col_colors, row_colors neuronal_final = load_adata("neuronal") mammillary = neuronal_final[ neuronal_final.obs.cluster_revised.isin(list("12345")), : ].copy() mammillary_heatmap_genes = pd.Index( [ "Nts", "Alcam", "Col25a1", "Ctxn3", "Spock3", "Serpini1", "Tshz2", "Trp53i11", "Grin3a", "Hpcal1", "Zbtb20", "Onecut2", "Rbms3", "Slc24a2", "Pvalb", "Calb1", "Nos1", "Tac2", "Cadm1", "Cxcl14", ] ) mammillary_marker_genes = pd.DataFrame( { "gene_name": mammillary_heatmap_genes, "cluster_revised": ["1"] * 4 + ["2"] * 7 + ["3"] * 4 + ["4"] * 2 + ["5"] * 3, "garbage": np.arange(len(mammillary_heatmap_genes)), } ) ( mammillary_heatmap_data, mammillary_col_colors, mammillary_row_colors, ) = get_heatmap_data_tmp( mammillary, mammillary_marker_genes, "cluster_revised", neuronal_palette ) cg = sns.clustermap( mammillary_heatmap_data, z_score=0, vmin=-3, vmax=3, cmap=heatmap_cmap, xticklabels=False, yticklabels=mammillary_heatmap_genes, row_cluster=False, col_cluster=False, col_colors=mammillary_col_colors, row_colors=mammillary_row_colors, robust=True, figsize=(4, 4), cbar_kws=dict(use_gridspec=True), ) cg.ax_row_colors.set_ylabel("Genes", size="small") cg.ax_col_colors.set_title("Cells", size="small", zorder=100) cg.ax_heatmap.tick_params(axis="y", which="major", labelsize="xx-small") cg.ax_col_colors.xaxis.tick_top() fix_heatmap_colorbar(cg, mammillary, mammillary_heatmap_data, heatmap_cmap) cg.fig.subplots_adjust(right=0.78) # cg.fig.tight_layout(rect=(0.025, 0.025, 0.975, 0.975)) fix_heatmap_annotations(cg) save_figure(cg.fig, "figure_07", "fig7_mammillary-heatmap", dpi=600, ext="png")
def figure_2a(): neuronal_final = load_adata("neuronal") gs = plt.GridSpec(1, 5) fig = plt.figure(figsize=(18, 4)) ax1 = fig.add_subplot(gs[0, 0]) ax2 = fig.add_subplot(gs[0, 1]) ax3 = fig.add_subplot(gs[0, 2]) ax4 = fig.add_subplot(gs[0, 3]) ax6 = fig.add_subplot(gs[0, 4]) axs = (ax1, ax2, ax3, ax4, ax6) genes = ["Slc17a6", "Slc32a1", "Gad1", "Hdc"] for ax, gene in zip(axs, genes): sc.pl.umap( neuronal_final, color=gene, use_raw=False, show=False, ax=ax, title="", color_map=red_colormap, size=10, ) ax.text( 0.05, 0.95, gene, ha="left", va="top", size="large", transform=ax.transAxes ) sc.pl.umap( neuronal_final, color="classification", ax=ax6, show=False, title="", legend_loc="right margin", palette=gaba_vs_glut_vs_hdc_palette, size=10, ) ax6.legend( frameon=False, loc="upper right", labelspacing=0, borderaxespad=0.1, markerscale=0.7, ) filtered_children = list( filter(lambda c: isinstance(c, matplotlib.axes.Axes), fig.get_children()) ) for k, child in enumerate(filtered_children[len(axs) :]): fig.delaxes(child) for ax in axs: ax.set_xlabel("") ax.set_ylabel("") fix_aspect_scatter_with_legend(fig) ax1.set_xlabel("UMAP1") ax2.set_xlabel("UMAP1") ax3.set_xlabel("UMAP1") ax4.set_xlabel("UMAP1") ax6.set_xlabel("UMAP1") ax6.set_ylabel("UMAP2") ax6.yaxis.set_label_position("right") fig.subplots_adjust(wspace=0.0) save_figure(fig, "figure_02", "fig2_gaba-vs-glut")