Commit d9018755 authored by mahendra-mariadassou's avatar mahendra-mariadassou
Browse files

Clean code and variable names

parent d9aa3a1a
#' @import shiny
#' @importFrom DT renderDT
#' @importFrom DT renderDT editData
#' @importFrom phyloseq tax_table
#' @importFrom phyloseq.extended write_phyloseq
#' @importFrom shinyjs hide
#' @importFrom shinyjs hide show
#' @importFrom dplyr distinct
app_server <- function(input, output, session) {
# Load package data in the session (for testing purpose)
# data("physeq", package = "affiliationExplorer")
......@@ -30,12 +31,11 @@ app_server <- function(input, output, session) {
physeq <- all$physeq
dict <- all$otu_dictionary
affi <- all$affi
# Ambiguous ASVs and their affiliation
ambiguous_otu <- unique(affi$OTU)
## Store cleaned otu as reactive values
otu <- reactiveValues(
cleaned = ambiguous_otu,
current_aff = NULL
# Store ambiguous ASVs and their affiliation in reactive environment
data <- reactiveValues(
amb_otus = unique(affi$OTU), ## Ambiguous otus
cleaned = phyloseq::tax_table(physeq)[unique(affi$OTU), ] %>% as("matrix"), ## Their current affiliation
affi = NULL ## Placeholder for conflicting affiliations of current ASV
)
# Add ASV Select Input
......@@ -44,22 +44,16 @@ app_server <- function(input, output, session) {
where = "beforeEnd",
ui = selectInput("asv",
label = "Select ASV",
choices = ambiguous_otu,
choices = data$amb_otus,
multiple = FALSE)
)
old_affiliations <- phyloseq::tax_table(physeq)[ambiguous_otu, ] %>% as("matrix")
## Store cleaned affiliations as reactive values
affiliations <- reactiveValues(
cleaned = old_affiliations
)
observeEvent(input$asv, {
# Extract Affiliation for a given OTU
otu$current_aff <- extract_affiliation(affi, input$asv)
amb <- find_level(otu$current_aff)
output$txt <- renderUI({paste(input$asv, "- ", nrow(data) ," affiliation, ambiguity at rank ", amb)})
output$table <- DT::renderDT({otu$current_aff},
data$affi <- extract_affiliation(affi, input$asv) %>% dplyr::distinct()
amb <- find_level(data$affi)
output$txt <- renderUI({paste(input$asv, "- ", nrow(data$affi) ,"conflicting affiliation, ambiguity at rank ", amb)})
output$table <- DT::renderDT({data$affi},
selection = list(mode = 'single', selected = NULL, target = 'row'),
editable = TRUE)
## Show considered replacement if one is selected
......@@ -68,50 +62,34 @@ app_server <- function(input, output, session) {
if (length(s)) {
paste(
"Current affiliation:",
paste(old_affiliations[input$asv, ], collapse = ';'),
paste(data$cleaned[input$asv, ], collapse = ';'),
"to be replaced with:",
paste(otu$current_aff[s, ], collapse = ';'),
paste(data$affi[s, ], collapse = ';'),
sep = "\n")
}
})
})
## Allow manual corrections
observeEvent(input$table_cell_edit, {
# info = input$table_cell_edit
# str(info)
# i = info$row
# j = info$col
# v = info$value
otu$current_aff <<- editData(otu$current_aff, input$table_cell_edit, "table")
data$affi <<- DT::editData(data$affi, input$table_cell_edit, "table")
})
## Replace affiliation upon confirmation
observeEvent(input$clean, {
s = input$table_rows_selected
if (length(s)) {
# cat(paste("Cleaning ASV", input$asv))
# browser()
## Update affiliations
## data <- extract_affiliation(affi, input$asv)
affiliations$cleaned[input$asv, ] <- unlist(otu$current_aff[s, ])
# Update otu
otu$cleaned <- setdiff(otu$cleaned, input$asv)
data$cleaned[input$asv, ] <- unlist(data$affi[s, ])
data$amb_otus <- setdiff(data$amb_otus, input$asv)
updateSelectInput(session, "asv",
label = "Select ASV",
choices = otu$cleaned,
selected = otu$cleaned[1]
choices = data$amb_otus,
selected = data$amb_otus[1]
)
}
})
## Reactive block to update ambiguous_otu, ambiguous_otu_affi and the list when clicking on the "Clean ASV" button
## cleaned_taxonomy <- as.character(data[s, ])
## phyloseq::tax_table(physeq)[input$asv, ] <- cleaned_taxonomy
## ambiguous_otu <- setdiff(ambiguous_otu, input$asv)
## updateUI
output$download <- downloadHandler(
filename = function() {
paste0('cleaned_biom-', Sys.Date(), '.biom')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment