-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.R
333 lines (293 loc) · 11.3 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
library(shinydashboard)
library(htmlwidgets)
library(shiny)
library(leaflet)
library(leaflet.extras)
library(ggplot2)
library(zeallot)
library(shinyBS)
library(shinyWidgets)
library(RColorBrewer)
library(d3heatmap)
coords.list <- list(readRDS(file = "data/coords1"), readRDS(file = "data/coords2"))
expr.data.list <- list(readRDS(file = "data/expr.data1"), readRDS(file = "data/expr.data2"))
dim.data.list <- list(readRDS(file = "data/dim.data1"), readRDS(file = "data/dim.data2"))
contrib.data <- readRDS(file = "data/contrib.data")
cell.data.list <- list(readRDS(file = "data/cell.data1"), readRDS(file = "data/cell.data2"))
gene.choices <- readRDS("data/gene.choices")
htmp <- readRDS("data/htmp")
htmp <- lapply(cell.data.list, function(x) {
corMat <- cor(t(x))
diag(corMat) <- NA
corMat <- as.data.frame(corMat)
return(corMat)
})
palette.select <- list("Spectral" = rev(brewer.pal(n = 9, name = "Spectral")),
"viridis" = "viridis", "magma" = "magma", "Blues" = "Blues", "Greens" = "Greens",
"Oranges" = "Oranges", "OrRd" = "OrRd", "Purples" = "Purples", "Reds" = "Reds",
"Jet" = c("darkblue", "cyan", "yellow", "red", "darkred"),
"GrRdBlk" = c("lightgray", "mistyrose", "red", "darkred", "black"),
"GrRd" = c("lightgray", "mistyrose", "red", "darkred"))
ui <- dashboardPage(
header = dashboardHeader(title = "Murine colon \ndata explorer"),
sidebar = dashboardSidebar(
width = 250,
column(width = 12,
selectInput(
inputId = "var",
label = "factor",
choices = colnames(dim.data.list[[1]]),
selected = "factor_1"),
selectizeInput(
inputId = "gene",
label = "gene",
choices = NULL),
selectInput(
inputId = "cell",
label = "cell",
choices = rownames(cell.data.list[[1]]),
selected = "Distal absorptive"),
sliderInput(
inputId = "alpha",
label = "Opacity", value = 1,
min = 0, max = 1, step = 0.01
),
sliderInput(
inputId = "maxcutoff",
label = "Max cutoff", value = 1,
min = 0.5, max = 1, step = 0.01
),
fluidRow(
column(width = 4, checkboxInput(
inputId = "revpal",
label = "reverse palette",
value = FALSE)),
column(width = 4, checkboxInput(
inputId = "scalealpha",
label = "opacity gradient",
value = FALSE))
),
actionBttn(inputId = "go", style = "bordered", size = "s", label = "Factor gene weights"),
actionBttn(inputId = "go2", style = "bordered", size = "s", label = "Value histogram"),
actionBttn(inputId = "go3", style = "bordered", size = "s", label = "Cell correlations"),
selectInput(
inputId = "pal",
label = "palette",
choices = names(palette.select),
selected = "Jet")
)
),
body = dashboardBody(
fluidRow(
tabBox(
title = "",
id = "tabset1", width = "100%",
tabPanel(
value = 1,
title = "d0 : naive",
column(width = 12, leafletOutput("map1", width = "100%", height = "800px"))),
tabPanel(
value = 2,
title = "Td14 : DSS-induced colitis",
column(width = 12, leafletOutput("map2", width = "100%", height = "800px")))
),
bsModal(
id = "modalExample",
title = "Gene weights", "go",
size = "large",
uiOutput("myUIOutput")),
bsModal(
id = "modalExample2",
title = "histogram", "go2",
size = "large",
plotOutput("histogram")),
bsModal(
id = "modalExample3",
title = "Cell-cell correlation plot", "go3",
size = "large",
d3heatmapOutput("corrplot", height = "700px", width = "750px"))
)
)
)
server <- function(input, output, session) {
updateSelectizeInput(session, 'gene', selected = "Muc2", choices = gene.choices, server = TRUE)
# Create reactive value (responds on input)
rv <- reactiveValues(lastBtn = "factor")
rv2 <- reactiveValues(curgene = "Muc2")
# Set last button to "gene" when a factor is selected
observeEvent(input$var, {
if (input$var > 0 ) {
rv$lastBtn = "factor"
}
})
# Set last button to "cell" when a cell type is selected
observeEvent(input$cell, {
if (input$cell > 0 ) {
rv$lastBtn = "cell"
}
})
# Set last button to "gene" when a gene is selected
observeEvent(input$gene, {
if (input$gene > 0 ) {
rv$lastBtn = "gene"
}
})
# Generate data
get_data <- reactive({
# Selected tab
i <- as.integer(input$tabset1)
# Get coordinates for selected tab
data <- coords.list[[i]]
# Collect data based on input type; gene, factor or cell type
if (rv$lastBtn == "gene") {
variable <- input$gene
if (input$gene %in% gene.choices) {
rv2$curgene <- variable
} else {
variable <- rv2$curgene
}
data[, paste0(variable, "_raw")] <- expr.data.list[[i]][variable, ]
data[, variable] <- expr.data.list[[i]][variable, ]
} else if (rv$lastBtn == "factor") {
variable <- input$var
data[, paste0(variable, "_raw")] <- dim.data.list[[i]][, variable]
data[, variable] <- dim.data.list[[i]][, variable]
} else if (rv$lastBtn == "cell") {
variable <- input$cell
data[, paste0(variable, "_raw")] <- cell.data.list[[i]][variable, ]
data[, variable] <- cell.data.list[[i]][variable, ]
}
# Clip data if max cut-off if specified, i.e. if max cut-off is lower than 1
if (input$maxcutoff < 1) {
data[, variable][data[, variable] > quantile(data[, variable], probs = input$maxcutoff)] <- quantile(data[, variable], probs = input$maxcutoff)
}
# Create a column with opacity values for each spot scaled by the value to be visualized
# The maximum opcaity value is set by alpha
# If no scaling is applied, all spots will have the same opacity specified by alpha
if (input$scalealpha) {
data[, "alpha"] <- scales::rescale(data[, variable], to = c(0, input$alpha))
} else {
data[, "alpha"] <- input$alpha
}
# reverse color palette if specified
if (input$revpal %in% c(T, F)) {
pal <- colorNumeric(
palette = palette.select[[input$pal]],
alpha = TRUE,
reverse = input$revpal,
domain = data[, variable])
pal_rev <- colorNumeric(
palette = palette.select[[input$pal]],
alpha = TRUE, reverse = !input$revpal,
domain = data[, variable])
}
return(list(data, variable, pal, pal_rev))
})
# Create a pop-up window to display gene contributions for factors
output$myUIOutput <- renderUI({
if (rv$lastBtn == "factor") {
plotOutput("contrib")
} else {
p("Select a factor to visualize to get gene weights!")
}
})
# generate gene contribution data for UIOutput
get_contrib <- reactive({
if (rv$lastBtn == "factor") {
vals <- sort(contrib.data[, input$var], decreasing = T)
df <- data.frame(gene = factor(names(vals)[1:40], levels = rev(names(vals)[1:40])), val = vals[1:40])
return(df)
}
})
# render gene contributions plot to be displayed in UIOutput
output$contrib <- renderPlot({
contrib <- get_contrib()
if (is.null(contrib)) {
ggplot()
} else {
ggplot(contrib, aes(gene, val)) +
geom_bar(stat = "identity", fill = "lightgray", color = "black") +
coord_flip() +
labs( x = "", y = "", fill = "gene\nweight") +
theme(panel.background = element_blank())
}
})
# render a histogram pop-up for the selected values to be visualzied
output$histogram <- renderPlot({
c(data, variable, pal, pal_rev) %<-% get_data()
ggplot(data, aes_string(paste0("`", variable, "_raw`"))) +
geom_histogram(fill = "lightgray", color = "black", bins = 30) +
theme(panel.background = element_blank()) +
labs(x = variable) +
geom_vline(aes(xintercept = input$maxcutoff*max(data[, paste0(variable, "_raw")]), color = "cutoff_threshold"), linetype = "longdash") +
scale_color_manual(values = c("cutoff_threshold" = "black"))
})
output$corrplot <- renderD3heatmap({
vals <- htmp[[as.integer(input$tabset1)]]
d3heatmap(vals,
opacity = 1,
col = rev(RColorBrewer::brewer.pal(n = 11, name = "RdBu")),
show_grid = FALSE,
na.rm = TRUE,
key = TRUE,
density.info = "none",
key.location = "tr",
keysize = 1,
key.title = "correlation")
})
# Create leaflets for two tabs and add tiles
# Output id is map1 and map2
lapply(1:2, function(i) {
output[[paste0("map", i)]] <- renderLeaflet({
m <- leaflet(width = 500, height = 500,
options = leafletOptions(crs = leafletCRS(crsClass = "L.CRS.Simple"), preferCanvas = TRUE)) %>%
addTiles(urlTemplate = paste0("map", i, "/tiles/{z}/{x}/{y}.png"),
options = tileOptions(continuousWorld = FALSE,
tms = TRUE,
tileSize = "256",
minZoom = "1",
maxZoom = "5")) %>%
setView(lat = 0, lng = 0, zoom = 1) %>%
#setMaxBounds(lng1 = 0, lat1 = 0, lng2 = 245.4, lat2 = -250.3) %>% # TODO: find a way to automatically set bounds around tiled image
addStyleEditor() %>%
addDrawToolbar(targetGroup = "draw",
polylineOptions = FALSE,
circleOptions = FALSE,
markerOptions = FALSE,
editOptions = editToolbarOptions(selectedPathOptions = selectedPathOptions())) %>%
fitBounds(lng1 = 0, lat1 = 0, lng2 = 256, lat2 = -256) %>%
addMiniMap(tiles = paste0("map", i, "/tiles/{z}/{x}/{y}.png"))
return(m)
})
})
# Listen to changes in zoom levels and updated of get_data()
toListen <- reactive({
list(input$map1_zoom, input$map2_zoom, get_data())
})
# Draw new circle markers when zoom levels are changed or when get_data() is updated
lapply(1:2, function(i) {
observeEvent(toListen(), {
c(data, variable, pal, pal_rev) %<-% get_data()
proxy <- leafletProxy(paste0("map", i))
radi = 1.7*2^(input[[paste0("map", i, "_zoom")]] - 1)
proxy %>%
clearMarkers() %>%
clearControls() %>%
addCircleMarkers(radius = radi,
lat = -coords.list[[i]]$pixel_y,
lng = coords.list[[i]]$pixel_x,
fillColor = pal(data[, variable]),
group = "Spots",
fillOpacity = data[, "alpha"],
stroke = FALSE) %>%
addLegend(pal = pal_rev,
className = "legendbox",
position = "topright",
title = variable,
values = data[, variable],
labFormat = labelFormat(transform = function(x) sort(x, decreasing = TRUE)))
})
})
}
# Run the application
shinyApp(ui = ui, server = server)