Skip to content
Snippets Groups Projects
shinyUI.R 49 KiB
Newer Older
Todor Kondic's avatar
Todor Kondic committed
mkUI <- function() {
    browseFile <- function(title,
                           buttonName,
                           txtName,
                           buttonTxt="Import compound list.",
                           description=NULL,
Todor Kondic's avatar
Todor Kondic committed
                           ...) {
        shinydashboard::box(title=title,
                            shiny::h5(description),
Todor Kondic's avatar
Todor Kondic committed
                            solidHeader=T,
                            collapsible=F,...)}
    
    confImport <- shinydashboard::box(title="Import",
Todor Kondic's avatar
Todor Kondic committed
                                      shiny::h5("There are two tables that need to be supplied before prescreening starts. One is the compound list, its format being the same like the one for the RMassBank (fields: ID,Name,SMILES,RT,CAS,mz,Level). Another is the compound set table (fields: ID,set). Once those tables are imported, they can further be modified as copies inside the project dir. Shinyscreen will never modify any initial (meta)data. If set field of the compound set table is NA, then that file is in a set of its own. The table format should be CSV with `,' as delimiter and any strings that possibly contain commas should be protected. LibreOffice Calc is helpful when it is needed to convert CSVs from one format to another, as well as protecting strings with quotes. "),
                                      shiny::textInput("impCmpListInp",
Todor Kondic's avatar
Todor Kondic committed
                                                       "Imported compound list.",
                                                       value=""),
Todor Kondic's avatar
Todor Kondic committed
                                      shiny::textInput("impSetIdInp",
Todor Kondic's avatar
Todor Kondic committed
                                                       "Compound set table.",
                                                       value=""),
                                      shiny::textInput("impGenRMBInp",
                                                       "RMassBank settings.",
Todor Kondic's avatar
Todor Kondic committed
                                                       value=""),
                                      shinyFiles::shinyFilesButton("impCmpListB",
                                                                   label="Import compound list.",
                                                                   title="",
                                                                   icon=shiny::icon("file"),
Todor Kondic's avatar
Todor Kondic committed
                                                                   multiple=F),
                                      shinyFiles::shinyFilesButton("impSetIdB",
                                                                   label="Import compound set table.",
                                                                   title="",
                                                                   icon=shiny::icon("file"),
                                                                   multiple=T),
Todor Kondic's avatar
Todor Kondic committed
                                      shinyFiles::shinyFilesButton("impGenRMBB",
                                                                   label="Import RMassBank settings.",
                                                                   title="",
                                                                   icon=shiny::icon("file"),
                                                                   multiple=F),
                                      width=NULL)

    confmzMLTags <- shinydashboard::box(title="Sets and Tags",
                                        shiny::h5("Shinyscreen uses two properties, tags and sets, to categorise mzML data. Tags are properties of individual files. For example, if a single file represents a collection of spectra acquired at a specific collision energy, that energy could be used as a tag. Tags are used to differentiate the spectra in a chromatogram. Sets are collections of tagged files and are read from the compound set table. Each set is going to be screened for a designated collection of masses."),
                                        shiny::textInput("tagPropInp",
Todor Kondic's avatar
Todor Kondic committed
                                                         "What is a tag? (example: collision energy; can be left empty.)",
                                                         value=""),
                                        shiny::textInput("tagsInp",
                                                         "Comma-delimited list of tag types",
                                                         value=""),
                                        width=NULL)

    confState <- shinydashboard::box(title="Configuration state",
Todor Kondic's avatar
Todor Kondic committed
                                     shiny::h5("Saves and restores current configuration."),
                                     shinyFiles::shinySaveButton("saveConfB",
                                                                 "Save configuration",
                                                                 title="Save",
                                                                 filename = "conf-state.rds",
                                                                 "rds"),
                                     shinyFiles::shinyFilesButton("restoreConfB",
                                                                  label="Restore configuration",
                                                                  multiple=F,
                                                                  title="Restore"),

    confPP<-shinydashboard::box(title="Preprocessing settings",
                                shiny::textInput("confFileTabBase",
                                                 "Basic file table.",
                                                 value=FN_FTAB_BASE),
                                shiny::textInput("confFileTabProcInp",
                                                 "Preprocessed file table.",
                                                 value=FN_FTAB_PP),
                                shiny::textInput("confResFileTab",
                                                 "Resulting file table.",
                                                 value=FN_FTAB),
                                width=NULL)
    
    confmzMLtab <-shinydashboard::box(title="mzML file table",
Todor Kondic's avatar
Todor Kondic committed
                                      shiny::h5("Use this file table to assign adduct modes and tags to the data files."),
                                      shinyFiles::shinyFilesButton("mzMLB",
                                                                   label="Select mzML files",
                                                                   title="Select mzML files",
Todor Kondic's avatar
Todor Kondic committed
                                                                   icon=shiny::icon("files-o"),
                                                                   multiple=T),
Todor Kondic's avatar
Todor Kondic committed
                                      shiny::actionButton("mzMLtabSubm",
                                                          label="Submit mzML list.",
                                                          icon=shiny::icon("check")),
                                      rhandsontable::rHandsontableOutput("mzMLtabCtrl"),
                                      width=NULL)

    
    confLayout <- shiny::fluidRow(shiny::column(confImport,
Todor Kondic's avatar
Todor Kondic committed
                                                confmzMLTags,
                                                confState,
                                                width=4),
                                  shiny::column(width=8,
                                                confmzMLtab))

    confTab <- shinydashboard::tabItem(tabName="config",
                                       confLayout)

    ## ***** Compound List Tab *****

Todor Kondic's avatar
Todor Kondic committed
    cmpListBox<-shinydashboard::box(title="Compound list",
                                    rhandsontable::rHandsontableOutput("cmpListCtrl"),
                                    width=NULL)
    
    cmpListLayout <- shiny::fluidRow(shiny::column(cmpListBox,
                                                   width = 12))
    cmpListTab <- shinydashboard::tabItem(tabName="compList",
Todor Kondic's avatar
Todor Kondic committed
                                          shiny::h5("This is an editable view of the compound list."),
                                          cmpListLayout)

    ## ***** Sets of compounds *****

    setIdBox<-shinydashboard::box(title="Compound sets",
Todor Kondic's avatar
Todor Kondic committed
                                  rhandsontable::rHandsontableOutput("setIdTabCtrl"),
                                  width = NULL)

    setIdLayout<-shiny::fluidRow(shiny::column(setIdBox,
                                               width = 12))

    setIdTab<-shinydashboard::tabItem(tabName="setId",
Todor Kondic's avatar
Todor Kondic committed
                                      shiny::h5("This is an editable view of the id/set list."),
                                      setIdLayout)
    genBoxParam1<-shinydashboard::box(title="Parameters of the run",
                                      shiny::textInput("genNoProc",
                                                      label="Number of processes.",
                                                      value=1),
                                      shiny::textInput("ppmLimFine",
                                                       label="ppm limit fine",
                                                       value=10),
                                      shiny::textInput("eicLim",
                                                       label="EIC limit.",
                                                       value=1e-3),
                                      shiny::textInput("intTresh",
                                                       label="Intensity threshold.",
                                                       value=1e5),
                                      shiny::textInput("noiseFac",
                                                       label="Signal-to-noise ratio.",
                                                       value=3),
                                      shiny::textInput("rtDelta",
                                                       label="Retention time Δ",
                                                       value=0.5),
                                      width=NULL)
    
    genBoxParam2<-shinydashboard::box(title=NULL,
                                      shiny::selectInput("genSetSelInp",
                                                         label="Select set(s).",
                                                         choices="",
                                                         multiple=T),
                                      shiny::actionButton(inputId="genRunB",
                                                          label="Run!",
                                                          icon=shiny::icon("bomb")),
                                      width=NULL)
    genBoxProcessed<-shinydashboard::box(title="Processed sets",
                                         shiny::actionButton(inputId="genFileTabB",
                                                             label="Generate file table.",
                                                             icon=shiny::icon("save")),
                                         shiny::actionButton(inputId="genRunPPB",
                                                             label="Preprocess!",
                                                             icon=shiny::icon("bomb")),
                                         rhandsontable::rHandsontableOutput("genTabProcCtrl"),
                                         width=NULL)
    genTab<-shinydashboard::tabItem(tabName = "gen",
                                    shiny::h5("Prepare for prescreening."),
                                    shiny::fluidRow(shiny::column(genBoxProcessed,
                                                                  width=4)),
                                    shiny::fluidRow(shiny::column(genBoxParam1,width=4)),
                                    shiny::fluidRow(shiny::column(genBoxParam2,width=4)))

    ## ***** Prescreening *****

    ## Prescreening elements
    preshead <- shinydashboard::dashboardHeader(title = "Prescreening")
    
    presCompInfo <- shiny::fluidRow(shinydashboard::box(title = "MS Prescreening",
                                                        width = 7,
                                                        height = "80px",
                                                        background = "blue",
                                                        ""),
                                    shinydashboard::box(title = "Compound ID N°",
                                                        width = 5,
                                                        height = "80px",
                                                        background = "olive",
                                                        shiny::textOutput("compoundID")))

    presPlotBox <- shinydashboard::box(title = "Plot",
                                       width = 7,color = "olive",
                                       solidHeader = FALSE,
                                       collapsible = TRUE,
Todor Kondic's avatar
Todor Kondic committed
                                       shiny::plotOutput("chromGram",
                                                         width = "100%",
                                                         height = "750px",
                                                         click = NULL,
                                                         dblclick = NULL,
                                                         hover = NULL,
                                                         hoverDelay = NULL,
                                                         hoverDelayType = NULL,
                                                         brush = NULL,
                                                         clickId = NULL,
                                                         hoverId = NULL),
                                       shiny::textInput("plotname",
                                                        "Insert plot name: (e.g. plotname_%i.pdf)",
                                                        value="plotCpdID_%i.pdf"),
                                       shiny::actionButton("saveplot",
                                                           "Save",
                                                           icon = shiny::icon("save")),
                                       shiny::actionButton("saveallplots",
                                                           "Save All Plots",
                                                           icon = shiny::icon("save")))

    presCompSelBox <- shinydashboard::box(title = "Compounds",
                                          width=5,
                                          solidHeader = FALSE,
                                          color = "olive",
                                          collapsible = TRUE,
                                          "",
                                          shiny::br(),
                                          shiny::selectInput("presSelSet",
                                                             "Set",
                                                             choices="",
                                                             multiple=F),
                                          shiny::actionButton("presPrev",
                                                              "Previous compound.",
                                                              icon = shiny::icon("backward")),
                                          shiny::actionButton("presNext",
                                                              "Next compound.",
                                                              icon = shiny::icon("forward")),
                                          shiny::selectInput("presSelCmpd",
                                                             "Compound",
                                                             choices="",
                                                             multiple=F))

    nvPanel<-shiny::uiOutput("nvPanel")
    presQABox <- shinydashboard::box(title = "Prescreening analysis",
                                     width = 5,
                                     solidHeader = FALSE,
                                     collapsible = TRUE,
                                     nvPanel,
                                     shiny::actionButton("submitQA",
                                                         "Submit",
                                                         icon = shiny::icon("save")),
                                     shiny::textInput("fn_ftable",
                                                      "File table Name",
                                                      value=FN_FTAB),
                                     shiny::actionButton("savefiletable",
                                                         "Save File Table",
                                                         icon = shiny::icon("save")))
    presPlotParBox <- shinydashboard::box(title = "Plot Parameters",
                                          width=7,
                                          solidHeader = FALSE,
                                          collapsible = TRUE,
                                          "",
                                          shiny::br(),
                                          shiny::numericInput("min_val",
                                                              "Minimum x Axis Value",
                                                              DEFAULT_RT_RANGE[[1]]),
                                          shiny::numericInput("max_val",
                                                              "Maximum x Axis Value",
                                                              DEFAULT_RT_RANGE[[2]]),
                                          shiny::radioButtons("yaxis",
                                                              "Parameters for y Axis",
                                                              c(linear = "linear",
                                                                log = "log")),
                                          shiny::numericInput("nice",
                                                              "Nice",
                                                              DEFAULT_RT_RANGE[[1]]),
                                          shiny::numericInput("steps",
                                                              "Steps",
                                                              DEFAULT_RT_RANGE[[2]]))
    presPlotWidget <- shiny::fluidRow(presPlotBox,
                                      presCompSelBox,
                                      presQABox,
                                      presPlotParBox)
    
    presTab <- shinydashboard::tabItem(tabName = "prescreen",
                                       shiny::h2("Prescreening"),
                                       presCompInfo,
                                       presPlotWidget)




    ## ***** Top-level Elements *****
    
    headerText <- "Shinyscreen"

    

    confSideItem <- shinydashboard::menuItem(text="Config",
                                             tabName="config",
                                             icon=shiny::icon("user-cog"))
    compListSideItem <- shinydashboard::menuItem(text="Compound list",
                                                 icon=shiny::icon("table"))
    setIdSideItem <- shinydashboard::menuItem(text="Compound sets",
                                                 tabName="setId",
                                              icon=shiny::icon("table"))

    genSideItem <- shinydashboard::menuItem(text="Generate prescreen data",
                                                 tabName="gen",
                                                 icon=shiny::icon("cogs"))
    presSideItem <- shinydashboard::menuItem(text="Prescreening",
                                             tabName="prescreen",
                                             icon=shiny::icon("chart-bar"))
    
    header <- shinydashboard::dashboardHeader(title=headerText)
    
    sidebar <- shinydashboard::dashboardSidebar(shinydashboard::sidebarMenu(confSideItem,
                                                                            compListSideItem,
                                                                            setIdSideItem,
                                                                            genSideItem,
                                                                            presSideItem))
    body <- shinydashboard::dashboardBody(shinydashboard::tabItems(confTab,
                                                                   cmpListTab,
                                                                   setIdTab,
                                                                   genTab,
    
    shinydashboard::dashboardPage(header,
                                  sidebar,
                                  body)}

##' @export
shinyScreenApp <- function(projDir=getwd()) {
    message("projDir=",projDir)
    modeLvl<- c("pH","pNa","pM",
                "mH","mFA")
Todor Kondic's avatar
Todor Kondic committed
    volumes <- shinyFiles::getVolumes()
    mk_mzMLtab<-function() {
        modeLvl<- c("pH","pNa","pM",
                "mH","mFA")
        res<-data.frame(Files=character(),
                        mode=factor(levels=modeLvl),
                        set=factor(),
                        tag=factor(),
    mk_cmpList<-function() {
        data.frame(ID=integer(),
                   Name=character(),
                   SMILES=character(),
                   RT=double(),
                   CAS=character(),
                   stringsAsFactors = F)
    }

    mk_setId<-function() {
        data.frame(ID=integer(),
                   set=character())}
    
Todor Kondic's avatar
Todor Kondic committed
    extd_mzMLtab<-function(ft,fn) {
        modeLvl<- c("select","pH","pNa","pM",
                    "mH","mFA")
Todor Kondic's avatar
Todor Kondic committed

        lSet<-levels(ft$set)
        lTag<-levels(ft$tag)
        newRow<-data.frame(Files=fn,
                           mode=factor(modeLvl[[1]],levels=modeLvl),
Todor Kondic's avatar
Todor Kondic committed
                           set=if (length(lSet) > 0) factor(lSet[[1]],levels=lSet) else factor("unspecified"),
                           tag=if (length(lTag) > 0) factor(lTag[[1]],levels=lTag) else factor("unspecified"),
                           stringsAsFactors = F)

        levels(newRow$mode)<-modeLvl
        res<-rbind(ft,newRow,
                   stringsAsFactors = F,
                   make.row.names = F)
        levels(res$mode)<-modeLvl
Todor Kondic's avatar
Todor Kondic committed

    readSetId<-function(fn) {
        read.csv(file=fn,
                 header=T,
                 stringsAsFactors = T,
                 comment.char = '',
                 na.strings = c("","NA"))
    }

    getMz<-function(ids,cmpdL) {
        mz<-sapply(ids,function(i) {mzs<-cmpdL$mz[cmpdL$ID==i]
            if (length(mzs)>1) mzs[[1]] else mzs
        })
        names(mz)<-NULL
        mz
    }
    mkCmpdDrop<-function(ids,cmpdL) {
        cmpdL$ID<-as.numeric(cmpdL$ID)
        mz<-getMz(ids,cmpdL)
        entries<-base::Map(function(i,m) paste(i,'; ','mz: ',m,sep=''),ids,mz)
        entries
    }
Todor Kondic's avatar
Todor Kondic committed

    queryFileTable <- function(df,set,id) {
        df[(df$ID %in% id) & (df$set %in% set),]
    }


    updateFileTable <- function(df,set,id,linput) {
Todor Kondic's avatar
Todor Kondic committed
        for (tag in names(linput)) {
            entries <- names(linput[[tag]])
            cond <- (df$ID %in% id) & (df$tag == tag) & (df$set %in% set)

Todor Kondic's avatar
Todor Kondic committed
            df[cond,entries] <- linput[[tag]]
        }
        df
    }
    
    getCheckboxValues <- function(tag,input,rv) {
        chkbox <- input[[rv$spectProps[[tag]]]]
Todor Kondic's avatar
Todor Kondic committed
        q <- sapply(QANAMES,function (qn) if (qn %in% chkbox) T else F)
        names(q) <- QANAMES
        q
    }
    server <- function(input,output,session) {

        ## ***** reactive values *****
Todor Kondic's avatar
Todor Kondic committed
        rvConf <- shiny::reactiveValues(mzMLtab=mk_mzMLtab(),
                                        tags=list(),
                                        sets=list(),
Todor Kondic's avatar
Todor Kondic committed
                                        spectProps=list(),
Todor Kondic's avatar
Todor Kondic committed
                                        QANAMES=QANAMES,
Todor Kondic's avatar
Todor Kondic committed
                                        MODEMAP=MODEMAP,
                                        impCmpListFn="",
Todor Kondic's avatar
Todor Kondic committed
                                        impSetIdFn="",
Todor Kondic's avatar
Todor Kondic committed
                                        impGenRMBFn="",
Todor Kondic's avatar
Todor Kondic committed
                                        fnFTBase="",
                                        mode=modeLvl,
                                        projDir=projDir,
                                        currSet=NA,
                                        currIDSel=1,
Todor Kondic's avatar
Todor Kondic committed
                                        currIDSet=list(),
Todor Kondic's avatar
Todor Kondic committed
                                        currID=NA,
                                        fnFT=NULL,
                                        fTab=NULL)

        rvCmpList<- shiny::reactiveValues(df=mk_cmpList())
        rvSetId<- shiny::reactiveValues(df=mk_setId())
Todor Kondic's avatar
Todor Kondic committed
        rvPres<-shiny::reactiveValues(cex=CEX,
                                      rt_digits=RT_DIGITS,
                                      m_digits=M_DIGITS,
                                      pal=PAL,
                                      plot_id=NULL)

        ## ***** shinyFiles observers *****
        wdroot<-c(wd=projDir)
        shinyFiles::shinyFileChoose(input, 'impCmpListB',roots=volumes)
        shinyFiles::shinyFileChoose(input, 'impSetIdB',roots=volumes)
Todor Kondic's avatar
Todor Kondic committed
        shinyFiles::shinyFileChoose(input, 'impGenRMBB',roots=volumes)
        
        shinyFiles::shinyFileSave(input, 'saveConfB',roots=wdroot)
        shinyFiles::shinyFileChoose(input, 'restoreConfB',roots=wdroot)
        shinyFiles::shinyFileChoose(input, 'mzMLB',roots=volumes)

        ## ***** reactive function definitions *****
        
        getTags<-shiny::reactive({
            if (length(input$tagsInp)>0 && !is.na(input$tagsInp)) unlist(strsplit(input$tagsInp, ",")) else list()
        })

        getSets<-shiny::reactive({
            levels(rvSetId$df$set)
        getCmpdL<-shiny::reactive({
            rvCmpList$df #rhandsontable::hot_to_r(input$cmpListCtrl)
        })

        getSetId<-shiny::reactive({
            rvSetId$df #rhandsontable::hot_to_r(input$setIdTabCtrl)
        update_tags_mzMLtab<-shiny::reactive({
            tags<-getTags()
            tagCol<-rvConf$mzMLtab$tag
            if (length(levels(tagCol))==0) rvConf$mzMLtab$tag<-factor(tagCol)
            rvConf$mzMLtab$tag<-factor(tagCol,levels=tags)
        update_sets_mzMLtab<-shiny::reactive({
            sets<-getSets()
            setCol<-rvConf$mzMLtab$set
            if (length(levels(setCol))==0) rvConf$mzMLtab$set<-factor(setCol)
            rvConf$mzMLtab$set<-factor(setCol,levels=sets)
        saveConf<-reactive({
            fn<-shinyFiles::parseSavePath(root=c(wd=rvConf$projDir),input$saveConfB)[["datapath"]]
            if ((! is.na(fn)) && length(fn)>0) {
                message("Saving config to",fn)
                sav<-list()
                sav<-list(rvConf=list(),
                          input=list())
                shiny::isolate(for (nm in names(rvConf)) {
                                   sav$rvConf[[nm]]<-rvConf[[nm]]
                               })
                sav$input$tagsInp<-input$tagsInp
                sav$input$setsInp<-input$setsInp
                sav$input$impCmpListInp<-input$impCmpListInp
                sav$input$impSetIdInp<-input$impSetIdInp
                sav$input$impGenRMBInp<-input$impGenRMBInp
                saveRDS(object=sav,file=fn)
                }
        })

        restoreConf<-reactive({
            input$restoreConfB
            fnobj<-shinyFiles::parseFilePaths(root=c(wd=rvConf$projDir),input$restoreConfB)
            fn<-fnobj[["datapath"]]
Todor Kondic's avatar
Todor Kondic committed
            if (length(fn)>0 && !is.na(fn) && nchar(fn)>0) {
                message("Restoring config from",fn)
Todor Kondic's avatar
Todor Kondic committed
                sav<-readRDS(fn)
                ## shiny::isolate({
                ##     sav<-readRDS(fn)
                ##     for (nm in names(sav$rvConf)) {
                ##         rvConf[[nm]]<-sav$rvConf[[nm]]
                ##     }
Todor Kondic's avatar
Todor Kondic committed
                shiny::updateTextInput(session=session,
                                       inputId="impGenRMBInp",
                                       value=sav$input$impGenRMBInp)
                shiny::updateTextInput(session=session,
                                       inputId="impCmpListInp",
                                       value=sav$input$impCmpListInp)
                shiny::updateTextInput(session=session,
                                       inputId="impSetIdInp",
                                       value=sav$input$impSetIdInp)
                shiny::updateTextInput(session=session,
                                       inputId="tagsInp",
                                       value=sav$input$tagsInp)
                shiny::updateTextInput(session=session,
                                       inputId="confFileTabBase",
                                       value=sav$input$confFileTabBase)
                shiny::updateTextInput(session=session,
                                       inputId="confFileTabProcInp",
                                       value=sav$input$confFileTabProcInp)
                shiny::updateTextInput(session=session,
                                       inputId="confResFileTab",
                                       value=sav$input$confResFileTab)

                rvConf$mzMLtab<-sav$rvConf$mzMLtab
Todor Kondic's avatar
Todor Kondic committed
                rvConf$fnFTBase<-sav$rvConf$fnFTBase
Todor Kondic's avatar
Todor Kondic committed
                rvConf$fnFT<-sav$rvConf$fnFT
        getCmpListdf<-shiny::reactive({rvCmpList$df})

Todor Kondic's avatar
Todor Kondic committed
        ## ***** Observe Event *****

        shiny::observeEvent(input$restoreConfB,{
            message("Restore event observed.")
            restoreConf()
Todor Kondic's avatar
Todor Kondic committed
            message("Restore event finished.")
        })

        shiny::observeEvent(input$saveConfB,{
            saveConf()
        })


Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$impSetIdB,{
            fnobj<-shinyFiles::parseFilePaths(roots=volumes,input$impSetIdB)
            fn<-fnobj[["datapath"]]
            if (length(fn)>0 && !is.na(fn)) {
                shiny::updateTextInput(session=session,
                                       inputId="impSetIdInp",
                                       value=fn)
            }})
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$impCmpListB,{
            fnobj<-shinyFiles::parseFilePaths(roots=volumes,input$impCmpListB)
            fn<-fnobj[["datapath"]]
            if (length(fn)>0 && !is.na(fn)) {
                shiny::updateTextInput(session=session,
                                       inputId="impCmpListInp",
                                       value=fn)
            }})
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$impGenRMBB,{
            fnobj<-shinyFiles::parseFilePaths(roots=volumes,input$impGenRMBB)
            fn<-fnobj[["datapath"]]
            if (length(fn)>0 && !is.na(fn)) {
                shiny::updateTextInput(session=session,
                                       inputId="impGenRMBInp",
                                       value=fn)
            }})
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$impCmpListInp,
        {
            impCmpFn<-input$impCmpListInp
            if (length(impCmpFn)>0 && !is.na(impCmpFn) && nchar(impCmpFn)>0) {
                rvConf$impCmpListFn<-impCmpFn
                message("rvConf$impCmpListFn is changed to:",impCmpFn)
                
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(rvConf$impCmpListFn,
        {
            fn<-rvConf$impCmpListFn
            if (file.exists(fn)) {
                message("Importing compound list from:",fn)
                df<-importCmpList(fn)
                rvCmpList$df<-df
                message("Done importing compound list from: ",fn)
Todor Kondic's avatar
Todor Kondic committed
        
        shiny::observeEvent(input$impSetIdInp,
        {
            fn<-input$impSetIdInp
            if (length(fn)>0 && !is.na(fn) && nchar(fn)>0) {
                rvConf$impSetIdFn<-fn
                message("rvConf$impSetIdFn is changed to:",fn)
            }
Todor Kondic's avatar
Todor Kondic committed
        
        shiny::observeEvent(rvConf$impSetIdFn,
        {
            fn<-rvConf$impSetIdFn
            if (file.exists(fn)) {
                message("Importing compound sets from:",fn)
                rvSetId$df<-readSetId(fn)
                message("Done importing compound sets from: ",fn)
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(rvConf$mzMLtab,{
            update_sets_mzMLtab()
            update_tags_mzMLtab()
        })

        shiny::observeEvent(input$mzMLtabCtrl,{
            ## input$cmpListCtrl
            ## input$setIdTabCtrl
Todor Kondic's avatar
Todor Kondic committed
            rvConf$mzMLtab<-rhandsontable::hot_to_r(input$mzMLtabCtrl)
            ## shiny::isolate({rvConf$mzMLtab<-rhandsontable::hot_to_r(input$mzMLtabCtrl)})
        shiny::observeEvent(input$cmpListCtrl,{
            df<-rhandsontable::hot_to_r(input$cmpListCtrl)
        })

        shiny::observeEvent(input$setIdTabCtrl,{
            df<-rhandsontable::hot_to_r(input$setIdTabCtrl)
            rvSetId$df<-df
            shiny::updateSelectInput(session=session,
                                     inputId="genSetSelInp",
                                     choices=levels(df$set))})



        ## shiny::observeEvent(input$confFileTabBase, {
        ##     fn<-input$confFileTabBase
        ##     if (length(fn)>0 && !is.na(fn)) {
Todor Kondic's avatar
Todor Kondic committed
        ##         rvConf$fnFTBase<-fn
        ##     }
        ## })

        shiny::observeEvent(input$genFileTabB,{
            fn<-input$confFileTabBase
Todor Kondic's avatar
Todor Kondic committed
            if (length(fn)>0 && !is.na(fn) && nchar(fn)>0) {
Todor Kondic's avatar
Todor Kondic committed
                message("Generating basic file table in file ",fn)
                ## files<-adornmzMLTab(rhandsontable::hot_to_r(input$mzMLtabCtrl),projDir=rvConf$projDir)
                files<-adornmzMLTab(rvConf$mzMLtab,projDir=rvConf$projDir)
Todor Kondic's avatar
Todor Kondic committed
                setId<-rvSetId$df
                cmpL<-rvCmpList$df
                df<-genSuprFileTbl(files,setId,destFn=fn)
                df<-addCmpLColsToFileTbl(df,cmpL)
                write.csv(x=df,file=fn,row.names=F)
                rvConf$fnFTBase<-fn
Todor Kondic's avatar
Todor Kondic committed
                message("Done generating basic file table in file ",fn)
            }

        })

        shiny::observeEvent(input$genRunB,{
            FnRMB<-input$impGenRMBInp
            nProc<-as.integer(input$genNoProc)
Todor Kondic's avatar
Todor Kondic committed
            fnTab<-rvConf$fnFTBase
            sets<-input$genSetSelInp
            message("Selected sets:")
            message(str(sets))
            message("Number of processes:",nProc)
            message("RMassBank settings file:",FnRMB)
            message("File table:",fnTab)
            if (length(fnTab)>0) {
                ## fTab<-read.csv(file=fnTab,
                ##                row.names=F,
                ##                heander=T,
                ##                comment.char='',
                ##                na.strings=c("","NA"),
                ##                stringsAsFactors=F)
                ## mask<-fTab$set %in% sets
                ## fTab<-fTab[mask,]

                for (s in sets) {
                    message("***** BEGIN set ",s, " *****")
                    fnCmpdList<-input$impCmpListInp
                    fnStgs<-input$impGenRMBInp
                    intTresh<-as.numeric(input$intTresh)
                    noiseFac<-as.numeric(input$noiseFac)
                    rtDelta<-as.numeric(input$rtDelta)
                    ppmLimFine<-as.numeric(input$ppmLimFine)
                    dest<-rvConf$projDir
                    eicLim<-as.numeric(input$eicLim)
                    gc()
                    gen(fnFileTab=fnTab,
                        fnCmpdList=fnCmpdList,
                        fnStgs=fnStgs,
                        dest=dest,
                        proc=nProc,
                        intTresh=intTresh,
                        noiseFac=noiseFac,
                        rtDelta=rtDelta,
                        ppmLimFine=ppmLimFine,
                        eicLim=eicLim)
                    message("***** END set ",s, " *****")
Todor Kondic's avatar
Todor Kondic committed
                }
                gc()
            }})
        


        shiny::observeEvent(input$genRunPPB,{
            message("Starting preprocessing.")
Todor Kondic's avatar
Todor Kondic committed
            
            shiny::isolate({
                sets<-getSets()
                cdf<-if (!is.null(rvCmpList$df)) rvCmpList$df else NULL})
Todor Kondic's avatar
Todor Kondic committed
            
            nr<-nrow(cdf)
            if (!is.null(nr)) {
                if (is.na(nr)) nr<-0
            } else nr<-0
            if (length(sets)>0 && nr>0) {
                doneSets<-sets[sapply(sets,isGenDone)]
                if (length(doneSets)>0) {
                    fnFullTab<-input$confFileTabProcInp
                    fnBaseTab<-input$confFileTabBase
                    fnOpen<-""
                    fnOpen<-if (file.exists(fnFullTab)) fnFullTab else fnBaseTab
                    fullFTab<-read.csv(file=fnOpen,
                                       comment.char = '',
                                       stringsAsFactors = F)

                                    doneFTab<-fullFTab[fullFTab$set %in% doneSets,]
Todor Kondic's avatar
Todor Kondic committed
                    if (nrow(doneFTab)>0) {
                        fnTmp<-ppInpFt()
                        write.csv(file=fnTmp,
                                  x=doneFTab,
                                  row.names=F)
                        message("fnTmp: ",fnTmp)
                        cmpdL<-rvCmpList$df
                        maxId<-do.call(max,as.list(sapply(doneSets,idsFromFiles)))
Todor Kondic's avatar
Todor Kondic committed
                        intTresh<-as.numeric(input$intTresh)
                        noiseFac<-as.numeric(input$noiseFac)
                        rtDelta<-as.numeric(input$rtDelta)
                        ## dr<-file.path(dirname(fnCand),sets)
                        preProc(fnFileTab=fnTmp,
                                lCmpdList=maxId,
                                fnDest=fnTmp,
                                intTresh=intTresh,
                                noiseFac=noiseFac,
                                rtDelta=rtDelta)
                        ppFnTab<-read.csv(file=fnTmp,
                                          comment.char = '',
                                          stringsAsFactors = F)
                        extNms<-names(ppFnTab)
                        basNms<-names(fullFTab)
                        diffNms<-setdiff(extNms,basNms)
                        nrf<-nrow(fullFTab)
                        for (nm in diffNms) {
                            z<-logical(length=nrf)
                            z<-T
                            fullFTab[[nm]]<-z
                        }
                        fullFTab[fullFTab$set %in% doneSets,]<-ppFnTab
                        write.csv(file=fnFullTab,
                                  x=fullFTab,
                                  row.names=F)
                        file.copy(fnFullTab,input$confResFileTab,overwrite=T)
                        message("Finished preprocessing.")
        })

        shiny::observeEvent(input$presSelSet,{
            sets<-getSets()
            if (length(sets)>0 && !is.na(sets)) {
                cmpdL<-getCmpdL()
                setID<-getSetId()
                if (nrow(cmpdL)>0 && nrow(setID)>0) {
Todor Kondic's avatar
Todor Kondic committed
                    set<-input$presSelSet
                    rvConf$currSet<-set
                    ids<-setID$ID[setID$set %in% set]
                    entries<-mkCmpdDrop(ids,cmpdL)
                    ch<-as.list(1:length(ids))
                    names(ch)<-entries
                    shiny::updateSelectInput(session=session,
                                             "presSelCmpd",
                                             choices=ch,
                                             selected = 1)
                    rvConf$currIDSet<-ids
                }
            }
        })

Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(rvConf$currSet,{
            message("In curr set")
Todor Kondic's avatar
Todor Kondic committed
            set<-rvConf$currSet
            fTab<-rvConf$fTab
Todor Kondic's avatar
Todor Kondic committed
            ids<-rvConf$currIDSet
Todor Kondic's avatar
Todor Kondic committed
            message("set:",set)
            message("n ftab:",nrow(fTab))
            if (!is.na(set) && length(fTab)>0) {
                tags<-rvConf$tags
                iSet<-which(set==fTab$set)
Todor Kondic's avatar
Todor Kondic committed
                sfTab<-fTab[iSet,]
Todor Kondic's avatar
Todor Kondic committed
                tags<-levels(factor(sfTab$tag))
                iTag<- match(tags,sfTab$tag)
                wd<-sfTab$wd[iTag]
Todor Kondic's avatar
Todor Kondic committed
                message("wd<")
                message(str(wd))
                message("wd>")
                preID<-ids #sfTab$ID[iTag]
Todor Kondic's avatar
Todor Kondic committed
                cmpL<-rvCmpList$df
                iCmpL<-match(preID,cmpL$ID)
                smiles<-cmpL$SMILES[iCmpL]
Todor Kondic's avatar
Todor Kondic committed
                mz<-cmpL$mz[iCmpL]
Todor Kondic's avatar
Todor Kondic committed
                names(smiles)<-as.character(preID)
                names(mz)<-as.character(preID)
                ## Get the basenames of eic files.
                eics <- list.files(path=wd[[1]],patt=".*eic.csv")
                eicsPref <- sapply(strsplit(eics,split="\\."),function(x) x[[1]])
                eicsID <- as.integer(eicsPref)
                maybekids <- sapply(eicsPref,function(x) {paste(x,'.kids.csv',sep='')})
                names(eics) <- eicsID
                names(maybekids) <- eicsID
Todor Kondic's avatar
Todor Kondic committed
                plot_id <- function (i,rtrange=NULL,log=input$yaxis) {
                    i=as.character(i)
                    mz=mz[[i]]
                    smile<-smiles[[i]]

                    plot_id_aux(i,wd=wd,eics=eics,maybekids=maybekids,mass=mz,smile=smile,tags=tags,log=log,rtrange=rtrange,cex=rvPres$cex,pal=rvPres$pal,rt_digits=rvPres$rt_digits,m_digits=rvPres$m_digits,fTab=sfTab)
                    }
Todor Kondic's avatar
Todor Kondic committed
                rvPres$plot_id<-plot_id
            }
        })

Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$presSelCmpd,{
            pos<-input$presSelCmpd
            rvConf$currIDSel<-as.numeric(pos)
        })
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$presPrev,{
            len<-length(rvConf$currIDSet)
            x<-rvConf$currIDSel-1
            if (x>0) rvConf$currIDSel<-x
        })
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$presNext,{
            len<-length(rvConf$currIDSet)
            x<-rvConf$currIDSel+1
            if (x<=len) rvConf$currIDSel<-x
        })
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(rvConf$fnFT,{
            fn<-rvConf$fnFT
Todor Kondic's avatar
Todor Kondic committed
            if (!is.null(fn) && file.exists(fn)) {
Todor Kondic's avatar
Todor Kondic committed
                rvConf$fTab<-read.csv(file=fn,
                                      comment.char = '',
                                      stringsAsFactors = F)
            }
        })
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(rvConf$currIDSel,{
            ids<-rvConf$currIDSet
            if (length(ids)>0) rvConf$currID<-ids[[rvConf$currIDSel]]
        })
Todor Kondic's avatar
Todor Kondic committed
        shiny::observeEvent(input$submitQA,{
            res <- lapply(rvConf$tags,getCheckboxValues,input,rvConf)
            names(res) <- rvConf$tags
            rvConf$fTab <- updateFileTable(df=rvConf$fTab,
                                           set=input$presSelSet,
                                           id=rvConf$currID,
                                           linput=res)
        })
        
        shiny::observeEvent(input$savefiletable,
        {
            fn<-input$fn_ftable
            message("Writing current file table to ",fn)
            write.csv(file=fn,x=rvConf$fTab,row.names = F)
        })


        shiny::observeEvent(input$saveplot,
        {
            i=rvConf$currID
            pfn <-input$plotname
            if (is.na(pfn)) pfn <- "plotCpdID_%i.pdf"
            fn <- sprintf(pfn,i)
            rtrange <- c(input$min_val,input$max_val)
            pdf(file=fn, width=12, height=8)
            rvPres$plot_id(i,rtrange=rtrange, log=input$yaxis)
            dev.off()
            message("Plotting compound ", i," to ",fn," done.")
        })
        shiny::observeEvent(input$saveallplots,
        {
            i=rvConf$currID
            pfn <-input$plotname
            if (is.na(pfn)) pfn <- "plotall.pdf"
            fn <- sprintf(pfn,i)
            pdf(file=fn, width=12, height=8)
            for (i in rvConf$currIDSet) {
                rvPres$plot_id(i,log=input$yaxis)
                message("Plotting compound ", i," done.")
            }
            dev.off()
        })
Todor Kondic's avatar
Todor Kondic committed
        ## ***** Observe *****
        shiny::observe({
            fchoice<-shinyFiles::parseFilePaths(root=volumes,input$mzMLB)
            paths<-fchoice[["datapath"]]
            isolate({
                for (pt in paths) {
                    rvConf$mzMLtab<-extd_mzMLtab(rvConf$mzMLtab,pt)
                }
        
                })
        })

        shiny::observe({
            shiny::updateSelectInput(session=session,
                                     inputId="presSelCmpd",
                                     selected=rvConf$currIDSel)
        })


        shiny::observe({
            shiny::invalidateLater(100,
                                   session=session)
            output$genTabProcCtrl<-rhandsontable::renderRHandsontable({
                
                sets<-getSets()
                genState<-sapply(sets,isGenDone)
                df<-if (!is.null(sets)) {data.frame(set=sets,
                                                    generated=genState,
                                                    stringsAsFactors=F)
                    } else {data.frame(sets=character(),
                                       generated=logical(),