【改装函数】弦图-可视化cellphonedb细胞互作结果

学术   2024-11-07 09:01   重庆  

偷偷问一下,关注了吗

内容获取


1、购买打包合集(《KS科研分享与服务》付费内容打包集合),价格感人,可以加入微信VIP群(答疑交流群,甚至有小伙伴觉得群比代码更好),可以获取建号以来所有内容,群成员专享视频教程,提前更新,其他更多福利!


2、《KS科研分享与服务》公众号有QQ群,进入门槛是20元(完全是为了防止白嫖党,请理解),请考虑清楚。群里有免费推文的注释代码和示例数据(终身拥有),没有付费内容,群成员福利是购买单个付费内容半价!


需要者详情请联系作者(非需要者勿扰,处理太费时间):


接上一节(【视频教程】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.colks_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.colks_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)

觉得我们分享有些用的,点个赞再走呗!

关注我们获取精彩内容:


关注不迷路:扫描下面二维码关注公众号!
B站视频号链接https://space.bilibili.com/471040659?spm_id_from=333.1007.0.0




关注 KS科研分享与服务,

认清正版优质内容和服务!

优质内容持续输出,物超所值!

合作联系:ks_account@163.com

新的板块-重要通知-双向选择

KS科研分享与服务
科研学习交流于分享,生信学习笔记,科研经历和生活!
 最新文章