-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxplot_Deconvolution.R
More file actions
196 lines (173 loc) · 6.73 KB
/
Boxplot_Deconvolution.R
File metadata and controls
196 lines (173 loc) · 6.73 KB
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
# Instalar pacotes se necessário
install.packages("dplyr")
install.packages("tibble")
install.packages("tidyr")
install.packages("ggplot2")
install.packages("RColorBrewer")
install.packages("reshape2")
install.packages("ggpubr")
install.packages("rstatix")
install.packages("forcats")
install.packages("broom")
# Carregar pacotes
library(dplyr)
library(tibble)
library(tidyr)
library(ggplot2)
library(RColorBrewer)
library(reshape2)
library(ggpubr)
library(rstatix)
library(forcats)
library(ggpubr)
library(broom)
# --- Gráfico para dados oriundos de CIBERSORTx ---
# Importar tabelas
cibersort <- read.csv("C:/DOcuments/cibersortx/CIBERSORTx_Job28_Results.csv")
cibersort$Group <- "Grupo1"
# Tabela de metadados
info <- read.csv("C:/DOcuments/cibersortx/metadata_cibersort.csv")
info_cibersort <- info[info$Group %in% c("CHIKV", "CTRL"),]
info_chikv <- info_cibersort %>%
select(SampleId, Group)
# É mais fácil se as colunas de metadados e do cibersort tiverem o mesmo nome
table_cibersort <- left_join(cibersort , info_chikv, by = "Mixture")
# Caso não tenham, use by = c() para juntar as tabelas
table_cibersort <- left_join(cibersort , info_chikv, by = c("Mixture" = "SampleId"))
# converter em data frame
dados <- as.data.frame(table_cibersort)
# Transformar em formato longo, selecionando colunas relevantes
dados_long <- dados %>%
tidyr::pivot_longer(
cols = -c(Mixture, Group, P.value, Correlation, RMSE),
names_to = "CellType",
values_to = "Fraction"
)
# Selecione a ordem desejada para os tipos celulares
ordem_final <- c(
"Neutrophils",
"Monocytes",
"B.cells.naive",
"B.cells.memory",
"Plasma.cells",
"Dendritic.cells.activated",
"Dendritic.cells.resting",
"NK.cells.activated",
"NK.cells.resting",
"Macrophages.M0",
"Macrophages.M1",
"Macrophages.M2",
"T.cells.CD8",
"T.cells.CD4.memory.resting",
"T.cells.CD4.naive",
"T.cells.CD4.memory.activated",
"T.cells.follicular.helper",
"T.cells.gamma.delta",
"T.cells.regulatory..Tregs.",
"Mast.cells.activated",
"Mast.cells.resting",
"Eosinophils"
)
# Aplicar a ordem aos fatores
dados_long$CellType <- factor(dados_long$CellType, levels = ordem_final)
# Renomear os tipos celulares para nomes mais legíveis
# Exemplo de renomeação (ajuste conforme seus dados)
# Isso vai criar um vetor de mapeamento
nice_names <- c(
"B.cells.naive" = "B cells (naive)",
"B.cells.memory" = "B cells (memory)",
"Plasma.cells" = "Plasma cells",
"T.cells.CD8" = "T cells CD8+",
"T.cells.CD4.naive" = "T cells CD4+ (naive)",
"T.cells.CD4.memory.resting" = "T cells CD4+ (memory resting)",
"T.cells.CD4.memory.activated" = "T cells CD4+ (memory activated)",
"T.cells.follicular.helper" = "T cells CD4+ (follicular helper)",
"T.cells.regulatory..Tregs." = "T cells Regulatory",
"T.cells.gamma.delta" = "T cells (gamma delta)",
"NK.cells.resting" = "NK cells (resting)",
"NK.cells.activated" = "NK cells (activated)",
"Monocytes" = "Monocytes",
"Macrophages.M0" = "Macrophages M0",
"Macrophages.M1" = "Macrophages M1",
"Macrophages.M2" = "Macrophages M2",
"Dendritic.cells.resting" = "Dendritic cells (resting)",
"Dendritic.cells.activated" = "Dendritic cells (activated)",
"Mast.cells.resting" = "Mast cells (resting)",
"Mast.cells.activated" = "Mast cells (activated)",
"Neutrophils" = "Neutrophils",
"Eosinophils" = "Eosinophils"
)
# Aplicar os novos nomes
# recorde usa o !!! para descompactar a lista de mapeamento
dados_long$CellType <- recode(dados_long$CellType, !!!nice_names)
# Definir a ordem dos grupos
dados_long$Group <- factor(dados_long$Group, levels = c("CHIKV", "CTRL"))
# Criar o gráfico de boxplot com pontos sobrepostos
ggplot(dados_long, aes(x = CellType, y = Fraction, fill = Group, colour = Group)) +
geom_boxplot(width = 0.6,
position = position_dodge(width = 0.8),
outlier.shape = NA, alpha = 0.4) + # cria o boxplot
geom_jitter(aes(color = Group),
alpha = 0.6, size = 1.5,
position = position_dodge(width = 0.8)) + # adiciona os pontos
scale_fill_manual(values = c("CHKV" = "#FF0000",
"CTRL" = "#7F7F7F")) + # preenchimento das caixas
scale_colour_manual(values = c( "CHIKV" = "#8B0000",
"CTRL" = "#000000")) + # cor dos pontos
labs(title = "Deconvolution",
subtitle = "Estimated cell fractions (CIBERSORTx)",
x = NULL, y = "Estimated fraction") + # rótulo dos eixos
geom_vline(xintercept = c(1.5,2.5, 3.5, 4.5, 5.5,
6.5, 7.5, 8.5, 9.5, 10.5,
11.5, 12.5, 13.5, 14.5, 15.5,
16.5, 17.5, 18.5, 19.5, 20.5, 21.5),
linetype = "dashed", color = "grey50") + # criar linhas verticais para separar os tipos celulares
theme_classic(base_size = 12) + # tema clássico
theme(
plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 10, hjust = 0.5, color = "grey30"),
axis.text.x = element_text(angle = 45, hjust = 1, size = 10, color = "black"),
axis.text.y = element_text(size = 10, color = "black"),
axis.title.y = element_text(size = 11, face = "bold"),
legend.position = "top",
legend.title = element_blank(),
legend.text = element_text(size = 10),
panel.grid = element_blank(),
axis.line = element_line(size = 0.4, colour = "black"),
panel.border = element_rect(fill = NA, colour = NA)
) + # Definir detalhes do gráfico
stat_compare_means(method = "kruskal.test",
label = "p.signif",
hide.ns = TRUE,
size = 6) # adicionar p-values ao gráfico
# Caso queira salvar o gráfico
ggsave("cibersort_deconvolution_boxplot.png", width = 10, height = 6, dpi = 300)
ggsave("cibersort_deconvolution_boxplot.pdf", width = 10, height = 6)
# Para executar testes estatísticos entre os grupos
# Rodar pairwise Wilcoxon para cada CellType
library(dplyr)
mann <- dados_long %>%
group_by(CellType) %>%
do({
# pairwise Wilcoxon para cada tipo celular
res <- pairwise.wilcox.test(.$Fraction, .$Group,
p.adjust.method = "fdr")
# transformar matriz em tibble
mat <- res$p.value
df <- as.data.frame(as.table(mat)) %>%
rename(group1 = Var1, group2 = Var2, p.value = Freq) %>%
filter(!is.na(p.value)) %>%
mutate(CellType = unique(.$CellType)) %>%
# acrescentar coluna com asteriscos de significância
mutate(signif = case_when(
p.value < 0.0001 ~ "****",
p.value < 0.001 ~ "***",
p.value < 0.01 ~ "**",
p.value < 0.05 ~ "*",
TRUE ~ "ns"
))
df
}) %>%
ungroup()
# Salvar resultados em CSV
write.csv(posthoc_tidy, "wil_results.csv", row.names = FALSE)