R/Munge_Data-Modules.R

Defines functions CombineFeatures ParseCAMERA

Documented in CombineFeatures ParseCAMERA

#' @title Parses CAMERA annotations
#'
#' @export
#' @description Parses the three annotation columns generated by CAMERA to
#'   remove redundant and duplicative ion adduct annotations. See
#'   \code{parse_pos_results(), parse_neg_results(), plot_metgroup()} for more
#'   details. For examples, see \code{InitWorkflow()} and vignettes.
#' @param from.table from which table should LUMA pull the Peaklist.
#' @param to.table to which should LUMA save the modified Peaklist.
#' @param CAMERA.obj \code{xsAnnotate} object used to plot metabolite groups.
#' @return NULL
ParseCAMERA <- function(from.table,to.table,CAMERA.obj) {

  #Set default values
  if(missing(CAMERA.obj))
    CAMERA.obj <- NULL

  ##Check for existing CAMERA objects. If not specified, check for saved CAMERA objects.
  ##If none exist, return error.
  if(is.null(CAMERA.obj)) {
    PreProcesslist <- .set_PreProcessFileNames(IonMode,BLANK)
    CAMERA.file <- PreProcesslist[[2]]

    #CAMERA sanity check
    if(file.exists(CAMERA.file)){
        CAMERA.obj <- .CAMERASanityCheck(CAMERA.obj,CAMERA.file)
      }
  } else {
    e <- new.env()
    e$CAMERA.obj <- CAMERA.obj
  if(!ls(e) %in% ls(envir = .GlobalEnv)) {
    stop("\n\nDid not find the specified CAMERA object in the global environment!\n\n\n")
  } else {
    PreProcesslist <- .set_PreProcessFileNames(IonMode,BLANK)
    CAMERA.file <- PreProcesslist[[2]]

    CAMERA.obj <- .CAMERASanityCheck(CAMERA.obj,CAMERA.file)
  }
}

  ## Section of code to parse the output from CAMERA and plot results ######
  cat("Validating CAMERA annotations.\n\n")

  Peak.list <- read_tbl(mytable = from.table,
                        peak.db = peak_db,
                        asdf = TRUE)

  ## Run the CAMERA Parser
  if(IonMode == "Positive"){
    new.Peak.list <- parse_pos_results(raw = Peak.list,
                                   rule = rules,
                                   IonMode = IonMode)
  } else {
    if(IonMode == "Negative"){
      new.Peak.list <- parse_neg_results(raw = Peak.list,
                                     rule = rules,
                                     IonMode = IonMode)
    }
  }

  write_tbl(mydf = new.Peak.list,
            peak.db = peak_db,
            myname = "input_parsed")

  ## Call to function which plots EICs, pspectra, dendrograms and correlation matrices for metabolite groups that contain more than one feature.
  ## Necessary to determine if adducts are real.
  #READ in the CAMERA Parsed peak list with Metabolite IDs
  Peak.list <- read_tbl(mytable = "input_parsed",
                        peak.db = peak_db,
                        asdf = TRUE)
  file.base <- gen_filebase(DataFiles,BLANK,ion.id,IonMode)

  myresults <- plot_metgroup(CAMERA.obj = CAMERA.obj,
                             Sample.df = data.frame(Sex = Sexes,
                                                    Class = Classes,
                                                    n = no.Samples,
                                                    Endogenous = Endogenous),
                             Peak.list = Peak.list,
                             center = XCMS.par$center,
                             BLANK = BLANK,
                             gen.plots = gen.plots,
                             IonMode = IonMode,
                             file.base = file.base)

  write_tbl(mydf = myresults[[1]],
            peak.db = peak_db,
            myname = to.table)

  write_xlsx(validate.sheets = myresults[[2]],
             file.base = file.base,
             myname = "Validate Metabolite Groups",
             mysheets = c("clear","muddy"))

  ## End parsing and plotting section ####

}

#' @title Combines Features into Metabolites
#'
#' @export
#' @description Combines all well-behaved features in metabolite groups into a
#'   single entry per metabolite. See \code{sum_features(), combine_phenodata()}
#'   for more details. For examples, see \code{InitWorkflow()} and vignettes.
#' @param from.table from which table should LUMA pull the Peaklist.
#' @param to.table to which should LUMA save the modified Peaklist.
#' @return NULL
CombineFeatures <- function(from.table,to.table) {
  ## Sums isotopic and adduct peaks and combined all feature data into a single metadata entry

  Peak.list <- read_tbl(mytable = from.table,
                        peak.db = peak_db,
                        asdf = TRUE)

  Summed.list <- sum_features(Sample.df = data.frame(Sex = Sexes,
                                                        Class = Classes,
                                                        n = no.Samples,
                                                        Endogenous = Endogenous),
                                 Peak.list = Peak.list,
                                 search.par = data.frame(ppm = ppm.cutoff,
                                                         rt = rt.cutoff,
                                                         Voidrt = Voidrt,
                                                         Corr.stat.pos = Corr.stat.pos,
                                                         Corr.stat.neg = Corr.stat.neg,
                                                         CV = cv.cutoff,
                                                         Minfrac = mf.cutoff,
                                                         Endogenous = Endogenous.thresh,
                                                         Solvent = Solvent.ratio,
                                                         gen.plots = gen.plots,
                                                         keep.singletons = keep.singletons),
                                 BLANK = BLANK,
                                 IonMode = IonMode,
                                 QC.id = QC.id)

  #Combine phenotype data for each metabolite group with summed intensity values
  new.Peak.list <- combine_phenodata(Sample.df = data.frame(Sex = Sexes,
                                                               Class = Classes,
                                                               n = no.Samples,
                                                               Endogenous = Endogenous),
                                        Peak.list = Peak.list,
                                        Summed.list = Summed.list,
                                        search.par = data.frame(ppm = ppm.cutoff,
                                                                rt = rt.cutoff,
                                                                Voidrt = Voidrt,
                                                                Corr.stat.pos = Corr.stat.pos,
                                                                Corr.stat.neg = Corr.stat.neg,
                                                                CV = cv.cutoff,
                                                                Minfrac = mf.cutoff,
                                                                Endogenous = Endogenous.thresh,
                                                                Solvent = Solvent.ratio,
                                                                gen.plots = gen.plots,
                                                                keep.singletons = keep.singletons),
                                        BLANK = BLANK,
                                        IonMode = IonMode,
                                        QC.id = QC.id)
  write_tbl(mydf = new.Peak.list,
            peak.db = peak_db,
            myname = to.table)

}
USEPA/LUMA documentation built on Aug. 29, 2020, 1:40 p.m.