From 5972240b629296e9a0a2bb00c2147b2b11bd54ef Mon Sep 17 00:00:00 2001 From: xec-cm Date: Wed, 26 Jul 2023 16:50:39 +0200 Subject: [PATCH 1/2] closes #89 --- R/corncob.R | 71 +++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/R/corncob.R b/R/corncob.R index c328084..cc63ca8 100644 --- a/R/corncob.R +++ b/R/corncob.R @@ -213,28 +213,6 @@ run_corncob <- function(rec, fdr, log2FC, rarefy) { - - - ## Temporal solution to https://github.com/bryandmartin/corncob/issues/141 - # ver <- utils::packageVersion("detectseparation") - # if (ver != "0.2") { - # rlang::abort(c( - # "!" = glue::glue( - # "Temporarily the version of the package ", - # "{crayon::bgMagenta('detectseparation')} must be ", - # "{crayon::blue('v0.2')}, but you have the version ", - # "{crayon::blue(ver)} installed." - # ), - # "*" = glue::glue( - # "Please first run {crayon::blue('remove.packages(\"detectseparation\")')}.", - # ), - # "*" = glue::glue( - # "Finally install the necessary version with ", - # "{crayon::blue('devtools::install_version(\"detectseparation\", version = 0.2)')}." - # ) - # ), - # use_cli_format = TRUE) - # } phy <- get_phy(rec) vars <- get_var(rec) @@ -259,22 +237,41 @@ run_corncob <- function(rec, dplyr::pull(sample_id) %>% phyloseq::prune_samples(phy) - corncob_res <- corncob::differentialTest( - formula = glue::glue("~ { var }") %>% stats::formula(), - data = f_phy, - phi.formula = phi.formula, - formula_null = formula_null, - phi.formula_null = phi.formula_null, - link = link, - phi.link = phi.link, - test = test, - boot = boot, - B = B, - filter_discriminant = filter_discriminant, - fdr_cutoff = Inf, - fdr = fdr + corncob_res <- rlang::catch_cnd( + corncob::differentialTest( + formula = glue::glue("~ { var }") %>% stats::formula(), + data = f_phy, + phi.formula = phi.formula, + formula_null = formula_null, + phi.formula_null = phi.formula_null, + link = link, + phi.link = phi.link, + test = test, + boot = boot, + B = 10e4, + filter_discriminant = filter_discriminant, + fdr_cutoff = Inf, + fdr = fdr + ) ) - + + ## Skip error for no convergenc + if (is(corncob_res, "error")) { + if (stringr::str_detect(corncob_res$message, "failed to converge")) { + rlang::abort(c( + glue::glue("{crayon::bgMagenta('corncob')}: All models failed to converge!"), + glue::glue("{crayon::bgMagenta('corncob')}: If you are seeing this, it is likely that your model is overspecified. This occurs when your sample size is not large enough to estimate all the parameters of your model. This is most commonly due to categorical variables that include many categories."), + glue::glue("Please remove or edit the {crayon::bgMagenta('step_corncob()')} and rerun.") + )) + } else { + rlang::abort(c( + glue::glue("{crayon::bgMagenta('corncob')}: Internal error!"), + glue::glue("Please remove or edit the {crayon::bgMagenta('step_corncob()')} and rerun."), + "Please report this bug on GitHub: https://github.com/MicrobialGenomics-IrsicaixaOrg/dar/issues" + )) + } + } + signif_taxa <- corncob::otu_to_taxonomy( OTU = corncob_res$significant_taxa, data = corncob_res$data, From 77dc95dacfc315032f9a82fd15d2c028fb330afd Mon Sep 17 00:00:00 2001 From: xec-cm Date: Thu, 27 Jul 2023 09:32:20 +0200 Subject: [PATCH 2/2] Update corncob.R --- R/corncob.R | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/R/corncob.R b/R/corncob.R index cc63ca8..0eeb7d0 100644 --- a/R/corncob.R +++ b/R/corncob.R @@ -236,28 +236,29 @@ run_corncob <- function(rec, dplyr::filter(!!dplyr::sym(var) %in% comparison) %>% dplyr::pull(sample_id) %>% phyloseq::prune_samples(phy) - - corncob_res <- rlang::catch_cnd( - corncob::differentialTest( - formula = glue::glue("~ { var }") %>% stats::formula(), - data = f_phy, - phi.formula = phi.formula, - formula_null = formula_null, - phi.formula_null = phi.formula_null, - link = link, - phi.link = phi.link, - test = test, - boot = boot, - B = 10e4, - filter_discriminant = filter_discriminant, - fdr_cutoff = Inf, - fdr = fdr - ) - ) + corncob_res <- + tryCatch({ + corncob::differentialTest( + formula = glue::glue("~ { var }") %>% stats::formula(), + data = f_phy, + phi.formula = phi.formula, + formula_null = formula_null, + phi.formula_null = phi.formula_null, + link = link, + phi.link = phi.link, + test = test, + boot = boot, + B = 10e4, + filter_discriminant = filter_discriminant, + fdr_cutoff = Inf, + fdr = fdr + ) + }, error = function(e) { conditionMessage(e) }) + ## Skip error for no convergenc - if (is(corncob_res, "error")) { - if (stringr::str_detect(corncob_res$message, "failed to converge")) { + if (!is(corncob_res, "differentialTest")) { + if (stringr::str_detect(corncob_res, "failed to converge")) { rlang::abort(c( glue::glue("{crayon::bgMagenta('corncob')}: All models failed to converge!"), glue::glue("{crayon::bgMagenta('corncob')}: If you are seeing this, it is likely that your model is overspecified. This occurs when your sample size is not large enough to estimate all the parameters of your model. This is most commonly due to categorical variables that include many categories."),