1-
2-
31# ' Visualize the correlation of layer modeling t-statistics with ComplexHeatmap
4- # ' @param query_colors named vector of colors for query row annotations
5- # ' @param reference_colors named vector of colors for reference column annotations
2+ # '
3+ # ' This function updates [layer_stat_cor_plot()], using ComplexHeatmap to plot
4+ # ' the correlation matrix between a reference and query modeling statistics
5+ # ' from [layer_stat_cor()]. Includes functionality to add color annotations,
6+ # ' (helpful to match to colors in Visium spot plots), and annotations from
7+ # ' [annotate_registered_clusters()].
8+ # '
9+ # ' @param color_max A `numeric(1)` specifying the highest correlation value for
10+ # ' the color scale (should be between 0 and 1).
11+ # ' @param color_min A `numeric(1)` specifying the lowest correlation value for
12+ # ' the color scale (should be between 0 and -1).
13+ # ' @param color_scale A `character` vector specifying the color scale for the
14+ # ' fill of the heatmap, defaults to classic purple -> green
15+ # ' @param query_colors named `character` vector of colors, Adds colors to query
16+ # ' row annotations
17+ # ' @param reference_colors named `character` vector of colors, Adds colors to
18+ # ' reference column annotations
19+ # ' @param annotation annotation data.frame output of [annotate_registered_clusters()],
20+ # ' adds 'X' for good confidence annotations, '*' for poor confidence
621# '
722# ' @inheritParams layer_stat_cor_plot
823# '
9- # ' @return ComplexHeatmap plot of t-stat correlations
24+ # ' @return ([Heatmap-class][ ComplexHeatmap::Heatmap-class]) plot of t-stat correlations
1025# ' @export
26+ # ' @author Louise Huuki-Myers
27+ # ' @family Layer correlation functions
28+ # '
29+ # ' @importFrom RColorBrewer brewer.pal
30+ # ' @importFrom grDevices colorRampPalette
31+ # ' @importFrom ComplexHeatmap columnAnnotation rowAnnotation Heatmap
1132# '
1233# ' @examples
1334# ' ## Obtain the necessary data
4869# ' reference_colors = libd_layer_colors)
4970# '
5071# ' ## Apply additional ComplexHeatmap param
51- # ' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE)
72+ # ' layer_stat_cor_plot_complex(cor_stats_layer, cluster_rows = FALSE, cluster_columns = FALSE )
5273# '
74+ # ' ## Add annotation
75+ # ' annotation_df <- annotate_registered_clusters(cor_stats_layer, confidence_threshold = .55)
76+ # ' layer_stat_cor_plot_complex(cor_stats_layer, annotation = annotation_df)
77+ # '
78+ # ' ## All together
79+ # ' layer_stat_cor_plot_complex(cor_stats_layer,
80+ # ' query_colors = cluster_colors,
81+ # ' reference_colors = libd_layer_colors,
82+ # ' annotation = annotation_df,
83+ # ' rect_gp = gpar(col = "black", lwd = 1))
84+ # '
5385layer_stat_cor_plot_complex <- function (cor_stats_layer ,
54- theSeq = seq(min(cor_stats_layer ), max(cor_stats_layer ), by = 0.01 ),
55- my.col = grDevices :: colorRampPalette(RColorBrewer :: brewer.pal(7 , " PRGn" ))(length(theSeq )),
86+ color_max = max(cor_stats_layer ),
87+ color_min = min(cor_stats_layer ),
88+ color_scale = RColorBrewer :: brewer.pal(7 , " PRGn" ),
5689 query_colors = NULL ,
5790 reference_colors = NULL ,
91+ annotation = NULL ,
5892 ...
5993 ){
6094
95+ # # define color pallet
96+ theSeq = seq(color_min , color_max , by = 0.01 )
97+ my.col = grDevices :: colorRampPalette(color_scale )(length(theSeq ))
98+
6199 # ## query annotations on row
62100 if (! is.null(query_colors )){
63101
@@ -84,16 +122,55 @@ layer_stat_cor_plot_complex <- function(cor_stats_layer,
84122 )
85123 } else ref_col_annotation <- NULL
86124
87-
125+ # # add annotation
126+ if (! is.null(annotation )){
127+ anno_matrix <- create_annotation_matrix(annotation , cor_stats_layer )
128+
129+ # # plot heatmap
130+ return (
131+ ComplexHeatmap :: Heatmap(
132+ matrix = cor_stats_layer ,
133+ col = my.col ,
134+ bottom_annotation = ref_col_annotation ,
135+ right_annotation = query_row_annotation ,
136+ cell_fun = function (j , i , x , y , width , height , fill ) {
137+ grid.text(anno_matrix [i , j ], x , y , gp = gpar(fontsize = 10 ))
138+ },
139+ ...
140+ ))
141+ }
88142
89143 # # plot heatmap
144+ return (
90145 ComplexHeatmap :: Heatmap(
91146 matrix = cor_stats_layer ,
92147 col = my.col ,
93148 bottom_annotation = ref_col_annotation ,
94149 right_annotation = query_row_annotation ,
95150 ...
96- )
151+ ))
97152
98153
99154}
155+
156+ create_annotation_matrix <- function (annotation_df , cor_stats_layer ){
157+
158+ anno_list <- lapply(rownames(cor_stats_layer ),
159+ function (cluster ){
160+ # look up confidence
161+ confidence <- annotation_df [match(cluster , annotation_df $ cluster )," layer_confidence" ]
162+ sym <- ifelse(confidence == " good" , " X" ," *" )
163+ # match annotations
164+ anno <- annotation_df [match(cluster , annotation_df $ cluster )," layer_label" ]
165+ return (ifelse(unlist(lapply(colnames(cor_stats_layer ), grepl , anno )),sym ," " ))
166+ })
167+
168+ anno_matrix <- t(data.frame (anno_list ))
169+ rownames(anno_matrix ) <- rownames(cor_stats_layer )
170+ colnames(anno_matrix ) <- colnames(cor_stats_layer )
171+
172+ return (anno_matrix )
173+ }
174+
175+
176+
0 commit comments