forked from kunstner/2022_canine_atopic_dermatitis_paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpha_diversity.R
153 lines (107 loc) · 5.99 KB
/
alpha_diversity.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
# General Information -----------------------------------------------------
# Authors: Mirja Thomsen
# Date: 2022-06-13
# Alpha diversity ---------------------------------------------------------
# Load libraries ----------------------------------------------------------
library(phyloseq)
library(tidyverse)
library(DivNet)
library(breakaway)
library(stats)
# Load data ---------------------------------------------------------------
ps <- readRDS(file = "phyloseq.OTU.RDS")
# DivNet estimate of Shannon for stool samples ----------------------------
ps_stool <- subset_samples(ps, location == "stool")
dv_stool <- DivNet::divnet(ps_stool, base = "Otu606") #using the most abundant OTU as base
combined_shannon <- ps_stool %>% sample_data %>% data.frame %>%
mutate(sample_names = rownames(.)) %>%
left_join(dv_stool$shannon %>% summary,
by = "sample_names") %>%
dplyr::filter( !is.na(estimate) )
combined_shannon %>% group_by(group) %>% summarize(mean = mean(estimate), median = median(estimate))
# Hypothesis testing
breakaway::betta(chats = combined_shannon$estimate,
ses = combined_shannon$error,
X = model.matrix(~group, data = combined_shannon))$table
# Plot alpha diversity
combined_shannon %>%
ggpubr::ggviolin(data = ., x = "group", y = "estimate", width = 1,
trim = T, add = c("boxplot", "dotplot"), add.params = list(color="grey2",fill = "grey75", size=0.2, width = 0.4),
color = "group", palette = c("orange1","#d7191c", "steelblue1", "steelblue4")) +
ylim(-0.15, 4) +
ylab("DivNet estimate of Shannon") +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1, size=14),
axis.text.y = element_text(size=14),
axis.ticks.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(size=14, vjust=2),
strip.background = element_rect(colour="white", fill="white", size=1.5, linetype="solid"),
legend.position = "none") +
scale_color_manual(values= c("orange1","#d7191c", "steelblue1", "steelblue4"))
ggsave(filename="plots/Shannon_groups_stool.pdf", height = 5, width=4.5)
# Community-wise estimate
dv_stool_group <- ps_stool %>% DivNet::divnet( X = "group", base = "Otu606") #using the most abundant OTU as base
combined_shannon_group <- ps_stool %>% sample_data %>% data.frame %>%
mutate(sample_names = rownames(.)) %>%
left_join(dv_stool_group$shannon %>% summary,
by = "sample_names") %>%
dplyr::filter( !is.na(estimate) )
# Hypothesis testing
DivNet::testDiversity(dv_stool_group, h0= "shannon")
# Plot community-wise alpha diversity
plot <- combined_shannon_group %>% group_by(group) %>% summarize(estimate = mean(estimate), error = mean(error))
ggplot(plot, aes(x=group, y=estimate, color=group)) +
geom_pointrange(aes(ymin=estimate-error, ymax=estimate+error))+
ylim(-0.15,4) +
ylab("DivNet estimate of Shannon") +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1, size=14),
axis.text.y = element_text(size=14),
axis.ticks.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(size=14, vjust=2),
strip.background = element_rect(colour="white", fill="white", size=1.5, linetype="solid"),
legend.position = "none") +
scale_color_manual(values=c("orange1","#d7191c", "steelblue1", "steelblue4"))
ggsave(filename="plots/group-wise_Shannon_stool.pdf", height = 5, width=4)
# Shannon diversity for skin samples --------------------------------------
# Exemplarily for skin site abdomen
sample_data(ps)$Shannon <- phyloseq::estimate_richness(physeq = ps, measures = "Shannon")$Shannon
ps_abdomen <- subset_samples(ps, location == "abdomen")
sample_data(ps_abdomen) %>% as_tibble() %>% group_by(group) %>% summarise(Shannondiv = mean(Shannon), SD = sd(Shannon))
# Hypothesis testing
stats::kruskal.test(Shannon ~ group, data = as_tibble(sample_data(ps_abdomen)))
wilcox <- sample_data(ps_abdomen) %>% as_tibble %>% filter (group == "ADpre" | group == "healthy") #likewise for other group comparisons
stats::wilcox.test(Shannon ~ group, data = wilcox)
# Plot alpha diversity
sample_data(ps_abdomen) %>% as_tibble %>%
ggpubr::ggviolin(data = ., x = "group", y = "Shannon", width = 1,
trim = T, add = c("boxplot", "dotplot"), add.params = list(color="grey2",fill = "grey75", size=0.3, width = 0.4),
color = "group", palette = c("orange1","#d7191c", "steelblue1", "steelblue4")) +
ylim(-0.15, 6) +
ggpubr::stat_compare_means() +
ggtitle("Abdomen") +
ylab("Estimated Shannon diversity") +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1, size=14),
axis.text.y = element_text(size=14),
axis.ticks.x=element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_text(size=14, vjust=2),
strip.background = element_rect(colour="white", fill="white", size=1.5, linetype="solid"),
legend.position = "none") +
scale_color_manual(values= c("orange1","#d7191c", "steelblue1", "steelblue4"))
ggsave(filename="plots/Shannon_groups_abdomen.pdf")