From 5bf6d1a2ffdbe1da3eb71f2dfa1616448203a4b8 Mon Sep 17 00:00:00 2001 From: David Barnett Date: Thu, 20 Jul 2023 17:12:46 +0200 Subject: [PATCH] fix #119 and bump version to 0.10.11 --- DESCRIPTION | 2 +- NEWS.md | 4 +++ R/taxatree_stats_p_adjust.R | 41 +++++++++++++++++-------- README.md | 38 +++++++++++------------ man/taxatree_stats_p_adjust.Rd | 10 ++++-- tests/testthat/test-taxatree_plots.R | 46 ++++++++++++++++++++++++++-- 6 files changed, 104 insertions(+), 37 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0a1e341c..c60a0f92 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: microViz Title: Microbiome Data Analysis and Visualization -Version: 0.10.10 +Version: 0.10.11 Authors@R: person(given = "David", family = "Barnett", diff --git a/NEWS.md b/NEWS.md index 45bcabf7..6e87de2b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# microViz 0.10.11 + +- Fix: `taxatree_stats_p_adjust` can accept multiple grouping variables without specifying new_var. + # microViz 0.10.10 - Fix: `comp_barplot` taxon ordering is now correct when tax_level is set to "Taxon" diff --git a/R/taxatree_stats_p_adjust.R b/R/taxatree_stats_p_adjust.R index afba730a..09bf7f87 100644 --- a/R/taxatree_stats_p_adjust.R +++ b/R/taxatree_stats_p_adjust.R @@ -8,9 +8,12 @@ #' @details #' Define how to group the p values for adjustment with the `grouping` argument. #' The default is to adjust the p values in groups at each taxonomic rank, -#' but you could also adjust per "model" / "taxon" or per "term". -#' Or even group by a combination of rank and term with c("rank", "term") -#' +#' but you could also adjust per "taxon" or per "term". +#' Or even group by a combination of rank and term with c("rank", "term"). +#' You should specify the name of the new variable containing the adjusted +#' p values in the new_var argument. If left as NULL the new variable name will +#' be created by pasting together p.adj, the method, and the grouping variable(s) +#' separated by ".". #' #' @param data psExtra with taxatree_stats dataframe, or just the dataframe #' @param grouping @@ -95,19 +98,33 @@ taxatree_stats_p_adjust <- function(data, method, grouping = "rank", p = "p.value", - new_var = paste0( - "p.adj.", method, ".", grouping - )) { - if (is(data, "psExtra") && is.data.frame(data@taxatree_stats)) { + new_var = NULL) { + # Input checks + if (!rlang::is_string(method)) rlang::abort("method must be a string") + rlang::arg_match(method, values = stats::p.adjust.methods) + if (!is.character(grouping)) rlang::abort("grouping must be character") + if (!is.null(new_var) && !rlang::is_string(new_var)) { + rlang::abort("new_var must be NULL or a string") + } + if (!is(data, "psExtra") && !is.data.frame(data)) { + rlang::abort("data must be a psExtra or a taxatree_stats dataframe") + } + + # Extract and check stats data frame + if (is(data, "psExtra")) { + if (!is.data.frame(data@taxatree_stats)) { + stop("psExtra input must have stats dataframe in taxatree_stats slot") + } df <- data@taxatree_stats - } else if (inherits(data, "data.frame")) { - df <- data } else { - stop( - "data must be a psExtra with taxatree_stats or just the stats dataframe" - ) + df <- data } + # set default new variable name + if (is.null(new_var)) { + new_var <- paste0("p.adj.", method, ".", paste(grouping, collapse = ".")) + } + # change grouping variable names from alternative nomenclature if (identical(grouping, "model") || identical(grouping, "taxon_name")) { grouping <- "taxon" } diff --git a/README.md b/README.md index 97ac4d69..a6a82ff8 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ date)](https://img.shields.io/docker/v/barnettdavid/microviz-rocker-verse?color= status](https://david-barnett.r-universe.dev/badges/microViz)](https://david-barnett.r-universe.dev/ui#package:microViz) [![JOSS article](https://joss.theoj.org/papers/4547b492f224a26d96938ada81fee3fa/status.svg)](https://joss.theoj.org/papers/4547b492f224a26d96938ada81fee3fa) -[![Citations](https://img.shields.io/badge/Citations-~44-blueviolet)](https://scholar.google.com/scholar?oi=bibs&hl=en&cites=5439940108464463894) +[![Citations](https://img.shields.io/badge/Citations-~50-blueviolet)](https://scholar.google.com/scholar?oi=bibs&hl=en&cites=5439940108464463894) [![Zenodo DOI](https://zenodo.org/badge/307119750.svg)](https://zenodo.org/badge/latestdoi/307119750) @@ -331,8 +331,8 @@ aitchison_perm <- aitchison_dists %>% n_processes = 1, n_perms = 99, # you should use at least 999! variables = "bmi_group" ) -#> 2023-06-27 10:28:15.676676 - Starting PERMANOVA with 99 perms with 1 processes -#> 2023-06-27 10:28:15.743616 - Finished PERMANOVA +#> 2023-07-20 17:10:01.831556 - Starting PERMANOVA with 99 perms with 1 processes +#> 2023-07-20 17:10:01.889154 - Finished PERMANOVA # view the permanova results perm_get(aitchison_perm) %>% as.data.frame() @@ -357,8 +357,8 @@ your permanova directly using the `ord_plot` function with constraints perm2 <- aitchison_dists %>% dist_permanova(variables = c("weight", "african", "sex"), seed = 321) #> Dropping samples with missings: 2 -#> 2023-06-27 10:28:15.754227 - Starting PERMANOVA with 999 perms with 1 processes -#> 2023-06-27 10:28:17.410055 - Finished PERMANOVA +#> 2023-07-20 17:10:01.900323 - Starting PERMANOVA with 999 perms with 1 processes +#> 2023-07-20 17:10:03.569987 - Finished PERMANOVA ``` We’ll visualise the effect of nationality and bodyweight on sample @@ -452,7 +452,7 @@ and by participating in this project you agree to abide by its terms. sessionInfo() #> R version 4.3.0 (2023-04-21) #> Platform: aarch64-apple-darwin20 (64-bit) -#> Running under: macOS Ventura 13.3.1 +#> Running under: macOS Ventura 13.4.1 #> #> Matrix products: default #> BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib @@ -469,27 +469,27 @@ sessionInfo() #> #> other attached packages: #> [1] ggplot2_3.4.2 dplyr_1.1.2 phyloseq_1.44.0 microViz_0.10.10 -#> [5] testthat_3.1.8 devtools_2.4.5 usethis_2.2.0 +#> [5] testthat_3.1.10 devtools_2.4.5 usethis_2.2.2 #> #> loaded via a namespace (and not attached): -#> [1] RColorBrewer_1.1-3 shape_1.4.6 rstudioapi_0.14 -#> [4] jsonlite_1.8.5 magrittr_2.0.3 farver_2.1.1 -#> [7] rmarkdown_2.22 GlobalOptions_0.1.2 fs_1.6.2 +#> [1] RColorBrewer_1.1-3 shape_1.4.6 rstudioapi_0.15.0 +#> [4] jsonlite_1.8.7 magrittr_2.0.3 farver_2.1.1 +#> [7] rmarkdown_2.23 GlobalOptions_0.1.2 fs_1.6.2 #> [10] zlibbioc_1.46.0 vctrs_0.6.3 multtest_2.56.0 #> [13] memoise_2.0.1 Cairo_1.6-0 RCurl_1.98-1.12 #> [16] htmltools_0.5.5 curl_5.0.1 Rhdf5lib_1.22.0 #> [19] rhdf5_2.44.0 htmlwidgets_1.6.2 plyr_1.8.8 -#> [22] cachem_1.0.8 commonmark_1.9.0 igraph_1.4.3 +#> [22] cachem_1.0.8 commonmark_1.9.0 igraph_1.5.0 #> [25] mime_0.12 lifecycle_1.0.3 iterators_1.0.14 -#> [28] pkgconfig_2.0.3 Matrix_1.5-4.1 R6_2.5.1 +#> [28] pkgconfig_2.0.3 Matrix_1.6-0 R6_2.5.1 #> [31] fastmap_1.1.1 clue_0.3-64 GenomeInfoDbData_1.2.10 -#> [34] shiny_1.7.4 digest_0.6.31 selectr_0.4-2 +#> [34] shiny_1.7.4.1 digest_0.6.33 selectr_0.4-2 #> [37] colorspace_2.1-0 S4Vectors_0.38.1 ps_1.7.5 -#> [40] pkgload_1.3.2 seriation_1.4.2 vegan_2.6-4 +#> [40] pkgload_1.3.2.1 seriation_1.4.2 vegan_2.6-4 #> [43] labeling_0.4.2 fansi_1.0.4 httr_1.4.6 -#> [46] mgcv_1.8-42 compiler_4.3.0 remotes_2.4.2 +#> [46] mgcv_1.9-0 compiler_4.3.0 remotes_2.4.2.1 #> [49] doParallel_1.0.17 withr_2.5.0 viridis_0.6.3 -#> [52] pkgbuild_1.4.0 highr_0.10 MASS_7.3-60 +#> [52] pkgbuild_1.4.2 highr_0.10 MASS_7.3-60 #> [55] sessioninfo_1.2.2 rjson_0.2.21 biomformat_1.28.0 #> [58] permute_0.9-7 tools_4.3.0 ape_5.7-1 #> [61] httpuv_1.6.11 glue_1.6.2 callr_3.7.3 @@ -498,19 +498,19 @@ sessionInfo() #> [70] cluster_2.1.4 reshape2_1.4.4 ade4_1.7-22 #> [73] generics_0.1.3 gtable_0.3.3 microbiome_1.22.0 #> [76] ca_0.71.1 tidyr_1.3.0 data.table_1.14.8 -#> [79] xml2_1.3.4 utf8_1.2.3 XVector_0.40.0 +#> [79] xml2_1.3.5 utf8_1.2.3 XVector_0.40.0 #> [82] BiocGenerics_0.46.0 markdown_1.7 foreach_1.5.2 #> [85] pillar_1.9.0 stringr_1.5.0 later_1.3.1 #> [88] circlize_0.4.15 splines_4.3.0 ggtext_0.1.2 #> [91] lattice_0.21-8 corncob_0.3.1 survival_3.5-5 #> [94] tidyselect_1.2.0 registry_0.5-1 ComplexHeatmap_2.16.0 -#> [97] Biostrings_2.68.0 miniUI_0.1.1.1 knitr_1.42 +#> [97] Biostrings_2.68.0 miniUI_0.1.1.1 knitr_1.43 #> [100] gridExtra_2.3 IRanges_2.34.0 stats4_4.3.0 #> [103] xfun_0.39 Biobase_2.60.0 matrixStats_1.0.0 #> [106] brio_1.1.3 stringi_1.7.12 yaml_2.3.7 #> [109] evaluate_0.21 codetools_0.2-19 tibble_3.2.1 #> [112] cli_3.6.1 xtable_1.8-4 munsell_0.5.0 -#> [115] processx_3.8.1 Rcpp_1.0.10 GenomeInfoDb_1.36.0 +#> [115] processx_3.8.2 Rcpp_1.0.11 GenomeInfoDb_1.36.0 #> [118] png_0.1-8 parallel_4.3.0 ellipsis_0.3.2 #> [121] prettyunits_1.1.1 profvis_0.3.8 urlchecker_1.0.1 #> [124] bitops_1.0-7 viridisLite_0.4.2 scales_1.2.1 diff --git a/man/taxatree_stats_p_adjust.Rd b/man/taxatree_stats_p_adjust.Rd index d2d729f0..53f11652 100644 --- a/man/taxatree_stats_p_adjust.Rd +++ b/man/taxatree_stats_p_adjust.Rd @@ -9,7 +9,7 @@ taxatree_stats_p_adjust( method, grouping = "rank", p = "p.value", - new_var = paste0("p.adj.", method, ".", grouping) + new_var = NULL ) } \arguments{ @@ -35,8 +35,12 @@ or more conservative family-wise error rate controlling methods such as "holm" o \details{ Define how to group the p values for adjustment with the \code{grouping} argument. The default is to adjust the p values in groups at each taxonomic rank, -but you could also adjust per "model" / "taxon" or per "term". -Or even group by a combination of rank and term with c("rank", "term") +but you could also adjust per "taxon" or per "term". +Or even group by a combination of rank and term with c("rank", "term"). +You should specify the name of the new variable containing the adjusted +p values in the new_var argument. If left as NULL the new variable name will +be created by pasting together p.adj, the method, and the grouping variable(s) +separated by ".". } \examples{ # This example is an abbreviated excerpt from article on taxon modelling on diff --git a/tests/testthat/test-taxatree_plots.R b/tests/testthat/test-taxatree_plots.R index 69a2ccd8..ea5d9bd7 100644 --- a/tests/testthat/test-taxatree_plots.R +++ b/tests/testthat/test-taxatree_plots.R @@ -40,7 +40,50 @@ lm_models <- phylo %>% # convert models to stats lm_stats <- taxatree_models2stats(lm_models) -# test plotting errors +# Test taxatree_stats_p_adjust input checks +test_that("taxatree_stats_p_adjust rejects invalid grouping argument", { + expect_error( + taxatree_stats_p_adjust(lm_stats, method = "BH", grouping = 1), + regexp = "grouping must be character" + ) +}) +test_that("taxatree_stats_p_adjust rejects invalid new_var argument", { + expect_error( + taxatree_stats_p_adjust(lm_stats, method = "BH", new_var = c("A", "B")), + regexp = "new_var must be NULL or a string" + ) +}) +test_that("taxatree_stats_p_adjust rejects invalid data argument", { + expect_error( + taxatree_stats_p_adjust(data = phylo, method = "BH"), + regexp = "data must be a psExtra or a taxatree_stats dataframe" + ) +}) +test_that("taxatree_stats_p_adjust rejects invalid method argument", { + expect_error( + taxatree_stats_p_adjust(data = lm_stats, method = "bh"), + regexp = "`method` must be one of \"holm\", \"hochberg\"," + ) + expect_error( + taxatree_stats_p_adjust(data = lm_stats, method = c("BH", "holm")), + regexp = "method must be a string" + ) +}) +# test basic taxatree_stats_p_adjust functionality +test_that("taxatree_stats_p_adjust works correctly with default grouping", { + result <- taxatree_stats_p_adjust(lm_stats, method = "BH") + expect_true("p.adj.BH.rank" %in% colnames(taxatree_stats_get(result))) +}) +test_that("taxatree_stats_p_adjust works correctly with 2 variable grouping", { + result <- taxatree_stats_p_adjust(lm_stats, method = "BH", grouping = c("rank", "term")) + expect_true("p.adj.BH.rank.term" %in% colnames(taxatree_stats_get(result))) +}) +test_that("taxatree_stats_p_adjust works correctly with provided new_var", { + result <- taxatree_stats_p_adjust(lm_stats, method = "BH", new_var = "new_p") + expect_true("new_p" %in% colnames(taxatree_stats_get(result))) +}) + +# test taxatree_plots plotting errors test_that("taxatree_plots throw informative errors", { expect_error( object = lm_models %>% taxatree_plots(), @@ -59,7 +102,6 @@ test_that("taxatree_plots throw informative errors", { ) }) - test_that("taxatree_plot plotting works", { local_edition(3) lm_plots <- taxatree_plots(lm_stats)