-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
213 lines (193 loc) · 6.58 KB
/
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
library(shiny)
library(shinydashboard)
library(GenomicRanges)
library(ggbio)
library(dplyr)
library(ggplot2)
server <- function(input, output, session) {
options(shiny.maxRequestSize = 30 * 1024^2)
myData <- reactiveVal()
storedPlots <- reactiveValues(all = NULL, individual = list())
plotGenerationStatus <- reactiveVal(FALSE)
processFile <- function(filePath) {
gr_data <- tryCatch({
df <- read.table(filePath,header = F, sep = '\t') %>%
setNames(c("chr", "start", "end", "score")) %>%
mutate(start = start + 1)
makeGRangesFromDataFrame(df, keep.extra.columns = T)
}, error = function(e) {
NULL
})
req(gr_data)
myData(gr_data)
updateSelectInput(session, "chromosome_select",
choices = c("all", levels(as.factor(seqlevels(gr_data)))))
# Set plot generation status to TRUE
plotGenerationStatus(TRUE)
# Generate and store plots
storedPlots$all <- ggplot(gr_data) +
geom_bar(stat = 'identity', aes(y = score)) +
facet_wrap(~seqnames, scales = "free", ncol = 4) +
theme_clear()
for(chr in levels(as.factor(seqnames(gr_data)))) {
filtered_data <- gr_data[seqnames(gr_data) == chr]
storedPlots$individual[[chr]] <- ggplot(filtered_data) +
geom_rect(stat = 'identity', aes(xmin = start, xmax = end, ymin = 0, ymax = score)) +
theme_clear()
}
# Set plot generation status to FALSE
plotGenerationStatus(FALSE)
}
observeEvent(input$file_myData, {
if(input$select_upload == "local") {
processFile(input$file_myData$datapath)
}
})
observeEvent(input$submit_url, {
if(input$select_upload == "online" && nzchar(input$url_myData)) {
tempFile <- tempfile(fileext = ".bedgraph")
download.file(input$url_myData, destfile = tempFile, mode = "wb")
processFile(tempFile)
}
}, ignoreInit = TRUE)
output$chromosome_selector <- renderUI({
req(storedPlots)
if(is.null(myData())) return(NULL) # Don't display if myData is NULL
selectInput(
inputId = "chromosome_select",
label = "Select Chromosome",
choices = c("all", levels(as.factor(seqlevels(myData()))))
)
})
output$plotTitle <- renderUI({
req(input$chromosome_select)
selected_chromosome <- input$chromosome_select
defaultTitle <- if(selected_chromosome == "all") {
"Profile for all TEs"
} else {
paste("Profile for", selected_chromosome)
}
textInput("plotTitle", "Plot Title", value = defaultTitle)
})
# Reactive expression for determining plot height
plotHeight <- reactive({
req(input$chromosome_select)
if(input$chromosome_select == "all") {
return(3000) # Height for "all" chromosomes
} else {
return(500) # Height for a specific chromosome
}
})
# Reactive expression for determining plot width
plotWidth <- reactive({
req(input$chromosome_select)
if(input$chromosome_select == "all") {
return(1000) # Height for "all" chromosomes
} else {
return("auto") # Height for a specific chromosome
}
})
output$ggbioPlot <- renderPlot({
req(myData(), input$chromosome_select, input$plotTitle, storedPlots,
!plotGenerationStatus())
selected_chromosome <- input$chromosome_select
# Get the plot from stored plots without redrawing
plot <- if(selected_chromosome == "all") {
req(storedPlots$all)
storedPlots$all
} else {
req(storedPlots$individual[[selected_chromosome]])
storedPlots$individual[[selected_chromosome]]
}
yAxisLabel <- input$yAxisLabel
plotTitle <- input$plotTitle
plot + ggtitle(plotTitle) + ylab(yAxisLabel)
}, height = function(){
plotHeight() # Use the reactive value for plot height
}, width = function(){
plotWidth()
})
output$downloadPlot <- downloadHandler(
filename = function() {
req(myData(), input$chromosome_select)
selected_chromosome <- input$chromosome_select
paste("teProfile_", selected_chromosome, if(input$downloadType == "PDF") ".pdf" else ".png", sep = "")
},
content = function(file) {
req(storedPlots, input$chromosome_select)
selected_chromosome <- input$chromosome_select
yAxisLabel <- input$yAxisLabel
plotTitle <- input$plotTitle
# Set different dimensions based on the chromosome selection
if(selected_chromosome == "all") {
width <- 1000
height <- 3000
plotus <- storedPlots$all + ggtitle(plotTitle) + ylab(yAxisLabel)
} else {
width <- 900
height <- 500
plotus <- storedPlots$individual[[selected_chromosome]] + ggtitle(plotTitle) + ylab(yAxisLabel)
}
if(input$downloadType == "PDF") {
pdf(file, width = width/100, height = height/100)
print(plotus)
dev.off()
} else {
png(file, width = width, height = height)
print(plotus)
dev.off()
}
}
)
}
ui <- dashboardPage(
dashboardHeader(title = "Genomic Data Visualization"),
dashboardSidebar(
sidebarMenu(
id = "sidebarmenu",
menuItem("Data upload", tabName = "data", icon = icon("upload")),
conditionalPanel(
"input.sidebarmenu === 'data'",
selectInput(
inputId = "select_upload",
label = "Please select an option",
choices = c("local" = "local", "online" = "online"),
selected = "local"
),
conditionalPanel(
condition = "input.select_upload === 'local'",
fileInput(inputId = "file_myData", label = "Choose BedGraph file", accept = c(".bed", ".bedgraph"))
),
conditionalPanel(
condition = "input.select_upload === 'online'",
textInput(inputId = "url_myData", label = "Enter URL for BedGraph file", value = ""),
actionButton("submit_url", "Upload")
),
uiOutput("chromosome_selector")
)
),
textInput("yAxisLabel", "Y-axis Label", value = "RPM"),
uiOutput("plotTitle"),
downloadButton("downloadPlot", "Save Plot", class = "btn-primary", style = "margin: 5px 5px 5px 15px;"),
radioButtons("downloadType", "Select File Type", choices = c("PNG", "PDF"))
),
dashboardBody(
tags$head(tags$style(HTML("
#ggbioPlot {
height: 900px;
overflow-y: auto;
}
"))),
tabItems(
tabItem(
tabName = "data",
fluidRow(
box(
title = "TE Profile", solidHeader = TRUE, collapsible = T,
width = 12, plotOutput(outputId = "ggbioPlot", height = "900px", width = "100%"))
)
)
)
)
)
shinyApp(ui, server)