Closed ixxmu closed 8 months ago
关于cellchat通讯的文章已经有很多了。但是没有看到完整版的代码分享。今天分享一下完整代码,并且提醒大家此代码对数据input的要求。
1 首先加载必要r包
library(CellChat)
library(patchwork)
library(ggplot2)
library(ggalluvial)
library(svglite)
library(Seurat)
library(openxlsx)
2 读取rds文件
load('subset_data.rds')
3.我们来看看rds文件的内容,这一步非常重要,是成功运行代码的关键
rds对象里一定要有stim列,表示组别
cell.type 列,表示细胞类型
> head(subset_data@meta.data)
orig.ident nCount_RNA nFeature_RNA percent.mt stim RNA_snn_res.0.6 seurat_clusters RNA_snn_res.1 cluster.sort cell.type legend_labels groups
AAACCTGAGCTCCTCT.1 silicosis 3630 1428 1.460055 SiO2_7 2 2 2 1 Macrophage-1 1 Macrophage-1 <NA>
AAACCTGCACAACTGT.1 silicosis 2559 945 2.032044 SiO2_7 2 2 2 1 Macrophage-1 1 Macrophage-1 <NA>
AAACCTGCACGTAAGG.1 silicosis 4055 1538 1.602959 SiO2_7 2 2 2 1 Macrophage-1 1 Macrophage-1 <NA>
AAACCTGCATGCGCAC.1 silicosis 917 556 1.308615 SiO2_7 2 2 2 1 Macrophage-1 1 Macrophage-1 <NA>
AAACCTGGTACGCTGC.1 silicosis 2398 1192 1.542952 SiO2_7 2 2 2 1 Macrophage-1 1 Macrophage-1 <NA>
AAACCTGGTAGGCTGA.1 silicosis 1993 1066 4.264927 SiO2_7 1 1 1 1 Macrophage-1 1 Macrophage-1 <NA>
组别和细胞类型:
> table(subset_data$stim)
NS_56 NS_7 SiO2_56 SiO2_7
3123 2842 4524 1790
> table(subset_data$cell.type)
Endothelial cell-1 Endothelial cell-2 Fibroblast Macrophage-1 Macrophage-2 nc rTEM tr unknown
456 287 1185 4588 377
4 接下来直接运行下面的代码即可得到 不同组别内 所有细胞互作的结果
for(stim in c('NS_56' , 'NS_7' ,'SiO2_56', 'SiO2_7' )){
#stim="NS_56"
#stim="SiO2_7","NS_7"
path="/home/data/t040413/mmm_/20231009_cellchat_mmm_silicosis/cellchat"
dir.create(paste(path, "cellchat", stim, sep = "/"),recursive = TRUE)
setwd( paste(path, "cellchat", stim, sep = "/") )
getwd()
data.input = subset_data$RNA@data[, subset_data$stim==stim]
head(data.input)
meta = data.frame(labels = Idents(subset_data)[subset_data$stim==stim], row.names = colnames(subset_data)[subset_data$stim==stim])
head(meta)
cellchat <- createCellChat(object = data.input, meta = meta, group.by = "labels")
CellChatDB <- CellChatDB.mouse
cellchat@DB <- CellChatDB
cellchat <- subsetData(cellchat)
#future::plan("multiprocess", workers = 4)
cellchat <- identifyOverExpressedGenes(cellchat)
cellchat <- identifyOverExpressedInteractions(cellchat)
cellchat <- projectData(cellchat, PPI.mouse)
cellchat <- computeCommunProb(cellchat) #不可有有为0的细胞数的组
cellchat <- filterCommunication(cellchat, min.cells = 10) #设置最低细胞数
cellchat <- computeCommunProbPathway(cellchat)
cellchat <- aggregateNet(cellchat)
df.net <- subsetCommunication(cellchat)
write.xlsx(df.net,file='0.Cell-Cell_Communications_At_L-R.xlsx', rowNames=F, colNames=T)
df.net <- subsetCommunication(cellchat, slot.name = "netP")
write.xlsx(df.net,file='0.Cell-Cell_Communications_At_Pathway.xlsx', rowNames=F, colNames=T)
groupSize <- as.numeric(table(cellchat@idents))
## NumberOfInteractions
mat <- cellchat@net$count
write.xlsx(mat, file='1.NumberOfInteractions.xlsx', rowNames=T, colNames=T)
pdf("1.NumberOfInteractions.pdf")
netVisual_circle(mat, vertex.weight = groupSize, weight.scale = T, label.edge= F, title.name = "Number of interactions")
dev.off()
pdf("1.NumberOfInteractions_Split.pdf")
for (i in 1:nrow(mat)) {
mat2 <- matrix(0, nrow = nrow(mat), ncol = ncol(mat), dimnames = dimnames(mat))
mat2[i, ] <- mat[i, ]
p = netVisual_circle(mat2, vertex.weight = groupSize, weight.scale = T, edge.weight.max = max(mat), title.name = rownames(mat)[i])
print(p)
}
dev.off()
## InteractionWeights
mat <- cellchat@net$weight
write.xlsx(mat, file='2.InteractionWeights.xlsx', rowNames=T, colNames=T)
pdf("2.InteractionWeights.pdf")
netVisual_circle(mat, vertex.weight = groupSize, weight.scale = T, label.edge= F, title.name = "Interaction weights/strength")
dev.off()
pdf("2.InteractionWeights_Split.pdf")
for (i in 1:nrow(mat)) {
mat2 <- matrix(0, nrow = nrow(mat), ncol = ncol(mat), dimnames = dimnames(mat))
mat2[i, ] <- mat[i, ]
p = netVisual_circle(mat2, vertex.weight = groupSize, weight.scale = T, edge.weight.max = max(mat), title.name = rownames(mat)[i])
print(p)
}
dev.off()
## cellchat@netP$pathways: the signaling pathways showing significant communications
pathways = cellchat@netP$pathways
## the left portion shows autocrine and paracrine signaling to certain cell groups of interest (i.e, the defined vertex.receiver)
## the right portion shows autocrine and paracrine signaling to the remaining cell groups in the dataset
vertex.receiver = seq(1,5)
pdf("3.Sig_Pathway_Hierarchy_Plot.pdf")
for(i in pathways){
print(i)
p = netVisual_aggregate(cellchat, signaling = i,vertex.receiver = vertex.receiver, vertex.label.cex = 0.4)
print(p)
}
dev.off()
pdf("3.Sig_Pathway_Circle_Plot.pdf")
for(i in pathways){
print(i)
p = netVisual_aggregate(cellchat, signaling = i,vertex.receiver = vertex.receiver, layout = "circle")
print(p)
}
dev.off()
pdf("4.Sig_Pathway_L-R_pair_Contribution.pdf")
for(i in pathways){
print(i)
p = netAnalysis_contribution(cellchat, signaling = i, title = paste0(i, " signaling pathway", " Contribution of each L-R pair"))
print(p)
}
dev.off()
pdf("4.Sig_Pathway_L-R_pair_bubbleplot.pdf", width=25, height=20)
p = netVisual_bubble(cellchat, remove.isolate = FALSE)
print(p)
dev.off()
cellchat <- netAnalysis_computeCentrality(cellchat, slot.name = "netP")
pdf("5.Signaling_Roles_Of_Cell_Groups_Heatmap.pdf")
for(i in pathways){
print(i)
p = netAnalysis_signalingRole_network(cellchat, signaling = i, width = 8, height = 2.5, font.size = 10)
print(p)
}
dev.off()
pdf("4.Sig_Pathway_L-R_pair_bubbleplot.pdf", width=25, height=20)
p = netVisual_bubble(cellchat, remove.isolate = FALSE)
print(p)
dev.off()
pdf("5.Signaling_Roles_Of_Cell_Groups_2D.pdf")
p = netAnalysis_signalingRole_scatter(cellchat)
print(p)
dev.off()
pdf("5.signals_Contribution_Of_Cell_Groups_Heatmap.pdf", width=10)
ht1 <- netAnalysis_signalingRole_heatmap(cellchat, pattern = "outgoing", font.size = 5)
ht2 <- netAnalysis_signalingRole_heatmap(cellchat, pattern = "incoming", font.size = 5)
print(ht1 + ht2)
dev.off()
save(cellchat, file = paste0("cellchat_",stim,"_.RData"))
}
5 输出的文件如下:
$ tree
.
├── NS_56
│ ├── 0.Cell-Cell_Communications_At_L-R.xlsx
│ ├── 0.Cell-Cell_Communications_At_Pathway.xlsx
│ ├── 1.NumberOfInteractions.pdf
│ ├── 1.NumberOfInteractions_Split.pdf
│ ├── 1.NumberOfInteractions.xlsx
│ ├── 2.InteractionWeights.pdf
│ ├── 2.InteractionWeights_Split.pdf
│ ├── 2.InteractionWeights.xlsx
│ ├── 3.Sig_Pathway_Circle_Plot.pdf
│ ├── 3.Sig_Pathway_Hierarchy_Plot.pdf
│ ├── 4.Sig_Pathway_L-R_pair_bubbleplot.pdf
│ ├── 4.Sig_Pathway_L-R_pair_Contribution.pdf
│ ├── 5.Signaling_Roles_Of_Cell_Groups_2D.pdf
│ ├── 5.Signaling_Roles_Of_Cell_Groups_Heatmap.pdf
│ ├── 5.signals_Contribution_Of_Cell_Groups_Heatmap.pdf
│ └── cellchat_NS_56_.RData
├── NS_7
│ ├── 0.Cell-Cell_Communications_At_L-R.xlsx
│ ├── 0.Cell-Cell_Communications_At_Pathway.xlsx
│ ├── 1.NumberOfInteractions.pdf
│ ├── 1.NumberOfInteractions_Split.pdf
│ ├── 1.NumberOfInteractions.xlsx
│ ├── 2.InteractionWeights.pdf
│ ├── 2.InteractionWeights_Split.pdf
│ ├── 2.InteractionWeights.xlsx
│ ├── 3.Sig_Pathway_Circle_Plot.pdf
│ ├── 3.Sig_Pathway_Hierarchy_Plot.pdf
│ ├── 4.Sig_Pathway_L-R_pair_bubbleplot.pdf
│ ├── 4.Sig_Pathway_L-R_pair_Contribution.pdf
│ ├── 5.Signaling_Roles_Of_Cell_Groups_2D.pdf
│ ├── 5.Signaling_Roles_Of_Cell_Groups_Heatmap.pdf
│ ├── 5.signals_Contribution_Of_Cell_Groups_Heatmap.pdf
│ └── cellchat_NS_7_.RData
├── SiO2_56
│ ├── 0.Cell-Cell_Communications_At_L-R.xlsx
│ ├── 0.Cell-Cell_Communications_At_Pathway.xlsx
│ ├── 1.NumberOfInteractions.pdf
│ ├── 1.NumberOfInteractions_Split.pdf
│ ├── 1.NumberOfInteractions.xlsx
│ ├── 2.InteractionWeights.pdf
│ ├── 2.InteractionWeights_Split.pdf
│ ├── 2.InteractionWeights.xlsx
│ ├── 3.Sig_Pathway_Circle_Plot.pdf
│ ├── 3.Sig_Pathway_Hierarchy_Plot.pdf
│ ├── 4.Sig_Pathway_L-R_pair_bubbleplot.pdf
│ ├── 4.Sig_Pathway_L-R_pair_Contribution.pdf
│ ├── 5.Signaling_Roles_Of_Cell_Groups_2D.pdf
│ ├── 5.Signaling_Roles_Of_Cell_Groups_Heatmap.pdf
│ ├── 5.signals_Contribution_Of_Cell_Groups_Heatmap.pdf
│ └── cellchat_SiO2_56_.RData
└── SiO2_7
├── 0.Cell-Cell_Communications_At_L-R.xlsx
├── 0.Cell-Cell_Communications_At_Pathway.xlsx
├── 1.NumberOfInteractions.pdf
├── 1.NumberOfInteractions_Split.pdf
├── 1.NumberOfInteractions.xlsx
├── 2.InteractionWeights.pdf
├── 2.InteractionWeights_Split.pdf
├── 2.InteractionWeights.xlsx
├── 3.Sig_Pathway_Circle_Plot.pdf
├── 3.Sig_Pathway_Hierarchy_Plot.pdf
├── 4.Sig_Pathway_L-R_pair_bubbleplot.pdf
├── 4.Sig_Pathway_L-R_pair_Contribution.pdf
├── 5.Signaling_Roles_Of_Cell_Groups_2D.pdf
├── 5.Signaling_Roles_Of_Cell_Groups_Heatmap.pdf
├── 5.signals_Contribution_Of_Cell_Groups_Heatmap.pdf
└── cellchat_SiO2_7_.RData
4 directories, 64 files
图形设计隐私,这里仅放出部分
output节选:
https://mp.weixin.qq.com/s/NBkQpp7XFY_5jJW0HIOVKQ