-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
213 lines (192 loc) · 6.75 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
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# load libraries
library(shiny)
library(shinythemes)
library(tidyverse)
library(plotly)
library(biomformat)
library(magrittr)
library(reshape2)
library(vegan)
library(ggsci)
library(ggdendro)
# import utility functions
source("./utils.R")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Shiny theme
theme = shinytheme("united"),
# Application title
titlePanel("MicroExplorer"),
# Navbar Pages
navbarPage("MicroExplorer",
tabPanel("Upload Data",
sidebarLayout(
sidebarPanel(
tags$div(tags$h3("Upload Files"),
style="margin-bottom:50px"),
radioButtons("fileFormat", label="Select File Format",
choices=c("CSV","BIOM"), inline = TRUE),
conditionalPanel(
condition = "input.fileFormat == 'CSV'",
fileInput("countFile", label="Choose Count Data CSV"),
fileInput("taxaFile", label="Choose Taxonomy Data CSV"),
fileInput("sampleFile", label="Choose Sample Data CSV")),
conditionalPanel(
condition = "input.fileFormat == 'BIOM'",
fileInput("biomFile", label="Choose BIOM File")),
actionButton("validate", "Load Data")),
mainPanel(
textOutput("validMsg")
)
)
),
tabPanel("Filter",
sidebarLayout(
sidebarPanel(
uiOutput("filterUI")
),
mainPanel(
plotOutput("hist_valid"),
plotOutput("hist_filtered")
)
)
),
tabPanel("Visualize",
sidebarLayout(
sidebarPanel(
uiOutput("visualizeUI")
),
mainPanel(
tabsetPanel(type="tabs",
tabPanel("Stacked Bars", plotlyOutput("stackedbar")),
tabPanel("Heatmap", plotlyOutput("heatmap"))
)
)
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
#############
# Upload Data
#############
# Reactive Raw Data
rawData <- reactive({
if (input$fileFormat == "CSV") {
req(!is.null(input$countFile) & !is.null(input$taxaFile) & !is.null(input$sampleFile))
csv2dat(input$countFile$datapath, input$taxaFile$datapath, input$sampleFile$datapath)
} else if (input$fileFormat == "BIOM") {
req(!is.null(input$biomeFile))
biom2dat(input$biomFile$datapath)
}
})
# Reactive Validate Data
validData <- eventReactive(input$validate, {
validateInputs(rawData()$countData, rawData()$taxaData, rawData()$sampleData)
})
# valid message
output$validMsg <- renderText({
req(!is.null(validData()))
validData()$msg
})
#############
# Filter Data
#############
# render Filtering UI
output$filterUI <- renderUI({
req(!(grepl("ERROR", validData()$msg)))
list(
tags$div(tags$h3("Filter Data"), style="margin-bottom:50px"),
tags$div(tags$h4("Filter Samples by Sequencing Depth: ")),
numericInput("seqDepth", "Min reads",
min = 0, max = max(colSums(validData()$countData)),
value = 5000),
tags$div(tags$h4("Filter Taxa by Prevalence: "), style="margin-top:50px"),
numericInput("minAbund", "Minimum Abundance (%)",
min = 0, max = 100, value = 0.01),
numericInput("minSamples", "In at least N (%) Samples",
min = 0, max = 100, value = 5),
tags$div(tags$h4("Filter Samples by Metadata: "), style="margin-top:50px"),
actionButton("filter", "Filter")
)
})
# Filter Data
filteredData <- eventReactive(input$filter, {
filterData(validData()$countData, validData()$taxaData, validData()$sampleData,
input$seqDepth, input$minAbund, input$minSamples)
})
# filtered data main panel
output$hist_valid <- renderPlot({
req(!is.null(validData()) & !(grepl("ERROR", validData()$msg)))
seqDepth <- colSums(validData()$countData)
hist(seqDepth, breaks=20)
})
output$hist_filtered <- renderPlot({
req(!is.null(filteredData()))
seqDepth <- colSums(filteredData()$countData)
hist(seqDepth, breaks=20)
})
###################
# Stacked Bar Plots
###################
# render visualize UI
output$visualizeUI <- renderUI({
req(!is.null(filteredData()$countData))
list(
radioButtons("plotDataset", label=h4("Dataset"),
choices=c("Raw","Filtered"), inline = TRUE,
selected = "Filtered"),
selectInput("taxaLevel", h4("Taxonomy Level"),
choices=base::colnames(filteredData()$taxaData)),
radioButtons("taxa2Plot", label = h4("Taxas to plot"),
choices=c("All", "MostAbundant"), inline = TRUE),
conditionalPanel(
condition = "input.taxa2Plot == 'MostAbundant'",
sliderInput("numTaxa2Plot", label = h5("Maximum Taxa to plot:"),
min = 1, max = 20, value = 10)
),
radioButtons("sortMethod", label= h4("Sort Samples"),
choices = c("Decreasing Taxa Abunance", "Cluster by Dissimilarity"),
selected = "Cluster by Dissimilarity"),
selectInput("facetField", h4("Facet By"),
choices=c("None", base::colnames(filteredData()$sampleData)), selected = "None")
)
})
# render stacked bar plot
output$stackedbar <- renderPlotly({
req(!is.null(input$taxaLevel))
if (input$plotDataset == "Filtered") {
plotStackedBar(filteredData()$countData, filteredData()$taxaData, filteredData()$sampleData,
input$taxaLevel, input$taxa2Plot, input$numTaxa2Plot,
input$sortMethod, input$facetField)
} else {
plotStackedBar(validData()$countData, validData()$taxaData, validData()$sampleData,
input$taxaLevel, input$taxa2Plot, input$numTaxa2Plot,
input$sortMethod, input$facetField)
}
})
# render heatmap
output$heatmap <- renderPlotly({
req(!is.null(input$taxaLevel))
if (input$plotDataset == "Filtered") {
plotHeatMap(filteredData()$countData, filteredData()$taxaData, filteredData()$sampleData,
input$taxaLevel, input$taxa2Plot, input$numTaxa2Plot,
input$sortMethod, input$facetField)
} else {
plotHeatMap(validData()$countData, validData()$taxaData, validData()$sampleData,
input$taxaLevel, input$taxa2Plot, input$numTaxa2Plot,
input$sortMethod, input$facetField)
}
})
}
# Run the application
shinyApp(ui = ui, server = server)