diff --git a/DESCRIPTION b/DESCRIPTION index 967c91ccc..26182f899 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 2.1.1.9147 -Date: 2025-02-14 +Version: 2.1.1.9148 +Date: 2025-02-15 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) data analysis and to work with microbial and antimicrobial properties by diff --git a/NEWS.md b/NEWS.md index 67cbac601..151fa5e78 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 2.1.1.9147 +# AMR 2.1.1.9148 *(this beta version will eventually become v3.0. We're happy to reach a new major milestone soon, which will be all about the new One Health support! Install this beta using [the instructions here](https://msberends.github.io/AMR/#latest-development-version).)* diff --git a/PythonPackage/AMR/AMR.egg-info/PKG-INFO b/PythonPackage/AMR/AMR.egg-info/PKG-INFO index 50835e462..e17678be9 100644 --- a/PythonPackage/AMR/AMR.egg-info/PKG-INFO +++ b/PythonPackage/AMR/AMR.egg-info/PKG-INFO @@ -1,6 +1,6 @@ Metadata-Version: 2.2 Name: AMR -Version: 2.1.1.9147 +Version: 2.1.1.9148 Summary: A Python wrapper for the AMR R package Home-page: https://github.com/msberends/AMR Author: Matthijs Berends diff --git a/PythonPackage/AMR/dist/AMR-2.1.1.9147-py3-none-any.whl b/PythonPackage/AMR/dist/AMR-2.1.1.9148-py3-none-any.whl similarity index 52% rename from PythonPackage/AMR/dist/AMR-2.1.1.9147-py3-none-any.whl rename to PythonPackage/AMR/dist/AMR-2.1.1.9148-py3-none-any.whl index ea82d530e..768ffcf4b 100644 Binary files a/PythonPackage/AMR/dist/AMR-2.1.1.9147-py3-none-any.whl and b/PythonPackage/AMR/dist/AMR-2.1.1.9148-py3-none-any.whl differ diff --git a/PythonPackage/AMR/dist/amr-2.1.1.9147.tar.gz b/PythonPackage/AMR/dist/amr-2.1.1.9147.tar.gz deleted file mode 100644 index cceae66c4..000000000 Binary files a/PythonPackage/AMR/dist/amr-2.1.1.9147.tar.gz and /dev/null differ diff --git a/PythonPackage/AMR/dist/amr-2.1.1.9148.tar.gz b/PythonPackage/AMR/dist/amr-2.1.1.9148.tar.gz new file mode 100644 index 000000000..89ae78ab1 Binary files /dev/null and b/PythonPackage/AMR/dist/amr-2.1.1.9148.tar.gz differ diff --git a/PythonPackage/AMR/setup.py b/PythonPackage/AMR/setup.py index dc2ecc627..5d72d1b42 100644 --- a/PythonPackage/AMR/setup.py +++ b/PythonPackage/AMR/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages setup( name='AMR', - version='2.1.1.9147', + version='2.1.1.9148', packages=find_packages(), install_requires=[ 'rpy2', diff --git a/R/aa_globals.R b/R/aa_globals.R index baa388825..479d56016 100755 --- a/R/aa_globals.R +++ b/R/aa_globals.R @@ -179,12 +179,16 @@ globalVariables(c( "microorganisms.codes", "mo", "n", + "n_susceptible", + "n_tested", + "n_total", "name", "new", "numerator", "observations", "old", "old_name", + "p_susceptible", "pattern", "R", "rank_index", diff --git a/R/antibiogram.R b/R/antibiogram.R index e122605f2..4bd4292da 100755 --- a/R/antibiogram.R +++ b/R/antibiogram.R @@ -441,7 +441,7 @@ antibiogram.default <- function(x, x <- ascertain_sir_classes(x, "x") meet_criteria(wisca, allow_class = "logical", has_length = 1) if (isTRUE(wisca)) { - if (!missing(mo_transform)) { + if (!is.null(mo_transform)) { warning_("WISCA must be based on the species level as WISCA parameters are based on this. For that reason, `mo_transform` will be ignored.") } mo_transform <- function(x) suppressMessages(suppressWarnings(paste(mo_genus(x, keep_synonyms = TRUE, language = NULL), mo_species(x, keep_synonyms = TRUE, language = NULL)))) @@ -469,7 +469,7 @@ antibiogram.default <- function(x, # try to find columns based on type if (is.null(col_mo)) { - col_mo <- search_type_in_df(x = x, type = "mo", info = interactive()) + col_mo <- search_type_in_df(x = x, type = "mo", info = info) stop_if(is.null(col_mo), "`col_mo` must be set") } # transform MOs @@ -594,7 +594,7 @@ antibiogram.default <- function(x, } if (all(out$n_tested < minimum, na.rm = TRUE) && wisca == FALSE) { warning_("All combinations had less than `minimum = ", minimum, "` results, returning an empty antibiogram") - return(as_original_data_class(data.frame(), class(out), extra_class = "antibiogram")) + return(as_original_data_class(data.frame(), class(x), extra_class = "antibiogram")) } else if (any(out$n_tested < minimum, na.rm = TRUE)) { out <- out %pm>% # also for WISCA, refrain from anything below 15 isolates: @@ -612,7 +612,7 @@ antibiogram.default <- function(x, } if (NROW(out) == 0) { - return(as_original_data_class(data.frame(), class(out), extra_class = "antibiogram")) + return(as_original_data_class(data.frame(), class(x), extra_class = "antibiogram")) } out$p_susceptible <- out$n_susceptible / out$n_tested @@ -927,7 +927,6 @@ antibiogram.default <- function(x, rownames(out) <- NULL rownames(wisca_parameters) <- NULL rownames(long_numeric) <- NULL - structure(out, has_syndromic_group = has_syndromic_group, combine_SI = combine_SI, @@ -943,7 +942,7 @@ antibiogram.default <- function(x, #' @export antibiogram.grouped_df <- function(x, antibiotics = where(is.sir), - mo_transform = function (...) "no_mo", + mo_transform = NULL, ab_transform = "name", syndromic_group = NULL, add_total_n = FALSE, @@ -960,6 +959,7 @@ antibiogram.grouped_df <- function(x, conf_interval = 0.95, interval_side = "two-tailed", info = interactive()) { + stop_ifnot(is.null(mo_transform), "`mo_transform` must not be set if creating an antibiogram using a grouped tibble. The groups will become the variables over which the antimicrobials are calculated, which could include the pathogen information (though not necessary). Nonetheless, this makes `mo_transform` redundant.", call = FALSE) stop_ifnot(is.null(syndromic_group), "`syndromic_group` must not be set if creating an antibiogram using a grouped tibble. The groups will become the variables over which the antimicrobials are calculated, making `syndromic_groups` redundant.", call = FALSE) groups <- attributes(x)$groups n_groups <- NROW(groups) @@ -969,16 +969,19 @@ antibiogram.grouped_df <- function(x, title = paste("Calculating AMR for", n_groups, "groups")) on.exit(close(progress)) + out <- NULL + wisca_parameters <- NULL + long_numeric <- NULL + for (i in seq_len(n_groups)) { - if (i > 1) progress$tick() + progress$tick() rows <- unlist(groups[i, ]$.rows) if (length(rows) == 0) { next } - new_out <- antibiogram(as.data.frame(x)[rows, , drop = FALSE], antibiotics = antibiotics, - mo_transform = function(x) "no_mo", + mo_transform = NULL, ab_transform = ab_transform, syndromic_group = NULL, add_total_n = add_total_n, @@ -994,17 +997,15 @@ antibiogram.grouped_df <- function(x, simulations = simulations, conf_interval = conf_interval, interval_side = interval_side, - info = i == 1 && info == TRUE) + info = FALSE) new_wisca_parameters <- attributes(new_out)$wisca_parameters new_long_numeric <- attributes(new_out)$long_numeric - if (i == 1) progress$tick() - if (NROW(new_out) == 0) { next } - # remove first column 'Pathogen' (in whatever language) + # remove first column 'Pathogen' (in whatever language), except WISCA since that never has Pathogen column if (isFALSE(wisca)) { new_out <- new_out[, -1, drop = FALSE] new_long_numeric <- new_long_numeric[, -1, drop = FALSE] @@ -1037,17 +1038,17 @@ antibiogram.grouped_df <- function(x, long_numeric <- rbind_AMR(long_numeric, new_long_numeric) } } - + close(progress) - out <- structure(as_original_data_class(out, class(x), extra_class = "antibiogram"), - has_syndromic_group = FALSE, - combine_SI = isTRUE(combine_SI), - wisca = isTRUE(wisca), - conf_interval = conf_interval, - formatting_type = formatting_type, - wisca_parameters = as_original_data_class(wisca_parameters, class(x)), - long_numeric = as_original_data_class(long_numeric, class(x))) + structure(as_original_data_class(out, class(x), extra_class = "antibiogram"), + has_syndromic_group = FALSE, + combine_SI = isTRUE(combine_SI), + wisca = isTRUE(wisca), + conf_interval = conf_interval, + formatting_type = formatting_type, + wisca_parameters = as_original_data_class(wisca_parameters, class(x)), + long_numeric = as_original_data_class(long_numeric, class(x))) } #' @export @@ -1072,6 +1073,7 @@ wisca <- function(x, antibiogram(x = x, antibiotics = antibiotics, ab_transform = ab_transform, + mo_transform = NULL, syndromic_group = syndromic_group, add_total_n = add_total_n, only_all_tested = only_all_tested, diff --git a/R/plotting.R b/R/plotting.R index 691f99407..d7255e4e8 100755 --- a/R/plotting.R +++ b/R/plotting.R @@ -227,10 +227,16 @@ #' } NULL -create_scale_mic <- function(aest, keep_operators, mic_range, ...) { +create_scale_mic <- function(aest, keep_operators, mic_range = NULL, ...) { ggplot_fn <- getExportedValue(paste0("scale_", aest, "_continuous"), ns = asNamespace("ggplot2")) args <- list(...) + breaks_set <- args$breaks + if (!is.null(args$limits)) { + stop_ifnot(is.null(mic_range), + "In `scale_", aest, "_mic()`, `limits` cannot be combined with `mic_range`, as they working identically. Use `mic_range` OR `limits`.", call = FALSE) + mic_range <- args$limits + } # do not take these arguments into account, as they will be overwritten and seem to allow weird behaviour args[c("aesthetics", "trans", "transform", "transform_df", "breaks", "labels", "limits")] <- NULL scale <- do.call(ggplot_fn, args) @@ -252,8 +258,30 @@ create_scale_mic <- function(aest, keep_operators, mic_range, ...) { df[[aest]] <- self$`.values_log` df } - scale$breaks <- function(..., self) log2(as.mic(self$`.values_levels`)) - scale$labels <- function(..., self) self$`.values_levels` + scale$breaks <- function(..., self) { + if (!is.null(breaks_set)) { + if (is.function(breaks_set)) { + breaks_set(...) + } else { + log2(as.mic(breaks_set)) + } + } else { + log2(as.mic(self$`.values_levels`)) + } + } + scale$labels <- function(..., self) { + if (is.null(breaks_set)) { + self$`.values_levels` + } else { + breaks <- tryCatch(scale$breaks(), error = function(e) NULL) + if (!is.null(breaks)) { + # for when breaks are set by the user + 2 ^ breaks + } else { + self$`.values_levels` + } + } + } scale$limits <- function(x, ..., self) { rng <- range(log2(as.mic(self$`.values_levels`))) # add 0.5 extra space diff --git a/data-raw/gpt_training_text_v2.1.1.9147.txt b/data-raw/gpt_training_text_v2.1.1.9148.txt similarity index 99% rename from data-raw/gpt_training_text_v2.1.1.9147.txt rename to data-raw/gpt_training_text_v2.1.1.9148.txt index e50f29ceb..ab6abb81e 100644 --- a/data-raw/gpt_training_text_v2.1.1.9147.txt +++ b/data-raw/gpt_training_text_v2.1.1.9148.txt @@ -1,6 +1,6 @@ This knowledge base contains all context you must know about the AMR package for R. You are a GPT trained to be an assistant for the AMR package in R. You are an incredible R specialist, especially trained in this package and in the tidyverse. -First and foremost, you are trained on version 2.1.1.9147. Remember this whenever someone asks which AMR package version you’re at. +First and foremost, you are trained on version 2.1.1.9148. Remember this whenever someone asks which AMR package version you’re at. Below are the contents of the file, the file, and all the files (documentation) in the package. Every file content is split using 100 hypens. ---------------------------------------------------------------------------------------------------- diff --git a/tests/testthat/test-antibiogram.R b/tests/testthat/test-antibiogram.R index 8725436c1..853e1628c 100644 --- a/tests/testthat/test-antibiogram.R +++ b/tests/testthat/test-antibiogram.R @@ -104,7 +104,7 @@ ab8 <- suppressWarnings(antibiogram(example_isolates, wisca = TRUE)) expect_inherits(ab8, "antibiogram") -expect_equal(colnames(ab8), c("Pathogen", "Piperacillin/tazobactam", "Piperacillin/tazobactam + Gentamicin", "Piperacillin/tazobactam + Tobramycin")) +expect_equal(colnames(ab8), c("Piperacillin/tazobactam", "Piperacillin/tazobactam + Gentamicin", "Piperacillin/tazobactam + Tobramycin")) # grouped tibbles @@ -128,7 +128,7 @@ expect_silent(plot(ab5)) expect_silent(plot(ab6)) expect_silent(plot(ab7)) expect_silent(plot(ab8)) -expect_error(plot(ab9)) +expect_silent(plot(ab9)) if (AMR:::pkg_is_available("ggplot2")) { expect_inherits(ggplot2::autoplot(ab1), "gg") @@ -139,5 +139,5 @@ if (AMR:::pkg_is_available("ggplot2")) { expect_inherits(ggplot2::autoplot(ab6), "gg") expect_inherits(ggplot2::autoplot(ab7), "gg") expect_inherits(ggplot2::autoplot(ab8), "gg") - expect_error(ggplot2::autoplot(ab9)) + expect_inherits(ggplot2::autoplot(ab9), "gg") }