cellchat (三) 和弦图

第一步 定义绘图函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
f_CC_cmp_nV_df <- function(cellchat){
g1 <- netVisual_diffInteraction(cellchat, weight.scale = T)
g2 <- netVisual_diffInteraction(cellchat, weight.scale = T, measure = "weight")
}

f_CC_cmp_nV_chord_gene <- function(object.list, cellchat, sources.use=NULL, targets.use=NULL, title.name=''){
if(is.null(targets.use)){
targets.use <- as.character(unique(unlist(cellchat@meta$ident)))
}
if(is.null(sources.use)){
sources.use <- as.character(unique(unlist(cellchat@meta$ident)))
}
gg1 <- netVisual_chord_gene(object.list[[1]], sources.use = sources.use, targets.use = targets.use, lab.cex = 0.5, title.name = paste0(title.name, names(object.list)[1]))
gg2 <- netVisual_chord_gene(object.list[[2]], sources.use = sources.use, targets.use = targets.use, lab.cex = 0.5, title.name = paste0(title.name, names(object.list)[2]))
}

f_CC_nV_split_s <- function(mat, sources.use){
mat2 <- matrix(0, nrow = nrow(mat), ncol = ncol(mat), dimnames = dimnames(mat))
idx <- order(mat[sources.use, ], decreasing = T)
mat2 <- mat2[idx, idx]
mat2[sources.use, colnames(mat2)] <- mat[sources.use, colnames(mat2)]
min_mat <- min(mat2)
if(min_mat<=0){
mat2 <- mat2 - min_mat + 1e-20
}
g1 <- netVisual_circle(mat2, sources.use = sources.use, vertex.weight = mat2[sources.use, ], weight.scale = T, title.name = sources.use)
}
f_CC_nV_split_t <- function(mat, targets.use){
mat2 <- matrix(0, nrow = nrow(mat), ncol = ncol(mat), dimnames = dimnames(mat))
idx <- order(mat[, targets.use], decreasing = T)
mat2 <- mat2[idx, idx]
mat2[rownames(mat2), targets.use] <- mat[rownames(mat2), targets.use]
min_mat <- min(mat2)
if(min_mat<=0){
mat2 <- mat2 - min_mat + 1e-20
}
g1 <- netVisual_circle(mat2, targets.use = targets.use, vertex.weight = mat2[, targets.use], weight.scale = T, title.name = targets.use)
}
f_CC_nV_o <- function(cellchat, measure='count', LC="HSPC", RC="CRPC"){
mat <- cellchat@net[[RC]][[measure]] - cellchat@net[[LC]][[measure]]
mat
}

f_CC_nV_df <- function(sources=T, ...){
mat <- f_CC_nV_o(...)
if(sources){
for(NM in rownames(mat)){
f_CC_nV_split_s(mat, sources.use = NM)
}
}else{
for(NM in colnames(mat)){
f_CC_nV_split_t(mat, targets.use = NM)
}
}
}

第二步 绘图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
pdf(file = 'nV_df.pdf', height = 32, width = 16)
par(mfrow = c(3,2), xpd=TRUE)
f_CC_cmp_nV_df(SS)
f_CC_cmp_nV_df(ER)
f_CC_cmp_nV_df(CC)
dev.off()

pdf(file = 'nV_chord_gene_fF_1.pdf', height = 48, width = 24)
par(mfrow = c(3,2), xpd=TRUE)
f_CC_cmp_nV_chord_gene(SS_l, SS, sources.use = c('Fibroblasts'), targets.use = c(1:6),title.name = 'SS:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(ER_l, ER, sources.use = c('Fibroblasts'), targets.use = c(1:6),title.name = 'ER:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(CC_l, CC, sources.use = c('Fibroblasts'), targets.use = c(1:6),title.name = 'CC:Signaling from Fibroblasts - ')
dev.off()

pdf(file = 'nV_chord_gene_fF_2.pdf', height = 48, width = 24)
par(mfrow = c(3,2), xpd=TRUE)
f_CC_cmp_nV_chord_gene(SS_l, SS, sources.use = c('Fibroblasts'), targets.use = c(7:12),title.name = 'SS:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(ER_l, ER, sources.use = c('Fibroblasts'), targets.use = c(7:12),title.name = 'ER:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(CC_l, CC, sources.use = c('Fibroblasts'), targets.use = c(7:12),title.name = 'CC:Signaling from Fibroblasts - ')
dev.off()

pdf(file = 'nV_chord_gene_fF_3.pdf', height = 48, width = 24)
par(mfrow = c(3,2), xpd=TRUE)
f_CC_cmp_nV_chord_gene(SS_l, SS, sources.use = c('Fibroblasts'), targets.use = c(13:18),title.name = 'SS:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(ER_l, ER, sources.use = c('Fibroblasts'), targets.use = c(13:18),title.name = 'ER:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(CC_l, CC, sources.use = c('Fibroblasts'), targets.use = c(13:18),title.name = 'CC:Signaling from Fibroblasts - ')
dev.off()

pdf(file = 'nV_chord_gene_fF_4.pdf', height = 48, width = 24)
par(mfrow = c(3,2), xpd=TRUE)
f_CC_cmp_nV_chord_gene(SS_l, SS, sources.use = c('Fibroblasts'), targets.use = c(19:24),title.name = 'SS:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(ER_l, ER, sources.use = c('Fibroblasts'), targets.use = c(19:24),title.name = 'ER:Signaling from Fibroblasts - ')
f_CC_cmp_nV_chord_gene(CC_l, CC, sources.use = c('Fibroblasts'), targets.use = c(19:24),title.name = 'CC:Signaling from Fibroblasts - ')
dev.off()

pdf(file = 'nV_df_SS_s_c.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = T, cellchat = SS)
dev.off()

pdf(file = 'nV_df_SS_s_w.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = T, cellchat = SS, measure = "weight")
dev.off()

pdf(file = 'nV_df_SS_t_c.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = F, cellchat = SS)
dev.off()

pdf(file = 'nV_df_SS_t_w.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = F, cellchat = SS, measure = "weight")
dev.off()

pdf(file = 'nV_df_ER_s_c.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = T, cellchat = ER)
dev.off()

pdf(file = 'nV_df_ER_s_w.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = T, cellchat = ER, measure = "weight")
dev.off()

pdf(file = 'nV_df_ER_t_c.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = F, cellchat = ER)
dev.off()

pdf(file = 'nV_df_ER_t_w.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = F, cellchat = ER, measure = "weight")
dev.off()

pdf(file = 'nV_df_CC_s_c.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = T, cellchat = CC)
dev.off()

pdf(file = 'nV_df_CC_s_w.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = T, cellchat = CC, measure = "weight")
dev.off()

pdf(file = 'nV_df_CC_t_c.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = F, cellchat = CC)
dev.off()

pdf(file = 'nV_df_CC_t_w.pdf', height = 36, width = 24)
par(mfrow = c(6,4), xpd=TRUE)
f_CC_nV_df(sources = F, cellchat = CC, measure = "weight")
dev.off()

cellchat (三) 和弦图
https://b.limour.top/1583.html
Author
Limour
Posted on
February 11, 2022
Licensed under