-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
370 lines (315 loc) · 14 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
server <- function(input, output, session) {
## Start with disabled submit button to avoid crashing the app
##----------------------------------------------------------------
observe({
if (!(input$gene=="" & input$chr=="chr1" & input$start==1 & input$end==2 & input$contact=="")) enable("doPlot")
})
## Hide or show sidebar panel
##----------------------------------------------------------------
observeEvent(input$toggleSidebar, {
shinyjs::toggle(id = "sidebar")
if (input$toggleSidebar %% 2 != 0) {
updateActionButton(session,
"toggleSidebar",
label = "Show sidebar",
icon = icon("eye"))
removeCssClass("main", "col-sm-8")
addCssClass("main", "col-sm-12")
} else if (input$toggleSidebar %% 2 == 0) {
updateActionButton(session,
"toggleSidebar",
label = "Hide sidebar",
icon = icon("eye-slash"))
removeCssClass("main", "col-sm-12")
addCssClass("main", "col-sm-8")
}
})
## Add options for virtual 4C and genes (improve loading time)
##----------------------------------------------------------------
updateSelectizeInput(session = session,
inputId = 'contacts.dataset',
choices = c("None" = "", list.art4C),
selected=183446,#"PDX1-AS1;PDX1",
server = TRUE)
updateSelectizeInput(session = session,
inputId = 'gene',
choices = c(Choose="", genes.key$gene_name),
selected="PDX1",
server = TRUE)
## Get coordinates to plot
##----------------------------------------------------------------
coordinates <- eventReactive(input$doPlot, {
if (input$coordType==2) {
coord <- GRanges(paste0(input$chr, ":",
input$start-as.numeric(input$ranges),
"-", input$end+as.numeric(input$ranges)))
} else if (input$coordType==1) {
load(paste0(path, input$genome, "/genes/",
input$genome, "_gene_annotation_ensemblv75_",
genes.key$chr[genes.key$gene_name %in% input$gene], ".rda"))
genes <- genes[genes$gene_name==input$gene &
genes$type=="GENE" &
genes$longest,]
coord <- regioneR::extendRegions(genes,
extend.start=as.numeric(input$ranges),
extend.end=as.numeric(input$ranges))
}
if (width(coord)>5e6) coord <- resize(coord, width=5e6, fix="center", ignore.strand=TRUE)
return(coord)
})
## Update coordinates
##----------------------------------------------------------------
observeEvent(input$doPlot, {
updateRadioButtons(session, "coordType",
selected = 2)
})
observe({
updateSelectInput(session, "chr",
selected=as.character(seqnames(coordinates())))
updateNumericInput(session, "start",
value = start(coordinates()))
updateNumericInput(session, "end",
value = end(coordinates()))
updateSelectInput(session, "ranges",
selected=0)
})
## Render slider once coordinates are present
##----------------------------------------------------------------
output$SLIDER = renderUI({
sliderInput(inputId="zoom",
label="Coordinates",
value=c(start(coordinates()),
end(coordinates())),
min=max(start(coordinates())-width(coordinates())*4,
1),
max=min(end(coordinates())+width(coordinates())*4,
len$length[len$chr==as.character(seqnames(coordinates()))]),
width="100%")
})
## Update coordinates to slider values
##----------------------------------------------------------------
observe({
print(input$zoom)
updateNumericInput(session, "start",
value = input$zoom[1])
updateNumericInput(session, "end",
value = input$zoom[2])
})
## Output coordinates in text
##----------------------------------------------------------------
output$coordinates <- renderText({
as.character(coordinates())
})
## Function to draw plot regulome
##----------------------------------------------------------------
makePlot <- eventReactive(input$doPlot, {
if (input$coordType==1) nm <- input$gene else nm <- as.character(coordinates())
message(paste0(">>>Creating_regulomePlot/", Sys.time(), "/", nm,
"/", input$snps.dataset, "/", input$maps_dataset, "/", input$contacts.dataset,
"/", input$clusters.dataset, "/", input$tfs.dataset))
plotRegulome(coordinates(),
snps_dataset=gsub("-", "", input$snps.dataset),
contacts_dataset=gsub("-", "", input$contacts.dataset),
maps_dataset=gsub("-", "", input$maps_dataset),
clusters_dataset=gsub("-", "", input$clusters.dataset),
tfs_dataset=gsub("-", "", input$tfs.dataset),
genome=input$genome,
path=path)
})
## Output Regulome Plot
##----------------------------------------------------------------
output$testPlot <- renderPlot ({
makePlot()
})
output$imagePlot <- renderImage ({
# For high-res displays, this will be greater than 1
# pixelratio <- session$clientData$pixelratio
pixelratio <- 1
# Read myImage's width and height. These are reactive values, so this
# expression will re-run whenever they change.
width <- session$clientData$output_imagePlot_width*pixelratio
height <- session$clientData$output_imagePlot_height*pixelratio
dpi=100*pixelratio
# A temp file to save the output.
outfile <- tempfile(fileext='.png')
# Generate the image file
ggplot2::ggsave(filename=outfile,
plot=makePlot(),
width=width/dpi,
height=height/dpi,
unit="in")
# Return a list containing the filename
list(src = outfile,
width = width,
height = height,
alt = "This is alternate text")
}, deleteFile = TRUE) # delete the temp file when finished
## Download Regulome Plot
##----------------------------------------------------------------
output$downloadPlot <- downloadHandler(
filename = function() {
coordinates <- coordinates()
filename <- paste0("IRBplot_", as.character(coordinates), ".pdf")
return(filename)
},
content = function(file) {
ggplot2::ggsave(filename=file, plot=makePlot(),
width=12, height=6.5, unit="in")
}
)
## Add UCSC link to plot
##----------------------------------------------------------------
output$UCSCLink <- renderUI({
url <- paste0("https://genome.ucsc.edu/cgi-bin/hgTracks?db=hg19&hubUrl=https://raw.githubusercontent.com/mireia-bioinfo/IRB_hub/master/hub.txt&hubUrl=https://raw.githubusercontent.com/mireia-bioinfo/CYT_hub/master/hub.txt&position=",
input$chr, ":", input$start, "-", input$end)
actionButton("UCSCLink", label="UCSC",
icon = icon("link"),
onclick = paste0("window.open(", shQuote(url, type="sh"), ",",
shQuote("_blank", type="sh"), ")"),
width="100%"
)
})
output$WashULink <- renderUI({
url <- paste0("http://epigenomegateway.wustl.edu/browser/?genome=hg19&hub=https://raw.githubusercontent.com/mireia-bioinfo/IRB_hub/master/washu_new_session.json&position=",
input$chr, ":", input$start, "-", input$end)
actionButton("WashULink", label="WashU",
icon = icon("link"),
onclick = paste0("window.open(", shQuote(url, type="sh"), ",",
shQuote("_blank", type="sh"), ")"),
width="100%"
)
})
## Close all connections to avoid error
closeAllConnections()
##----------------------------------------------------------------
## Create Tables
##----------------------------------------------------------------
## Maps table
maps.df <- eventReactive(input$doPlot, {
maps.l <- create_mapsRegulome(coordinates=coordinates(),
maps_dataset=gsub("-", "", input$maps_dataset),
genome=input$genome,
path=path)
tfs.l <- create_tfsRegulome(coordinates=coordinates(),
tfs_dataset=gsub("-", "", input$tfs.dataset),
genome=input$genome,
path=path)
if (maps.l$name!="" & tfs.l$name!="" & length(maps.l$value)>0) {
## Add tfs data
ol <- findOverlaps(maps.l$value,
tfs.l$value)
tfs.ol <- split(tfs.l$value$TF[subjectHits(ol)],
queryHits(ol))
tfs.ol <- lapply(tfs.ol, unique)
tfs.ol <- sapply(tfs.ol, paste0, collapse=", ")
maps.l$value$TFBS <- "-"
maps.l$value$TFBS[as.numeric(names(tfs.ol))] <- tfs.ol
## Convert to data.frame
maps.l$value <- data.frame(maps.l$value)[,c(6, 1:3, 7)]
colnames(maps.l$value) <- c("Class", "Chr", "Start", "End", "TFBS")
} else if (maps.l$name!="" & tfs.l$name=="" & length(maps.l$value)>0) {
maps.l$value$TFBS <- "-"
maps.l$value <- data.frame(maps.l$value)[,c(6, 1:3, 7)]
colnames(maps.l$value) <- c("Class", "Chr", "Start", "End", "TFBS")
} else {
maps.l$value <- data.frame("Class"=NA,
"Chr"=NA,
"Start"=NA,
"End"=NA,
"TFBS"=NA)
}
maps.l
})
output$mapsTable <- DT::renderDataTable({
col <- rep("black", length(maps.df()$col))
col[grep("black", maps.df()$col)] <- "white"
col[grep("#000000", maps.df()$col)] <- "white"
DT::datatable(maps.df()$value,
rownames=FALSE) %>%
formatStyle("Class",
backgroundColor=styleEqual(names(maps.df()$col),
adjustcolor(maps.df()$col, alpha.f=0.7)),
color=styleEqual(names(maps.df()$col),
col)
)
})
## SNPs table
snps.df <- eventReactive(input$doPlot, {
snps <- create_snpsRegulome(coordinates=coordinates(),
snps_dataset=gsub("-", "", input$snps.dataset),
genome=input$genome,
path=path)
if (snps$name!="") {
snps <- data.frame(snps$value)[,c(1,2,6:7)]
snps <- snps[order(snps$PVAL),]
colnames(snps) <- c("Chr", "Position", "rsID", "P-value")
} else {
snps <- data.frame("Chr"=NA,
"Position"=NA,
"rsID"=NA,
"P-value"=NA)
}
snps
})
output$snpsTable <- DT::renderDataTable({
DT::datatable(snps.df(),
rownames=FALSE)
})
## Genes table
genes.df <- eventReactive(c(input$doPlot, input$rnadb), {
## Load gene RNA data
load(paste0(path,input$genome, "/genes/",
input$genome, "_gene_", input$rnadb, "_", input$chr, ".rda"))
genes <- subsetByOverlaps(genes, coordinates())
if (input$rnadb=="moran") {
genes <- data.frame(genes)[,c(7,6,8)]
colnames(genes) <- c("Gene Symbol", "Gene ID (UCSC)",
"Expression (RPKM)")
genes <- genes[order(genes[,3], decreasing=TRUE),]
genes
} else {
genes <- data.frame(genes)[,c(7,6,8:10)]
colnames(genes) <- c("Gene Symbol", "Gene ID (Ensembl)",
"Control Expression*", "Cytokine-induced Expression*",
"log2 fold-change")
genes$`log2 fold-change` <- round(genes$`log2 fold-change`, 2)
genes <- genes[order(genes[,5], decreasing=TRUE),]
}
})
output$genesTable <- DT::renderDataTable({
if (input$rnadb=="moran") {
formattable::as.datatable(formattable::formattable(genes.df(),
list("Expression (RPKM)"=formattable::color_tile("transparent",
"steelblue3"))),
rownames=FALSE)
} else {
formattable::as.datatable(formattable::formattable(genes.df(),
list("log2 fold-change"=formattable::formatter("span",
style = x ~ formattable::style(color = ifelse(x >= 1,
"green", "gray"))))),
rownames=FALSE) %>%
DT::formatRound(columns=c("Control Expression*", "Cytokine-induced Expression*","log2 fold-change"),
digits=2)
}
})
##----------------------------------------------------------------
## Download Tables
##----------------------------------------------------------------
datasetInput <- reactive({
switch(input$dataset,
"maps"=maps.df()$value,
"snps"=snps.df(),
"genes"=genes.df())
})
output$downloadDataset <- downloadHandler(
filename = function() {
filename <- paste0(input$dataset, "_", as.character(coordinates()), ".csv")
return(filename)
},
content = function(file) {
write.csv(as.data.frame(datasetInput()), file, row.names=FALSE, quote=FALSE)
}
)
## Close all connections to avoid error
closeAllConnections()
}