R/check_contrasts.R

Defines functions check_contrasts

Documented in check_contrasts

#' Check contrasts
#'
#' Check all possible contrasts used by \code{\link{results_all}}
#'
#' @param trt a vector
#' @param vs either compare all vs. all (default) or a all vs. a specific treatment, or see note.
#' @param vs2 position of specific treatment in contrast vector, set FALSE for specific treatment vs all
#'
#' @note If you combine factors of interest into a single group following section 3.3 in the DESeq2 vignette,
#'  you can set vs = "combined" to limit the comparisons to those within the first group.
#'
#' @return A vector of contrasts
#'
#' @author Chris Stubben
#'
#' @examples
#'  # check_contrasts( samples$trt)
#'  trt <- c("heart", "lung", "control")
#'  check_contrasts(trt)
#'  data.frame( vs=check_contrasts(trt))
#'  check_contrasts(trt, vs = "control")
#'  check_contrasts(trt, vs = "control", vs2=FALSE)
#'  check_contrasts(factor(trt, levels = trt)  )
#'  # combine 2 trt groups  (66 possible contrasts, 12 within 1st group).
#'  trt <- paste( 1:4, rep(c("A", "B", "C"), each=4))
#'  check_contrasts( trt, vs = "combined")
#' @export

check_contrasts <- function( trt, vs="all", vs2 = TRUE){
   if(is.factor(trt)){
      n <- levels( trt )
   }else{
      n <- sort(unique(trt))
   }
   contrast <- utils::combn(n, 2)
   if( vs == "combined"){
      ## if two columns are combined into a single trt group, compare within first group
      n1 <- apply(contrast, 2, function(x) length(unique( gsub("[ _-].+", "", x)))==1)
      contrast <- contrast[, n1]
   }else if( vs %in% n){
      if(vs2){
         contrast <- rbind( n[n!=vs], vs)
      }else{
         contrast <- rbind( vs, n[n!=vs])
      }
   }
   vs <- apply(contrast, 2, paste, collapse = " vs. ")
   message(length(vs), " contrasts: ")
   vs
}
HuntsmanCancerInstitute/hciR documentation built on March 6, 2020, 12:40 p.m.