Skip to content

Commit 61af5df

Browse files
authored
Merge pull request #99 from LieberInstitute/improve_layer_stat_cor_plot
Improve layer stat cor plot
2 parents 41866c2 + 2822d51 commit 61af5df

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

R/annotate_registered_clusters.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ annotate_registered_clusters <-
5858
annotate_registered_cluster,
5959
cutoff_merge_ratio = cutoff_merge_ratio
6060
)
61+
62+
if(any(grepl("/", colnames(cor_stats_layer)))) {
63+
stop(
64+
"Cannot use refrence lables containing '/' - check colnames(cor_stats_layer)",
65+
call. = FALSE
66+
)
67+
}
6168

6269
confidence <- apply(cor_stats_layer, 1, max) > confidence_threshold
6370

R/layer_stat_cor_plot.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,26 @@ layer_stat_cor_plot <- function(
199199
}
200200

201201
create_annotation_matrix <- function(annotation_df, cor_stats_layer) {
202+
203+
if(!setequal(rownames(cor_stats_layer), annotation_df$cluster)){
204+
stop(
205+
"Query cluster names do not match between rownames(cor_stats_layer) and annotation$cluster.\n
206+
Be sure the annotation data matches.",
207+
call. = FALSE
208+
)
209+
}
210+
202211
anno_list <- lapply(
203212
rownames(cor_stats_layer),
204213
function(cluster) {
205214
# look up confidence
206215
confidence <- annotation_df[match(cluster, annotation_df$cluster), "layer_confidence"]
207216
sym <- ifelse(confidence == "good", "X", "*")
208217
# match annotations
209-
anno <- annotation_df[match(cluster, annotation_df$cluster), "layer_label"]
210-
return(ifelse(unlist(lapply(colnames(cor_stats_layer), grepl, anno)), sym, ""))
218+
anno <- gsub("\\*","",annotation_df[match(cluster, annotation_df$cluster), "layer_label"])
219+
anno_lgl_row <- unlist(lapply(colnames(cor_stats_layer), "%in%", unlist(strsplit(anno, split = "/"))))
220+
221+
return(ifelse(anno_lgl_row, sym, ""))
211222
}
212223
)
213224

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## Obtain the necessary data
2+
if (!exists("modeling_results")) {
3+
modeling_results <- fetch_data(type = "modeling_results")
4+
}
5+
6+
## query spatialDLPFC modeling results
7+
query_modeling_results <- fetch_data(
8+
type = "spatialDLPFC_Visium_modeling_results"
9+
)
10+
11+
## Compute the correlations
12+
cor_stats_layer <- layer_stat_cor(
13+
stats = query_modeling_results$enrichment,
14+
modeling_results,
15+
model_type = "enrichment"
16+
)
17+
18+
## Obtain labels
19+
annotate_registered_clusters(cor_stats_layer)
20+
21+
22+
test_that("Stop with slash", {
23+
colnames(cor_stats_layer) <- gsub("ayer", "/", colnames(cor_stats_layer))
24+
expect_error(annotate_registered_clusters(cor_stats_layer))
25+
})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
## Obtain the necessary data
3+
## reference human pilot modeling results
4+
if (!exists("modeling_results")) {
5+
modeling_results <- fetch_data(type = "modeling_results")
6+
}
7+
8+
## query spatialDLPFC modeling results
9+
query_modeling_results <- fetch_data(
10+
type = "spatialDLPFC_Visium_modeling_results"
11+
)
12+
13+
## Compute the correlations
14+
cor_stats_layer <- layer_stat_cor(
15+
stats = query_modeling_results$enrichment,
16+
modeling_results,
17+
model_type = "enrichment"
18+
)
19+
20+
## Default plot with no annotations and defaults for ComplexHeatmap()
21+
# default_plot <- layer_stat_cor_plot(cor_stats_layer)
22+
23+
24+
## Add annotation
25+
annotation_df <- annotate_registered_clusters(
26+
cor_stats_layer,
27+
confidence_threshold = .55
28+
)
29+
30+
# anno_plot <- layer_stat_cor_plot(cor_stats_layer, annotation = annotation_df)
31+
32+
## test w/ numeric reference lables
33+
cor_stats_numeric <- cor_stats_layer
34+
colnames(cor_stats_numeric) <- c(13, 6:1)
35+
36+
annotation_df_numeric <- annotate_registered_clusters(
37+
cor_stats_numeric,
38+
confidence_threshold = .55
39+
)
40+
41+
# layer_stat_cor_plot(cor_stats_numeric, annotation = annotation_df_numeric)
42+
43+
44+
test_that("annotation checks work",{
45+
annotation_df$cluster <- gsub("Sp09", "",annotation_df$cluster)
46+
expect_error(create_annotation_matrix(annotation_df, cor_stats_layer))
47+
})
48+
49+
test_that("annotation matrix works w/ short cluster names", {
50+
anno_matrix <- create_annotation_matrix(annotation_df, cor_stats_layer)
51+
anno_matrix_numeric <- create_annotation_matrix(annotation_df_numeric, cor_stats_numeric)
52+
colnames(anno_matrix_numeric) <- colnames(anno_matrix)
53+
54+
expect_equal(anno_matrix, anno_matrix_numeric)
55+
})
56+

0 commit comments

Comments
 (0)