From 1086e96e48fe4481b13c335688c192dfe33d1446 Mon Sep 17 00:00:00 2001 From: lcolladotor Date: Fri, 13 Dec 2024 16:29:36 -0500 Subject: [PATCH] Fix some things related to #93 + re-run devtools::document() --- R/gene_set_enrichment_plot.R | 108 ++++++++++++++++---------------- man/gene_set_enrichment.Rd | 12 ++++ man/gene_set_enrichment_plot.Rd | 7 ++- 3 files changed, 70 insertions(+), 57 deletions(-) diff --git a/R/gene_set_enrichment_plot.R b/R/gene_set_enrichment_plot.R index bc674b71..2882ba10 100644 --- a/R/gene_set_enrichment_plot.R +++ b/R/gene_set_enrichment_plot.R @@ -2,46 +2,46 @@ #' #' This function takes the output of [gene_set_enrichment()] and creates a #' ComplexHeatmap visualization of the results. Fill of the heatmap represents -#' the -log10(p-val), Odds-ratios are printed for test that pass specified -#' significance threshold `ORcut`. -#' -#' Includes functionality to plot the size of the input gene sets as barplot -#' annotations. +#' the -log10(p-val), Odds-ratios are printed for test that pass specified +#' significance threshold `ORcut`. +#' +#' Includes functionality to plot the size of the input gene sets as barplot +#' annotations. #' #' @param enrichment The output of [gene_set_enrichment()]. #' @param xlabs A vector of names in the same order and length as -#' `unique(enrichment$ID)`. +#' `unique(enrichment$ID)`. #' @param PThresh A `numeric(1)` specifying the P-value threshold for the #' maximum value in the `-log10(p)` scale. #' @param ORcut A `numeric(1)` specifying the P-value threshold for the #' minimum value in the `-log10(p)` scale for printing the odds ratio values -#' in the cells of the resulting plot. Defaults to 3 or p-val < 0.001. +#' in the cells of the resulting plot. Defaults to 3 or p-val < 0.001. #' @param enrichOnly A `logical(1)` indicating whether to show only odds ratio #' values greater than 1. #' @param mypal A `character` vector with the color palette to use. Colors will -#' be in order from 0 to lowest P-val `max(-log(enrichment$Pval))`. Defaults to -#' white, yellow, red pallet. +#' be in order from 0 to lowest P-val `max(-log(enrichment$Pval))`. Defaults to +#' white, yellow, red pallet. #' @param plot_SetSize_bar A `logical(1)` indicating whether to plot SetSize #' from `enrichment` as an `anno_barplot` at the top of the heatmap. -#' @param gene_list_length Optional named `numeric` vector indicating the length +#' @param gene_list_length Optional named `numeric` vector indicating the length #' of the `gene_list` used to calculate `enrichment`, if inclided and #' `plot_setSize_bar = TRUE` then the top `anno_barplot` will show the `SetSize` -#' and the difference from the length of the input gene_list. -#' #' @param model_sig_length Optional named `numeric` vector indicating the -#' number of significant genes in `modeling_results` used to calculate +#' and the difference from the length of the input gene_list. +#' #' @param model_sig_length Optional named `numeric` vector indicating the +#' number of significant genes in `modeling_results` used to calculate #' `enrichment`. If included `anno_barplot` will be added to rows. #' #' @param model_colors named `character` vector of colors, Adds colors to #' row annotations. #' #' @param ... Additional parameters passed to #' [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()]. #' -#' @return A ([Heatmap-class][ComplexHeatmap::Heatmap-class]) visualizing the +#' @return A ([Heatmap-class][ComplexHeatmap::Heatmap-class]) visualizing the #' gene set enrichment odds ratio and p-value results. #' @export #' @importFrom stats reshape #' @importFrom circlize colorRamp2 #' @importFrom ComplexHeatmap columnAnnotation rowAnnotation Heatmap anno_barplot -#' +#' #' @family Gene set enrichment functions #' @author Andrew E Jaffe, Leonardo Collado-Torres #' @seealso layer_matrix_plot @@ -81,26 +81,27 @@ #' ) #' #' ## Visualize the gene set enrichment results -#' +#' #' ## Default plot #' gene_set_enrichment_plot( #' enrichment = asd_sfari_enrichment #' ) -#' -#' ## Use a custom green color palette & use shorter gene set names (x-axis labels) +#' +#' ## Use a custom green color palette & use shorter gene set names +#' ## (x-axis labels) #' gene_set_enrichment_plot( #' asd_sfari_enrichment, #' xlabs = gsub(".*_", "", unique(asd_sfari_enrichment$ID)), #' mypal = c("white",RColorBrewer::brewer.pal(9, "BuGn")) #' ) -#' +#' #' ## Add bar plot annotations for SetSize of model genes in the gene_lists #' gene_set_enrichment_plot( #' asd_sfari_enrichment, #' xlabs = gsub(".*_", "", unique(asd_sfari_enrichment$ID)), #' plot_SetSize_bar = TRUE #' ) -#' +#' #' ## Add stacked bar plot annotations showing SetSize and difference from the #' ## length of the input gene_list #' gene_set_enrichment_plot( @@ -109,7 +110,7 @@ #' plot_SetSize_bar = TRUE, #' gene_list_length = lapply(asd_safari_geneList, length) #' ) -#' +#' #' ## add bar plot annotations for number of enriched genes from layers #' if (!exists("sce_layer")) sce_layer <- fetch_data(type = "sce_layer") #' sig_genes <- sig_genes_extract( @@ -118,10 +119,10 @@ #' sce_layer = sce_layer, #' n = nrow(sce_layer) #' ) -#' +#' #' sig_genes <- sig_genes[sig_genes$fdr < 0.1,] #' n_sig_model <- as.list(table(sig_genes$test)) -#' +#' #' ## add barplot with n significant genes from modeling #' gene_set_enrichment_plot( #' asd_sfari_enrichment, @@ -129,16 +130,15 @@ #' plot_SetSize_bar = TRUE, #' model_sig_length = n_sig_model #' ) -#' layer_gene_count <- get_gene_enrichment_count(model_results = modeling_results) #' -#'## add color annotaions +#' ## add color annotations #' gene_set_enrichment_plot( #' asd_sfari_enrichment, #' xlabs = gsub(".*_", "", unique(asd_sfari_enrichment$ID)), #' plot_SetSize_bar = TRUE, #' model_colors = libd_layer_colors #' ) -#' +#' #' ## add barplot with n significant genes from modeling filled with model color #' gene_set_enrichment_plot( #' asd_sfari_enrichment, @@ -147,7 +147,7 @@ #' model_sig_length = n_sig_model, #' model_colors = libd_layer_colors #' ) -#' +#' gene_set_enrichment_plot <- function( enrichment, @@ -169,37 +169,37 @@ gene_set_enrichment_plot <- "L", seq_len(6) ), "WM"))) } - + ## Check inputs stopifnot(is(enrichment, "data.frame")) stopifnot(all(c("ID", "test", "OR", "Pval") %in% colnames(enrichment))) stopifnot(ORcut <= PThresh) stopifnot(length(xlabs) == length(unique(enrichment$ID))) - + ## Convert to -log10 scale and threshold the pvalues enrichment$log10_P_thresh <- round(-log10(enrichment$Pval), 2) enrichment$log10_P_thresh[which(enrichment$log10_P_thresh > PThresh)] <- PThresh - + ## Change some values for the plot if (enrichOnly) { enrichment$log10_P_thresh[enrichment$OR < 1] <- 0 } enrichment$OR_char <- as.character(round(enrichment$OR, 2)) enrichment$OR_char[enrichment$log10_P_thresh < ORcut] <- "" - + ## sub xlabs labels if(!is.null(gene_list_length)){ stopifnot(setequal(names(gene_list_length), unique(enrichment$ID))) gene_list_length <- gene_list_length[unique(enrichment$ID)] names(gene_list_length) <- xlabs } - + for(i in seq(length(xlabs))){ enrichment$ID <- gsub(unique(enrichment$ID)[[i]], xlabs[[i]], enrichment$ID) } - + ## Make into wide matrices make_wide <- function(var = "OR_char") { res <- @@ -219,58 +219,58 @@ gene_set_enrichment_plot <- } wide_or <- make_wide("OR_char") wide_p <- make_wide("log10_P_thresh") - + ## define color pallet - mypal = circlize::colorRamp2(breaks = seq(0, max(wide_p), length.out = length(mypal)), + mypal = circlize::colorRamp2(breaks = seq(0, max(wide_p), length.out = length(mypal)), colors = mypal) - + ## Add gene count annotations enrichment_setsize <- unique(enrichment[,c("ID", "SetSize")]) - + ## COL annotations if(plot_SetSize_bar){ - + if(!is.null(gene_list_length)){ stopifnot(all(colnames(wide_p) %in% names(gene_list_length))) enrichment_setsize$SetInput <- unlist(gene_list_length[enrichment_setsize$ID]) enrichment_setsize$Diff <- enrichment_setsize$SetInput - enrichment_setsize$SetSize } - + rownames(enrichment_setsize) <- enrichment_setsize$ID enrichment_setsize$ID <- NULL enrichment_setsize$SetInput <- NULL ## only plot SetSize + Diff - + col_gene_anno <- ComplexHeatmap::columnAnnotation( `SetSize` = ComplexHeatmap::anno_barplot(enrichment_setsize) ) - + } else col_gene_anno <- NULL - - if(!is.null(model_colors)){ + + if(!is.null(model_colors)){ ## shorten names if they match HumanPilot data if (all(c("WM", paste0("Layer", seq_len(6))) %in% names(model_colors))) { names(model_colors) <-gsub("ayer", "", names(model_colors)) }} - - + + ## ROW annotations if(!is.null(model_sig_length)){ ## add row barplot annotation - + ## shorten names if they match HumanPilot data if (all(names(model_sig_length) %in% c("WM", paste0("Layer", seq_len(6))))) { names(model_sig_length) <-gsub("ayer", "", names(model_sig_length)) } - + stopifnot(all(rownames(wide_p) %in% names(model_sig_length))) model_sig_length <- t(data.frame(model_sig_length)) - + if(!is.null(model_colors)){ ## barplot with colors row_gene_anno <- ComplexHeatmap::rowAnnotation( `n\nmodel sig` = ComplexHeatmap::anno_barplot(model_sig_length[rownames(wide_p), ], gp = gpar(fill = model_colors[rownames(wide_p)]) ) # annotation_label = anno_title_row - + ) } else { ## barplot no colors row_gene_anno <- ComplexHeatmap::rowAnnotation( @@ -278,20 +278,20 @@ gene_set_enrichment_plot <- # annotation_label = anno_title_row ) } - + } else if(!is.null(model_colors)){ ## only apply color annotation - + stopifnot(all(rownames(wide_p) %in% names(model_colors))) model_colors <- model_colors[rownames(wide_p)] - + row_gene_anno <- ComplexHeatmap::rowAnnotation( " " = rownames(wide_p), col = list(" " = model_colors), show_legend = FALSE ) - + }else row_gene_anno <- NULL - + ComplexHeatmap::Heatmap(wide_p, col = mypal, name = "-log10(p-val)", diff --git a/man/gene_set_enrichment.Rd b/man/gene_set_enrichment.Rd index 04702437..bde810b4 100644 --- a/man/gene_set_enrichment.Rd +++ b/man/gene_set_enrichment.Rd @@ -43,6 +43,18 @@ into \code{layerB-layerA}.} \value{ A table in long format with the enrichment results using \code{\link[stats:fisher.test]{stats::fisher.test()}}. +\itemize{ +\item \code{OR} odds ratio. +\item \code{Pval} p-value for \code{fisher.test()}. +\item \code{test} group or layer in the \code{modeling_results}. +\item \code{NumSig} Number of genes from the gene set present in \code{modeling_results} & +with \code{fdr < fdr_cut} and \code{t_stat > 0} (unless reverse = TRUE) for \code{test} in +modeling results. +\item \code{SetSize} Number of genes from \code{modeling_results} present in \code{gene_set}. +\item \code{ID} name of gene set. +\item \code{model_type} record of input model type from \verb{modeling results}. +\item \code{fdr_cut} record of input \code{frd_cut}. +} } \description{ Using the layer-level (group-level) data, this function evaluates whether diff --git a/man/gene_set_enrichment_plot.Rd b/man/gene_set_enrichment_plot.Rd index 8370f5a6..2acad49d 100644 --- a/man/gene_set_enrichment_plot.Rd +++ b/man/gene_set_enrichment_plot.Rd @@ -6,6 +6,7 @@ \usage{ gene_set_enrichment_plot( enrichment, + xlabs = unique(enrichment$ID), PThresh = 12, ORcut = 3, enrichOnly = FALSE, @@ -108,7 +109,8 @@ gene_set_enrichment_plot( enrichment = asd_sfari_enrichment ) -## Use a custom green color palette & use shorter gene set names (x-axis labels) +## Use a custom green color palette & use shorter gene set names +## (x-axis labels) gene_set_enrichment_plot( asd_sfari_enrichment, xlabs = gsub(".*_", "", unique(asd_sfari_enrichment$ID)), @@ -130,7 +132,6 @@ gene_set_enrichment_plot( plot_SetSize_bar = TRUE, gene_list_length = lapply(asd_safari_geneList, length) ) -layer_gene_count <- get_gene_enrichment_count(model_results = modeling_results) ## add bar plot annotations for number of enriched genes from layers if (!exists("sce_layer")) sce_layer <- fetch_data(type = "sce_layer") @@ -152,7 +153,7 @@ gene_set_enrichment_plot( model_sig_length = n_sig_model ) -## add color annotaions +## add color annotations gene_set_enrichment_plot( asd_sfari_enrichment, xlabs = gsub(".*_", "", unique(asd_sfari_enrichment$ID)),