R/subsetSCE.R
12e9568f
 #' @title Subset a SingleCellExperiment object by rows
 #' @description Used to peform subsetting of a
 #' \linkS4class{SingleCellExperiment} object using a variety of methods that
 #' indicate the correct rows to keep. The various methods,
 #' \code{index}, \code{bool}, and \code{rowData}, can be used in conjunction
 #' with one another. If \code{returnAsAltExp} is set to \code{TRUE},
 #' then the returned object will have the same number of rows as the input
 #' \code{inSCE} as the subsetted object will be stored in the
6fe21ae1
 #' \code{\link{altExp}} slot.
12e9568f
 #' @param inSCE Input \linkS4class{SingleCellExperiment} object.
 #' @param index Integer vector. Vector of indicies indicating which rows
 #' to keep. If \code{NULL}, this will not be used for subsetting.
 #' Default \code{NULL}.
 #' @param bool Boolean vector. Vector of \code{TRUE} or \code{FALSE}
 #' indicating which rows should be kept. Needs to be the same length as the
 #' number of rows in \code{inSCE}. If \code{NULL}, this will not be used
 #' for subsetting. Default \code{NULL}.
 #' @param rowData Character. An expression that will identify a subset of rows
 #' using variables found in the \code{rowData} of \code{inSCE}. For example,
 #' if \code{x} is a numeric vector in \code{rowData}, then \code{"x < 5"} will
09571895
 #' return all rows with x less than 5. Single quotes should be used for
12e9568f
 #' character strings. For example, \code{"y == 'yes'"} will return all
 #' rows where y is "yes". Multiple expressions can be evaluated by placing them
 #' in a vector. For example \code{c("x < 5", "y =='yes'")} will apply both
 #' operations for subsetting. If \code{NULL}, this will not be used for
 #' subsetting. Default \code{NULL}.
09571895
 #' @param returnAsAltExp Boolean. If \code{TRUE}, the subsetted
12e9568f
 #' \linkS4class{SingleCellExperiment} object will be returned in the
 #' \code{altExp} slot of \code{inSCE}. If \code{FALSE}, the subsetted
 #' \linkS4class{SingleCellExperiment} object will be directly returned.
 #' @param altExpName Character. Name of the alternative experiment object to
 #' add if \code{returnAsAltExp = TRUE}. Default \code{subset}.
 #' @param prependAltExpName Boolean. If \code{TRUE}, \code{altExpName} will
 #' be added to the beginning of the assay names in the \code{altExp} object.
 #' This is only utilized if \code{returnAsAltExp = TRUE}. Default \code{TRUE}.
 #' @author Joshua D. Campbell
09571895
 #' @return A \link[SingleCellExperiment]{SingleCellExperiment} object that has
 #' been subsetted by rowData.
12e9568f
 #' @examples
 #' data(scExample)
09571895
 #'
12e9568f
 #' # Set a variable up in the rowData indicating mitochondrial genes
 #' rowData(sce)$isMito <- ifelse(grepl("^MT-", rowData(sce)$feature_name),
 #'                               "yes", "no")
09571895
 #' sce <- subsetSCERows(sce, rowData = "isMito == 'yes'")
12e9568f
 #' @export
05baeaeb
 #' @importFrom SummarizedExperiment assays assays<-
