-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
448 lines (401 loc) · 18.1 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
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
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Michael Sierk
# 7/29/20
# Last update: 1/6/20
# changed app to SequaPath, reconfigured diretories, redeployed to shinyapps.io
# -> problem during redeployment when clicking "publish" button, used this command at the console:
# deployApp(appDir=getwd(), appFileManifest="appFileManifest.txt")
#
# For Nephros, Inc.
#
# Read in .csv from EPI2ME software, count reads from each taxid. Output:
# 1) Table of counts normalized by copy number
# 2) Histogram of percentage of reads in the barcode, labeled by pathogenicity
# csv headers
# filename read_id exit_status runid taxid barcode accuracy lca genus_taxid
# Allow user to select file from filesystem
library(shiny)
library(dplyr)
library(ggplot2)
library(taxonomizr) # converts taxid to genus & species
library(writexl) # for writing Excel output
source("getDBs.R") # script for updating DBs
library(shinyBS) # for mouseover popups
library(cowplot)
# Change to TRUE to (re)generate the the taxonomy database nameNode.sqlite
make_taxonomy_db = FALSE
if (make_taxonomy_db) {
makeTaxonomyDB()
}
# Define UI for application
#ui <- fluidPage(theme = shinytheme("flatly"),
ui <- fluidPage(
# Application title
title = "SequaPath",
fluidRow(
column(4, height=100, background = "light-blue",
# Use imageOutput to place the image on the page
imageOutput("logo", height="50px")
),
column(8, height=100, background = "light-blue",
h2("SequaPath Bacterial Census")
)
),
fluidRow(
# Main panel for displaying outputs ----
column(12,
# Output: Tabset w/ plot, summary, and table ----
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("countPlot")),
tabPanel("Output Table", htmlOutput("table_barcode"),
tableOutput("counts_table")),
tabPanel("Input File", h4("Input file"),
textOutput("file1_name"),
tableOutput("input_csv"))
),
tags$hr(style="border-color: black;")
), # main panel
),
fluidRow(
column(4,
# Input: Select a file ----
fileInput("file1", "Choose CSV File (200 MB limit)",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Input: Select which barcode to show
# depends on output.barcode
h4("Barcode:"),
uiOutput("selectBarcode"),
# Add: need free text entry for experiment # (ID?), and notes
# - will be associated with Barcode, included in output table
textInput("barcodeLabel", "Barcode Label", value = "", width = NULL, placeholder = NULL),
# Input: Slider for # of genera to put in plot
sliderInput("reads_cutoff",
"Minimum number of reads:",
min = 1,
max = 50,
value = 30),
bsTooltip("file1", "Input .csv file must have following headers: ",
"top", options = list(container = "body"))
),
column(4,
# Horizontal line ----
h4("Output Table Display"),
# Input: Select number of rows to display ----
radioButtons("disp", "Output Rows:",
choices = c(Head = "head",
All = "all"),
selected = "head", inline=TRUE),
hr(),
h4("Input .csv file info"),
# Input: Checkbox if file has header ----
checkboxInput("header", "File has header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Type of Separator:",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ",", inline = TRUE),
# Input: show input file?
checkboxInput("showInput", "Show input file head", FALSE)
),
column(4,
# Horizontal line ----
h4("Download options"),
# Download the report
div(style = "padding: 4px 4px",
downloadButton("report", "Generate report")
),
# Ouput: download Excel file of the table
#h5("Download Data"),
div(style = "padding: 4px 4px",
downloadButton("downloadTable", "Download Table (Excel)")
),
# Download the histogram plot
div(style = "padding: 4px 4px",
downloadButton("downloadPlot", "Download Plot"),
radioButtons("fileOutType", label = "Select plot file type:",
choices=list("png", "pdf"), inline = TRUE)
)
),
) # fluidRow
) # ui
# Define server logic required to plot the histogram and show tables
server <- function(input, output, session) {
# Allow larger files to be uploaded
options(shiny.maxRequestSize=200*1024^2)
inputTable <- reactive ({
req(input$file1)
# when reading semicolon separated files,
# having a comma separator causes `read.csv` to throw error
tryCatch(
{
reads <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
na.strings=c("-1","NA"))
reads <- na.omit(reads) # get rid of missing barcodes, genera
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
return(reads)
})
load16Sdb <- reactive ({
# get the 16S rRNA copy number
copynumdb <- get16Sdb() # from getDBs.R
return(copynumdb)
})
loadPathogenDB <- reactive ({
pathDB <- getPathogenDB(redo_table = FALSE) # from getDBs.R
return(pathDB)
})
# add a tooltip
addTooltip(session, "file1", "Input .csv file must have following headers: ",
placement = "top", trigger = "hover",
options = NULL)
# get the list of barcodes from input file for the dropdown menu
output$selectBarcode <- renderUI ({
reads = inputTable()
selectInput("barcode", NULL, sort(unique(reads$barcode)))
})
countTable <- reactive ({
reads = inputTable()
# input table header: filename,read_id,exit_status,runid,taxid,barcode,accuracy,lca,genus_taxid
# keep taxid, barcode, accuracy, genus_taxid
# reads below threshold have taxid, genus_taxid = -1
# - count the reads by barcode and by genus
# - divide by copy number if have it
# - calculate the % of barcode reads
# - get the taxon name
counts <- reads %>%
select(barcode, taxid, genus_taxid, accuracy) %>%
group_by(barcode) %>%
count(barcode, genus_taxid, name="genus_reads") %>%
mutate(barcode_reads=sum(genus_reads)) %>%
mutate(genus = getTaxonomy(genus_taxid, sqlFile = "db/nameNode.sqlite",
desiredTaxa = c("genus"))) # getTaxonomy from taxonomizr
# get the species(?)
#mutate(species)=getTaxonomy(genus_taxid, sqlFile = "nameNode.sqlite",
# desiredTaxa = c("species"))
# get the 16S rRNA copy number
copynumdb <- load16Sdb()
# taxid rank name childcount min max mode median mean stddev sum16slist
# According to PMC4021573
# r = c/g x 100/S, where r = adjusted reads, c = counts, g = copy #, S=community richness
counts <- left_join(counts, copynumdb, by = c("genus_taxid" = "taxid"))
counts$mean[is.na(counts$mean)] <- 1 # change the ones not in the DB to 1
# 8/12/20 divide reads by copy number, use that to calculate % of barcode
counts <- counts %>%
mutate(genus_reads_norm = genus_reads/mean) %>%
mutate(perc_of_barcode = 100*genus_reads_norm/barcode_reads)
# get pathogen data here
# genusTaxid Genus Species Pathogen(N/P1/P2/P3) Biofilm(B) Gram (Pos/Neg) Notes and Synonyms
# Pathogen = P1 - severe pathogen, P2 - pathogen, P3 = ?, N - nonpathogen
pathDB <- loadPathogenDB() # from getDBs.R
#print("pathDB: ")
#print(head(pathDB))
# reads in as text, convert genus_taxid to numeric for join
# Note 8/25/20: genus_taxid is being converted to a float
pathDB <- transform(pathDB, genus_taxid=suppressWarnings(as.integer(genusTaxid)))
counts <- left_join(counts, pathDB, by="genus_taxid")
counts$Pathogen[is.na(counts$Pathogen)] <- "U" # make NAs U for Unknown
#print("Counts table: ")
#print(head(counts))
return(counts)
})
# select out just the data from selected barcode for plotting/output
barcodeTable <- reactive ({
if(is.null(input$barcode)){return(NULL)}
counts <- countTable()
#head(counts)
# full table headers:
#barcode genus_taxid genus_reads barcode_reads genus rank name childcount min max mode median mean stddev sum16slist genus_reads_norm perc_of_barcode Genus Pathogen
countsOut <- counts %>%
filter(genus_reads >= input$reads_cutoff & barcode==input$barcode) %>%
select(barcode, genus, Pathogen, Biofilm,
genus_reads, perc_of_barcode,
mean, barcode_reads,
genus_reads_norm) %>%
arrange(desc(perc_of_barcode))
return(countsOut)
})
barcodeReads <- reactive ({
# want to keep barcode reads as a separate variable
countsOut <- barcodeTable()
get_barcode_reads <- countsOut %>% slice(1)
barcodeReads <- get_barcode_reads$barcode_reads
return(barcodeReads)
})
createPlot <- reactive ({
# wait until we have an input file
req(input$file1)
# wait until input$barcode is set
if(is.null(input$barcode)){return(NULL)}
plot_counts <- barcodeTable()
# make the plot object
group.colors <- c(P1 ="red", P2 = "blue", P3 = "light blue", N = "light green", U = "gray")
group.labels <- c(P1 = "CDC Pathogen", P2 = "Nephros Pathogen", P3 = "Koch Institute Pathogen",
N = "Nonpathogen", U = "Unknown")
# col = Biofilm, plus scale_color_manual outlines genera that are Biofilm producers
pl <- ggplot(plot_counts, aes(genus, perc_of_barcode, fill=Pathogen, group=Biofilm)) +
geom_bar(aes(col = Biofilm), stat="identity") +
theme_cowplot(12) +
theme(plot.title = element_text(size = 18, face = "bold"),
plot.subtitle = element_text(size = 14),
plot.caption = element_text(size = 14, face = "italic"),
axis.text.x = element_text(angle = 90, size = 14, hjust = 1, vjust = 0.5),
axis.title.x = element_text(face = "bold", size = 18),
axis.text.y = element_text(size = 14),
axis.title.y = element_text(face = "bold", size = 18),
legend.text = element_text(size = 14),
legend.title = element_text(size = 14, face = "bold"),
legend.key.size = unit(6,"mm")) +
xlab("Genus") +
ylab(paste0("% of Barcode Reads\n(min reads: ",
input$reads_cutoff,") ")) +
guides(fill = guide_legend(ncol=1),
shape = guide_legend(override.aes = list(size = 0.5))) +
#Specify colours
scale_fill_manual(values=group.colors, labels=group.labels) +
scale_color_manual(values=c(B="black"), guide = "none") +
labs(title = input$barcode, subtitle = input$barcodeLabel, caption = "Outline = Biofilm producer")
#annotate("text", Inf, Inf, label = "Outline = Biofilm producer", hjust = 1, vjust = 1, size = 5)
pl <- ggdraw(pl) +
draw_image("nephros_logo.jpg", x = 0.95, y = 0.1, hjust = 1, vjust = 1, width = 0.1, height = 0.1)
return(pl)
})
# display the logo with renderImage
output$logo <- renderImage({
# Return a list containing the filename and alt text
list(src = "nephros_logo.jpg", alt = "logo", height=50)
}, deleteFile = FALSE)
output$countPlot <- renderPlot({
pl <- createPlot()
print(pl)
})
# print out the name of the input file
output$file1_name <- renderText({
paste("file name: ", input$file1$name)
})
# display the input table
output$input_csv <- renderTable({
if (input$showInput == TRUE) {
reads = inputTable()
return(head(reads))
}
},
# attempt to format column widths
#options = list(
# autoWidth = TRUE,
# columnDefs = list(list(width = '200px', targets = "_all")))
)
# for selected columns: targets = c(1,3)
# To set different column widths for multiple columns you can use:
#columnDefs = (list(list(width = '200px', targets =c(0, 2)),
#list(width = '80px', targets =c(6))))
# print out the barcode and label before rendering the table
output$table_barcode <- renderUI({
barcodeReads <- barcodeReads()
str1 <- paste(input$barcode,"(",input$barcodeLabel,")")
str2 <- paste("Barcode reads: ", barcodeReads)
HTML(paste(str1, str2, sep='<br/>'))
})
output$counts_table <- renderTable({
# display the output table
table_counts = barcodeTable()
get_barcode_reads <- distinct(table_counts, barcode_reads)
barcodeReads <- get_barcode_reads$barcode_reads
print(paste("Barcode reads: ", barcodeReads))
table_counts <- table_counts %>%
ungroup() %>%
select('Genus' = genus, Pathogen, Biofilm, '% of barcode' = perc_of_barcode,
'Normalized genus reads' = genus_reads_norm, genus_reads,
'Copy number' = mean)
if(input$disp == "head") {
return(head(table_counts))
}
else {
return(table_counts)
}
})
### Print a report
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
tempLogo <- file.path(tempdir(), "nephros_logo.png")
file.copy("nephros_logo.png", tempLogo, overwrite = TRUE)
tempLogo <- file.path(tempdir(), "nephros_logo.jpg")
file.copy("nephros_logo.jpg", tempLogo, overwrite = TRUE)
tempsty <- file.path(tempdir(), "unicode-math.sty")
file.copy("unicode-math.sty", tempsty, overwrite = TRUE)
# Set up parameters to pass to Rmd document
# Need to figure out what parameters to send here
# the plot
# the input file name
# the barcode (multiple plots for different barcodes?)
# table output for top 10 genera?
table_counts = barcodeTable()
barcodeReads = barcodeReads()
table_counts <- table_counts %>%
ungroup() %>%
select('Genus' = genus, Pathogen, Biofilm, '% of barcode' = perc_of_barcode,
'Normalized genus reads' = genus_reads_norm, 'Genus reads' = genus_reads,
'Copy number' = mean) %>%
mutate_if(is.numeric, format, digits=2)
params <- list(pl = createPlot(), inpName = input$file1$name, barcode=input$barcode,
barcodeLabel = input$barcodeLabel, barcodeReads = barcodeReads,
table_counts = table_counts)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
### Download the plot
output$downloadPlot <- downloadHandler(
filename = function(){
paste(input$barcode, input$fileOutType, sep='.')
},
content = function(file) {
if (input$fileOutType=="png")
png(file, width=10, height=7, units="in", res=300)
else
pdf(file, width=10, height=7)
pl <- createPlot()
print(pl)
dev.off()
})
# Download Excel version of output table ----
output$downloadTable <- downloadHandler(
filename = function() {
paste(input$barcode, ".xlsx", sep = "")
},
content = function(file) {
table_counts <- barcodeTable()
table_counts <- table_counts %>%
ungroup() %>%
select('Genus' = genus, Pathogen, Biofilm, '% of barcode' = perc_of_barcode,
'Normalized genus reads' = genus_reads_norm, 'Genus reads' = genus_reads,
'Copy number' = mean, 'Barcode reads' = barcode_reads)
write_xlsx(table_counts, file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)