-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
426 lines (294 loc) · 13.3 KB
/
Copy pathapp.R
File metadata and controls
426 lines (294 loc) · 13.3 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#A GUI tool to nominate drug combinations from CRISPR+drug screens and other external resources
#charlie.wright@stjude.org | @biowright
#Load libraries
library(shiny)
library(shiny.semantic)
library(semantic.dashboard)
library(plotly)
library(dplyr)
library(stringr)
library(DT)
library(googlesheets4)
library(readr)
library(shinyalert)
library(shinybusy)
gs4_deauth() #Needed so that googlesheets4 skips requiring an authenticated Google user
ui <- dashboard_page(
dashboardHeader(title=actionButton(inputId = "load_button",
label="Load data",
style="background-color: #8bc9f0; border-color: #2e6da4"),
show_menu_button = TRUE,
titleWidth = "thin"),
dashboardSidebar(
side = "left",
size = "wide",
sidebarMenu(
HTML("<br>"), #Space at the top
fluidRow(
column(width=16,
align="center",
offset=3,
shiny::sliderInput("effectSize", #Needs to be direct from shiny, because shiny.semantic is buggy when sliderInput is inside sidebarMenu
HTML("<b>Minimum effect size</b>"),
value = -0.14,
min = -0.4073,
max = 0.382))), #End fluidRow
HTML("<br><br>"),
splitLayout(
selectInput(inputId ="stat_metric",
label = "Choose statistic option):", #Select metric for cutoff (FDR or p-value)
choices = c("p-value", "FDR"), width = "98%"),
numericInput(inputId="stat_alpha", #Select alpha value (for corresponding selected stat metric)
label= paste0("Significance Threshold (", "\u03B1", ")"),
value=0.05,
min=0,
max=1,
step=0.01,
width = "98%" ),
#Background color and height of splitLayout box
style = "background-color:#FFFFFF; height:100px;"),
HTML("<br>"),
#Check box to cap the number of known drugs per gene
fluidRow(
column(width=16,
align="center",
offset=3,
checkbox_input(input_id="capdrugs",
label="Limit maximum drugs per gene?",
is_marked=FALSE,
type = ""),
#Display popup with info when icon is clicked
useShinyalert(), #Required for popup
actionLink("info_button", "", icon = icon("info")),
), #End column
), #End fluidRow
#If the 'capdrugs' box is checked, Show the corresponding slider
conditionalPanel(
condition = "input.capdrugs == 1", #NOTE for condition this is ==1 and not ==TRUE
fluidRow(
column(width=16,
align="center",
offset=3,
shiny::sliderInput(inputId = "max_drugs", #Needs to be direct from shiny, because shiny.semantic is buggy when sliderInput is inside sidebarMenu
HTML("<b>Max drugs per gene</b>"),
value = 10,
min = 1,
max = 63,
step=1)))
),#End conditionalPanel
HTML("<br><br><br>"),
checkbox_input(input_id = "na_checkbox",
label="Remove control gRNAs",
is_marked = TRUE),
checkbox_input(input_id = "cancer_drugs_checkbox",
label="Only show cancer drugs",
is_marked = FALSE)
)#End sidebarMenu)
),#End dashboardSidebar
dashboardBody(
add_busy_bar(color = "forestgreen", centered=TRUE, height="10px"), #Add busy bar here
#Display number of total combinations
fluidRow(
column(
width=16,
align="center", offset=3,
htmlOutput("mytext")
)
),
fluidRow(
box(plotlyOutput("plot1", height = 850), width = 8),
tabBox(
tabs = list(
list(menu="Plots", content=column(width=16,plotlyOutput("plot2", height=400),
plotlyOutput("plot3", height=400))),
list(menu="Table", content=DT::dataTableOutput("mytable", height = 800), width = 8),
list(menu="DepMap Data", content=column(width=16,plotlyOutput("plot4", height=700))))
)
,
)
),
theme = "cyborg"
)
server <- shinyServer(function(input, output, session) {
#Create an eventReactive which loads in the google sheet data after the load_button is pressed:
mysheetreactive <- eventReactive(input$load_button, {
mydf <- as.data.frame(read_sheet("https://docs.google.com/spreadsheets/d/1yGP9HJllTrkLbCEn2YcmdHf0UvjfEx8L90VHFMMjp0A/edit?usp=sharing"))
#Read in the 'cancer drugs'
#CHEMBL database, antineoplastic agents, phase 3 or higher, small molecules
#NOTE: There are *definitely* a few discrepencies due to drug synonyms, take with a slight grain of salt
cancer_drugs <- as.data.frame(read_sheet("https://docs.google.com/spreadsheets/d/12ZvjQMUspSaXzEZfhMPJbCc0kj2SJMGcnzaBRakvt6k/edit?usp=sharing"))
depmap_data <- as.data.frame(read_sheet("https://docs.google.com/spreadsheets/d/11NX3jelUwtEfDAodaPpsNxS0QplMEWn9dyLr0vc3WQw/edit?usp=sharing"))
return(list(mydf=mydf,
cancer_drugs=cancer_drugs,
depmap_data=depmap_data))
})#End eventReactive
#Make a reactive that adjusts data according to thresholds
myreactive <- reactive({
req(mysheetreactive()$mydf)
req(mysheetreactive()$cancer_drugs)
req(mysheetreactive()$depmap_data)
req(input$stat_metric)
req(input$stat_alpha)
req(input$max_drugs)
mydf <- mysheetreactive()$mydf
cancer_drugs <- mysheetreactive()$cancer_drugs
depmap_data <- mysheetreactive()$depmap_data
#mydf2 will be initial adjusted data
if(input$stat_metric=="p-value"){
mydf2 <- mydf[mydf$NB_effectSize <= input$effectSize &
mydf$NB_Sensitizing_P <= input$stat_alpha,]
}else if(input$stat_metric=="FDR"){
mydf2 <- mydf[mydf$NB_effectSize <= input$effectSize &
mydf$NB_Sensitizing_FDR <= input$stat_alpha,]
}
mydf3 <- mydf2 %>%
group_by(tx, Gene) %>%
summarize(known_drug=unique(unlist(str_split(known_drug, pattern = "\\|"))),
count=n(),
concat=paste0(tx, "_", Gene))
#set max colors
max_drugs_per_gene <- input$max_drugs
#grab the max drugs for each 'concat' (treatment_Gene)
group1 = mydf3 %>%
group_by(concat) %>%
filter(row_number() <= max_drugs_per_gene) %>%
ungroup() %>%
arrange(concat)
#Put the last part into 'if' statement making sure there is at least 1 row in mydf3
#If not, make mydf4 NULL otherwise there will be errors in the app
if(nrow(group1>=1)){
#identify those with unique drugs not in group1
unique_drugs = mydf3 %>%
filter(!known_drug %in% group1$known_drug)
# bind group1 to unique_colors (unique_colors goes first)
# and keep the "max_colors" number of rows for each name
mydf4 = bind_rows(unique_drugs, group1) %>%
group_by(concat) %>%
filter(row_number() <= max_drugs_per_gene) %>%
ungroup() %>%
arrange(concat)
#Make a second concat column
mydf4$concat2 <- paste0(mydf4$tx, "_", mydf4$known_drug)
#Remove duplicate entires of concat2 (tx_knowndrug)
mydf4 <- mydf4[!duplicated(mydf4$concat2),]
mydf4 <- subset(mydf4, select= -c(count))
} else {
mydf4 <- NULL
}
#If 'na_checkbox' is checked, remove entries where Gene contains the string "controlGene" (aka this is a control non-targeting gRNA)
#NOTE: This is specific to this dataset; This will not work if control names are changed to "nt_gRNA1", etc.
if(input$na_checkbox==TRUE){
mydf4 <- mydf4[!grepl("controlGene", mydf4$Gene),]
} else {
mydf4 <- mydf4
}
#If 'cancer_drugs_checkbox' is checked,
#Remove all non-cancer drugs (CAVEATS, see above about chembl data)
if(input$cancer_drugs_checkbox==TRUE){
mydf4 <- mydf4[mydf4$known_drug %in% cancer_drugs$known_drug,]
} else {
mydf4 <- mydf4
}
depmap_data2 <- merge(mydf4, depmap_data, by="Gene")
return(list(mydf2=mydf2,
mydf3=mydf3,
mydf4=mydf4,
depmap_data2=depmap_data2))
}) #End myreactive
output$plot1 <- renderPlotly({
if(input$stat_metric=="p-value"){
p <- ggplot(mysheetreactive()$mydf, aes(x=NB_effectSize, y=-log10(NB_Sensitizing_P), text=Gene)) +
geom_point(color="gray76") +
facet_wrap(~tx, ncol=2) +
xlab("NB Effect Size") +
ylab("-Log10(p-value)") +
geom_hline(yintercept=-log10(input$stat_alpha)) +
geom_vline(xintercept=input$effectSize) +
theme(panel.border=element_rect(fill=NA, color="black"),
panel.background=element_rect(fill="gray95")) +
geom_point(data=myreactive()$mydf2, aes(x=NB_effectSize, y=-log10(NB_Sensitizing_P)), color="red")
} else if(input$stat_metric=="FDR"){
p <- ggplot(mysheetreactive()$mydf, aes(x=NB_effectSize, y=-log10(NB_Sensitizing_FDR), text=Gene)) +
geom_point(color="gray76") +
facet_wrap(~tx, ncol=2) +
xlab("NB Effect Size") +
ylab("-Log10(FDR)") +
geom_hline(yintercept=-log10(input$stat_alpha)) +
geom_vline(xintercept=input$effectSize) +
theme(panel.border=element_rect(fill=NA, color="black"),
panel.background=element_rect(fill="gray95")) +
geom_point(data=myreactive()$mydf2, aes(x=NB_effectSize, y=-log10(NB_Sensitizing_FDR)), color="red")
}
ggplotly(p) %>% config(displayModeBar = F)
})
#Count how many total drug combinations there are (rows of mydf4) and unique drugs there are (unique entries of 'known_drug' column)
output$mytext <- renderText({
req(myreactive()$mydf4)
mydf4 <- myreactive()$mydf4
myresult <- paste0(HTML("<b><font size='+2'>",nrow(mydf4),"</b></font> total combinations <b>", "<br><br>",
length(unique(mydf4$known_drug)), "</b> unique compounds"))
return(myresult)
})
#Render data table
output$mytable = DT::renderDataTable({
req(myreactive()$mydf4)
DT::datatable(myreactive()$mydf4,
extensions = c('Responsive', 'Buttons'),
options = list(scrollY='700px',
paging=FALSE,
ordering=TRUE,
searching=TRUE,
dom = 'Bfrtip',
buttons = c('csv')))
})
#The popup that appears when the actionlink 'info_button' is clicked
observeEvent(input$info_button, {
# Show a modal when the button is pressed
shinyalert("Drugs per Gene",
"The drugs per gene are capped at 10 by default, but this can be changed. When value is >1, drugs are selected to maximize the unique total drug combinations.",
type = "info")
})
output$plot2 <- renderPlotly({
req(myreactive()$mydf4)
mydf4 <- myreactive()$mydf4
p <- mydf4 %>%
group_by(tx) %>%
summarize(count=n()) %>%
ggplot(aes(x=tx, y=count)) +
geom_bar(stat="identity", fill="lightblue", color="black")+
theme(panel.border=element_rect(fill=NA, color="black"),
axis.text.x = element_text(angle = 45, vjust=0.5),
plot.title=element_text(hjust=0.5)) +
xlab("") +
ggtitle("Total # of drugs per treatment")
ggplotly(p) %>% config(displayModeBar = F)
})
output$plot3 <- renderPlotly({
req(myreactive()$mydf4)
mydf4 <- myreactive()$mydf4
p <- mydf4 %>%
group_by(Gene) %>%
summarize(count=n()) %>%
ggplot(aes(x=Gene, y=count)) +
geom_bar(stat="identity", fill="darkolivegreen2", color="black")+
theme(panel.border=element_rect(fill=NA, color="black"),
axis.text.x = element_text(angle = 45, vjust=0.5),
plot.title=element_text(hjust=0.5)) +
xlab("") +
ggtitle("Total # of drugs per gene (all treatments)")
ggplotly(p) %>% config(displayModeBar = F)
})
output$plot4 <- renderPlotly({
req(myreactive()$depmap_data2)
depmap_data2 <- myreactive()$depmap_data2
p <-ggplot(depmap_data2, aes(x=reorder(Gene, -log10(`P-Value`)), y=-log10(`P-Value`))) +
geom_point() +
xlab("Gene") +
ylab("-Log10(p-value)") +
theme(panel.border=element_rect(fill=NA, color="black"),
plot.title = element_text(hjust=0.5))+
ggtitle("Hits referenced against dependencies in Neuroblastoma (DepMap)")
ggplotly(p) %>% config(displayModeBar = F)
})
})
shinyApp(ui, server)