From a9fdd363450a154ed24b4e8e806ba568abb65d6d Mon Sep 17 00:00:00 2001 From: "Jacob T. Nearing" Date: Tue, 26 Nov 2024 16:53:58 -0500 Subject: [PATCH 1/4] Returns the ggplot2 objects in the function maaslin_plot_results --- R/maaslin3.R | 9 +++++---- R/viz.R | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/R/maaslin3.R b/R/maaslin3.R index 6b2bb23..9055783 100755 --- a/R/maaslin3.R +++ b/R/maaslin3.R @@ -2326,6 +2326,7 @@ maaslin_plot_results <- function(output, plot_associations = TRUE, max_pngs = 30, balanced = FALSE) { + ret_plots <- list() # create an output folder and figures folder if it does not exist if (!file.exists(output)) { logging::loginfo("Creating output folder") @@ -2363,7 +2364,7 @@ maaslin_plot_results <- function(output, heatmap_vars <- trimws(unlist(strsplit(heatmap_vars, ','))) } - maaslin3_summary_plot( + sum_plot <- maaslin3_summary_plot( merged_results, summary_plot_file, figures_folder, @@ -2376,7 +2377,7 @@ maaslin_plot_results <- function(output, balanced = balanced ) } - + ret_plots[["Summary_plot"]] <- sum_plot # Individual association plots if (plot_associations) { logging::loginfo( @@ -2410,9 +2411,9 @@ maaslin_plot_results <- function(output, invokeRestart("muffleWarning") }) }) - - return(plots_out) + ret_plots[["Assocations"]] <- plots_out } + return(ret_plots) } maaslin_plot_results_from_output <- function(output, diff --git a/R/viz.R b/R/viz.R index 10232a5..dce6af9 100644 --- a/R/viz.R +++ b/R/viz.R @@ -473,6 +473,7 @@ maaslin3_summary_plot <- median_comparison_abundance = FALSE, median_comparison_prevalence = FALSE, balanced=FALSE) { + ret_plots <- list() if (first_n > 200) { logging::logerror( paste( @@ -686,6 +687,9 @@ maaslin3_summary_plot <- } else { final_plot <- NULL } + ret_plots[["coefficient"]] <- p1 + ret_plots[["heat"]] <- p2 + ret_plots[["final"]] <- final_plot # Save plot if (!is.null(final_plot)) { @@ -731,6 +735,7 @@ maaslin3_summary_plot <- }) }) } + return(ret_plots) } # Create a scatterplot for abundance vs. continuous associations From 4141c8555c1231d5c6c49c09a07816c1a0879494 Mon Sep 17 00:00:00 2001 From: Will Nickols <78048944+WillNickols@users.noreply.github.com> Date: Mon, 2 Dec 2024 09:11:42 -0500 Subject: [PATCH 2/4] Update maaslin3.R --- R/maaslin3.R | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/R/maaslin3.R b/R/maaslin3.R index 9055783..faaead6 100755 --- a/R/maaslin3.R +++ b/R/maaslin3.R @@ -2364,7 +2364,7 @@ maaslin_plot_results <- function(output, heatmap_vars <- trimws(unlist(strsplit(heatmap_vars, ','))) } - sum_plot <- maaslin3_summary_plot( + summary_plot <- maaslin3_summary_plot( merged_results, summary_plot_file, figures_folder, @@ -2376,8 +2376,9 @@ maaslin_plot_results <- function(output, median_comparison_prevalence = median_comparison_prevalence, balanced = balanced ) + ret_plots[["summary_plot"]] <- summary_plot } - ret_plots[["Summary_plot"]] <- sum_plot + # Individual association plots if (plot_associations) { logging::loginfo( @@ -2411,7 +2412,7 @@ maaslin_plot_results <- function(output, invokeRestart("muffleWarning") }) }) - ret_plots[["Assocations"]] <- plots_out + ret_plots[["assocation_plots"]] <- plots_out } return(ret_plots) } @@ -2438,6 +2439,7 @@ maaslin_plot_results_from_output <- function(output, plot_associations = TRUE, max_pngs = 30, balanced = FALSE) { + ret_plots <- list() # create an output folder and figures folder if it does not exist if (!file.exists(output)) { @@ -2481,7 +2483,7 @@ maaslin_plot_results_from_output <- function(output, heatmap_vars <- trimws(unlist(strsplit(heatmap_vars, ','))) } - maaslin3_summary_plot( + summary_plot <- maaslin3_summary_plot( merged_results, summary_plot_file, figures_folder, @@ -2493,6 +2495,7 @@ maaslin_plot_results_from_output <- function(output, median_comparison_prevalence = median_comparison_prevalence, balanced = balanced ) + ret_plots[["summary_plot"]] <- summary_plot } # Individual associations @@ -2568,17 +2571,14 @@ maaslin_plot_results_from_output <- function(output, invokeRestart("muffleWarning") }) }) - - - } else { - plots_out <- NULL + ret_plots[["assocation_plots"]] <- plots_out } if ('logging::writeToFile' %in% names(logging::getLogger()[['handlers']])) { logging::removeHandler('logging::writeToFile') } - return(plots_out) + return(ret_plots) } ########################################################## From bbb59f368ddcc731ede8c14a9f76c06604ded445 Mon Sep 17 00:00:00 2001 From: Will Nickols <78048944+WillNickols@users.noreply.github.com> Date: Mon, 2 Dec 2024 09:19:26 -0500 Subject: [PATCH 3/4] Update test_maaslin_plot_results.R --- tests/testthat/test_maaslin_plot_results.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test_maaslin_plot_results.R b/tests/testthat/test_maaslin_plot_results.R index 2d139cf..7fa22f0 100644 --- a/tests/testthat/test_maaslin_plot_results.R +++ b/tests/testthat/test_maaslin_plot_results.R @@ -38,7 +38,7 @@ plot_out <- maaslin_plot_results(output = output_tmp, median_comparison_abundance = FALSE, max_significance = 0.1) -expect_is(plot_out$var1$a$logistic, 'ggplot') +expect_is(plot_out$assocation_plots$var1$a$logistic, 'ggplot') expect_equal(list.files(file.path(output_tmp, 'figures', 'association_plots', 'var1', 'logistic')), From ea1bbac558ab42062c0e91c93b641f900eb2a658 Mon Sep 17 00:00:00 2001 From: Will Nickols <78048944+WillNickols@users.noreply.github.com> Date: Mon, 2 Dec 2024 09:20:08 -0500 Subject: [PATCH 4/4] Update test_maaslin_plot_results_from_output.R --- tests/testthat/test_maaslin_plot_results_from_output.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test_maaslin_plot_results_from_output.R b/tests/testthat/test_maaslin_plot_results_from_output.R index 678f1eb..bf1c34c 100644 --- a/tests/testthat/test_maaslin_plot_results_from_output.R +++ b/tests/testthat/test_maaslin_plot_results_from_output.R @@ -65,11 +65,11 @@ plot_out2 <- maaslin_plot_results(output = output_tmp, median_comparison_abundance = FALSE, max_significance = 0.1) -expect_equal(plot_out$var1$a$logistic$data, plot_out2$var1$a$logistic$data) -expect_equal(plot_out$var1$a$logistic$mapping, - plot_out2$var1$a$logistic$mapping) -expect_equal(plot_out$var1$a$logistic$scales, - plot_out2$var1$a$logistic$scales) +expect_equal(plot_out$assocation_plots$var1$a$logistic$data, plot_out2$assocation_plots$var1$a$logistic$data) +expect_equal(plot_out$assocation_plots$var1$a$logistic$mapping, + plot_out$assocation_plots2$var1$a$logistic$mapping) +expect_equal(plot_out$assocation_plots$var1$a$logistic$scales, + plot_out2$assocation_plots$var1$a$logistic$scales) unlink(output_tmp, recursive = T)