R/baselineCorr.R

Defines functions baselineCorr

Documented in baselineCorr

##' Baseline Correction
##' 
##' Offers three different methods for baseline correction of raw spectral
##' data. Methods include monotone minimum, linear interpolation, and LOESS
##' curve fitting.
##' 
##' 
##' @param dat The name of the spectral data frame, containing \code{m/z} data
##' in the first column and spectral intensity data in subsequent columns.
##' @param mass_dat Character string. The name of the column in \code{dat}
##' containing the \emph{m/z} data for the spectrum that will be corrected.
##' @param intensity_dat Character string. The name of the column in \code{dat}
##' containing the intensity data for the spectrum to be baseline corrected.
##' @param method Character string. The method that is to be used to perform
##' the baseline correction. Either \code{"monotone"} for monotone minimum
##' correction, \code{"linear"} for linear interpolation, or \code{"loess"} for
##' LOESS curve fitting.
##' @param n Single odd numeric value. If \code{method = "linear"}, the size of
##' the window that should be used for linear interpolation of the spectral
##' baseline.
##' @return Returns a new data frame containing the baseline corrected spectral
##' data.
##' @section Methods: \describe{ \item{monotone_min}{ Identifies valleys in the
##' spectrum by evaluating the slopes between adjacent intensity values. Moving
##' from left to right, if slope of point A is greater than 0, the nearest
##' point B with slope of less than 0 is located. Points in between A and B are
##' recorded as potential peaks. If slope of A < 0, nearest point B with slope
##' > 0 is located. Points between A and B are recorded as valleys. Each
##' intensity value in the spectrum is then subtracted by the intensity of the
##' nearest valley, correcting any irregularities in the baseline of the data.
##' } \item{linear}{ Divides a spectrum into small segments and evaluates the
##' mean of points in each segment as a valley (1). A baseline is then
##' generated by linearly interpolating valleys across all small segments. In
##' each segment, the intensity of each peak is subtracted by the intensity of
##' the valley in that segment. } \item{loess}{ Divides a spectrum into small
##' segments and evaluates the quantile in each segment. The baseline is
##' estimated as follows. If the intensity of point A in a segment is less than
##' the quantile, the intensity of point A is recorded as a baseline predictor
##' for that point. If the intensity of point A is greater than or equal to the
##' quantile in the segment, the quantile for the segment is recorded the
##' baseline predictor for that point. Local polynomial regression fitting is
##' applied to the predictor points. The baseline is then subtracted from the
##' raw spectral signal.  } }
##' @author Kristen Yeh <kristenyeh@@trentu.ca> Wesley Burr <wesleyburr@@trentu.ca>
##' @seealso \code{\link{smoothSpectrum}}, \code{\link{peakDet}}
##' @references https://github.com/wesleyburr/subMaldi (1) Yang, C., He, Z. &
##' Yu, W. Comparison of public peak detection algorithms for MALDI mass
##' spectrometry data analysis. BMC Bioinformatics 10, 4 (2009).
##' https://doi.org/10.1186/1471-2105-10-4
##' @keywords methods manip
##' @examples
##' 
##' ## Load sample dataset "bsline"
##' data("bsline")
##' 
##' ## Correct the baseline using the monotone minimum method
##' bsline_mono <- baselineCorr(bsline, "mass", "raw", 
##'                       method = "monotone_min")
##' 
##' ## Correct the baseline using the linear interpolation method
##' ## Window size of 7
##' bsline_linear <- baselineCorr(bsline, "mass", "raw", 
##'                       method = "linear", n = 7)
##' 


# ----------------------------------------------------------------------------
# Last Updated: February 3, 2021
# Author: Kristen Yeh, Sophie Castel
# Title: subMALDI - Baseline Correction Parent Function
# ----------------------------------------------------------------------------



baselineCorr <- function(dat, mass_dat, intensity_dat, method = NULL, n = NULL){
  
  if( is.null(method) | !(method %in% c("monotone_min", "linear", "loess")) ){
    stop('Please select a valid baseline correction method. See ?baselineCorr for list of methods.')
  }
  
  if(method == "monotone_min"){
    r <- .base_mono(dat = dat, mass_dat = mass_dat, intensity_dat = intensity_dat) 
  } else if(method == "linear"){
    r <- .base_linear(dat = dat, mass_dat = mass_dat, intensity_dat = intensity_dat, n = n) 
  } else if(method == "loess"){ 
    r <- .base_loess(dat = dat, mass_dat = mass_dat, intensity_dat = intensity_dat) 
  }
  
  return(r)
}


# ----------------------------------------------------------------------------
wesleyburr/subMaldi documentation built on Oct. 1, 2021, 7:07 a.m.