-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBatch.effect.evaluation.R
182 lines (147 loc) · 6.19 KB
/
Batch.effect.evaluation.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
library(tidyverse)
library(ROGUE)
library(ggplot2)
# Load data
matr1 <- readr::read_rds("/home/pauling/projects/04_SEmodel/01_data/04_rogue/10_batch/matr1_con.rds.gz")
matr2 <- readr::read_rds("/home/pauling/projects/04_SEmodel/01_data/04_rogue/10_batch/matr2_sti.rds.gz")
mda <- rbind(matr1, matr2)
info <- readr::read_tsv("/home/pauling/projects/04_SEmodel/01_data/04_rogue/10_batch/nbt.4096-S3.txt")
info <- info %>% dplyr::filter(Cell_Name %in% rownames(mda))
info1 <- info %>% dplyr::filter(Stim_Condition == "ctrl")
info2 <- info %>% dplyr::filter(Stim_Condition == "stim")
matr1 <- t(matr1[info1$Cell_Name,])
matr2 <- t(matr2[info2$Cell_Name,])
mda <- t(mda[info$Cell_Name,])
###############################################################################
### ###
### Batch ~ interferon-beta ###
### ###
###############################################################################
# ROGUE calculation
rogue.res1 <- rogue(
matr1,
labels = info1$Cluster_ID,
samples = info1$Patient,
min.cell.n = 20,
platform = "UMI",
remove.outlier.n = 0
)
rogue.res2 <- rogue(
matr2,
labels = info2$Cluster_ID,
samples = info2$Patient,
min.cell.n = 20,
platform = "UMI",
remove.outlier.n = 0
)
rogue.res.comb <- rogue(
mda,
labels = info$Cluster_ID,
samples = info$Patient,
min.cell.n = 20,
platform = "UMI",
remove.outlier.n = 0
)
# Group
rogue.res1 <- rogue.res1 %>% dplyr::mutate(Condition = "Batch 1 (ctrl)")
rogue.res2 <- rogue.res2 %>% dplyr::mutate(Condition = "Batch 2 (stim)")
rogue.res.comb <- rogue.res.comb %>% dplyr::mutate(Condition = "Aggregated")
rogue.res <- rogue.res1 %>% dplyr::bind_rows(rogue.res2, rogue.res.comb)
# Boxplot
fig.path <- "/home/pauling/projects/04_SEmodel/07_NC_revision/02.figures/09.batch/01.pbmc/01.stimulation"
pauling.theme <- function(poi = "right", size = 12, title.size = 13){
theme(
legend.position = poi,
axis.title = element_text(size = title.size,color="black"),
axis.text = element_text(size = size,color="black"),
legend.title = element_text(size = size),
legend.text = element_text(size = size),
axis.text.y = element_text(color="black"),
axis.text.x = element_text(color="black"))
}
rogue.res$Condition <- factor(rogue.res$Condition, levels = c("Batch 1 (ctrl)","Batch 2 (stim)","Aggregated"))
#rogue.res %>% readr::write_rds("/home/pauling/projects/04_SEmodel/07_NC_revision/03.data/08.batch/rogue.rds.gz", compress = "gz")
cell.types <- c("CD14 Mono","CD16 Mono","CD4 Memory T","CD4 Naive T","CD8 T", "DC")
for (i in 1:6) {
rogue.res %>%
tidyr::gather(key = "CellTypes", value = "ROGUE", -Condition) %>%
dplyr::filter(CellTypes == cell.types[i]) %>%
ggplot(aes(Condition, ROGUE, colour = Condition)) +
geom_boxplot(aes(fill = Condition), alpha = 0.5) +
geom_point(aes(colour = Condition)) +
theme_classic() +
pauling.theme(poi = "NULL") +
labs(x = "") +
scale_colour_manual(values = c("#00688B", "#008B8B", "#CD6839")) +
scale_fill_manual(values = c("#00688B", "#008B8B", "#CD6839")) +
labs(
title = cell.types[i]
) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(color="black", angle = 30, hjust = 1)) -> p
ggsave(plot = p, filename = paste0(i,".",cell.types[i],".pdf"), path = fig.path, width = 3.3, height = 4.39)
}
# t.test
pv <- list()
for (i in 1:6) {
t1 <- t.test(rogue.res1[,cell.types[i]], rogue.res.comb[,cell.types[i]], alternative = "greater")
t2 <- t.test(rogue.res2[,cell.types[i]], rogue.res.comb[,cell.types[i]], alternative = "greater")
p1 <- t1$p.value
p2 <- t2$p.value
pv[[i]] <- c(p1,p2)
}
pv <- Reduce(rbind, pv)
pv <- as.matrix(pv)
colnames(pv) <- c("P.value-1","P.value-2")
rownames(pv) <- use.cell.types
###############################################################################
### ###
### Batch ~ Patient ###
### ###
###############################################################################
fig.path2 <- "/home/pauling/projects/04_SEmodel/07_NC_revision/02.figures/09.batch/01.pbmc/02.individual"
cell.types <- c("CD14 Mono","CD16 Mono","CD4 Memory T","CD4 Naive T","CD8 T", "DC")
patient.number <- length(unique(info1$Patient))
complete.rogue <- list()
for(i in 1:length(cell.types)){
rep.rogue <- c()
print(cell.types[i])
for (n in 1:20) {
info1 %>%
dplyr::filter(Cluster_ID == cell.types[i]) %>%
dplyr::group_by(Patient) %>%
dplyr::sample_frac(1/patient.number) %>%
dplyr::pull(Cell_Name) -> cell.names
tmp.matr <- matr1[,cell.names]
tmp.ent <- SE_fun(tmp.matr)
rep.rogue[n] <- CalculateRogue(tmp.ent, platform = "UMI")
print(rep.rogue[n])
}
complete.rogue[[i]] <- tibble(rogue = rep.rogue, cell.type = cell.types[i])
}
complete.rogue <- Reduce(rbind, complete.rogue)
complete.rogue <- complete.rogue %>% dplyr::mutate(Condition = "Aggregated")
rogue.ind <- rogue.res1 %>%
dplyr::mutate(Condition = "Individual") %>%
tidyr::gather(key = "cell.type", value = "rogue", -Condition)
rogue.mg <- rbind(rogue.ind, complete.rogue)
rogue.mg$Condition <- factor(rogue.mg$Condition, levels = c("Individual","Aggregated"))
for (i in 1:6) {
rogue.mg %>%
dplyr::filter(cell.type == cell.types[i]) %>%
dplyr::rename(ROGUE = rogue) %>%
ggplot(aes(Condition, ROGUE, colour = Condition)) +
geom_boxplot(aes(fill = Condition), alpha = 0.5) +
geom_point(aes(colour = Condition)) +
theme_classic() +
pauling.theme(poi = "NULL") +
labs(x = "") +
scale_colour_manual(values = c("#00688B", "#CD6839")) +
scale_fill_manual(values = c("#00688B", "#CD6839")) +
labs(
title = cell.types[i]
) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(color="black", angle = 30, hjust = 1)) -> p
ggsave(plot = p, filename = paste0(i,".",cell.types[i],".pdf"), path = fig.path2, width = 3.3, height = 4.39)
}