【改装函数】弦图-可视化cellphonedb细胞互作结果
2024-12-23 本文已影响0人
KS科研分享与服务
接上一节(【视频教程】Cellphonedb v5更新:python版Scanpy单细胞分析后续之cpdb细胞通讯)。我们通过对cpdb结果的整合得到cpdb_pbmc_summary,这里我们对结果进行可视化,主要是弦图的可视化,看了一下,之前好像对于cpdb没有做过太多的弦图。为了方便,我们借助了R包和一些文章,然后进行了修改,包装为可视化函数,方便使用。完整代码已发布微信VIP群,请自行下载!视频解说见B站:https://www.bilibili.com/video/BV1V4DVY8Ehq/?spm_id_from=333.999.0.0&vd_source=05b5479545ba945a8f5d7b2e7160ea34首先我们用整合结果做个常见的气泡图,就会很简单,可以筛选需要展示的互作,结果可以自行修饰。
setwd('D:\\KS项目\\公众号文章\\cpdb结果可视化之-气泡图-弦图')
df <- read.csv('./cpdb_pbmc_summary.txt', header = T, sep = '\t', row.names = 1)
# plot dotplot ------------------------------------------------------------
#提取需要的互作
# unique(df$celltype_pair)
df_sce <- df[df$celltype_pair %in% c("B|B","B|CD14 Monocytes","B|CD4 T","B|CD8 T","B|Dendritic",
"B|FCGR3A Monocytes","B|NK","B|FCGR3A Monocytes"),]
#筛选显著的互作
df_sce <- df_sce[df_sce$pval <=0.05,]
colnames(df)
# [1] "interacting_pair" "celltype_pair" "mean" "pval"
library(ggplot2)
#ggplot作图
ggplot(df_sce, aes(x=celltype_pair, y=interacting_pair,
size= -log10(pval+0.001), color=mean)) +
geom_point(stat = "identity")+
geom_point(aes(size= -log10(pval+0.001)), shape = 21, colour="black", stroke=1)+
theme_bw()+
scale_y_discrete(limits=rev)+
scale_fill_viridis_c(option = "magma", guide = "colourbar", aesthetics = "color", direction=-1)+
theme(axis.text.x = element_text(size = 10, angle=45, hjust=1, vjust=1, colour = 'black'),
axis.text.y = element_text(size = 10, colour = 'black'),
axis.title.x = element_text(face='bold', size=12),
axis.title.y = element_text(face='bold', size=12))+
xlab("Cell_pair") + ylab("ligand_receptor pair")
弦图1:这个灵感来自于(R语言弦图绘制-(单细胞互作弦图))。有一个包italk也是分析互作的,但是我觉得它的可视化还挺好,可以直接拿来用,之前也演示了cellchat的结果,这里我们将LRPlot函数进行修改,能够适用于于我们的分析可视化:展示不同细胞之间的互作,以及受配体对,如果需要展示特定的,可以自行筛选数据!
# plot circle -------------------------------------------------------------
library(tidyr)
library(dplyr)
library(iTALK)
library(circlize)
# circle_plot1 ------------------------------------------------------------
#挑选显著互作
df_sig <- df[df$pval <=0.05 & df$mean >= 2,]
df_sig <- df_sig %>% separate(celltype_pair, c("source", "target"), "\\|")
df_sig <- df_sig %>% separate(interacting_pair, into = c("ligand", "receptor"), sep = "_", extra = "merge")
#可以选择自己需要展示的受配体展示,挑选数据即可
#这里我直接按照mean排序,选择前40可视化
df_sig <- df_sig[order(-df_sig$mean),]
df_sig_plot <- df_sig[1:40,]
# colnames(df_sig)[3] <- "cell_from"
# colnames(df_sig)[4] <- "cell_to"
#
#
# df_sig <- df_sig %>%
# select(1:2, cell_from_mean_exprs = 3, 4:ncol(.)) %>%
# mutate(cell_from_mean_exprs = df_sig$mean)
#
#
# df_sig <- df_sig %>%
# select(1:2, cell_from_mean_exprs = 3, 4:ncol(.)) %>%
# mutate(cell_from_mean_exprs = df_sig$mean)
#设置颜色
gene_col<-structure(c(rep('#CC3333',length(df_sig_plot$ligand)),
rep("#006699",length(df_sig_plot$receptor))),
names=c(df_sig_plot$ligand,df_sig_plot$receptor))
cell_col <- structure(c("#d2981a", "#a53e1f", "#457277", "#8f657d", "#42819F", "#86AA7D", "#CBB396"),
names=unique(df_sig_plot$source))#设置细胞颜色
#弦图连线体现source cell,link_cols = T,不设置link.arr.col
ks_iTALK_LRPlot(df_sig_plot,#挑选前40作图
link_cols = T,
link.arr.lwd=df_sig_plot$mean,
link.arr.width=0.1,
link.arr.type = "triangle",
link.arr.length = 0.1,
# link.arr.col = alpha("#47B9B5",0.4),#连线颜色设置
print.cell = T,
track.height_1=uh(5, "mm"),
track.height_2 = uh(10, "mm"),
annotation.height_1=0.02,
annotation.height_2=0.02,
text.vjust = "0.1cm",
gene_col = gene_col,
cell_col = cell_col)
#弦图连线不体现source cell,link_cols = F,设置link.arr.col
ks_iTALK_LRPlot(df_sig_plot,#挑选前40作图
link_cols = F,
link.arr.lwd=df_sig_plot$mean,
link.arr.width=0.1,
link.arr.type = "triangle",
link.arr.length = 0.1,
link.arr.col = alpha("#47B9B5",0.4),#连线颜色设置
print.cell = T,
track.height_1=uh(5, "mm"),
track.height_2 = uh(10, "mm"),
annotation.height_1=0.02,
annotation.height_2=0.02,
text.vjust = "0.1cm",
gene_col = gene_col,
cell_col = cell_col)
弦图2:
这个是参考了一篇《Nature》文章的方式,具体文献参见:reference:A prenatal skin atlas reveals immune regulation of human skin morphogenesis,https://doi.org/10.1038/s41586-024-08002-x,文章提供了代码可以学习一下非常好。展示是将source cell/ligands和receiver cell/receptor结合展示,最好是展示特定的受配体和特定细胞,我们也将这个过程修改为函数。
# circle_plot2 ------------------------------------------------------------
#挑选自己感兴趣的细胞进行可视化,选择显著的互作
df_sig <- df[df$pval <=0.05,]
cell_inter = c('NK|CD14 Monocytes', 'NK|FCGR3A Monocytes')
LR = c('CD1D_LILRB2', 'CD44_TYROBP', 'CD48_CD244',"HLA-F_KIR3DL2")
ks_comm_chordDiagram(data = df_sig,
cell_inter = cell_inter,
LR=LR,
group=c("NK"='#0c0459',
"CD14 Monocytes"='#F09709',
"FCGR3A Monocytes"='#47B9B5'),
text_size=0.8)