Skip to content

Commit

Permalink
add example and test for abVolPlot
Browse files Browse the repository at this point in the history
  • Loading branch information
mihem committed Nov 13, 2024
1 parent ef59900 commit 3217b69
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 26 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ importFrom(grDevices,dev.off)
importFrom(grDevices,pdf)
importFrom(magrittr,"%>%")
importFrom(methods,new)
importFrom(rstatix,wilcox_test)
importFrom(stats,as.formula)
importFrom(stats,model.matrix)
importFrom(stats,p.adjust)
Expand Down
38 changes: 24 additions & 14 deletions R/visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,36 @@ stackedPlot <- function(object, x_axis, y_axis, x_order, y_order, color, width,
#' @param dir_output directory to save the output plot (default: ".")
#' @return save volcano abundance plot
#' @examples
#' \dontrun{
#' abVolPlot(
#' object = aie_sct,
#' cluster_idents = "predicted.id",
#' sample = "sample",
#' cluster_order = unique(aie_sct$predicted.id),
#' group_by = "AIE_type",
#' group1 = "LGI1",
#' group2 = "control",
#' color = dittoColors(),
#' min_pct = 0.5
#' library(Seurat)
#' library(rstatix)
#' set.seed(123)
#' pbmc_small$predicted.id <- sample(c("Cluster1", "Cluster2"), ncol(pbmc_small), replace = TRUE)
#' pbmc_small$sample <- sample(c("Sample1", "Sample2"), ncol(pbmc_small), replace = TRUE)
#' pbmc_small$AIE_type <- sample(c("LGI1", "control"), ncol(pbmc_small), replace = TRUE)
#' suppressWarnings(
#' plot <- abVolPlot(
#' object = pbmc_small,
#' cluster_idents = "predicted.id",
#' sample = "sample",
#' cluster_order = c("Cluster1", "Cluster2"),
#' group_by = "AIE_type",
#' group1 = "LGI1",
#' group2 = "control",
#' color = c("Cluster1" = "blue", "Cluster2" = "red"),
#' width = 5,
#' height = 5,
#' dir_output = "."
#' )
#' )
#' }
#' @export
#' unlink("volcano_plot_predicted.id_pbmc_small_LGI1_control.pdf")
#' @importFrom dplyr across left_join mutate
#' @importFrom ggplot2 aes geom_hline geom_point geom_vline ggplot ggsave scale_color_manual theme theme_classic xlab ylab
#' @importFrom stats wilcox.test
#' @importFrom tibble rownames_to_column tibble
#' @importFrom tidyr pivot_longer
#' @importFrom tidyselect where
#' @importFrom rstatix wilcox_test
#' @export

abVolPlot <- function(object, cluster_idents, sample, cluster_order, group_by, group1, group2, color, width = 5, height = 5, min_cells = 10, paired = FALSE, dir_output = ".") {
if (!inherits(object, "Seurat")) {
Expand All @@ -406,7 +416,7 @@ abVolPlot <- function(object, cluster_idents, sample, cluster_order, group_by, g

for (i in cluster_order) {
out1 <- cl_size_ind[cl_size_ind$cluster == i, ]
pvalue_res[i] <- wilcox.test(count ~ group_by, data = out1, paired = paired)$p.value
pvalue_res[i] <- wilcox_test(count ~ group_by, data = out1, paired = paired)$p
}

pvalue_cl <- data.frame(cluster = cluster_order, pvalue = pvalue_res)
Expand Down
33 changes: 21 additions & 12 deletions man/abVolPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions tests/testthat/test_visualization.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,65 @@ test_that("stackedPlot works as expected", {
# Cleanup: Remove the generated file
unlink("stacked_barplot_pbmc_small_disease.pdf")
})

test_that("abVolPlot works as expected", {
library(Seurat)
library(rstatix)
set.seed(123)
pbmc_small$predicted.id <- sample(c("Cluster1", "Cluster2"), ncol(pbmc_small), replace = TRUE)
pbmc_small$sample <- sample(c("Sample1", "Sample2"), ncol(pbmc_small), replace = TRUE)
pbmc_small$AIE_type <- sample(c("LGI1", "control"), ncol(pbmc_small), replace = TRUE)

# Run the function
suppressWarnings(
plot <- abVolPlot(
object = pbmc_small,
cluster_idents = "predicted.id",
sample = "sample",
cluster_order = c("Cluster1", "Cluster2"),
group_by = "AIE_type",
group1 = "LGI1",
group2 = "control",
color = c("Cluster1" = "blue", "Cluster2" = "red"),
width = 5,
height = 5,
dir_output = "."
)
)

# Test 1: Function creates a file in the correct directory
expect_true(file.exists("volcano_plot_predicted.id_pbmc_small_LGI1_control.pdf"))
# Test 2: Function creates a file that is not empty
expect_gt(file.info("volcano_plot_predicted.id_pbmc_small_LGI1_control.pdf")$size, 0)
# Test 3: Test plot objects
expect_s3_class(plot, "ggplot")
p <- ggplot2::ggplot_build(plot)
expect_equal(
p$layout$panel_params[[1]]$x$limits,
c(-1, 1)
)

# Test 4: Check if the function throws an error if object is not a Seurat object
expect_error(
abVolPlot(
object = data.frame(a = c(1:3)),
cluster_idents = "predicted.id",
sample = "sample",
cluster_order = c("Cluster1", "Cluster2"),
group_by = "AIE_type",
group1 = "LGI1",
group2 = "control",
color = c("Cluster1" = "blue", "Cluster2" = "red"),
width = 5,
height = 5,
dir_output = "."
),
"Object must be a Seurat object"
)

# Test 5: Check if the plot has the expected elements
expect_true("GeomPoint" %in% sapply(plot$layers, function(x) class(x$geom)[1]))

# Cleanup: Remove the generated file
unlink("volcano_plot_predicted.id_pbmc_small_LGI1_control.pdf")
})

0 comments on commit 3217b69

Please sign in to comment.