diff --git a/R/api.R b/R/api.R index c7f4e39aae6221fc53875c86b3f83e5eada5bdee..9b54c1eec8fbcddab9d4f9ae2c071fa747b74bfd 100644 --- a/R/api.R +++ b/R/api.R @@ -38,17 +38,29 @@ run_in_dir <- function(m) { } + + ##' @export load_compound_input <- function(m) { - m$input$tab$known <- if (shiny::isTruthy(m$conf$compounds$known)) - file2tab(m$conf$compounds$known) else EMPTY_KNOWN - m$input$tab$unknown <- if (shiny::isTruthy(m$conf$compounds$unknown)) - file2tab(m$conf$compounds$unknown) else EMPTY_UNKNOWN - + coll <- list() + fields <- colnames(EMPTY_CMPD_LIST) + fns <- m$conf$compounds$lists + for (l in 1:length(fns)) { + fn <- fns[[l]] + fnfields <- colnames(fn) + dt <- file2tab(fn) + verify_cmpd_l(dt=dt,fn=fn) + nonexist <- setdiff(colnames,fields) + coll[[l]] <- dt[,(nonexist) := NULL] + coll[[l]]$ORIG <- fn + + } + cmpds <- if (length(fns)>0) rbindlist(l=coll,use.names = T, fill = T) else EMPTY_CMPD_LIST + cmpds[,("known"):=.(the_ifelse(!is.na(SMILES),"structure",the_ifelse(!is.na(Formula),"formula","mz")))] + m$input$tab$cmpds <- cmpds m$input$tab$setid <- read_setid(m$conf$compounds$sets, - m$input$tab$known, - m$input$tab$unknown) + m$input$tab$cmpds) m } diff --git a/R/mix.R b/R/mix.R index 3386aa2de15a56fb84855fe90a17f3ac05557f5a..29384e988554e19da43fb281690ff4a7cd3e7273 100644 --- a/R/mix.R +++ b/R/mix.R @@ -639,17 +639,17 @@ vald_comp_tab<-function(df,ndf,checkSMILES=F,checkMz=F,checkNames=F) { df } -read_setid <- function(fn,known,unk) { +read_setid <- function(fn,cmpds) { assert(file.exists(fn),msg=paste("Please provide valid compounds set table:", fn)) - assert(nrow(known)>0 || nrow(unk) > 0,msg="Please provide at least one compounds list.") + assert(nrow(cmpds) > 0,msg="Please provide at least one compounds list.") setid <- file2tab(fn) - id_k <- known$ID - id_u <- unk$ID - tmp <- setid[,.(ID,set,origin=the_ifelse(ID %in% id_k,"known",NA_character_))] - tmp <- tmp[,.(ID,set,origin=the_ifelse(is.na(origin) & ID %in% id_u,"unknown",origin))] - natmp <- tmp[is.na(origin),.(ID,set)] - assert(nrow(natmp)==0,msg=paste("The following IDs from set table have not been found in the compound table:","------",print_table(natmp),"------",sep = "\n")) - tmp + x<-cmpds[setid,on='ID'][,.SD,.SDcols=c(colnames(setid),'known')] + + sids <- unique(setid$ID) + cids <- unique(cmpds$ID) + diff <- setdiff(sids,cids) + assert(length(diff)==0,msg=paste("The following IDs from set table have not been found in the compound table:","------",print_table(dtable(diff)),"------",sep = "\n")) + x } @@ -675,3 +675,19 @@ new_state <- function(conf,GUI) { m$input$tab$unknown <- EMPTY_UNKNOWN m } + +verify_cmpd_l <- function(dt,fn) { + fields <- colnames(EMPTY_CMPD_LIST) + dtflds <- colnames(dt) + ess <- c('SMILES','Formula','mz') + pres <- ess %in% dtflds + assert(length(pres) > 0, + msg = paste('Compound list from ',fn, + 'does not contain any of "SMILES", "Formula", or "mz". \nThe compound list needs at least one of those to be valid.')) + exst <- ess[pres] + x <- lapply(exst,function (nm) all(is.na(dt[[nm]]))) + assert(!all(x), msg = paste('At least one of', paste(exst,collapse = T), + '\nmust contain some values in compound list from',fn)) + + invisible(T) +} diff --git a/R/resources.R b/R/resources.R index b078f932f34b17fc55999a7c10c8ff64744a472a..9bb4fc9e0f779ae2b5ae87f796bba227ec4f231b 100644 --- a/R/resources.R +++ b/R/resources.R @@ -120,6 +120,18 @@ MS1_SN_FAC <- 3.0 NUM_INP_WIDTH="15%" + + +## Possible compound list fields +EMPTY_CMPD_LIST <- dtable(ID=character(), + SMILES=character(), + Name=character(), + Formula=character(), + RT=numeric(), + mz=numeric(), + known=character(), + ORIG=character()) +COMP_LIST_COLS <- c("ID","Name","SMILES","Formula","RT","mz") ## Comprehensive table properties COMP_NAME_MAP <- list(RT="rt") ## COMP_NAMES <-c("ID","mz","rt","adduct","set","origin","Name","SMILES")