Skip to content

Commit

Permalink
fix: add conflict management
Browse files Browse the repository at this point in the history
  • Loading branch information
gregfrasco committed Nov 12, 2024
1 parent 33ba61a commit f5a61a3
Showing 1 changed file with 63 additions and 45 deletions.
108 changes: 63 additions & 45 deletions comets_shinyapp_example/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,40 @@ library(data.table)
library(ggplot2)
library(tidyverse)

# Use conflicted to set preferences for conflicting functions
conflict_prefer("between", "data.table")
conflict_prefer("filter", "dplyr")
conflict_prefer("first", "dplyr")
conflict_prefer("hour", "lubridate")
conflict_prefer("isoweek", "lubridate")
conflict_prefer("lag", "dplyr")
conflict_prefer("last", "dplyr")
conflict_prefer("mday", "lubridate")
conflict_prefer("minute", "lubridate")
conflict_prefer("month", "lubridate")
conflict_prefer("quarter", "lubridate")
conflict_prefer("second", "lubridate")
conflict_prefer("transpose", "purrr")
conflict_prefer("wday", "lubridate")
conflict_prefer("week", "lubridate")
conflict_prefer("yday", "lubridate")
conflict_prefer("year", "lubridate")

abundance_df = fread("./species_abundance_filt.csv",
nThread = 8, drop = 1, header = T) %>%
abundance_df = fread("./species_abundance_filt.csv",
nThread = 8, drop = 1, header = T) %>%
rename("temperature"="soilTemp","pH" = "soilInCaClpH", "abundance"="percentage")

# Read in data file for species selections - info on environmental preferences
organism_df_to_subset = fread("./organism_data_to_subset.csv", drop = 1, header = T) %>%
mutate(taxon=`Species of interest`) %>%
organism_df_to_subset = fread("./organism_data_to_subset.csv", drop = 1, header = T) %>%
mutate(taxon=`Species of interest`) %>%
rename("Genome source" = source)

# Read in data file to just show species names/links
set.seed(1)
organism_df_to_print = fread("./organism_data_to_print.csv", drop = 1, header = T)

# Read in biome data file
biome_info = fread("./nlcd_key.csv", drop = 1, header = T)
biome_info = fread("./nlcd_key.csv", drop = 1, header = T)
biome_choices = biome_info$nlcdClass
names(biome_choices) = biome_info$prettyNlcd

