-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
205 lines (161 loc) · 6.04 KB
/
server.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
# ----- Servidor de la Aplicación ----
server <- function(input, output, session) {
# ----- Variables Reactivas -----
physeq <- reactive({
if (input$data_source == "Archivo") {
if (!is.null(input$file1)) {
return(readRDS(input$file1$datapath))
}
} else {
if (input$dataset == "Global Patterns") {
data("GlobalPatterns")
return(GlobalPatterns)
} else if (input$dataset == "Enterotype") {
data("enterotype")
return(enterotype)
} else if (input$dataset == "Soil") {
data("soilrep")
return(soilrep)
}
}
})
# Verificar que el objeto phyloseq tiene árbol filogenético
has_tree <- reactive({
tryCatch({
cat("Trying to get the phylogenetic tree...\n")
tree <- phy_tree(physeq())
cat("Got the tree, checking if it's NULL...\n")
if (is.null(tree)) {
cat("The tree is NULL.\n")
return(FALSE)
} else {
cat("The tree is not NULL.\n")
return(TRUE)
}
}, error = function(e) {
cat("Caught an error: ", e$message, "\n")
return(FALSE)
})
})
# ----- Descargar el objeto Phyloseq -----
output$downloadData <- downloadHandler(
filename = function() {
paste("phyloseq-", Sys.Date(), ".rds", sep="")
},
content = function(file) {
saveRDS(physeq(), file)
}
)
# ----- Observar el objeto Phyloseq seleccionado -----
observeEvent(input$update, {
# Mostrar la estructura del objeto phyloseq
output$physeq_summary <- renderPrint({
print(physeq())
})
# Generar el árbol filogenético si existe dentro del objeto
if (has_tree()) {
output$phylo_tree <- renderPlot({
plot_tree(physeq(), color = "SampleType")
})
} else {
shinyalert(title = "Warning", text = "El objeto phyloseq cargado carece de árbol filogenético", type = "warning")
}
})
# ----- Filtrado del Objeto Phyloseq -----
# Inicializa physeq_filtered como NULL
physeq_filtered <- reactiveVal(NULL)
# Actualiza physeq_filtered cuando se carga physeq
observe({
if (!is.null(physeq())) {
physeq_filtered(physeq())
}
})
output$variableui <- renderUI({
selectInput("variable", "Selecciona una variable de metadata", choices = colnames(sample_data(physeq_filtered())))
})
output$valueui <- renderUI({
req(input$variable)
selectizeInput("value", "Seleccione los valores que desea filtrar", choices = unique(sample_data(physeq_filtered())[,input$variable]), multiple = TRUE)
})
# Actualiza physeq_filtered cada vez que el usuario aplique un filtro
observeEvent(input$filter, {
req(input$variable)
req(input$value)
# Verifica si la variable seleccionada existe en los datos de muestra
if (input$variable %in% colnames(sample_data(physeq_filtered()))) {
# Aplica el filtro a physeq_filtered en lugar de physeq
filtered <- tryCatch({
ps_filter(physeq_filtered(), eval(parse(text = input$variable)) %in% input$value)
}, error = function(e) {
NULL
})
# Verifica si el filtro fue exitoso
if (!is.null(filtered)) {
# Actualiza physeq_filtered con el resultado del filtro
physeq_filtered(filtered)
# Mostrar la estructura del objeto phyloseq
output$filtered_physeq_summary <- renderPrint({
print(physeq_filtered())
})
# Generar el árbol filogenético
if (has_tree()) {
output$filtered_phylo_tree <- renderPlot({
plot_tree(physeq_filtered(), color = "SampleType")
})
} else {
shinyalert(title = "Warning", text = "El objeto phyloseq cargado carece de árbol filogenético", type = "warning")
}
} else {
# Muestra una alerta shiny si el filtro no fue exitoso
shinyalert(title = "Error", text = "El filtro no pudo ser aplicado. Por favor, verifica las variables y los valores seleccionados.", type = "error")
}
} else {
# Muestra una alerta shiny si la variable seleccionada no existe en los datos de muestra
shinyalert(title = "Error", text = paste("La variable seleccionada", input$variable, "no existe en los datos de muestra"), type = "error")
}
})
# ----- Estudio de la Diversidad -----
output$alpha_variableui <- renderUI({
if (is.null(physeq_filtered())) {
selectInput("variable_alpha", "Selecciona una variable de metadata", choices = colnames(sample_data(physeq())))
} else {
selectInput("variable_alpha", "Selecciona una variable de metadata", choices = colnames(sample_data(physeq_filtered())))
}
})
observeEvent(input$update_diversity, {
# Realizar el análisis de diversidad alfa
if (is.null(physeq_filtered())) {
diversity_data <- plot_richness(physeq(), measures = input$diversity, color = input$variable_alpha)
} else {
diversity_data <- plot_richness(physeq_filtered(), measures = input$diversity, color = input$variable_alpha)
}
# Generar el gráfico de diversidad
output$diversityPlot <- renderPlot({
diversity_data
})
})
observeEvent(input$update_heatmaps, {
# Generar los mapas de calor de abundancia de OTUs y especies
output$heatmap_otus <- renderPlot({
plot_heatmap(physeq(), taxa.label = "OTU")
})
output$heatmap_species <- renderPlot({
plot_heatmap(physeq(), taxa.label = "Species")
})
})
# ----- Realizar PCoA -----
# pcoa <- ape::pcoa(distance_matrix)
# Generar el gráfico de PCoA
#output$pcoaPlot <- renderPlot({
# ggplot(pcoa$vectors, aes_string(x = "Axis.1", y = "Axis.2")) +
# geom_point() +
# theme_minimal()
#})
# ----- Realizar la prueba PERMANOVA -----
# permanova_results <- adonis(distance_matrix ~ sample_data(physeq())$Group)
# Mostrar los resultados de la prueba PERMANOVA
#output$permanovaResults <- renderTable({
# permanova_results$aov.tab
#})
# ----- Estudio de Grafos -----
}