R/extract_experimental_groups.R

Defines functions extract_experimental_groups

Documented in extract_experimental_groups

#' Extract the experimental groups from ExpressionSet metadata
#'
#' @param ExprSet S4 type object containing expression and meta data. 
#' @param case_ctrl_col String or character vector with the ExpressionSet column name containing
#'     the Case/Control information.
#'
#' @return group, a character vector with each subsequent entry corresponding to a sample in the dataset.
#' @export
#'
#' @examples
#' 
#' gse <- download_gse_data("GSE108000")
#' 
#' group <- extract_experimental_groups(gse, "title")
#' 
extract_experimental_groups <- function(ExprSet = NULL, case_ctrl_col = NULL) {
  
  #for now i'm only including "control" because i need to figure out how to use apply properly so all of these
  #string options can be used. Or i need to find a more elegant way of doing this function.
  #this post seemed helpful
  #https://stackoverflow.com/questions/34702882/assigning-groups-using-grepl-with-multiple-inputs
  control_str_options <- "control"
  
  status_col <- as.character(ExprSet[[case_ctrl_col]])
  
  is_control <- grepl(stringr::fixed(paste(control_str_options), ignore_case = TRUE), status_col, perl = TRUE, ignore.case = TRUE)
  
  group <- vector()
 
  for (i in 1:length(is_control)) {
    
    if (is_control[i] == "TRUE") {
      
      group[i] <- "Control"
    
    }else{
      
      group[i] <- "Case"
      
    }
  }
  
  on.exit(cat("Samples successfully assigned to groups!\n"))
  
  return(group)
  
}
jeffreyLbrabec/tinker documentation built on Nov. 4, 2019, 2:37 p.m.