-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.R
374 lines (335 loc) · 16.6 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
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
#
# This is a ShinyActivePathways.
# A Shiny App runs ActivePathways and visualize data in enrichment map.
# You can run the application by clicking
# the 'Run App' button above.
#
library(shiny)
library(ActivePathways)
library(visNetwork) # For interactive Network
library(shinyWidgets) # For some fancy shiny component
library(shinyscreenshot)
library(shinyalert) # For dialogue
library(tools)
library(dplyr)
library(spsComps) # For error handling
source(file = "./enrichmentMap/enrichmentMap.R")
source(file = "./color/color.R")
source(file = "./documentation/documentation.R")
options(shiny.maxRequestSize = 100*1024^2) # define maximum upload file size
# Define UI for application that draws a histogram
ui <- fluidPage(
includeCSS("www/styles.css"), # include css file
# naviation bar
navbarPage(title = "ShinyActivePathways",
collapsible = TRUE,
tabPanel("Visualization",
# side bar
sidebarLayout(
sidebarPanel(
tabsetPanel(
tabPanel("Input Files", wellPanel(
tags$p(
"ShinyActivePathways can run ActivePathways method for
enrichment analysis and visualize result as enrichment map"
),
tags$p(
"Upload scoresFile and gmtFile, then click 'Run ActivePathways!'"
),
fileInput(inputId = "scoresFile",
label = "Score file",
accept = c(
".tsv",
".csv"
)
),
fileInput(inputId = "gmtFile",
label = "GMT file",
accept = c(
".gmt"
)
),
actionButton(inputId = "runPanel",
label = "Run ActivePathways!") %>%
bsTooltip("Run ActivePathways with Parameters", "bottom", bgcolor = "#219ebc"),
actionButton(inputId = "runEx",
label = "Run Example") %>%
bsTooltip("Run ActivePathways with an Example", "bottom", bgcolor = "#ffb703"),
# sliderInput(inputId = "size", "Label Size: ", min = 0, max = 30, value = 14),
)),
tabPanel("Adjust", wellPanel(
shinyWidgets::sliderTextInput(
inputId = "size",
label = "Font Size: ",
seq(from = 0, to = 28),
selected = 14
),
sliderInput(
inputId = "edgeCutoff",
label = "Edge cutoff (Similarity): ",
min = 0.25,
max = 1.0,
value = 0.25
),
numericInput("edgePrecise",
label = "Value:",
value = 0.25,
min = 0.25,
max = 1.0),
checkboxInput("highlightNeighbors",
label = "highlight neighbors"),
checkboxInput("navigationButtons",
label = "hide navigation buttons"),
checkboxInput("manipulation",
label = "enable manipulation")
),
),
tabPanel("Download", wellPanel(
downloadLink('downloadNetwork', 'Download network as .html'),
)
)
)
),
# Main panel
mainPanel(
wellPanel(
visNetworkOutput(outputId = "visNet",
width = "100%",
height = "500px"),
),
hr(),
verbatimTextOutput('geneList')
)
)
),
tabPanel("Documentation",
wellPanel(
tags$div(
HTML(documentHTML)
)
))
)
)
# Define server logic
server <- function(input, output, session) {
# Parameters for ActivePathways
observeEvent(input$runPanel, {
shinyalert::shinyalert(html = TRUE, text = tagList(
div(style="display:inline-block",
textInput(inputId = "cutoff", value = 0.1, label = "Cutoff")),
div(style="display:inline-block",
textInput(inputId = "significant", value = 0.05, label = "Significant")),
selectInput(inputId = "mergeMethod", choices = c("Brown", "Fisher"), selected = "Brown", label = "Merge Method"),
selectInput(inputId = "correctionMethod", choices = c("holm",
"fdr",
"hochberg",
"hommel",
"bonferroni",
"BH",
"BY",
"none"),
selected = "holm",
label = "Correction Method"),
sliderInput(inputId = "filterRange",
"Geneset Filter:",
min = 0, max = 1500,
value = c(5, 1000)),
hr(),
selectInput(inputId = "metric",
choices = c("jaccard", "overlap", "combined - 50% each"),
selected = "jaccard",
label = "Overlap measures"),
type = "input"
))
})
# Uploads score files
scores <- reactive({
print(input$scoresFile)
preTable <- NULL
if (file_ext(input$scoresFile$datapath) == "tsv") {
preTable <- read.table(file = input$scoresFile$datapath, header = TRUE, sep = '\t', row.names = 1)
} else if (file_ext(input$scoresFile$datapath) == "csv") {
preTable <- read.table(file = input$scoresFile$datapath, header = TRUE, sep = ',', row.names = 1)
}
data <- as.matrix(preTable)
data[is.na(data)] <- 1
return(data)
})
# Uploads gmt file
gmt <- reactive({
return(read.GMT(input$gmtFile$datapath))
})
enrichmentResult <- reactiveValues(data = NULL)
enrichNetwork <- reactiveValues(data = NULL)
# Render visNetwork
output$visNet <- renderVisNetwork({
if (! is.null(enrichNetwork$data)) {
enrichNetwork$data %>%
visInteraction(navigationButtons = TRUE, multiselect = TRUE) %>%
visEvents(select = "function(nodes) {
Shiny.onInputChange('current_node_id', nodes.nodes);
;}") # for display genes contained in each gene set
}
})
gmtDataRAW <- reactiveValues(value = NULL)
# For display genes contained in each gene set
selectedPathwayId <- reactiveValues(data = NULL)
observeEvent(input$current_node_id, {
print(paste("The selected node ID is:", input$current_node_id))
gmtData <- getDataFromGMT(gmtDataRAW$value)
selectedPathwayId$data <- gmtData$ids[gmtData$pathwayNames == input$current_node_id]
print(paste("The pathway ID is:", selectedPathwayId$data))
})
# Render information of gene list in text
output$geneList <- renderPrint({
if (! is.null(selectedPathwayId$data)) {
gmtDataRAW$value[[selectedPathwayId$data]]
}})
# Modify network properties
observe({
visNetworkProxy("visNet") %>%
visNodes(font = list(size = input$size)) %>% # for change font size of network
visOptions(highlightNearest = input$highlightNeighbors) %>% # for highlight neighbors
visInteraction(navigationButtons = !input$navigationButtons) %>%
visOptions(manipulation = input$manipulation)
# Download network as html files
output$downloadNetwork <- downloadHandler(
filename = function() {
paste('network-', Sys.Date(), '.html', sep='')
},
content = function(con) {
enrichNetwork$data %>% visSave(con)
}
)
})
# Run ActivePathways
fullResult <- reactiveValues(nodes = NULL, edges = NULL)
observeEvent(input$shinyalert, {
if (input$shinyalert) {
shinyCatch(
gmtDataRAW$value <- gmt()
, blocking_level = "error"
)
shinyCatch(
enrichmentResult$data <- ActivePathways(score = scores(),
gmt = gmt(),
cutoff = as.numeric(input$cutoff),
significant = as.numeric(input$significant),
merge.method = input$mergeMethod,
correction.method = input$correctionMethod,
geneset.filter = input$filterRange)
, blocking_level = "error")
}
g <- NULL
shinyCatch(
g <- plotEnrichmentMap(gmt(),
enrichmentResult$data,
algorithm = input$metric,
similarityCutoff = 0.25,
pvalueCutoff = NULL,
k = 0.5)
, blocking_level = "error")
visigraph <- visNetwork::visIgraph(g)
nodes <- visigraph$x$nodes
nodes$color <- getColors(enrichmentResult$data)
edges <- visigraph$x$edges
edges$id <- paste0("e", seq_along(edges$from))
edges$color <- rgb(137,207,240, max = 255)
fullResult$nodes <- nodes
fullResult$edges <- edges
enrichNetwork$data <- visNetwork(nodes, edges) %>%
visIgraphLayout() %>%
visExport(type = "png", name = "export-network",
float = "left", label = "Save network", style= "")
})
# Hardcoded example
observeEvent(input$runEx, {
nodes <- data.frame(id = c("DAP12 signaling",
"Axon guidance",
"Signaling by EGFR",
"Cellular Senescence"))
nodes$size <- c(21.90, 31.75, 22.30, 13.80)
nodes$color <- c("#FF0000", "#FFFFB3", "#FF6D00", "#FF2400")
nodes$label <- c("DAP12 signaling",
"Axon guidance",
"Signaling by EGFR",
"Cellular Senescence")
nodes$x <- c(-0.86622341, -1.00000000, -0.71840501, 0.91650067)
nodes$y <- c(-0.5461513 -0.3337750 -0.4780640 -1.0000000)
edges <- data.frame(from = c("DAP12 signaling",
"DAP12 signaling",
"Axon guidance"
))
edges$to <- c("Axon guidance",
"Signaling by EGFR",
"Signaling by EGFR"
)
edges$weight <- c(0.4111283, 0.8952880, 0.4039634)
edges$width <- c(0.8222566, 1.7905759, 0.8079268)
edges$id <- paste0("e", seq_along(edges$from))
edges$color <- rgb(137,207,240, max = 255)
fullResult$nodes <- nodes
fullResult$edges <- edges
enrichNetwork$data <- visNetwork(nodes, edges) %>%
visIgraphLayout() %>%
visExport(type = "png", name = "export-network",
float = "left", label = "Save network", style= "")
gmtDataRAW$value <- list()
lstItem1 <- list()
lstItem1$id <- "REAC:2424491"
lstItem1$name <- "DAP12 signaling"
lstItem1$genes <- c("IL17RD", "PSMC1", "PDGFRB", "PSMD14", "TNRC6C",
"CD80", "DUSP10", "(Not showing full list for example)")
lstItem2 <- list()
lstItem2$id <- "REAC:177929"
lstItem2$name <- "Signaling by EGFR"
lstItem2$genes <- c("ADCY3", "HRAS", "PSMC4", "TLN1", "MLST8", "THEM4",
"LAMTOR2", "CALM3", "RASGRF1", "DUSP7", "PSMC2",
"(Not showing full list for example)")
lstItem3 <- list()
lstItem3$id <- "REAC:422475"
lstItem3$name <- "Axon guidance"
lstItem3$genes <- c("SEMA3E", "PSME4", "FGF10", "TIAM1", "RGMA", "JAK1",
"CD72", "GRB7", "COL6A5", "PSMC2", "SRGAP2", "LAMTOR2",
"(Not showing full list for example)")
lstItem4 <- list()
lstItem4$id <- "REAC:2559583"
lstItem4$name <- "Cellular Senescence"
lstItem4$genes <- c("E2F3", "MDM4", "MDM2", "HIST3H3", "CDC27", "CBX2",
"ACD", "CDK2", "CBX8", "KDM6B", "TNRC6B",
"(Not showing full list for example)")
gmtLst <- list()
gmtLst$"REAC:2424491" <- lstItem1
gmtLst$"REAC:177929" <- lstItem2
gmtLst$"REAC:422475" <- lstItem3
gmtLst$"REAC:2559583" <- lstItem4
gmtDataRAW$value <- gmtLst
})
# edge Cutoff logic
observe({
updateSliderInput(
session = session,
inputId = "edgeCutoff",
value = input$edgePrecise
)
})
observe({
updateSliderInput(
session = session,
inputId = "edgePrecise",
value = input$edgeCutoff
)
})
observe({
filteredResult <- fullResult$edges[fullResult$edges$weight < input$edgeCutoff, ]
print(filteredResult)
filteredEdges <- filteredResult$id
hiddenEdges <- fullResult$edges[! fullResult$edges$id %in% filteredEdges,]
if (!is.null(filteredResult) & !is.null(filteredEdges)) {
visRemoveEdges(visNetworkProxy("visNet"), id = filteredEdges)
visUpdateEdges(visNetworkProxy("visNet"), edges = hiddenEdges)
}
})
}
# Run the application
shinyApp(ui = ui, server = server)