This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.R
331 lines (280 loc) · 10 KB
/
server.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
shinyServer(function(input, output,session) {
##How do you look at it from different perspectives
#Multiple drug, one disease
#Multiple diseases, one drug
#Filters based on performance
vds <- reactive({
rho <- vdsRho[,unlist(input$organ)]
rho <- as.data.frame(rho)
rho$names = row.names(vdsRho)
rho <- rho[order(rho$rho),]
rho
})
# Filtered cell line drug sensitivity data
filtered.vds <- reactive({
rho <- vds()
rho <- rho[rho$rho>=input$threshold,]
rho
})
# Annonate selected drugs for Model 1
drugAnnonates1 <- reactive({
a <- list()
rho <- filtered.vds()
selectedDrug <- input$drugList1
if(length(selectedDrug) != 0){
for (i in c(1:length(selectedDrug))){
m <- rho[rho$names %in% selectedDrug,]
a[[i]] <- list(
x = m$names[i],
y = m$rho[i],
text = m$names[i],
showarrow = TRUE,
arrowhead = 7,
ax = 0,
ay = -100
)
}
}
a
})
# Plot Model 1
output$vfsperf <- renderPlotly({
rho <- filtered.vds()
a <- drugAnnonates1()
# note how size is automatically scaled and added as hover text
plot_ly(rho, x=names, y=rho, mode="markers")%>%
layout(xaxis = list(title="Drug"),
yaxis = list(title="Rho"),
annotations = a)
})
# Generate a list medianValues of drugs for each disease area
drugMedianValues <- reactive({
validate(
need(try(input$diseaseArea != ""), "Please choose at least one area")
)
medianValues <- lapply(input$diseaseArea, function(x) {
diseaseRho <- drugRho[[x]]
medianVal <- unlist(lapply(diseaseRho, function(x) {
values <- unlist(strsplit(x, ","))
values <- values[values != "NA"]
values <- as.numeric(values)
median(values,na.rm = T)
}))
temp <- data.frame(drug = row.names(drugRho), medianVal, disease = x)
return(temp)
})
medianValues
})
# Generate a dataframe of median values. Filtered by threshold, sorted
filtered.drugMedianValues <- reactive({
medianValues <- drugMedianValues()
threshold <- input$thresholdMedian
df1 <- medianValues[[1]]
# filter f1 according to the median threshold and drug choices from Model 1
# then sort by medianVal
# get the rownames of the order
valueIndex <- which(df1$medianVal>=threshold)
df_filter <- medianValues[[1]][valueIndex,]
drugChoices1 <- filtered.vds()$names
drugChoices2 <- df_filter$drug
newDrugChoices2 <- intersect(drugChoices1,drugChoices2)
df_filter <- medianValues[[1]][medianValues[[1]]$drug %in% newDrugChoices2,]
df_sort <- df_filter[order(df_filter$medianVal),]
ordered.threshold <- as.numeric(rownames(df_sort))
for (i in c(1:length(medianValues))){
medianValues[[i]] <- medianValues[[i]][ordered.threshold,]
}
medianValues <- do.call(rbind,medianValues)
medianValues
})
# Annonate selected drugs for Model 3
drugAnnonates2 <- reactive({
a <- list()
rho <- drugMedianValues()[[1]]
selectedDrug <- input$drugList2
if(length(selectedDrug) != 0){
for (i in c(1:length(selectedDrug))){
m <- rho[rho$drug %in% selectedDrug,]
a[[i]] <- list(
x = m$drug[i],
y = m$medianVal[i],
text = m$drug[i],
showarrow = TRUE,
arrowhead = 7,
ax = 0,
ay = -150
)
}
}
a
})
# Plot drug sensitivity (Model 3)
output$drugRho <- renderPlotly({
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0,{
medianValues <- filtered.drugMedianValues()
a <- drugAnnonates2()
plot_ly(medianValues, x=drug, y= medianVal,color=disease, mode="markers")%>%
layout(annotations = a)
})
})
# Update choices
observe({
# Model 1 drug list update
updateSelectInput(session, "drugList1", choices = sort(filtered.vds()$names), selected = input$drugList1)
filteredDrugChoices <- as.character(filtered.drugMedianValues()$drug)
filteredDrugChoices <- sort(filteredDrugChoices)
# Model 3 drug list update
updateSelectInput(session, "drugList2", choices = filteredDrugChoices, selected = input$drugList2)
# Model 2 drug choices update
updateSelectInput(session, "drugList3", choices = filteredDrugChoices,selected = input$drugList3)
# Model 2 other disease area update
otherDiseaseArea <- diseases[!(diseases %in% input$diseaseArea[1])]
updateSelectInput(session, "otherDiseaseList", choices = otherDiseaseArea ,selected = input$otherDiseaseList)
# Model 2 sliderMax
EM <- top20Data()$effect
resultEM <- max(abs(max(EM)),abs(min(EM)))
updateSliderInput(session, "thresholdEM", max = floor(resultEM*1000)/1000)
})
# Outputs selected organ from Model 1 in Model 2
output$selectedOrgan <- renderText({
input$organ
})
# Outputs selected disease area from Model 1 in Model 2
output$selectedArea <- renderText({
input$diseaseArea[1]
})
# Generates Model 2 data
top20Data <- reactive({
validate(
need(input$drugList3 != '', "Please choose a drug")
#need(length(input$otherDiseaseList) > 0, "Please choose at least one disease area")
)
R = vdsRdf[vdsRdf$drug == input$drugList3,]
#May have multiple diseases, so loop through and gather top 20 freqCounts of each
#disease area
diseaseList <- union(input$otherDiseaseList,input$diseaseArea[1])
diseaseList <- sort(diseaseList)
data = lapply(diseaseList, function(x) {
diseaseArea=R[R$disease == x,]
filtered = diseaseArea[order(diseaseArea$freqCounts,decreasing = T)[1:20],]
filtered = filtered[abs(filtered$effect)>=input$thresholdEM,]
return(filtered)
})
data = do.call(rbind, data)
data
})
# Model 2 Plot
output$dsPlot <- renderPlotly({
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0,{
incProgress(session= session)
data <- top20Data()
#filtered = diseaseArea[diseaseArea$freqCounts > 0.05,]
#filtered = filtered[filtered$freqEvents > 0.01,]
# note how size is automatically scaled and added as hover text
size <- normalize.vector((data$freqEvents)^2)
size <- as.numeric(format(size,digits = 3))
total <- sampleSizeData[data$disease,input$drugList3]
plot_ly(data, x = effect, y = freqCounts,hoverinfo="text",
text = paste("Molecular Trait: ",genes,
"</br>Feature Stability: ", freqCounts,
"</br>Effect Magnitude: ", effect,
"</br>Disease: ", disease,
"</br>Drug: ", drug,
"</br>Event frequency: ", format(freqEvents*100,digits = 2),"% out of",total),
size = size,color = disease,
mode = "markers") %>%
layout(xaxis = list(title="Effect Magnitude"),
yaxis = list(title="Feature Stability"))
})
})
changeColor <- reactiveValues(data = FALSE)
observeEvent(input$changeColorButton,{
changeColor$data <- TRUE
})
observeEvent(input$clearButton,{
changeColor$data <- FALSE
})
output$colorLegend <- renderUI({
if(changeColor$data){
list(
div("+",class ="col-sm-2",style="margin-top:2%;margin-right:2%;border:1px solid #000;background-color:#ffcccc;text-align:center"),
div("-",class ="col-sm-2",style="margin-top:2%;margin-right:2%;border:1px solid #000;background-color:#b3e6ff;text-align:center")
)
}else{
div()
}
})
# Model 2 table
output$dsDataTable = renderDataTable({
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0,{
incProgress(session= session)
data <- top20Data()
#show.column <- input$show_vars
show.column <- showtable
#Filter by freqCounts and freqEvents
#filtered = diseaseArea[diseaseArea$freqCounts > 0.05,]
#filtered = filtered[filtered$freqEvents > 0.01,]
if(changeColor$data){
options = list(
rowCallback = JS(
"function(row, data) {",
"if(parseFloat(data[2]) > 0){",
"$('td', row).css({'background-color': '#ffcccc'});",
"}else{",
"$('td', row).css({'background-color': '#b3e6ff'});",
"}",
"}"),
searching = TRUE
)
}else{
options = list(searching = TRUE)
}
datatable(
data[,show.column],
rownames = FALSE,
filter = 'top',
options = options
)
})
})
# Model 4: drug information table
output$drugTable = renderDataTable({
validate(
need(input$drugSelected != '', "Please select at least one drug")
)
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0,{
incProgress(session= session)
data <- drugData[drugData$cpd_name %in% input$drugSelected,]
show.column <- input$show_drug
datatable(
data[,show.column],
rownames = FALSE,
filter = 'top',
options = list(shinyServer(function(input, output,session) {
searching = TRUE
}))
)
})
})
# Model 5: cell line information table
output$cellLineTable = renderDataTable({
withProgress(message = 'Calculation in progress',
detail = 'This may take a while...', value = 0,{
incProgress(session= session)
data <- cellLineData[cellLineData$ccle_primary_site %in% input$cellLineSelected,]
show.column <- input$show_cell_line
datatable(
data[,show.column],
rownames = FALSE,
filter = 'top',
options = list(
searching = TRUE
)
)
})
})
})