R/plotRrnaMappingRate-methods.R

#' @name plotRrnaMappingRate
#' @author Michael Steinbaugh, Rory Kirchner, Victor Barrera
#' @inherit AcidGenerics::plotRrnaMappingRate
#' @note Updated 2022-05-09.
#'
#' @inheritParams AcidRoxygen::params
#' @param ... Additional arguments.
#'
#' @examples
#' data(bcb)
#'
#' ## bcbioRNASeq ====
#' plotRrnaMappingRate(bcb)
NULL



## Updated 2022-05-09.
`plotRrnaMappingRate,bcbioRNASeq` <- # nolint
    function(object,
             interestingGroups = NULL,
             limit = 0.1,
             labels = list(
                 "title" = "rRNA mapping rate",
                 "subtitle" = NULL,
                 "sampleAxis" = NULL,
                 "metricAxis" = "rRNA mapping rate (%)"
             ),
             flip = getOption(x = "acid.flip", default = TRUE)) {
        validObject(object)
        assert(
            isProportion(limit),
            isFlag(flip)
        )
        labels <- matchLabels(labels)
        interestingGroups(object) <-
            matchInterestingGroups(object, interestingGroups)
        interestingGroups <- interestingGroups(object)
        data <- metrics(object)
        metricCol <- "rrnaRate"
        ## Warn and early return if rRNA rate was not calculated.
        if (!isSubset(metricCol, colnames(data))) {
            alertWarning("rRNA mapping rate was not calculated. Skipping plot.")
            return(invisible(NULL))
        }
        p <- ggplot(
            data = as.data.frame(data),
            mapping = aes(
                x = .data[["sampleName"]],
                y = .data[[metricCol]] * 100L,
                fill = .data[["interestingGroups"]]
            )
        ) +
            acid_geom_bar() +
            acid_scale_y_continuous_nopad()
        ## Labels.
        labels[["fill"]] <- paste(interestingGroups, collapse = ":\n")
        names(labels)[names(labels) == "sampleAxis"] <- "x"
        names(labels)[names(labels) == "metricAxis"] <- "y"
        p <- p + do.call(what = labs, args = labels)
        ## Limit.
        if (isPositive(limit)) {
            limit <- limit * 100L
            if (limit < 100L) {
                p <- p + acid_geom_abline(yintercept = limit)
            }
        }
        ## Color palette.
        p <- p + acid_scale_fill_discrete()
        ## Flip.
        if (isTRUE(flip)) {
            p <- p + acid_discrete_coord_flip()
        }
        ## Hide sample name legend.
        if (identical(interestingGroups, "sampleName")) {
            p <- p + guides(fill = "none")
        }
        ## Return.
        p
    }



#' @rdname plotRrnaMappingRate
#' @export
setMethod(
    f = "plotRrnaMappingRate",
    signature = signature(object = "bcbioRNASeq"),
    definition = `plotRrnaMappingRate,bcbioRNASeq`
)
hbc/bcbioRNASeq documentation built on March 28, 2024, 3:01 p.m.