12e9568f
 subsetSCERows <- function(inSCE, index = NULL, bool = NULL, rowData = NULL,
                           returnAsAltExp = TRUE, altExpName = "subset",
                           prependAltExpName = TRUE) {
09571895
 
12e9568f
   if(is.null(index) & is.null(bool) & is.null(rowData)) {
     stop("At least one of 'index', 'bool', or 'rowData' must be supplied.")
   }
f41eb9f2
   final.ix <- rep(TRUE, nrow(inSCE))
09571895
 
12e9568f
   # Parse index containing integers
   if(!is.null(index)) {
     if(min(index) < 1 | max(index) > nrow(inSCE)) {
       stop("'index' must contain integers between 1 and the number of rows ",
            "in 'inSCE': ", nrow(inSCE))
     }
f41eb9f2
     final.ix[!seq(nrow(inSCE)) %in% index] <- FALSE
12e9568f
   }
09571895
 
12e9568f
   # Parse Boolean vector
   if(!is.null(bool)) {
     if(length(bool) != nrow(inSCE) | !is.logical(bool)) {
       stop("'bool' must be a logical vector the same length as the number of ",
            "rows in 'inSCE': ", nrow(inSCE))
     }
f41eb9f2
     final.ix[!bool] <- FALSE
12e9568f
   }
09571895
 
12e9568f
   # Parse expressions for rowData variables
   if(!is.null(rowData)) {
     for(i in seq_along(rowData)) {
       temp <- eval(parse(text = as.character(rowData[i])),
                    envir = as.data.frame(rowData(inSCE)))
       if(length(temp) != nrow(inSCE) | !is.logical(temp)) {
09571895
         stop("The expression ", rowData[i], " did not produce a boolean ",
12e9568f
              "vector the same length as the number of rows in 'inSCE'. ",
              "Please ensure that the spelling of the variable you are ",
              "trying to use matches one of the column names in ",
              "'rowData(inSCE)' and that your expression is valid.")
       }
a792a109
       final.ix[!temp] <- FALSE
12e9568f
     }
   }
09571895
 
12e9568f
   # Filter object and return in different ways depending on flag
   temp.SCE <- inSCE[final.ix,]
   if(isTRUE(returnAsAltExp)) {
     if(is.null(altExpName) || length(altExpName) > 1
                            || !is.character(altExpName)) {
       stop("'altExpName' needs to be a character of length 1 if the subset
            is going to be saved as an ", "'altExp' object.")
     }
     if(isTRUE(prependAltExpName)) {
09571895
       names(assays(temp.SCE)) <- paste0(altExpName, names(assays(temp.SCE)))
12e9568f
     }
     SingleCellExperiment::altExp(inSCE, altExpName) <- temp.SCE
   } else {
     inSCE <- temp.SCE
   }
   return(inSCE)
 }
 
 #' @title Subset a SingleCellExperiment object by columns
 #' @description Used to peform subsetting of a
 #' \linkS4class{SingleCellExperiment} object using a variety of methods that
 #' indicate the correct columns to keep. The various methods,
 #' \code{index}, \code{bool}, and \code{colData}, can be used in conjunction
09571895
 #' with one another.
12e9568f
 #' @param inSCE Input \linkS4class{SingleCellExperiment} object.
 #' @param index Integer vector. Vector of indicies indicating which columns
 #' to keep. If \code{NULL}, this will not be used for subsetting.
 #' Default \code{NULL}.
 #' @param bool Boolean vector. Vector of \code{TRUE} or \code{FALSE}
 #' indicating which columns should be kept. Needs to be the same length as the
 #' number of columns in \code{inSCE}. If \code{NULL}, this will not be used
 #' for subsetting. Default \code{NULL}.
 #' @param colData Character. An expression that will identify a subset of
 #' columns using variables found in the \code{colData} of \code{inSCE}.
 #' For example, if \code{x} is a numeric vector in \code{colData},
 #' then \code{"x < 5"} will return all columns with x less than 5.
 #' Single quotes should be used for character strings. For example,
 #' \code{"y == 'yes'"} will return all columns where y is "yes".
 #' Multiple expressions can be evaluated by placing them in a vector.
09571895
 #' For example \code{c("x < 5", "y =='yes'")} will apply both operations for
12e9568f
 #' subsetting. If \code{NULL}, this will not be used for subsetting.
 #' Default \code{NULL}.
09571895
 #' @return A \link[SingleCellExperiment]{SingleCellExperiment} object that has
 #' been subsetted by colData.
12e9568f
 #' @author Joshua D. Campbell
 #' @examples
 #' data(scExample)
09571895
 #' sce <- subsetSCECols(sce, colData = "type != 'EmptyDroplet'")
12e9568f
 #' @export
 subsetSCECols <- function(inSCE, index = NULL, bool = NULL, colData = NULL) {
09571895
 
12e9568f
   if(is.null(index) & is.null(bool) & is.null(colData)) {
     stop("At least one of 'index', 'bool', or 'colData' must be supplied.")
   }
f41eb9f2
   final.ix <- rep(TRUE, ncol(inSCE))
09571895
 
12e9568f
   # Parse index containing integers
   if(!is.null(index)) {
     if(min(index) < 1 | max(index) > ncol(inSCE)) {
       stop("'index' must contain integers between 1 and the number of columns ",
            "in 'inSCE': ", ncol(inSCE))
     }
f41eb9f2
     final.ix[!seq(ncol(inSCE)) %in% index] <- FALSE
12e9568f
   }
09571895
 
12e9568f
   # Parse Boolean vector
   if(!is.null(bool)) {
     if(length(bool) != ncol(inSCE) | !is.logical(bool)) {
       stop("'bool' must be a logical vector the same length as the number of ",
            "colmns in 'inSCE': ", ncol(inSCE))
     }
f41eb9f2
     final.ix[!bool] <- FALSE
12e9568f
   }
09571895
 
12e9568f
   # Parse expressions for colData variables
   if(!is.null(colData)) {
     for(i in seq_along(colData)) {
       temp <- eval(parse(text = as.character(colData[i])),
                    envir = as.data.frame(colData(inSCE)))
       if(length(temp) != ncol(inSCE) | !is.logical(temp)) {
09571895
         stop("The expression ", colData[i], " did not produce a boolean ",
12e9568f
              "vector the same length as the number of columns in 'inSCE'. ",
              "Please ensure that the spelling of the variable you are ",
              "trying to use matches one of the column names in ",
              "'colData(inSCE)' and that your expression is valid.")
       }
a792a109
       final.ix[!temp] <- FALSE
12e9568f
     }
   }
09571895
 
12e9568f
   inSCE <- inSCE[,final.ix]
5f6e4ace
   if(!is.null(S4Vectors::metadata(inSCE)$seurat$obj)){
     S4Vectors::metadata(inSCE)$seurat$obj@meta.data <- S4Vectors::metadata(inSCE)$seurat$obj@meta.data[final.ix, ]
9b54f208
   }
12e9568f
   return(inSCE)
05baeaeb
   }