-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.R
More file actions
253 lines (213 loc) · 7.72 KB
/
server.R
File metadata and controls
253 lines (213 loc) · 7.72 KB
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
# Define server logic
server <- function(input, output, session) {
### Reactive values, organized by module ---
# Module 0A: Read and Store MiRNA Counts Data
longData <- reactiveVal()
wideData <- reactiveVal()
# Module 0B: Read and Store Metadata
metadata <- reactiveVal()
# Module 0C: Assess species selection and build mart objects accordingly
mart <- reactive({
validate(
need(input$hsa || input$mmu, "Please select at least one species."),
need(!(input$hsa && input$mmu), "Please select only one species at a time.")
)
if (input$hsa) {
tryCatch({
useEnsembl(biomart = "ensembl",
dataset = "hsapiens_gene_ensembl",
mirror = "useast")
}, warning = function(w) {
showNotification(paste("Cannot contact Ensembl, please try again later: ", w$message), type = "error")
return(NULL)
}, error = function(e) {
showNotification(paste("Cannot contact Ensembl, please try again later: ", e$message), type = "error")
return(NULL)
})
} else if (input$mmu) {
tryCatch({
useEnsembl(biomart = "ensembl",
dataset = "mmusculus_gene_ensembl",
mirror = "useast")
}, warning = function(w) {
showNotification(paste("Cannot contact Ensembl, please try again later", w$message), type = "error")
return(NULL)
}, error = function(e) {
showNotification(paste("Cannot contact Ensembl, please try again later", e$message), type = "error")
return(NULL)
})
} else {
return(NULL)
}
})
org_string <- reactive({
validate(
need(input$hsa || input$mmu, "Please select at least one species."),
need(!(input$hsa && input$mmu), "Please select only one species at a time.")
)
if (input$hsa) {
return("hgnc_symbol")
} else if (input$mmu) {
return("external_gene_name")
} else {
return(NULL)
}
})
org_abbrev <- reactive({
validate(
need(input$hsa || input$mmu, "Please select at least one species."),
need(!(input$hsa && input$mmu), "Please select only one species at a time.")
)
if (input$hsa) {
return("hsa")
} else if (input$mmu) {
return("mmu")
} else {
return(NULL)
}
})
org_db <- reactive({
validate(
need(input$hsa || input$mmu, "Please select at least one species."),
need(!(input$hsa && input$mmu), "Please select only one species at a time.")
)
if (input$hsa) {
return(org.Hs.eg.db)
} else if (input$mmu) {
return(org.Mm.eg.db)
} else {
return(NULL)
}
})
organism_reactive <- reactive({
validate(
need(input$hsa || input$mmu, "Please select at least one species.")
)
if (input$hsa) {
return("human")
} else if (input$mmu) {
return("mouse")
} else {
return(NULL)
}
})
org_mirtarbase <- reactive({
# Validate species selection
validate(
need(input$hsa || input$mmu, "Please select at least one species."),
need(!(input$hsa && input$mmu), "Please select only one species at a time.")
)
# Determine species
species <- if (isTRUE(input$hsa)) "hsa" else if (isTRUE(input$mmu)) "mmu" else NULL
req(species)
# If validated targets are requested, require user upload
if (isTRUE(input$validated)) {
if (species == "hsa") {
validate(need(!is.null(input$file_hsa), "Please upload hsa_MTI.csv to retrieve validated targets."))
path <- input$file_hsa$datapath
} else {
validate(need(!is.null(input$file_mmu), "Please upload mmu_MTI.csv to retrieve validated targets."))
path <- input$file_mmu$datapath
}
# Future code: handle colnames nicely and allow more species
# df <- readr::read_csv(here(
# file = path),
# col_types = readr::cols(.default = readr::col_character())
# )
df <-read.csv(here(path))
} else {
# Not validated: choose your behavior.
# Option A: return NULL (no data until validated is chosen)
return(NULL)
}
# Apply species-specific filtering/renaming
df <- df %>%
dplyr::filter(.data$Species..Target.Gene. == species) %>%
dplyr::rename(entrezgene_id = .data$Target.Gene..Entrez.ID.) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), as.character))
df
})
# Module 1: Exploratory Visualization
boxplot_reactive <- reactiveVal()
mds_xy_reactive <- reactiveVal()
stacked_plot_reactive <- reactiveVal()
mds_plot_reactive <- reactiveVal()
heatmap_reactive <- reactiveVal()
volcano_plot_reactive <- reactiveVal()
significant_heatmap_reactive <- reactiveVal()
correlation_plot_reactive <- reactiveVal()
chord_plot_reactive <- reactiveVal()
network_plot_reactive <- reactiveVal()
dotplot_reactive <- reactiveVal()
barplot_reactive <- reactiveVal()
# Module 2: Data Subsetting
subset_data_reactive <- reactiveVal()
# Module 3: Do Differential miRNA Expression Analysis with DESEQ2
DESEQ_obj <- reactiveVal()
DE_miRNA_results_reactive <- reactiveVal()
res_significant <- reactiveVal()
vst_cts_reactive <- reactiveVal()
significant_heatmap_reactive <- reactiveVal()
heatmap_mat_plotted_reactive <- reactiveVal()
heatmap_annotation_reactive <- reactiveVal()
mirna_data_reactive <- reactiveVal()
# Module 4: Predict target genes for all DE miRNA
predicted_target_reactive <- reactiveVal()
upregulated_miRNA_genes_reactive <- reactiveVal()
downregulated_miRNA_genes_reactive <- reactiveVal()
upregulated_miRNA_reactive <- reactiveVal()
downregulated_miRNA_reactive <- reactiveVal()
All_miRNA_Pathways_reactive <- reactiveVal()
miRNA_mapped_pathways <- reactiveVal()
chord_data_reactive <- reactiveVal()
network_displayed <- reactiveVal(list(nodes = NULL, edges = NULL))
path_network_displayed <- reactiveVal(list(nodes = NULL, edges = NULL))
# Module 5: Predict target genes for a single DE miRNA
displayed_gene_results <- reactiveVal()
Single_miRNA_Pathways_reactive <- reactiveVal()
# Module 6: Do Differential Gene Expression Analysis with DESEQ2
mrna_res_significant <- reactiveVal()
mrna_data_reactive <- reactiveVal()
mrna_wideData <- reactiveVal()
# Module 7: miRNA-mRNA Correlation Analysis
neg_cor_reactive <- reactiveVal()
supported_neg_cor_reactive <- reactiveVal()
cor_results_reactive <- reactiveVal()
global_cor_perm_result <- reactiveVal()
vis_nodes_reactive <- reactiveVal()
vis_edges_reactive <- reactiveVal()
corr_miRNA_Pathways_reactive <- reactiveVal()
corr_miRNA_mapped_pathways_reactive <- reactiveVal()
corr_chord_plot_reactive <- reactiveVal()
corr_chord_data_reactive <- reactiveVal()
corr_network_displayed <- reactiveVal(list(nodes = NULL, edges = NULL))
path_corr_network_displayed <- reactiveVal(list(nodes = NULL, edges = NULL))
# Species validation observer
observeEvent(input$submit, {
output$error_message <- renderText({
if (input$hsa && input$mmu) {
return("Please select only one species at a time.")
} else if (input$hsa || input$mmu) {
return("Success!")
} else {
return("Please select at least one species.")
}
})
})
# Source modular server files
files_to_source <- list.files("Server_Code", pattern = "\\.R$", ignore.case = TRUE)
cat("\n\n IN SERVER.R. Sourcing ... \n\n")
for (file in files_to_source) {
print(file)
source(
paste0("Server_Code/", file),
local = TRUE
)
}
# Auto-click the "Use Default Data" button when the app starts
observe({
shinyjs::delay(100, {
shinyjs::click("useDefaultData")
})
}) %>% bindEvent(TRUE, once = TRUE)
}