R/methods-plotPCACovariates.R

#' Find Correlation Between Principal Components (PCs) and Covariates
#'
#' [DEGreport::degCovariates()] wrapper supporting a [bcbioRNASeq] object.
#'
#' @name plotPCACovariates
#' @family Quality Control Functions
#' @author Lorena Pantano, Michael Steinbaugh
#'
#' @inheritParams general
#' @param metrics Include sample summary metrics as covariates. Defaults to
#'   include all metrics columns (`TRUE`), but desired columns can be specified
#'   here as a character vector.
#' @param ... Additional arguments, passed to [DEGreport::degCovariates()].
#'
#' @seealso
#' - [DEGreport::degCovariates()].
#' - [DESeq2::rlog()].
#' - [DESeq2::varianceStabilizingTransformation()].
#'
#' @return `ggplot`.
#'
#' @examples
#' plotPCACovariates(
#'     object = bcb_small,
#'     metrics = c("exonicRate", "intronicRate")
#' )
NULL



# Methods ======================================================================
#' @rdname plotPCACovariates
#' @export
setMethod(
    "plotPCACovariates",
    signature("bcbioRNASeq"),
    function(
        object,
        metrics = TRUE,
        normalized = c("rlog", "vst", "tmm", "tpm"),
        ...
    ) {
        validObject(object)
        assert_is_any_of(metrics, c("character", "logical"))
        normalized <- match.arg(normalized)

        counts <- counts(object, normalized = normalized)

        metadata <- metrics(object)
        factors <- select_if(metadata, is.factor)
        numerics <- select_if(metadata, is.numeric) %>%
            # Drop columns that are all zeroes (not useful to plot)
            .[, colSums(.) > 0L, drop = FALSE]
        metadata <- cbind(factors, numerics)

        # Select the metrics to use for plot
        if (identical(metrics, TRUE)) {
            # Sort columns alphabetically
            col <- sort(colnames(metadata))
        } else if (identical(metrics, FALSE)) {
            # Use the defined interesting groups
            col <- interestingGroups(object)
        } else if (is.character(metrics)) {
            col <- metrics
        }

        # Stop on 1 metrics column
        assert_all_are_greater_than(length(col), 1L)
        assert_is_subset(col, colnames(metadata))
        metadata <- metadata[, col, drop = FALSE]

        degCovariates(
            counts = counts,
            metadata = metadata,
            ...
        )
    }
)
roryk/bcbioRnaseq documentation built on May 27, 2019, 10:44 p.m.