-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
506 lines (430 loc) · 15.5 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
library(DT)
library(shiny)
library(igraph)
library(plotly)
library(rstackdeque)
library(jsonlite)
options(shiny.maxRequestSize = 100*1024^2) #100MB file size limit
source("external/graph_utils.R", local = TRUE)
source("external/makenetjson.R", local = TRUE)
source("external/protein_label_dictionary.R",local = TRUE)
conf <- fromJSON("./www/data/config.json")
#check if rds file already exists
graph_file_rds=paste(conf$FilePath,"_graph.rds",sep="")
comm_file_rds=paste(conf$FilePath,"_communities.rds",sep="")
if(!file.exists(graph_file_rds)){
graph <- build_initial_graph(conf)
}else{
print("Loading graph from rds ")
graph<-readRDS(graph_file_rds)
}
if(!file.exists(comm_file_rds)){
communities <- get_communities(graph)
}else{
print("Loading communities from rds ")
communities<-readRDS(comm_file_rds)
}
print(conf$FilePath)
saveRDS(graph,paste(conf$FilePath,"_graph.rds",sep=""))
saveRDS(communities,paste(conf$FilePath,"_communities.rds",sep=""))
htmlloaded = FALSE
s1 <- rstack()
s2 <-rstack()
s3 <- rstack()
mp <<- NULL
sortedlabel<-NULL
protienDSpathway<<-data.frame()
disptable<<-NULL
lbllist<<-NULL
is_comm_graph <- TRUE
colormapping<-data.frame(Entity=character(),Color=character(),stringsAsFactors = FALSE)
interactionmapping<-data.frame(Entity1=character(),Entity2=character(),stringsAsFactors = FALSE)
# uniqueentities <<- NULL
function(input, output, session){
global <- reactiveValues()
global$viz_stack <- insert_top(s1, list(graph, communities))
global$name <- insert_top(s2, "")
typ_colors <- conf$Type_colors
tcDF <- typ_colors[[1]]
# render with sigma the current graph (in json)
output$graph_with_sigma <- renderUI({
print("output$graph_with_sigma")
data <- graph_to_write()
makenetjson(data[[1]], "./www/data/current_graph.json", data[[2]],conf)
update_stats(data[[1]], data[[2]])
observe({
session$sendCustomMessage(type = "updategraph",message="xyz")
})
return(includeHTML("./www/graph.html"))
})
# Generate the current graph name (as a list of community labels)
output$name <- renderText({
name <- as.list(rev(global$name))
name <- paste(name, collapse = "/", sep="/")
return(paste(c("Current Community", name)))
})
# Generate a table of node degrees
output$entities_table <- DT::renderDataTable({
if (!is.null(global$nodes)){
# table <- global$nodes[c("Name", "Type", "Degree","PageRank")]
table <- global$nodes[c("name", "type", "degree","pagerank")]
}
},
options = list(order = list(list(1, 'desc'))),
rownames = FALSE,
selection = "single"
)
# Plot the degree distribution of the current graph
output$degree_distribution <- renderPlotly({
if (!is.null(global$nodes)){
x <-list(
title = "Degree"
)
y <- list(
title = "Number of nodes"
)
plot_ly(x = global$nodes[["degree"]], type="histogram", color="#FF8800") %>%
layout(xaxis = x, yaxis = y)
}
})
# Plot the pagerank distribution of the current graph
output$pagerank_distribution <- renderPlotly({
if (!is.null(global$nodes)) {
x <-list(
title = "PageRank"
)
y <- list(
title = "Number of nodes"
)
plot_ly(x = global$nodes[["pagerank"]], type="histogram", color="#FF8800") %>%
layout(xaxis = x, yaxis = y)
}
})
output$plotgraph1 <-DT::renderDataTable(
{
print("output$plotgraph1")
protienDSpathway<<-data.frame()
sortedlabel<-NULL
#labelfreq <- lapply(rawlabels,table)
proteins<-global$nodes[global$nodes$type=="Protein","name"]
print("Printing Proteins ..")
#print(proteins)
# This takes forever. If we can load a previously built object do it; otherwise don't hold your breath
withProgress(message = "Loading ...",value = 0,{
if(is.null(mp)){
filename = 'mp.rds'
if (file.exists(filename)){
mp <<- NULL
mp <<- readRDS(filename)
} else {
mp <<- getproteinlabeldict()
saveRDS(mp, file=filename)
}
}
})
lapply(proteins,appendlabel)
table <- data.frame(Protein="No pathway data available")
if (nrow(protienDSpathway)>1){
labelfreq <- table(protienDSpathway)
if (ncol(labelfreq)>1){
z<-apply(labelfreq,1,sum)
sortedlabel<-labelfreq[order(as.numeric(z), decreasing=TRUE),]
disptable<<-as.data.frame.matrix(sortedlabel)
} else {
disptable <<- as.data.frame.matrix(labelfreq)
}
row.names(disptable) <<- strtrim(row.names(disptable), 50)
}
disptable
},
rownames = TRUE,
selection = "single"
)
output$choose_entTypes <- renderUI({
dat <- read.csv(conf$FilePath, header = input$header,
sep = input$sep, quote = input$quote)
x<-paste(dat[,"type1"],dat[,"type2"],collapse = ",",sep=",")
uniqueentities<<-unique(unlist(strsplit(x,",")))
uniqueentities <- unlist(tcDF[[1]])
# Create the checkboxes and select them all by default
checkboxGroupInput("entTypes", "Entity Types",
choices = uniqueentities,
selected = uniqueentities)
})
# output$legend <- renderTable(tcDF)
trList = list()
for (i in seq_len(nrow(tcDF))) {
trList[[i]] <- tags$tr(
tags$td(span(style = sprintf(
"width:1.1em; height:1.1em; background-color:%s; display:inline-block;",
tcDF[i,2]
))),
tags$td(tcDF[i,1])
)
}
output$legend <- renderUI({
tags$table(class = "table",
tags$thead(tags$tr(
tags$th("Color"),
tags$th("Entity")
)),
tags$tbody(
trList,
tags$tr(
tags$td(span(style = sprintf(
"width:1.1em; height:1.1em; background-color:%s; display:inline-block;",
conf$community_color
))),
tags$td("Community")
)
)
)
})
# Populate Entity definitions dropdowns if input file is selected
output$contents <- renderTable({
print("output$contents")
inFile <- input$file1
if (is.null(inFile))
return(NULL)
dat <- read.csv(inFile$datapath, header = input$header,
sep = input$sep, quote = input$quote)
updateSelectInput(session,"entity1",choices = colnames(dat))
updateSelectInput(session,"entity2",choices = colnames(dat))
updateSelectInput(session,"type1",choices = colnames(dat))
updateSelectInput(session,"type2",choices = colnames(dat))
return (NULL)
})
output$plotgraph2 <- renderPlotly({
#withProgress(message = "Loading ...",value = 0,{
#getrawentititesfromComm(global$currentCommId)
#})
labelfreq <- table(protienDSpathway)
z<-apply(labelfreq,1,sum)
sortedlabel<-labelfreq[order(z, decreasing=TRUE),]
x<-as.data.frame(sortedlabel,row.names=rownames(sortedlabel),col.names=colnames(sortedlabel))
plot_ly(z = sortedlabel,x=colnames(sortedlabel),y=rownames(sortedlabel), type = "heatmap",hoverinfo = "text",
text = paste(colnames(sortedlabel),rownames(sortedlabel)),colorscale = "Hot") %>% layout(xaxis = list(title="Proteins"),yaxis=list(title="Disease Pathway"))
})
# Event handler for Done button in Entity definitions tab
# Update dropdowns in Define Entity interactions and Entity colors
observeEvent(input$entitymapping_button, {
inFile <- input$file1
dat <- read.csv(inFile$datapath, header = input$header,
sep = input$sep, quote = input$quote)
x<-paste(dat[,"type1"],dat[,"type2"],collapse = ",",sep=",")
uniqueentities<<-unique(unlist(strsplit(x,",")))
updateSelectInput(session,"entcolors",choices = uniqueentities)
updateSelectInput(session,"entintr1",choices=uniqueentities)
updateSelectInput(session,"entintr2",choices=uniqueentities)
# updateCheckboxGroupInput(session,"entTypes",choices = uniqueentities)
})
# Event handler for Assign color button in Entity colors tab
# Renders color mappings table when button is pressed
observeEvent(input$entdone,{
print(uniqueentities)
colormapping <<- rbind(colormapping,data.frame(Entity=toString(input[["entcolors"]]),Color=toString(input[["entcol"]])))
output$enttable <- renderTable(colormapping)
})
# Event handler for Assign interaction button in Define Entity interactions tab
observeEvent(input$entintrdone,{
if(nrow(interactionmapping) >0){
comb1 <- sum(grepl(input[["entintr1"]],interactionmapping$Entity1))
comb2 <- sum(grepl(input[["entintr2"]],interactionmapping$Entity2))
comb3 <-sum(grepl(input[["entintr1"]],interactionmapping$Entity2))
comb4<-sum(grepl(input[["entintr2"]],interactionmapping$Entity1))
if((comb1>0)&&(comb2>0))
return(NULL)
if((comb3>0)&&(comb4>0))
return(NULL)
}
interactionmapping <<- rbind(interactionmapping,data.frame(Entity1=toString(input[["entintr1"]]),Entity2=toString(input[["entintr2"]])))
output$entintrtable <- renderTable(interactionmapping)
})
#saveoptionscsv event
observeEvent(input$saveoptionscsv,{
fpath<-input$file1$datapath
for(ent in uniqueentities){
print(ent)
if(nrow(colormapping[colormapping$Entity==ent,]) == 0){
colormapping <<- rbind(colormapping,data.frame(Entity=ent,Color=rgb(runif(1),runif(1),runif(1))))
}
}
typecolors<-toJSON(colormapping)
interactions <- toJSON(interactionmapping)
elements_list = sprintf('[{"FilePath":"%s",
"Entity1_Col": "%s",
"Entity2_Col":"%s",
"Type1_Col":"%s",
"Type2_Col":"%s",
"Type_colors":%s,
"Interactions":%s,
"community_color":"%s",
"community_threshold":"%s"
}]', fpath, input$entity1,input$entity2, input$type1,input$type2, typecolors,interactions, input$community_col,input$comm_size)
print(elements_list)
con <- file("./www/data/config_1.json")
writeLines(elements_list,con)
close(con)
conf <<- fromJSON("./www/data/config_1.json")
resetgraph(conf)
})
# reset button
observeEvent(input$reset_button, {
resetgraph(conf)
})
#Search button
observeEvent(input$search_button,{
searchelm <- strsplit(input$searchentitiy,",")
data <- peek_top(global$viz_stack)
graph <- data[[1]]
communities <- data[[2]]
memcomm <- NULL
if (is_comm_graph){
ii<-1
for(elm in unlist(searchelm)){
print(elm)
memcomm[ii] <- communities$membership[which(elm== V(graph)$name)]
ii<-ii+1
}
memcommunity<-paste(memcomm,collapse = ",")
} else {
memcommunity <- input$searchentitiy
}
observe({
session$sendCustomMessage(type = "commmemmsg" ,
message = list(id=memcommunity))
})
})
# disease pathway table click
observe({
row <- input$plotgraph1_rows_selected
val<-disptable[as.numeric(row),]
if(is.null(val)){
return(NULL)
}
z<-apply(val,1,function(x) which(x==max(x)))
#print(rownames(z))
last_selected_row = tail(row, n=1)
#proteins<-protienDSpathway[protienDSpathway$Pathway==unlist(last_selected_row),]$Protein
#print(proteins)
session$sendCustomMessage(type = "commmemmsg" ,
message = list(id=paste(rownames(z),collapse=",")))
})
# table click
observe({
row <- input$entities_table_rows_selected
if (length(row)) {
session$sendCustomMessage(type = "commmemmsg" ,
message = list(id=global$nodes[row,1]))
}
})
# back button
observeEvent(input$back_button, {
size <- length(global$viz_stack)
if (size > 1){
global$viz_stack <- without_top(global$viz_stack)
global$name <- without_top(global$name)
}
})
# on-click from sigma.js
observeEvent(input$comm_id, {
print("sigma node click");
memcommunity <- NULL
if (is_comm_graph){
data <- peek_top(global$viz_stack)
graph <- data[[1]]
communities <- data[[2]]
graph <- subgraph_of_one_community(graph, communities, input$comm_id)
communities <- get_communities(graph,input$select)
global$viz_stack <- insert_top(global$viz_stack, list(graph, communities))
global$name <- insert_top(global$name, input$comm_id)
if(input$searchentitiy =="")
return()
searchelm=input$searchentitiy
memcomm <- NULL
if (vcount(graph) > as.numeric(conf$community_threshold)) {
# if (is_comm_graph){
ii<-1
for(elm in unlist(searchelm)){
if(length(which(elm== V(graph)$name)) != 0){
memcomm[ii] <- communities$membership[which(elm== V(graph)$name)]
ii<-ii+1
}
}
memcommunity<-paste(memcomm,collapse = ",")
} else {
memcommunity <- input$searchentitiy
}
}
else {
memcommunity <- input$searchentitiy
}
print(memcommunity)
observe({
session$sendCustomMessage(type = "commmemmsg" ,
message = list(id=memcommunity))
})
})
resetgraph<-function(conf)
{
graph <- build_initial_graph(conf)
is_comm_graph <- TRUE
communities <- get_communities(graph,input$select)
global$viz_stack <- rstack()
global$viz_stack <- insert_top(global$viz_stack, list(graph, communities))
global$name <- insert_top(s2, "")
x<-as.data.frame(conf$Interactions)
z<-c()
itr<-1
for(ii in x$Entity1){
pastestr=paste0(ii,"-",x$Entity2[itr],sep="")
z[itr] <- c(pastestr=pastestr)
itr<-itr+1
}
z[itr] <- paste0("all"="All")
updateRadioButtons(session,"interactions",label="Show Interactions:",choices=z,selected="All")
print(input$community_col)
}
processrow<-function (elm)
{
p(paste(toString(elm[1]), "'s are ",sep=""), span(toString(elm[2]), style = paste("color:",toString(elm[2]),sep="")))
}
# update the summary stats
update_stats <- function(graph, is_comm_graph){
nodes <- get.data.frame(graph, what="vertices")
nodes$degree <- degree(graph)
nodes$pagerank <- page_rank(graph)$vector
# if (is_comm_graph==TRUE){
# colnames(nodes) <- c("Name", "Type", "Comm", "Size", "Degree","PageRank")
# } else {
# colnames(nodes) <- c("Name", "Type", "Comm", "Degree","PageRank")
# }
global$nodes <- nodes
}
# writes out the current viz graph to a json for sigma
graph_to_write <- reactive({
print("graph_to_write")
data <- peek_top(global$viz_stack)
graph <- data[[1]]
graphdf <- get.data.frame(graph, what="vertices")
communities <- data[[2]]
print(paste("is_comm_graph=",is_comm_graph))
# Try and apply community detection if there are a lot of nodes to visualize
if (vcount(graph) > as.numeric(conf$community_threshold)) {
print("apply community detection")
community_graph <- get_community_graph(graph, communities)
commdf <- get.data.frame(community_graph, what="vertices")
if (vcount(community_graph) > 1){
is_comm_graph <<- TRUE
return(list(community_graph, TRUE))
}
}
# If we have few enough nodes (or would have just 1 (sub)community) visualize as is
V(graph)$size <- 1
is_comm_graph <<- FALSE
# Remove nodes we aren't we don't want that type of node
dellist <- c()
indx <- 1
return(list(graph, FALSE))
})
}