1.灵感来源
灵感来源于Single-cell atlas of healthy human blood unveils age-related loss of NKG2C+GZMB−CD8+ memory T cells and accumulation of type 2 memory T cells的图2.C。原文用密度图展示CD4+ T cells在不同年龄组的分布情况。
(C) UMAP density plots characterizing the distribution of CD4+ T cells across age groups A–E.
2.思路
目前的任务是证明“证明细胞类型A发生了衰老”,此前已经尝试多种方法效果不佳,包括按每细胞类型每年龄段进行基因集打分/GSEA富集等。考虑到可能存在“单个细胞类型的单个年龄段并非所有细胞都发生了衰老(或者说衰老程度不同)”的情况,决定通过卡值的方法,超过阈值的细胞定义为“衰老亚型(发生了衰老/衰老程度更高)”,并使用密度图展示这部分细胞。
密度图本身的作用是“展示密度”,即基于平面上点坐标分布越密集,颜色越深。那么应该如何在展示所有细胞的基础上,只展示部分细胞的分布密度?
我的思路是1.绘制一个所有细胞的散点图(m);2.绘制只有衰老亚型的散点图并叠加密度图(n1/n2/n3);3.在AI中把图n叠加到图m上方。
3.代码实现
3.1 python
import scanpy as sc
#基因集打分
aging_gene_list_f = [x for x in list(aging_gene_list) if x in adata.var_names]
sc.tl.score_genes(adata,aging_gene_list_f)
#定义衰老亚型
adata.obs.loc[adata.obs['score']>=0.6,['aging_subtype']]='aged'
adata.obs.loc[adata.obs['score']<0.6,['aging_subtype']]='unaged'
import collections
collections.Counter(adata.obs['aging_subtype'])
#导出UMAP坐标值以及衰老亚型信息
df_den = pd.DataFrame(adata.obsm['X_umap'])
df_den['aging_subtype'] = list(pd.DataFrame(adata.obs['aging_subtype'])['aging_subtype'])
df_den = df_den.rename(columns={0:'X_umap_1', 1: 'X_umap_2','aging_subtype':'aging_subtype'})
df_den
df_den.to_csv('/df_den_re.tsv',sep='\t')
df_den格式如下:
3.2 R
library(cetcolor)
library(Seurat)
library(ggplot2)
df_den <- read.table('/df_den_re.tsv',sep="\t",header=T,row.names=1)
df_den$X_umap_1
###0 all of cells
m <- ggplot(df_den, aes(x = df_den$X_umap_1, y = df_den$X_umap_2)) +
geom_point()
# m + geom_density_2d_filled(alpha = 0.5) #查看所有细胞分布密度,无意义
###1 aging subtype with categorical legend and contour line
df_den_filtered <- df_den %>% filter(aging_subtype == 'aged')
n1 <- ggplot(df_den_filtered, aes(x = df_den_filtered$X_umap_1, y = df_den_filtered$X_umap_2)) +
geom_point() + geom_density_2d_filled(alpha = 0.5 ) + geom_density_2d(linewidth = 0.25, colour = "black")
###2 aging subtype with categorical legend
df_den_filtered <- df_den %>% filter(aging_subtype == 'aged')
n2 <- ggplot(df_den_filtered, aes(x = df_den_filtered$X_umap_1, y = df_den_filtered$X_umap_2)) +
geom_point() + geom_density_2d_filled(alpha = 0.5 )
###3 aging subtype with continuous legend
scale.col <- cet_pal(16, name = "fire")
n3 <- ggplot(df_den_filtered, aes(x = df_den_filtered$X_umap_1, y = df_den_filtered$X_umap_2)) +
geom_point() + stat_density_2d(aes_string(x = df_den_filtered$X_umap_1, y =df_den_filtered$X_umap_2, fill = "after_stat(level)"),
linewidth = 0.2, geom = "density_2d_filled",
colour = "ivory", alpha = 0.4, n = 150, h = c(1.2, 1.2)) &
scale_fill_gradientn(colours = scale.col)
m + n1 + n2 + n3
3.3 出图
搞掂。