利用矩阵文件进行细胞的聚类
细胞聚类的结果综合考虑了"hclust"的结果、"kmeans"聚类的结果、以及目前的研究现状
cellcluster <- as.data.frame(t(input_data))
#cellcluster[1:5,1:5]
hc <- hclust(dist((cellcluster)))
hcd <- as.dendrogram(hc)
(clus4 <- cutree(hc, 4)) #分4类
A <- as.character(rownames(as.data.frame(subset(clus4,clus4==1))))
B <- as.character(rownames(as.data.frame(subset(clus4,clus4==2))))
C <- as.character(rownames(as.data.frame(subset(clus4,clus4==3))))
D <- as.character(rownames(as.data.frame(subset(clus4,clus4==4))))
cls <- list(A,B,C,D)
nodes <- as.data.frame(unlist(cls))
nodes$type <- c(rep("B",9),rep("A",4),rep("C",5),rep("D",5))
names(nodes) <- c("media","type.label")
#以hclust的结果为基础,调整部分细胞所属的cluster
nodes$type.label[nodes$media=="T cells follicular helper"] <- "B"
nodes$type.label[nodes$media=="B cells naive"] <- "A"
nodes$type.label[nodes$media=="T cells CD4 naive"] <- "A"
nodes$type.label[nodes$media=="Plasma cells"] <- "A"
nodes$type.label[nodes$media=="Dendritic cells resting"] <- "C"
nodes$type.label[nodes$media=="Eosinophils"] <- "C"
nodes$type.label[nodes$media=="Mast cells resting"] <- "A"
nodes <- as.data.frame(nodes)
nodes$media <- as.character(nodes$media)
nodes
构建网络的input文件
# 合并生存分析的数据和细胞分类的数据
summary(nodes$media %in% bb$ID) #检查细胞名是否一致
nodes <- merge(nodes, bb, by.x = "media", "ID", all.x = T, all.y = T) #按细胞名merge
nodes$Fraction <- abs(nodes$weight_HR)
nodes$id <- paste("S", 01:23, sep = "")
nodes <- nodes[order(nodes$type.label),]
nodes <- nodes[,c(ncol(nodes),1:ncol(nodes)-1)]
nodes <- nodes[order(nodes$type.label),]
nodes
#建立nodes和links的连接id,把细胞名换成ID
paste0("'",nodes$media,"'","=","'",nodes$id,"'",collapse = ",")
corpvlue$from <- revalue(corpvlue$from,c('B cells memory'='S1','B cells naive'='S2','Dendritic cells activated'='S3',
'Dendritic cells resting'='S4','Eosinophils'='S5','Fibroblasts'='S6','Macrophages M0'='S7',
'Macrophages M1'='S8','Macrophages M2'='S9','Mast cells activated'='S10',
'Mast cells resting'='S11','Monocytes'='S12','Neutrophils'='S13',
'NK cells activated'='S14','NK cells resting'='S15','Plasma cells'='S16',
'T cells CD4 memory activated'='S17','T cells CD4 memory resting'='S18',
'T cells CD4 naive'='S19','T cells CD8'='S20','T cells follicular helper'='S21',
'T cells gamma delta'='S22','T cells regulatory Tregs'='S23'))
corpvlue$to <- revalue(corpvlue$to,c('B cells memory'='S1','B cells naive'='S2','Dendritic cells activated'='S3',
'Dendritic cells resting'='S4','Eosinophils'='S5','Fibroblasts'='S6','Macrophages M0'='S7',
'Macrophages M1'='S8','Macrophages M2'='S9','Mast cells activated'='S10',
'Mast cells resting'='S11','Monocytes'='S12','Neutrophils'='S13',
'NK cells activated'='S14','NK cells resting'='S15','Plasma cells'='S16',
'T cells CD4 memory activated'='S17','T cells CD4 memory resting'='S18',
'T cells CD4 naive'='S19','T cells CD8'='S20','T cells follicular helper'='S21',
'T cells gamma delta'='S22','T cells regulatory Tregs'='S23'))
(links <- corpvlue)
#利用nodes和links构建网络的input文件
net <- graph_from_data_frame(d=links, vertices=nodes, directed=T)
开始画图
# Generate colors based on cell clusters:
V(net)$color <- revalue(nodes$type.label,c("A"=mycol[1],"B"=mycol[2],"C"=mycol[3],"D"=mycol[4]))
# Compute node degrees (#links) and use that to set node size:
# Set edge width based on weight-log10(p_value):
V(net)$size <- (1 + V(net)$weight)*3 #节点圆的大小,可根据自己的数据再做调整
V(net)$label <- V(net)$media #设置标签
E(net)$arrow.mode <- 0 #不需要箭头
E(net)$edge.color <- "tomato" # tomato gray80
E(net)$width <- 1+E(net)$weight/6 #连接之间权重
pdf("Immune_network.pdf", width = 9.75, height = 8.78 )
plot(net,
layout=layout_in_circle, #按圆圈布局
edge.curved=.2, #画弯曲的连线
vertex.label.color=V(net)$color, #细胞名的颜色
vertex.label.dist= -2, #标签和节点的位置错开,后期还是要用AI调整
edge.color=links$color)
#cluster的图例
legend("topright", #图例的位置
c("Cell cluster-A", "Cell cluster-B", "Cell cluster-C", "Cell cluster-D"),
pch=21, col="black", pt.bg=mycol, pt.cex=3,
cex=1.3, bty="n", ncol=1)
#节点圆大小的图例,参考了FigureYa75base_volcano
f <- c(0.05, 0.001, 0.00001, 0.00000001)
s <- sqrt(abs(log10(f)))*3
legend("bottomright",
inset=c(0,-.1), #向下移
legend=f, text.width = .2,
title = "logrank test, P value", title.adj = -.5,
pch=21, pt.cex=s, bty='n',
horiz = TRUE, #横向排列
col = "black")
#连线的图例
legend("bottomright",
c("Positive correlation with P < 0.0001",
"Negative correlation with P < 0.0001"),
col = c(poscol, negcol), bty="n",
cex = 1, lty = 1, lwd = 5)
dev.off()

文字的位置用Illustrator调整;再根据“output_HR_corlor.csv”的最后一列用Illustrator在圆心上画点,点的颜色代表HR的正负。