-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
351 lines (264 loc) · 10 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
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
# Load libraries and data -------------------------------------------------
library(shiny)
library(DT)
library(tidyverse)
# library(BiocManager)
# options(repos = BiocManager::repositories())
biomart_table <- readRDS("app_data/biomart_table.Rds")
example_data <- read_lines("example_data/shiny_app_test_data.txt")
# First define the UI section ---------------------------------------------
ui <- fluidPage(
# Link to some custom CSS
tags$head(tags$link(
rel = "stylesheet", type = "text/css", href = "css/user.css"
)),
title = "Human ID Converter",
# Select the Bootswatch3 "Readable": https://bootswatch.com/3/readable/
theme = "css/readablebootstrap.css",
# Header/title of the app, which has some custom tweaks applied in
# "user.css"
tags$h1("Human ID Converter"),
sidebarLayout(
sidebarPanel = tags$div(
class = "col-sm-4 manual-sidebar",
tags$form(
class = "well",
tags$h3("Hello!"),
tags$p(HTML(
"Welcome to <b>Human ID Converter</b>, an app ",
"designed to facilitate mapping between different ",
"human gene identifiers. It functions by searching a ",
"table with your input genes and returning any ",
"matches. Each input can contain a mix of any of the ",
"supported ID types: HGNC, Ensembl, Entrez, and UniProt."
)),
tags$p(
"The data used for the mapping comes from Ensembl's ",
tags$a(
"BioMart",
href = "http://ensemblgenomes.org/info/access/biomart",
.noWS = c("before", "after")
),
". If you run into any trouble, please ",
"open an issue at the ",
tags$a(
"Github page",
href = "https://github.com/travis-m-blimkie/HumanIDConverter",
.noWS = c("before", "after"),
),
"."
),
tags$p(
"To get started, paste your genes into the field ",
"below (one per line), and click the 'Search' button ",
"to see your results."
),
tags$br(),
# Field for user to input their genes
textAreaInput(
inputId = "pastedInput",
label = NULL,
placeholder = "Your genes here...",
height = 155
),
# Link to load example data, primarily to making testing
# easier
actionLink(
inputId = "tryExample",
label = "Load Example Data",
style = "font-size: 110%"
),
# Search button, which is a trigger for lots of
# outputs/buttons
actionButton(
class = "btn-primary",
style = "float: right; padding-bottom: 10px",
inputId = "search",
label = "Search",
icon = icon("search")
),
tags$br(),
tags$br(),
# Download button and some text for matching genes
uiOutput("matchedBtn"),
# Download button and some text for non-matching genes
uiOutput("nonMatchedBtn")
) # Closes the well/form
), # Closes sidebar div/well
mainPanel = tags$div(
class = "col-sm-8",
tags$br(),
# Output table of matching genes
uiOutput("matchedPanel"),
# Output table of non-matching genes
uiOutput("nonMatchedPanel")
) # Closes mainPanel() div
) # Closes the sidebarLayout()
) # Closes the fluidPage() UI call
# Now the server section --------------------------------------------------
server <- function(input, output) {
inputGenes <- reactiveVal()
# Load in example data when linked clicked, and provide a notification. Note
# the "message" notification type has been modified; see "www/user.css" for
# details.
observeEvent(input$tryExample, {
inputGenes(example_data)
showNotification(
id = "exampleSuccess",
duration = 5,
closeButton = TRUE,
type = "message",
ui = paste0(
"Example data successfully loaded! Click the Search ",
"button to continue."
)
)
})
# Take the user's input and clean it up, matching one space or newline
observeEvent(input$pastedInput, {
input$pastedInput %>%
str_split(., pattern = " |\n") %>%
unlist() %>%
inputGenes()
}, ignoreInit = TRUE, ignoreNULL = TRUE)
# Now look for the user's genes in the biomaRt table
matchedGenes <- reactive({
req(inputGenes())
biomart_table %>%
filter_all(., any_vars(. %in% inputGenes())) %>%
distinct(HGNC, .keep_all = TRUE) %>%
arrange(HGNC)
})
# Get the genes that didn't have matches
nonMatchedGenes <- reactive({
req(matchedGenes())
myMatches <- unlist(matchedGenes()) %>% as.character()
noMatches <-
tibble("Input Genes" = setdiff(inputGenes(), myMatches)) %>%
arrange(1)
return(noMatches)
})
# Creating a character vector version of non-matching genes for download
# purposes
nonMatchedGenes_chr <- reactive({
req(nonMatchedGenes())
nonMatchedGenes() %>% pull(1)
})
# Output tables -------------------------------------------------------
# First for the matching genes
output$matchedTable <- DT::renderDataTable(
isolate(matchedGenes()),
rownames = FALSE,
options = list(
scrollX = "100%",
scrollY = "250px",
scrollCollapse = TRUE,
paging = FALSE
)
)
observeEvent(input$search, {
output$matchedPanel <- renderUI({
if (nrow(matchedGenes()) != 0) {
return(
tagList(
tags$h3("We found matches for the following genes:"),
DT::dataTableOutput("matchedTable")
)
)
} else {
return(NULL)
}
})
}, ignoreNULL = TRUE, ignoreInit = TRUE)
# Now for non-matching genes
output$nonMatchedTable <- DT::renderDataTable(
isolate(nonMatchedGenes()),
rownames = FALSE,
options = list(
scrollX = "100%",
scrollY = "250px",
scrollCollapse = TRUE,
paging = FALSE
)
)
observeEvent(input$search, {
output$nonMatchedPanel <- renderUI({
isolate(nonMatchedGenes())
if (nrow(nonMatchedGenes()) != 0) {
return(tagList(
tags$hr(),
tags$br(),
tags$h3("The following genes did not have a match:"),
DT::dataTableOutput("nonMatchedTable"),
tags$br()
))
} else {
return(NULL)
}
})
}, ignoreNULL = TRUE, ignoreInit = TRUE)
# Download buttons ----------------------------------------------------
# First for the matched genes
output$matchedDl <- downloadHandler(
filename = "matching_genes.csv",
content = function(f) {
readr::write_csv(matchedGenes(), path = f)
}
)
observeEvent(input$search, {
output$matchedBtn <- renderUI({
isolate(matchedGenes())
if (nrow(matchedGenes()) != 0) {
tagList(
tags$hr(),
tags$p(
"We successfully matched some of your input ",
"genes. Check the table on the right to see the ",
"results, and click the button below to download ",
"the table."
),
downloadButton(
class = "btn btn-success",
style = "float: right; padding-bottom: 10px",
outputId = "matchedDl",
label = "Matched Genes"
),
tags$br(),
tags$br()
)
}
})
}, ignoreNULL = TRUE, ignoreInit = TRUE)
# Now for the non-matching genes
output$nonMatchedDl <- downloadHandler(
filename = "non_matching_genes.txt",
content = function(f) {
readr::write_lines(nonMatchedGenes_chr(), path = f)
}
)
observeEvent(input$search, {
output$nonMatchedBtn <- renderUI({
isolate(nonMatchedGenes())
if (nrow(nonMatchedGenes()) != 0) {
tagList(
tags$hr(),
tags$p(
"We were unable to find matches for the genes ",
"shown in the bottom table on the right. Click ",
"the button below to download them."
),
downloadButton(
class = "btn btn-warning",
style = "float: right; padding-bottom: 10px",
outputId = "nonMatchedDl",
label = "Non-Matching Genes"
),
tags$br(),
tags$br()
)
}
})
}, ignoreNULL = TRUE, ignoreInit = TRUE)
} # Closes the server() call
# Call the app to start ---------------------------------------------------
shinyApp(ui = ui, server = server)