R/do.noise.reduce.R

Defines functions do.noise.reduce

Documented in do.noise.reduce

#' Reduce noise by change values below a specified threshold to that value, reducing the noise generated by 'negative' values
#'
#' @usage do.noise.reduce(dat, use.cols, cutoffs)
#'
#' @param dat NO DEFAULT. Data table of cells (rows) x features (columns)
#' @param use.cols NO DEFAULT. A vector of column names -- specifying which columns will have noise reduction applied to them
#' @param cutoffs NO DEFAULT. A vector of numeric cutoff values for each of the use.cols -- values below this threshold will be modified to the threshold value.
#'
#' @return Returns a data.table with the noise-reduced data added as new columns, including a 'noiseRed' suffix
#'
#' @author Thomas M Ashhurst, \email{thomas.ashhurst@@sydney.edu.au}
#'
#' @references \url{https://sydneycytometry.org.au/spectre}.
#'
#' @examples
#'
#' min(Spectre::demo.asinh$Ly6G_asinh) # Minimum value of CD11b_asinh should be -5.176318
#'
#' res <- Spectre::do.noise.reduce(dat = Spectre::demo.asinh,
#'                                 use.cols = names(Spectre::demo.asinh)[c(11:19)],
#'                                 cutoffs = rep(0, 9))
#'
# min(res$Ly6G_asinh_noiseRed) # Minimum of Ly6G_asinh_noiseRed should be 0

#' @import data.table
#'
#' @export

do.noise.reduce <- function(dat,
                            use.cols,
                            cutoffs){

  ### Checks
      message("Limit adjustment transformation is experimental - please use with caution.")
      if(length(use.cols) != length(cutoffs)){
        warning("The number of your columns to modify the number of cutoff values. Please check your input.")
      }

  ### Establish dataset
      value <- dat[,use.cols,with = FALSE]

  ### Changes values < cutoff to the cutoff values
      if(!is.null(cutoffs)){
        for(i in c(1:length(use.cols))){
          # i <- 1
          a <- use.cols[[i]]
          b <- cutoffs[[i]]
          temp <- value[,a,with = FALSE]
          temp[temp[[a]] < b,] <- b
          value[,a] <- temp
        }
      }

  ### Names
      names(value) <- paste0(names(value), "_noiseRed")
      dat <- cbind(dat, value)

  ###
      return(dat)
}
tomashhurst/Spectre documentation built on Dec. 23, 2021, 11:55 a.m.