-
Notifications
You must be signed in to change notification settings - Fork 18
/
CellPhoneDB_analysis_demo.R
444 lines (361 loc) · 27 KB
/
CellPhoneDB_analysis_demo.R
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
library(data.table)
library(Seurat)
library(dplyr)
library(ggplot2)
library(reshape2)
library(stringr)
library(plyr)
library(pheatmap)
cellphone_dat <- read.table("Receptor_and_ligation_significant.txt",
header = T,
row.names = NULL,
sep = "\t",
stringsAsFactors = F)
cellphone_dat$A_B <- cellphone_dat$interacting_pair
cellphone_dat <- cbind(cellphone_dat, str_split(cellphone_dat$interacting_pair, pattern = "_", simplify = T)[, 1:2] %>% as.data.frame %>% mutate(B_A = paste0(V2, "_", V1)))
number_fo_significant_RL <- table(cellphone_dat$interacting_pair) %>% sort(decreasing = T) %>% length() ##460
table(cellphone_dat$tissue) %>% sort(decreasing = T)
receptors_and_cell_types <- cellphone_dat[, 1:2]
receptors_and_cell_types$comb <- with(receptors_and_cell_types, paste(interacting_pair," ", variable))
unique_receptors_and_cell_types <- receptors_and_cell_types[!duplicated(receptors_and_cell_types$comb), ]
dat_for_plot <- table(unique_receptors_and_cell_types$interacting_pair) %>% unclass %>% as.data.frame()
dat_for_plot <- data.frame(name = row.names(dat_for_plot), frequence = dat_for_plot$.) %>% subset(frequence >= 50)
##-----------------frequence of each paired ligation and receptors across different tissues---------------------###
pdf("receptor_ligation_barplot.pdf", height = 10, width = 7)
ggplot(dat_for_plot, mapping = aes(x = reorder(name, frequence), y = frequence)) +
geom_bar(stat = "identity", fill = "#4DBBD5FF", width = 0.75) +
theme_classic() +
theme(axis.text.x = element_text(hjust = 0.5, vjust = 0.5, angle = 0, size = 8),
axis.text.y = element_text(hjust = 1, vjust = 0.5, size = 6)) +
scale_y_continuous(expand = c(0.01, 0.5)) +
coord_flip() +
labs(x = "ligation_receptors")
dev.off()
##---------------number of paired ligation and receptors in each tissue---------#####
pdf("receptor_ligation_barplot_in_each_tissue.pdf", height = 10, width = 15)
cellphone_dat %>% ggplot(., aes(x = tissue)) +
geom_bar(width = 0.7, fill = "#4DBBD5FF") +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust = 1)) +
scale_y_continuous(expand = c(0.002, 0), limits = c(0, 3000), breaks = seq(0, 3000, 500))
dev.off()
###-----------------------------------------prepare the files for cytoscape---------------####
celltype_celltypes_annotation <- cellphone_dat %>%
mutate(Type1 = part1 %>%
gsub(pattern = "Absorptive|Basal|Cholang|Enterocyte|Epi|Follicular|HPro|Granular|Goblet|Pit|Secretory|Spinous|Tuft|Squamous|Chief|Hepatic_Oval|Stem|Simple", replacement = "Epi"),
Type2 = part2 %>%
gsub(pattern = "Absorptive|Basal|Cholang|Enterocyte|Epi|Follicular|HPro|Granular|Goblet|Pit|Secretory|Spinous|Tuft|Squamous|Chief|Hepatic_Oval|Stem|Simple", replacement = "Epi"))
celltype_celltypes_annotation <- celltype_celltypes_annotation %>%
mutate(Type1 = Type1 %>%
gsub(pattern = "Epi.*", replacement = "Epi"),
Type2 = Type2 %>%
gsub(pattern = "Epi.*", replacement = "Epi"))
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*CD4.*", replacement = "CD4")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*CD4.*", replacement = "CD4")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*CD8.*", replacement = "CD8")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*CD8.*", replacement = "CD8")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = "B_Naive", replacement = "B")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = "B_Memory", replacement = "B")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = "B_Naive", replacement = "B")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = "B_Memory", replacement = "B")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = "TGD", replacement = "TGD")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = "TGD", replacement = "TGD")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*LEC$", replacement = "Endo")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*BEC$", replacement = "Endo")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*LEC$", replacement = "Endo")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*BEC$", replacement = "Endo")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*Mon$", replacement = "Myeloid")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*Mac$", replacement = "Myeloid")
celltype_celltypes_annotation$Type1 <- celltype_celltypes_annotation$Type1 %>% gsub(., pattern = ".*DC$", replacement = "Myeloid")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*Mon$", replacement = "Myeloid")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*Mac$", replacement = "Myeloid")
celltype_celltypes_annotation$Type2 <- celltype_celltypes_annotation$Type2 %>% gsub(., pattern = ".*DC$", replacement = "Myeloid")
celltype_celltypes_annotation <- data.frame(celltype_celltypes_annotation %>% dplyr::select(interacting_pair, A_B, Type1, Type2)) %>% mutate(Type = paste0(Type1, "_", Type2))
row.names(celltype_celltypes_annotation) <- 1:dim(celltype_celltypes_annotation)[1]
Tcells_CD4 <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "CD4", value = F), ]
Tcells_CD8 <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "CD8", value = F), ]
Bcells <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "B", value = F), ]
Plasma <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "Plasma", value = F), ]
Fib <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "^Fib_|_Fib$", value = F), ]
Smo <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "^Smo_|_Smo$", value = F), ]
FibSmo <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "FibSmo", value = F), ]
Endo <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "Endo", value = F), ]
Myeloid <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "Myeloid", value = F), ]
NK <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "NK", value = F), ]
Epi <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "Epi", value = F), ]
TGD <- celltype_celltypes_annotation[grep(celltype_celltypes_annotation$Type, pattern = "TGD", value = F), ]
for (cell in c("Tcells_CD4", "Tcells_CD8", "Bcells", "Plasma", "Fib", "Smo", "FibSmo", "Myeloid", "NK", "Epi", "Endo", "TGD")) {
Tcells_CD4_for_cytoscape <- get(cell) %>% dplyr::select(Type) %>% table() %>% unclass %>% as.data.frame()
Tcells_CD4_for_cytoscape <- Tcells_CD4_for_cytoscape %>% transmute(from = row.names(Tcells_CD4_for_cytoscape) %>% str_split(. ,pattern = "_", simplify = T) %>% `[`(, 1),
to = row.names(Tcells_CD4_for_cytoscape) %>% str_split(. ,pattern = "_", simplify = T)%>% `[`(, 2),
number = Tcells_CD4_for_cytoscape$.)
write.table(Tcells_CD4_for_cytoscape, paste0(cell, "_for_cytoscape.txt"), sep = "\t", quote = F, row.names = F)
}
##----------summarize the number of interactions for each cell type
tmp <- data.frame()
for (cell in c("Tcells_CD4", "Tcells_CD8", "Bcells", "Plasma", "Fib", "Smo", "FibSmo", "Myeloid", "NK", "Epi", "Endo")) {
Tcells_CD4_for_cytoscape <- get(cell) %>% dplyr::select(Type) %>% table() %>% unclass %>% as.data.frame()
Tcells_CD4_for_cytoscape <- Tcells_CD4_for_cytoscape %>% transmute(from = row.names(Tcells_CD4_for_cytoscape) %>% str_split(. ,pattern = "_", simplify = T) %>% `[`(, 1),
to = row.names(Tcells_CD4_for_cytoscape) %>% str_split(. ,pattern = "_", simplify = T)%>% `[`(, 2),
comb = paste0(from, "_", to),
number = Tcells_CD4_for_cytoscape$.)
cells <- table(Tcells_CD4_for_cytoscape$from) %>% sort(decreasing = T) %>% `[`(1) %>% names
row.names(Tcells_CD4_for_cytoscape) <- 1:dim(Tcells_CD4_for_cytoscape)[1]
exchanged_position <- with(Tcells_CD4_for_cytoscape, grep(from, pattern = paste0("^", cells, "$"), value = F, fixed = F))
from_1 <- Tcells_CD4_for_cytoscape[exchanged_position, "from"]
to_1 <- Tcells_CD4_for_cytoscape[exchanged_position, "to"]
## begin to exchange
Tcells_CD4_for_cytoscape[exchanged_position, "from"] <- to_1
Tcells_CD4_for_cytoscape[exchanged_position, "to"] <- from_1
result <- Tcells_CD4_for_cytoscape %>% dplyr::group_by(from, to) %>% dplyr::summarise(summary = sum(number), n = n()) %>% as.data.frame()
tmp <- rbind(tmp, result)
}
write.table(tmp, "summary_dat_for_cytoscape.txt", sep = "\t", quote = F, row.names = F)
###------------------modify Receptor_and_ligation_all.txt data-----##############
all_cellphone_dat <- all_cellphone_dat %>%
mutate(Type1 = part1 %>%
gsub(pattern = "Absorptive|Basal|Cholang|Enterocyte|Epi|Follicular|HPro|Granular|Goblet|Pit|Secretory|Spinous|Tuft|Squamous|Chief|Hepatic_Oval|Stem|Simple", replacement = "Epi"),
Type2 = part2 %>%
gsub(pattern = "Absorptive|Basal|Cholang|Enterocyte|Epi|Follicular|HPro|Granular|Goblet|Pit|Secretory|Spinous|Tuft|Squamous|Chief|Hepatic_Oval|Stem|Simple", replacement = "Epi"))
all_cellphone_dat <- all_cellphone_dat %>%
mutate(Type1 = Type1 %>%
gsub(pattern = "Epi.*", replacement = "Epi"),
Type2 = Type2 %>%
gsub(pattern = "Epi.*", replacement = "Epi"))
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "Mac")] <- "Myeloid"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "Mon")] <- "Myeloid"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "Plasma")] <- "Plasma"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "B_Memory")] <- "B"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "B_Naive")] <- "B"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "LEC")] <- "Endo"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "BEC")] <- "Endo"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "^Smo$")] <- "Smo"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "FibSmo")] <- "FibSmo"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "^Fib$")] <- "Fib"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "DC")] <- "Myeloid"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "CD4")] <- "CD4"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "CD8")] <- "CD8"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "NK")] <- "NK"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "Epi")] <- "Epi"
all_cellphone_dat$cell1[grepl(all_cellphone_dat$Type1, pattern = "TGD")] <- "TGD"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "Mac")] <- "Myeloid"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "Mon")] <- "Myeloid"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "Plasma")] <- "Plasma"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "B_Memory")] <- "B"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "B_Naive")] <- "B"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "LEC")] <- "Endo"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "BEC")] <- "Endo"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "^Smo$")] <- "Smo"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "FibSmo")] <- "FibSmo"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "^Fib$")] <- "Fib"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "DC")] <- "Myeloid"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "CD4")] <- "CD4"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "CD8")] <- "CD8"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "NK")] <- "NK"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "Epi")] <- "Epi"
all_cellphone_dat$cell2[grepl(all_cellphone_dat$Type2, pattern = "TGD")] <- "TGD"
all_cellphone_dat$Type <- paste0(all_cellphone_dat$cell1, "_", all_cellphone_dat$cell2)
write.table(all_cellphone_dat, "Receptor_and_ligation_all_modified.txt", sep = "\t", quote = F)
##########-------------------cell-cell interaction pheatmap----------------------------------#############
cellphone_dat <- read.table("Receptor_and_ligation_significant.txt",
header = T,
row.names = NULL,
sep = "\t",
stringsAsFactors = F)
# cellphone_dat$A_B <- cellphone_dat$interacting_pair
# cellphone_dat <- cbind(cellphone_dat, str_split(cellphone_dat$interacting_pair, pattern = "_", simplify = T)[, 1:2] %>% as.data.frame %>% mutate(B_A = paste0(V2, "_", V1)))
cellphone_dat <- cellphone_dat %>%
mutate(Type1 = part1 %>%
gsub(pattern = "Absorptive|Basal|Cholang|Enterocyte|Epi|Follicular|HPro|Granular|Goblet|Pit|Secretory|Spinous|Tuft|Squamous|Chief|Hepatic_Oval|Stem|Simple", replacement = "Epi"),
Type2 = part2 %>%
gsub(pattern = "Absorptive|Basal|Cholang|Enterocyte|Epi|Follicular|HPro|Granular|Goblet|Pit|Secretory|Spinous|Tuft|Squamous|Chief|Hepatic_Oval|Stem|Simple", replacement = "Epi"))
cellphone_dat <- cellphone_dat %>%
mutate(Type1 = Type1 %>%
gsub(pattern = "Epi.*", replacement = "Epi"),
Type2 = Type2 %>%
gsub(pattern = "Epi.*", replacement = "Epi"))
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "Mac")] <- "Myeloid"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "Mon")] <- "Myeloid"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "Plasma")] <- "Plasma"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "B_Memory")] <- "B"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "B_Naive")] <- "B"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "LEC")] <- "Endo"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "BEC")] <- "Endo"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "^Smo$")] <- "Smo"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "FibSmo")] <- "FibSmo"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "^Fib$")] <- "Fib"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "DC")] <- "Myeloid"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "CD4")] <- "CD4"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "CD8")] <- "CD8"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "NK")] <- "NK"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "Epi")] <- "Epi"
cellphone_dat$cell1[grepl(cellphone_dat$Type1, pattern = "TGD")] <- "TGD"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "Mac")] <- "Myeloid"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "Mon")] <- "Myeloid"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "Plasma")] <- "Plasma"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "B_Memory")] <- "B"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "B_Naive")] <- "B"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "LEC")] <- "Endo"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "BEC")] <- "Endo"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "^Smo$")] <- "Smo"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "FibSmo")] <- "FibSmo"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "^Fib$")] <- "Fib"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "DC")] <- "Myeloid"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "CD4")] <- "CD4"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "CD8")] <- "CD8"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "NK")] <- "NK"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "Epi")] <- "Epi"
cellphone_dat$cell2[grepl(cellphone_dat$Type2, pattern = "TGD")] <- "TGD"
all_cellphone_dat <- read.table("Receptor_and_ligation_all_modified.txt",
header = T,
row.names = 1,
sep = "\t",
stringsAsFactors = F)
all_cellphone_dat <- read.table("Receptor_and_ligation_all_modified.txt",
header = T,
row.names = 1,
sep = "\t",
stringsAsFactors = F)
###. find the significant paired RL-------------
T_epi <- data.frame(cellphone_dat %>% dplyr::select(interacting_pair, variable, cell1, cell2, pvalue, mean)) %>% mutate(Type = paste0(cell1, "_", cell2))
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$Type, pattern = "Epi", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$Type, pattern = "CD8", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi_pvalue <- T_epi %>% dplyr::select(interacting_pair, variable, pvalue)
paired_RL_names <- T_epi_pvalue %>% dplyr::select(interacting_pair) %>% unlist() %>% unname %>% unique %>% sort()
# paired_RL_names <- (T_epi$interacting_pair %>% table() %>% sort())[(T_epi$interacting_pair %>% table() %>% sort()) >= 40] %>% names
###.2 extracted the significant paired RL from raw data-------------
T_epi <- data.frame(all_cellphone_dat %>%
dplyr::select(interacting_pair, variable, cell1, cell2, Type, pvalue, mean, tissue)) %>%
mutate(Types = paste0(variable, ".", tissue ))
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$Type, pattern = "Epi", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$Type, pattern = "CD8", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi_pvalue <- T_epi %>% dplyr::select(interacting_pair, Types, pvalue) %>% unique()
T_epi_mean <- T_epi %>% dplyr::select(interacting_pair, Types, mean) %>% unique()
TEpi_pvalue <- dcast(T_epi_pvalue, interacting_pair ~ Types) %>% melt()
TEpi_mean <- dcast(T_epi_mean, interacting_pair ~ Types) %>% melt()
merged_T_Epi <- merge(TEpi_pvalue, TEpi_mean, by = c("interacting_pair", "variable"))
setnames(merged_T_Epi, old = c("value.x", "value.y"), new = c("pvalue", "mean"))
merged_T_Epi$mean[is.na(merged_T_Epi$mean)] <- 0
merged_T_Epi$pvalue[is.na(merged_T_Epi$pvalue)] <- 1
row.names(merged_T_Epi) <- 1:dim(merged_T_Epi)[1]
merged_T_Epi <- merged_T_Epi[merged_T_Epi$interacting_pair %in% paired_RL_names, ]
merged_T_Epi$pvalue <- log10(merged_T_Epi$pvalue)
merged_T_Epi$pvalue[is.infinite(merged_T_Epi$pvalue)] <- -1.5
rownames(merged_T_Epi) <- 1:dim(merged_T_Epi)[1]
top10_PLs <- (merged_T_Epi %>% subset(pvalue <0) %>% dplyr::select(-c(3:4)) %>% unique %>% dplyr::select(1) %>% table() %>% sort(decreasing = T))[1:10] %>% names()
merged_T_Epi <- merged_T_Epi %>% subset(interacting_pair %in% top10_PLs )
##---culster the RL using Pvlue; RL: Receptor and Ligation
dct_RL <- dcast(merged_T_Epi[, -4], interacting_pair ~ variable)
row.names(dct_RL) <- dct_RL$interacting_pair
dct_RL <- dct_RL[, -1] %>% as.matrix()
dct_RL[is.na(dct_RL)] <- 0
hdct_RL <- hclust(dist(dct_RL), method = "complete")
##---culster the celltyeps using Pvlue; CT: Cell Type
dct_CT <- dcast(merged_T_Epi[, -4], interacting_pair ~ variable)
row.names(dct_CT) <- dct_CT$interacting_pair
dct_CT <- dct_CT[, -1] %>% as.matrix() %>% t
dct_CT[is.na(dct_CT)] <- 0
hdct_CT <- hclust(dist(dct_CT), method = "complete")
###reorder the x axis
merged_T_Epi$interacting_pair <- factor(merged_T_Epi$interacting_pair, levels = hdct_RL$labels[hdct_RL$order])
CD8_select <- hdct_CT$labels[hdct_CT$order] %>% str_split(pattern = "\\.", simplify = T) %>% as.data.frame %>% dplyr::select(2) %>% unlist() %>% as.character %>% grep(pattern = "CD8")
Epi_select <- hdct_CT$labels[hdct_CT$order] %>% str_split(pattern = "\\.", simplify = T) %>% as.data.frame %>% dplyr::select(2) %>% unlist() %>% as.character %>% grep(pattern = "Epi", invert = T)
reordered_CD8_Epi <- c(hdct_CT$labels[hdct_CT$order][CD8_select], hdct_CT$labels[hdct_CT$order][Epi_select])
merged_T_Epi$variable <- factor(merged_T_Epi$variable, levels = reordered_CD8_Epi)
(reordered_CD8_Epi %>% str_split(pattern = "\\.", simplify = T) %>% as.data.frame %>% dplyr::select(1))
CD8_select_reorder <- (reordered_CD8_Epi)[1:51][(reordered_CD8_Epi %>% str_split(pattern = "\\.", simplify = T) %>% as.data.frame %>% dplyr::select(3))[1:51, ] %>% as.character %>% order()]
Epi_select_reorder <- (reordered_CD8_Epi)[52:102][(reordered_CD8_Epi %>% str_split(pattern = "\\.", simplify = T) %>% as.data.frame %>% dplyr::select(3))[52:102, ] %>% as.character %>% order()]
reordered_CD8_Epi_reorder <- c(CD8_select_reorder, Epi_select_reorder)
reordered_CD8_Epi_reorder <- gsub(reordered_CD8_Epi_reorder, pattern = "_cDNA", replacement = "")
merged_T_Epi$variable <- gsub(merged_T_Epi$variable %>% as.character, pattern = "_cDNA", replacement = "")
merged_T_Epi$variable <- factor(merged_T_Epi$variable %>% as.character, levels = reordered_CD8_Epi_reorder)
png("Epi_VS_CD8.png", width = 11, height = 8, units = "in", res = 400)
# pdf("Epi_VS_CD8.pdf", width = 12, height = 2.6)
ggplot(merged_T_Epi, aes(x = variable, y = interacting_pair, size = -pvalue)) +
geom_point(aes(colour = mean)) +
theme(axis.text.x = element_text(angle = 90, size = 6, vjust = 0.2, hjust = 1),
axis.text.y = element_text(angle = 0, size = 6, vjust = 0.2, hjust = 1),
axis.ticks.x = element_line(size = 0.1),
axis.ticks.y = element_line(size = 0.1),
axis.ticks.length = unit(.08, "cm"),
panel.background = element_rect(linetype = "solid", color = "black", size = 0.1, fill = "white"),
panel.grid = element_blank(),
plot.margin = unit(c(0.1, 0, 0.1, 0), "in")) +
scale_size_continuous(range = c(0, 2), name = "-log10(P-value)") +
scale_color_gradientn(colours = colorRampPalette(c("#23405b", "#3e73a0", "yellow", "red", "#950000"))(10)) +
labs(colour = "Mean",
x = "Cell Type",
y = "Interacting Pair")
dev.off()
##------epi interacting with Epi------##
T_epi <- data.frame(cellphone_dat %>% dplyr::select(interacting_pair, variable, cell1, cell2, pvalue, mean)) %>% mutate(Type = paste0(cell1, "_", cell2))
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$cell1, pattern = "Epi", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$cell2, pattern = "Epi", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi_pvalue <- T_epi %>% dplyr::select(interacting_pair, variable, pvalue)
paired_RL_names <- T_epi_pvalue %>% dplyr::select(interacting_pair) %>% unlist() %>% unname %>% unique %>% sort()
###.2 extracted the significant paired RL from raw data-------------
T_epi <- data.frame(all_cellphone_dat %>%
dplyr::select(interacting_pair, variable, cell1, cell2, Type, pvalue, mean, tissue)) %>%
mutate(Types = paste0(variable, ".", tissue ))
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$cell1, pattern = "Epi", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi <- T_epi[grep(T_epi$cell2, pattern = "Epi", value = F), ]
row.names(T_epi) <- 1:dim(T_epi)[1]
T_epi_pvalue <- T_epi %>% dplyr::select(interacting_pair, Types, pvalue) %>% unique()
T_epi_mean <- T_epi %>% dplyr::select(interacting_pair, Types, mean) %>% unique()
TEpi_pvalue <- dcast(T_epi_pvalue, interacting_pair ~ Types) %>% melt()
TEpi_mean <- dcast(T_epi_mean, interacting_pair ~ Types) %>% melt()
merged_T_Epi <- merge(TEpi_pvalue, TEpi_mean, by = c("interacting_pair", "variable"))
# merged_T_Epi$variable <- merged_T_Epi %>% select(variable) %>% unlist %>% as.character %>% gsub(pattern = "Novel", replacement = "Epithelial_DCD_high")
setnames(merged_T_Epi, old = c("value.x", "value.y"), new = c("pvalue", "mean"))
merged_T_Epi$mean[is.na(merged_T_Epi$mean)] <- 0
merged_T_Epi$pvalue[is.na(merged_T_Epi$pvalue)] <- 1
row.names(merged_T_Epi) <- 1:dim(merged_T_Epi)[1]
merged_T_Epi <- merged_T_Epi[merged_T_Epi$interacting_pair %in% paired_RL_names, ]
merged_T_Epi$pvalue <- log10(merged_T_Epi$pvalue)
merged_T_Epi$pvalue[is.infinite(merged_T_Epi$pvalue)] <- -1.5
rownames(merged_T_Epi) <- 1:dim(merged_T_Epi)[1]
##---culster the RL using Pvlue; RL: Receptor and Ligation
dct_RL <- dcast(merged_T_Epi[, -4], interacting_pair ~ variable)
row.names(dct_RL) <- dct_RL$interacting_pair
dct_RL <- dct_RL[, -1] %>% as.matrix()
dct_RL[is.na(dct_RL)] <- 0
hdct_RL <- hclust(dist(dct_RL), method = "complete")
##---culster the celltyeps using Pvlue; CT: Cell Type
dct_CT <- dcast(merged_T_Epi[, -4], interacting_pair ~ variable)
row.names(dct_CT) <- dct_CT$interacting_pair
dct_CT <- dct_CT[, -1] %>% as.matrix() %>% t
dct_CT[is.na(dct_CT)] <- 0
hdct_CT <- hclust(dist(dct_CT), method = "complete")
merged_T_Epi$interacting_pair <- factor(merged_T_Epi$interacting_pair %>% as.character %>% gsub(pattern = "_cDNA", replacement = ""),
levels = hdct_RL$labels[hdct_RL$order] %>% as.character %>% gsub(pattern = "_cDNA", replacement = ""))
merged_T_Epi$variable <- factor(merged_T_Epi$variable %>% as.character %>% gsub(pattern = "_cDNA", replacement = ""),
levels = hdct_CT$labels[hdct_CT$order] %>% as.character %>% gsub(pattern = "_cDNA", replacement = ""))
png("Epi.png", width = 21, height = 14.5, res = 1000, units = "in")
# pdf("Epi_VS_Epi.pdf", width = 15, height = 15)
ggplot(merged_T_Epi, aes(x = variable, y = interacting_pair, size = -pvalue)) +
geom_point(aes(colour = mean)) +
theme(axis.text.x = element_text(angle = 90, size = 5, vjust = 0.2, hjust = 1),
axis.text.y = element_text(angle = 0, size = 4.5, vjust = 0.2, hjust = 1),
axis.ticks.x = element_line(size = 0.1),
axis.ticks.y = element_line(size = 0.1),
axis.ticks.length = unit(.08, "cm"),
panel.background = element_rect(linetype = "solid", color = "black", size = 0.1, fill = "white"),
panel.grid = element_blank(),
plot.margin = unit(c(0.1, 0, 0.1, 0), "in")) +
scale_size_continuous(range = c(0, 2), name = "-log10(P-value)") +
scale_color_gradientn(colours = colorRampPalette(c("#23405b", "#3e73a0", "yellow", "red", "#950000"))(10)) +
labs(colour = "Mean",
x = "Cell Type",
y = "Interacting Pair")
dev.off()