R/filter_features.R

Defines functions filter_features

Documented in filter_features

#' Filter a matrix with counts
#' @param method Method used for the filter: counts or cpm
#' @param min_value minimum value of counts or cpm
#' @param thres_samp minimum number of samples with min_value counts
#' @param treatment factor with treatment for each sample. If this parameter is passed,
#' the filter condition is verified for each treatment (and not overall)
#' @param at_least_one if at least one treatment meet the requirements, the feature is not removed
#' if treatment is passed. If FALSE, all the treatmens must meet this requirement
#'@export

filter_features <- function(x, method = c("counts", "cpm"),
                            min_value, thres_samp,
                            groups = NULL, at_least_one = TRUE) {

  if(method == "cpm") {
    library_size <- colSums(x)
    fx <- t(apply(x, 1, function(y) 1E6 * y / library_size))
  } else {
    fx <- x
  }

  if(!is.null(groups)) {
    good_features <- apply(fx >= min_value, 1, function(y) tapply(y, groups, function(z) sum(z) >= thres_samp))
    if(at_least_one) {
      good_features <-  colSums(good_features ) != 0
    } else {
      good_features  <-  colSums(good_features ) == nlevels(groups)
    }
  } else {
    good_features  <- rowSums(fx >= min_value) >= thres_samp
  }

  cat(sum(!good_features ), " features filtered...\n")
  list(counts = x[good_features , ], good_features = good_features)
}
leandroroser/RNASeqFunctions documentation built on May 17, 2019, 7:31 p.m.