From a2b87bf923ce2fb373d5b212c8f930a85e924fcf Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 20:25:32 +1000 Subject: [PATCH] fix heatmap if cell type combination not symmetrical (#102) * adopt from ktplotspy * Update plot_cpdb_heatmap.R * fix * fix the issue * Update plot_cpdb_heatmap.R * fix missing test * split out the heatmap tests * Update test_cpdbplot_heatmap.R * Update test_cpdbplot_heatmap.R --- R/plot_cpdb_heatmap.R | 62 +++--- R/utils.R | 3 +- man/kidneyimmune.Rd | 2 +- man/plot_cpdb.Rd | 4 +- man/plot_cpdb_heatmap.Rd | 3 + .../{test_cpdbplot1.R => test_cpdbplot.R} | 196 +++++++++++------- tests/testthat/test_cpdbplot_heatmap.R | 56 +++++ 7 files changed, 218 insertions(+), 108 deletions(-) rename tests/testthat/{test_cpdbplot1.R => test_cpdbplot.R} (67%) create mode 100644 tests/testthat/test_cpdbplot_heatmap.R diff --git a/R/plot_cpdb_heatmap.R b/R/plot_cpdb_heatmap.R index 43bcc19c..4ae46944 100644 --- a/R/plot_cpdb_heatmap.R +++ b/R/plot_cpdb_heatmap.R @@ -22,7 +22,6 @@ #' @param alpha pvalue threshold to trim. #' @param return_tables whether or not to return the results as a table rather than the heatmap #' @param symmetrical whether or not to return as symmetrical matrix -#' @param special_character_regex_pattern search pattern if the cell type names contains special character. NULL defaults to '/|:|\\?|\\*|\\+|[\\]|\\(|\\)'. #' @param ... passed to pheatmap::pheatmap. #' @return pheatmap object of cellphone db output #' @examples @@ -35,40 +34,39 @@ #' @include utils.R #' @export -plot_cpdb_heatmap <- function( - pvals, cell_types = NULL, degs_analysis = FALSE, log1p_transform = FALSE, - show_rownames = TRUE, show_colnames = TRUE, scale = "none", cluster_cols = TRUE, - cluster_rows = TRUE, border_color = "white", fontsize_row = 11, fontsize_col = 11, - family = "Arial", main = "", treeheight_col = 0, treeheight_row = 0, low_col = "dodgerblue4", - mid_col = "peachpuff", high_col = "deeppink4", alpha = 0.05, return_tables = FALSE, - symmetrical = TRUE, special_character_regex_pattern = NULL, ...) { +plot_cpdb_heatmap <- function(pvals, cell_types = NULL, degs_analysis = FALSE, log1p_transform = FALSE, + show_rownames = TRUE, show_colnames = TRUE, scale = "none", cluster_cols = TRUE, + cluster_rows = TRUE, border_color = "white", fontsize_row = 11, fontsize_col = 11, + family = "Arial", main = "", treeheight_col = 0, treeheight_row = 0, low_col = "dodgerblue4", + mid_col = "peachpuff", high_col = "deeppink4", alpha = 0.05, return_tables = FALSE, + symmetrical = TRUE, ...) { requireNamespace("reshape2") requireNamespace("grDevices") - if (is.null(special_character_regex_pattern)) { - special_character_regex_pattern <- DEFAULT_SPEC_PAT - } all_intr <- pvals col_start <- ifelse(colnames(all_intr)[DEFAULT_CLASS_COL] == "classification", - DEFAULT_V5_COL_START, DEFAULT_COL_START - ) + DEFAULT_V5_COL_START, DEFAULT_COL_START) intr_pairs <- all_intr$interacting_pair all_intr <- t(all_intr[, -c(1:col_start - 1)]) colnames(all_intr) <- intr_pairs - if (!is.null(cell_types)) { - cell_types <- lapply(cell_types, function(cell_type) { - .sub_pattern(cell_type = cell_type, pattern = special_character_regex_pattern) - }) - cell_types_comb <- apply(expand.grid(cell_types, cell_types), 1, function(z) paste(z, collapse = "|")) - cell_types_keep <- row.names(all_intr)[row.names(all_intr) %in% cell_types_comb] - empty_celltypes <- setdiff(cell_types_comb, cell_types_keep) - all_intr <- all_intr[row.names(all_intr) %in% cell_types_keep, ] - if (length(empty_celltypes) > 0) { - tmp_ <- matrix(0, nrow = length(empty_celltypes), ncol = ncol(all_intr)) - colnames(tmp_) <- colnames(all_intr) - rownames(tmp_) <- empty_celltypes - tmp_ <- as.data.frame(tmp_) - all_intr <- rbind(all_intr, tmp_) + if (is.null(cell_types)) { + cell_types <- sort(unique(unlist(strsplit(colnames(pvals)[col_start:ncol(pvals)], + paste0("\\", DEFAULT_CPDB_SEP))))) + } + cell_types_comb <- apply(expand.grid(cell_types, cell_types), 1, function(z) { + paste(z, collapse = "|") + }) + cell_types_keep <- row.names(all_intr)[row.names(all_intr) %in% cell_types_comb] + empty_celltypes <- setdiff(cell_types_comb, cell_types_keep) + all_intr <- all_intr[row.names(all_intr) %in% cell_types_keep, ] + if (length(empty_celltypes) > 0) { + tmp_ <- matrix(0, nrow = length(empty_celltypes), ncol = ncol(all_intr)) + colnames(tmp_) <- colnames(all_intr) + rownames(tmp_) <- empty_celltypes + if (!degs_analysis) { + tmp_ <- tmp_ + 1 } + tmp_ <- as.data.frame(tmp_) + all_intr <- as.matrix(rbind(all_intr, tmp_)) } all_count <- reshape2::melt(all_intr) if (!degs_analysis) { @@ -101,20 +99,18 @@ plot_cpdb_heatmap <- function( count_mat <- log1p(count_mat) } - p <- pheatmap(count_mat, - show_rownames = show_rownames, show_colnames = show_colnames, + p <- pheatmap(count_mat, show_rownames = show_rownames, show_colnames = show_colnames, scale = scale, cluster_cols = cluster_cols, border_color = border_color, cluster_rows = cluster_rows, fontsize_row = fontsize_row, fontsize_col = fontsize_col, main = main, treeheight_row = treeheight_row, family = family, color = col.heatmap, - treeheight_col = treeheight_col, ... - ) + treeheight_col = treeheight_col, ...) if (return_tables) { if (symmetrical) { all_sum <- rowSums(count_mat) all_sum <- data.frame(all_sum) return(list(count_network = count_mat, interaction_count = all_sum)) } else { - count_mat <- t(count_mat) # so that the table output is the same layout as the plot + count_mat <- t(count_mat) # so that the table output is the same layout as the plot row_sum <- rowSums(count_mat) col_sum <- colSums(count_mat) all_sum <- data.frame(row_sum, col_sum) @@ -126,4 +122,4 @@ plot_cpdb_heatmap <- function( } else { stop("There are no significant results using p-value of: ", alpha, call. = FALSE) } -} +} \ No newline at end of file diff --git a/R/utils.R b/R/utils.R index e724f08e..a3baf419 100644 --- a/R/utils.R +++ b/R/utils.R @@ -9,6 +9,7 @@ DEFAULT_V5_COL_START <- 14 DEFAULT_CLASS_COL <- 13 DEFAULT_COL_START <- 12 SPECIAL_SEP <- paste0(rep(DEFAULT_SEP, 3), collapse = "") +DEFAULT_CPDB_SEP <- "|" .prep_dimensions <- function(input, reference) { @@ -30,7 +31,7 @@ SPECIAL_SEP <- paste0(rep(DEFAULT_SEP, 3), collapse = "") .prep_table <- function(data) { dat <- data rownames(dat) <- paste0(dat$id_cp_interaction, SPECIAL_SEP, dat$interacting_pair) - colnames(dat) <- gsub("\\|", DEFAULT_SEP, colnames(dat)) + colnames(dat) <- gsub(paste0("\\", DEFAULT_CPDB_SEP), DEFAULT_SEP, colnames(dat)) rownames(dat) <- gsub("_", "-", rownames(dat)) rownames(dat) <- gsub("[.]", " ", rownames(dat)) return(dat) diff --git a/man/kidneyimmune.Rd b/man/kidneyimmune.Rd index 6024cef3..6b4ac3b1 100644 --- a/man/kidneyimmune.Rd +++ b/man/kidneyimmune.Rd @@ -24,7 +24,7 @@ A SingleCellExperiment object with the following slots filled \describe{ \item{assays}{ - \itemize{Currently only contains "counts" and "logcounts" + \itemize{Currently only contains 'counts' and 'logcounts' \item{counts - Raw expression data} \item{logcounts - Normalized expression data}} } diff --git a/man/plot_cpdb.Rd b/man/plot_cpdb.Rd index 95c4080b..42569afa 100644 --- a/man/plot_cpdb.Rd +++ b/man/plot_cpdb.Rd @@ -112,7 +112,7 @@ Plotting CellPhoneDB results \donttest{ data(kidneyimmune) data(cpdb_output) -plot_cpdb(kidneyimmune, "B cell", "CD4T cell", "celltype", means, pvals, splitby_key = "Experiment", genes = c("CXCL13", "CD274", "CXCR5")) -plot_cpdb(kidneyimmune, "B cell", "CD4T cell", "celltype", means, pvals, splitby_key = "Experiment", gene_family = "chemokines") +plot_cpdb(kidneyimmune, 'B cell', 'CD4T cell', 'celltype', means, pvals, splitby_key = 'Experiment', genes = c('CXCL13', 'CD274', 'CXCR5')) +plot_cpdb(kidneyimmune, 'B cell', 'CD4T cell', 'celltype', means, pvals, splitby_key = 'Experiment', gene_family = 'chemokines') } } diff --git a/man/plot_cpdb_heatmap.Rd b/man/plot_cpdb_heatmap.Rd index 4bae53a7..e918d0d5 100644 --- a/man/plot_cpdb_heatmap.Rd +++ b/man/plot_cpdb_heatmap.Rd @@ -6,6 +6,7 @@ \usage{ plot_cpdb_heatmap( pvals, + cell_types = NULL, degs_analysis = FALSE, log1p_transform = FALSE, show_rownames = TRUE, @@ -32,6 +33,8 @@ plot_cpdb_heatmap( \arguments{ \item{pvals}{Dataframe corresponding to `pvalues.txt` or `relevant_interactions.txt` from CellPhoneDB.} +\item{cell_types}{vector of cell types to plot. If NULL, all cell types will be plotted.} + \item{degs_analysis}{Whether `CellPhoneDB` was run in `deg_analysis` mode} \item{log1p_transform}{Whether to log1p transform the output.} diff --git a/tests/testthat/test_cpdbplot1.R b/tests/testthat/test_cpdbplot.R similarity index 67% rename from tests/testthat/test_cpdbplot1.R rename to tests/testthat/test_cpdbplot.R index 17d280d4..f20c87a5 100644 --- a/tests/testthat/test_cpdbplot1.R +++ b/tests/testthat/test_cpdbplot.R @@ -5,64 +5,83 @@ data(cpdb_output_v5) test_that("combine_cpdb works 1", { p <- combine_cpdb(decon, decon, decon) + expect_that(class(p), equals("data.frame")) }) test_that("plot_cpdb works 1", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means, pvals = pvals, splitby_key = "Experiment", - genes = c("CXCL13", "CD274", "CXCR5"), keep_significant_only = FALSE) + genes = c("CXCL13", "CD274", "CXCR5"), keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 2", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means, pvals = pvals, splitby_key = "Experiment", - gene_family = "chemokines", keep_significant_only = FALSE) + gene_family = "chemokines", keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 3", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means, pvals = pvals, splitby_key = "Experiment", - gene_family = "chemokines", default_style = FALSE, keep_significant_only = FALSE) + gene_family = "chemokines", default_style = FALSE, keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 4", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means, pvals = pvals, splitby_key = "Experiment", - gene_family = "chemokines", keep_significant_only = FALSE) + gene_family = "chemokines", keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 5", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means, pvals = pvals, splitby_key = "Experiment", - gene_family = "chemokines", default_style = FALSE, keep_significant_only = FALSE) + gene_family = "chemokines", default_style = FALSE, keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 6", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means2, pvals = pvals2, gene_family = "custom_family", custom_gene_family = list(custom_family = c("CXCL13", "CD274", "CXCR5")), - keep_significant_only = FALSE) + keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 7", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means2, pvals = pvals2, gene_family = "custom_family", custom_gene_family = data.frame(custom_family = c("CXCL13", "CD274", "CXCR5")), - keep_significant_only = FALSE) + keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb works 8", { - p <- plot_cpdb(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, - celltype_key = "celltype", means = means2, pvals = pvals2, gene_family = c("chemokines", - "th1"), keep_significant_only = FALSE) + p <- plot_cpdb( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + celltype_key = "celltype", means = means2, pvals = pvals2, gene_family = c( + "chemokines", + "th1" + ), keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) @@ -79,10 +98,14 @@ test_that("weird characters are ok", { colnames(pvals) <- gsub("NK cell", "LTi-Like ILC3", colnames(pvals)) # remove the original split.by tags - colnames(means) <- gsub("Wilms2_|TxK1_|RCC1_|RCC2_|RCC3_|Wilms3_|TxK4_|VHLRCC_|Wilms1_|Teen_|Tx_|TxK3_|TxK2_|PapRCC", - "", colnames(means)) - colnames(pvals) <- gsub("Wilms2_|TxK1_|RCC1_|RCC2_|RCC3_|Wilms3_|TxK4_|VHLRCC_|Wilms1_|Teen_|Tx_|TxK3_|TxK2_|PapRCC", - "", colnames(pvals)) + colnames(means) <- gsub( + "Wilms2_|TxK1_|RCC1_|RCC2_|RCC3_|Wilms3_|TxK4_|VHLRCC_|Wilms1_|Teen_|Tx_|TxK3_|TxK2_|PapRCC", + "", colnames(means) + ) + colnames(pvals) <- gsub( + "Wilms2_|TxK1_|RCC1_|RCC2_|RCC3_|Wilms3_|TxK4_|VHLRCC_|Wilms1_|Teen_|Tx_|TxK3_|TxK2_|PapRCC", + "", colnames(pvals) + ) # transpose and average to get rid of duplicate columns means_df <- as.data.frame(t(means[, c(12:ncol(means))])) @@ -103,44 +126,60 @@ test_that("weird characters are ok", { newpvals <- cbind(pvals[, 1:11], pvals_df) # plot_cpdb - p <- plot_cpdb(cell_type1 = "TRC+", cell_type2 = "LTi-Like ILC3", scdata = kidneyimmune, - celltype_key = "celltype", means = newmeans, pvals = newpvals, genes = c("LTB", - "LTBR", "KITL", "KIT", "CCR6"), keep_significant_only = FALSE) + p <- plot_cpdb( + cell_type1 = "TRC+", cell_type2 = "LTi-Like ILC3", scdata = kidneyimmune, + celltype_key = "celltype", means = newmeans, pvals = newpvals, genes = c( + "LTB", + "LTBR", "KITL", "KIT", "CCR6" + ), keep_significant_only = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb2 works 1", { - p <- plot_cpdb2(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb2( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means2, pvals = pvals2, deconvoluted = decon2, desiredInteractions = list(c("CD4T cell", "B cell"), c("B cell", "CD4T cell")), - interaction_grouping = interaction_annotation, edge_group_colors = c(Activating = "#e15759", + interaction_grouping = interaction_annotation, edge_group_colors = c( + Activating = "#e15759", Chemotaxis = "#59a14f", Inhibitory = "#4e79a7", `Intracellular trafficking` = "#9c755f", - DC_development = "#B07aa1", Unknown = NA), node_group_colors = c(`CD4T cell` = "#86bc86", - `B cell` = "#79706e"), keep_significant_only = TRUE, standard_scale = TRUE, - remove_self = TRUE) + DC_development = "#B07aa1", Unknown = NA + ), node_group_colors = c( + `CD4T cell` = "#86bc86", + `B cell` = "#79706e" + ), keep_significant_only = TRUE, standard_scale = TRUE, + remove_self = TRUE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb2 works 2", { - p <- plot_cpdb2(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, - celltype_key = "celltype", means = means2, pvals = pvals2, deconvoluted = decon2) + p <- plot_cpdb2( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + celltype_key = "celltype", means = means2, pvals = pvals2, deconvoluted = decon2 + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb3 works 1", { - p <- plot_cpdb3(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb3( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", means = means2, pvals = pvals2, deconvoluted = decon2, - keep_significant_only = TRUE, standard_scale = TRUE, remove_self = TRUE) + keep_significant_only = TRUE, standard_scale = TRUE, remove_self = TRUE + ) expect_that(class(p), equals("recordedplot")) }) test_that("plot_cpdb3 2", { - p <- plot_cpdb3(cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, + p <- plot_cpdb3( + cell_type1 = "B cell", cell_type2 = "CD4T cell", scdata = kidneyimmune, celltype_key = "celltype", splitby_key = "Experiment", means = means, pvals = pvals, deconvoluted = decon, keep_significant_only = TRUE, standard_scale = TRUE, - remove_self = TRUE) + remove_self = TRUE + ) expect_that(class(p[[1]]), equals("recordedplot")) expect_that(class(p[[2]]), equals("recordedplot")) expect_that(class(p[[3]]), equals("recordedplot")) @@ -155,112 +194,127 @@ test_that("plot_cpdb3 2", { }) test_that("plot_cpdb4 works 1", { - p <- plot_cpdb4(interaction = "CLEC2D-KLRB1", cell_type1 = "NK", cell_type2 = "Mast", + p <- plot_cpdb4( + interaction = "CLEC2D-KLRB1", cell_type1 = "NK", cell_type2 = "Mast", scdata = kidneyimmune, celltype_key = "celltype", means = means2, pvals = pvals2, deconvoluted = decon2, keep_significant_only = TRUE, standard_scale = TRUE, - remove_self = TRUE) + remove_self = TRUE + ) expect_that(class(p), equals("recordedplot")) }) test_that("plot_cpdb4 works 2", { - p <- plot_cpdb4(interaction = c("CLEC2D-KLRB1", "CD40-CD40LG"), cell_type1 = "NK|B", + p <- plot_cpdb4( + interaction = c("CLEC2D-KLRB1", "CD40-CD40LG"), cell_type1 = "NK|B", cell_type2 = "Mast|CD4T", scdata = kidneyimmune, celltype_key = "celltype", - means = means2, pvals = pvals2, deconvoluted = decon2, desiredInteractions = list(c("NK cell", - "Mast cell"), c("NK cell", "NKT cell"), c("NKT cell", "Mast cell"), c("B cell", - "CD4T cell")), keep_significant_only = TRUE) + means = means2, pvals = pvals2, deconvoluted = decon2, desiredInteractions = list(c( + "NK cell", + "Mast cell" + ), c("NK cell", "NKT cell"), c("NKT cell", "Mast cell"), c( + "B cell", + "CD4T cell" + )), keep_significant_only = TRUE + ) expect_that(class(p), equals("recordedplot")) }) test_that("plot_cpdb4 works 3", { - p <- plot_cpdb4(interaction = "CLEC2D-KLRB1", cell_type1 = "NK", cell_type2 = "Mast", + p <- plot_cpdb4( + interaction = "CLEC2D-KLRB1", cell_type1 = "NK", cell_type2 = "Mast", scdata = kidneyimmune, celltype_key = "celltype", splitby_key = "Experiment", means = means, pvals = pvals, deconvoluted = decon, keep_significant_only = TRUE, - standard_scale = TRUE, remove_self = TRUE) + standard_scale = TRUE, remove_self = TRUE + ) for (i in 1:13) { expect_that(class(p[[i]]), equals("recordedplot")) } }) -test_that("plot_cpdb_heatmap works", { - p <- plot_cpdb_heatmap(pvals2) - expect_that(class(p), equals("pheatmap")) -}) - -test_that("plot_cpdb_heatmap works2", { - p <- plot_cpdb_heatmap(pvals2, cell_types = c("B cell", "CD4T cell", "NK cell")) - expect_that(class(p), equals("pheatmap")) -}) - - test_that("plot_cpdb v5 1", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", genes = c("TGFB2", "CSF1R"), max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, interaction_scores = interaction_scores_v5, - scale_alpha_by_interaction_scores = TRUE, min_interaction_score = 20) + scale_alpha_by_interaction_scores = TRUE, min_interaction_score = 20 + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 2", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", genes = c("TGFB2", "CSF1R"), max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, cellsign = cellsign_v5, - scale_alpha_by_interaction_scores = TRUE) + scale_alpha_by_interaction_scores = TRUE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 3", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, cellsign = cellsign_v5, scale_alpha_by_cellsign = TRUE, - filter_by_cellsign = TRUE) + filter_by_cellsign = TRUE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 4", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, - standard_scale = TRUE, cellsign = cellsign_v5, scale_alpha_by_cellsign = TRUE) + standard_scale = TRUE, cellsign = cellsign_v5, scale_alpha_by_cellsign = TRUE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 5", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", genes = c("TGFB2", "CSF1R"), max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, interaction_scores = interaction_scores_v5, - scale_alpha_by_interaction_scores = TRUE, min_interaction_score = 20, default_style = FALSE) + scale_alpha_by_interaction_scores = TRUE, min_interaction_score = 20, default_style = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 6", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", genes = c("TGFB2", "CSF1R"), max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, cellsign = cellsign_v5, - scale_alpha_by_interaction_scores = TRUE, default_style = FALSE) + scale_alpha_by_interaction_scores = TRUE, default_style = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 7", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, cellsign = cellsign_v5, scale_alpha_by_cellsign = TRUE, - filter_by_cellsign = TRUE, default_style = FALSE) + filter_by_cellsign = TRUE, default_style = FALSE + ) expect_true(is.ggplot(p)) }) test_that("plot_cpdb v5 8", { - p <- plot_cpdb(scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", + p <- plot_cpdb( + scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", cell_type2 = "EVT_1|EVT_2|GC|iEVT|eEVT|VCT_CCC", means = means_v5, pvals = relevant_interactions_v5, celltype_key = "cell_labels", max_size = 6, highlight_size = 0.75, degs_analysis = TRUE, standard_scale = TRUE, cellsign = cellsign_v5, scale_alpha_by_cellsign = TRUE, - default_style = FALSE) + default_style = FALSE + ) expect_true(is.ggplot(p)) -}) \ No newline at end of file +}) diff --git a/tests/testthat/test_cpdbplot_heatmap.R b/tests/testthat/test_cpdbplot_heatmap.R new file mode 100644 index 00000000..00e2009f --- /dev/null +++ b/tests/testthat/test_cpdbplot_heatmap.R @@ -0,0 +1,56 @@ +data(kidneyimmune) +data(cpdb_output) +data(cpdb_output2) +data(cpdb_output_v5) + + +test_that("plot_cpdb_heatmap works", { + p <- plot_cpdb_heatmap(pvals2) + expect_that(class(p), equals("pheatmap")) +}) + +test_that("plot_cpdb_heatmap return table", { + p <- plot_cpdb_heatmap(pvals2, return_tables = TRUE) + expect_true(is.list(p)) + expect_true(is.matrix(p[[1]])) + expect_true(is.data.frame(p[[2]])) +}) + +test_that("plot_cpdb_heatmap works for log", { + p <- plot_cpdb_heatmap(pvals2, log1p_transform = TRUE) + expect_that(class(p), equals("pheatmap")) +}) + +test_that("plot_cpdb_heatmap return table", { + p <- plot_cpdb_heatmap(pvals2, return_tables = TRUE, log1p_transform = TRUE) + expect_true(is.list(p)) + expect_true(is.matrix(p[[1]])) + expect_true(is.data.frame(p[[2]])) +}) + +test_that("plot_cpdb_heatmap works2", { + p <- plot_cpdb_heatmap(relevant_interactions_v5, cell_types = c( + "iEVT", "PV MYH11", + "PV STEAP4", "EVT_1" + ), degs_analysis = TRUE) + expect_that(class(p), equals("pheatmap")) +}) + +test_that("plot_cpdb_heatmap works2", { + p <- plot_cpdb_heatmap(relevant_interactions_v5, cell_types = c( + "iEVT", "PV MYH11", + "PV STEAP4", "EVT_1" + ), degs_analysis = FALSE) + expect_that(class(p), equals("pheatmap")) +}) + + +test_that("plot_cpdb_heatmap return table not symmetrical", { + p <- plot_cpdb_heatmap(relevant_interactions_v5, cell_types = c( + "iEVT", "PV MYH11", + "PV STEAP4", "EVT_1" + ), degs_analysis = TRUE, return_tables = TRUE, symmetrical = FALSE) + expect_true(is.list(p)) + expect_true(is.matrix(p[[1]])) + expect_true(is.data.frame(p[[2]])) +})