-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
397 lines (359 loc) · 15.4 KB
/
app.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#### NMDS plot ####
library(shiny)
library(shinythemes)
library(readxl)
library(openxlsx)
library(tidyverse)
library(dplyr)
library(vegan)
library(tools)
library(shinycssloaders)
library(ggrepel)
library(RColorBrewer)
#### ui ####
ui <- fluidPage(
theme = shinytheme("sandstone"),
titlePanel("NMDS app"),
sidebarLayout(
sidebarPanel(
h4("1."),
fileInput('otu',
'Choose OTU table',
accept = c('sheetName', 'header'),
multiple = FALSE),
checkboxInput('header_otu',
'Header',
value = TRUE),
a("example",
href = "https://github.com/Vojczech/NMDS_shiny",
target="_blank"),
tags$br(),
textInput('sheet_otu', 'Exact name of the excel sheet (required)',
placeholder = "name of the sheet"),
downloadButton("downloadData",
"Download OTU table"),
tags$hr(style = "border-color: black;"),
h4("2."),
fileInput('samples',
'Choose sample list',
accept = c('sheetName', 'header'),
multiple = FALSE),
checkboxInput('header_samples',
'Header',
value = TRUE),
textInput('sheet_samples',
'Exact name of the excel sheet (required)',
placeholder = "name of the sheet"),
tags$hr(style = "border-color: black;"),
h4("3."),
sliderInput("percent_treshold",
"Filter OTUs by percentage per sample",
min = 0,
max = 100, c(3),
post = "%",
step = 0.5),
numericInput("no_samples",
"Number of samples with percentage >= upper value",
value = 3,
min = 1,
step = 1),
helpText("Max number of samples:"),
textOutput("sample_range"),
tags$br(),
tags$hr(style = "border-color: black;"),
h4("4."),
uiOutput("grouping_factor"),
radioButtons("factor_select",
"Colours by",
c("Factor" = "Factor", "Numeric" = "Values"),
inline = T,
selected = "Factor"),
checkboxInput("ellipses",
"Display ellipses - grouping by factor only",
value = FALSE),
checkboxInput("sample_disp",
"Display sample labels",
value = FALSE),
uiOutput("label_factor"),
downloadButton("downloadMultivar",
"Table ready for NMDS"),
tags$hr(style = "border-color: black;"),
h4("5."),
radioButtons("dissimilarity",
"Dissimilarity matrix",
c("Hellinger distance" = "hell", "Bray-Curtis" = "bray"),
inline = T,
selected = "hell"
),
uiOutput("fitted"),
tags$br(),
downloadButton("downloadMultivarFinal",
"Table with NMDS results for external plotting"),
tags$br(),
tags$br(),
downloadButton("downloadPlotFinal",
"Download final plot as PDF"),
tags$hr(style = "border-color: black;"),
tags$br(),
a("Minimal examples of input excel files are available on GitHub",
href = "https://github.com/Vojczech/NMDS_shiny",
target="_blank")
),
mainPanel(
tabsetPanel(
tabPanel("NMDS",
h5(textOutput("caption1")),
tableOutput("contents1") %>% withSpinner(type = getOption("spinner.type", default = 4)),
h5(textOutput("caption2")),
tableOutput("contents2") %>% withSpinner(type = getOption("spinner.type", default = 4)),
plotOutput("contents3") %>% withSpinner(type = getOption("spinner.type", default = 4))
),
tabPanel("About",
h4("Plots for fast insight into community data"),
p("Visit", a("this link", href = "https://github.com/Vojczech/NMDS_shiny", target="_blank"), "for brief tutorial."),
p("Tested on real data from the paper", a("Tláskal et al., 2017.", href = "https://academic.oup.com/femsec/article-abstract/93/12/fix157/4604780", target = "_blank"), "App is producing same results as metaMDS and envfit functions from the vegan package alone."),
p("Please note that apps hosted for free on shinyapps.io are limited to 1GB of memory. Therefore loading of larger OTU tables may take a while. If server disconnects after upload try to decrease size of excel file by e.g. deleting of singleton OTUs."),
p("packages:",
p(a("tidyverse", href = "https://www.tidyverse.org/", target="_blank")),
p(a("vegan", href = "https://cran.r-project.org/web/packages/vegan/index.html", target="_blank")),
p(a("ggrepel", href = "https://github.com/slowkow/ggrepel", target="_blank")),
p(a("shinycssloaders", href = "https://github.com/andrewsali/shinycssloaders", target="_blank")),
p(a("openxlsx", href = "https://github.com/awalker89/openxlsx", target="_blank")))
)
)
)
)
)
#### server ####
server <- function(input, output) {
options(shiny.maxRequestSize=30*1024^2)
# OTUs
dataset_otu <- reactive({
validate(
need(input$otu != "", "Please select a file and sheet with OTUs")
)
infile = input$otu
if (is.null(infile))
return(NULL)
readxl::read_excel(infile$datapath, sheet = input$sheet_otu, col_names = input$header_otu)
})
# samples
dataset_samples <- reactive({
validate(
need(input$samples != "", "Please select a file and sheet with samples")
)
infile = input$samples
if (is.null(infile))
return(NULL)
readxl::read_excel(infile$datapath, sheet = input$sheet_samples, col_names = input$header_samples)
})
# total number of samples
samples_count <- reactive({
number = nrow(dataset_samples())
})
output$sample_range <- renderText({
samples_count()
})
# text and table
output$caption1 <- renderText({
"first 5 rows of OTU table are displayed"
})
output$contents1 <- renderTable({
head(dataset_otu(), 5)
})
# text and table
output$caption2 <- renderText({
"first 5 rows of sample table are displayed"
})
output$contents2 <- renderTable({
head(dataset_samples(), 5)
})
# download
output$downloadData <- downloadHandler(
filename = function() {
paste("original_table", input$otu, sep = "_")
},
content = function(file) {
write.xlsx(dataset_otu(), file)
})
# ggplot grouping factor
ggplot_factor <- reactive({
factor <- dataset_samples()[,input$grouping_factor_input, drop = FALSE]
colnames(factor)<- "ggplot_factor"
factor
})
# filtr percentage
filtered_titles <- reactive({
otus_percent <- dataset_otu()
tbl_df(otus_percent) %>%
gather(sample, per, (2:ncol(otus_percent))) %>%
group_by_at(c(1,2)) %>%
filter(per >= input$percent_treshold) %>%
ungroup() %>%
group_by_at(c(1)) %>%
dplyr::summarise(treshold_count = n()) %>%
filter(treshold_count >= input$no_samples) %>%
select(c(1))
})
# filtr multivar OTUs
otus_multivar <- reactive({
filtered_titles_list <- filtered_titles()
otus_percent <- dataset_otu()
tbl_df(otus_percent) %>%
gather(sample, per, (2:ncol(otus_percent))) %>%
right_join(filtered_titles_list) %>%
spread(sample, per)
})
# vegan matrix
otus_multivar_for_plot <- reactive({
dataset_samples <- dataset_samples()
otus_multivar <- otus_multivar()
dataset_samples
otus_multivar <- gather(otus_multivar, sample, perc, 2:ncol(otus_multivar)) %>%
spread(1, perc) %>% # this function orders NMDS input table according to sample names, must be consistent with dataset_samples() order
arrange((match(sample, dataset_samples$sample_name))) %>% # ensures same order of NMDS input as dataset_samples() table but needs "sample_name" as column name
tibble::column_to_rownames(var = "sample")
})
# vegan matrix download
output$downloadMultivar <- downloadHandler(
filename = function() {
paste("vegan_ready", input$otu, sep = "_")
},
content = function(file) {
write.xlsx(otus_multivar_for_plot(), file, colNames = TRUE, rowNames = TRUE)
})
# NMDS without envfit
mdsord <- reactive({
if(input$dissimilarity == "hell") {
set.seed(31)
mdsord <- metaMDS(comm = decostand(otus_multivar_for_plot(), "hellinger"), distance = "euclidean", trace = FALSE, k = 2, trymax = 200, autotransform = FALSE)
} else {
set.seed(31)
mdsord <- metaMDS(comm = otus_multivar_for_plot(), distance = "bray", trace = FALSE, k = 2, trymax = 200, autotransform = FALSE)
}
NMDS_data <- dataset_samples()
ggplot_factor <- as.data.frame(ggplot_factor())
NMDS_x <- mdsord$points[ ,1]
NMDS_y <- mdsord$points[ ,2]
nmds_stress <- round(mdsord$stress, digits = 3)
NMDS_data_final <- cbind(NMDS_data, NMDS_x, NMDS_y, ggplot_factor, data.frame(nmds_stress))
})
# NMDS envfit included, important are same parametres and set.seed
mdsord_fitted <- reactive({
if(input$dissimilarity == "hell") {
set.seed(31)
mdsord <- metaMDS(comm = decostand(otus_multivar_for_plot(), "hellinger"), distance = "euclidean", trace = FALSE, k = 2, trymax = 200, autotransform = FALSE)
} else {
set.seed(31)
mdsord <- metaMDS(comm = otus_multivar_for_plot(), distance = "bray", trace = FALSE, k = 2, trymax = 200, autotransform = FALSE)
}
if(is.null(input$fitted_factors)){
} else {
set.seed(31)
fitted_plot <- envfit(mdsord, fitted_df(), permutations = 999, arrow.mul = 1)
envfit_scores <- as.data.frame(scores(fitted_plot, display = "vectors"))
envfit_scores <- cbind.data.frame(envfit_scores, env.variables = rownames(envfit_scores), stringsAsFactors = FALSE)
}
})
# NMDS final points and variables table download
output$downloadMultivarFinal <- downloadHandler(
filename = function() {
paste("final_positions", input$otu, sep = "_")
},
content = function(file) {
l <- list("nmds_points" = mdsord(), "variables_score" = mdsord_fitted())
write.xlsx(l, file)
})
output$grouping_factor <- renderUI({
selectInput("grouping_factor_input", "Grouping factor",
colnames(dataset_samples()),
selected = NULL)
})
# label points
output$label_factor <- renderUI({
selectInput("label_factor_input", "Label points",
colnames(dataset_samples()),
selected = NULL)
})
label_df <- reactive({
label_variables <- dataset_samples()[,input$label_factor_input, drop = FALSE]
label_variables <- unlist(label_variables) # data.frame to atomic vector which is needed for geom_text
})
# env variables for envfit
output$fitted <- renderUI({
checkboxGroupInput("fitted_factors",
"Choose fitted environmental factors",
choices = colnames(dataset_samples()),
selected = NULL)
})
fitted_df <- reactive({
variables <- dataset_samples()[,input$fitted_factors, drop = FALSE]
variables
})
# ggplot NMDS, points are from NMDS without envfit, arrows for env variables are from NMDS with envfit
mdsord_final <- reactive({
mdsord <- mdsord()
mdsord_fitted <- mdsord_fitted()
stress <- unique(mdsord$nmds_stress) # for stress value of NMDS
# coloured by factor or value
if (input$factor_select == "Factor") {
mdsord$ggplot_factor <- as.factor(mdsord$ggplot_factor)
} else {
mdsord$ggplot_factor <- as.numeric(mdsord$ggplot_factor)
}
# if env variables are available
if(is.null(input$fitted_factors)){
ggplot(data = mdsord, aes(y = NMDS_y, x = NMDS_x)) +
geom_point(aes(colour = ggplot_factor), show.legend = TRUE, size = 4.5) +
{if (is.factor(mdsord$ggplot_factor)== TRUE && (input$ellipses)==TRUE) {
stat_ellipse(aes(colour = ggplot_factor), type = "t") # add ellipses for factorial grouping
}} +
{if (is.factor(mdsord$ggplot_factor)== TRUE) {
scale_colour_brewer(palette = "Paired", type = "div") # color in the case of discrete values
} else {
scale_color_viridis_c() # color in the case of continuous values
}} +
annotate("text", x = (0+max(mdsord$NMDS_x)), y = (0+min(mdsord$NMDS_y)), label = paste("stress\n", stress), size = 3.5) +
{if(input$sample_disp)
geom_text_repel(aes(x = NMDS_x, y = NMDS_y, label = label_df(), color = ggplot_factor), size = 2, segment.color = 'grey50', segment.size = 0.2)} + # display sample names
theme_bw() +
ggtitle("NMDS plot")
} else {
ggplot(data = mdsord, aes(y = NMDS_y, x = NMDS_x)) +
geom_point(aes(colour = ggplot_factor), show.legend = TRUE, size = 4.5) +
{if (is.factor(mdsord$ggplot_factor)== TRUE && (input$ellipses)==TRUE) {
stat_ellipse(aes(colour = ggplot_factor), type = "t") # add ellipses for factorial grouping
}} +
{if (is.factor(mdsord$ggplot_factor)== TRUE) {
scale_colour_brewer(palette = "Paired", type = "div") # color in the case of discrete values
} else {
scale_color_viridis_c() # color in the case of continuous values
}} +
annotate("text", x = (0+max(mdsord$NMDS_x)), y = (0+min(mdsord$NMDS_y)), label = paste("stress\n", stress), size = 3.5) +
{if(input$sample_disp)
geom_text_repel(aes(x = NMDS_x, y = NMDS_y, label = label_df(), color = ggplot_factor), size = 2, segment.color = 'grey50', segment.size = 0.2)} + # display sample names
theme_bw() +
ggtitle("NMDS plot") +
geom_segment(data = mdsord_fitted(),
aes(x = 0, xend = 1.2*NMDS1, y = 0, yend = 1.2*NMDS2),
arrow = arrow(length = unit(0.25, "cm")), colour = "#556b2f", size = 0.7) +
geom_text(data = mdsord_fitted(),
aes(x = 1.2*NMDS1, y = 1.2*NMDS2, label = env.variables),
size = 6,
hjust = -0.3)
}
})
output$contents3 <- renderPlot({
mdsord_final()
})
# download NMDS
output$downloadPlotFinal <- downloadHandler(
filename = function() {
paste(tools::file_path_sans_ext(input$otu), '.pdf', sep='')
},
content = function(file) {
ggsave(file, plot = mdsord_final(), device = "pdf", dpi = 300, height = 210, width = 297, units = "mm")
}
)
}
shinyApp(ui, server)