1
0
mirror of https://github.com/msberends/AMR.git synced 2025-07-08 07:51:57 +02:00

(v0.7.1.9059) bug_drug_combinations

This commit is contained in:
2019-08-25 22:53:22 +02:00
parent 7c069145ac
commit e46cb0399f
23 changed files with 1081 additions and 230 deletions

View File

@ -21,9 +21,10 @@
#' Check availability of columns
#'
#' Easy check for availability of columns in a data set. This makes it easy to get an idea of which antibiotic combination can be used for calculation with e.g. \code{\link{portion_IR}}.
#' Easy check for availability of columns in a data set. This makes it easy to get an idea of which antimicrobial combination can be used for calculation with e.g. \code{\link{portion_R}}.
#' @param tbl a \code{data.frame} or \code{list}
#' @param width number of characters to present the visual availability, defaults to filling the width of the console
#' @details The function returns a \code{data.frame} with columns \code{"resistant"} and \code{"visual_resistance"}. The values in that columns are calculated with \code{\link{portion_R}}.
#' @return \code{data.frame} with column names of \code{tbl} as row names
#' @inheritSection AMR Read more on our website!
#' @export
@ -44,10 +45,10 @@
availability <- function(tbl, width = NULL) {
x <- base::sapply(tbl, function(x) { 1 - base::sum(base::is.na(x)) / base::length(x) })
n <- base::sapply(tbl, function(x) base::length(x[!base::is.na(x)]))
IR <- base::sapply(tbl, function(x) base::ifelse(is.rsi(x), portion_IR(x, minimum = 0), NA))
IR_print <- character(length(IR))
IR_print[!is.na(IR)] <- percent(IR[!is.na(IR)], round = 1, force_zero = TRUE)
IR_print[is.na(IR)] <- ""
R <- base::sapply(tbl, function(x) base::ifelse(is.rsi(x), portion_R(x, minimum = 0), NA))
R_print <- character(length(R))
R_print[!is.na(R)] <- percent(R[!is.na(R)], round = 1, force_zero = TRUE)
R_print[is.na(R)] <- ""
if (is.null(width)) {
width <- options()$width -
@ -63,14 +64,14 @@ availability <- function(tbl, width = NULL) {
width <- width / 2
}
if (length(IR[is.na(IR)]) == ncol(tbl)) {
if (length(R[is.na(R)]) == ncol(tbl)) {
width <- width * 2 + 10
}
x_chars_IR <- strrep("#", round(width * IR, digits = 2))
x_chars_S <- strrep("-", width - nchar(x_chars_IR))
vis_resistance <- paste0("|", x_chars_IR, x_chars_S, "|")
vis_resistance[is.na(IR)] <- ""
x_chars_R <- strrep("#", round(width * R, digits = 2))
x_chars_SI <- strrep("-", width - nchar(x_chars_R))
vis_resistance <- paste0("|", x_chars_R, x_chars_SI, "|")
vis_resistance[is.na(R)] <- ""
x_chars <- strrep("#", round(x, digits = 2) / (1 / width))
x_chars_empty <- strrep("-", width - nchar(x_chars))
@ -78,9 +79,9 @@ availability <- function(tbl, width = NULL) {
df <- data.frame(count = n,
available = percent(x, round = 1, force_zero = TRUE),
visual_availabilty = paste0("|", x_chars, x_chars_empty, "|"),
resistant = IR_print,
resistant = R_print,
visual_resistance = vis_resistance)
if (length(IR[is.na(IR)]) == ncol(tbl)) {
if (length(R[is.na(R)]) == ncol(tbl)) {
df[,1:3]
} else {
df

107
R/bug_drug_combinations.R Normal file
View File

@ -0,0 +1,107 @@
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# #
# This R package was created for academic research and was publicly #
# released in the hope that it will be useful, but it comes WITHOUT #
# ANY WARRANTY OR LIABILITY. #
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
#' Determine bug-drug combinations
#'
#' Determine antimicrobial resistance (AMR) of all bug-drug combinations in your data set where at least 30 (default) isolates are available per species. Use \code{format} on the result to prettify it to a printable format, see Examples.
#' @inheritParams eucast_rules
#' @inheritParams rsi_df
#' @importFrom dplyr rename
#' @importFrom tidyr spread
#' @importFrom clean freq
#' @export
#' @source \strong{M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition}, 2014, \emph{Clinical and Laboratory Standards Institute (CLSI)}. \url{https://clsi.org/standards/products/microbiology/documents/m39/}.
#' @inheritSection AMR Read more on our website!
#' @examples
#' \donttest{
#' x <- bug_drug_combinations(septic_patients)
#' x
#' format(x)
#' }
bug_drug_combinations <- function(x, col_mo = NULL, minimum = 30) {
if (!is.data.frame(x)) {
stop("`x` must be a data frame.", call. = FALSE)
}
# try to find columns based on type
# -- mo
if (is.null(col_mo)) {
col_mo <- search_type_in_df(x = x, type = "mo")
}
if (is.null(col_mo)) {
stop("`col_mo` must be set.", call. = FALSE)
}
x <- x %>%
mutate(col_mo = x %>% pull(col_mo)) %>%
filter(mo %in% (clean::freq(mo) %>%
filter(count >= minimum) %>%
pull(item))) %>%
group_by(mo) %>%
AMR::rsi_df(translate_ab = FALSE, combine_SI = FALSE) %>%
select(-value) %>%
spread(interpretation, isolates) %>%
mutate(total = S + I + R) %>%
filter(total >= minimum) %>%
rename(ab = antibiotic)
structure(.Data = x, class = c("bugdrug", class(x)))
}
#' @importFrom dplyr everything rename
#' @importFrom tidyr spread
#' @exportMethod format.bugdrug
#' @export
format.bugdrug <- function(x, combine_SI = TRUE, add_ab_group = TRUE, ...) {
if (combine_SI == TRUE) {
x$isolates <- x$R
} else {
x$isolates <- x$R + x$I
}
y <- x %>%
mutate(mo = mo_name(mo),
txt = paste0(AMR:::percent(isolates / total, force_zero = TRUE),
" (", trimws(format(isolates, big.mark = ",")), "/",
trimws(format(total, big.mark = ",")), ")")) %>%
select(ab, mo, txt) %>%
spread(mo, txt) %>%
mutate_all(~ifelse(is.na(.), "", .)) %>%
mutate(ab = paste0(ab_name(ab), " (", as.ab(ab), ", ", ab_atc(ab), ")"),
ab_group = ab_group(ab)) %>%
select(ab_group, ab, everything()) %>%
arrange(ab_group, ab) %>%
mutate(ab_group = ifelse(ab_group != lag(ab_group) | is.na(lag(ab_group)), ab_group, ""))
if (add_ab_group == FALSE) {
y <- y %>% select(-ab_group)
}
y <- y %>% rename("Group" = ab_group,
"Antibiotic" = ab)
y
}
#' @exportMethod print.bugdrug
#' @export
#' @importFrom crayon blue
print.bugdrug <- function(x, ...) {
print(as.data.frame(x, stringsAsFactors = FALSE))
message(blue("NOTE: Use 'format()' on this result to get a format that is ready for export or printing."))
}

View File

@ -19,23 +19,25 @@
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
freq_def <- clean:::freq.default
#' @importFrom clean freq
#' @export
clean::freq
#' @exportMethod freq.mo
#' @importFrom dplyr n_distinct
#' @importFrom clean freq
#' @importFrom clean freq.default
#' @export
#' @noRd
freq.mo <- function(x, ...) {
# replace with freq.default() if next `clean` version is published on CRAN
freq_def(x = x, ...,
.add_header = list(families = n_distinct(mo_family(x, language = NULL)),
genera = n_distinct(mo_genus(x, language = NULL)),
species = n_distinct(paste(mo_genus(x, language = NULL),
mo_species(x, language = NULL)))))
freq.default(x = x, ...,
.add_header = list(families = n_distinct(mo_family(x, language = NULL)),
genera = n_distinct(mo_genus(x, language = NULL)),
species = n_distinct(paste(mo_genus(x, language = NULL),
mo_species(x, language = NULL)))))
}
#' @exportMethod freq.rsi
#' @importFrom clean freq
#' @importFrom clean freq.default
#' @export
#' @noRd
freq.rsi <- function(x, ...) {
@ -43,12 +45,12 @@ freq.rsi <- function(x, ...) {
x_name <- gsub(".*[$]", "", x_name)
ab <- suppressMessages(suppressWarnings(AMR::as.ab(x_name)))
if (!is.na(ab)) {
freq_def(x = x, ...,
.add_header = list(Drug = paste0(ab_name(ab), " (", ab, ", ", ab_atc(ab), ")"),
group = ab_group(ab),
`%SI` = portion_SI(x, minimum = 0, as_percent = TRUE)))
freq.default(x = x, ...,
.add_header = list(Drug = paste0(ab_name(ab), " (", ab, ", ", ab_atc(ab), ")"),
group = ab_group(ab),
`%SI` = AMR::portion_SI(x, minimum = 0, as_percent = TRUE)))
} else {
freq_def(x = x, ...,
.add_header = list(`%SI` = portion_SI(x, minimum = 0, as_percent = TRUE)))
freq.default(x = x, ...,
.add_header = list(`%SI` = AMR::portion_SI(x, minimum = 0, as_percent = TRUE)))
}
}

View File

@ -169,6 +169,7 @@ rsi_calc <- function(...,
}
#' @importFrom dplyr %>% summarise_if mutate select everything bind_rows
#' @importFrom tidyr gather
rsi_calc_df <- function(type, # "portion" or "count"
data,
translate_ab = "name",
@ -187,10 +188,10 @@ rsi_calc_df <- function(type, # "portion" or "count"
combine_SI <- FALSE
}
if (isTRUE(combine_SI) & isTRUE(combine_IR)) {
stop("either `combine_SI` or `combine_IR` can be TRUE", call. = FALSE)
stop("either `combine_SI` or `combine_IR` can be TRUE, not both", call. = FALSE)
}
if (data %>% select_if(is.rsi) %>% ncol() == 0) {
if (!any(sapply(data, is.rsi), na.rm = TRUE)) {
stop("No columns with class 'rsi' found. See ?as.rsi.", call. = FALSE)
}
@ -198,7 +199,7 @@ rsi_calc_df <- function(type, # "portion" or "count"
translate_ab <- "name"
}
get_summaryfunction <- function(int) {
get_summaryfunction <- function(int, type) {
# look for portion_S, count_S, etc:
int_fn <- get(paste0(type, "_", int), envir = asNamespace("AMR"))
@ -218,11 +219,11 @@ rsi_calc_df <- function(type, # "portion" or "count"
select(interpretation, everything())
}
resS <- get_summaryfunction("S")
resI <- get_summaryfunction("I")
resR <- get_summaryfunction("R")
resSI <- get_summaryfunction("SI")
resIR <- get_summaryfunction("IR")
resS <- get_summaryfunction("S", type)
resI <- get_summaryfunction("I", type)
resR <- get_summaryfunction("R", type)
resSI <- get_summaryfunction("SI", type)
resIR <- get_summaryfunction("IR", type)
data.groups <- group_vars(data)
if (isFALSE(combine_SI) & isFALSE(combine_IR)) {
@ -245,11 +246,11 @@ rsi_calc_df <- function(type, # "portion" or "count"
}
res <- res %>%
tidyr::gather(antibiotic, value, -interpretation, -data.groups) %>%
gather(antibiotic, value, -interpretation, -data.groups) %>%
select(antibiotic, everything())
if (!translate_ab == FALSE) {
res <- res %>% mutate(antibiotic = ab_property(antibiotic, property = translate_ab, language = language))
res <- res %>% mutate(antibiotic = AMR::ab_property(antibiotic, property = translate_ab, language = language))
}
res

View File

@ -21,7 +21,6 @@
#' @rdname portion
#' @rdname count
#' @importFrom dplyr %>% select_if bind_rows summarise_if mutate group_vars select everything
#' @export
rsi_df <- function(data,
translate_ab = "name",