-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBeta_diversity.rmd
279 lines (208 loc) · 10.6 KB
/
Beta_diversity.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
---
title: "Beta diversity Analysis"
author: "Óscar Brochado Kith"
date: "2023-10-24"
output: html_document
runtime: shiny
---
```{r echo=FALSE, warning=FALSE,error=FALSE,message=FALSE}
library(shiny)
library(ggplot2)
library(phyloseq)
library(lme4)
library(rstatix)
library(DT)
library(vegan)
library(ape)
```
```{r echo=FALSE}
fileInput("file",label="Select your phyloseq object",accept=".rds")
selectInput("transpose",label = "Are the taxa as rows in your otu table?",c("Yes","No"),selected = "No")
selectInput("data",label = "Beta diversity method",c("bray", "jaccard", "aitchison", "robust.aitchison","weighted unifrac","unweighted unifrac"))
selectInput("rank",label="Taxonomic rank","")
sliderInput("percentage",
label = "Percentage of prevalence",
min = 0,
max = 100,
value = 10)
selectInput("variable",label="Select grouping variable","")
selectInput("data2",label="Groups to compare","",multiple=TRUE)
loaded_data <- reactiveVal(NULL)
observeEvent(input$file, {
req(input$file)
datos <- readRDS(input$file$datapath)
loaded_data(datos)
col_options <- colnames(as.data.frame(sample_data(datos)))
updateSelectInput(session, "variable", choices = col_options)
})
observeEvent(input$variable, {
req(loaded_data())
datos2 <- readRDS(input$file$datapath)
col_options2 <- unique(data.frame(sample_data(datos2))[,input$variable])
updateSelectInput(session, "data2", choices =col_options2)
})
loaded_data2 <- reactiveVal(NULL)
observeEvent(input$file, {
# Verificar si se ha seleccionado un archivo
if (is.null(input$file)) return()
# Leer el archivo cargado (puedes ajustar la función según el formato de tu archivo)
data <- readRDS(input$file$datapath)
# Almacenar los datos en la reactiveVal
loaded_data2(data)
# Extraer las columnas del archivo como opciones para selectInput
ranks <- colnames(as.data.frame(tax_table(data)))
# Configurar las opciones para selectInput
updateSelectInput(session, "rank", choices = ranks)
})
```
# Beta diversity
This analysis will take some time, please be patient...
```{r echo=FALSE,message=FALSE, warning=FALSE,error=FALSE}
renderDataTable({
req(input$data2,input$variable)
ps<-readRDS(input$file$datapath)
if (input$transpose=="Yes") {
otu_table(ps)<-t(otu_table(ps))
}
min_samples <- round((nrow(sample_data(ps))*as.numeric(input$percentage)/100)) # Ajusta según tu criterio
ps<-tax_glom(ps,taxrank = input$rank)
data_phylo_filt <- prune_taxa(taxa_sums(ps) >= min_samples, ps)
sample_data(data_phylo_filt)$grouping_variable<-as.factor(data.frame(sample_data(data_phylo_filt))[,input$variable])
taxa<-tax_table(data_phylo_filt)
samples_df<-sample_data(data_phylo_filt)
set.seed(1782) # set seed for analysis reproducibility
OTU_filt_rar = rarefy_even_depth(otu_table(data_phylo_filt), rngseed = TRUE, replace = FALSE) # rarefy the raw data using Phyloseq package
data_otu_filt_rar = data.frame(otu_table(OTU_filt_rar)) # create a separated file
data_phylo_filt_rar <- phyloseq(OTU_filt_rar, taxa, sample_data(samples_df)) # create a phyloseq object
ps_temp<-phyloseq(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],
taxa,
sample_data(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,])
if (input$data=="aitchison") {
ps_clr<-microbiome::transform(data_phylo_filt,"clr")
a<-as.data.frame(
adonis2(as.formula(otu_table(ps_clr)[sample_data(ps_clr)$grouping_variable %in% input$data2,]~(sample_data(ps_clr))$grouping_variable[sample_data(ps_clr)$grouping_variable%in%input$data2]),permutations=9999, method="euclidean",na.action = na.omit)
)
rownames(a)[1]<-as.character(input$variable)
a
}
else if (input$data=="weighted unifrac") {
tree<-phy_tree(data_phylo_filt)
data_phylo_filt_rar <- phyloseq(OTU_filt_rar, taxa, sample_data(samples_df),tree) # create a phyloseq object
ps_temp<-phyloseq(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],
taxa,
sample_data(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],tree)
unifrac.dist <- UniFrac(ps_temp,
weighted = TRUE,
normalized = TRUE,
parallel = FALSE,
fast = TRUE)
permanova <- adonis2(unifrac.dist ~ sample_data(ps_temp)$grouping_variable,na.action = na.omit)
as.data.frame(permanova)
}
else if (input$data=="unweighted unifrac") {
tree<-phy_tree(data_phylo_filt)
data_phylo_filt_rar <- phyloseq(OTU_filt_rar, taxa, sample_data(samples_df),tree)
ps_temp<-phyloseq(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],
taxa,
sample_data(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],tree)
unifrac.dist <- UniFrac(ps_temp,
weighted = FALSE,
normalized = TRUE,
parallel = FALSE,
fast = TRUE)
permanova <- adonis2(unifrac.dist ~ sample_data(ps_temp)$grouping_variable,na.action = na.omit)
as.data.frame(permanova)
}
else{
a<- data.frame(
adonis2(as.formula(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,]~(sample_data(data_phylo_filt_rar))$grouping_variable[sample_data(data_phylo_filt_rar)$grouping_variable%in%input$data2]),permutations=9999, method=input$data,na.action = na.omit)
)
rownames(a)[1]<-as.character(input$variable)
a
}
})
```
```{r echo=FALSE,message=FALSE, warning=FALSE,error=FALSE}
renderPlot({
req(length(input$data2)>1,input$variable)
ps<-readRDS(input$file$datapath)
if (input$transpose=="Yes") {
otu_table(ps)<-t(otu_table(ps))
}
min_samples <- round((nrow(sample_data(ps))*as.numeric(input$percentage)/100)) # Ajusta según tu criterio
ps<-tax_glom(ps,taxrank = input$rank)
data_phylo_filt <- prune_taxa(taxa_sums(ps) >= min_samples, ps)
sample_data(data_phylo_filt)$grouping_variable<-as.factor(data.frame(sample_data(data_phylo_filt))[,input$variable])
taxa<-tax_table(data_phylo_filt)
samples_df<-sample_data(data_phylo_filt)
set.seed(1782) # set seed for analysis reproducibility
OTU_filt_rar = rarefy_even_depth(otu_table(data_phylo_filt), rngseed = TRUE, replace = FALSE) # rarefy the raw data using Phyloseq package
data_otu_filt_rar = data.frame(otu_table(OTU_filt_rar)) # create a separated file
data_phylo_filt_rar <- phyloseq(OTU_filt_rar, taxa, sample_data(samples_df)) # create a phyloseq object
ps_temp<-phyloseq(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],
taxa,
sample_data(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,])
if (input$data=="aitchison") {
ps_clr<-microbiome::transform(data_phylo_filt,"clr")
ps_clr_temp<-phyloseq(otu_table(ps_clr)[sample_data(ps_clr)$grouping_variable %in% input$data2,],
taxa,
sample_data(ps_clr)[sample_data(ps_clr)$grouping_variable %in% input$data2,])
dist_matrix <- vegdist(otu_table(ps_clr_temp), method = "euclidean")
# PCoA)
ordination_result <- pcoa(dist_matrix)
# plot_ordination
plot_ordination(ps_clr_temp, ordination_result, color = "grouping_variable") + geom_point(size=3) + stat_ellipse()
}
else if (input$data=="weighted unifrac") {
tree<-phy_tree(data_phylo_filt)
data_phylo_filt_rar <- phyloseq(OTU_filt_rar, taxa, sample_data(samples_df),tree)
ps_temp<-phyloseq(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],
taxa,
sample_data(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],tree)
ordu.wt.uni <- ordinate(ps_temp, "PCoA", "unifrac", weighted=T)
wt.unifrac <- plot_ordination(ps_temp,
ordu.wt.uni, color="grouping_variable")
wt.unifrac <- wt.unifrac + ggtitle("Weighted UniFrac") + geom_point(size = 2)
wt.unifrac <- wt.unifrac + theme_classic() + scale_color_brewer("grouping_variable", palette = "Set2")
print(wt.unifrac+ stat_ellipse())
unifrac.dist <- UniFrac(ps_temp,
weighted = TRUE,
normalized = TRUE,
parallel = FALSE,
fast = TRUE)
permanova <- adonis2(unifrac.dist ~ sample_data(ps_temp)$grouping_variable,na.action = na.omit)
permanova
a<-betadisper(unifrac.dist, sample_data(ps_temp)$grouping_variable, type = c("median","centroid"), bias.adjust = FALSE,
sqrt.dist = FALSE, add = FALSE)
print(as.data.frame(a$grouping_variable.distances))
}
else if (input$data=="unweighted unifrac") {
tree<-phy_tree(data_phylo_filt)
data_phylo_filt_rar <- phyloseq(OTU_filt_rar, taxa, sample_data(samples_df),tree) # create a phyloseq object
ps_temp<-phyloseq(otu_table(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],
taxa,
sample_data(data_phylo_filt_rar)[sample_data(data_phylo_filt_rar)$grouping_variable %in% input$data2,],tree)
ordu.unwt.uni <- ordinate(ps_temp, "PCoA", "unifrac", weighted=F)
unwt.unifrac <- plot_ordination(ps_temp,
ordu.unwt.uni, color="grouping_variable")
unwt.unifrac <- unwt.unifrac + ggtitle("Unweighted UniFrac") + geom_point(size = 2)
unwt.unifrac <- unwt.unifrac + theme_classic() + scale_color_brewer("grouping_variable", palette = "Set2")
print(unwt.unifrac+ stat_ellipse())
unifrac.dist <- UniFrac(ps_temp,
weighted = FALSE,
normalized = TRUE,
parallel = FALSE,
fast = TRUE)
permanova <- adonis2(unifrac.dist ~ sample_data(ps_temp)$grouping_variable,na.action = na.omit)
permanova
a<-betadisper(unifrac.dist, sample_data(ps_temp)$grouping_variable, type = c("median","centroid"), bias.adjust = FALSE,
sqrt.dist = FALSE, add = FALSE)
print(as.data.frame(a$grouping_variable.distances))
}
else{
pcoa_bc_temp=ordinate(ps_temp,"PCoA",input$data)
plot_ordination(ps_temp, pcoa_bc_temp, color = "grouping_variable") +
geom_point(size = 3) + stat_ellipse()
}
})
```