R/plot_marker_express_ALL.R

Defines functions plot_marker_express_ALL

Documented in plot_marker_express_ALL

#' Plot the top marker genes for ALL cell types
#'
#' This function plots the top n marker genes for a all cell types based off of
#' the `stats` table from `get_mean_ratio2()` in a multi-page pdf.
#' The gene expression is plotted as violin plot with `plot_gene_express` and adds
#' annotations to each plot.
#'
#' @param sce [SummarizedExperiment-class][SummarizedExperiment::SummarizedExperiment-class] object
#' @param stats A `data.frame()` generated by get_mean_ratio and/or findMarkers_1vAll
#' @param pdf_fn A `character()` of the pdf filename to plot to
#' @param n_genes An `integer()` of number of markers you'd like to plot
#' @param rank_col The `character()` name of column to rank genes by in `stats`
#' @param anno_col The `character()` name of column containing annotation in `stats`
#' @param cellType_col The `character()` name of colData column containing cell type for sce data,
#'  matches `cellType.target` in `stats`
#' @param color_pal  A named `character(1)` vector that contains a color pallet matching the values in `cellType_col`.
#' @param plot_points A logical indicating whether to plot points over the violin,
#' defaults to `FALSE` as these often become over plotted and quite large (especially when saved as PDF)
#'
#' @return A pdf with violin plots for the expression of top marker genes for all cell types
#' @export
#'
#' @examples
#' # plot_marker_express_ALL(sce.test, stat = marker_test, pdf_fn = "./plots/test_marker_expression_ALL.pdf")
#' @family expression plotting functions
#' @importFrom ggplot2 ggplot geom_violin geom_text facet_wrap stat_summary
plot_marker_express_ALL <- function(
        sce,
        stats,
        pdf_fn = "marker_expression.pdf",
        n_genes = 10,
        rank_col = "rank_ratio",
        anno_col = "anno_ratio",
        cellType_col = "cellType",
        color_pal = NULL,
        plot_points = FALSE) {
    stopifnot(cellType_col %in% colnames(colData(sce)))

    if (is.factor(sce[[cellType_col]])) {
        cell_types <- levels(sce[[cellType_col]])
    } else {
        cell_types <- unique(sce[[cellType_col]])
    }

    if (!all(cell_types %in% stats$cellType.target)) {
        # missing <- cell_types[!cell_types %in% stats$cellType.target]
        stop("Stats is missing cell types, check you're using the correct marker stats data and cellType_col")
    }

    grDevices::pdf(pdf_fn)
    message("Plotting Markers for...")
    for (ct in cell_types) {
        message("\t", ct)
        print(plot_marker_express(
            sce = sce,
            stats = stats,
            cell_type = ct,
            n_genes = n_genes,
            rank_col = rank_col,
            anno_col = anno_col,
            cellType_col = cellType_col,
            color_pal = color_pal,
            plot_points = plot_points
        ))
    }
    message("Done!")
    grDevices::dev.off()
}
lahuuki/DeconvoBuddies documentation built on May 5, 2024, 9:35 a.m.