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 +})