Expand All @@ -32,32 +50,32 @@ library(shinyhelper) # For adding help tooltips
# UI
ui <- fluidPage(
titlePanel("SoilMicrobeDB: An Interactive Database of Soil Microbial Genomes"),

tags$p("The SoilMicrobeDB is a collection of over 30,000 soil microbial genomes, each annotated with ecological preferences for environmental conditions such as pH, temperature, and biome type. This tool allows you to filter, analyze, and visualize data on microbial species across different soil environments, using the sample collection from the National Ecological Observatory Network (NEON)."),

tags$h4("Filters"),
fluidRow(
column(4,
column(4,
selectInput("biome", "Select Biome", choices = unique(organism_df_to_subset$biome), multiple = TRUE) %>%
shinyhelper::helper(
type = "inline",
title = "Biome Preference",
type = "inline",
title = "Biome Preference",
content = "Biome preference is assigned if a taxon is present in at least 2% of samples within the biome. Biomes are assigned to each NEON sample using the National Land Cover Database.",
icon = "question-circle"
)


),
column(4,
sliderInput("pH_range", "pH Preference Range", min = 3, max = 9, value = c(3, 9)) %>%
column(4,
sliderInput("pH_range", "pH Preference Range", min = 3, max = 9, value = c(3, 9)) %>%
shinyhelper::helper(
type = "inline",
title = "pH Preference",
content = "pH preference of each taxon is assigned as the peak of a LOESS curve fit to abundance data across the range of pH values. Click on a taxon to visualize or download this data.",
icon = "question-circle"
)
),
column(4,
column(4,
sliderInput("temperature_range", "Temperature Preference Range", min = 0, max = 40, value = c(0, 40)) %>%
shinyhelper::helper(
type = "inline",
Expand All @@ -67,20 +85,20 @@ ui <- fluidPage(
)
)
),

tags$h4("Organism Data Table"),
DT::dataTableOutput("organism_table"),
downloadButton("download_organism", "Download Taxon List"),

uiOutput("modal_abundance_plot")
)

# Server
server <- function(input, output, session) {

# Initialize shinyhelper
shinyhelper::observe_helpers(withMathJax = TRUE)

# Reactive Filtered Organism DataFrame
filtered_organism_df <- reactive({
organism_df_to_subset %>%
Expand All @@ -90,28 +108,28 @@ server <- function(input, output, session) {
between(temperature_preference, input$temperature_range[1], input$temperature_range[2])
)
})

# Display Organism Data Table
output$organism_table <- DT::renderDataTable({
filtered_organism_df() %>% select(Kingdom, Genus, "Species of interest", "Genome source", "Functional in COMETS?")
}, selection = 'single')

# Download Filtered Organism Data
output$download_organism <- downloadHandler(
filename = function() { paste("filtered_organism_data.csv") },
content = function(file) { write.csv(filtered_organism_df(), file, row.names = FALSE) }
)

# Modal Abundance Plot
output$modal_abundance_plot <- renderUI({
req(input$organism_table_rows_selected)
selected_row <- filtered_organism_df()[input$organism_table_rows_selected, ]
selected_taxon <- selected_row$taxon

# Filter abundance_df for selected taxon and check for data
abundance_filtered <- abundance_df %>%
filter(taxon == selected_taxon)

if (nrow(abundance_filtered) == 0) {
# Show error message if no data is available for the selected taxon
modalDialog(
Expand All @@ -135,68 +153,68 @@ server <- function(input, output, session) {
)
}
})

# pH Plot
output$pH_plot <- renderPlot({
req(input$organism_table_rows_selected)
selected_taxon <- filtered_organism_df()[input$organism_table_rows_selected, "taxon"]
abundance_data <- abundance_df %>% filter(taxon == selected_taxon)

if (nrow(abundance_data) == 0) {
return(NULL) # Avoid rendering if no data
}
ggplot(abundance_data,

ggplot(abundance_data,
aes(x = pH, y = abundance)) +#, color=nlcdClass)) +
geom_point(aes(color=biome),
alpha=.5,
position=position_jitter(width = .01, height=0), size=2) +
alpha=.5,
position=position_jitter(width = .01, height=0), size=2) +
#geom_smooth(method = "loess",show.legend = F, span=.7) +
geom_smooth(method="gam",,
#method = "loess",
show.legend = F, se=F) +
theme_bw(base_size = 18) +
#scale_y_sqrt() +
xlab("Soil pH") +
theme_bw(base_size = 18) +
#scale_y_sqrt() +
xlab("Soil pH") +
labs(title = paste("Abundance vs. pH for", selected_taxon)) +
ylab("Microbial abundance")
})

# Temperature Plot
output$temperature_plot <- renderPlot({
req(input$organism_table_rows_selected)
selected_taxon <- filtered_organism_df()[input$organism_table_rows_selected, "taxon"]
abundance_data <- abundance_df %>% filter(taxon == selected_taxon)

if (nrow(abundance_data) == 0) {
return(NULL) # Avoid rendering if no data
}
ggplot(abundance_data,

ggplot(abundance_data,
aes(x = temperature, y = abundance#, color=nlcdClass
)) +
geom_point(aes(color=biome),
alpha=.5,
position=position_jitter(width = .01, height=0), size=2) +
alpha=.5,
position=position_jitter(width = .01, height=0), size=2) +
# geom_smooth(method = "loess", show.legend = F, span=.7) +
geom_smooth(method="gam",
#method = "loess",
#method = "loess",
show.legend = F, se=F) +
theme_bw(base_size = 18) +
#scale_y_sqrt() +
theme_bw(base_size = 18) +
#scale_y_sqrt() +
xlab("Soil temperature") +
ylab("Microbial abundance") +
labs(title = paste("Abundance vs. temperature for", selected_taxon))

})

# Download Filtered Abundance Data
output$download_filtered_abundance <- downloadHandler(
filename = function() { paste("taxon_abundance_data.csv") },
content = function(file) {
selected_taxon <- filtered_organism_df()[input$organism_table_rows_selected, "taxon"]
abundance_data <- abundance_df %>% filter(taxon == selected_taxon)

if (nrow(abundance_data) > 0) {
write.csv(abundance_data, file, row.names = FALSE)
}
Expand Down

0 comments on commit f5a61a3

Please sign in to comment.