Skip to content

Commit

Permalink
Merge pull request #5 from nearinj/master
Browse files Browse the repository at this point in the history
Returns the ggplot2 objects in the function maaslin_plot_results
  • Loading branch information
WillNickols authored Dec 2, 2024
2 parents 06e16f4 + ea1bbac commit accf620
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
19 changes: 10 additions & 9 deletions R/maaslin3.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -2363,7 +2364,7 @@ maaslin_plot_results <- function(output,
heatmap_vars <- trimws(unlist(strsplit(heatmap_vars, ',')))
}

maaslin3_summary_plot(
summary_plot <- maaslin3_summary_plot(
merged_results,
summary_plot_file,
figures_folder,
Expand All @@ -2375,6 +2376,7 @@ maaslin_plot_results <- function(output,
median_comparison_prevalence = median_comparison_prevalence,
balanced = balanced
)
ret_plots[["summary_plot"]] <- summary_plot
}

# Individual association plots
Expand Down Expand Up @@ -2410,9 +2412,9 @@ maaslin_plot_results <- function(output,
invokeRestart("muffleWarning")
})
})

return(plots_out)
ret_plots[["assocation_plots"]] <- plots_out
}
return(ret_plots)
}

maaslin_plot_results_from_output <- function(output,
Expand All @@ -2437,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)) {
Expand Down Expand Up @@ -2480,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,
Expand All @@ -2492,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
Expand Down Expand Up @@ -2567,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)
}

##########################################################
Expand Down
5 changes: 5 additions & 0 deletions R/viz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -736,6 +740,7 @@ maaslin3_summary_plot <-
"summary_plot_gg.RDS",
sep = ""))
}
return(ret_plots)
}

# Create a scatterplot for abundance vs. continuous associations
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_maaslin_plot_results.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test_maaslin_plot_results_from_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit accf620

Please sign in to comment.