-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlive.R
366 lines (327 loc) · 11.4 KB
/
live.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
#' @import shiny
#' @import ggplot2
selectProtein <- function(tab, input, max_hover=1) {
sel <- NULL
tab_idx <- as.numeric(input$allProteinTable_rows_selected)
if(!is.null(input$plot_brush)){
brushed <- na.omit(brushedPoints(tab, input$plot_brush))
sel <- as.numeric(rownames(brushed))
} else if(!is.null(input$plot_hover)) {
near <- nearPoints(tab, input$plot_hover, threshold = 20, maxpoints = max_hover)
sel <- as.numeric(rownames(near))
} else if(length(tab_idx) > 0) {
sel <- tab_idx
}
return(sel)
}
#' Protein information
#'
#' Internal function to create a rendered table with protein annotations.
#'
#' @param tab Table used to create a plot
#' @param input Input variable from shiny server
#' @param pdat \code{proteusData} object with data and annotations
#' @param max_points Maximum number of points to select
#'
#' @return Rendered table with protein(s) annotations
proteinInfo <- function(tab, input, pdat, max_points) {
renderTable({
sel <- selectProtein(tab, input)
if(!is.null(sel)) {
n <- length(sel)
if (is.null(pdat$annotation)){
data.frame(Error='No annotation found on the Proteus object. Consult vignette.')
} else {
if (n >= 1 && n <= max_points && sel > 0) {
data.frame(pdat$annotation[sel, ])
} else if (n > max_points) {
data.frame(Error=paste('Only', max_points, 'points can be selected.'))
}
}
}
})
}
#' Replicate table
#'
#' Internal function to create a rendered table with replicate intensities. When
#' multiple points are selected, the returned table contains mean values. If
#' \code{intensityScale} in the input is 'log', data are log-10 transformed.
#'
#' @param tab Table used to create a plot
#' @param input Input variable from shiny server
#' @param pdat \code{proteusData} object with data and annotations
#' @param max_points Maximum number of points to select
#'
#' @return Rendered table with replicate intensities.
replicateTable <- function(tab, input, pdat, max_points) {
renderTable({
sel <- selectProtein(tab, input)
if(!is.null(sel)) {
dat <- pdat$tab[sel,, drop=FALSE]
if(input$intensityScale == 'Log'){
dat <- log10(dat)
}
if(length(sel) <= max_points) {
df <- data.frame(Sample=colnames(dat), Intensity=signif(colMeans(dat, na.rm = TRUE), 3))
df$Intensity[is.nan(df$Intensity)] <- NA
df
}
}
}, width = "80px")
}
#' Significance table
#'
#' @param tab Table used to create a plot
#' @param res Result from limma
#' @param input Input variable from shiny server
#'
#' @return A rendered table with p-value and adjusted p-value
significanceTable <- function(tab, res, input) {
renderTable({
sel <- selectProtein(tab, input)
if(!is.null(sel) && length(sel) == 1) {
data.frame(`P-value`=sprintf("%.2g", res$P.Value[sel]), `adjusted P-value`=sprintf("%.2g", res$adj.P.Val[sel]), check.names = FALSE)
}
}, width = "100px")
}
#' Jitter plot
#'
#' Internal function to create a jitter plot with replicate intensities versus
#' condition.
#'
#' @param tab Table used to create a plot
#' @param input Input variable from shiny server
#' @param pdat \code{proteusData} object with data and annotations
#' @param max_points Maximum number of points to select
#'
#' @return A ggplot2 object.
jitterPlot <- function(tab, input, pdat, max_points) {
renderPlot({
sel <- selectProtein(tab, input)
if(!is.null(sel) && length(sel) <= max_points) {
dat <- pdat$tab[sel,, drop=FALSE]
if(input$intensityScale == 'Log'){
dat <- log10(dat)
}
m <- colMeans(dat, na.rm = TRUE)
s <- apply(dat, 2, function(x) sd(x, na.rm = TRUE) / sqrt(na.omit(length(x))))
n <- length(sel)
p <- data.frame(
intensity = m,
lo = m - s,
up = m + s,
condition = factor(pdat$metadata$condition, levels=pdat$conditions),
replicate = as.factor(pdat$metadata$replicate)
)
p$shape <- rep(21, length(p$intensity))
p$shape[which(p$intensity==0)] <- 24
pd <- ggplot2::position_dodge(width = 0.4)
ggplot(p, aes_(x=~condition, y=~intensity, ymin=~lo, ymax=~up, colour=~replicate, shape=~shape, fill=~replicate)) +
theme(text = element_text(size=20), legend.position = "none", legend.direction = "horizontal") +
{if (input$intensityScale == '') ylim(0, NA)} +
geom_point(position=pd, size=4, na.rm=TRUE) +
{if(n > 1) geom_errorbar(position=pd, width = 0.1)} +
scale_shape_identity() + # necessary for shape mapping
viridis::scale_fill_viridis(discrete=TRUE) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5)) +
{if (input$intensityScale == 'Log') labs(x = 'Condition', y = 'Log Intensity') else labs(x = 'Condition', y = 'Intensity')}
}
})
}
#' All protein table
#'
#' Internal function to create a table with all proteins (bottom of the web page).
#'
#' @param res A data frame with results from limmaDE.
#'
#' @return A rendered table.
allProteinTable <- function(res) {
DT::renderDataTable({
# assume first column is id ("protein" or "peptide")
idcol <- names(res)[1]
cols <- c(idcol, "logFC", "adj.P.Val", grep("mean_", colnames(res), value=TRUE))
d <- res[, cols]
d[, 2:ncol(d)] <- sapply(d[, 2:ncol(d)], function(x) signif(x, 3))
d <- DT::datatable(d, class = 'cell-border strip hover')
DT::formatStyle(d, 0, cursor = 'pointer')
})
}
#' Volcano plot in live Shiny session
#'
#' \code{plotVolcano_live} makes a volcano plot from limma results.
#'
#' @param pdat Protein \code{proteusData} object.
#' @param res Result table from \code{\link{limmaDE}}.
#' @param max_points Maximum number of points that can be selected.
#'
#' @return A \code{shiny} session hosted locally.
#'
#' @examples
#' library(shiny)
#' library(proteusLabelFree)
#' data(proteusLabelFree)
#' prodat.med <- normalizeData(prodat)
#' res <- limmaDE(prodat.med)
#' \dontrun{
#' plotVolcano_live(prodat.med, res)
#' }
#'
#' @export
plotVolcano_live <- function(pdat, res, max_points=100){
if(!is(pdat, "proteusData")) stop ("Input data must be of class proteusData.")
res$"-log10(P.Value)" <- -log10(res$P.Value)
#######################################################################
ui <- shinyUI(fluidPage(
# Application title
titlePanel("PlotVolcano live"),
fluidRow(
column(5, plotOutput("plotVolcano", height = "600px", width = "100%", brush = "plot_brush",hover="plot_hover")),
column(7,
fluidRow(tableOutput("proteinInfo")),
fluidRow(
column(4,
radioButtons("intensityScale","Intesity Scale:",choices = c("Linear scale" = "","Log scale"="Log"),inline = TRUE)
)
),
fluidRow(
column(4,
fluidRow(plotOutput("jitterPlot", height = "300px",width = "100%")),
fluidRow(htmlOutput("gap")),
fluidRow(tableOutput("significanceTable"))
),
column(3,
fluidRow(tableOutput("replicateTable"))
)
)
),
fluidRow(
column(6, htmlOutput("proteinTable"))
)
),
# Show main protein table
fluidRow(
column(width = 12,
DT::dataTableOutput("allProteinTable"))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$proteinInfo <- proteinInfo(res, input, pdat, max_points)
output$gap <- renderUI({HTML('<br/>')})
output$replicateTable <- replicateTable(res, input, pdat, max_points)
output$significanceTable <- significanceTable(res, res, input)
output$jitterPlot <- jitterPlot(res, input, pdat, max_points)
# Volcano plot
output$plotVolcano <- renderPlot({
tab_idx <- as.numeric(input$allProteinTable_rows_selected)
pVol <- plotVolcano(res, binhex=FALSE)
if(length(tab_idx) > 0) {
pVol <- pVol + geom_point(data=res[tab_idx,], size=3, color='red')
}
pVol
})
output$allProteinTable <- allProteinTable(res)
}
# Run the application
shinyApp(ui = ui, server = server)
}
#' Fold-change intensity diagram in live Shiny session
#'
#' \code{plotFID_live} makes a log10 fold change versus log10 sum intensity plot,
#' usually known as MA plot.
#'
#' @param pdat Protein \code{proteusData} object.
#' @param res Result table from \code{\link{limmaDE}}.
#' @param max_points Maximum number of points that can be selected.
#'
#' @return A \code{shiny} session hosted locally.
#'
#' @examples
#' library(shiny)
#' library(proteusLabelFree)
#' data(proteusLabelFree)
#' prodat.med <- normalizeData(prodat)
#' res <- limmaDE(prodat.med,sig.level = 0.05)
#' \dontrun{
#' plotFID_live(prodat.med, res)
#' }
#'
#' @export
plotFID_live <- function(pdat, res, max_points=100){
if(!is(pdat, "proteusData")) stop ("Input data must be of class proteusData.")
res$"-log10(P.Value)" <- -log10(res$P.Value)
# Generate the fold-change/intensity dataset. The same as in the FID plot.
condMeans <- function(cond) {
m <- rowMeans(log10(pdat$tab)[,which(pdat$metadata$condition == cond), drop=FALSE], na.rm=TRUE)
m[is.nan(m)] <- NA
m
}
m1 <- condMeans(pdat$conditions[1])
m2 <- condMeans(pdat$conditions[2])
good <- !is.na(m1) & !is.na(m2)
fi <- data.frame(
id = rownames(pdat$tab),
x = (m1 + m2) / 2,
y = m2 - m1,
good = good
)
rownames(fi) <- 1:nrow(fi)
mx <- 1.1 * max(abs(fi$y), na.rm=TRUE)
m <- rbind(m1[!good], m2[!good])
fi[!good, "x"] <- colSums(m, na.rm=TRUE)
fi[!good, "y"] <- ifelse(is.na(m[1,]), mx, -mx)
#######################################################################
ui <- shinyUI(fluidPage(
# Application title
titlePanel("PlotFID live"),
fluidRow(
column(5, plotOutput("plotFID", height = "600px", width = "100%", brush = "plot_brush",hover="plot_hover")),
column(7,
fluidRow(tableOutput("proteinInfo")),
fluidRow(
column(4,
radioButtons("intensityScale","Intesity Scale:",choices = c("Linear scale" = "","Log scale"="Log"),inline = TRUE)
)
),
fluidRow(
column(4,
fluidRow(plotOutput("jitterPlot", height = "400px",width = "100%")),
fluidRow(htmlOutput("gap")),
fluidRow(tableOutput("significanceTable"))),
column(3,
fluidRow(tableOutput("replicateTable")))
)
),
fluidRow(
column(6, htmlOutput("proteinTable"))
)
),
# Show main protein table
fluidRow(
column(width = 12,
DT::dataTableOutput("allProteinTable"))
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$proteinInfo <- proteinInfo(fi, input, pdat, max_points)
output$gap <- renderUI({HTML('<br/>')})
output$replicateTable <- replicateTable(fi, input, pdat, max_points)
output$significanceTable <- significanceTable(fi, res, input)
output$jitterPlot <- jitterPlot(fi, input, pdat, max_points)
#FID plot
output$plotFID <- renderPlot({
tab_idx <- as.numeric(input$allProteinTable_rows_selected)
pFID <- plotFID(pdat,binhex=FALSE)
if (length(tab_idx) > 0){
pFID <- pFID + geom_point(data=fi[tab_idx,], size=3, color='red')
}
pFID
})
output$allProteinTable <- allProteinTable(res)
}
# Run the application
shinyApp(ui = ui, server = server)
}