-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.R
405 lines (360 loc) · 15.6 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
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
398
399
400
401
402
403
404
405
shinyServer(
function(input, output, session) {
#' Expression data
exprs <- reactive({
exprs <- switch(input$datasetType,
"Platform" = subset(datasets,Platform %in% input$platform),
"DataSource" = subset(datasets,DataSource %in% input$dataset)
)
exprs <- rmNA(exprs)
})
#' Return the available histology, to be used in the updateSelectInput
histo <- reactive({
levels(exprs()[,"Pathology"])
})
#' Return the available subtype, to be used in the updateSelectInput
subtype <- reactive({
if (input$histology == "All"){
df <- exprs()
subtype <- levels(df$Subtype)
} else{
df <- subset(exprs(), Pathology == input$histology)
subtype <- intersect(levels(df$Subtype),df$Subtype)
}
subtype
})
#' When switching datasets if the selected histo is not available it will choose "All"
histo_selected <- reactive ({
if (input$histology %in% c("All", histo())){
input$histology
} else {
"All"
}
})
#' When switching datasets if the selected subtype is not available it will choose "All"
subtype_selected <- reactive ({
if (input$subtype %in% c("All", subtype())){
input$subtype
} else {
"All"
}
})
observe({
updateSelectInput(session, inputId = "histology", choices = c("All", histo()), selected = histo_selected())
updateSelectInput(session, inputId = "subtype", choices = c("All", subtype()), selected = subtype_selected())
})
#' Text matching with the gene names list
updateSelectizeInput(session, inputId = "gene", choices = gene_names, server = TRUE)
plot_type <- reactive({
input$plotType
})
#' Generate a dataframe with the data to plot
data <- reactive({
validate(
need(input$gene != "", "Please, enter a gene name in the panel on the left")%then%
# Not all genes are available for all the dataset
need(input$gene %in% names(exprs()),"Gene not available for this platform")
)
mRNA <- exprs()[ ,input$gene]
data <- cbind(mRNA, exprs()[,1:19]) # To combine with pData
samples <- data[which(duplicated(data$PtID)),]$PtID
data <- data[data$PtID %in% samples,]
data
})
observeEvent(plot_type(), {
updateRadioButtons(session, inputId = "point_line", selected = "Box plot")
})
#' Populate Xaxis labels
observe({
updateTextInput(session, inputId = "myXlab",value = paste0("\n",plot_type()))
})
# Tukey plot active only when tukey stat data are shown
observeEvent(!input$tukeyHSD, {
updateCheckboxInput(session, inputId = "tukeyPlot", value = FALSE)
})
observeEvent(input$point_line == 'Scatter plot', {
updateCheckboxInput(session, inputId = "tukeyPlot", value = FALSE)
updateCheckboxInput(session, inputId = "paired_tTest", value = FALSE)
updateCheckboxInput(session, inputId = "tTest", value = FALSE)
})
#' Reactive function to generate the box plots
box_Plot <- reactive({
data <- data()
xlabel <- paste("\n", input$myXlab)
ylabel <- paste(input$myYlab,"\n")
col <- input$colorP
shape <- input$shapeP
if(input$colorP == "None") {
col <- NULL
}
if(input$shapeP == "None") {
shape <- NULL
}
theme <- theme(axis.text.x = element_text(size = input$axis_text_size, angle = input$xaxisLabelAngle, hjust = ifelse(input$xaxisLabelAngle == 0,0.5,1)),
axis.text.y = element_text(size = input$axis_text_size),
legend.text = element_text(size = input$axis_text_size*0.8),
legend.title = element_text(size = input$axis_text_size*0.8),
axis.title.x = element_text(size = input$axis_title_size),
axis.title.y = element_text(size = input$axis_title_size))
p <- ggplot(data, mapping=aes_string(x=plot_type(), y = "mRNA")) +
geom_boxplot(outlier.size = 0, outlier.stroke = 0) +
geom_jitter(position = position_jitter(width = .25), mapping = aes_string(colour = col, shape = shape),
size = input$point_size, alpha = input$alpha) +
ylab(ylabel) + xlab(xlabel) + theme_bw() + theme
if(input$point_line == "Lines") {
p <- ggplot(data, mapping=aes_string(x=plot_type(), y = "mRNA", group="PtID", colour = col, shape = shape)) +
geom_line() +
geom_point(size = input$point_size) +
ylab(ylabel) + xlab(xlabel) + theme_bw() + theme
}
if(input$point_line == "Scatter plot") {
exp <- data %>% select(PtID,Progression,mRNA) %>% spread(Progression, mRNA)
if(input$colorP == "None") {
p <- ggplot(exp, aes(Initial, Recurrent)) +
geom_point(size = input$point_size) + geom_smooth(method = "lm", se = TRUE) +
geom_rug() + theme
} else {
group <- data %>% select_("PtID","Progression",input$colorP) %>% spread_("Progression",input$colorP)
names(group)[2:3] <- c(paste0(input$colorP,"_Initial"),paste0(input$colorP,"_Recurrence"))
data <- merge(exp,group,by="PtID")
p <- ggplot(data, aes(Initial, Recurrent)) +
geom_point(size = input$point_size) + geom_smooth(method = "lm", se = TRUE) +
geom_rug(sides="b", aes_string(colour = paste0(input$colorP,"_Initial"))) +
geom_rug(sides="l", aes_string(colour = paste0(input$colorP,"_Recurrence"))) +
theme + theme(legend.title = element_blank())
}
}
if (input$tukeyPlot) {
t <- tukey() %>%
mutate(comparison = row.names(.)) %>%
ggplot(aes(reorder(comparison, diff), diff, ymin = lwr, ymax= upr, colour = Significance)) +
geom_point() + geom_errorbar(width = 0.25) + ylab("\nDifferences in mean levels") + xlab("") +
geom_hline(yintercept = 0, colour="darkgray", linetype = "longdash") + coord_flip() + theme
p <- grid.arrange(p, t, ncol=2, widths = c(3,2))
}
return(p)
})
box_width <- reactive({
if(input$tukeyPlot)
input$plot_width* 1.8 else
input$plot_width
})
#' Create the selected plot
output$plot <- renderPlot({
box_Plot()
}, width = box_width, height = function()input$plot_height)
#' Data for the statistic
stat_data <- reactive({
mRNA <- data()[ ,"mRNA"]
group <- data()[ ,plot_type()]
data <- data.frame(mRNA, group)
data
})
#' Summary statistic
output$summary <- renderTable({
data <- stat_data()
stat <- data %>%
group_by(group) %>%
summarise(Sample_count = paste0(n()," (", round(n()*100/dim(data)[1], 2), "%)" ), # prop.table
median = median(mRNA, na.rm=T), mad = mad(mRNA, na.rm=T), mean = mean(mRNA, na.rm=T),
sd = sd(mRNA, na.rm=T)) %>%
data.frame()
row.names(stat) <- stat$group
tot <- data %>%
summarise(Sample_count = n(), median = median(mRNA, na.rm=T),
mad = mad(mRNA, na.rm=T), mean = mean(mRNA, na.rm=T), sd = sd(mRNA, na.rm=T))
stat <- stat[,-1]
stat <- rbind(stat,TOTAL = tot)
stat
}, rownames = TRUE, align='rrrrrr')
#' Tukey post-hoc test, to combine it with the boxplot and to render in a table
tukey <- reactive({
validate(
need(nlevels(stat_data()$group)>1,message = "There is only one category, group comparison cannot be performed")
)
data <- stat_data()
tukey <- data.frame(TukeyHSD(aov(mRNA ~ group, data = data))[[1]])
tukey$Significance <- as.factor(starmaker(tukey$p.adj, p.levels = c(.001, .01, .05, 1), symbols=c("***", "**", "*", "ns")))
tukey <- tukey[order(tukey$diff, decreasing = TRUE), ]
tukey
})
#' Render tukey
output$tukeyTest <- renderTable({
tukey()
},rownames = TRUE , digits = c(2,2,2,-1,2))
#' Pairwise t test
output$pairwiseTtest <- renderTable({
validate(
need(nlevels(stat_data()$group)>1,message = "There is only one category, group comparison cannot be performed")
)
data <- stat_data()
pttest <- pairwise.t.test(data$mRNA, data$group, na.rm= TRUE, p.adj = "bonferroni", paired = FALSE)
pttest$p.value
}, rownames = TRUE, digits = -1)
# Paired t-test active only when Progression plot are shown
observeEvent(plot_type() != "Progression", {
updateCheckboxInput(session, inputId = "paired_tTest", value = FALSE)
})
#' Paired t-test
output$pairedTtest <- renderTable({
req(plot_type() == "Progression")
data <- stat_data()
pttest <- broom::tidy(t.test(mRNA ~ group, data, paired=TRUE))
pttest
},rownames = TRUE)
#' Get the selected download file type.
download_plot_file_type <- reactive({
input$downloadPlotFileType
})
observe({
plotFileType <- input$downloadPlotFileType
plotFileTypePDF <- plotFileType == "pdf"
plotUnit <- ifelse(plotFileTypePDF, "inches", "pixels")
plotUnitDef <- ifelse(plotFileTypePDF, 7, 600)
plotUnitMin <- ifelse(plotFileTypePDF, 1, 100)
plotUnitMax <- ifelse(plotFileTypePDF, 12, 2000)
plotUnitStep <- ifelse(plotFileTypePDF, 0.1, 50)
updateNumericInput(
session,
inputId = "downloadPlotHeight",
label = sprintf("Height (%s)", plotUnit),
value = plotUnitDef, min = plotUnitMin, max = plotUnitMax, step = plotUnitStep)
updateNumericInput(
session,
inputId = "downloadPlotWidth",
label = sprintf("Width (%s)", plotUnit),
value = plotUnitDef, min = plotUnitMin, max = plotUnitMax, step = plotUnitStep)
})
#' Get the download dimensions.
download_plot_height <- reactive({
input$downloadPlotHeight
})
download_plot_width <- reactive({
input$downloadPlotWidth
})
#' Download the Plot
output$downloadPlot <- downloadHandler(
filename = function() {
paste0(Sys.Date(), "_", input$gene, "_", input$dataset, "_", input$plotTypeSel,
".", download_plot_file_type())
},
# The argument content below takes filename as a function and returns what's printed to it.
content = function(file) {
# Gets the name of the function to use from the downloadFileType reactive element.
plotFunction <- match.fun(download_plot_file_type())
plotFunction(file, width = download_plot_width(), height = download_plot_height())
if (input$tukeyPlot) {
grid.draw(box_Plot())
} else {
print(box_Plot())
}
dev.off()
}
)
#' Extract the survival data.
surv_data <- reactive({
df <- data()
# df <- subset(df, !is.na(df$status))
df <- subset(df,Progression == "Initial")
if (input$histology != "All"){
df <- subset(df, Pathology == input$histology)
}
if (input$subtype != "All") {
df <- subset(df, Subtype == input$subtype)
}
# exclude G-CIMP is selected
if (input$gcimpSurv){
df <- subset(df, GcimpPrediction != "GCIMP")
}
df
})
#' Create a slider for the manual cutoff of the Kaplan Meier plot
mRNA_surv <- reactive({
surv_need()
req(input$histology %in% c("All", histo()))
mRNA <- surv_data()[ ,"mRNA"]
mRNA.values <- round(mRNA[!is.na(mRNA)],2)
# Generate a vector of continuos values, excluding the first an last value
mRNA.values <- sort(mRNA.values[mRNA.values != min(mRNA.values) & mRNA.values != max(mRNA.values)])
})
#' Create a rug plot with the mRNA expression value for the manual cutoff
output$boxmRNA <- renderPlot({
req(input$mInput)
mRNA <- round(mRNA_surv(),2)
q <- quantile(mRNA)
xrange <-range(mRNA)
par(mar = c(0,0,0,0))
plot(0, 0, type = "n", xlim = c(xrange[1] + 0.25, xrange[2]) , ylim = c(-0.1, + 0.1), ylab ="", xlab = "", axes = FALSE)
points(x = mRNA, y = rep(0, length(mRNA)), pch="|", col=rgb(0, 0, 0, 0.25))
# Add a red line to show which is the current cutoff.
points(x = input$mInput, y = 0, pch = "|", col="red", cex = 2.5)
points(x = q[2:4], y = rep(0,3), pch = "|", col="blue", cex = 2)
}, bg = "transparent")
#' Generate the slider for the manual cutoff
output$numericCutoff <- renderUI({
sliderInput(inputId = "mInput",label = NULL, min = min(mRNA_surv()), max = max(mRNA_surv()),
value = median(mRNA_surv()), step = 0.05, round = -2)
})
#' Requirements for all the survival plots
surv_need <- reactive({
validate(
need(input$gene != "", "Please, enter a gene name in the panel on the left")%then%
need(input$gene %in% names(exprs()),"Gene not available for this dataset")
)
})
#' busy indicator when switching surv tab
#' http://stackoverflow.com/questions/18237987/show-that-shiny-is-busy-or-loading-when-changing-tab-panels
output$activeTabSurv <- reactive({
return(input$tabSurv)
})
outputOptions(output, 'activeTabSurv', suspendWhenHidden=FALSE)
#' Set survival plot height
surv_plot_height <- reactive({
if(input$allSubSurv){
ifelse(length(subtype())>4, 1300, 650)
} else {
400
}
})
#' Create a Kaplan Meier plot with cutoff based on quantiles or manual selection
output$survPlot <- renderPlot({
surv_need ()
req(input$histology %in% c("All", histo()))
# Use 'try' to suppress a message throwed the first time manual cutoff is selected
if(input$allSubSurv) {
nrow <- ceiling(length(subtype())/2)
par(mfrow = c(nrow,2), mar=c(3.5,3.5,3.5,1.5), mgp=c(2.2,.95,0))
try({
for (i in subtype()) {
survivalPlot(surv_data(), surv_type = input$surv_type, gene = input$gene, group = input$histology, subtype = i,
cutoff = input$cutoff, numeric = input$mInput, cex = 1.2)
}
}, silent = TRUE)
} else {
try(survivalPlot(surv_data(), surv_type = input$surv_type, gene = input$gene, group = input$histology, subtype = input$subtype,
cutoff = input$cutoff, numeric = input$mInput), silent = TRUE)
}
}, height = surv_plot_height, width = function(){if(!input$allSubSurv) {500} else {900}})
#' Download the survPlot
output$downloadsurvPlot <- downloadHandler(
filename = function() {
paste0(Sys.Date(), "_", input$gene, "_", input$dataset, "_survPlot.", download_plot_file_type())
},
content = function(file) {
plotFunction <- match.fun(download_plot_file_type())
plotFunction(file, width = download_plot_width(), height = download_plot_height())
if(input$allSubSurv) {
nrow <- ceiling(length(subtype())/2)
par(mfrow = c(nrow,2), mar=c(3.5,3.5,3.5,1.5), mgp=c(2.2,.95,0))
for (i in subtype()) {
survivalPlot(surv_data(),surv_type = input$surv_type, gene = input$gene, group = input$histology, subtype = i,
cutoff = input$cutoff, numeric = input$mInput, cex = 1.2)
}
} else {
survivalPlot(surv_data(), surv_type = input$surv_type, gene =input$gene, group = input$histology, subtype = input$subtype,
cutoff = input$cutoff, numeric = input$mInput)
}
dev.off()
}
)
})