From af72cbed52176d8543af779d69246ae2f24e8c8d Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Tue, 10 Dec 2024 15:46:01 -0500 Subject: [PATCH 01/15] Improve title for layer_stat_cor_plot --- R/layer_stat_cor_plot.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/layer_stat_cor_plot.R b/R/layer_stat_cor_plot.R index 0d2653b8..a293fb66 100644 --- a/R/layer_stat_cor_plot.R +++ b/R/layer_stat_cor_plot.R @@ -1,4 +1,4 @@ -#' Visualize the layer modeling correlation of statistics +#' Visualize the correlation of layer modeling t-statistics #' #' This function makes a heatmap from the [layer_stat_cor()] correlation matrix #' between a given set of cell cluster/type statistics derived from scRNA-seq From 18b1a57079d6570010165bd480678b60f3679160 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Tue, 10 Dec 2024 16:54:36 -0500 Subject: [PATCH 02/15] Update docs for layer stat cor plot --- man/layer_stat_cor_plot.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/layer_stat_cor_plot.Rd b/man/layer_stat_cor_plot.Rd index 73496b45..e01991a9 100644 --- a/man/layer_stat_cor_plot.Rd +++ b/man/layer_stat_cor_plot.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/layer_stat_cor_plot.R \name{layer_stat_cor_plot} \alias{layer_stat_cor_plot} -\title{Visualize the layer modeling correlation of statistics} +\title{Visualize the correlation of layer modeling t-statistics} \usage{ layer_stat_cor_plot( cor_stats_layer, From c371907b138892c25acf64fd5270146553877ede Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Tue, 10 Dec 2024 16:58:52 -0500 Subject: [PATCH 03/15] Start adding ComplexHeatmap version of layer_stat_cor_plot, add color vectors as annotations --- NAMESPACE | 1 + R/layer_stat_cor_plot_complex.R | 99 ++++++++++++++++++++++++++++++ man/layer_stat_cor_plot_complex.Rd | 71 +++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 R/layer_stat_cor_plot_complex.R create mode 100644 man/layer_stat_cor_plot_complex.Rd diff --git a/NAMESPACE b/NAMESPACE index 42d8758a..946c57f9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ export(layer_boxplot) export(layer_matrix_plot) export(layer_stat_cor) export(layer_stat_cor_plot) +export(layer_stat_cor_plot_complex) export(locate_images) export(read10xVisiumAnalysis) export(read10xVisiumWrapper) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R new file mode 100644 index 00000000..f27c7d27 --- /dev/null +++ b/R/layer_stat_cor_plot_complex.R @@ -0,0 +1,99 @@ + + +#' Visualize the correlation of layer modeling t-statistics with ComplexHeatmap +#' @param query_colors named vector of colors for query row annotations +#' @param reference_colors named vector of colors for reference column annotations +#' +#' @inheritParams layer_stat_cor_plot +#' +#' @return ComplexHeatmap plot of t-stat correlations +#' @export +#' +#' @examples +#' ## Obtain the necessary data +#' ## reference human pilot modeling results +#' if (!exists("modeling_results")) { +#' modeling_results <- fetch_data(type = "modeling_results") +#' } +#' +#' ## querey spatailDLPFC modeling +#' query_modeling_results <- fetch_data(type = "spatialDLPFC_Visium_modeling_results") +#' +#' ## extract t-statics and rename +#' registration_t_stats <- query_modeling_results$enrichment[, grep("^t_stat", colnames(query_modeling_results$enrichment))] +#' colnames(registration_t_stats) <- gsub("^t_stat_", "", colnames(registration_t_stats)) +#' +#' ## Compute the correlations +#' cor_stats_layer <- layer_stat_cor( +#' stats = registration_t_stats, +#' modeling_results, +#' model_type = "enrichment" +#' ) +#' +#' ## Visualize the correlation matrix +#' +#' ## most basic +#' layer_stat_cor_plot_complex(cor_stats_layer) +#' +#' ## add colors +#' ## add libd_layer_colors to refrence Human Pilot layers +#' layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) +#' +#' ## supply polychrome colors to query clusters +#' cluster_colors <- Polychrome::palette36.colors(nrow(cor_stats_layer)) +#' names(cluster_colors) <- rownames(cor_stats_layer) +#' +#' layer_stat_cor_plot_complex(cor_stats_layer, +#' query_colors = cluster_colors, +#' reference_colors = libd_layer_colors) +#' +#' ## Apply additional ComplexHeatmap param +#' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE) +#' +layer_stat_cor_plot_complex <- function(cor_stats_layer, + theSeq = seq(min(cor_stats_layer), max(cor_stats_layer), by = 0.01), + my.col = grDevices::colorRampPalette(RColorBrewer::brewer.pal(7, "PRGn"))(length(theSeq)), + query_colors = NULL, + reference_colors = NULL, + ... + ){ + + # ## query annotations on row + if(!is.null(query_colors)){ + + stopifnot(all(rownames(cor_stats_layer) %in% names(query_colors))) + query_colors <- query_colors[rownames(cor_stats_layer)] + + + query_row_annotation <- ComplexHeatmap::rowAnnotation( + " " = rownames(cor_stats_layer), + col = list(" " = query_colors), + show_legend = FALSE) + + } else query_row_annotation <- NULL + + ## reference annotation on bottom + if(!is.null(reference_colors)){ + stopifnot(all(colnames(cor_stats_layer) %in% names(reference_colors))) + reference_colors <- reference_colors[colnames(cor_stats_layer)] + + ref_col_annotation <- ComplexHeatmap::columnAnnotation( + " " = colnames(cor_stats_layer), + col = list(" " = reference_colors), + show_legend = FALSE + ) + } else ref_col_annotation <- NULL + + + + ## plot heatmap + ComplexHeatmap::Heatmap( + matrix = cor_stats_layer, + col = my.col, + bottom_annotation = ref_col_annotation, + right_annotation = query_row_annotation, + ... + ) + + +} diff --git a/man/layer_stat_cor_plot_complex.Rd b/man/layer_stat_cor_plot_complex.Rd new file mode 100644 index 00000000..839d6743 --- /dev/null +++ b/man/layer_stat_cor_plot_complex.Rd @@ -0,0 +1,71 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/layer_stat_cor_plot_complex.R +\name{layer_stat_cor_plot_complex} +\alias{layer_stat_cor_plot_complex} +\title{Visualize the correlation of layer modeling t-statistics with ComplexHeatmap} +\usage{ +layer_stat_cor_plot_complex( + cor_stats_layer, + theSeq = seq(min(cor_stats_layer), max(cor_stats_layer), by = 0.01), + my.col = (grDevices::colorRampPalette(RColorBrewer::brewer.pal(7, + "PRGn")))(length(theSeq)), + query_colors = NULL, + reference_colors = NULL, + ... +) +} +\arguments{ +\item{cor_stats_layer}{The output of \code{\link[=layer_stat_cor]{layer_stat_cor()}}.} + +\item{query_colors}{named vector of colors for query row annotations} + +\item{reference_colors}{named vector of colors for reference column annotations} +} +\value{ +ComplexHeatmap plot of t-stat correlations +} +\description{ +Visualize the correlation of layer modeling t-statistics with ComplexHeatmap +} +\examples{ +## Obtain the necessary data +## reference human pilot modeling results +if (!exists("modeling_results")) { + modeling_results <- fetch_data(type = "modeling_results") +} + +## querey spatailDLPFC modeling +query_modeling_results <- fetch_data(type = "spatialDLPFC_Visium_modeling_results") + +## extract t-statics and rename +registration_t_stats <- query_modeling_results$enrichment[, grep("^t_stat", colnames(query_modeling_results$enrichment))] +colnames(registration_t_stats) <- gsub("^t_stat_", "", colnames(registration_t_stats)) + +## Compute the correlations +cor_stats_layer <- layer_stat_cor( + stats = registration_t_stats, + modeling_results, + model_type = "enrichment" +) + +## Visualize the correlation matrix + +## most basic +layer_stat_cor_plot_complex(cor_stats_layer) + +## add colors +## add libd_layer_colors to refrence Human Pilot layers +layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) + +## supply polychrome colors to query clusters +cluster_colors <- Polychrome::palette36.colors(nrow(cor_stats_layer)) +names(cluster_colors) <- rownames(cor_stats_layer) + +layer_stat_cor_plot_complex(cor_stats_layer, + query_colors = cluster_colors, + reference_colors = libd_layer_colors) + +## Apply additional ComplexHeatmap param +layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE) + +} From 9028592182a1d1f75c0368f708200f4c0974ffa7 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 10:54:31 -0500 Subject: [PATCH 04/15] Add simplified layer labels as new column instead of modifiing layer_label (this preserves match with refernce data) --- R/annotate_registered_clusters.R | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/R/annotate_registered_clusters.R b/R/annotate_registered_clusters.R index 13e1d5ab..865343c4 100644 --- a/R/annotate_registered_clusters.R +++ b/R/annotate_registered_clusters.R @@ -59,13 +59,6 @@ annotate_registered_clusters <- cutoff_merge_ratio = cutoff_merge_ratio ) - if (all(colnames(cor_stats_layer) %in% c("WM", paste0("Layer", seq_len(6))))) { - ## Simplify names when working with the default data - annotated <- gsub("ayer", "", annotated) - annotated <- gsub("\\/L", "\\/", annotated) - annotated <- gsub("^WM\\/", "WM\\/L", annotated) - } - confidence <- apply(cor_stats_layer, 1, max) > confidence_threshold result <- data.frame( @@ -83,6 +76,16 @@ annotate_registered_clusters <- result$layer_label, ifelse(result$layer_confidence == "good", "", "*") ) + + ## Add simplified label for WM/Layer annotations + if (all(colnames(cor_stats_layer) %in% c("WM", paste0("Layer", seq_len(6))))) { + result$layer_label_simple <- result$layer_label + ## Simplify names when working with the default data + result$layer_label_simple <- gsub("ayer", "", result$layer_label_simple ) + result$layer_label_simple <- gsub("\\/L", "\\/", result$layer_label_simple ) + result$layer_label_simple <- gsub("^WM\\/", "WM\\/L", result$layer_label_simple ) + } + return(result) } From 1bc9eac24c15732f0bb6d8b1d1f40c9784e8a822 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 13:10:22 -0500 Subject: [PATCH 05/15] Add ComplexHeatmap imports --- DESCRIPTION | 3 ++- NAMESPACE | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index a95b2b05..ad66f27f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -79,7 +79,8 @@ Imports: statmod, MatrixGenerics, rlang, - dplyr + dplyr, + ComplexHeatmap RoxygenNote: 7.3.2 Roxygen: list(markdown = TRUE) URL: https://github.com/LieberInstitute/spatialLIBD diff --git a/NAMESPACE b/NAMESPACE index 946c57f9..3fb849d5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -57,6 +57,9 @@ import(plotly, except = last_plot) import(shiny) importFrom(AnnotationHub,query) importFrom(BiocGenerics,which) +importFrom(ComplexHeatmap,Heatmap) +importFrom(ComplexHeatmap,columnAnnotation) +importFrom(ComplexHeatmap,rowAnnotation) importFrom(DT,DTOutput) importFrom(DT,renderDT) importFrom(GenomicRanges,seqnames) From f4e07a3d30c669302338c61abdd2f947da0e29ae Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 13:11:37 -0500 Subject: [PATCH 06/15] Update docs, improve params, add X annotations to heatmap --- R/layer_stat_cor_plot_complex.R | 97 +++++++++++++++++++++++++++--- man/layer_stat_cor_plot_complex.Rd | 56 ++++++++++++++--- 2 files changed, 135 insertions(+), 18 deletions(-) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R index f27c7d27..a07b70c0 100644 --- a/R/layer_stat_cor_plot_complex.R +++ b/R/layer_stat_cor_plot_complex.R @@ -1,13 +1,34 @@ - - #' Visualize the correlation of layer modeling t-statistics with ComplexHeatmap -#' @param query_colors named vector of colors for query row annotations -#' @param reference_colors named vector of colors for reference column annotations +#' +#' This function updates [layer_stat_cor_plot()], using ComplexHeatmap to plot +#' the correlation matrix between a reference and query modeling statistics +#' from [layer_stat_cor()]. Includes functionality to add color annotations, +#' (helpful to match to colors in Visium spot plots), and annotations from +#' [annotate_registered_clusters()]. +#' +#' @param color_max A `numeric(1)` specifying the highest correlation value for +#' the color scale (should be between 0 and 1). +#' @param color_min A `numeric(1)` specifying the lowest correlation value for +#' the color scale (should be between 0 and -1). +#' @param color_scale A `character` vector specifying the color scale for the +#' fill of the heatmap, defaults to classic purple -> green +#' @param query_colors named `character` vector of colors, Adds colors to query +#' row annotations +#' @param reference_colors named `character` vector of colors, Adds colors to +#' reference column annotations +#' @param annotation annotation data.frame output of [annotate_registered_clusters()], +#' adds 'X' for good confidence annotations, '*' for poor confidence #' #' @inheritParams layer_stat_cor_plot #' -#' @return ComplexHeatmap plot of t-stat correlations +#' @return ([Heatmap-class][ComplexHeatmap::Heatmap-class]) plot of t-stat correlations #' @export +#' @author Louise Huuki-Myers +#' @family Layer correlation functions +#' +#' @importFrom RColorBrewer brewer.pal +#' @importFrom grDevices colorRampPalette +#' @importFrom ComplexHeatmap columnAnnotation rowAnnotation Heatmap #' #' @examples #' ## Obtain the necessary data @@ -48,16 +69,33 @@ #' reference_colors = libd_layer_colors) #' #' ## Apply additional ComplexHeatmap param -#' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE) +#' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) #' +#' ## Add annotation +#' annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) +#' layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) +#' +#' ## All together +#' layer_stat_cor_plot_complex(cor_stats_layer, +#' query_colors = cluster_colors, +#' reference_colors = libd_layer_colors, +#' annotation = annotation_df, +#' rect_gp = gpar(col = "black", lwd = 1)) +#' layer_stat_cor_plot_complex <- function(cor_stats_layer, - theSeq = seq(min(cor_stats_layer), max(cor_stats_layer), by = 0.01), - my.col = grDevices::colorRampPalette(RColorBrewer::brewer.pal(7, "PRGn"))(length(theSeq)), + color_max = max(cor_stats_layer), + color_min = min(cor_stats_layer), + color_scale = RColorBrewer::brewer.pal(7, "PRGn"), query_colors = NULL, reference_colors = NULL, + annotation = NULL, ... ){ + ## define color pallet + theSeq = seq(color_min, color_max, by = 0.01) + my.col = grDevices::colorRampPalette(color_scale)(length(theSeq)) + # ## query annotations on row if(!is.null(query_colors)){ @@ -84,16 +122,55 @@ layer_stat_cor_plot_complex <- function(cor_stats_layer, ) } else ref_col_annotation <- NULL - + ## add annotation + if(!is.null(annotation)){ + anno_matrix <- create_annotation_matrix(annotation, cor_stats_layer) + + ## plot heatmap + return( + ComplexHeatmap::Heatmap( + matrix = cor_stats_layer, + col = my.col, + bottom_annotation = ref_col_annotation, + right_annotation = query_row_annotation, + cell_fun = function(j, i, x, y, width, height, fill) { + grid.text(anno_matrix[i, j], x, y, gp = gpar(fontsize = 10)) + }, + ... + )) + } ## plot heatmap + return( ComplexHeatmap::Heatmap( matrix = cor_stats_layer, col = my.col, bottom_annotation = ref_col_annotation, right_annotation = query_row_annotation, ... - ) + )) } + +create_annotation_matrix <- function(annotation_df, cor_stats_layer){ + + anno_list <- lapply(rownames(cor_stats_layer), + function(cluster){ + # look up confidence + confidence <- annotation_df[match(cluster, annotation_df$cluster),"layer_confidence"] + sym <- ifelse(confidence=="good", "X","*") + # match annotations + anno <- annotation_df[match(cluster, annotation_df$cluster),"layer_label"] + return(ifelse(unlist(lapply(colnames(cor_stats_layer), grepl, anno)),sym,"")) + }) + + anno_matrix <- t(data.frame(anno_list)) + rownames(anno_matrix) <- rownames(cor_stats_layer) + colnames(anno_matrix) <- colnames(cor_stats_layer) + + return(anno_matrix) +} + + + diff --git a/man/layer_stat_cor_plot_complex.Rd b/man/layer_stat_cor_plot_complex.Rd index 839d6743..d4e022a0 100644 --- a/man/layer_stat_cor_plot_complex.Rd +++ b/man/layer_stat_cor_plot_complex.Rd @@ -6,26 +6,45 @@ \usage{ layer_stat_cor_plot_complex( cor_stats_layer, - theSeq = seq(min(cor_stats_layer), max(cor_stats_layer), by = 0.01), - my.col = (grDevices::colorRampPalette(RColorBrewer::brewer.pal(7, - "PRGn")))(length(theSeq)), + color_max = max(cor_stats_layer), + color_min = min(cor_stats_layer), + color_scale = RColorBrewer::brewer.pal(7, "PRGn"), query_colors = NULL, reference_colors = NULL, + annotation = NULL, ... ) } \arguments{ \item{cor_stats_layer}{The output of \code{\link[=layer_stat_cor]{layer_stat_cor()}}.} -\item{query_colors}{named vector of colors for query row annotations} +\item{color_max}{A \code{numeric(1)} specifying the highest correlation value for +the color scale (should be between 0 and 1).} -\item{reference_colors}{named vector of colors for reference column annotations} +\item{color_min}{A \code{numeric(1)} specifying the lowest correlation value for +the color scale (should be between 0 and -1).} + +\item{color_scale}{A \code{character} vector specifying the color scale for the +fill of the heatmap, defaults to classic purple -> green} + +\item{query_colors}{named \code{character} vector of colors, Adds colors to query +row annotations} + +\item{reference_colors}{named \code{character} vector of colors, Adds colors to +reference column annotations} + +\item{annotation}{annotation data.frame output of \code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}, +adds 'X' for good confidence annotations, '*' for poor confidence} } \value{ -ComplexHeatmap plot of t-stat correlations +(\link[ComplexHeatmap:Heatmap-class]{Heatmap-class}) plot of t-stat correlations } \description{ -Visualize the correlation of layer modeling t-statistics with ComplexHeatmap +This function updates \code{\link[=layer_stat_cor_plot]{layer_stat_cor_plot()}}, using ComplexHeatmap to plot +the correlation matrix between a reference and query modeling statistics +from \code{\link[=layer_stat_cor]{layer_stat_cor()}}. Includes functionality to add color annotations, +(helpful to match to colors in Visium spot plots), and annotations from +\code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}. } \examples{ ## Obtain the necessary data @@ -66,6 +85,27 @@ layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) ## Apply additional ComplexHeatmap param -layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE) +layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) + +## Add annotation +annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) +layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) +## All together +layer_stat_cor_plot_complex(cor_stats_layer, + query_colors = cluster_colors, + reference_colors = libd_layer_colors, + annotation = annotation_df, + rect_gp = gpar(col = "black", lwd = 1)) + +} +\seealso{ +Other Layer correlation functions: +\code{\link{annotate_registered_clusters}()}, +\code{\link{layer_stat_cor}()}, +\code{\link{layer_stat_cor_plot}()} +} +\author{ +Louise Huuki-Myers } +\concept{Layer correlation functions} From 804add1ef02cfe08be80550778bc2e2c922d3f08 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 13:12:02 -0500 Subject: [PATCH 07/15] link layer_layer_stat_cor_plot --- man/annotate_registered_clusters.Rd | 3 ++- man/layer_stat_cor.Rd | 3 ++- man/layer_stat_cor_plot.Rd | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/man/annotate_registered_clusters.Rd b/man/annotate_registered_clusters.Rd index 41d6245a..9ba61cfb 100644 --- a/man/annotate_registered_clusters.Rd +++ b/man/annotate_registered_clusters.Rd @@ -65,6 +65,7 @@ annotate_registered_clusters(cor_stats_layer, cutoff_merge_ratio = 1) \seealso{ Other Layer correlation functions: \code{\link{layer_stat_cor}()}, -\code{\link{layer_stat_cor_plot}()} +\code{\link{layer_stat_cor_plot}()}, +\code{\link{layer_stat_cor_plot_complex}()} } \concept{Layer correlation functions} diff --git a/man/layer_stat_cor.Rd b/man/layer_stat_cor.Rd index 3801a1d4..b42b45bc 100644 --- a/man/layer_stat_cor.Rd +++ b/man/layer_stat_cor.Rd @@ -86,7 +86,8 @@ summary(layer_stat_cor( \seealso{ Other Layer correlation functions: \code{\link{annotate_registered_clusters}()}, -\code{\link{layer_stat_cor_plot}()} +\code{\link{layer_stat_cor_plot}()}, +\code{\link{layer_stat_cor_plot_complex}()} } \author{ Andrew E Jaffe, Leonardo Collado-Torres diff --git a/man/layer_stat_cor_plot.Rd b/man/layer_stat_cor_plot.Rd index e01991a9..5a9d8189 100644 --- a/man/layer_stat_cor_plot.Rd +++ b/man/layer_stat_cor_plot.Rd @@ -91,7 +91,8 @@ layer_matrix_plot annotate_registered_clusters Other Layer correlation functions: \code{\link{annotate_registered_clusters}()}, -\code{\link{layer_stat_cor}()} +\code{\link{layer_stat_cor}()}, +\code{\link{layer_stat_cor_plot_complex}()} } \author{ Andrew E Jaffe, Leonardo Collado-Torres From d3278485f5245a1a8ea89cfaf5cd75191b25f7a1 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 13:51:18 -0500 Subject: [PATCH 08/15] Write out colors instead of using Polychrome --- R/layer_stat_cor_plot_complex.R | 2 +- man/layer_stat_cor_plot_complex.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R index a07b70c0..e3e9cc28 100644 --- a/R/layer_stat_cor_plot_complex.R +++ b/R/layer_stat_cor_plot_complex.R @@ -61,7 +61,7 @@ #' layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) #' #' ## supply polychrome colors to query clusters -#' cluster_colors <- Polychrome::palette36.colors(nrow(cor_stats_layer)) +#' cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') #' names(cluster_colors) <- rownames(cor_stats_layer) #' #' layer_stat_cor_plot_complex(cor_stats_layer, diff --git a/man/layer_stat_cor_plot_complex.Rd b/man/layer_stat_cor_plot_complex.Rd index d4e022a0..109a5056 100644 --- a/man/layer_stat_cor_plot_complex.Rd +++ b/man/layer_stat_cor_plot_complex.Rd @@ -77,7 +77,7 @@ layer_stat_cor_plot_complex(cor_stats_layer) layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) ## supply polychrome colors to query clusters -cluster_colors <- Polychrome::palette36.colors(nrow(cor_stats_layer)) +cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') names(cluster_colors) <- rownames(cor_stats_layer) layer_stat_cor_plot_complex(cor_stats_layer, From 7447f6148819e12b93fb6327508024f9c56c39cf Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 14:31:51 -0500 Subject: [PATCH 09/15] Add missing documentaion for ... --- R/layer_stat_cor_plot_complex.R | 1 + man/layer_stat_cor_plot_complex.Rd | 2 ++ 2 files changed, 3 insertions(+) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R index e3e9cc28..9326b7fb 100644 --- a/R/layer_stat_cor_plot_complex.R +++ b/R/layer_stat_cor_plot_complex.R @@ -18,6 +18,7 @@ #' reference column annotations #' @param annotation annotation data.frame output of [annotate_registered_clusters()], #' adds 'X' for good confidence annotations, '*' for poor confidence +#' @param ... Additinal parameters passed to [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()] #' #' @inheritParams layer_stat_cor_plot #' diff --git a/man/layer_stat_cor_plot_complex.Rd b/man/layer_stat_cor_plot_complex.Rd index 109a5056..1df67a69 100644 --- a/man/layer_stat_cor_plot_complex.Rd +++ b/man/layer_stat_cor_plot_complex.Rd @@ -35,6 +35,8 @@ reference column annotations} \item{annotation}{annotation data.frame output of \code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}, adds 'X' for good confidence annotations, '*' for poor confidence} + +\item{...}{Additinal parameters passed to \code{\link[ComplexHeatmap:Heatmap]{ComplexHeatmap::Heatmap()}}} } \value{ (\link[ComplexHeatmap:Heatmap-class]{Heatmap-class}) plot of t-stat correlations From f2c9742b5f7fdb08a0027fb66e4e69efd1350a2f Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 14:57:43 -0500 Subject: [PATCH 10/15] rm gpar example --- R/layer_stat_cor_plot_complex.R | 5 +++-- man/layer_stat_cor_plot_complex.Rd | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R index 9326b7fb..95472725 100644 --- a/R/layer_stat_cor_plot_complex.R +++ b/R/layer_stat_cor_plot_complex.R @@ -80,8 +80,9 @@ #' layer_stat_cor_plot_complex(cor_stats_layer, #' query_colors = cluster_colors, #' reference_colors = libd_layer_colors, -#' annotation = annotation_df, -#' rect_gp = gpar(col = "black", lwd = 1)) +#' annotation = annotation_df, +#' cluster_rows = FALSE, +#' cluster_columns = FALSE) #' layer_stat_cor_plot_complex <- function(cor_stats_layer, color_max = max(cor_stats_layer), diff --git a/man/layer_stat_cor_plot_complex.Rd b/man/layer_stat_cor_plot_complex.Rd index 1df67a69..b85903f6 100644 --- a/man/layer_stat_cor_plot_complex.Rd +++ b/man/layer_stat_cor_plot_complex.Rd @@ -97,8 +97,9 @@ layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) layer_stat_cor_plot_complex(cor_stats_layer, query_colors = cluster_colors, reference_colors = libd_layer_colors, - annotation = annotation_df, - rect_gp = gpar(col = "black", lwd = 1)) + annotation = annotation_df, + cluster_rows = FALSE, + cluster_columns = FALSE) } \seealso{ From 51aa0c819faeaa7e5c063f8b999ec37668878586 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Wed, 11 Dec 2024 15:45:57 -0500 Subject: [PATCH 11/15] Add 'Cor' as title to color scale --- R/layer_stat_cor_plot_complex.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R index 95472725..9aa509ba 100644 --- a/R/layer_stat_cor_plot_complex.R +++ b/R/layer_stat_cor_plot_complex.R @@ -133,6 +133,7 @@ layer_stat_cor_plot_complex <- function(cor_stats_layer, ComplexHeatmap::Heatmap( matrix = cor_stats_layer, col = my.col, + name = "Cor", bottom_annotation = ref_col_annotation, right_annotation = query_row_annotation, cell_fun = function(j, i, x, y, width, height, fill) { @@ -147,6 +148,7 @@ layer_stat_cor_plot_complex <- function(cor_stats_layer, ComplexHeatmap::Heatmap( matrix = cor_stats_layer, col = my.col, + name = "Cor", bottom_annotation = ref_col_annotation, right_annotation = query_row_annotation, ... From 8237d45f6f5753cdad1a064aca10b19e196fe79a Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Thu, 12 Dec 2024 10:49:06 -0500 Subject: [PATCH 12/15] Improve docs --- R/layer_stat_cor_plot_complex.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R index 9aa509ba..d7cfdbc9 100644 --- a/R/layer_stat_cor_plot_complex.R +++ b/R/layer_stat_cor_plot_complex.R @@ -6,6 +6,7 @@ #' (helpful to match to colors in Visium spot plots), and annotations from #' [annotate_registered_clusters()]. #' +#' @param cor_stats_layer The output of [layer_stat_cor()]. #' @param color_max A `numeric(1)` specifying the highest correlation value for #' the color scale (should be between 0 and 1). #' @param color_min A `numeric(1)` specifying the lowest correlation value for @@ -18,9 +19,9 @@ #' reference column annotations #' @param annotation annotation data.frame output of [annotate_registered_clusters()], #' adds 'X' for good confidence annotations, '*' for poor confidence -#' @param ... Additinal parameters passed to [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()] +#' @param ... Additional parameters passed to [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()] +#' ex. `cluster_rows` and `cluster_columns`. #' -#' @inheritParams layer_stat_cor_plot #' #' @return ([Heatmap-class][ComplexHeatmap::Heatmap-class]) plot of t-stat correlations #' @export From 8d6ed2626dd4f52d3ce737556544a31168afc498 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Thu, 12 Dec 2024 10:50:30 -0500 Subject: [PATCH 13/15] Replace layer_stat_cor_plot with layer_stat_cor_complex --- R/layer_stat_cor_plot.R | 255 ++++++++++++++++++++++++---------------- 1 file changed, 155 insertions(+), 100 deletions(-) diff --git a/R/layer_stat_cor_plot.R b/R/layer_stat_cor_plot.R index a293fb66..1846a61f 100644 --- a/R/layer_stat_cor_plot.R +++ b/R/layer_stat_cor_plot.R @@ -1,126 +1,181 @@ -#' Visualize the correlation of layer modeling t-statistics -#' -#' This function makes a heatmap from the [layer_stat_cor()] correlation matrix -#' between a given set of cell cluster/type statistics derived from scRNA-seq -#' or snRNA-seq data (among other types) and the layer statistics from the -#' Human DLPFC Visium data (when using the default arguments). -#' +#' Visualize the correlation of layer modeling t-statistics with ComplexHeatmap +#' +#' This function updates [layer_stat_cor_plot()], using ComplexHeatmap to plot +#' the correlation matrix between a reference and query modeling statistics +#' from [layer_stat_cor()]. Includes functionality to add color annotations, +#' (helpful to match to colors in Visium spot plots), and annotations from +#' [annotate_registered_clusters()]. +#' #' @param cor_stats_layer The output of [layer_stat_cor()]. -#' @param max A `numeric(1)` specifying the highest correlation value for the -#' color scale (should be between 0 and 1). -#' @param min A `numeric(1)` specifying the lowest correlation value for the -#' color scale (should be between 0 and -1). -#' @param layerHeights A `numeric()` vector of length equal to -#' `ncol(cor_stats_layer) + 1` that starts at 0 specifying where -#' to plot the y-axis breaks which can be used for re-creating the length of -#' each brain layer. Gets passed to [layer_matrix_plot()]. -#' @param cex Passed to [layer_matrix_plot()]. +#' @param color_max A `numeric(1)` specifying the highest correlation value for +#' the color scale (should be between 0 and 1). +#' @param color_min A `numeric(1)` specifying the lowest correlation value for +#' the color scale (should be between 0 and -1). +#' @param color_scale A `character` vector specifying the color scale for the +#' fill of the heatmap, defaults to classic purple -> green +#' @param query_colors named `character` vector of colors, Adds colors to query +#' row annotations +#' @param reference_colors named `character` vector of colors, Adds colors to +#' reference column annotations +#' @param annotation annotation data.frame output of [annotate_registered_clusters()], +#' adds 'X' for good confidence annotations, '*' for poor confidence +#' @param ... Additional parameters passed to [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()] +#' ex. `cluster_rows` and `cluster_columns`. #' -#' @return A heatmap for the correlation matrix between statistics. +#' +#' @return ([Heatmap-class][ComplexHeatmap::Heatmap-class]) plot of t-stat correlations #' @export -#' @author Andrew E Jaffe, Leonardo Collado-Torres +#' @author Louise Huuki-Myers #' @family Layer correlation functions -#' @seealso layer_matrix_plot annotate_registered_clusters #' #' @importFrom RColorBrewer brewer.pal #' @importFrom grDevices colorRampPalette -#' @details Check -#' https://github.com/LieberInstitute/HumanPilot/blob/master/Analysis/Layer_Guesses/dlpfc_snRNAseq_annotation.R -#' for a full analysis from which this family of functions is derived from. +#' @importFrom ComplexHeatmap columnAnnotation rowAnnotation Heatmap #' #' @examples -#' #' ## Obtain the necessary data +#' ## reference human pilot modeling results #' if (!exists("modeling_results")) { #' modeling_results <- fetch_data(type = "modeling_results") #' } +#' +#' ## querey spatailDLPFC modeling +#' query_modeling_results <- fetch_data(type = "spatialDLPFC_Visium_modeling_results") +#' +#' ## extract t-statics and rename +#' registration_t_stats <- query_modeling_results$enrichment[, grep("^t_stat", colnames(query_modeling_results$enrichment))] +#' colnames(registration_t_stats) <- gsub("^t_stat_", "", colnames(registration_t_stats)) #' #' ## Compute the correlations #' cor_stats_layer <- layer_stat_cor( -#' tstats_Human_DLPFC_snRNAseq_Nguyen_topLayer, +#' stats = registration_t_stats, #' modeling_results, #' model_type = "enrichment" #' ) #' #' ## Visualize the correlation matrix -#' layer_stat_cor_plot(cor_stats_layer, max = max(cor_stats_layer)) -#' -#' ## Annotate then re-plot -#' rownames(cor_stats_layer) <- paste0( -#' rownames(cor_stats_layer), -#' " - ", -#' annotate_registered_clusters(cor_stats_layer)$layer_label -#' ) -#' layer_stat_cor_plot(cor_stats_layer, max = max(cor_stats_layer)) -#' -#' ## Restrict the range of colors further -#' layer_stat_cor_plot(cor_stats_layer, max = 0.25) +#' +#' ## most basic +#' layer_stat_cor_plot_complex(cor_stats_layer) +#' +#' ## add colors +#' ## add libd_layer_colors to refrence Human Pilot layers +#' layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) +#' +#' ## supply polychrome colors to query clusters +#' cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') +#' names(cluster_colors) <- rownames(cor_stats_layer) #' -#' ## Repeat with just the top 10 layer marker genes -#' layer_stat_cor_plot(layer_stat_cor( -#' tstats_Human_DLPFC_snRNAseq_Nguyen_topLayer, -#' modeling_results, -#' model_type = "enrichment", -#' top_n = 10 -#' ), max = 0.25) -#' -#' ## Now with the "pairwise" modeling results and also top_n = 10 -#' layer_stat_cor_plot(layer_stat_cor( -#' tstats_Human_DLPFC_snRNAseq_Nguyen_topLayer, -#' modeling_results, -#' model_type = "pairwise", -#' top_n = 10 -#' ), max = 0.25) -layer_stat_cor_plot <- - function( - cor_stats_layer, - max = 0.81, - min = -max, - layerHeights = NULL, - cex = 1.2) { - ## From https://github.com/LieberInstitute/HumanPilot/blob/master/Analysis/Layer_Guesses/dlpfc_snRNAseq_annotation.R - theSeq <- seq(min, max, by = 0.01) - my.col <- grDevices::colorRampPalette(RColorBrewer::brewer.pal(7, "PRGn"))(length(theSeq)) - - ## Subset values - cor_stats_layer[cor_stats_layer <= min] <- min - cor_stats_layer[cor_stats_layer >= max] <- max - - ## Re-shape the matrix - mat_vals <- t(cor_stats_layer) - - ## Re-order and shorten names if they match our data - if (all(rownames(mat_vals) %in% c("WM", paste0("Layer", seq_len(6))))) { - rownames(mat_vals) <- gsub("ayer", "", rownames(mat_vals)) - mat_vals <- mat_vals[c("WM", paste0("L", rev(seq_len(6)))), , drop = FALSE] +#' layer_stat_cor_plot_complex(cor_stats_layer, +#' query_colors = cluster_colors, +#' reference_colors = libd_layer_colors) +#' +#' ## Apply additional ComplexHeatmap param +#' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) +#' +#' ## Add annotation +#' annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) +#' layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) +#' +#' ## All together +#' layer_stat_cor_plot_complex(cor_stats_layer, +#' query_colors = cluster_colors, +#' reference_colors = libd_layer_colors, +#' annotation = annotation_df, +#' cluster_rows = FALSE, +#' cluster_columns = FALSE) +#' +layer_stat_cor_plot <- function(cor_stats_layer, + color_max = max(cor_stats_layer), + color_min = min(cor_stats_layer), + color_scale = RColorBrewer::brewer.pal(7, "PRGn"), + query_colors = NULL, + reference_colors = NULL, + annotation = NULL, + ... +){ + + ## define color pallet + theSeq = seq(color_min, color_max, by = 0.01) + my.col = grDevices::colorRampPalette(color_scale)(length(theSeq)) + + # ## query annotations on row + if(!is.null(query_colors)){ + + stopifnot(all(rownames(cor_stats_layer) %in% names(query_colors))) + query_colors <- query_colors[rownames(cor_stats_layer)] + + + query_row_annotation <- ComplexHeatmap::rowAnnotation( + " " = rownames(cor_stats_layer), + col = list(" " = query_colors), + show_legend = FALSE) + + } else query_row_annotation <- NULL + + ## reference annotation on bottom + if(!is.null(reference_colors)){ + stopifnot(all(colnames(cor_stats_layer) %in% names(reference_colors))) + reference_colors <- reference_colors[colnames(cor_stats_layer)] + + ref_col_annotation <- ComplexHeatmap::columnAnnotation( + " " = colnames(cor_stats_layer), + col = list(" " = reference_colors), + show_legend = FALSE + ) + } else ref_col_annotation <- NULL + + ## add annotation + if(!is.null(annotation)){ + anno_matrix <- create_annotation_matrix(annotation, cor_stats_layer) + + ## plot heatmap + return( + ComplexHeatmap::Heatmap( + matrix = cor_stats_layer, + col = my.col, + name = "Cor", + bottom_annotation = ref_col_annotation, + right_annotation = query_row_annotation, + cell_fun = function(j, i, x, y, width, height, fill) { + grid.text(anno_matrix[i, j], x, y, gp = gpar(fontsize = 10)) + }, + ... + )) + } + + ## plot heatmap + return( + ComplexHeatmap::Heatmap( + matrix = cor_stats_layer, + col = my.col, + name = "Cor", + bottom_annotation = ref_col_annotation, + right_annotation = query_row_annotation, + ... + )) + + +} - ## Use our default layer heights also - if (is.null(layerHeights)) { - layerHeights <- c(0, 40, 55, 75, 85, 110, 120, 135) - } - } +create_annotation_matrix <- function(annotation_df, cor_stats_layer){ + + anno_list <- lapply(rownames(cor_stats_layer), + function(cluster){ + # look up confidence + confidence <- annotation_df[match(cluster, annotation_df$cluster),"layer_confidence"] + sym <- ifelse(confidence=="good", "X","*") + # match annotations + anno <- annotation_df[match(cluster, annotation_df$cluster),"layer_label"] + return(ifelse(unlist(lapply(colnames(cor_stats_layer), grepl, anno)),sym,"")) + }) + + anno_matrix <- t(data.frame(anno_list)) + rownames(anno_matrix) <- rownames(cor_stats_layer) + colnames(anno_matrix) <- colnames(cor_stats_layer) + + return(anno_matrix) +} - ## From fields:::imagePlotInfo - midpoints <- seq(min, max, length.out = length(my.col)) - delta <- (midpoints[2] - midpoints[1]) / 2 - breaks <- c(midpoints[1] - delta, midpoints + delta) - legend_cuts <- seq(-1, 1, by = 0.1) - legend_cuts <- legend_cuts[legend_cuts >= min & legend_cuts <= max] - axis.args <- list( - at = legend_cuts, - labels = legend_cuts - ) - layer_matrix_plot( - matrix_values = mat_vals, - matrix_labels = NULL, - xlabs = NULL, - layerHeights = layerHeights, - mypal = my.col, - breaks = breaks, - axis.args = axis.args, - srt = 90, - cex = cex - ) - } From f6b74cd03144e6e0e240d200320478368507ed67 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Thu, 12 Dec 2024 10:53:49 -0500 Subject: [PATCH 14/15] Update docs for layer_stat_cor_plot --- R/layer_stat_cor_plot.R | 27 +++++---- man/layer_stat_cor_plot.Rd | 119 +++++++++++++++++++++---------------- 2 files changed, 82 insertions(+), 64 deletions(-) diff --git a/R/layer_stat_cor_plot.R b/R/layer_stat_cor_plot.R index 1846a61f..df432d8a 100644 --- a/R/layer_stat_cor_plot.R +++ b/R/layer_stat_cor_plot.R @@ -1,8 +1,9 @@ #' Visualize the correlation of layer modeling t-statistics with ComplexHeatmap #' -#' This function updates [layer_stat_cor_plot()], using ComplexHeatmap to plot -#' the correlation matrix between a reference and query modeling statistics -#' from [layer_stat_cor()]. Includes functionality to add color annotations, +#' Use ComplexHeatmap to plot the correlation matrix between a reference and +#' query modeling statistics from [layer_stat_cor()]. +#' +#' Includes functionality to add color annotations, #' (helpful to match to colors in Visium spot plots), and annotations from #' [annotate_registered_clusters()]. #' @@ -12,13 +13,13 @@ #' @param color_min A `numeric(1)` specifying the lowest correlation value for #' the color scale (should be between 0 and -1). #' @param color_scale A `character` vector specifying the color scale for the -#' fill of the heatmap, defaults to classic purple -> green +#' fill of the heatmap, defaults to classic purple -> green. #' @param query_colors named `character` vector of colors, Adds colors to query -#' row annotations +#' row annotations. #' @param reference_colors named `character` vector of colors, Adds colors to -#' reference column annotations +#' reference column annotations. #' @param annotation annotation data.frame output of [annotate_registered_clusters()], -#' adds 'X' for good confidence annotations, '*' for poor confidence +#' adds 'X' for good confidence annotations, '*' for poor confidence. #' @param ... Additional parameters passed to [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()] #' ex. `cluster_rows` and `cluster_columns`. #' @@ -56,29 +57,29 @@ #' ## Visualize the correlation matrix #' #' ## most basic -#' layer_stat_cor_plot_complex(cor_stats_layer) +#' layer_stat_cor_plot(cor_stats_layer) #' #' ## add colors #' ## add libd_layer_colors to refrence Human Pilot layers -#' layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) +#' layer_stat_cor_plot(cor_stats_layer, reference_colors = libd_layer_colors) #' #' ## supply polychrome colors to query clusters #' cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') #' names(cluster_colors) <- rownames(cor_stats_layer) #' -#' layer_stat_cor_plot_complex(cor_stats_layer, +#' layer_stat_cor_plot(cor_stats_layer, #' query_colors = cluster_colors, #' reference_colors = libd_layer_colors) #' #' ## Apply additional ComplexHeatmap param -#' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) +#' layer_stat_cor_plot(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) #' #' ## Add annotation #' annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) -#' layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) +#' layer_stat_cor_plot(cor_stats_layer, annotation = annotation_df) #' #' ## All together -#' layer_stat_cor_plot_complex(cor_stats_layer, +#' layer_stat_cor_plot(cor_stats_layer, #' query_colors = cluster_colors, #' reference_colors = libd_layer_colors, #' annotation = annotation_df, diff --git a/man/layer_stat_cor_plot.Rd b/man/layer_stat_cor_plot.Rd index 5a9d8189..482233a5 100644 --- a/man/layer_stat_cor_plot.Rd +++ b/man/layer_stat_cor_plot.Rd @@ -2,99 +2,116 @@ % Please edit documentation in R/layer_stat_cor_plot.R \name{layer_stat_cor_plot} \alias{layer_stat_cor_plot} -\title{Visualize the correlation of layer modeling t-statistics} +\title{Visualize the correlation of layer modeling t-statistics with ComplexHeatmap} \usage{ layer_stat_cor_plot( cor_stats_layer, - max = 0.81, - min = -max, - layerHeights = NULL, - cex = 1.2 + color_max = max(cor_stats_layer), + color_min = min(cor_stats_layer), + color_scale = RColorBrewer::brewer.pal(7, "PRGn"), + query_colors = NULL, + reference_colors = NULL, + annotation = NULL, + ... ) } \arguments{ \item{cor_stats_layer}{The output of \code{\link[=layer_stat_cor]{layer_stat_cor()}}.} -\item{max}{A \code{numeric(1)} specifying the highest correlation value for the -color scale (should be between 0 and 1).} +\item{color_max}{A \code{numeric(1)} specifying the highest correlation value for +the color scale (should be between 0 and 1).} -\item{min}{A \code{numeric(1)} specifying the lowest correlation value for the -color scale (should be between 0 and -1).} +\item{color_min}{A \code{numeric(1)} specifying the lowest correlation value for +the color scale (should be between 0 and -1).} -\item{layerHeights}{A \code{numeric()} vector of length equal to -\code{ncol(cor_stats_layer) + 1} that starts at 0 specifying where -to plot the y-axis breaks which can be used for re-creating the length of -each brain layer. Gets passed to \code{\link[=layer_matrix_plot]{layer_matrix_plot()}}.} +\item{color_scale}{A \code{character} vector specifying the color scale for the +fill of the heatmap, defaults to classic purple -> green.} -\item{cex}{Passed to \code{\link[=layer_matrix_plot]{layer_matrix_plot()}}.} +\item{query_colors}{named \code{character} vector of colors, Adds colors to query +row annotations.} + +\item{reference_colors}{named \code{character} vector of colors, Adds colors to +reference column annotations.} + +\item{annotation}{annotation data.frame output of \code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}, +adds 'X' for good confidence annotations, '*' for poor confidence.} + +\item{...}{Additional parameters passed to \code{\link[ComplexHeatmap:Heatmap]{ComplexHeatmap::Heatmap()}} +ex. \code{cluster_rows} and \code{cluster_columns}.} } \value{ -A heatmap for the correlation matrix between statistics. +(\link[ComplexHeatmap:Heatmap-class]{Heatmap-class}) plot of t-stat correlations } \description{ -This function makes a heatmap from the \code{\link[=layer_stat_cor]{layer_stat_cor()}} correlation matrix -between a given set of cell cluster/type statistics derived from scRNA-seq -or snRNA-seq data (among other types) and the layer statistics from the -Human DLPFC Visium data (when using the default arguments). +Use ComplexHeatmap to plot the correlation matrix between a reference and +query modeling statistics from \code{\link[=layer_stat_cor]{layer_stat_cor()}}. } \details{ -Check -https://github.com/LieberInstitute/HumanPilot/blob/master/Analysis/Layer_Guesses/dlpfc_snRNAseq_annotation.R -for a full analysis from which this family of functions is derived from. +Includes functionality to add color annotations, +(helpful to match to colors in Visium spot plots), and annotations from +\code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}. } \examples{ - ## Obtain the necessary data +## reference human pilot modeling results if (!exists("modeling_results")) { modeling_results <- fetch_data(type = "modeling_results") } +## querey spatailDLPFC modeling +query_modeling_results <- fetch_data(type = "spatialDLPFC_Visium_modeling_results") + +## extract t-statics and rename +registration_t_stats <- query_modeling_results$enrichment[, grep("^t_stat", colnames(query_modeling_results$enrichment))] +colnames(registration_t_stats) <- gsub("^t_stat_", "", colnames(registration_t_stats)) + ## Compute the correlations cor_stats_layer <- layer_stat_cor( - tstats_Human_DLPFC_snRNAseq_Nguyen_topLayer, + stats = registration_t_stats, modeling_results, model_type = "enrichment" ) ## Visualize the correlation matrix -layer_stat_cor_plot(cor_stats_layer, max = max(cor_stats_layer)) -## Annotate then re-plot -rownames(cor_stats_layer) <- paste0( - rownames(cor_stats_layer), - " - ", - annotate_registered_clusters(cor_stats_layer)$layer_label -) -layer_stat_cor_plot(cor_stats_layer, max = max(cor_stats_layer)) +## most basic +layer_stat_cor_plot(cor_stats_layer) -## Restrict the range of colors further -layer_stat_cor_plot(cor_stats_layer, max = 0.25) +## add colors +## add libd_layer_colors to refrence Human Pilot layers +layer_stat_cor_plot(cor_stats_layer, reference_colors = libd_layer_colors) -## Repeat with just the top 10 layer marker genes -layer_stat_cor_plot(layer_stat_cor( - tstats_Human_DLPFC_snRNAseq_Nguyen_topLayer, - modeling_results, - model_type = "enrichment", - top_n = 10 -), max = 0.25) +## supply polychrome colors to query clusters +cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') +names(cluster_colors) <- rownames(cor_stats_layer) -## Now with the "pairwise" modeling results and also top_n = 10 -layer_stat_cor_plot(layer_stat_cor( - tstats_Human_DLPFC_snRNAseq_Nguyen_topLayer, - modeling_results, - model_type = "pairwise", - top_n = 10 -), max = 0.25) +layer_stat_cor_plot(cor_stats_layer, + query_colors = cluster_colors, + reference_colors = libd_layer_colors) + +## Apply additional ComplexHeatmap param +layer_stat_cor_plot(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) + +## Add annotation +annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) +layer_stat_cor_plot(cor_stats_layer, annotation = annotation_df) + +## All together +layer_stat_cor_plot(cor_stats_layer, + query_colors = cluster_colors, + reference_colors = libd_layer_colors, + annotation = annotation_df, + cluster_rows = FALSE, + cluster_columns = FALSE) + } \seealso{ -layer_matrix_plot annotate_registered_clusters - Other Layer correlation functions: \code{\link{annotate_registered_clusters}()}, \code{\link{layer_stat_cor}()}, \code{\link{layer_stat_cor_plot_complex}()} } \author{ -Andrew E Jaffe, Leonardo Collado-Torres +Louise Huuki-Myers } \concept{Layer correlation functions} From 445a62cbb0b0f782c3426363454b0b3e85db577f Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Thu, 12 Dec 2024 10:54:40 -0500 Subject: [PATCH 15/15] rm layer_stat_cor_complex.R --- NAMESPACE | 1 - R/layer_stat_cor_plot_complex.R | 181 ---------------------------- man/annotate_registered_clusters.Rd | 3 +- man/layer_stat_cor.Rd | 3 +- man/layer_stat_cor_plot.Rd | 3 +- man/layer_stat_cor_plot_complex.Rd | 114 ------------------ 6 files changed, 3 insertions(+), 302 deletions(-) delete mode 100644 R/layer_stat_cor_plot_complex.R delete mode 100644 man/layer_stat_cor_plot_complex.Rd diff --git a/NAMESPACE b/NAMESPACE index 3fb849d5..c85f884a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,7 +25,6 @@ export(layer_boxplot) export(layer_matrix_plot) export(layer_stat_cor) export(layer_stat_cor_plot) -export(layer_stat_cor_plot_complex) export(locate_images) export(read10xVisiumAnalysis) export(read10xVisiumWrapper) diff --git a/R/layer_stat_cor_plot_complex.R b/R/layer_stat_cor_plot_complex.R deleted file mode 100644 index d7cfdbc9..00000000 --- a/R/layer_stat_cor_plot_complex.R +++ /dev/null @@ -1,181 +0,0 @@ -#' Visualize the correlation of layer modeling t-statistics with ComplexHeatmap -#' -#' This function updates [layer_stat_cor_plot()], using ComplexHeatmap to plot -#' the correlation matrix between a reference and query modeling statistics -#' from [layer_stat_cor()]. Includes functionality to add color annotations, -#' (helpful to match to colors in Visium spot plots), and annotations from -#' [annotate_registered_clusters()]. -#' -#' @param cor_stats_layer The output of [layer_stat_cor()]. -#' @param color_max A `numeric(1)` specifying the highest correlation value for -#' the color scale (should be between 0 and 1). -#' @param color_min A `numeric(1)` specifying the lowest correlation value for -#' the color scale (should be between 0 and -1). -#' @param color_scale A `character` vector specifying the color scale for the -#' fill of the heatmap, defaults to classic purple -> green -#' @param query_colors named `character` vector of colors, Adds colors to query -#' row annotations -#' @param reference_colors named `character` vector of colors, Adds colors to -#' reference column annotations -#' @param annotation annotation data.frame output of [annotate_registered_clusters()], -#' adds 'X' for good confidence annotations, '*' for poor confidence -#' @param ... Additional parameters passed to [ComplexHeatmap::Heatmap()][ComplexHeatmap::Heatmap()] -#' ex. `cluster_rows` and `cluster_columns`. -#' -#' -#' @return ([Heatmap-class][ComplexHeatmap::Heatmap-class]) plot of t-stat correlations -#' @export -#' @author Louise Huuki-Myers -#' @family Layer correlation functions -#' -#' @importFrom RColorBrewer brewer.pal -#' @importFrom grDevices colorRampPalette -#' @importFrom ComplexHeatmap columnAnnotation rowAnnotation Heatmap -#' -#' @examples -#' ## Obtain the necessary data -#' ## reference human pilot modeling results -#' if (!exists("modeling_results")) { -#' modeling_results <- fetch_data(type = "modeling_results") -#' } -#' -#' ## querey spatailDLPFC modeling -#' query_modeling_results <- fetch_data(type = "spatialDLPFC_Visium_modeling_results") -#' -#' ## extract t-statics and rename -#' registration_t_stats <- query_modeling_results$enrichment[, grep("^t_stat", colnames(query_modeling_results$enrichment))] -#' colnames(registration_t_stats) <- gsub("^t_stat_", "", colnames(registration_t_stats)) -#' -#' ## Compute the correlations -#' cor_stats_layer <- layer_stat_cor( -#' stats = registration_t_stats, -#' modeling_results, -#' model_type = "enrichment" -#' ) -#' -#' ## Visualize the correlation matrix -#' -#' ## most basic -#' layer_stat_cor_plot_complex(cor_stats_layer) -#' -#' ## add colors -#' ## add libd_layer_colors to refrence Human Pilot layers -#' layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) -#' -#' ## supply polychrome colors to query clusters -#' cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') -#' names(cluster_colors) <- rownames(cor_stats_layer) -#' -#' layer_stat_cor_plot_complex(cor_stats_layer, -#' query_colors = cluster_colors, -#' reference_colors = libd_layer_colors) -#' -#' ## Apply additional ComplexHeatmap param -#' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) -#' -#' ## Add annotation -#' annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) -#' layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) -#' -#' ## All together -#' layer_stat_cor_plot_complex(cor_stats_layer, -#' query_colors = cluster_colors, -#' reference_colors = libd_layer_colors, -#' annotation = annotation_df, -#' cluster_rows = FALSE, -#' cluster_columns = FALSE) -#' -layer_stat_cor_plot_complex <- function(cor_stats_layer, - color_max = max(cor_stats_layer), - color_min = min(cor_stats_layer), - color_scale = RColorBrewer::brewer.pal(7, "PRGn"), - query_colors = NULL, - reference_colors = NULL, - annotation = NULL, - ... - ){ - - ## define color pallet - theSeq = seq(color_min, color_max, by = 0.01) - my.col = grDevices::colorRampPalette(color_scale)(length(theSeq)) - - # ## query annotations on row - if(!is.null(query_colors)){ - - stopifnot(all(rownames(cor_stats_layer) %in% names(query_colors))) - query_colors <- query_colors[rownames(cor_stats_layer)] - - - query_row_annotation <- ComplexHeatmap::rowAnnotation( - " " = rownames(cor_stats_layer), - col = list(" " = query_colors), - show_legend = FALSE) - - } else query_row_annotation <- NULL - - ## reference annotation on bottom - if(!is.null(reference_colors)){ - stopifnot(all(colnames(cor_stats_layer) %in% names(reference_colors))) - reference_colors <- reference_colors[colnames(cor_stats_layer)] - - ref_col_annotation <- ComplexHeatmap::columnAnnotation( - " " = colnames(cor_stats_layer), - col = list(" " = reference_colors), - show_legend = FALSE - ) - } else ref_col_annotation <- NULL - - ## add annotation - if(!is.null(annotation)){ - anno_matrix <- create_annotation_matrix(annotation, cor_stats_layer) - - ## plot heatmap - return( - ComplexHeatmap::Heatmap( - matrix = cor_stats_layer, - col = my.col, - name = "Cor", - bottom_annotation = ref_col_annotation, - right_annotation = query_row_annotation, - cell_fun = function(j, i, x, y, width, height, fill) { - grid.text(anno_matrix[i, j], x, y, gp = gpar(fontsize = 10)) - }, - ... - )) - } - - ## plot heatmap - return( - ComplexHeatmap::Heatmap( - matrix = cor_stats_layer, - col = my.col, - name = "Cor", - bottom_annotation = ref_col_annotation, - right_annotation = query_row_annotation, - ... - )) - - -} - -create_annotation_matrix <- function(annotation_df, cor_stats_layer){ - - anno_list <- lapply(rownames(cor_stats_layer), - function(cluster){ - # look up confidence - confidence <- annotation_df[match(cluster, annotation_df$cluster),"layer_confidence"] - sym <- ifelse(confidence=="good", "X","*") - # match annotations - anno <- annotation_df[match(cluster, annotation_df$cluster),"layer_label"] - return(ifelse(unlist(lapply(colnames(cor_stats_layer), grepl, anno)),sym,"")) - }) - - anno_matrix <- t(data.frame(anno_list)) - rownames(anno_matrix) <- rownames(cor_stats_layer) - colnames(anno_matrix) <- colnames(cor_stats_layer) - - return(anno_matrix) -} - - - diff --git a/man/annotate_registered_clusters.Rd b/man/annotate_registered_clusters.Rd index 9ba61cfb..41d6245a 100644 --- a/man/annotate_registered_clusters.Rd +++ b/man/annotate_registered_clusters.Rd @@ -65,7 +65,6 @@ annotate_registered_clusters(cor_stats_layer, cutoff_merge_ratio = 1) \seealso{ Other Layer correlation functions: \code{\link{layer_stat_cor}()}, -\code{\link{layer_stat_cor_plot}()}, -\code{\link{layer_stat_cor_plot_complex}()} +\code{\link{layer_stat_cor_plot}()} } \concept{Layer correlation functions} diff --git a/man/layer_stat_cor.Rd b/man/layer_stat_cor.Rd index b42b45bc..3801a1d4 100644 --- a/man/layer_stat_cor.Rd +++ b/man/layer_stat_cor.Rd @@ -86,8 +86,7 @@ summary(layer_stat_cor( \seealso{ Other Layer correlation functions: \code{\link{annotate_registered_clusters}()}, -\code{\link{layer_stat_cor_plot}()}, -\code{\link{layer_stat_cor_plot_complex}()} +\code{\link{layer_stat_cor_plot}()} } \author{ Andrew E Jaffe, Leonardo Collado-Torres diff --git a/man/layer_stat_cor_plot.Rd b/man/layer_stat_cor_plot.Rd index 482233a5..8eccd53d 100644 --- a/man/layer_stat_cor_plot.Rd +++ b/man/layer_stat_cor_plot.Rd @@ -108,8 +108,7 @@ layer_stat_cor_plot(cor_stats_layer, \seealso{ Other Layer correlation functions: \code{\link{annotate_registered_clusters}()}, -\code{\link{layer_stat_cor}()}, -\code{\link{layer_stat_cor_plot_complex}()} +\code{\link{layer_stat_cor}()} } \author{ Louise Huuki-Myers diff --git a/man/layer_stat_cor_plot_complex.Rd b/man/layer_stat_cor_plot_complex.Rd deleted file mode 100644 index b85903f6..00000000 --- a/man/layer_stat_cor_plot_complex.Rd +++ /dev/null @@ -1,114 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/layer_stat_cor_plot_complex.R -\name{layer_stat_cor_plot_complex} -\alias{layer_stat_cor_plot_complex} -\title{Visualize the correlation of layer modeling t-statistics with ComplexHeatmap} -\usage{ -layer_stat_cor_plot_complex( - cor_stats_layer, - color_max = max(cor_stats_layer), - color_min = min(cor_stats_layer), - color_scale = RColorBrewer::brewer.pal(7, "PRGn"), - query_colors = NULL, - reference_colors = NULL, - annotation = NULL, - ... -) -} -\arguments{ -\item{cor_stats_layer}{The output of \code{\link[=layer_stat_cor]{layer_stat_cor()}}.} - -\item{color_max}{A \code{numeric(1)} specifying the highest correlation value for -the color scale (should be between 0 and 1).} - -\item{color_min}{A \code{numeric(1)} specifying the lowest correlation value for -the color scale (should be between 0 and -1).} - -\item{color_scale}{A \code{character} vector specifying the color scale for the -fill of the heatmap, defaults to classic purple -> green} - -\item{query_colors}{named \code{character} vector of colors, Adds colors to query -row annotations} - -\item{reference_colors}{named \code{character} vector of colors, Adds colors to -reference column annotations} - -\item{annotation}{annotation data.frame output of \code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}, -adds 'X' for good confidence annotations, '*' for poor confidence} - -\item{...}{Additinal parameters passed to \code{\link[ComplexHeatmap:Heatmap]{ComplexHeatmap::Heatmap()}}} -} -\value{ -(\link[ComplexHeatmap:Heatmap-class]{Heatmap-class}) plot of t-stat correlations -} -\description{ -This function updates \code{\link[=layer_stat_cor_plot]{layer_stat_cor_plot()}}, using ComplexHeatmap to plot -the correlation matrix between a reference and query modeling statistics -from \code{\link[=layer_stat_cor]{layer_stat_cor()}}. Includes functionality to add color annotations, -(helpful to match to colors in Visium spot plots), and annotations from -\code{\link[=annotate_registered_clusters]{annotate_registered_clusters()}}. -} -\examples{ -## Obtain the necessary data -## reference human pilot modeling results -if (!exists("modeling_results")) { - modeling_results <- fetch_data(type = "modeling_results") -} - -## querey spatailDLPFC modeling -query_modeling_results <- fetch_data(type = "spatialDLPFC_Visium_modeling_results") - -## extract t-statics and rename -registration_t_stats <- query_modeling_results$enrichment[, grep("^t_stat", colnames(query_modeling_results$enrichment))] -colnames(registration_t_stats) <- gsub("^t_stat_", "", colnames(registration_t_stats)) - -## Compute the correlations -cor_stats_layer <- layer_stat_cor( - stats = registration_t_stats, - modeling_results, - model_type = "enrichment" -) - -## Visualize the correlation matrix - -## most basic -layer_stat_cor_plot_complex(cor_stats_layer) - -## add colors -## add libd_layer_colors to refrence Human Pilot layers -layer_stat_cor_plot_complex(cor_stats_layer, reference_colors = libd_layer_colors) - -## supply polychrome colors to query clusters -cluster_colors <- c('#5A5156', '#E4E1E3', '#F6222E', '#FE00FA', '#16FF32', '#3283FE', '#FEAF16', '#B00068', '#1CFFCE') -names(cluster_colors) <- rownames(cor_stats_layer) - -layer_stat_cor_plot_complex(cor_stats_layer, - query_colors = cluster_colors, - reference_colors = libd_layer_colors) - -## Apply additional ComplexHeatmap param -layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE) - -## Add annotation -annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55) -layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df) - -## All together -layer_stat_cor_plot_complex(cor_stats_layer, - query_colors = cluster_colors, - reference_colors = libd_layer_colors, - annotation = annotation_df, - cluster_rows = FALSE, - cluster_columns = FALSE) - -} -\seealso{ -Other Layer correlation functions: -\code{\link{annotate_registered_clusters}()}, -\code{\link{layer_stat_cor}()}, -\code{\link{layer_stat_cor_plot}()} -} -\author{ -Louise Huuki-Myers -} -\concept{Layer correlation functions}