-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
313 lines (292 loc) · 11.1 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
require(shiny)
require(PhysioSpaceMethods)
require(HumanPhysioSpace)
require(PlantPhysioSpace)
require(parallel)
# Allow upload of 1GB tables
options(shiny.maxRequestSize = 1024 ^ 3)
# Show all errors in detail:
options(shiny.sanitize.errors = FALSE)
# Allow for maximum parallel computation - use all available cores
options(mc.cores = detectCores())
phys.spaces <-
c('', unlist(lapply(c('HumanPhysioSpace', 'PlantPhysioSpace'), function(x)
data(package = x)$results[, "Item"])))
isValid <-
function(x) {
!is.null(x) && !all(is.na(x)) && length(x) > 0
}
allOrSelection <-
function(sub.set, compl.set) {
if (isValid(sub.set) && isValid(compl.set)) {
intersect(sub.set, compl.set)
} else if (!isValid(sub.set) && isValid(compl.set))
compl.set
}
ui <- fluidPage(titlePanel("Physiospace Web Interface"),
# Sidebar layout with input and output definitions
sidebarLayout(
# Sidebar panel for inputs
sidebarPanel(
# Input: Slider for the number of bins
selectInput("physSpace", "Select a Physio-Space for your analysis", choices = phys.spaces),
conditionalPanel(
condition = "typeof input.physSpace !== 'undefined' && input.physSpace !== ''",
selectInput(
"geneListOrExpressionMatrix",
"Please select a mode of input",
choices = c('',
"Gene-List",
"Expression-Matrix")
)
),
conditionalPanel(
condition = paste(
"typeof input.physSpace !== 'undefined' && input.physSpace !== '' &&",
"typeof input.geneListOrExpressionMatrix !== 'undefined' &&",
"input.geneListOrExpressionMatrix === 'Expression-Matrix'"
),
fileInput(
"file1",
paste(
"Please upload your table (max. 1GB and CSV-Format). For details on how to prepare it,",
"please consult the Vignette of the R package `PhysioSpaceMethods`."
),
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")
)
),
conditionalPanel(
condition = paste(
"typeof input.physSpace !== 'undefined' && input.physSpace !== '' &&",
"typeof input.geneListOrExpressionMatrix !== 'undefined' &&",
"input.geneListOrExpressionMatrix === 'Gene-List'"
),
selectizeInput(
"upGenes",
"Please select the significantly up-regulated Genes",
c(),
multiple = TRUE
),
selectizeInput(
"downGenes",
"Please select the significantly down-regulated Genes",
c(),
multiple = TRUE
)
),
conditionalPanel(condition = "output.receivedAllInput",
actionButton("compute", "Compute")),
conditionalPanel(
condition = "output.done === 'true'",
tags$hr(),
tags$h3('Plot settings'),
sliderInput(
"reducedPlotting",
"If non-zero this is the number of significant rows plotted per gene.",
min = 0,
max = 20,
value = 0,
step = 1
),
selectizeInput(
"physSpaceTissues",
"Select subset of Physio-Space tissues to display in plot. Leave empty to display all.",
c(),
multiple = TRUE
),
selectizeInput(
"experimentTissues",
"Select subset of your experiment's tissues to display in plot. Leave empty to display all.",
c(),
multiple = TRUE
),
tags$hr(),
tags$h3('Results'),
downloadButton("downloadPhysSpaceMap", "Download results table (Physio-Map)"),
tags$br(),
downloadButton("downloadPhysioHeatMap", "Download plot (PDF)")
)
)
,
# Main panel for displaying outputs:
mainPanel(plotOutput(outputId = "physSpacePlot"))
))
# Define the server:
server <- function(input, output, session) {
# Generate the choices of gene identifiers depending on the selected Physio-Space:
physSpaceGeneIds <- reactive({
if (!is.null(input$physSpace) && nchar(input$physSpace) > 0) {
rownames(get(input$physSpace))
} else
c()
})
# Enable selection of up- and down-regulated genes:
observe({
if (!is.null(input$geneListOrExpressionMatrix) &&
input$geneListOrExpressionMatrix == 'Gene-List') {
updateSelectizeInput(session,
"upGenes",
choices = physSpaceGeneIds(),
server = TRUE)
updateSelectizeInput(session,
"downGenes",
choices = physSpaceGeneIds(),
server = TRUE)
}
})
# Generate input for the function calculatePhysioMap:
calc.phys.map.inp <- reactive({
if (!is.null(input$file1$datapath)) {
# Read uploaded data as matrix
counts.df <- as.matrix(
read.table(
input$file1$datapath,
sep = ",",
quote = '"',
comment.char = '',
header = TRUE,
stringsAsFactors = FALSE
)
)
# row-names must be gene names in the first column
rownames(counts.df) <-
counts.df[, 1]
counts.df[,-1,drop=FALSE]
} else if (isValid(input$upGenes) &&
isValid(input$downGenes)) {
# Just use a list of significantly up- and down-regulated genes:
list(input$upGenes, input$downGenes)
}
})
# Calculate the Physiospace Map
phys.map <- reactive({
if (as.logical(input$compute) && (
isValid(input$upGenes) &&
isValid(input$downGenes) ||
isValid(input$file1$datapath)
)) {
# Create a Progress object
progress <-
shiny::Progress$new()
# Make sure it closes when we exit this reactive, even if there's an error
on.exit(progress$close())
# Show progress message
progress$set(message = "Analyzing data. This can take a while..", value = 0)
tryCatch({
PhysioSpaceMethods::calculatePhysioMap(
InputData = calc.phys.map.inp(),
Space = get(input$physSpace),
NumbrOfCores = getOption("mc.cores", 1)
)
}, error = function(e) {
showNotification(
paste("An unexpected error has occurred. Please try again.", e),
duration = 6,
type = 'error'
)
}, finally = {
# Do nothing. This is needed to prevent the shiny App from dying in case of an error.
})
}
})
# Enable selection of tissues to reduce large plots:
observe({
if (!is.null(phys.map())) {
updateSelectizeInput(
session,
"physSpaceTissues",
choices = rownames(phys.map()),
server = TRUE
)
updateSelectizeInput(
session,
"experimentTissues",
choices = colnames(phys.map()),
server = TRUE
)
}
})
# Plot the results
plotPhysioHeatMap <- reactive({
if (!is.null(phys.map()) &&
is.matrix(phys.map())) {
pm.cols <-
allOrSelection(input$experimentTissues, colnames(phys.map()))
pm.rows <-
allOrSelection(input$physSpaceTissues, rownames(phys.map()))
PhysioScores = phys.map()[pm.rows, pm.cols, drop = FALSE]
if(any(is.infinite(range(PhysioScores)))){
Mx <- max(PhysioScores[is.finite(PhysioScores)])
Mn <- min(PhysioScores[is.finite(PhysioScores)])
PhysioScores[PhysioScores > Mx] <- 2*Mx
PhysioScores[PhysioScores < Mn] <- -2*abs(Mn)
}
p.h <- PhysioHeatmap(
PhysioResults = PhysioScores,
main = "PhysioSpace Heatmap",
SymmetricColoring = TRUE,
SpaceClustering = TRUE,
# The PhysioMap's rows are the columns of the PhysioSpace:
Space = get(input$physSpace)[, pm.rows, drop = FALSE],
ReducedPlotting = (if (!is.null(input$reducedPlotting) &&
as.integer(input$reducedPlotting) > 0) {
as.integer(input$reducedPlotting)
} else
FALSE)
)
# See stackoverflow.com/questions/27276994/outputting-shiny-non-ggplot-plot-to-pdf
dev.copy2pdf(file = "/home/shiny/plot.pdf")
p.h
}
})
# Make plot available in output:
output$physSpacePlot <-
renderPlot({
plotPhysioHeatMap()
})
# Enable downloading of results-table:
output$downloadPhysSpaceMap <- downloadHandler(
filename = function() {
"physioSpace_Results.csv"
},
content = function(file) {
write.table(
phys.map(),
file,
row.names = TRUE,
sep = ",",
quote = FALSE
)
}
)
# Enable downloading of the Plot in PDF:
output$downloadPhysioHeatMap <- downloadHandler(
filename =
'physioHeatMap.pdf'
,
content = function(file) {
# See stackoverflow.com/questions/27276994/outputting-shiny-non-ggplot-plot-to-pdf
file.copy("/home/shiny/plot.pdf", file)
},
contentType = 'application/pdf'
)
# Enable the user to start the computation, if all required input has been provided:
output$receivedAllInput <- reactive({
isValid(input$upGenes) &&
isValid(input$downGenes) || isValid(input$file1$datapath)
})
outputOptions(output, 'receivedAllInput', suspendWhenHidden = FALSE)
# Signal success in computation:
output$done <- reactive({
if (!is.null(phys.map()))
'true'
else
'false'
})
outputOptions(output, 'done', suspendWhenHidden = FALSE)
}
# Start the shiny server
shinyApp(ui = ui, server = server)