R/methods-plot5Prime3PrimeBias.R

#' Plot 5'->3' Bias
#'
#' @name plot5Prime3PrimeBias
#' @family Quality Control Functions
#' @author Michael Steinbaugh
#'
#' @inheritParams general
#'
#' @return `ggplot`.
#'
#' @examples
#' plot5Prime3PrimeBias(bcb_small)
NULL



# Methods ======================================================================
#' @rdname plot5Prime3PrimeBias
#' @export
setMethod(
    "plot5Prime3PrimeBias",
    signature("bcbioRNASeq"),
    function(
        object,
        interestingGroups,
        limit = 2L,
        fill = scale_fill_hue(),
        flip = TRUE,
        title = "5'->3' bias"
    ) {
        validObject(object)
        if (missing(interestingGroups)) {
            interestingGroups <- bcbioBase::interestingGroups(object)
        }
        assertIsAnImplicitInteger(limit)
        assert_all_are_non_negative(limit)
        assertIsFillScaleDiscreteOrNULL(fill)
        assert_is_a_bool(flip)
        assertIsAStringOrNULL(title)

        metrics <- metrics(object) %>%
            uniteInterestingGroups(interestingGroups)

        # Legacy code: make sure `x53Bias` is camel sanitized to `x5x3Bias`.
        # The internal camel method has been updated in basejump 0.1.11.
        if ("x53Bias" %in% colnames(metrics)) {
            metrics[["x5x3Bias"]] <- metrics[["x53Bias"]]
        }

        p <- ggplot(
            data = metrics,
            mapping = aes_string(
                x = "sampleName",
                y = "x5x3Bias",
                fill = "interestingGroups"
            )
        ) +
            geom_bar(
                color = "black",
                stat = "identity"
            ) +
            labs(
                title = title,
                x = "sample",
                y = "5'->3' bias",
                fill = paste(interestingGroups, collapse = ":\n")
            )

        if (is_positive(limit)) {
            p <- p + .qcLine(limit)
        }

        if (is(fill, "ScaleDiscrete")) {
            p <- p + fill
        }

        if (isTRUE(flip)) {
            p <- p + coord_flip()
        }

        if (identical(interestingGroups, "sampleName")) {
            p <- p + guides(fill = FALSE)
        }

        p
    }
)
WeiSong-bio/roryk-bcbioRNASeq documentation built on July 6, 2019, 12:02 a.m.