8c3894dc |
#' @title Run Cluster Summary Metrics
#' @description Calculates the mean expression of percent of cells that express the
#' given genes for each cluster
#'
#' @param inSCE The single cell experiment to use.
#' @param useAssay The assay to use.
|
4a5b5f77 |
#' @param featureNames A string or vector of strings with each gene to aggregate.
|
5992e49b |
#' @param displayName A string that is the name of the column used for genes.
|
4a5b5f77 |
#' @param groupNames The name of a colData entry that can be used as groupNames.
|
d6c4f292 |
#' @param scale Option to scale the data. Default: \code{FALSE}. Selected assay will not be scaled.
|
8c3894dc |
#' @return A dataframe with mean expression and percent of cells in cluster that
#' express for each cluster.
#' @examples
|
30cf9713 |
#' data("scExample")
|
4a5b5f77 |
#' runClusterSummaryMetrics(inSCE=sce, useAssay="counts", featureNames=c("B2M", "MALAT1"),
#' displayName="feature_name", groupNames="type")
|
8c3894dc |
#' @export
|
61da00f2 |
runClusterSummaryMetrics <- function(inSCE, useAssay="logcounts", featureNames, displayName=NULL, groupNames="cluster", scale = FALSE){
if(isTRUE(scale)){
runNormalization(inSCE=inSCE, useAssay=useAssay, scale = TRUE, normalizationMethod = NULL, transformation = NULL,
pseudocountsBeforeNorm = NULL, pseudocountsBeforeTransform = NULL)
}
|
4a5b5f77 |
if (!groupNames %in% names(SingleCellExperiment::colData(inSCE))) {
stop("Specified variable '", groupNames, "' not found in colData(inSCE)")
|
8c3894dc |
}
|
c971f8ec |
if (!is.null(displayName)) {
dimnames(inSCE)[[1]] <- rowData(inSCE)[[displayName]]
dimnames(assay(inSCE, i=useAssay, withDimnames=FALSE))[[1]] <- rowData(inSCE)[[displayName]]
|
4a5b5f77 |
falseGenes <- setdiff(featureNames, rowData(inSCE)[[displayName]])
featureNames <- intersect(featureNames, rowData(inSCE)[[displayName]])
|
c971f8ec |
warning <- paste0("rowData(inSCE)$", displayName)
}
else {
|
4a5b5f77 |
falseGenes <- setdiff(featureNames, dimnames(inSCE)[[1]])
featureNames <- intersect(featureNames, dimnames(inSCE)[[1]])
|
c971f8ec |
warning <- "dimnames"
}
|
4a5b5f77 |
if (length(featureNames) == 0) {
|
c971f8ec |
stop("All genes in '", toString(falseGenes), "' not found in ", warning)
}
|
8c3894dc |
if (length(falseGenes) > 0) {
|
c971f8ec |
warning("Specified genes '", toString(falseGenes), "' not found in ", warning)
|
8c3894dc |
}
|
c971f8ec |
|
61da00f2 |
tempSCE <- inSCE[featureNames, ]
if(isTRUE(scale)){
|
79909625 |
tempSCE <- runNormalization(inSCE=tempSCE, outAssayName = "scaled", useAssay=useAssay,scale = TRUE, normalizationMethod = NULL, transformation = NULL,
|
61da00f2 |
pseudocountsBeforeNorm = NULL, pseudocountsBeforeTransform = NULL)
|
79909625 |
useAssay <- "scaled"
|
61da00f2 |
}
|
79fe5e19 |
|
61da00f2 |
avgExpr <- assay(scuttle::aggregateAcrossCells(tempSCE, ids=SingleCellExperiment::colData(inSCE)[,groupNames],
|
03ad5922 |
statistics="mean", use.assay.type=useAssay,
|
61da00f2 |
subset.row=NULL))
|
c83b010c |
|
8c3894dc |
|
61da00f2 |
percExpr <- assay(scuttle::aggregateAcrossCells(tempSCE, ids=SingleCellExperiment::colData(inSCE)[,groupNames],
|
03ad5922 |
statistics="prop.detected", use.assay.type=useAssay,
|
61da00f2 |
subset.row=NULL))
|
8c3894dc |
|
c83b010c |
|
4a5b5f77 |
df <- data.frame(featureNames = featureNames)
|
c83b010c |
df$avgExpr <- avgExpr
df$percExpr <- percExpr
return(df)
|
c971f8ec |
}
|