-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-figure4.Rmd
126 lines (113 loc) · 4.37 KB
/
08-figure4.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
---
title: "Figure 4 (Space variation)"
author: "Tu Hu"
date: "06/07/2022"
output: html_document
---
## Space effect - DGE anotomic area (Figure 4b 4c)
```{r DGE space, cache=TRUE, eval=FALSE}
DGE_space <-
tibble(
C1 = c("LS", "HC"),
C2 = c("NL", "HC")
) %>%
mutate(
contrast = paste0(C1, "vs", C2),
se_exp = map2(C1, C2, ~ se[,se$skin_type %in% c(.x, .y)]),
design_f = ifelse(C1 == "HC" & C2 == "HC",
"~ subject + visit + biopsy_area",
"~ subject + skin_type + visit + biopsy_area"),
deseq = map2(se_exp, design_f, ~ DESeqDataSet(.x, .y %>% as.formula)),
deseq = map(deseq, ~ DESeq(.x, parallel = T)),
res_name = map(deseq, ~ resultsNames(.x) %>% grep(pattern = "biopsy_area", ., value = T))) %>%
unnest(cols = res_name) %>%
mutate(
res_shrink = map2(deseq, res_name, ~ lfcShrink(.x, coef = .y, type = "apeglm", parallel = T)),
res_shrink = map(res_shrink, ~ as_tibble(.x, rownames = "gene_name"))
)
```
### Table S5
```{r output table s5, eval=FALSE}
DGE_space_filter <-
DGE_space %>% select(-se_exp, -design_f, -deseq) %>%
unnest(cols=res_shrink) %>%
filter(padj < .05, abs(log2FoldChange)>1)
readr::write_csv(DGE_space_filter, "data/supplementary/table_s5_dge_space.csv")
```
## Space effect - biological replicates
### Biological replicates
```{r found biological replicates}
bioreplicates <-
colData(se) %>% as_tibble() %>%
mutate(biological_rep_id = paste(subject, visit, skin_type)) %>%
group_by(biological_rep_id) %>%
summarise(n = n()) %>%
filter(n == 2) %>%
pull(biological_rep_id)
bioreplicates_t <-
colData(se) %>% as_tibble() %>%
mutate(biological_rep_id = paste(subject, visit, skin_type)) %>%
filter(biological_rep_id %in% bioreplicates) %>%
left_join(se[c("CCL22"),] %>% as_tibble())
# saveRDS(bioreplicates_t, "data/_TMP/bioreplicate_t.rds")
replicate_g <-
bioreplicates_t %>%
select(biological_rep_id,
subject, visit, skin_type, feature,
counts_scaled, replicate_ID) %>%
ggplot(aes(x = replicate_ID,
y = counts_scaled,
group = biological_rep_id)) +
geom_point(aes(color = skin_type)) +
facet_grid(feature ~ skin_type, scales = "free") +
geom_line(aes(color = skin_type), alpha = .5) +
geom_boxplot(aes(group = replicate_ID, fill = skin_type), alpha = .5) +
scale_color_manual(values = pca_color) +
scale_fill_manual(values = pca_color) +
scale_y_continuous(trans = "log2") +
xlab("replicate") +
theme(axis.title.y = element_blank())
# ggsave("data/supplementary/supplement_replicate.png", replicate_g,
# height = 25, width = 8)
```
```{r}
t <-
readRDS(url("https://cdn.jsdelivr.net/gh/tuhulab/Shiny_AD_time_space@master/data/bioreplicate_t.rds")) %>%
select(biological_rep_id,
subject, visit, skin_type, feature,
counts_scaled, replicate_ID) %>%
arrange(biological_rep_id) %>%
mutate(counts_scaled = counts_scaled + 1) %>%
filter(feature %in% "CCL22") %>%
mutate(skin_type =
case_when(skin_type == "LS" ~ "Lesional",
skin_type == "NL" ~ "Non-lesional",
skin_type == "HC" ~ "Healthy control"),
skin_type =
factor(skin_type, levels = c("Healthy control",
"Non-lesional",
"Lesional")))
stat_test <-
t %>% group_by(feature, skin_type) %>%
t_test(counts_scaled ~ replicate_ID, paired = T) %>%
add_significance() %>%
add_xy_position(x = "replicate_ID",
y.trans = log2, step.increase = .08) %>%
group_by(feature) %>% mutate(y.position = max(y.position)) %>%
ungroup()
g_bioreplicate <-
t %>% filter(counts_scaled > 50) %>%
pivot_wider(names_from = replicate_ID, values_from = counts_scaled) %>%
ggpaired(cond1 = "01", cond2 = "02", y = "counts_scaled",
xlab = "Biological replicate",
ylab = "",
line.color = "grey", line.size = .4,
fill = "skin_type",
palette = c("Lesional" = "#eb2d0c",
"Non-lesional" = "#eb8b9b",
"Healthy control" = "#91cf60")) %>%
facet(facet.by = c("skin_type"), scales = "free_y") +
stat_pvalue_manual(stat_test, label = "p") +
scale_y_continuous(trans = "log2", expand = expansion(mult = c(.05, .1)), breaks = c(10, 100, 1000, 10000, 30000)) +
rremove("legend")
```