/
19-plot-acute-bdl.Rmd
327 lines (284 loc) · 8.1 KB
/
19-plot-acute-bdl.Rmd
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
---
title: "Acute BDL (SFig. 2.6)"
output:
workflowr::wflow_html:
code_folding: hide
editor_options:
chunk_output_type: console
---
```{r chunk-setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
autodep = TRUE,
cache = TRUE,
message = FALSE,
warning = FALSE
)
```
```{r wall-time-start, cache=FALSE, include=FALSE}
# Track time spent on performing this analysis
start_time <- Sys.time()
```
## Introduction
Here we generate publication ready plots of the analysis of the acute BDL mouse model.
## Libraries and sources
These libraries and sources are used for this analysis.
```{r libs-and-src, message=FALSE, warning=FALSE, cache=FALSE}
library(tidyverse)
library(tidylog)
library(here)
library(AachenColorPalette)
library(cowplot)
library(lemon)
library(ggpubr)
library(patchwork)
source(here("code/utils-plots.R"))
```
Definition of global variables that are used throughout this analysis.
```{r analysis-specific-params, cache=FALSE}
# i/o
data_path <- "data/mouse-acute-bdl"
output_path <- "output/mouse-acute-bdl"
# graphical parameters
# fontsize
fz <- 7
# keys to annotate contrasts
key_mm <- readRDS(here("data/meta-chronic-vs-acute/contrast_annotation.rds"))
```
## Design
```{r design}
design <- ggdraw() +
draw_image(here(data_path, "exp-design.pdf"))
```
## Histology
```{r histology}
histology <- ggdraw() +
draw_image(here(data_path, "histology.png"))
```
## Liver enyzmes
```{r liver-enzymes}
df <- read_csv2(here(data_path, "liver_enzymes.csv")) %>%
mutate(time = fct_inorder(time)) %>%
pivot_longer(col = -c(time), names_to = "enzyme", values_to = "y") %>%
mutate(enzyme = factor(str_to_upper(enzyme), levels = c("ALT", "AST", "ALP")))
df_summary <- df %>%
group_by(time, enzyme) %>%
summarise(mean_se(y)) %>%
ungroup()
liver_enzymes <- df_summary %>%
ggplot(aes(x = time, y = y)) +
geom_errorbar(aes(ymin = ymin, ymax = ymax), width = 0.5) +
geom_col() +
facet_rep_wrap(~enzyme, scales = "free", ncol = 1) +
labs(x = NULL, y = "U/L") +
my_theme(grid = "y", fsize = fz) +
stat_compare_means(
data = df, label = "p.signif",
ref.group = "Control", hide.ns = T
) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
liver_enzymes
```
## PCA
```{r pca}
pca_result <- readRDS(here(output_path, "pca_result.rds"))
keys <- key_mm %>%
filter(treatment == "Bile Duct Ligation" & class == "Acute") %>%
distinct(time = value, label = time_label2) %>%
drop_na() %>%
mutate(time = ordered(time))
pca_plot <- pca_result$coords %>%
inner_join(keys, by = "time") %>%
mutate(treatment = case_when(
treatment == "bdl" ~ "BDL",
treatment == "sham" ~ "Sham Surgery"
)) %>%
ggplot(aes(x = PC1, y = PC2, color = label, shape = treatment, label = label)) +
geom_point() +
labs(
x = paste0("PC1", " (", pca_result$var[1], "%)"),
y = paste0("PC2", " (", pca_result$var[2], "%)"),
color = "Time", shape = "Group"
) +
my_theme(fsize = fz) +
theme(
legend.position = "top",
legend.box.margin = margin(10, 0, -20, 10)
) +
scale_color_manual(values = aachen_color(c(
"blue", "bordeaux", "orange",
"green"
))) +
scale_shape_manual(values = c(19, 15))
pca_plot
```
## Volcano plot
```{r volcano-plot}
df <- readRDS(here(output_path, "limma_result.rds")) %>%
filter(contrast_reference == "bdl") %>%
inner_join(key_mm, by = "contrast") %>%
select(-contrast) %>%
rename(contrast = time_label2) %>%
mutate(contrast = fct_drop(contrast)) %>%
mutate(regulation = fct_recode(regulation,
Up = "up", Down = "down",
n.s. = "ns"
))
deg_count <- df %>%
add_count(contrast, regulation) %>%
filter(regulation != "n.s.") %>%
mutate(regulation = fct_drop(regulation)) %>%
mutate(
logFC = case_when(
regulation == "Up" ~ 0.75 * max(logFC),
regulation == "Down" ~ 0.75 * min(logFC)
),
pval = 0.4
) %>%
distinct(n, contrast, logFC, pval, regulation, value) %>%
complete(contrast, nesting(regulation, logFC, pval), fill = list(n = 0))
main_time <- c(1, 3, 7)
# for main panel
volcano_main <- df %>%
filter(value %in% main_time) %>%
plot_volcano(nrow = 1) +
geom_text(
data = filter(deg_count, value %in% main_time),
aes(y = pval, label = n), size = fz / (14 / 5),
hjust = "inward", vjust = "inward",
show.legend = F
) +
theme(
legend.position = "top",
legend.box.margin = margin(-10, 0, -10, 0)
) +
labs(color = "Regulation") +
my_theme(grid = "y", fsize = fz)
# for supp panel
volcano_supp <- df %>%
filter(!value %in% main_time) %>%
plot_volcano(ncol = 1) +
geom_text(
data = filter(deg_count, !value %in% main_time),
aes(y = pval, label = n), size = fz / (14 / 5),
show.legend = F
) +
theme(
legend.position = "top",
legend.box.margin = margin(-10, 0, -10, 0)
) +
labs(color = "Regulation") +
my_theme(grid = "y", fsize = fz)
volcano_main
volcano_supp
```
## Top DEGs
```{r top-degs}
df <- readRDS(here(output_path, "limma_result.rds")) %>%
filter(contrast_reference == "bdl") %>%
inner_join(key_mm) %>%
select(-class) %>%
rename(class = time_label2)
top_genes_df <- df %>%
filter(regulation != "ns") %>%
group_by(class, sign(logFC)) %>%
slice_max(order_by = abs(logFC), n = 10, with_ties = F) %>%
ungroup() %>%
nest(data = -c(class, value))
plots <- top_genes_df %>%
mutate(p = pmap(., .f = plot_top_genes, fontsize = fz))
main_time <- c(1, 3, 7)
# for main panel
top_genes_main <- plots %>%
filter(value %in% main_time) %>%
pull(p) %>%
wrap_plots() +
plot_layout(nrow = 1)
# for supp panel
top_genes_supp <- plots %>%
filter(!value %in% main_time) %>%
pull(p) %>%
wrap_plots() +
plot_layout(ncol = 1)
top_genes_main
top_genes_supp
```
## Time series cluster
```{r ts-cluster}
stem_res <- readRDS(here(output_path, "stem_result.rds")) %>%
filter(key == "bdl") %>%
filter(p <= 0.05) %>%
mutate(profile = fct_reorder(str_c(
"STEM ID: ",
as.character(profile)
), p))
# extract meta data of profiles
profile_anno <- stem_res %>%
group_by(key, profile, p) %>%
mutate(y = 1.2 * abs(max(value))) %>%
ungroup() %>%
mutate(max_time = max(time)) %>%
distinct(key, profile, p, size, y, max_time) %>%
mutate(label = str_c(size, " ", "genes"))
ts_cluster <- stem_res %>%
plot_stem_profiles(model_profile = F, nrow = 1) +
labs(x = "Time in Days") +
geom_text(
data = profile_anno, aes(x = 0, y = y, label = label),
inherit.aes = F, size = fz / (14 / 5), hjust = "inward"
) +
my_theme(grid = "no", fsize = fz) +
scale_x_continuous(
breaks = unique(stem_res$time),
guide = guide_axis(n.dodge = 1)
)
ts_cluster
```
## Collage
### Supplementary Figure 2.6
```{r s-fig-2-6, fig.width=7.87, fig.height=11.69}
sfig2_6 <- (design + histology) /
((liver_enzymes | pca_plot) + plot_layout(widths = c(1, 3))) /
((volcano_main / top_genes_main)) /
ts_cluster +
plot_layout(height = c(1.5, 2, 1.75, 0.75)) +
plot_annotation(tag_levels = list(c(
"A", "B", "C", "D", "E", "F", "", "", "",
"", "", "G"
))) &
theme(
plot.tag = element_text(size = fz + 3, face = "bold"),
legend.key.height = unit(11.5, "pt"),
legend.key.width = unit(12.5, "pt")
)
sfig2_6
ggsave(here("figures/Supplementary Figure 2.6.pdf"), sfig2_6,
width = 21, height = 29.7, units = c("cm")
)
ggsave(here("figures/Supplementary Figure 2.6.png"), sfig2_6,
width = 21, height = 29.7, units = c("cm")
)
```
### Supplementary Figure 2.7
```{r s-fig-2-7}
sfig2_7 <- (volcano_supp | top_genes_supp) +
plot_annotation(tag_levels = list(c("A", "B"))) &
theme(
plot.tag = element_text(size = fz + 3, face = "bold"),
legend.key.height = unit(11.5, "pt"),
legend.key.width = unit(12.5, "pt")
)
sfig2_7
ggsave(here("figures/Supplementary Figure 2.7.pdf"), sfig2_7,
width = 21, height = 10, units = c("cm")
)
ggsave(here("figures/Supplementary Figure 2.7.png"), sfig2_7,
width = 21, height = 10, units = c("cm")
)
```
```{r wall-time-end, cache=FALSE, include=FALSE}
duration <- abs(as.numeric(difftime(Sys.time(), start_time, units = "secs")))
t <- print(sprintf("%02d:%02d", duration %% 3600 %/% 60, duration %% 60 %/% 1))
```
Time spend to execute this analysis: `r t` minutes.