diff --git a/DESCRIPTION b/DESCRIPTION index 331974c1..2b574620 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.6.1.9034 -Date: 2019-05-20 +Version: 0.6.1.9035 +Date: 2019-05-23 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/NAMESPACE b/NAMESPACE index 7c5b7957..a84b5929 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -132,6 +132,7 @@ export(kurtosis) export(labels_rsi_count) export(left_join_microorganisms) export(like) +export(mdr_tb) export(mdro) export(mo_authors) export(mo_class) diff --git a/NEWS.md b/NEWS.md index ec01cb5a..588f0965 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,9 +1,10 @@ -# AMR 0.6.1.9001 +# AMR 0.6.1.90xx **Note: latest development version** #### New * Support for translation of disk diffusion and MIC values to RSI values (i.e. antimicrobial interpretations). Supported guidelines are EUCAST (2011 to 2019) and CLSI (2011 to 2019). Use `as.rsi()` on an MIC value (created with `as.mic()`), a disk diffusion value (created with the new `as.disk()`) or on a complete date set containing columns with MIC or disk diffusion values. * Function `mo_name()` as alias of `mo_fullname()` +* Added guidelines of the WHO to determine mutli-drug resistance (MDR) for TB (`mdr_tb()`) and added a new vignette about MDR #### Changed * Completely reworked the `antibiotics` data set: @@ -11,7 +12,7 @@ * Column `ab` contains a human readable EARS-Net code, used by ECDC and WHO/WHONET - this is the primary identifier used in this package * Column `atc` contains the ATC code, used by WHO/WHOCC * Column `cid` contains the CID code (Compound ID), used by PubChem - * Based on the Compound ID, more than a thousand official brand names have been added from many different countries + * Based on the Compound ID, almost 5,000 official brand names have been added from many different countries * All references to antibiotics in our package now use EARS-Net codes, like `AMX` for amoxicillin * Functions `atc_certe`, `ab_umcg` and `atc_trivial_nl` have been removed * All `atc_*` functions are superceded by `ab_*` functions @@ -24,7 +25,10 @@ * Added ~5,000 more old taxonomic names to the `microorganisms.old` data set, which leads to better results finding when using the `as.mo()` function * This package now honours the new EUCAST insight (2019) that S and I are but classified as susceptible, where I is defined as 'increased exposure' and not 'intermediate' anymore. For functions like `portion_df()` and `count_df()` this means that their new parameter `combine_SI` is TRUE at default. * Removed deprecated functions `guess_mo()`, `guess_atc()`, `EUCAST_rules()`, `interpretive_reading()`, `rsi()` -* Frequency tables of microbial IDs speed improvement +* Frequency tables (`freq()`): + * speed improvement for microbial IDs + * fixed level names in markdown + * * Removed all hardcoded EUCAST rules and replaced them with a new reference file: `./inst/eucast/eucast.tsv` * Added ceftazidim intrinsic resistance to *Streptococci* * Changed default settings for `age_groups()`, to let groups of fives and tens end with 100+ instead of 120+ diff --git a/R/data.R b/R/data.R index 0aec1b15..2264ad45 100755 --- a/R/data.R +++ b/R/data.R @@ -22,7 +22,7 @@ #' Data set with ~450 antibiotics #' #' A data set containing all antibiotics. Use \code{\link{as.ab}} or one of the \code{\link{ab_property}} functions to retrieve values from this data set. Three identifiers are included in this data set: an antibiotic ID (\code{ab}, primarily used in this package) as defined by WHONET/EARS-Net, an ATC code (\code{atc}) as defined by the WHO, and a Compound ID (\code{cid}) as found in PubChem. Other properties in this data set are derived from one or more of these codes. -#' @format A \code{\link{data.frame}} with 455 observations and 13 variables: +#' @format A \code{\link{data.frame}} with 454 observations and 13 variables: #' \describe{ #' \item{\code{ab}}{Antibiotic ID as used in this package (like \code{AMC}), using the official EARS-Net (European Antimicrobial Resistance Surveillance Network) codes where available} #' \item{\code{atc}}{ATC code (Anatomical Therapeutic Chemical) as defined by the WHOCC, like \code{J01CR02}} diff --git a/R/eucast_rules.R b/R/eucast_rules.R index b92fae3a..64ac2ebf 100755 --- a/R/eucast_rules.R +++ b/R/eucast_rules.R @@ -116,7 +116,7 @@ EUCAST_VERSION_EXPERT_RULES <- "3.1, 2016" #' @export #' @importFrom dplyr %>% select pull mutate_at vars group_by summarise n #' @importFrom crayon bold bgGreen bgYellow bgRed black green blue italic strip_style white -#' @return The input of \code{tbl_}, possibly with edited values of antibiotics. Or, if \code{verbose = TRUE}, a \code{data.frame} with all original and new values of the affected bug-drug combinations. +#' @return The input of \code{x}, possibly with edited values of antibiotics. Or, if \code{verbose = TRUE}, a \code{data.frame} with all original and new values of the affected bug-drug combinations. #' @source #' \itemize{ #' \item{ @@ -184,16 +184,16 @@ eucast_rules <- function(x, verbose = FALSE, ...) { - tbl_ <- x + x <- x - if (!is.data.frame(tbl_)) { + 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(tbl = tbl_, type = "mo") + col_mo <- search_type_in_df(x = x, type = "mo") } if (is.null(col_mo)) { stop("`col_mo` must be set.", call. = FALSE) @@ -376,12 +376,12 @@ eucast_rules <- function(x, edit_rsi <- function(to, rule, rows, cols) { cols <- unique(cols[!is.na(cols) & !is.null(cols)]) if (length(rows) > 0 & length(cols) > 0) { - before_df <- tbl_original - before <- as.character(unlist(as.list(tbl_original[rows, cols]))) + before_df <- x_original + before <- as.character(unlist(as.list(x_original[rows, cols]))) tryCatch( # insert into original table - tbl_original[rows, cols] <<- to, + x_original[rows, cols] <<- to, warning = function(w) { if (w$message %like% 'invalid factor level') { warning('Value "', to, '" could not be applied to column(s) `', paste(cols, collapse = '`, `'), '` because this value is not an existing factor level.', call. = FALSE) @@ -396,9 +396,9 @@ eucast_rules <- function(x, } ) - tbl_[rows, cols] <<- tbl_original[rows, cols] + x[rows, cols] <<- x_original[rows, cols] - after <- as.character(unlist(as.list(tbl_original[rows, cols]))) + after <- as.character(unlist(as.list(x_original[rows, cols]))) # before_df might not be a data.frame, but a tibble of data.table instead old <- as.data.frame(before_df, stringsAsFactors = FALSE)[rows,] @@ -406,9 +406,9 @@ eucast_rules <- function(x, for (i in 1:length(cols)) { verbose_new <- data.frame(row = rows, col = cols[i], - mo_fullname = tbl_[rows, "fullname"], + mo_fullname = x[rows, "fullname"], old = as.character(old[, cols[i]]), - new = as.character(tbl_[rows, cols[i]]), + new = as.character(x[rows, cols[i]]), rule = strip_style(rule[1]), rule_group = strip_style(rule[2]), rule_name = strip_style(rule[3]), @@ -426,11 +426,11 @@ eucast_rules <- function(x, } # save original table - tbl_original <- tbl_ + x_original <- x # join to microorganisms data set suppressWarnings( - tbl_ <- tbl_ %>% + x <- x %>% mutate_at(vars(col_mo), as.mo) %>% left_join_microorganisms(by = col_mo, suffix = c("_oldcols", "")) %>% mutate(gramstain = mo_gramstain(pull(., col_mo), language = "en"), @@ -448,18 +448,18 @@ eucast_rules <- function(x, if (!ab_missing(AMP) & !ab_missing(AMX)) { if (verbose == TRUE) { cat("\n VERBOSE: transforming", - length(which(tbl_[, AMX] == "S" & !tbl_[, AMP] %in% c("S", "I", "R"))), + length(which(x[, AMX] == "S" & !x[, AMP] %in% c("S", "I", "R"))), "empty ampicillin fields to 'S' based on amoxicillin. ") cat("\n VERBOSE: transforming", - length(which(tbl_[, AMX] == "I" & !tbl_[, AMP] %in% c("S", "I", "R"))), + length(which(x[, AMX] == "I" & !x[, AMP] %in% c("S", "I", "R"))), "empty ampicillin fields to 'I' based on amoxicillin. ") cat("\n VERBOSE: transforming", - length(which(tbl_[, AMX] == "R" & !tbl_[, AMP] %in% c("S", "I", "R"))), + length(which(x[, AMX] == "R" & !x[, AMP] %in% c("S", "I", "R"))), "empty ampicillin fields to 'R' based on amoxicillin. \n") } - tbl_[which(tbl_[, AMX] == "S" & !tbl_[, AMP] %in% c("S", "I", "R")), AMP] <- "S" - tbl_[which(tbl_[, AMX] == "I" & !tbl_[, AMP] %in% c("S", "I", "R")), AMP] <- "I" - tbl_[which(tbl_[, AMX] == "R" & !tbl_[, AMP] %in% c("S", "I", "R")), AMP] <- "R" + x[which(x[, AMX] == "S" & !x[, AMP] %in% c("S", "I", "R")), AMP] <- "S" + x[which(x[, AMX] == "I" & !x[, AMP] %in% c("S", "I", "R")), AMP] <- "I" + x[which(x[, AMX] == "R" & !x[, AMP] %in% c("S", "I", "R")), AMP] <- "R" } else if (ab_missing(AMP) & !ab_missing(AMX)) { # ampicillin column is missing, but amoxicillin is available message(blue(paste0("NOTE: Using column `", bold(AMX), "` as input for ampicillin (J01CA01) since many EUCAST rules depend on it."))) @@ -605,36 +605,36 @@ eucast_rules <- function(x, target_value <- eucast_rules_df[i, 7] if (is.na(source_antibiotics)) { - rows <- tryCatch(which(tbl_[, col_mo_property] %like% mo_value), + rows <- tryCatch(which(x[, col_mo_property] %like% mo_value), error = function(e) integer(0)) } else { - source_antibiotics <- get_antibiotic_columns(source_antibiotics, tbl_) + source_antibiotics <- get_antibiotic_columns(source_antibiotics, x) if (length(source_value) == 1 & length(source_antibiotics) > 1) { source_value <- rep(source_value, length(source_antibiotics)) } if (length(source_antibiotics) == 0) { rows <- integer(0) } else if (length(source_antibiotics) == 1) { - rows <- tryCatch(which(tbl_[, col_mo_property] %like% mo_value - & tbl_[, source_antibiotics[1L]] == source_value[1L]), + rows <- tryCatch(which(x[, col_mo_property] %like% mo_value + & x[, source_antibiotics[1L]] == source_value[1L]), error = function(e) integer(0)) } else if (length(source_antibiotics) == 2) { - rows <- tryCatch(which(tbl_[, col_mo_property] %like% mo_value - & tbl_[, source_antibiotics[1L]] == source_value[1L] - & tbl_[, source_antibiotics[2L]] == source_value[2L]), + rows <- tryCatch(which(x[, col_mo_property] %like% mo_value + & x[, source_antibiotics[1L]] == source_value[1L] + & x[, source_antibiotics[2L]] == source_value[2L]), error = function(e) integer(0)) } else if (length(source_antibiotics) == 3) { - rows <- tryCatch(which(tbl_[, col_mo_property] %like% mo_value - & tbl_[, source_antibiotics[1L]] == source_value[1L] - & tbl_[, source_antibiotics[2L]] == source_value[2L] - & tbl_[, source_antibiotics[3L]] == source_value[3L]), + rows <- tryCatch(which(x[, col_mo_property] %like% mo_value + & x[, source_antibiotics[1L]] == source_value[1L] + & x[, source_antibiotics[2L]] == source_value[2L] + & x[, source_antibiotics[3L]] == source_value[3L]), error = function(e) integer(0)) } else { stop("only 3 antibiotics supported for source_antibiotics ", call. = FALSE) } } - cols <- get_antibiotic_columns(target_antibiotics, tbl_) + cols <- get_antibiotic_columns(target_antibiotics, x) # Apply rule on data ------------------------------------------------------ # this will return the unique number of changes @@ -671,7 +671,7 @@ eucast_rules <- function(x, cat(paste0("\n", silver(strrep("-", options()$width - 1)), "\n")) cat(bold(paste('EUCAST rules', paste0(wouldve, 'affected'), formatnr(n_distinct(verbose_info$row)), - 'out of', formatnr(nrow(tbl_original)), + 'out of', formatnr(nrow(x_original)), 'rows, making a total of', formatnr(nrow(verbose_info)), 'edits\n'))) n_added <- verbose_info %>% filter(is.na(old)) %>% nrow() @@ -742,7 +742,7 @@ eucast_rules <- function(x, if (verbose == TRUE) { verbose_info } else { - tbl_original + x_original } } diff --git a/R/first_isolate.R b/R/first_isolate.R index c315b660..8c6637c2 100755 --- a/R/first_isolate.R +++ b/R/first_isolate.R @@ -191,7 +191,7 @@ first_isolate <- function(x, # try to find columns based on type # -- mo if (is.null(col_mo)) { - col_mo <- search_type_in_df(tbl = x, type = "mo") + col_mo <- search_type_in_df(x = x, type = "mo") } if (is.null(col_mo)) { stop("`col_mo` must be set.", call. = FALSE) @@ -199,7 +199,7 @@ first_isolate <- function(x, # -- date if (is.null(col_date)) { - col_date <- search_type_in_df(tbl = x, type = "date") + col_date <- search_type_in_df(x = x, type = "date") } if (is.null(col_date)) { stop("`col_date` must be set.", call. = FALSE) @@ -217,7 +217,7 @@ first_isolate <- function(x, col_patient_id <- "patient_id" message(blue(paste0("NOTE: Using combined columns ", bold("`First name`, `Last name` and `Sex`"), " as input for `col_patient_id`."))) } else { - col_patient_id <- search_type_in_df(tbl = x, type = "patient_id") + col_patient_id <- search_type_in_df(x = x, type = "patient_id") } } if (is.null(col_patient_id)) { @@ -226,7 +226,7 @@ first_isolate <- function(x, # -- key antibiotics if (is.null(col_keyantibiotics)) { - col_keyantibiotics <- search_type_in_df(tbl = x, type = "keyantibiotics") + col_keyantibiotics <- search_type_in_df(x = x, type = "keyantibiotics") } if (isFALSE(col_keyantibiotics)) { col_keyantibiotics <- NULL @@ -234,7 +234,7 @@ first_isolate <- function(x, # -- specimen if (is.null(col_specimen)) { - col_specimen <- search_type_in_df(tbl = x, type = "specimen") + col_specimen <- search_type_in_df(x = x, type = "specimen") } if (isFALSE(col_specimen)) { col_specimen <- NULL @@ -547,10 +547,10 @@ filter_first_isolate <- function(x, col_mo = NULL, ...) { filter(x, first_isolate(x = x, - col_date = col_date, - col_patient_id = col_patient_id, - col_mo = col_mo, - ...)) + col_date = col_date, + col_patient_id = col_patient_id, + col_mo = col_mo, + ...)) } #' @rdname first_isolate diff --git a/R/freq.R b/R/freq.R index ad9b68a6..776fe011 100755 --- a/R/freq.R +++ b/R/freq.R @@ -336,9 +336,10 @@ frequency_tbl <- function(x, cols <- unlist(strsplit(x.name, "$", fixed = TRUE))[2] x.name <- unlist(strsplit(x.name, "$", fixed = TRUE))[1] # try to find the object to determine dimensions + x.obj <- tryCatch(get(x.name), error = function(e) NULL) x.name <- paste0("`", x.name , "`") - if (!is.null(x.obj)) { + if (!is.null(dim(x.obj))) { x.name <- paste0(x.name, " (", x.obj %>% @@ -664,6 +665,10 @@ format_header <- function(x, markdown = FALSE, decimal.mark = ".", big.mark = ", if (nchar(levels_text) > 70) { # levels text wider than half the console levels_text <- paste0(substr(levels_text, 1, 70 - 3), "...") + if (nchar(gsub("[^`]", "", levels_text)) %% 2 == 1) { + # odd number of backticks, should be even + levels_text <- paste0(levels_text, "`") + } } header$levels <- paste0(length(header$levels), ": ", levels_text) header <- header[names(header) != "ordered"] diff --git a/R/key_antibiotics.R b/R/key_antibiotics.R index 6ffd9804..d6dd5b6a 100755 --- a/R/key_antibiotics.R +++ b/R/key_antibiotics.R @@ -22,7 +22,7 @@ #' Key antibiotics for first \emph{weighted} isolates #' #' These function can be used to determine first isolates (see \code{\link{first_isolate}}). Using key antibiotics to determine first isolates is more reliable than without key antibiotics. These selected isolates will then be called first \emph{weighted} isolates. -#' @param tbl table with antibiotics coloms, like \code{amox} and \code{amcl}. +#' @param x table with antibiotics coloms, like \code{AMX} or \code{amox} #' @param x,y characters to compare #' @inheritParams first_isolate #' @param universal_1,universal_2,universal_3,universal_4,universal_5,universal_6 column names of \strong{broad-spectrum} antibiotics, case-insensitive. At default, the columns containing these antibiotics will be guessed with \code{\link{guess_ab_col}}. @@ -76,33 +76,33 @@ #' #' key_antibiotics_equal(strainA, strainB, ignore_I = FALSE) #' # FALSE, because I is not ignored and so the 4th value differs -key_antibiotics <- function(tbl, +key_antibiotics <- function(x, col_mo = NULL, - universal_1 = guess_ab_col(tbl, "AMX"), - universal_2 = guess_ab_col(tbl, "AMC"), - universal_3 = guess_ab_col(tbl, "CXM"), - universal_4 = guess_ab_col(tbl, "TZP"), - universal_5 = guess_ab_col(tbl, "CIP"), - universal_6 = guess_ab_col(tbl, "SXT"), - GramPos_1 = guess_ab_col(tbl, "VAN"), - GramPos_2 = guess_ab_col(tbl, "TEC"), - GramPos_3 = guess_ab_col(tbl, "TCY"), - GramPos_4 = guess_ab_col(tbl, "ERY"), - GramPos_5 = guess_ab_col(tbl, "OXA"), - GramPos_6 = guess_ab_col(tbl, "RIF"), - GramNeg_1 = guess_ab_col(tbl, "GEN"), - GramNeg_2 = guess_ab_col(tbl, "TOB"), - GramNeg_3 = guess_ab_col(tbl, "COL"), - GramNeg_4 = guess_ab_col(tbl, "CTX"), - GramNeg_5 = guess_ab_col(tbl, "CAZ"), - GramNeg_6 = guess_ab_col(tbl, "MEM"), + universal_1 = guess_ab_col(x, "AMX"), + universal_2 = guess_ab_col(x, "AMC"), + universal_3 = guess_ab_col(x, "CXM"), + universal_4 = guess_ab_col(x, "TZP"), + universal_5 = guess_ab_col(x, "CIP"), + universal_6 = guess_ab_col(x, "SXT"), + GramPos_1 = guess_ab_col(x, "VAN"), + GramPos_2 = guess_ab_col(x, "TEC"), + GramPos_3 = guess_ab_col(x, "TCY"), + GramPos_4 = guess_ab_col(x, "ERY"), + GramPos_5 = guess_ab_col(x, "OXA"), + GramPos_6 = guess_ab_col(x, "RIF"), + GramNeg_1 = guess_ab_col(x, "GEN"), + GramNeg_2 = guess_ab_col(x, "TOB"), + GramNeg_3 = guess_ab_col(x, "COL"), + GramNeg_4 = guess_ab_col(x, "CTX"), + GramNeg_5 = guess_ab_col(x, "CAZ"), + GramNeg_6 = guess_ab_col(x, "MEM"), warnings = TRUE, ...) { # try to find columns based on type # -- mo if (is.null(col_mo)) { - col_mo <- search_type_in_df(tbl = tbl, type = "mo") + col_mo <- search_type_in_df(x = x, type = "mo") } if (is.null(col_mo)) { stop("`col_mo` must be set.", call. = FALSE) @@ -112,7 +112,7 @@ key_antibiotics <- function(tbl, col.list <- c(universal_1, universal_2, universal_3, universal_4, universal_5, universal_6, GramPos_1, GramPos_2, GramPos_3, GramPos_4, GramPos_5, GramPos_6, GramNeg_1, GramNeg_2, GramNeg_3, GramNeg_4, GramNeg_5, GramNeg_6) - check_available_columns <- function(tbl, col.list, info = TRUE) { + check_available_columns <- function(x, col.list, info = TRUE) { # check columns col.list <- col.list[!is.na(col.list) & !is.null(col.list)] names(col.list) <- col.list @@ -121,18 +121,18 @@ key_antibiotics <- function(tbl, for (i in 1:length(col.list)) { if (is.null(col.list[i]) | isTRUE(is.na(col.list[i]))) { col.list[i] <- NA - } else if (toupper(col.list[i]) %in% colnames(tbl)) { + } else if (toupper(col.list[i]) %in% colnames(x)) { col.list[i] <- toupper(col.list[i]) - } else if (tolower(col.list[i]) %in% colnames(tbl)) { + } else if (tolower(col.list[i]) %in% colnames(x)) { col.list[i] <- tolower(col.list[i]) - } else if (!col.list[i] %in% colnames(tbl)) { + } else if (!col.list[i] %in% colnames(x)) { col.list[i] <- NA } } - if (!all(col.list %in% colnames(tbl))) { + if (!all(col.list %in% colnames(x))) { if (info == TRUE) { warning('Some columns do not exist and will be ignored: ', - col.list.bak[!(col.list %in% colnames(tbl))] %>% toString(), + col.list.bak[!(col.list %in% colnames(x))] %>% toString(), '.\nTHIS MAY STRONGLY INFLUENCE THE OUTCOME.', immediate. = TRUE, call. = FALSE) @@ -141,7 +141,7 @@ key_antibiotics <- function(tbl, col.list } - col.list <- check_available_columns(tbl = tbl, col.list = col.list, info = warnings) + col.list <- check_available_columns(x = x, col.list = col.list, info = warnings) universal_1 <- col.list[universal_1] universal_2 <- col.list[universal_2] universal_3 <- col.list[universal_3] @@ -183,30 +183,30 @@ key_antibiotics <- function(tbl, } # join to microorganisms data set - tbl <- tbl %>% + x <- x %>% mutate_at(vars(col_mo), as.mo) %>% left_join_microorganisms(by = col_mo) %>% mutate(key_ab = NA_character_, gramstain = mo_gramstain(pull(., col_mo))) # Gram + - tbl <- tbl %>% mutate(key_ab = - if_else(gramstain == "Gram positive", - apply(X = tbl[, gram_positive], - MARGIN = 1, - FUN = function(x) paste(x, collapse = "")), - key_ab)) + x <- x %>% mutate(key_ab = + if_else(gramstain == "Gram positive", + apply(X = x[, gram_positive], + MARGIN = 1, + FUN = function(x) paste(x, collapse = "")), + key_ab)) # Gram - - tbl <- tbl %>% mutate(key_ab = - if_else(gramstain == "Gram negative", - apply(X = tbl[, gram_negative], - MARGIN = 1, - FUN = function(x) paste(x, collapse = "")), - key_ab)) + x <- x %>% mutate(key_ab = + if_else(gramstain == "Gram negative", + apply(X = x[, gram_negative], + MARGIN = 1, + FUN = function(x) paste(x, collapse = "")), + key_ab)) # format - key_abs <- tbl %>% + key_abs <- x %>% pull(key_ab) %>% gsub('(NA|NULL)', '.', .) %>% gsub('[^SIR]', '.', ., ignore.case = TRUE) diff --git a/R/mdro.R b/R/mdro.R index 175bf947..6cd37db3 100755 --- a/R/mdro.R +++ b/R/mdro.R @@ -23,12 +23,25 @@ #' #' Determine which isolates are multidrug-resistant organisms (MDRO) according to country-specific guidelines. #' @param x table with antibiotic columns, like e.g. \code{AMX} and \code{AMC} -#' @param country country code to determine guidelines. EUCAST rules will be used when left empty, see Details. Should be or a code from the \href{https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements}{list of ISO 3166-1 alpha-2 country codes}. Case-insensitive. Currently supported are \code{de} (Germany) and \code{nl} (the Netherlands). +#' @param country country code to determine guidelines. Should be or a code from the \href{https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Officially_assigned_code_elements}{list of ISO 3166-1 alpha-2 country codes}. Case-insensitive. +#' @param guideline a specific guideline to mention. For some countries this will be determined automatically, see Details. EUCAST guidelines will be used when left empty, see Details. #' @param info print progress #' @inheritParams eucast_rules #' @param verbose print additional info: missing antibiotic columns per parameter #' @inheritSection eucast_rules Antibiotics -#' @details When \code{country} will be left blank, guidelines will be taken from EUCAST Expert Rules Version 3.1 "Intrinsic Resistance and Exceptional Phenotypes Tables" (\href{http://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/Expert_rules_intrinsic_exceptional_V3.1.pdf}{link}). +#' @details When \code{country} is set, the parameter guideline will be ignored as these guidelines will be used: +#' +#' \itemize{ +#' \item{\code{country = "nl"}: Rijksinstituut voor Volksgezondheid en Milieu "WIP-richtlijn BRMO (Bijzonder Resistente Micro-Organismen) [ZKH]" (\href{https://www.rivm.nl/Documenten_en_publicaties/Professioneel_Praktisch/Richtlijnen/Infectieziekten/WIP_Richtlijnen/WIP_Richtlijnen/Ziekenhuizen/WIP_richtlijn_BRMO_Bijzonder_Resistente_Micro_Organismen_ZKH}{link})} +#' } +#' +#' Please suggest your own country's specific guidelines by letting us know: \url{https://gitlab.com/msberends/AMR/issues/new}. +#' +#' Other currently supported guidelines are: +#' \itemize{ +#' \item{\code{guideline = "eucast"}: EUCAST Expert Rules Version 3.1 "Intrinsic Resistance and Exceptional Phenotypes Tables" (\href{http://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/Expert_rules_intrinsic_exceptional_V3.1.pdf}{link})} +#' \item{\code{guideline = "tb"}: World Health Organization "Companion handbook to the WHO guidelines for the programmatic management of drug-resistant tuberculosis" (\href{https://www.who.int/tb/publications/pmdt_companionhandbook/en/}{link})} +#' } #' @return Ordered factor with levels \code{Negative < Positive, unconfirmed < Positive}. #' @rdname mdro #' @importFrom dplyr %>% @@ -43,21 +56,41 @@ #' BRMO = brmo(.)) mdro <- function(x, country = NULL, + guideline = NULL, col_mo = NULL, info = TRUE, verbose = FALSE, ...) { - tbl_ <- x - - if (!is.data.frame(tbl_)) { + if (!is.data.frame(x)) { stop("`x` must be a data frame.", call. = FALSE) } + if (length(guideline) > 1) { + stop("`guideline` must be a length one character string.", call. = FALSE) + } + if (!is.null(country)) { + guideline <- country + } + if (is.null(guideline)) { + guideline <- "eucast" + } + if (!tolower(guideline) %in% c("nl", "de", "eucast", "tb")) { + stop("invalid guideline: ", guideline, call. = FALSE) + } + guideline <- list(code = tolower(guideline)) + # try to find columns based on type # -- mo if (is.null(col_mo)) { - col_mo <- search_type_in_df(tbl = tbl_, type = "mo") + col_mo <- search_type_in_df(x = x, type = "mo") + } + if (is.null(col_mo) & guideline$code == "tb") { + message(blue("NOTE: No column found as input for `col_mo`,", + bold("assuming all records contain", + italic("Mycobacterium tuberculosis.")))) + x$mo <- AMR::as.mo("Mycobacterium tuberculosis") + col_mo <- "mo" } if (is.null(col_mo)) { stop("`col_mo` must be set.", call. = FALSE) @@ -67,50 +100,59 @@ mdro <- function(x, stop("`country` must be a length one character string.", call. = FALSE) } - if (is.null(country)) { - country <- "EUCAST" - } - country <- trimws(country) - if (tolower(country) != "eucast" & !country %like% "^[a-z]{2}$") { - stop("This is not a valid ISO 3166-1 alpha-2 country code: '", country, "'. Please see ?mdro.", call. = FALSE) - } - - # create list and make country code case-independent - guideline <- list(country = list(code = tolower(country))) - - if (guideline$country$code == "eucast") { - guideline$country$name <- "(European guidelines)" + if (guideline$code == "eucast") { guideline$name <- "EUCAST Expert Rules, \"Intrinsic Resistance and Exceptional Phenotypes Tables\"" - guideline$version <- "Version 3.1" + guideline$author <- "EUCAST (European Committee on Antimicrobial Susceptibility Testing)" + guideline$version <- "3.1" guideline$source <- "http://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/Expert_rules_intrinsic_exceptional_V3.1.pdf" + + } else if (guideline$code == "tb") { + guideline$name <- "Companion handbook to the WHO guidelines for the programmatic management of drug-resistant tuberculosis" + guideline$author <- "WHO (World Health Organization)" + guideline$version <- "WHO/HTM/TB/2014.11" + guideline$source <- "https://www.who.int/tb/publications/pmdt_companionhandbook/en/" + # support per country: - } else if (guideline$country$code == "de") { - guideline$country$name <- "Germany" + } else if (guideline$code == "de") { + guideline$name <- "Germany" guideline$name <- "" guideline$version <- "" guideline$source <- "" - } else if (guideline$country$code == "nl") { - guideline$country$name <- "The Netherlands" - guideline$name <- "WIP-Richtlijn BRMO" + } else if (guideline$code == "nl") { + guideline$name <- "WIP-Richtlijn Bijzonder Resistente Micro-organismen (BRMO)" + guideline$author <- "RIVM (Rijksinstituut voor de Volksgezondheid)" guideline$version <- "Revision as of December 2017" guideline$source <- "https://www.rivm.nl/Documenten_en_publicaties/Professioneel_Praktisch/Richtlijnen/Infectieziekten/WIP_Richtlijnen/WIP_Richtlijnen/Ziekenhuizen/WIP_richtlijn_BRMO_Bijzonder_Resistente_Micro_Organismen_ZKH" # add here more countries like this: # } else if (country$code == "xx") { # country$name <- "country name" } else { - stop("This country code is currently unsupported: ", guideline$country$code, call. = FALSE) + stop("This guideline is currently unsupported: ", guideline$code, call. = FALSE) } if (info == TRUE) { cat("Determining multidrug-resistant organisms (MDRO), according to:\n", - "Guideline: ", red(paste0(guideline$name, ", ", guideline$version, "\n")), - "Country : ", red(paste0(guideline$country$name, "\n")), - "Source : ", blue(paste0(guideline$source, "\n")), + "Guideline: ", red(guideline$name), "\n", + "Version: ", red(guideline$version), "\n", + "Author: ", red(guideline$author), "\n", + "Source: ", blue(guideline$source), "\n", "\n", sep = "") } - - cols_ab <- get_column_abx(x = x, verbose = verbose, ...) + if (guideline$code == "tb") { + cols_ab <- get_column_abx(x = x, + soft_dependencies = c("CAP", + "ETH", + "GAT", + "INH", + "PZA", + "RIF", + "RIB", + "RFP"), + verbose = verbose, ...) + } else { + cols_ab <- get_column_abx(x = x, verbose = verbose, ...) + } AMC <- cols_ab["AMC"] AMK <- cols_ab["AMK"] @@ -175,7 +217,20 @@ mdro <- function(x, TOB <- cols_ab["TOB"] TZP <- cols_ab["TZP"] VAN <- cols_ab["VAN"] - + # additional for TB + CAP <- cols_ab["CAP"] + ETH <- cols_ab["ETH"] + GAT <- cols_ab["GAT"] + INH <- cols_ab["INH"] + PZA <- cols_ab["PZA"] + RIF <- cols_ab["RIF"] + RIB <- cols_ab["RIB"] + RFP <- cols_ab["RFP"] + abx_tb <- c(CAP, ETH, GAT, INH, PZA, RIF, RIB, RFP) + abx_tb <- abx_tb[!is.na(abx_tb)] + if (guideline$code == "tb" & length(abx_tb) == 0) { + stop("No antimycobacterials found in data set.", call. = FALSE) + } ab_missing <- function(ab) { isTRUE(ab %in% c(NULL, NA)) | length(ab) == 0 @@ -194,96 +249,96 @@ mdro <- function(x, cols <- cols[!is.na(cols)] if (length(rows) > 0 & length(cols) > 0) { if (any_all == "any") { - row_filter <- which(tbl_[, cols] == "R") + row_filter <- which(x[, cols] == "R") } else if (any_all == "all") { - row_filter <- tbl_ %>% + row_filter <- x %>% mutate(index = 1:nrow(.)) %>% filter_at(vars(cols), all_vars(. == "R")) %>% pull((index)) } rows <- rows[rows %in% row_filter] - tbl_[rows, "MDRO"] <<- to + x[rows, "MDRO"] <<- to } } - tbl_ <- tbl_ %>% + x <- x %>% mutate_at(vars(col_mo), as.mo) %>% # join to microorganisms data set left_join_microorganisms(by = col_mo) %>% # add unconfirmed to where genus is available mutate(MDRO = ifelse(!is.na(genus), 1, NA_integer_)) - if (guideline$country$code == "eucast") { + if (guideline$code == "eucast") { # EUCAST ------------------------------------------------------------------ # Table 5 trans_tbl(3, - which(tbl_$family == "Enterobacteriaceae" - | tbl_$fullname %like% "^Pseudomonas aeruginosa" - | tbl_$genus == "Acinetobacter"), + which(x$family == "Enterobacteriaceae" + | x$fullname %like% "^Pseudomonas aeruginosa" + | x$genus == "Acinetobacter"), COL, "all") trans_tbl(3, - which(tbl_$fullname %like% "^Salmonella Typhi"), + which(x$fullname %like% "^Salmonella Typhi"), c(carbapenems, fluoroquinolones), "any") trans_tbl(3, - which(tbl_$fullname %like% "^Haemophilus influenzae"), + which(x$fullname %like% "^Haemophilus influenzae"), c(cephalosporins_3rd, carbapenems, fluoroquinolones), "any") trans_tbl(3, - which(tbl_$fullname %like% "^Moraxella catarrhalis"), + which(x$fullname %like% "^Moraxella catarrhalis"), c(cephalosporins_3rd, fluoroquinolones), "any") trans_tbl(3, - which(tbl_$fullname %like% "^Neisseria meningitidis"), + which(x$fullname %like% "^Neisseria meningitidis"), c(cephalosporins_3rd, fluoroquinolones), "any") trans_tbl(3, - which(tbl_$fullname %like% "^Neisseria gonorrhoeae"), + which(x$fullname %like% "^Neisseria gonorrhoeae"), AZM, "any") # Table 6 trans_tbl(3, - which(tbl_$fullname %like% "^Staphylococcus (aureus|epidermidis|coagulase negatief|hominis|haemolyticus|intermedius|pseudointermedius)"), + which(x$fullname %like% "^Staphylococcus (aureus|epidermidis|coagulase negatief|hominis|haemolyticus|intermedius|pseudointermedius)"), c(VAN, TEC, DAP, LNZ, QDA, TGC), "any") trans_tbl(3, - which(tbl_$genus == "Corynebacterium"), + which(x$genus == "Corynebacterium"), c(VAN, TEC, DAP, LNZ, QDA, TGC), "any") trans_tbl(3, - which(tbl_$fullname %like% "^Streptococcus pneumoniae"), + which(x$fullname %like% "^Streptococcus pneumoniae"), c(carbapenems, VAN, TEC, DAP, LNZ, QDA, TGC, RIF), "any") trans_tbl(3, # Sr. groups A/B/C/G - which(tbl_$fullname %like% "^Streptococcus (pyogenes|agalactiae|equisimilis|equi|zooepidemicus|dysgalactiae|anginosus)"), + which(x$fullname %like% "^Streptococcus (pyogenes|agalactiae|equisimilis|equi|zooepidemicus|dysgalactiae|anginosus)"), c(PEN, cephalosporins, VAN, TEC, DAP, LNZ, QDA, TGC), "any") trans_tbl(3, - which(tbl_$genus == "Enterococcus"), + which(x$genus == "Enterococcus"), c(DAP, LNZ, TGC, TEC), "any") trans_tbl(3, - which(tbl_$fullname %like% "^Enterococcus faecalis"), + which(x$fullname %like% "^Enterococcus faecalis"), c(AMP, AMX), "any") # Table 7 trans_tbl(3, - which(tbl_$genus == "Bacteroides"), + which(x$genus == "Bacteroides"), MTR, "any") trans_tbl(3, - which(tbl_$fullname %like% "^Clostridium difficile"), + which(x$fullname %like% "^Clostridium difficile"), c(MTR, VAN), "any") } - if (guideline$country$code == "de") { + if (guideline$code == "de") { # Germany ----------------------------------------------------------------- stop("We are still working on German guidelines in this beta version.", call. = FALSE) } - if (guideline$country$code == "nl") { + if (guideline$code == "nl") { # Netherlands ------------------------------------------------------------- aminoglycosides <- aminoglycosides[!is.na(aminoglycosides)] fluoroquinolones <- fluoroquinolones[!is.na(fluoroquinolones)] @@ -298,32 +353,32 @@ mdro <- function(x, # Table 1 trans_tbl(3, - which(tbl_$family == "Enterobacteriaceae"), + which(x$family == "Enterobacteriaceae"), c(aminoglycosides, fluoroquinolones), "all") trans_tbl(2, - which(tbl_$family == "Enterobacteriaceae"), + which(x$family == "Enterobacteriaceae"), carbapenems, "any") trans_tbl(2, - which(tbl_$family == "Enterobacteriaceae"), + which(x$family == "Enterobacteriaceae"), ESBLs, "all") # Table 2 trans_tbl(2, - which(tbl_$genus == "Acinetobacter"), + which(x$genus == "Acinetobacter"), c(carbapenems), "any") trans_tbl(3, - which(tbl_$genus == "Acinetobacter"), + which(x$genus == "Acinetobacter"), c(aminoglycosides, fluoroquinolones), "all") trans_tbl(3, - which(tbl_$fullname %like% "^Stenotrophomonas maltophilia"), + which(x$fullname %like% "^Stenotrophomonas maltophilia"), SXT, "all") @@ -332,39 +387,108 @@ mdro <- function(x, & !ab_missing(CIP) & !ab_missing(CAZ) & !ab_missing(TZP) ) { - tbl_$psae <- 0 - tbl_[which(tbl_[, MEM] == "R" | tbl_[, IPM] == "R"), "psae"] <- 1 + tbl_[which(tbl_[, MEM] == "R" | tbl_[, IPM] == "R"), "psae"] - tbl_[which(tbl_[, GEN] == "R" & tbl_[, TOB] == "R"), "psae"] <- 1 + tbl_[which(tbl_[, GEN] == "R" & tbl_[, TOB] == "R"), "psae"] - tbl_[which(tbl_[, CIP] == "R"), "psae"] <- 1 + tbl_[which(tbl_[, CIP] == "R"), "psae"] - tbl_[which(tbl_[, CAZ] == "R"), "psae"] <- 1 + tbl_[which(tbl_[, CAZ] == "R"), "psae"] - tbl_[which(tbl_[, TZP] == "R"), "psae"] <- 1 + tbl_[which(tbl_[, TZP] == "R"), "psae"] + x$psae <- 0 + x[which(x[, MEM] == "R" | x[, IPM] == "R"), "psae"] <- 1 + x[which(x[, MEM] == "R" | x[, IPM] == "R"), "psae"] + x[which(x[, GEN] == "R" & x[, TOB] == "R"), "psae"] <- 1 + x[which(x[, GEN] == "R" & x[, TOB] == "R"), "psae"] + x[which(x[, CIP] == "R"), "psae"] <- 1 + x[which(x[, CIP] == "R"), "psae"] + x[which(x[, CAZ] == "R"), "psae"] <- 1 + x[which(x[, CAZ] == "R"), "psae"] + x[which(x[, TZP] == "R"), "psae"] <- 1 + x[which(x[, TZP] == "R"), "psae"] } else { - tbl_$psae <- 0 + x$psae <- 0 } - tbl_[which( - tbl_$fullname %like% "Pseudomonas aeruginosa" - & tbl_$psae >= 3 + x[which( + x$fullname %like% "Pseudomonas aeruginosa" + & x$psae >= 3 ), "MDRO"] <- 3 # Table 3 trans_tbl(3, - which(tbl_$fullname %like% "Streptococcus pneumoniae"), + which(x$fullname %like% "Streptococcus pneumoniae"), PEN, "all") trans_tbl(3, - which(tbl_$fullname %like% "Streptococcus pneumoniae"), + which(x$fullname %like% "Streptococcus pneumoniae"), VAN, "all") trans_tbl(3, - which(tbl_$fullname %like% "Enterococcus faecium"), + which(x$fullname %like% "Enterococcus faecium"), c(PEN, VAN), "all") } - factor(x = tbl_$MDRO, - levels = 1:3, - labels = c("Negative", "Positive, unconfirmed", "Positive"), - ordered = TRUE) + prepare_drug <- function(ab) { + # returns vector values of drug + # if `ab` is a column name, looks up the values in `x` + if (length(ab) == 1 & is.character(ab)) { + if (ab %in% colnames(x)) { + ab <- as.data.frame(x)[, ab] + } + } + ab <- as.character(as.rsi(ab)) + ab[is.na(ab)] <- "" + ab + } + drug_is_R <- function(ab) { + # returns logical vector + ab <- prepare_drug(ab) + if (length(ab) == 1) { + rep(ab, NROW(x)) == "R" + } else { + ab == "R" + } + } + drug_is_not_R <- function(ab) { + # returns logical vector + ab <- prepare_drug(ab) + if (length(ab) == 1) { + rep(ab, NROW(x)) != "R" + } else { + ab != "R" + } + } + + if (guideline$code == "tb") { + # Tuberculosis ------------------------------------------------------------ + x <- x %>% + mutate(mono_count = 0, + mono_count = ifelse(drug_is_R(INH), mono_count + 1, mono_count), + mono_count = ifelse(drug_is_R(RIF), mono_count + 1, mono_count), + mono_count = ifelse(drug_is_R(ETH), mono_count + 1, mono_count), + mono_count = ifelse(drug_is_R(PZA), mono_count + 1, mono_count), + mono_count = ifelse(drug_is_R(RIB), mono_count + 1, mono_count), + mono_count = ifelse(drug_is_R(RFP), mono_count + 1, mono_count), + # from here on logicals + mono = mono_count > 0, + poly = ifelse(mono_count > 1 & drug_is_not_R(RIF) & drug_is_not_R(INH), + TRUE, FALSE), + mdr = ifelse(drug_is_R(RIF) & drug_is_R(INH), + TRUE, FALSE), + xdr = ifelse(drug_is_R(LVX) | drug_is_R(MFX) | drug_is_R(GAT), + TRUE, FALSE), + second = ifelse(drug_is_R(CAP) | drug_is_R(KAN) | drug_is_R(AMK), + TRUE, FALSE), + xdr = ifelse(mdr & xdr & second, TRUE, FALSE)) %>% + mutate(mdr_tb = case_when(xdr ~ 5, + mdr ~ 4, + poly ~ 3, + mono ~ 2, + TRUE ~ 1), + # keep all real TB, make other species NA + mdr_tb = ifelse(x$fullname == "Mycobacterium tuberculosis", mdr_tb, NA_real_)) + } + + # return results + if (guideline$code == "tb") { + factor(x = x$mdr_tb, + levels = 1:5, + labels = c("Negative", "Mono-resistance", "Poly-resistance", "Multidrug resistance", "Extensive drug resistance"), + ordered = TRUE) + } else { + factor(x = x$MDRO, + levels = 1:3, + labels = c("Negative", "Positive, unconfirmed", "Positive"), + ordered = TRUE) + } } #' @rdname mdro @@ -381,6 +505,12 @@ mrgn <- function(x, country = "de", ...) { #' @rdname mdro #' @export -eucast_exceptional_phenotypes <- function(x, country = "EUCAST", ...) { - mdro(x = x, country = "EUCAST", ...) +mdr_tb <- function(x, guideline = "TB", ...) { + mdro(x = x, guideline = "TB", ...) +} + +#' @rdname mdro +#' @export +eucast_exceptional_phenotypes <- function(x, guideline = "EUCAST", ...) { + mdro(x = x, guideline = "EUCAST", ...) } diff --git a/R/misc.R b/R/misc.R index 8c4eb0f8..0dab33fb 100755 --- a/R/misc.R +++ b/R/misc.R @@ -87,43 +87,43 @@ percent <- function(x, round = 1, force_zero = FALSE, decimal.mark = getOption(" #' @importFrom crayon blue bold red #' @importFrom dplyr %>% pull -search_type_in_df <- function(tbl, type) { +search_type_in_df <- function(x, type) { # try to find columns based on type found <- NULL - colnames(tbl) <- trimws(colnames(tbl)) + colnames(x) <- trimws(colnames(x)) # -- mo if (type == "mo") { - if ("mo" %in% lapply(tbl, class)) { - found <- colnames(tbl)[lapply(tbl, class) == "mo"][1] - } else if (any(colnames(tbl) %like% "^(mo|microorganism|organism|bacteria)")) { - found <- colnames(tbl)[colnames(tbl) %like% "^(mo|microorganism|organism|bacteria)"][1] - } else if (any(colnames(tbl) %like% "species")) { - found <- colnames(tbl)[colnames(tbl) %like% "species"][1] + if ("mo" %in% lapply(x, class)) { + found <- colnames(x)[lapply(x, class) == "mo"][1] + } else if (any(colnames(x) %like% "^(mo|microorganism|organism|bacteria)s?$")) { + found <- colnames(x)[colnames(x) %like% "^(mo|microorganism|organism|bacteria)s?$"][1] + } else if (any(colnames(x) %like% "species")) { + found <- colnames(x)[colnames(x) %like% "species"][1] } } # -- key antibiotics if (type == "keyantibiotics") { - if (any(colnames(tbl) %like% "^key.*(ab|antibiotics)")) { - found <- colnames(tbl)[colnames(tbl) %like% "^key.*(ab|antibiotics)"][1] + if (any(colnames(x) %like% "^key.*(ab|antibiotics)")) { + found <- colnames(x)[colnames(x) %like% "^key.*(ab|antibiotics)"][1] } } # -- date if (type == "date") { - if (any(colnames(tbl) %like% "^(specimen date|specimen_date|spec_date)")) { + if (any(colnames(x) %like% "^(specimen date|specimen_date|spec_date)")) { # WHONET support - found <- colnames(tbl)[colnames(tbl) %like% "^(specimen date|specimen_date|spec_date)"][1] - if (!any(class(tbl %>% pull(found)) %in% c("Date", "POSIXct"))) { + found <- colnames(x)[colnames(x) %like% "^(specimen date|specimen_date|spec_date)"][1] + if (!any(class(x %>% pull(found)) %in% c("Date", "POSIXct"))) { stop(red(paste0("ERROR: Found column `", bold(found), "` to be used as input for `col_", type, "`, but this column contains no valid dates. Transform its values to valid dates first.")), call. = FALSE) } } else { - for (i in 1:ncol(tbl)) { - if (any(class(tbl %>% pull(i)) %in% c("Date", "POSIXct"))) { - found <- colnames(tbl)[i] + for (i in 1:ncol(x)) { + if (any(class(x %>% pull(i)) %in% c("Date", "POSIXct"))) { + found <- colnames(x)[i] break } } @@ -131,16 +131,16 @@ search_type_in_df <- function(tbl, type) { } # -- patient id if (type == "patient_id") { - if (any(colnames(tbl) %like% "^(identification |patient|patid)")) { - found <- colnames(tbl)[colnames(tbl) %like% "^(identification |patient|patid)"][1] + if (any(colnames(x) %like% "^(identification |patient|patid)")) { + found <- colnames(x)[colnames(x) %like% "^(identification |patient|patid)"][1] } } # -- specimen if (type == "specimen") { - if (any(colnames(tbl) %like% "(specimen type|spec_type)")) { - found <- colnames(tbl)[colnames(tbl) %like% "(specimen type|spec_type)"][1] - } else if (any(colnames(tbl) %like% "^(specimen)")) { - found <- colnames(tbl)[colnames(tbl) %like% "^(specimen)"][1] + if (any(colnames(x) %like% "(specimen type|spec_type)")) { + found <- colnames(x)[colnames(x) %like% "(specimen type|spec_type)"][1] + } else if (any(colnames(x) %like% "^(specimen)")) { + found <- colnames(x)[colnames(x) %like% "^(specimen)"][1] } } diff --git a/R/portion.R b/R/portion.R index 9d94f80f..a11d3990 100755 --- a/R/portion.R +++ b/R/portion.R @@ -31,7 +31,7 @@ #' @param data a \code{data.frame} containing columns with class \code{rsi} (see \code{\link{as.rsi}}) #' @param translate_ab a column name of the \code{\link{antibiotics}} data set to translate the antibiotic abbreviations to, using \code{\link{ab_property}} #' @inheritParams ab_property -#' @param combine_SI a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter \code{combine_IR}, but this now follows the redefinition by EUCAST about the interpretion of I (increased exposure) in 2019, see below. Default is now \code{TRUE}. +#' @param combine_SI a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter \code{combine_IR}, but this now follows the redefinition by EUCAST about the interpretion of I (increased exposure) in 2019, see section 'Interpretation of S, I and R' below. Default is \code{TRUE}. #' @param combine_IR a logical to indicate whether all values of I and R must be merged into one, so the output only consists of S vs. I+R (susceptible vs. non-susceptible). This is outdated, see parameter \code{combine_SI}. #' @inheritSection as.rsi Interpretation of S, I and R #' @details \strong{Remember that you should filter your table to let it contain only first isolates!} Use \code{\link{first_isolate}} to determine them in your data set. diff --git a/R/resistance_predict.R b/R/resistance_predict.R index 00b42277..7b344346 100755 --- a/R/resistance_predict.R +++ b/R/resistance_predict.R @@ -140,7 +140,7 @@ resistance_predict <- function(x, # -- date if (is.null(col_date)) { - col_date <- search_type_in_df(tbl = x, type = "date") + col_date <- search_type_in_df(x = x, type = "date") } if (is.null(col_date)) { stop("`col_date` must be set.", call. = FALSE) diff --git a/R/rsi.R b/R/rsi.R index d62f21cf..c0f83de4 100755 --- a/R/rsi.R +++ b/R/rsi.R @@ -39,9 +39,9 @@ #' In 2019, EUCAST has decided to change the definitions of susceptibility testing categories S, I and R as shown below. Results of several consultations on the new definitions are available on the EUCAST website under "Consultations". #' #' \itemize{ -#' \item{\strong{S}}{Susceptible, standard dosing regimen: A microorganism is categorised as "Susceptible, standard dosing regimen", when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.} -#' \item{\strong{I}}{Susceptible, increased exposure: A microorganism is categorised as "Susceptible, Increased exposure" when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.} -#' \item{\strong{R}}{Resistant: A microorganism is categorised as "Resistant" when there is a high likelihood of therapeutic failure even when there is increased exposure.} +#' \item{\strong{S} - }{Susceptible, standard dosing regimen: A microorganism is categorised as "Susceptible, standard dosing regimen", when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.} +#' \item{\strong{I} - }{Susceptible, increased exposure: A microorganism is categorised as "Susceptible, Increased exposure" when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.} +#' \item{\strong{R} - }{Resistant: A microorganism is categorised as "Resistant" when there is a high likelihood of therapeutic failure even when there is increased exposure.} #' } #' #' Exposure is a function of how the mode of administration, dose, dosing interval, infusion time, as well as distribution and excretion of the antimicrobial agent will influence the infecting organism at the site of infection. @@ -259,9 +259,9 @@ exec_as.rsi <- function(method, x, mo, ab, guideline) { #' @importFrom crayon red blue #' @export as.rsi.data.frame <- function(x, col_mo = NULL, guideline = "EUCAST", ...) { - tbl_ <- x + x <- x - ab_cols <- colnames(tbl_)[sapply(tbl_, function(x) is.mic(x) | is.disk(x))] + ab_cols <- colnames(x)[sapply(x, function(y) is.mic(y) | is.disk(y))] if (length(ab_cols) == 0) { stop("No columns with MIC values or disk zones found in this data set. Use as.mic or as.disk to transform antibiotic columns.", call. = FALSE) } @@ -269,14 +269,14 @@ as.rsi.data.frame <- function(x, col_mo = NULL, guideline = "EUCAST", ...) { # try to find columns based on type # -- mo if (is.null(col_mo)) { - col_mo <- search_type_in_df(tbl = tbl_, type = "mo") + col_mo <- search_type_in_df(x = x, type = "mo") } if (is.null(col_mo)) { stop("`col_mo` must be set.", call. = FALSE) } # transform all MICs - ab_cols <- colnames(tbl_)[sapply(tbl_, is.mic)] + ab_cols <- colnames(x)[sapply(x, is.mic)] if (length(ab_cols) > 0) { for (i in 1:length(ab_cols)) { if (is.na(suppressWarnings(as.ab(ab_cols[i])))) { @@ -284,16 +284,16 @@ as.rsi.data.frame <- function(x, col_mo = NULL, guideline = "EUCAST", ...) { next } message(blue(paste0("Interpreting column `", bold(ab_cols[i]), "` (", ab_name(ab_cols[i], tolower = TRUE), ")...")), appendLF = FALSE) - tbl_[, ab_cols[i]] <- exec_as.rsi(method = "mic", - x = tbl_ %>% pull(ab_cols[i]), - mo = tbl_ %>% pull(col_mo), + x[, ab_cols[i]] <- exec_as.rsi(method = "mic", + x = x %>% pull(ab_cols[i]), + mo = x %>% pull(col_mo), ab = as.ab(ab_cols[i]), guideline = guideline) message(blue(" OK.")) } } # transform all disks - ab_cols <- colnames(tbl_)[sapply(tbl_, is.disk)] + ab_cols <- colnames(x)[sapply(x, is.disk)] if (length(ab_cols) > 0) { for (i in 1:length(ab_cols)) { if (is.na(suppressWarnings(as.ab(ab_cols[i])))) { @@ -301,16 +301,16 @@ as.rsi.data.frame <- function(x, col_mo = NULL, guideline = "EUCAST", ...) { next } message(blue(paste0("Interpreting column `", bold(ab_cols[i]), "` (", ab_name(ab_cols[i], tolower = TRUE), ")...")), appendLF = FALSE) - tbl_[, ab_cols[i]] <- exec_as.rsi(method = "disk", - x = tbl_ %>% pull(ab_cols[i]), - mo = tbl_ %>% pull(col_mo), + x[, ab_cols[i]] <- exec_as.rsi(method = "disk", + x = x %>% pull(ab_cols[i]), + mo = x %>% pull(col_mo), ab = as.ab(ab_cols[i]), guideline = guideline) message(blue(" OK.")) } } - tbl_ + x } #' @rdname as.rsi diff --git a/_pkgdown.yml b/_pkgdown.yml index ebe89a1b..cf1f13f6 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -38,6 +38,9 @@ navbar: - text: 'Predict antimicrobial resistance' icon: 'fa-dice' href: 'articles/resistance_predict.html' + - text: 'Determine multi-drug resistance (MDR)' + icon: 'fa-skull-crossbones' + href: 'articles/MDR.html' - text: 'Work with WHONET data' icon: 'fa-globe-americas' href: 'articles/WHONET.html' diff --git a/data/antibiotics.rda b/data/antibiotics.rda index 14248d0e..4b6213b4 100755 Binary files a/data/antibiotics.rda and b/data/antibiotics.rda differ diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index f314972c..fe103ca0 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@
@@ -114,6 +114,13 @@ Predict antimicrobial resistance +AMR.Rmd
Note: values on this page will change with every website update since they are based on randomly created values and the page was written in R Markdown. However, the methodology remains unchanged. This page was generated on 20 May 2019.
+Note: values on this page will change with every website update since they are based on randomly created values and the page was written in R Markdown. However, the methodology remains unchanged. This page was generated on 23 May 2019.
So, we can draw at least two conclusions immediately. From a data scientist perspective, the data looks clean: only values M
and F
. From a researcher perspective: there are slightly more men. Nothing we didn’t already know.
The data is already quite clean, but we still need to transform some variables. The bacteria
column now consists of text, and we want to add more variables based on microbial IDs later on. So, we will transform this column to valid IDs. The mutate()
function of the dplyr
package makes this really easy:
data <- data %>%
@@ -442,14 +449,14 @@
# Pasteurella multocida (no new changes)
# Staphylococcus (no new changes)
# Streptococcus groups A, B, C, G (no new changes)
-# Streptococcus pneumoniae (1443 new changes)
+# Streptococcus pneumoniae (1542 new changes)
# Viridans group streptococci (no new changes)
#
# EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016)
-# Table 01: Intrinsic resistance in Enterobacteriaceae (1303 new changes)
+# Table 01: Intrinsic resistance in Enterobacteriaceae (1286 new changes)
# Table 02: Intrinsic resistance in non-fermentative Gram-negative bacteria (no new changes)
# Table 03: Intrinsic resistance in other Gram-negative bacteria (no new changes)
-# Table 04: Intrinsic resistance in Gram-positive bacteria (2746 new changes)
+# Table 04: Intrinsic resistance in Gram-positive bacteria (2788 new changes)
# Table 08: Interpretive rules for B-lactam agents and Gram-positive cocci (no new changes)
# Table 09: Interpretive rules for B-lactam agents and Gram-negative rods (no new changes)
# Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no new changes)
@@ -457,24 +464,24 @@
# Table 13: Interpretive rules for quinolones (no new changes)
#
# Other rules
-# Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (2313 new changes)
-# Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (115 new changes)
+# Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (2231 new changes)
+# Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (126 new changes)
# Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no new changes)
# Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no new changes)
# Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no new changes)
# Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no new changes)
#
# --------------------------------------------------------------------------
-# EUCAST rules affected 6,579 out of 20,000 rows, making a total of 7,920 edits
+# EUCAST rules affected 6,562 out of 20,000 rows, making a total of 7,973 edits
# => added 0 test results
#
-# => changed 7,920 test results
-# - 123 test results changed from S to I
-# - 4,680 test results changed from S to R
-# - 1,131 test results changed from I to S
-# - 306 test results changed from I to R
-# - 1,657 test results changed from R to S
-# - 23 test results changed from R to I
+# => changed 7,973 test results
+# - 114 test results changed from S to I
+# - 4,779 test results changed from S to R
+# - 1,118 test results changed from I to S
+# - 330 test results changed from I to R
+# - 1,606 test results changed from R to S
+# - 26 test results changed from R to I
# --------------------------------------------------------------------------
#
# Use verbose = TRUE to get a data.frame with all specified edits instead.
So only 28.4% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
So only 28.5% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
For future use, the above two syntaxes can be shortened with the filter_first_isolate()
function:
Only 2 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and should be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics()
function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.
Only 1 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and should be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics()
function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.
If a column exists with a name like ‘key(…)ab’ the first_isolate()
function will automatically use it and determine the first weighted isolates. Mind the NOTEs in below output:
data <- data %>%
mutate(keyab = key_antibiotics(.)) %>%
@@ -650,7 +657,7 @@
# NOTE: Using column `patient_id` as input for `col_patient_id`.
# NOTE: Using column `keyab` as input for `col_keyantibiotics`. Use col_keyantibiotics = FALSE to prevent this.
# [Criterion] Inclusion based on key antibiotics, ignoring I.
-# => Found 14,968 first weighted isolates (74.8% of total)
isolate | @@ -667,8 +674,8 @@||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | -2010-03-19 | -N8 | +2010-04-13 | +S1 | B_ESCHR_COL | S | S | @@ -679,58 +686,58 @@|||||||
2 | -2010-05-06 | -N8 | +2010-06-02 | +S1 | B_ESCHR_COL | -R | +S | S | S | S | FALSE | -TRUE | +FALSE | |
3 | -2010-05-20 | -N8 | +2010-07-21 | +S1 | B_ESCHR_COL | +I | +S | R | S | -S | -S | -FALSE | -FALSE | -|
4 | -2010-07-20 | -N8 | -B_ESCHR_COL | -S | -S | -S | -S | FALSE | TRUE | |||||
5 | -2010-09-04 | -N8 | +||||||||||||
4 | +2010-09-19 | +S1 | B_ESCHR_COL | +R | S | -S | -S | +R | S | FALSE | FALSE | |||
5 | +2010-09-19 | +S1 | +B_ESCHR_COL | +S | +S | +S | +R | +FALSE | +TRUE | +|||||
6 | -2010-09-13 | -N8 | +2010-10-14 | +S1 | B_ESCHR_COL | -R | +S | S | S | S | @@ -739,20 +746,20 @@||||
7 | -2011-04-01 | -N8 | +2010-10-28 | +S1 | B_ESCHR_COL | S | S | S | S | -TRUE | -TRUE | +FALSE | +FALSE | |
8 | -2011-10-15 | -N8 | +2010-12-09 | +S1 | B_ESCHR_COL | R | S | @@ -763,8 +770,8 @@|||||||
9 | -2011-11-14 | -N8 | +2010-12-15 | +S1 | B_ESCHR_COL | S | S | @@ -775,23 +782,23 @@|||||||
10 | -2012-02-11 | -N8 | +2011-01-07 | +S1 | B_ESCHR_COL | -S | -S | +R | +I | S | S | FALSE | -FALSE | +TRUE |
Instead of 2, now 7 isolates are flagged. In total, 74.8% of all isolates are marked ‘first weighted’ - 46.5% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
+Instead of 1, now 7 isolates are flagged. In total, 75.6% of all isolates are marked ‘first weighted’ - 47.1% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
As with filter_first_isolate()
, there’s a shortcut for this new algorithm too:
So we end up with 14,968 isolates for analysis.
+So we end up with 15,125 isolates for analysis.
We can remove unneeded columns:
@@ -799,6 +806,7 @@date | patient_id | hospital | @@ -815,12 +823,13 @@|||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2013-08-26 | -Q7 | -Hospital A | +1 | +2012-10-26 | +T6 | +Hospital D | B_STPHY_AUR | -S | -S | +R | +R | S | S | F | @@ -830,9 +839,58 @@TRUE |
2012-10-04 | +2 | +2011-10-13 | +Y4 | +Hospital B | +B_STRPT_PNE | +R | +R | +S | +R | +F | +Gram positive | +Streptococcus | +pneumoniae | +TRUE | +|
3 | +2010-02-09 | O2 | -Hospital A | +Hospital B | +B_STRPT_PNE | +R | +R | +S | +R | +F | +Gram positive | +Streptococcus | +pneumoniae | +TRUE | +|
6 | +2015-09-19 | +P5 | +Hospital D | +B_STRPT_PNE | +S | +S | +S | +R | +F | +Gram positive | +Streptococcus | +pneumoniae | +TRUE | +||
7 | +2011-05-13 | +X5 | +Hospital B | B_ESCHR_COL | R | S | @@ -844,60 +902,16 @@coli | TRUE | |||||||
2012-10-23 | -C4 | +||||||||||||||
8 | +2015-03-07 | +Z7 | Hospital C | B_ESCHR_COL | S | S | -S | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -||
2011-08-17 | -R4 | -Hospital B | -B_ESCHR_COL | R | S | -R | -S | -F | -Gram negative | -Escherichia | -coli | -TRUE | -|||
2016-01-18 | -L2 | -Hospital B | -B_ESCHR_COL | -S | -S | -S | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -|||
2011-09-06 | -O1 | -Hospital A | -B_ESCHR_COL | -R | -S | -S | -S | F | Gram negative | Escherichia | @@ -921,9 +935,9 @@|||||
1 | Escherichia coli | -7,394 | -49.4% | -7,394 | -49.4% | +7,486 | +49.5% | +7,486 | +49.5% | ||||||
2 | Staphylococcus aureus | -3,716 | -24.8% | -11,110 | +3,731 | +24.7% | +11,217 | 74.2% | |||||||
3 | Streptococcus pneumoniae | -2,291 | -15.3% | -13,401 | -89.5% | +2,360 | +15.6% | +13,577 | +89.8% | ||||||
4 | Klebsiella pneumoniae | -1,567 | -10.5% | -14,968 | +1,548 | +10.2% | +15,125 | 100.0% | |||||||
Hospital A | -0.4708540 | +0.4556355 | |||||||||||||
Hospital B | -0.4617585 | +0.4661553 | |||||||||||||
Hospital C | -0.4622030 | +0.4759657 | |||||||||||||
Hospital D | -0.4691689 | +0.4577347 |
EUCAST.Rmd
G_test.Rmd
MDR.Rmd
With the function mdro()
, you can determine multi-drug resistant organisms (MDRO). It currently support these guidelines:
As an example, I will make a data set to determine multi-drug resistant TB:
+# a helper function to get a random vector with values S, I and R
+# with the probabilities 50%-10%-40%
+sample_rsi <- function() {
+ sample(c("S", "I", "R"),
+ size = 5000,
+ prob = c(0.5, 0.1, 0.4),
+ replace = TRUE)
+}
+
+my_TB_data <- data.frame(rifampicin = sample_rsi(),
+ isoniazid = sample_rsi(),
+ gatifloxacin = sample_rsi(),
+ ethambutol = sample_rsi(),
+ pyrazinamide = sample_rsi(),
+ moxifloxacin = sample_rsi(),
+ kanamycin = sample_rsi())
Because all column names are automatically verified for valid drug names or codes, this would have worked exactly the same:
+my_TB_data <- data.frame(RIF = sample_rsi(),
+ INH = sample_rsi(),
+ GAT = sample_rsi(),
+ ETH = sample_rsi(),
+ PZA = sample_rsi(),
+ MFX = sample_rsi(),
+ KAN = sample_rsi())
The data set looks like this now:
+head(my_TB_data)
+# rifampicin isoniazid gatifloxacin ethambutol pyrazinamide moxifloxacin
+# 1 R R R R R S
+# 2 S R R S R S
+# 3 R S S I S S
+# 4 R S S R S S
+# 5 R R S I I R
+# 6 R S S S S S
+# kanamycin
+# 1 S
+# 2 R
+# 3 S
+# 4 R
+# 5 S
+# 6 I
We can now add the interpretation of MDR-TB to our data set:
+my_TB_data$mdr <- mdr_tb(my_TB_data)
+# NOTE: No column found as input for `col_mo`, assuming all records contain Mycobacterium tuberculosis.
+# Determining multidrug-resistant organisms (MDRO), according to:
+# Guideline: Companion handbook to the WHO guidelines for the programmatic management of drug-resistant tuberculosis
+# Version: WHO/HTM/TB/2014.11
+# Author: WHO (World Health Organization)
+# Source: https://www.who.int/tb/publications/pmdt_companionhandbook/en/
+# Warning: Reliability might be improved if these antimicrobial results
+# would be available too: `CAP` (capreomycin), `RIB` (rifabutin), `RFP`
+# (rifapentine)
And review the result with a frequency table:
+freq(my_TB_data$mdr)
+#
+#
+# **Frequency table of `mdr` from `my_TB_data` (5,000 x 8)**
+#
+# Class: `factor` > `ordered` (`numeric`)
+# Length: 5,000 (of which NA: 0 = 0.00%)
+# Levels: 5: `Negative` < `Mono-resistance` < `Poly-resistance` < `Multidrug res...`
+# Unique: 5
+#
+#
+# | |Item | Count| Percent| Cum. Count| Cum. Percent|
+# |:--|:-------------------------|-----:|-------:|----------:|------------:|
+# |1 |Mono-resistance | 3,297| 65.9%| 3,297| 65.9%|
+# |2 |Negative | 627| 12.5%| 3,924| 78.5%|
+# |3 |Multidrug resistance | 612| 12.2%| 4,536| 90.7%|
+# |4 |Poly-resistance | 263| 5.3%| 4,799| 96.0%|
+# |5 |Extensive drug resistance | 201| 4.0%| 5,000| 100.0%|
SPSS.Rmd
WHONET.Rmd
ab_property.Rmd
benchmarks.Rmd
In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 5 milliseconds means it can determine 200 input values per second. It case of 100 milliseconds, this is only 10 input values per second. The second input is the only one that has to be looked up thoroughly. All the others are known codes (the first one is a WHONET code) or common laboratory codes, or common full organism names like the last one. Full organism names are always preferred.
To achieve this speed, the as.mo
function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of Thermus islandicus (B_THERMS_ISL
), a bug probably never found before in humans:
T.islandicus <- microbenchmark(as.mo("theisl"),
@@ -236,12 +243,12 @@
print(T.islandicus, unit = "ms", signif = 2)
# Unit: milliseconds
# expr min lq mean median uq max neval
-# as.mo("theisl") 470 480 510 520 530 530 10
-# as.mo("THEISL") 470 470 500 490 520 530 10
-# as.mo("T. islandicus") 74 75 85 76 81 120 10
-# as.mo("T. islandicus") 74 75 92 76 120 140 10
-# as.mo("Thermus islandicus") 73 73 74 74 74 75 10
That takes 8.2 times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like Thermus islandicus) are almost fast - these are the most probable input from most data sets.
+# as.mo("theisl") 470 470 500 500 520 520 10 +# as.mo("THEISL") 470 470 510 520 520 530 10 +# as.mo("T. islandicus") 73 74 79 74 74 120 10 +# as.mo("T. islandicus") 73 74 84 74 75 130 10 +# as.mo("Thermus islandicus") 74 74 94 75 120 140 10 +That takes 8.1 times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like Thermus islandicus) are almost fast - these are the most probable input from most data sets.
In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Thermus islandicus (which is very uncommon):
par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)
@@ -287,8 +294,8 @@
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
-# mo_fullname(x) 677 770 812 790 884 988 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.79 seconds (789 ms). You only lose time on your unique input values.
+# mo_fullname(x) 633 675 747 722 763 933 10 +So transforming 500,000 values (!!) of 50 unique values only takes 0.72 seconds (721 ms). You only lose time on your unique input values.
So going from mo_fullname("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0017 seconds - it doesn’t even start calculating if the result would be the same as the expected resulting value. That goes for all helper functions:
run_it <- microbenchmark(A = mo_species("aureus"),
B = mo_genus("Staphylococcus"),
@@ -317,14 +324,14 @@
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
-# A 0.442 0.477 0.583 0.572 0.673 0.806 10
-# B 0.442 0.502 0.626 0.611 0.761 0.826 10
-# C 1.360 1.620 1.820 1.810 2.060 2.150 10
-# D 0.449 0.514 0.622 0.639 0.725 0.798 10
-# E 0.401 0.440 0.530 0.508 0.568 0.799 10
-# F 0.353 0.391 0.502 0.494 0.635 0.695 10
-# G 0.451 0.559 0.595 0.609 0.646 0.747 10
-# H 0.191 0.278 0.395 0.376 0.528 0.653 10
Of course, when running mo_phylum("Firmicutes")
the function has zero knowledge about the actual microorganism, namely S. aureus. But since the result would be "Firmicutes"
too, there is no point in calculating the result. And because this package ‘knows’ all phyla of all known bacteria (according to the Catalogue of Life), it can just return the initial value immediately.
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.
diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png index f1fd99d1..924101ae 100644 Binary files a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png differ diff --git a/docs/articles/freq.html b/docs/articles/freq.html index 5694c455..6fc26e20 100644 --- a/docs/articles/freq.html +++ b/docs/articles/freq.html @@ -40,7 +40,7 @@ @@ -76,6 +76,13 @@ Predict antimicrobial resistancefreq.Rmd
A frequency table is actaually a regular data.frame
, with the exception that it contains an additional class.
A frequency table is actually a regular data.frame
, with the exception that it contains an additional class.
[1] “frequency_tbl” “data.frame”
@@ -729,7 +736,7 @@ Median: 31 July 2009 (47.39%)na.rm
With the na.rm
parameter (defaults to TRUE
, but they will always be shown into the header), you can include NA
values in the frequency table:
With the na.rm
parameter you can remove NA
values from the frequency table (defaults to TRUE
, but the number of NA
values will always be shown into the header):
Frequency table of AMX
from a data.frame
(2,000 x 49)
Frequency table of AMX
from a data.frame
(2,000 x 49)
Class: factor
> ordered
> rsi
(numeric
)
+Length: 2,000 (of which NA: 771 = 38.55%)
+Levels: 3: S
< I
< R
+Unique: 4
Drug: Amoxicillin (AMX, J01CA04)
+Group: Beta-lactams/penicillins
+%SI: 44.43%
+ | Item | +Count | +Percent | +Cum. Count | +Cum. Percent | +
---|---|---|---|---|---|
1 | +(NA) | +771 | +38.6% | +771 | +38.6% | +
2 | +R | +683 | +34.2% | +1,454 | +72.7% | +
3 | +S | +543 | +27.2% | +1,997 | +99.8% | +
4 | +I | +3 | +0.2% | +2,000 | +100.0% | +
row.names
The default frequency tables shows row indices. To remove them, use row.names = FALSE
:
A frequency table shows row indices. To remove them, use row.names = FALSE
:
Frequency table of hospital_id
from a data.frame
(2,000 x 49)
Class: factor
(numeric
)
Length: 2,000 (of which NA: 0 = 0.00%)
@@ -842,8 +903,8 @@ Unique: 4
markdown
The markdown
parameter is TRUE
at default in non-interactive sessions, like in reports created with R Markdown. This will always print all rows, unless nmax
is set.
Frequency table of hospital_id
from a data.frame
(2,000 x 49)
Class: factor
(numeric
)
Length: 2,000 (of which NA: 0 = 0.00%)
diff --git a/docs/articles/index.html b/docs/articles/index.html
index 1ce938da..d552f329 100644
--- a/docs/articles/index.html
+++ b/docs/articles/index.html
@@ -78,7 +78,7 @@
mo_property.Rmd
resistance_predict.Rmd
We created this package for both academic research and routine analysis at the Faculty of Medical Sciences of the University of Groningen, the Netherlands, and the Medical Microbiology & Infection Prevention (MMBI) department of the University Medical Center Groningen (UMCG). This R package is actively maintained and is free software; you can freely use and distribute it for both personal and commercial (but not patent) purposes under the terms of the GNU General Public License version 2.0 (GPL-2), as published by the Free Software Foundation. Read the full license here.
This package can be used for:
This package is ready-to-use for a professional environment by specialists in the following fields:
Medical Microbiology
diff --git a/docs/news/index.html b/docs/news/index.html index ca6d8444..744bdd2d 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -78,7 +78,7 @@ @@ -114,6 +114,13 @@ Predict antimicrobial resistanceNote: latest development version
as.rsi()
on an MIC value (created with as.mic()
), a disk diffusion value (created with the new as.disk()
) or on a complete date set containing columns with MIC or disk diffusion values.mo_name()
as alias of mo_fullname()
mdr_tb()
) and added a new vignette about MDRcid
contains the CID code (Compound ID), used by PubChemAMX
for amoxicillinatc_certe
, ab_umcg
and atc_trivial_nl
have been removedatc_*
functions are superceded by ab_*
functionsportion_df()
and count_df()
this means that their new parameter combine_SI
is TRUE at default.guess_mo()
, guess_atc()
, EUCAST_rules()
, interpretive_reading()
, rsi()
freq()
):
+./inst/eucast/eucast.tsv
as.mo(..., allow_uncertain = 3)
Contents
A data.frame
with 455 observations and 13 variables:
A data.frame
with 454 observations and 13 variables:
ab
Antibiotic ID as used in this package (like AMC
), using the official EARS-Net (European Antimicrobial Resistance Surveillance Network) codes where available
atc
ATC code (Anatomical Therapeutic Chemical) as defined by the WHOCC, like J01CR02
cid
Compound ID as found in PubChem
In 2019, EUCAST has decided to change the definitions of susceptibility testing categories S, I and R as shown below. Results of several consultations on the new definitions are available on the EUCAST website under "Consultations".
SSusceptible, standard dosing regimen: A microorganism is categorised as "Susceptible, standard dosing regimen", when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.
ISusceptible, increased exposure: A microorganism is categorised as "Susceptible, Increased exposure" when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.
RResistant: A microorganism is categorised as "Resistant" when there is a high likelihood of therapeutic failure even when there is increased exposure.
S - Susceptible, standard dosing regimen: A microorganism is categorised as "Susceptible, standard dosing regimen", when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.
I - Susceptible, increased exposure: A microorganism is categorised as "Susceptible, Increased exposure" when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.
R - Resistant: A microorganism is categorised as "Resistant" when there is a high likelihood of therapeutic failure even when there is increased exposure.
Exposure is a function of how the mode of administration, dose, dosing interval, infusion time, as well as distribution and excretion of the antimicrobial agent will influence the infecting organism at the site of infection.
Source: http://www.eucast.org/newsiandr/.
diff --git a/docs/reference/count.html b/docs/reference/count.html index 343beaed..37d638bc 100644 --- a/docs/reference/count.html +++ b/docs/reference/count.html @@ -81,7 +81,7 @@ count_R and count_IR can be used to count resistant isolates, count_S and count_a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter combine_IR
, but this now follows the redefinition by EUCAST about the interpretion of I (increased exposure) in 2019, see below. Default is now TRUE
.
a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter combine_IR
, but this now follows the redefinition by EUCAST about the interpretion of I (increased exposure) in 2019, see section 'Interpretation of S, I and R' below. Default is TRUE
.
In 2019, EUCAST has decided to change the definitions of susceptibility testing categories S, I and R as shown below. Results of several consultations on the new definitions are available on the EUCAST website under "Consultations".
SSusceptible, standard dosing regimen: A microorganism is categorised as "Susceptible, standard dosing regimen", when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.
ISusceptible, increased exposure: A microorganism is categorised as "Susceptible, Increased exposure" when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.
RResistant: A microorganism is categorised as "Resistant" when there is a high likelihood of therapeutic failure even when there is increased exposure.
S - Susceptible, standard dosing regimen: A microorganism is categorised as "Susceptible, standard dosing regimen", when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.
I - Susceptible, increased exposure: A microorganism is categorised as "Susceptible, Increased exposure" when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.
R - Resistant: A microorganism is categorised as "Resistant" when there is a high likelihood of therapeutic failure even when there is increased exposure.
Exposure is a function of how the mode of administration, dose, dosing interval, infusion time, as well as distribution and excretion of the antimicrobial agent will influence the infecting organism at the site of infection.
Source: http://www.eucast.org/newsiandr/.
diff --git a/docs/reference/eucast_rules.html b/docs/reference/eucast_rules.html index 71aeb914..b516af56 100644 --- a/docs/reference/eucast_rules.html +++ b/docs/reference/eucast_rules.html @@ -80,7 +80,7 @@ @@ -116,6 +116,13 @@ Predict antimicrobial resistance +The input of tbl_
, possibly with edited values of antibiotics. Or, if verbose = TRUE
, a data.frame
with all original and new values of the affected bug-drug combinations.
The input of x
, possibly with edited values of antibiotics. Or, if verbose = TRUE
, a data.frame
with all original and new values of the affected bug-drug combinations.
a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter combine_IR
, but this now follows the redefinition by EUCAST about the interpretion of I (increased exposure) in 2019, see below. Default is now TRUE
.
a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter combine_IR
, but this now follows the redefinition by EUCAST about the interpretion of I (increased exposure) in 2019, see section 'Interpretation of S, I and R' below. Default is TRUE
.
mdro()
brmo()
mrgn()
eucast_exceptional_phenotypes()
mdro()
brmo()
mrgn()
mdr_tb()
eucast_exceptional_phenotypes()
Determine multidrug-resistant organisms (MDRO)
key_antibiotics(tbl, col_mo = NULL, universal_1 = guess_ab_col(tbl, - "AMX"), universal_2 = guess_ab_col(tbl, "AMC"), - universal_3 = guess_ab_col(tbl, "CXM"), - universal_4 = guess_ab_col(tbl, "TZP"), - universal_5 = guess_ab_col(tbl, "CIP"), - universal_6 = guess_ab_col(tbl, "SXT"), GramPos_1 = guess_ab_col(tbl, - "VAN"), GramPos_2 = guess_ab_col(tbl, "TEC"), - GramPos_3 = guess_ab_col(tbl, "TCY"), GramPos_4 = guess_ab_col(tbl, - "ERY"), GramPos_5 = guess_ab_col(tbl, "OXA"), - GramPos_6 = guess_ab_col(tbl, "RIF"), GramNeg_1 = guess_ab_col(tbl, - "GEN"), GramNeg_2 = guess_ab_col(tbl, "TOB"), - GramNeg_3 = guess_ab_col(tbl, "COL"), GramNeg_4 = guess_ab_col(tbl, - "CTX"), GramNeg_5 = guess_ab_col(tbl, "CAZ"), - GramNeg_6 = guess_ab_col(tbl, "MEM"), warnings = TRUE, ...) +key_antibiotics(x, col_mo = NULL, universal_1 = guess_ab_col(x, "AMX"), + universal_2 = guess_ab_col(x, "AMC"), universal_3 = guess_ab_col(x, + "CXM"), universal_4 = guess_ab_col(x, "TZP"), + universal_5 = guess_ab_col(x, "CIP"), universal_6 = guess_ab_col(x, + "SXT"), GramPos_1 = guess_ab_col(x, "VAN"), + GramPos_2 = guess_ab_col(x, "TEC"), GramPos_3 = guess_ab_col(x, + "TCY"), GramPos_4 = guess_ab_col(x, "ERY"), + GramPos_5 = guess_ab_col(x, "OXA"), GramPos_6 = guess_ab_col(x, + "RIF"), GramNeg_1 = guess_ab_col(x, "GEN"), + GramNeg_2 = guess_ab_col(x, "TOB"), GramNeg_3 = guess_ab_col(x, + "COL"), GramNeg_4 = guess_ab_col(x, "CTX"), + GramNeg_5 = guess_ab_col(x, "CAZ"), GramNeg_6 = guess_ab_col(x, + "MEM"), warnings = TRUE, ...) key_antibiotics_equal(x, y, type = c("keyantibiotics", "points"), ignore_I = TRUE, points_threshold = 2, info = FALSE)@@ -263,8 +269,8 @@
tbl | -table with antibiotics coloms, like |
+ x | +table with antibiotics coloms, like |
---|---|---|---|
col_mo | @@ -290,10 +296,6 @@... | other parameters passed on to function |
|
x, y | -characters to compare |
- ||
type | type to determine weighed isolates; can be |
@@ -310,6 +312,10 @@
info | print progress |
x, y | +characters to compare |
+
mdro(x, country = NULL, col_mo = NULL, info = TRUE, - verbose = FALSE, ...) +mdro(x, country = NULL, guideline = NULL, col_mo = NULL, + info = TRUE, verbose = FALSE, ...) brmo(..., country = "nl") mrgn(x, country = "de", ...) -eucast_exceptional_phenotypes(x, country = "EUCAST", ...)+mdr_tb(x, guideline = "TB", ...) + +eucast_exceptional_phenotypes(x, guideline = "EUCAST", ...)
country | -country code to determine guidelines. EUCAST rules will be used when left empty, see Details. Should be or a code from the list of ISO 3166-1 alpha-2 country codes. Case-insensitive. Currently supported are |
+ country code to determine guidelines. Should be or a code from the list of ISO 3166-1 alpha-2 country codes. Case-insensitive. |
+
---|---|---|
guideline | +a specific guideline to mention. For some countries this will be determined automatically, see Details. EUCAST guidelines will be used when left empty, see Details. |
|
col_mo | @@ -285,7 +298,15 @@||
combine_SI | -a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter |
+ a logical to indicate whether all values of S and I must be merged into one, so the output only consists of S+I vs. R (susceptible vs. resistant). This used to be the parameter |
combine_IR | @@ -335,9 +342,9 @@ portion_R and portion_IR can be used to calculate resistance, portion_S and port