From e33d8b9abe497ed1c73e075878faaeb16bf12063 Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 17:04:32 +1000 Subject: [PATCH 1/9] adopt from ktplotspy --- R/plot_cpdb_heatmap.R | 34 ++++++++++++++-------------------- R/utils.R | 3 ++- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/R/plot_cpdb_heatmap.R b/R/plot_cpdb_heatmap.R index 43bcc19..2064b05 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 @@ -41,12 +40,9 @@ plot_cpdb_heatmap <- function( 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, ...) { + 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 @@ -54,21 +50,19 @@ plot_cpdb_heatmap <- function( 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 <- unique(unlist(strsplit(colnames(all_intr)[col_start] intr_pairs, 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 + tmp_ <- as.data.frame(tmp_) + all_intr <- rbind(all_intr, tmp_) } all_count <- reshape2::melt(all_intr) if (!degs_analysis) { diff --git a/R/utils.R b/R/utils.R index e724f08..a3baf41 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) From 0a09f0c85404a05318fda87a464695f8a08e1d5b Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 17:15:01 +1000 Subject: [PATCH 2/9] Update plot_cpdb_heatmap.R --- R/plot_cpdb_heatmap.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plot_cpdb_heatmap.R b/R/plot_cpdb_heatmap.R index 2064b05..3ee57a8 100644 --- a/R/plot_cpdb_heatmap.R +++ b/R/plot_cpdb_heatmap.R @@ -51,7 +51,7 @@ plot_cpdb_heatmap <- function( all_intr <- t(all_intr[, -c(1:col_start - 1)]) colnames(all_intr) <- intr_pairs if (is.null(cell_types)) { - cell_types <- unique(unlist(strsplit(colnames(all_intr)[col_start] intr_pairs, DEFAULT_CPDB_SEP))) + cell_types <- sort(unique(unlist(strsplit(all_intr.columns[col_start:], 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] From e755059ac3089de19b17918e8e0305d2045f91f0 Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 17:32:00 +1000 Subject: [PATCH 3/9] fix --- R/plot_cpdb_heatmap.R | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/R/plot_cpdb_heatmap.R b/R/plot_cpdb_heatmap.R index 3ee57a8..7d5ce3a 100644 --- a/R/plot_cpdb_heatmap.R +++ b/R/plot_cpdb_heatmap.R @@ -34,26 +34,26 @@ #' @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, ...) { +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") 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 <- sort(unique(unlist(strsplit(all_intr.columns[col_start:], DEFAULT_CPDB_SEP)))) + cell_types <- sort(unique(unlist(strsplit(colnames(all_intr)[col_start:ncol(all_intr)], + paste0("\\", DEFAULT_CPDB_SEP))))) } - cell_types_comb <- apply(expand.grid(cell_types, cell_types), 1, function(z) paste(z, collapse = "|")) + 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, ] @@ -95,20 +95,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) @@ -120,4 +118,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 From 3689d07e76b8578791e4432e2d9d5576b2ce8395 Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 18:41:44 +1000 Subject: [PATCH 4/9] fix the issue --- R/plot_cpdb_heatmap.R | 10 +- man/kidneyimmune.Rd | 2 +- man/plot_cpdb.Rd | 4 +- man/plot_cpdb_heatmap.Rd | 3 + tests/testthat/test_cpdbplot1.R | 189 +++++++++++++++++++++----------- 5 files changed, 141 insertions(+), 67 deletions(-) diff --git a/R/plot_cpdb_heatmap.R b/R/plot_cpdb_heatmap.R index 7d5ce3a..bbe5d10 100644 --- a/R/plot_cpdb_heatmap.R +++ b/R/plot_cpdb_heatmap.R @@ -52,8 +52,9 @@ plot_cpdb_heatmap <- function(pvals, cell_types = NULL, degs_analysis = FALSE, l cell_types <- sort(unique(unlist(strsplit(colnames(all_intr)[col_start:ncol(all_intr)], paste0("\\", DEFAULT_CPDB_SEP))))) } - cell_types_comb <- apply(expand.grid(cell_types, cell_types), 1, function(z) paste(z, - collapse = "|")) + 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, ] @@ -61,8 +62,11 @@ plot_cpdb_heatmap <- function(pvals, cell_types = NULL, degs_analysis = FALSE, l 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 <- rbind(all_intr, tmp_) + all_intr <- as.matrix(rbind(all_intr, tmp_)) } all_count <- reshape2::melt(all_intr) if (!degs_analysis) { diff --git a/man/kidneyimmune.Rd b/man/kidneyimmune.Rd index 6024cef..6b4ac3b 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 95c4080..42569af 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 4bae53a..e918d0d 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_cpdbplot1.R index 17d280d..4a2b61f 100644 --- a/tests/testthat/test_cpdbplot1.R +++ b/tests/testthat/test_cpdbplot1.R @@ -9,60 +9,78 @@ test_that("combine_cpdb works 1", { 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 +97,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 +125,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,29 +193,39 @@ 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")) } @@ -189,78 +237,97 @@ test_that("plot_cpdb_heatmap works", { }) test_that("plot_cpdb_heatmap works2", { - p <- plot_cpdb_heatmap(pvals2, cell_types = c("B cell", "CD4T cell", "NK cell")) + 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 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 +}) From 2d1a536733903e80f4ea58c8a8970405e7c12fda Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 18:48:41 +1000 Subject: [PATCH 5/9] Update plot_cpdb_heatmap.R --- R/plot_cpdb_heatmap.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/plot_cpdb_heatmap.R b/R/plot_cpdb_heatmap.R index bbe5d10..4ae4694 100644 --- a/R/plot_cpdb_heatmap.R +++ b/R/plot_cpdb_heatmap.R @@ -49,7 +49,7 @@ plot_cpdb_heatmap <- function(pvals, cell_types = NULL, degs_analysis = FALSE, l all_intr <- t(all_intr[, -c(1:col_start - 1)]) colnames(all_intr) <- intr_pairs if (is.null(cell_types)) { - cell_types <- sort(unique(unlist(strsplit(colnames(all_intr)[col_start:ncol(all_intr)], + 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) { From 468d1f873fb0b0af76e452f4cdbb222e4e7e5da7 Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 18:51:08 +1000 Subject: [PATCH 6/9] fix missing test --- tests/testthat/test_cpdbplot1.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test_cpdbplot1.R b/tests/testthat/test_cpdbplot1.R index 4a2b61f..ff7cae4 100644 --- a/tests/testthat/test_cpdbplot1.R +++ b/tests/testthat/test_cpdbplot1.R @@ -5,6 +5,7 @@ data(cpdb_output_v5) test_that("combine_cpdb works 1", { p <- combine_cpdb(decon, decon, decon) + expect_that(class(p), equals("data.frame")) }) From fdeb74d3d16338b5c6536286bd4a31bda7b8f7eb Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 19:06:39 +1000 Subject: [PATCH 7/9] split out the heatmap tests --- .../{test_cpdbplot1.R => test_cpdbplot.R} | 14 ------ tests/testthat/test_cpdbplot_heatmap.R | 45 +++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) rename tests/testthat/{test_cpdbplot1.R => test_cpdbplot.R} (97%) create mode 100644 tests/testthat/test_cpdbplot_heatmap.R diff --git a/tests/testthat/test_cpdbplot1.R b/tests/testthat/test_cpdbplot.R similarity index 97% rename from tests/testthat/test_cpdbplot1.R rename to tests/testthat/test_cpdbplot.R index ff7cae4..f20c87a 100644 --- a/tests/testthat/test_cpdbplot1.R +++ b/tests/testthat/test_cpdbplot.R @@ -232,20 +232,6 @@ test_that("plot_cpdb4 works 3", { } }) -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(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 v5 1", { p <- plot_cpdb( scdata = sce_v5, cell_type1 = "PV MYH11|PV STEAP4|PV MMPP11", diff --git a/tests/testthat/test_cpdbplot_heatmap.R b/tests/testthat/test_cpdbplot_heatmap.R new file mode 100644 index 0000000..cb94ea8 --- /dev/null +++ b/tests/testthat/test_cpdbplot_heatmap.R @@ -0,0 +1,45 @@ +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")) +}) From be92f5866f3e9f022d15c7fd4baac38605ca173f Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 19:29:16 +1000 Subject: [PATCH 8/9] Update test_cpdbplot_heatmap.R --- tests/testthat/test_cpdbplot_heatmap.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/testthat/test_cpdbplot_heatmap.R b/tests/testthat/test_cpdbplot_heatmap.R index cb94ea8..4a6bd40 100644 --- a/tests/testthat/test_cpdbplot_heatmap.R +++ b/tests/testthat/test_cpdbplot_heatmap.R @@ -43,3 +43,12 @@ test_that("plot_cpdb_heatmap works2", { ), degs_analysis = FALSE) 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 = TRUE, symmetrical = FALSE) + expect_that(class(p), equals("pheatmap")) +}) From 551ad1978b70bdcbbac09061461d611a626b79dd Mon Sep 17 00:00:00 2001 From: Zewen Kelvin Tuong Date: Sat, 17 Feb 2024 19:44:56 +1000 Subject: [PATCH 9/9] Update test_cpdbplot_heatmap.R --- tests/testthat/test_cpdbplot_heatmap.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test_cpdbplot_heatmap.R b/tests/testthat/test_cpdbplot_heatmap.R index 4a6bd40..00e2009 100644 --- a/tests/testthat/test_cpdbplot_heatmap.R +++ b/tests/testthat/test_cpdbplot_heatmap.R @@ -45,10 +45,12 @@ test_that("plot_cpdb_heatmap works2", { }) -test_that("plot_cpdb_heatmap works2", { +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, symmetrical = FALSE) - expect_that(class(p), equals("pheatmap")) + ), 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]])) })