-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPCD10Cr_mutations_app.R
executable file
·250 lines (225 loc) · 8.22 KB
/
APCD10Cr_mutations_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
# Copyright 2022 Bryce Carson
# Author: Bryce Carson <[email protected]>
# URL: https://github.com/bryce-carson/APCD10Cr_Carson_2022
#
# APCD10Cr_mutations_app.R is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# APCD10Cr_mutations_app.R is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
library(ComplexHeatmap)
library(DBI)
library(DT)
library(RSQLite)
library(circlize)
library(shiny)
library(shinyWidgets)
library(tidyverse)
library(glue)
library(waiter)
options(shiny.reactlog = TRUE)
ht_opt$message <- FALSE
## TODO: document _for the people that didn't already read it twice_ that the database is a hard requirement!
## E.g.: ensure the database is fully downloaded before launching the app, and ensure that the db is in the same working directory as the *app.R file.
db <- dbConnect(RSQLite::SQLite(), "APCD10Cr_mutations_app_db_created_20211228.db")
metadata <- dbReadTable(db, "metadata")
# DataTable cannot render blob objects, so we need to ensure that we deselect
# them before rendering.
metadata_pretty <- metadata %>%
select(
-filenameList,
-replicates
)
makeAComplexHeatmap <- function(matrix, windowSize = 1) {
offset <- 1e-8
## NOTE: the maximum value from the range of rows containing conditionally
## deleterious mutations only.
cdrange <- sort(
c(1, max(range(
if (windowSize == 1) {
matrix[-seq(50, 950, length = 10), ]
} else {
matrix[-seq(5, 95, length = 10), ]
}
)))
)
aprange <- c(-0.025, 0, 0.025)
breaks <- sort(c(aprange, (max(aprange) + offset), 1, max(cdrange)))
colors <- c("Purple", "White", "Orange", "White", "White", "Black")
matrixColours <- circlize::colorRamp2(breaks, colors)
splitChromosomes <- if (windowSize == 1) {
rep(paste0("Chr ", letters[1:10]), 100) %>% sort()
} else {
rep(paste0("Chr ", letters[1:10]), 10) %>% sort()
}
return(Heatmap(matrix,
height = unit(24, "cm"),
cluster_columns = FALSE, cluster_rows = FALSE, col = matrixColours, show_heatmap_legend = TRUE, show_column_names = TRUE, use_raster = TRUE, split = splitChromosomes, gap = unit(3, "mm"), border = TRUE,
heatmap_legend_param = list(
at = breaks,
labels = c(min(aprange), "0", max(aprange), "NIL", "1", as.character(max(cdrange)))
)
))
}
server <- function(input, output, session) {
## Hide the initial loading waiter when the server idles for the first time
## after load.
waiter::waiter_preloader(html = spin_1(),
color = "#333e48",
image = "",
fadeout = TRUE,
logo = "")
fullApp_loadingScreen <- waiter::Waiter$new(
id = c("sojournDensityPlot",
"heatmapPopulationOne",
"heatmapPopulationTwo"),
hide_on_render = TRUE
)
##### OUTPUTS
## NOTE: the currently selected row is accessed with the
## `input$metadata_pretty_rows_selected` variable.
output$metadata_pretty <- DT::renderDataTable(metadata_pretty,
selection = "single",
options = list(
rownames = FALSE,
searchHighlight = TRUE
),
server = FALSE
)
output$sojournDensityPlot <- renderPlot({
req(input$metadata_pretty_rows_selected)
if (is.list(sojournDensityList()) != TRUE) {
print("sojournDensityList() is not a list. Check the reactive function, ensure the database is clean, etc.")
} else {
# print(class(sojournDensityList()))
# print(sojournDensityList())
base <- ggplot()
base <- base +
geom_point(
data = sojournDensityList(),
mapping = aes(x = position, y = meanDensity)
) +
geom_vline(xintercept = seq(50, 950, length = 10) * 1000, size = 0.5)
return(base)
}
})
output$heatmapPopulationOne <- renderPlot({
req(input$metadata_pretty_rows_selected)
return(heatmapList()[[1]] %>% makeAComplexHeatmap())
})
output$heatmapPopulationTwo <- renderPlot({
req(input$metadata_pretty_rows_selected)
return(heatmapList()[[2]] %>% makeAComplexHeatmap())
})
##### REACTIVES
sojournDensityList <- reactive({
req(input$metadata_pretty_rows_selected)
sojournDensityList_rawCxn <- dbGetQuery(db, "SELECT sojournDensityList FROM sojournDensities WHERE rowid = :selected_row_id",
params = list(selected_row_id = input$metadata_pretty_rows_selected)
)$sojournDensityList %>% unlist()
if (is.raw(sojournDensityList_rawCxn) == TRUE) {
sojournDensityList_deblobbed <- sojournDensityList_rawCxn %>% unserialize()
return(sojournDensityList_deblobbed)
} else {
print("Returned value from database query is not a raw / connection that can be unserialized.")
print(glue("Class of sojournDensityList_rawCxn: {class(sojournDensityList_rawCxn)}"))
}
})
heatmapList <- reactive({
# Requirements
req(input$replicate_selection)
req(input$metadata_pretty_rows_selected)
# SQLite query
# Must be a single filename
heatmap_List <- dbGetQuery(db, "SELECT heatmapList FROM heatmaps WHERE filename IS :filename",
params = list(filename = input$replicate_selection)
)$heatmapList[[1]] %>% unserialize()
return(heatmap_List)
})
##### EVENT OBSERVATIONS
observeEvent(input$metadata_pretty_rows_selected,
{
if (!is.null(input$metadata_pretty_rows_selected)) {
fullApp_loadingScreen$show()
## The filenames, as a named list.
parameter_set_filenames <- dplyr::slice(metadata, input$metadata_pretty_rows_selected) %>%
pull(filenameList) %>%
chuck(1) %>%
unserialize() %>%
unlist() %>%
as.character() %>%
as.list()
names(parameter_set_filenames) <- letters[1:10]
## The random seed extracted from the filename.
parameter_set_seeds <- parameter_set_filenames %>%
str_extract("\\d{13}") %>%
as.list()
freezeReactiveValue(input, "replicate_selection")
updatePickerInput(session = session,
inputId = "replicate_selection",
choices = parameter_set_filenames,
choicesOpt = list(
subtext = paste(
"Random seed:",
parameter_set_seeds
)
),
options = pickerOptions(showSubtext = TRUE)
)
updateTabsetPanel(
inputId = "conditional_display",
selected = "sojourn_density"
)
} else {
updateTabsetPanel(
inputId = "conditional_display",
selected = "user_instruction"
)
}
},
ignoreNULL = FALSE
)
}
ui <- fluidPage(
## Use Waiter
waiter::useWaiter(),
## Application title
titlePanel("Antagonistic Pleiotropy (w/ Conditionally Deleterious Mutations)"),
# Data Table
fluidRow(column(12,
DT::dataTableOutput("metadata_pretty"))),
# Plots
## Sojourn Density Plot
fluidRow(column(12,
tabsetPanel(
id = "conditional_display",
type = "hidden",
tabPanelBody(
"user_instruction",
paste(
"Select a row of the metadata to view the",
"sojourn density for the selected parameter set,",
"and the heatmaps for a subsequently selected",
"replicate within that parameter set."
)
),
tabPanelBody(
"sojourn_density",
pickerInput(
inputId = "replicate_selection",
label = "Replicate",
choices = NULL
),
plotOutput("sojournDensityPlot"),
plotOutput("heatmapPopulationOne"),
plotOutput("heatmapPopulationTwo")
)
)))
)
shinyApp(ui, server)