diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b5e66c17..e24afe1d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -41,7 +41,8 @@ before_script: cache: key: "$CI_COMMIT_REF_SLUG" paths: - - /usr/lib/R/library/ + - /usr/local/lib/R/ + - /usr/lib/R/ R 3: stage: build diff --git a/NAMESPACE b/NAMESPACE index b3c3d316..aee04dee 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -76,6 +76,7 @@ export(g.test) export(geom_rsi) export(get_locale) export(ggplot_rsi) +export(guess_ab) export(guess_atc) export(guess_mo) export(header) diff --git a/NEWS.md b/NEWS.md index 009cc4e7..b21e0fc8 100755 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ * Contains the complete manual of this package and all of its functions with an explanation of their parameters * Contains a comprehensive tutorial about how to conduct antimicrobial resistance analysis * Support for [`dplyr`](https://dplyr.tidyverse.org) version 0.8.0 +* Function `guess_ab` to find an antibiotic column in a table * Function `mo_failures()` to review values that could not be coerced to a valid MO code, using `as.mo()`. This latter function will now only show a maximum of 25 uncoerced values. * Function `mo_renamed()` to get a list of all returned values from `as.mo()` that have had taxonomic renaming * Function `age()` to calculate the (patients) age in years diff --git a/R/eucast_rules.R b/R/eucast_rules.R index 51926fea..616b8da9 100755 --- a/R/eucast_rules.R +++ b/R/eucast_rules.R @@ -30,7 +30,7 @@ #' @param ... parameters that are passed on to \code{eucast_rules} #' @inheritParams first_isolate #' @section Antibiotics: -#' To define antibiotics column names, input a text (case-insensitive) or use \code{NULL} to skip a column (e.g. \code{tica = NULL}). Non-existing columns will anyway be skipped with a warning. +#' To define antibiotics column names, leave as it is to determine it automatically with \code{\link{guess_ab}} or input a text (case-insensitive) or use \code{NULL} to skip a column (e.g. \code{tica = NULL}). Non-existing columns will anyway be skipped with a warning. #' #' Abbrevations of the column containing antibiotics in the form: \strong{abbreviation}: generic name (\emph{ATC code}) #' @@ -158,69 +158,69 @@ eucast_rules <- function(tbl, info = TRUE, rules = c("breakpoints", "expert", "other", "all"), verbose = FALSE, - amcl = 'amcl', - amik = 'amik', - amox = 'amox', - ampi = 'ampi', - azit = 'azit', - azlo = 'azlo', - aztr = 'aztr', - cefa = 'cefa', - cfep = 'cfep', - cfot = 'cfot', - cfox = 'cfox', - cfra = 'cfra', - cfta = 'cfta', - cftr = 'cftr', - cfur = 'cfur', - chlo = 'chlo', - cipr = 'cipr', - clar = 'clar', - clin = 'clin', - clox = 'clox', - coli = 'coli', - czol = 'czol', - dapt = 'dapt', - doxy = 'doxy', - erta = 'erta', - eryt = 'eryt', - fosf = 'fosf', - fusi = 'fusi', - gent = 'gent', - imip = 'imip', - kana = 'kana', - levo = 'levo', - linc = 'linc', - line = 'line', - mero = 'mero', - mezl = 'mezl', - mino = 'mino', - moxi = 'moxi', - nali = 'nali', - neom = 'neom', - neti = 'neti', - nitr = 'nitr', - norf = 'norf', - novo = 'novo', - oflo = 'oflo', - oxac = 'oxac', - peni = 'peni', - pipe = 'pipe', - pita = 'pita', - poly = 'poly', - pris = 'pris', - qida = 'qida', - rifa = 'rifa', - roxi = 'roxi', - siso = 'siso', - teic = 'teic', - tetr = 'tetr', - tica = 'tica', - tige = 'tige', - tobr = 'tobr', - trim = 'trim', - trsu = 'trsu', - vanc = 'vanc') { + amcl = guess_ab(), + amik = guess_ab(), + amox = guess_ab(), + ampi = guess_ab(), + azit = guess_ab(), + azlo = guess_ab(), + aztr = guess_ab(), + cefa = guess_ab(), + cfep = guess_ab(), + cfot = guess_ab(), + cfox = guess_ab(), + cfra = guess_ab(), + cfta = guess_ab(), + cftr = guess_ab(), + cfur = guess_ab(), + chlo = guess_ab(), + cipr = guess_ab(), + clar = guess_ab(), + clin = guess_ab(), + clox = guess_ab(), + coli = guess_ab(), + czol = guess_ab(), + dapt = guess_ab(), + doxy = guess_ab(), + erta = guess_ab(), + eryt = guess_ab(), + fosf = guess_ab(), + fusi = guess_ab(), + gent = guess_ab(), + imip = guess_ab(), + kana = guess_ab(), + levo = guess_ab(), + linc = guess_ab(), + line = guess_ab(), + mero = guess_ab(), + mezl = guess_ab(), + mino = guess_ab(), + moxi = guess_ab(), + nali = guess_ab(), + neom = guess_ab(), + neti = guess_ab(), + nitr = guess_ab(), + norf = guess_ab(), + novo = guess_ab(), + oflo = guess_ab(), + oxac = guess_ab(), + peni = guess_ab(), + pipe = guess_ab(), + pita = guess_ab(), + poly = guess_ab(), + pris = guess_ab(), + qida = guess_ab(), + rifa = guess_ab(), + roxi = guess_ab(), + siso = guess_ab(), + teic = guess_ab(), + tetr = guess_ab(), + tica = guess_ab(), + tige = guess_ab(), + tobr = guess_ab(), + trim = guess_ab(), + trsu = guess_ab(), + vanc = guess_ab()) { EUCAST_VERSION_BREAKPOINTS <- "8.1, 2018" EUCAST_VERSION_EXPERT_RULES <- "3.1, 2016" @@ -268,12 +268,80 @@ eucast_rules <- function(tbl, } # check columns + if (identical(amcl, as.name("guess_ab"))) { amcl <- guess_ab(tbl, "amcl", verbose = verbose) } + if (identical(amik, as.name("guess_ab"))) { amik <- guess_ab(tbl, "amik", verbose = verbose) } + if (identical(amox, as.name("guess_ab"))) { amox <- guess_ab(tbl, "amox", verbose = verbose) } + if (identical(ampi, as.name("guess_ab"))) { ampi <- guess_ab(tbl, "ampi", verbose = verbose) } + if (identical(azit, as.name("guess_ab"))) { azit <- guess_ab(tbl, "azit", verbose = verbose) } + if (identical(azlo, as.name("guess_ab"))) { azlo <- guess_ab(tbl, "azlo", verbose = verbose) } + if (identical(aztr, as.name("guess_ab"))) { aztr <- guess_ab(tbl, "aztr", verbose = verbose) } + if (identical(cefa, as.name("guess_ab"))) { cefa <- guess_ab(tbl, "cefa", verbose = verbose) } + if (identical(cfep, as.name("guess_ab"))) { cfep <- guess_ab(tbl, "cfep", verbose = verbose) } + if (identical(cfot, as.name("guess_ab"))) { cfot <- guess_ab(tbl, "cfot", verbose = verbose) } + if (identical(cfox, as.name("guess_ab"))) { cfox <- guess_ab(tbl, "cfox", verbose = verbose) } + if (identical(cfra, as.name("guess_ab"))) { cfra <- guess_ab(tbl, "cfra", verbose = verbose) } + if (identical(cfta, as.name("guess_ab"))) { cfta <- guess_ab(tbl, "cfta", verbose = verbose) } + if (identical(cftr, as.name("guess_ab"))) { cftr <- guess_ab(tbl, "cftr", verbose = verbose) } + if (identical(cfur, as.name("guess_ab"))) { cfur <- guess_ab(tbl, "cfur", verbose = verbose) } + if (identical(chlo, as.name("guess_ab"))) { chlo <- guess_ab(tbl, "chlo", verbose = verbose) } + if (identical(cipr, as.name("guess_ab"))) { cipr <- guess_ab(tbl, "cipr", verbose = verbose) } + if (identical(clar, as.name("guess_ab"))) { clar <- guess_ab(tbl, "clar", verbose = verbose) } + if (identical(clin, as.name("guess_ab"))) { clin <- guess_ab(tbl, "clin", verbose = verbose) } + if (identical(clox, as.name("guess_ab"))) { clox <- guess_ab(tbl, "clox", verbose = verbose) } + if (identical(coli, as.name("guess_ab"))) { coli <- guess_ab(tbl, "coli", verbose = verbose) } + if (identical(czol, as.name("guess_ab"))) { czol <- guess_ab(tbl, "czol", verbose = verbose) } + if (identical(dapt, as.name("guess_ab"))) { dapt <- guess_ab(tbl, "dapt", verbose = verbose) } + if (identical(doxy, as.name("guess_ab"))) { doxy <- guess_ab(tbl, "doxy", verbose = verbose) } + if (identical(erta, as.name("guess_ab"))) { erta <- guess_ab(tbl, "erta", verbose = verbose) } + if (identical(eryt, as.name("guess_ab"))) { eryt <- guess_ab(tbl, "eryt", verbose = verbose) } + if (identical(fosf, as.name("guess_ab"))) { fosf <- guess_ab(tbl, "fosf", verbose = verbose) } + if (identical(fusi, as.name("guess_ab"))) { fusi <- guess_ab(tbl, "fusi", verbose = verbose) } + if (identical(gent, as.name("guess_ab"))) { gent <- guess_ab(tbl, "gent", verbose = verbose) } + if (identical(imip, as.name("guess_ab"))) { imip <- guess_ab(tbl, "imip", verbose = verbose) } + if (identical(kana, as.name("guess_ab"))) { kana <- guess_ab(tbl, "kana", verbose = verbose) } + if (identical(levo, as.name("guess_ab"))) { levo <- guess_ab(tbl, "levo", verbose = verbose) } + if (identical(linc, as.name("guess_ab"))) { linc <- guess_ab(tbl, "linc", verbose = verbose) } + if (identical(line, as.name("guess_ab"))) { line <- guess_ab(tbl, "line", verbose = verbose) } + if (identical(mero, as.name("guess_ab"))) { mero <- guess_ab(tbl, "mero", verbose = verbose) } + if (identical(mezl, as.name("guess_ab"))) { mezl <- guess_ab(tbl, "mezl", verbose = verbose) } + if (identical(mino, as.name("guess_ab"))) { mino <- guess_ab(tbl, "mino", verbose = verbose) } + if (identical(moxi, as.name("guess_ab"))) { moxi <- guess_ab(tbl, "moxi", verbose = verbose) } + if (identical(nali, as.name("guess_ab"))) { nali <- guess_ab(tbl, "nali", verbose = verbose) } + if (identical(neom, as.name("guess_ab"))) { neom <- guess_ab(tbl, "neom", verbose = verbose) } + if (identical(neti, as.name("guess_ab"))) { neti <- guess_ab(tbl, "neti", verbose = verbose) } + if (identical(nitr, as.name("guess_ab"))) { nitr <- guess_ab(tbl, "nitr", verbose = verbose) } + if (identical(norf, as.name("guess_ab"))) { norf <- guess_ab(tbl, "norf", verbose = verbose) } + if (identical(novo, as.name("guess_ab"))) { novo <- guess_ab(tbl, "novo", verbose = verbose) } + if (identical(oflo, as.name("guess_ab"))) { oflo <- guess_ab(tbl, "oflo", verbose = verbose) } + if (identical(oxac, as.name("guess_ab"))) { oxac <- guess_ab(tbl, "oxac", verbose = verbose) } + if (identical(peni, as.name("guess_ab"))) { peni <- guess_ab(tbl, "peni", verbose = verbose) } + if (identical(pipe, as.name("guess_ab"))) { pipe <- guess_ab(tbl, "pipe", verbose = verbose) } + if (identical(pita, as.name("guess_ab"))) { pita <- guess_ab(tbl, "pita", verbose = verbose) } + if (identical(poly, as.name("guess_ab"))) { poly <- guess_ab(tbl, "poly", verbose = verbose) } + if (identical(pris, as.name("guess_ab"))) { pris <- guess_ab(tbl, "pris", verbose = verbose) } + if (identical(qida, as.name("guess_ab"))) { qida <- guess_ab(tbl, "qida", verbose = verbose) } + if (identical(rifa, as.name("guess_ab"))) { rifa <- guess_ab(tbl, "rifa", verbose = verbose) } + if (identical(roxi, as.name("guess_ab"))) { roxi <- guess_ab(tbl, "roxi", verbose = verbose) } + if (identical(siso, as.name("guess_ab"))) { siso <- guess_ab(tbl, "siso", verbose = verbose) } + if (identical(teic, as.name("guess_ab"))) { teic <- guess_ab(tbl, "teic", verbose = verbose) } + if (identical(tetr, as.name("guess_ab"))) { tetr <- guess_ab(tbl, "tetr", verbose = verbose) } + if (identical(tica, as.name("guess_ab"))) { tica <- guess_ab(tbl, "tica", verbose = verbose) } + if (identical(tige, as.name("guess_ab"))) { tige <- guess_ab(tbl, "tige", verbose = verbose) } + if (identical(tobr, as.name("guess_ab"))) { tobr <- guess_ab(tbl, "tobr", verbose = verbose) } + if (identical(trim, as.name("guess_ab"))) { trim <- guess_ab(tbl, "trim", verbose = verbose) } + if (identical(trsu, as.name("guess_ab"))) { trsu <- guess_ab(tbl, "trsu", verbose = verbose) } + if (identical(vanc, as.name("guess_ab"))) { vanc <- guess_ab(tbl, "vanc", verbose = verbose) } col.list <- c(amcl, amik, amox, ampi, azit, azlo, aztr, cefa, cfra, cfep, cfot, cfox, cfta, cftr, cfur, chlo, cipr, clar, clin, clox, coli, czol, dapt, doxy, erta, eryt, fosf, fusi, gent, imip, kana, levo, linc, line, mero, mezl, mino, moxi, nali, neom, neti, nitr, novo, norf, oflo, oxac, peni, pipe, pita, poly, pris, qida, rifa, roxi, siso, teic, tetr, tica, tige, tobr, trim, trsu, vanc) + if (length(col.list) < 63) { + warning('Some columns do not exist -- THIS MAY STRONGLY INFLUENCE THE OUTCOME.', + immediate. = TRUE, + call. = FALSE) + } col.list <- check_available_columns(tbl = tbl, col.list = col.list, info = info) amcl <- col.list[amcl] amik <- col.list[amik] @@ -339,8 +407,8 @@ eucast_rules <- function(tbl, trsu <- col.list[trsu] vanc <- col.list[vanc] - amount_changed <- 0 - amount_affected_rows <- integer(0) + number_changed <- 0 + number_affected_rows <- integer(0) verbose_info <- data.frame(rule_type = character(0), rule_set = character(0), force_to = character(0), @@ -352,7 +420,7 @@ eucast_rules <- function(tbl, # helper function for editing the table edit_rsi <- function(to, rule, rows, cols) { - cols <- unique(cols[!is.na(cols)]) + cols <- unique(cols[!is.na(cols) & !is.null(cols)]) if (length(rows) > 0 & length(cols) > 0) { before <- as.character(unlist(as.list(tbl_original[rows, cols]))) tryCatch( @@ -376,8 +444,8 @@ eucast_rules <- function(tbl, tbl[rows, cols] <<- to )) after <- as.character(unlist(as.list(tbl_original[rows, cols]))) - amount_changed <<- amount_changed + sum(before != after, na.rm = TRUE) - amount_affected_rows <<- unique(c(amount_affected_rows, rows)) + number_changed <<- number_changed + sum(before != after, na.rm = TRUE) + number_affected_rows <<- unique(c(number_affected_rows, rows)) changed_results <<- changed_results + sum(before != after, na.rm = TRUE) # will be reset at start of every rule if (verbose == TRUE) { @@ -399,7 +467,7 @@ eucast_rules <- function(tbl, } } na.rm <- function(col) { - if (is.na(col)) { + if (is.null(col)) { "" } else { col @@ -420,7 +488,7 @@ eucast_rules <- function(tbl, } # since ampicillin ^= amoxicillin, get the first from the latter (not in original EUCAST table) - if (!is.na(ampi) & !is.na(amox)) { + if (!is.null(ampi) & !is.null(amox)) { if (verbose == TRUE) { cat(bgGreen("\n VERBOSE: transforming", length(which(tbl[, amox] == "S" & !tbl[, ampi] %in% c("S", "I", "R"))), @@ -435,7 +503,7 @@ eucast_rules <- function(tbl, tbl[which(tbl[, amox] == "S" & !tbl[, ampi] %in% c("S", "I", "R")), ampi] <- "S" tbl[which(tbl[, amox] == "I" & !tbl[, ampi] %in% c("S", "I", "R")), ampi] <- "I" tbl[which(tbl[, amox] == "R" & !tbl[, ampi] %in% c("S", "I", "R")), ampi] <- "R" - } else if (is.na(ampi) & !is.na(amox)) { + } else if (is.null(ampi) & !is.null(amox)) { # ampicillin column is missing, but amoxicillin is available message(blue(paste0("NOTE: Using column `", bold(amox), "` as input for ampicillin (J01CA01) since many EUCAST rules depend on it."))) ampi <- amox @@ -471,21 +539,21 @@ eucast_rules <- function(tbl, cat(rule) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$order == 'Enterobacteriales' & tbl[, ampi] == 'S'), cols = amox) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'I', rule = c(rule_group, rule), rows = which(tbl$order == 'Enterobacteriales' & tbl[, ampi] == 'I'), cols = amox) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$order == 'Enterobacteriales' @@ -502,7 +570,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni) & !is.na(cfox)) { + if (!is.null(peni) & !is.null(cfox)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Staphylococcus" @@ -516,21 +584,21 @@ eucast_rules <- function(tbl, & tbl[, cfox] == 'S'), cols = c(oxac, clox)) } - if (!is.na(cfox)) { + if (!is.null(cfox)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$genus == "Staphylococcus" & tbl[, cfox] == 'R'), cols = all_betalactam) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Staphylococcus saprophyticus" & tbl[, ampi] == 'S'), cols = c(amox, amcl, pipe, pita)) } - if (!is.na(cfox)) { + if (!is.null(cfox)) { # inferred from cefoxitin edit_rsi(to = 'S', rule = c(rule_group, rule), @@ -548,14 +616,14 @@ eucast_rules <- function(tbl, & tbl[, cfox] == 'R'), cols = c(carbapenems, cephalosporins[cephalosporins != na.rm(cfta)])) } - if (!is.na(norf)) { + if (!is.null(norf)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Staphylococcus" & tbl[, norf] == 'S'), cols = c(cipr, levo, moxi, oflo)) } - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Staphylococcus" @@ -572,7 +640,7 @@ eucast_rules <- function(tbl, & tbl[, eryt] == 'R'), cols = c(azit, clar, roxi)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Staphylococcus" @@ -589,14 +657,14 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(ampi)) { # penicillin group + if (!is.null(ampi)) { # penicillin group edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Enterococcus faecium" & tbl[, ampi] == 'R'), cols = all_betalactam) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Enterococcus" @@ -613,7 +681,7 @@ eucast_rules <- function(tbl, & tbl[, ampi] == 'R'), cols = c(amox, amcl, pipe, pita)) } - if (!is.na(norf)) { + if (!is.null(norf)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Enterococcus" @@ -640,7 +708,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus (pyogenes|agalactiae|dysgalactiae|group A|group B|group C|group G)" @@ -657,14 +725,14 @@ eucast_rules <- function(tbl, & tbl[, peni] == 'R'), cols = c(aminopenicillins, ureidopenicillins, cephalosporins, carbapenems, clox, amcl)) } - if (!is.na(norf)) { + if (!is.null(norf)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus (pyogenes|agalactiae|dysgalactiae|group A|group B|group C|group G)" & tbl[, norf] == 'S'), cols = c(levo, moxi)) } - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus (pyogenes|agalactiae|dysgalactiae|group A|group B|group C|group G)" @@ -681,7 +749,7 @@ eucast_rules <- function(tbl, & tbl[, eryt] == 'R'), cols = c(azit, clar, roxi)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus (pyogenes|agalactiae|dysgalactiae|group A|group B|group C|group G)" @@ -698,14 +766,14 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus pneumoniae" & tbl[, peni] == 'S'), cols = c(ampi, amox, amcl, pipe, pita)) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus pneumoniae" @@ -722,14 +790,14 @@ eucast_rules <- function(tbl, & tbl[, ampi] == 'R'), cols = c(amox, amcl, pipe, pita)) } - if (!is.na(norf)) { + if (!is.null(norf)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus pneumoniae" & tbl[, norf] == 'S'), cols = c(levo, moxi)) } - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus pneumoniae" @@ -746,7 +814,7 @@ eucast_rules <- function(tbl, & tbl[, eryt] == 'R'), cols = c(azit, clar, roxi)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Streptococcus pneumoniae" @@ -768,14 +836,14 @@ eucast_rules <- function(tbl, "intermedius", "mitis", "mutans", "oligofermentans", "oralis", "parasanguinis", "peroris", "pseudopneumoniae", "salivarius", "sanguinis", "sinensis", "sobrinus", "thermophilus", "vestibularis") - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Streptococcus" & tbl$species %in% viridans_group & tbl[, peni] == 'S'), cols = c(ampi, amox, amcl, pipe, pita)) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus == "Streptococcus" & tbl$species %in% viridans_group @@ -802,7 +870,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Haemophilus influenzae" @@ -819,14 +887,14 @@ eucast_rules <- function(tbl, & tbl[, ampi] == 'R'), cols = c(amox, pipe)) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Haemophilus influenzae" & tbl[, peni] == 'S'), cols = c(ampi, amox, amcl, pipe, pita)) } - if (!is.na(amcl)) { + if (!is.null(amcl)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Haemophilus influenzae" @@ -843,14 +911,14 @@ eucast_rules <- function(tbl, & tbl[, amcl] == 'R'), cols = pita) } - if (!is.na(nali)) { + if (!is.null(nali)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Haemophilus influenzae" & tbl[, nali] == 'S'), cols = c(cipr, levo, moxi, oflo)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Haemophilus influenzae" @@ -867,7 +935,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(amcl)) { + if (!is.null(amcl)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Moraxella catarrhalis" @@ -884,14 +952,14 @@ eucast_rules <- function(tbl, & tbl[, amcl] == 'R'), cols = pita) } - if (!is.na(nali)) { + if (!is.null(nali)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Moraxella catarrhalis" & tbl[, nali] == 'S'), cols = c(cipr, levo, moxi, oflo)) } - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Moraxella catarrhalis" @@ -908,7 +976,7 @@ eucast_rules <- function(tbl, & tbl[, eryt] == 'R'), cols = c(azit, clar, roxi)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Moraxella catarrhalis" @@ -925,7 +993,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus %in% c("Clostridium", "Actinomyces", "Propionibacterium", @@ -961,7 +1029,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$genus %in% c("Bacteroides", "Prevotella", "Porphyromonas", @@ -991,7 +1059,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Pasteurella multocida" @@ -1018,7 +1086,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Campylobacter (jejuni|coli)" @@ -1035,7 +1103,7 @@ eucast_rules <- function(tbl, & tbl[, eryt] == 'R'), cols = c(azit, clar)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Campylobacter (jejuni|coli)" @@ -1062,7 +1130,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(norf)) { + if (!is.null(norf)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Aerococcus (sanguinicola|urinae)" @@ -1079,7 +1147,7 @@ eucast_rules <- function(tbl, & tbl[, norf] == 'R'), cols = fluoroquinolones) } - if (!is.na(cipr)) { + if (!is.null(cipr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Aerococcus (sanguinicola|urinae)" @@ -1106,7 +1174,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Kingella kingae" @@ -1123,7 +1191,7 @@ eucast_rules <- function(tbl, & tbl[, peni] == 'R'), cols = c(ampi, amox)) } - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Kingella kingae" @@ -1140,7 +1208,7 @@ eucast_rules <- function(tbl, & tbl[, eryt] == 'R'), cols = c(azit, clar)) } - if (!is.na(tetr)) { + if (!is.null(tetr)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% "^Kingella kingae" @@ -1442,7 +1510,7 @@ eucast_rules <- function(tbl, cat(rule) } # rule 8.3 - if (!is.na(peni)) { + if (!is.null(peni)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl$fullname %like% '^Streptococcus (pyogenes|agalactiae|dysgalactiae|group A|group B|group C|group G)' @@ -1450,14 +1518,14 @@ eucast_rules <- function(tbl, cols = c(aminopenicillins, cephalosporins, carbapenems)) } # rule 8.6 - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$genus == 'Enterococcus' & tbl[, ampi] == 'R'), cols = c(ureidopenicillins, carbapenems)) } - if (!is.na(amox)) { + if (!is.null(amox)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$genus == 'Enterococcus' @@ -1476,7 +1544,7 @@ eucast_rules <- function(tbl, cat(rule) } # rule 9.3 - if (!is.na(tica) & !is.na(pipe)) { + if (!is.null(tica) & !is.null(pipe)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$family == 'Enterobacteriaceae' @@ -1496,7 +1564,7 @@ eucast_rules <- function(tbl, cat(rule) } # rule 10.2 - # if (!is.na(ampi)) { + # if (!is.null(ampi)) { # you should know first if the are B-lactamase positive, so do not run for now # edit_rsi(to = 'R', # rule = c(rule_group, rule), @@ -1516,7 +1584,7 @@ eucast_rules <- function(tbl, cat(rule) } # rule 11.1 - if (!is.na(eryt)) { + if (!is.null(eryt)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl[, eryt] == 'S'), @@ -1542,7 +1610,7 @@ eucast_rules <- function(tbl, cat(rule) } # rule 12.2 - if (!is.na(tobr)) { + if (!is.null(tobr)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$genus == 'Staphylococcus' @@ -1550,7 +1618,7 @@ eucast_rules <- function(tbl, cols = c(kana, amik)) } # rule 12.3 - if (!is.na(gent)) { + if (!is.null(gent)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$genus == 'Staphylococcus' @@ -1558,7 +1626,7 @@ eucast_rules <- function(tbl, cols = aminoglycosides) } # rule 12.8 - if (!is.na(gent) & !is.na(tobr)) { + if (!is.null(gent) & !is.null(tobr)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$family == 'Enterobacteriaceae' @@ -1567,7 +1635,7 @@ eucast_rules <- function(tbl, cols = gent) } # rule 12.9 - if (!is.na(gent) & !is.na(tobr)) { + if (!is.null(gent) & !is.null(tobr)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$family == 'Enterobacteriaceae' @@ -1588,7 +1656,7 @@ eucast_rules <- function(tbl, cat(rule) } # rule 13.2 - if (!is.na(moxi)) { + if (!is.null(moxi)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$genus == 'Staphylococcus' @@ -1596,7 +1664,7 @@ eucast_rules <- function(tbl, cols = fluoroquinolones) } # rule 13.4 - if (!is.na(moxi)) { + if (!is.null(moxi)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$fullname %like% '^Streptococcus pneumoniae' @@ -1604,7 +1672,7 @@ eucast_rules <- function(tbl, cols = fluoroquinolones) } # rule 13.5 - if (!is.na(cipr)) { + if (!is.null(cipr)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$family == 'Enterobacteriaceae' @@ -1612,7 +1680,7 @@ eucast_rules <- function(tbl, cols = fluoroquinolones) } # rule 13.8 - if (!is.na(cipr)) { + if (!is.null(cipr)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl$fullname %like% '^Neisseria gonorrhoeae' @@ -1639,7 +1707,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(amcl)) { + if (!is.null(amcl)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl[, amcl] == 'R'), @@ -1654,7 +1722,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(pita)) { + if (!is.null(pita)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl[, pita] == 'R'), @@ -1669,7 +1737,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(trsu)) { + if (!is.null(trsu)) { edit_rsi(to = 'R', rule = c(rule_group, rule), rows = which(tbl[, trsu] == 'R'), @@ -1684,7 +1752,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(ampi)) { + if (!is.null(ampi)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl[, ampi] == 'S'), @@ -1699,7 +1767,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(pipe)) { + if (!is.null(pipe)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl[, pipe] == 'S'), @@ -1714,7 +1782,7 @@ eucast_rules <- function(tbl, changed_results <- 0 cat(rule) } - if (!is.na(trim)) { + if (!is.null(trim)) { edit_rsi(to = 'S', rule = c(rule_group, rule), rows = which(tbl[, trim] == 'S'), @@ -1737,7 +1805,7 @@ eucast_rules <- function(tbl, } else { wouldve <- "" } - if (amount_changed == 0) { + if (number_changed == 0) { colour <- green } else { colour <- blue @@ -1745,11 +1813,11 @@ eucast_rules <- function(tbl, decimal.mark <- getOption("OutDec") big.mark <- ifelse(decimal.mark != ",", ",", ".") cat(bold(paste('\n=> EUCAST rules', paste0(wouldve, 'affected'), - amount_affected_rows %>% length() %>% format(big.mark = big.mark, decimal.mark = decimal.mark), + number_affected_rows %>% length() %>% format(big.mark = big.mark, decimal.mark = decimal.mark), 'out of', nrow(tbl_original) %>% format(big.mark = big.mark, decimal.mark = decimal.mark), 'rows ->', colour(paste0(wouldve, 'changed'), - amount_changed %>% format(big.mark = big.mark, decimal.mark = decimal.mark), 'test results.\n\n')))) + number_changed %>% format(big.mark = big.mark, decimal.mark = decimal.mark), 'test results.\n\n')))) } if (verbose == TRUE) { diff --git a/R/guess_ab.R b/R/guess_ab.R new file mode 100644 index 00000000..70fd6dab --- /dev/null +++ b/R/guess_ab.R @@ -0,0 +1,76 @@ +# ==================================================================== # +# 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.gitab.io/AMR. # +# ==================================================================== # + +#' Guess antibiotic column +#' +#' This tries to find a column name in a data set based on information from the \code{\link{antibiotics}} data set. +#' @param tbl a \code{data.frame} +#' @param col a character to look for +#' @param verbose a logical to indicate whether additional info should be printed +#' @importFrom dplyr %>% select filter_all any_vars +#' @export +#' @inheritSection AMR Read more on our website! +# @examples +# +guess_ab <- function(tbl = NULL, col = NULL, verbose = FALSE) { + if (is.null(tbl) & is.null(col)) { + return(as.name("guess_ab")) + } + #stop("This function should not be called directly.") + if (length(col) > 1) { + warning("argument 'col' has length > 1 and only the first element will be used") + col <- col[1] + } + if (!is.data.frame(tbl)) { + stop("`tbl` must be a data.frame") + } + tbl_names <- colnames(tbl) + ab_result <- antibiotics %>% + select(atc:trade_name) %>% + filter_all(any_vars(tolower(.) == tolower(col))) + if (nrow(ab_result) > 1) { + # get most likely one + if (col %in% ab_result$atc) { + ab_result <- ab_result %>% filter(atc == col) + } else if (col %in% ab_result$certe) { + ab_result <- ab_result %>% filter(certe == col) + } else if (col %in% ab_result$umcg) { + ab_result <- ab_result %>% filter(umcg == col) + } else if (col %in% ab_result$umcg) { + ab_result <- ab_result %>% filter(official == col) + } else { + ab_result <- ab_result[1,] + } + } + tbl_result <- tbl_names[tbl_names %in% ab_result] + if (length(tbl_result) > 1) { + tbl_result <- tbl_result[1] + warning('using column `', tbl_result, '` for col "', col, '"', call. = FALSE) + } else if (length(tbl_result) == 0) { + if (verbose == TRUE) { + message('no result found for col "', col, '"') + } + return(NULL) + } else if (verbose == TRUE) { + message('using column `', tbl_result, '` for col "', col, '"') + } + tbl_result +} diff --git a/R/key_antibiotics.R b/R/key_antibiotics.R index 716e7780..9b37438e 100644 --- a/R/key_antibiotics.R +++ b/R/key_antibiotics.R @@ -78,24 +78,24 @@ #' # FALSE, because I is not ignored and so the 4th value differs key_antibiotics <- function(tbl, col_mo = NULL, - universal_1 = "amox", - universal_2 = "amcl", - universal_3 = "cfur", - universal_4 = "pita", - universal_5 = "cipr", - universal_6 = "trsu", - GramPos_1 = "vanc", - GramPos_2 = "teic", - GramPos_3 = "tetr", - GramPos_4 = "eryt", - GramPos_5 = "oxac", - GramPos_6 = "rifa", - GramNeg_1 = "gent", - GramNeg_2 = "tobr", - GramNeg_3 = "coli", - GramNeg_4 = "cfot", - GramNeg_5 = "cfta", - GramNeg_6 = "mero", + universal_1 = guess_ab(tbl, "amox"), + universal_2 = guess_ab(tbl, "amcl"), + universal_3 = guess_ab(tbl, "cfur"), + universal_4 = guess_ab(tbl, "pita"), + universal_5 = guess_ab(tbl, "cipr"), + universal_6 = guess_ab(tbl, "trsu"), + GramPos_1 = guess_ab(tbl, "vanc"), + GramPos_2 = guess_ab(tbl, "teic"), + GramPos_3 = guess_ab(tbl, "tetr"), + GramPos_4 = guess_ab(tbl, "eryt"), + GramPos_5 = guess_ab(tbl, "oxac"), + GramPos_6 = guess_ab(tbl, "rifa"), + GramNeg_1 = guess_ab(tbl, "gent"), + GramNeg_2 = guess_ab(tbl, "tobr"), + GramNeg_3 = guess_ab(tbl, "coli"), + GramNeg_4 = guess_ab(tbl, "cfot"), + GramNeg_5 = guess_ab(tbl, "cfta"), + GramNeg_6 = guess_ab(tbl, "mero"), warnings = TRUE, ...) { @@ -114,6 +114,7 @@ key_antibiotics <- function(tbl, GramPos_1, GramPos_2, GramPos_3, GramPos_4, GramPos_5, GramPos_6, GramNeg_1, GramNeg_2, GramNeg_3, GramNeg_4, GramNeg_5, GramNeg_6) col.list <- check_available_columns(tbl = tbl, col.list = col.list, info = warnings) + print(col.list) universal_1 <- col.list[universal_1] universal_2 <- col.list[universal_2] universal_3 <- col.list[universal_3] @@ -139,12 +140,12 @@ key_antibiotics <- function(tbl, gram_positive = c(universal, GramPos_1, GramPos_2, GramPos_3, GramPos_4, GramPos_5, GramPos_6) - gram_positive <- gram_positive[!is.na(gram_positive)] + gram_positive <- gram_positive[!is.null(gram_positive)] gram_negative = c(universal, GramNeg_1, GramNeg_2, GramNeg_3, GramNeg_4, GramNeg_5, GramNeg_6) - gram_negative <- gram_negative[!is.na(gram_negative)] + gram_negative <- gram_negative[!is.null(gram_negative)] # join to microorganisms data set tbl <- tbl %>% @@ -152,6 +153,9 @@ key_antibiotics <- function(tbl, left_join_microorganisms(by = col_mo) %>% mutate(key_ab = NA_character_) + print(as.character(gram_positive)) + print(gram_negative) + # Gram + tbl <- tbl %>% mutate(key_ab = if_else(gramstain == "Gram positive", diff --git a/R/mdro.R b/R/mdro.R index 6d2f6b5f..09deedcd 100755 --- a/R/mdro.R +++ b/R/mdro.R @@ -46,66 +46,66 @@ mdro <- function(tbl, country = NULL, col_mo = NULL, info = TRUE, - amcl = 'amcl', - amik = 'amik', - amox = 'amox', - ampi = 'ampi', - azit = 'azit', - aztr = 'aztr', - cefa = 'cefa', - cfra = 'cfra', - cfep = 'cfep', - cfot = 'cfot', - cfox = 'cfox', - cfta = 'cfta', - cftr = 'cftr', - cfur = 'cfur', - chlo = 'chlo', - cipr = 'cipr', - clar = 'clar', - clin = 'clin', - clox = 'clox', - coli = 'coli', - czol = 'czol', - dapt = 'dapt', - doxy = 'doxy', - erta = 'erta', - eryt = 'eryt', - fosf = 'fosf', - fusi = 'fusi', - gent = 'gent', - imip = 'imip', - kana = 'kana', - levo = 'levo', - linc = 'linc', - line = 'line', - mero = 'mero', - metr = 'metr', - mino = 'mino', - moxi = 'moxi', - nali = 'nali', - neom = 'neom', - neti = 'neti', - nitr = 'nitr', - novo = 'novo', - norf = 'norf', - oflo = 'oflo', - peni = 'peni', - pipe = 'pipe', - pita = 'pita', - poly = 'poly', - qida = 'qida', - rifa = 'rifa', - roxi = 'roxi', - siso = 'siso', - teic = 'teic', - tetr = 'tetr', - tica = 'tica', - tige = 'tige', - tobr = 'tobr', - trim = 'trim', - trsu = 'trsu', - vanc = 'vanc') { + amcl = guess_ab(), + amik = guess_ab(), + amox = guess_ab(), + ampi = guess_ab(), + azit = guess_ab(), + aztr = guess_ab(), + cefa = guess_ab(), + cfra = guess_ab(), + cfep = guess_ab(), + cfot = guess_ab(), + cfox = guess_ab(), + cfta = guess_ab(), + cftr = guess_ab(), + cfur = guess_ab(), + chlo = guess_ab(), + cipr = guess_ab(), + clar = guess_ab(), + clin = guess_ab(), + clox = guess_ab(), + coli = guess_ab(), + czol = guess_ab(), + dapt = guess_ab(), + doxy = guess_ab(), + erta = guess_ab(), + eryt = guess_ab(), + fosf = guess_ab(), + fusi = guess_ab(), + gent = guess_ab(), + imip = guess_ab(), + kana = guess_ab(), + levo = guess_ab(), + linc = guess_ab(), + line = guess_ab(), + mero = guess_ab(), + metr = guess_ab(), + mino = guess_ab(), + moxi = guess_ab(), + nali = guess_ab(), + neom = guess_ab(), + neti = guess_ab(), + nitr = guess_ab(), + novo = guess_ab(), + norf = guess_ab(), + oflo = guess_ab(), + peni = guess_ab(), + pipe = guess_ab(), + pita = guess_ab(), + poly = guess_ab(), + qida = guess_ab(), + rifa = guess_ab(), + roxi = guess_ab(), + siso = guess_ab(), + teic = guess_ab(), + tetr = guess_ab(), + tica = guess_ab(), + tige = guess_ab(), + tobr = guess_ab(), + trim = guess_ab(), + trsu = guess_ab(), + vanc = guess_ab()) { if (!is.data.frame(tbl)) { stop("`tbl` must be a data frame.", call. = FALSE) @@ -169,12 +169,77 @@ mdro <- function(tbl, } # check columns + if (identical(amcl, as.name("guess_ab"))) { amcl <- guess_ab(tbl, "amcl", verbose = info) } + if (identical(amik, as.name("guess_ab"))) { amik <- guess_ab(tbl, "amik", verbose = info) } + if (identical(amox, as.name("guess_ab"))) { amox <- guess_ab(tbl, "amox", verbose = info) } + if (identical(ampi, as.name("guess_ab"))) { ampi <- guess_ab(tbl, "ampi", verbose = info) } + if (identical(azit, as.name("guess_ab"))) { azit <- guess_ab(tbl, "azit", verbose = info) } + if (identical(aztr, as.name("guess_ab"))) { aztr <- guess_ab(tbl, "aztr", verbose = info) } + if (identical(cefa, as.name("guess_ab"))) { cefa <- guess_ab(tbl, "cefa", verbose = info) } + if (identical(cfra, as.name("guess_ab"))) { cfra <- guess_ab(tbl, "cfra", verbose = info) } + if (identical(cfep, as.name("guess_ab"))) { cfep <- guess_ab(tbl, "cfep", verbose = info) } + if (identical(cfot, as.name("guess_ab"))) { cfot <- guess_ab(tbl, "cfot", verbose = info) } + if (identical(cfox, as.name("guess_ab"))) { cfox <- guess_ab(tbl, "cfox", verbose = info) } + if (identical(cfta, as.name("guess_ab"))) { cfta <- guess_ab(tbl, "cfta", verbose = info) } + if (identical(cftr, as.name("guess_ab"))) { cftr <- guess_ab(tbl, "cftr", verbose = info) } + if (identical(cfur, as.name("guess_ab"))) { cfur <- guess_ab(tbl, "cfur", verbose = info) } + if (identical(chlo, as.name("guess_ab"))) { chlo <- guess_ab(tbl, "chlo", verbose = info) } + if (identical(cipr, as.name("guess_ab"))) { cipr <- guess_ab(tbl, "cipr", verbose = info) } + if (identical(clar, as.name("guess_ab"))) { clar <- guess_ab(tbl, "clar", verbose = info) } + if (identical(clin, as.name("guess_ab"))) { clin <- guess_ab(tbl, "clin", verbose = info) } + if (identical(clox, as.name("guess_ab"))) { clox <- guess_ab(tbl, "clox", verbose = info) } + if (identical(coli, as.name("guess_ab"))) { coli <- guess_ab(tbl, "coli", verbose = info) } + if (identical(czol, as.name("guess_ab"))) { czol <- guess_ab(tbl, "czol", verbose = info) } + if (identical(dapt, as.name("guess_ab"))) { dapt <- guess_ab(tbl, "dapt", verbose = info) } + if (identical(doxy, as.name("guess_ab"))) { doxy <- guess_ab(tbl, "doxy", verbose = info) } + if (identical(erta, as.name("guess_ab"))) { erta <- guess_ab(tbl, "erta", verbose = info) } + if (identical(eryt, as.name("guess_ab"))) { eryt <- guess_ab(tbl, "eryt", verbose = info) } + if (identical(fosf, as.name("guess_ab"))) { fosf <- guess_ab(tbl, "fosf", verbose = info) } + if (identical(fusi, as.name("guess_ab"))) { fusi <- guess_ab(tbl, "fusi", verbose = info) } + if (identical(gent, as.name("guess_ab"))) { gent <- guess_ab(tbl, "gent", verbose = info) } + if (identical(imip, as.name("guess_ab"))) { imip <- guess_ab(tbl, "imip", verbose = info) } + if (identical(kana, as.name("guess_ab"))) { kana <- guess_ab(tbl, "kana", verbose = info) } + if (identical(levo, as.name("guess_ab"))) { levo <- guess_ab(tbl, "levo", verbose = info) } + if (identical(linc, as.name("guess_ab"))) { linc <- guess_ab(tbl, "linc", verbose = info) } + if (identical(line, as.name("guess_ab"))) { line <- guess_ab(tbl, "line", verbose = info) } + if (identical(mero, as.name("guess_ab"))) { mero <- guess_ab(tbl, "mero", verbose = info) } + if (identical(metr, as.name("guess_ab"))) { metr <- guess_ab(tbl, "metr", verbose = info) } + if (identical(mino, as.name("guess_ab"))) { mino <- guess_ab(tbl, "mino", verbose = info) } + if (identical(moxi, as.name("guess_ab"))) { moxi <- guess_ab(tbl, "moxi", verbose = info) } + if (identical(nali, as.name("guess_ab"))) { nali <- guess_ab(tbl, "nali", verbose = info) } + if (identical(neom, as.name("guess_ab"))) { neom <- guess_ab(tbl, "neom", verbose = info) } + if (identical(neti, as.name("guess_ab"))) { neti <- guess_ab(tbl, "neti", verbose = info) } + if (identical(nitr, as.name("guess_ab"))) { nitr <- guess_ab(tbl, "nitr", verbose = info) } + if (identical(novo, as.name("guess_ab"))) { novo <- guess_ab(tbl, "novo", verbose = info) } + if (identical(norf, as.name("guess_ab"))) { norf <- guess_ab(tbl, "norf", verbose = info) } + if (identical(oflo, as.name("guess_ab"))) { oflo <- guess_ab(tbl, "oflo", verbose = info) } + if (identical(peni, as.name("guess_ab"))) { peni <- guess_ab(tbl, "peni", verbose = info) } + if (identical(pipe, as.name("guess_ab"))) { pipe <- guess_ab(tbl, "pipe", verbose = info) } + if (identical(pita, as.name("guess_ab"))) { pita <- guess_ab(tbl, "pita", verbose = info) } + if (identical(poly, as.name("guess_ab"))) { poly <- guess_ab(tbl, "poly", verbose = info) } + if (identical(qida, as.name("guess_ab"))) { qida <- guess_ab(tbl, "qida", verbose = info) } + if (identical(rifa, as.name("guess_ab"))) { rifa <- guess_ab(tbl, "rifa", verbose = info) } + if (identical(roxi, as.name("guess_ab"))) { roxi <- guess_ab(tbl, "roxi", verbose = info) } + if (identical(siso, as.name("guess_ab"))) { siso <- guess_ab(tbl, "siso", verbose = info) } + if (identical(teic, as.name("guess_ab"))) { teic <- guess_ab(tbl, "teic", verbose = info) } + if (identical(tetr, as.name("guess_ab"))) { tetr <- guess_ab(tbl, "tetr", verbose = info) } + if (identical(tica, as.name("guess_ab"))) { tica <- guess_ab(tbl, "tica", verbose = info) } + if (identical(tige, as.name("guess_ab"))) { tige <- guess_ab(tbl, "tige", verbose = info) } + if (identical(tobr, as.name("guess_ab"))) { tobr <- guess_ab(tbl, "tobr", verbose = info) } + if (identical(trim, as.name("guess_ab"))) { trim <- guess_ab(tbl, "trim", verbose = info) } + if (identical(trsu, as.name("guess_ab"))) { trsu <- guess_ab(tbl, "trsu", verbose = info) } + if (identical(vanc, as.name("guess_ab"))) { vanc <- guess_ab(tbl, "vanc", verbose = info) } col.list <- c(amcl, amik, amox, ampi, azit, aztr, cefa, cfra, cfep, cfot, cfox, cfta, cftr, cfur, chlo, cipr, clar, clin, clox, coli, czol, dapt, doxy, erta, eryt, fosf, fusi, gent, imip, kana, - levo, linc, line, mero, metr, mino, moxi, nali, neom, neti, nitr, - novo, norf, oflo, peni, pita, poly, qida, rifa, roxi, siso, - teic, tetr, tica, tige, tobr, trim, trsu, vanc) + levo, linc, line, mero, metr, mino, moxi, nali, neom, neti, + nitr, novo, norf, oflo, peni, pipe, pita, poly, qida, rifa, + roxi, siso, teic, tetr, tica, tige, tobr, trim, trsu, vanc) + if (length(col.list) < 60) { + warning('Some columns do not exist -- THIS MAY STRONGLY INFLUENCE THE OUTCOME.', + immediate. = TRUE, + call. = FALSE) + } col.list <- check_available_columns(tbl = tbl, col.list = col.list, info = info) amcl <- col.list[amcl] amik <- col.list[amik] @@ -221,6 +286,7 @@ mdro <- function(tbl, norf <- col.list[norf] oflo <- col.list[oflo] peni <- col.list[peni] + pipe <- col.list[pipe] pita <- col.list[pita] poly <- col.list[poly] qida <- col.list[qida] diff --git a/R/misc.R b/R/misc.R index 86c7c06e..c2b17c6a 100755 --- a/R/misc.R +++ b/R/misc.R @@ -80,7 +80,7 @@ check_available_columns <- function(tbl, col.list, info = TRUE) { } else if (tolower(col.list[i]) %in% colnames(tbl)) { col.list[i] <- tolower(col.list[i]) } else if (!col.list[i] %in% colnames(tbl)) { - col.list[i] <- NA + col.list[i] <- NULL } } if (!all(col.list %in% colnames(tbl))) { diff --git a/man/eucast_rules.Rd b/man/eucast_rules.Rd index 53475be6..33d13699 100644 --- a/man/eucast_rules.Rd +++ b/man/eucast_rules.Rd @@ -25,22 +25,27 @@ \usage{ eucast_rules(tbl, col_mo = NULL, info = TRUE, rules = c("breakpoints", "expert", "other", "all"), verbose = FALSE, - amcl = "amcl", amik = "amik", amox = "amox", ampi = "ampi", - azit = "azit", azlo = "azlo", aztr = "aztr", cefa = "cefa", - cfep = "cfep", cfot = "cfot", cfox = "cfox", cfra = "cfra", - cfta = "cfta", cftr = "cftr", cfur = "cfur", chlo = "chlo", - cipr = "cipr", clar = "clar", clin = "clin", clox = "clox", - coli = "coli", czol = "czol", dapt = "dapt", doxy = "doxy", - erta = "erta", eryt = "eryt", fosf = "fosf", fusi = "fusi", - gent = "gent", imip = "imip", kana = "kana", levo = "levo", - linc = "linc", line = "line", mero = "mero", mezl = "mezl", - mino = "mino", moxi = "moxi", nali = "nali", neom = "neom", - neti = "neti", nitr = "nitr", norf = "norf", novo = "novo", - oflo = "oflo", oxac = "oxac", peni = "peni", pipe = "pipe", - pita = "pita", poly = "poly", pris = "pris", qida = "qida", - rifa = "rifa", roxi = "roxi", siso = "siso", teic = "teic", - tetr = "tetr", tica = "tica", tige = "tige", tobr = "tobr", - trim = "trim", trsu = "trsu", vanc = "vanc") + amcl = guess_ab(), amik = guess_ab(), amox = guess_ab(), + ampi = guess_ab(), azit = guess_ab(), azlo = guess_ab(), + aztr = guess_ab(), cefa = guess_ab(), cfep = guess_ab(), + cfot = guess_ab(), cfox = guess_ab(), cfra = guess_ab(), + cfta = guess_ab(), cftr = guess_ab(), cfur = guess_ab(), + chlo = guess_ab(), cipr = guess_ab(), clar = guess_ab(), + clin = guess_ab(), clox = guess_ab(), coli = guess_ab(), + czol = guess_ab(), dapt = guess_ab(), doxy = guess_ab(), + erta = guess_ab(), eryt = guess_ab(), fosf = guess_ab(), + fusi = guess_ab(), gent = guess_ab(), imip = guess_ab(), + kana = guess_ab(), levo = guess_ab(), linc = guess_ab(), + line = guess_ab(), mero = guess_ab(), mezl = guess_ab(), + mino = guess_ab(), moxi = guess_ab(), nali = guess_ab(), + neom = guess_ab(), neti = guess_ab(), nitr = guess_ab(), + norf = guess_ab(), novo = guess_ab(), oflo = guess_ab(), + oxac = guess_ab(), peni = guess_ab(), pipe = guess_ab(), + pita = guess_ab(), poly = guess_ab(), pris = guess_ab(), + qida = guess_ab(), rifa = guess_ab(), roxi = guess_ab(), + siso = guess_ab(), teic = guess_ab(), tetr = guess_ab(), + tica = guess_ab(), tige = guess_ab(), tobr = guess_ab(), + trim = guess_ab(), trsu = guess_ab(), vanc = guess_ab()) EUCAST_rules(...) @@ -69,7 +74,7 @@ Apply susceptibility rules as defined by the European Committee on Antimicrobial } \section{Antibiotics}{ -To define antibiotics column names, input a text (case-insensitive) or use \code{NULL} to skip a column (e.g. \code{tica = NULL}). Non-existing columns will anyway be skipped with a warning. +To define antibiotics column names, leave as it is to determine it automatically with \code{\link{guess_ab}} or input a text (case-insensitive) or use \code{NULL} to skip a column (e.g. \code{tica = NULL}). Non-existing columns will anyway be skipped with a warning. Abbrevations of the column containing antibiotics in the form: \strong{abbreviation}: generic name (\emph{ATC code}) diff --git a/man/guess_ab.Rd b/man/guess_ab.Rd new file mode 100644 index 00000000..8ce0f929 --- /dev/null +++ b/man/guess_ab.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/guess_ab.R +\name{guess_ab} +\alias{guess_ab} +\title{Guess antibiotic column} +\usage{ +guess_ab(tbl = NULL, col = NULL, verbose = FALSE) +} +\arguments{ +\item{tbl}{a \code{data.frame}} + +\item{col}{a character to look for} + +\item{verbose}{a logical to indicate whether additional info should be printed} +} +\description{ +This tries to find a column name in a data set based on information from the \code{\link{antibiotics}} data set. +} +\section{Read more on our website!}{ + +\if{html}{\figure{logo.png}{options: height=40px style=margin-bottom:5px} \cr} +On our website \url{https://msberends.gitlab.io/AMR} you can find \href{https://msberends.gitlab.io/AMR/articles/AMR.html}{a omprehensive tutorial} about how to conduct AMR analysis and find \href{https://msberends.gitlab.io/AMR/reference}{the complete documentation of all functions}, which reads a lot easier than in R. +} + diff --git a/man/key_antibiotics.Rd b/man/key_antibiotics.Rd index ba9758d7..03686a7e 100755 --- a/man/key_antibiotics.Rd +++ b/man/key_antibiotics.Rd @@ -5,13 +5,17 @@ \alias{key_antibiotics_equal} \title{Key antibiotics for first \emph{weighted} isolates} \usage{ -key_antibiotics(tbl, col_mo = NULL, universal_1 = "amox", - universal_2 = "amcl", universal_3 = "cfur", universal_4 = "pita", - universal_5 = "cipr", universal_6 = "trsu", GramPos_1 = "vanc", - GramPos_2 = "teic", GramPos_3 = "tetr", GramPos_4 = "eryt", - GramPos_5 = "oxac", GramPos_6 = "rifa", GramNeg_1 = "gent", - GramNeg_2 = "tobr", GramNeg_3 = "coli", GramNeg_4 = "cfot", - GramNeg_5 = "cfta", GramNeg_6 = "mero", warnings = TRUE, ...) +key_antibiotics(tbl, col_mo = NULL, universal_1 = guess_ab(tbl, + "amox"), universal_2 = guess_ab(tbl, "amcl"), + universal_3 = guess_ab(tbl, "cfur"), universal_4 = guess_ab(tbl, + "pita"), universal_5 = guess_ab(tbl, "cipr"), + universal_6 = guess_ab(tbl, "trsu"), GramPos_1 = guess_ab(tbl, + "vanc"), GramPos_2 = guess_ab(tbl, "teic"), GramPos_3 = guess_ab(tbl, + "tetr"), GramPos_4 = guess_ab(tbl, "eryt"), GramPos_5 = guess_ab(tbl, + "oxac"), GramPos_6 = guess_ab(tbl, "rifa"), GramNeg_1 = guess_ab(tbl, + "gent"), GramNeg_2 = guess_ab(tbl, "tobr"), GramNeg_3 = guess_ab(tbl, + "coli"), GramNeg_4 = guess_ab(tbl, "cfot"), GramNeg_5 = guess_ab(tbl, + "cfta"), GramNeg_6 = guess_ab(tbl, "mero"), warnings = TRUE, ...) key_antibiotics_equal(x, y, type = c("keyantibiotics", "points"), ignore_I = TRUE, points_threshold = 2, info = FALSE) diff --git a/man/mdro.Rd b/man/mdro.Rd index e5c90a8e..a0915e83 100644 --- a/man/mdro.Rd +++ b/man/mdro.Rd @@ -8,21 +8,26 @@ \title{Determine multidrug-resistant organisms (MDRO)} \usage{ mdro(tbl, country = NULL, col_mo = NULL, info = TRUE, - amcl = "amcl", amik = "amik", amox = "amox", ampi = "ampi", - azit = "azit", aztr = "aztr", cefa = "cefa", cfra = "cfra", - cfep = "cfep", cfot = "cfot", cfox = "cfox", cfta = "cfta", - cftr = "cftr", cfur = "cfur", chlo = "chlo", cipr = "cipr", - clar = "clar", clin = "clin", clox = "clox", coli = "coli", - czol = "czol", dapt = "dapt", doxy = "doxy", erta = "erta", - eryt = "eryt", fosf = "fosf", fusi = "fusi", gent = "gent", - imip = "imip", kana = "kana", levo = "levo", linc = "linc", - line = "line", mero = "mero", metr = "metr", mino = "mino", - moxi = "moxi", nali = "nali", neom = "neom", neti = "neti", - nitr = "nitr", novo = "novo", norf = "norf", oflo = "oflo", - peni = "peni", pipe = "pipe", pita = "pita", poly = "poly", - qida = "qida", rifa = "rifa", roxi = "roxi", siso = "siso", - teic = "teic", tetr = "tetr", tica = "tica", tige = "tige", - tobr = "tobr", trim = "trim", trsu = "trsu", vanc = "vanc") + amcl = guess_ab(), amik = guess_ab(), amox = guess_ab(), + ampi = guess_ab(), azit = guess_ab(), aztr = guess_ab(), + cefa = guess_ab(), cfra = guess_ab(), cfep = guess_ab(), + cfot = guess_ab(), cfox = guess_ab(), cfta = guess_ab(), + cftr = guess_ab(), cfur = guess_ab(), chlo = guess_ab(), + cipr = guess_ab(), clar = guess_ab(), clin = guess_ab(), + clox = guess_ab(), coli = guess_ab(), czol = guess_ab(), + dapt = guess_ab(), doxy = guess_ab(), erta = guess_ab(), + eryt = guess_ab(), fosf = guess_ab(), fusi = guess_ab(), + gent = guess_ab(), imip = guess_ab(), kana = guess_ab(), + levo = guess_ab(), linc = guess_ab(), line = guess_ab(), + mero = guess_ab(), metr = guess_ab(), mino = guess_ab(), + moxi = guess_ab(), nali = guess_ab(), neom = guess_ab(), + neti = guess_ab(), nitr = guess_ab(), novo = guess_ab(), + norf = guess_ab(), oflo = guess_ab(), peni = guess_ab(), + pipe = guess_ab(), pita = guess_ab(), poly = guess_ab(), + qida = guess_ab(), rifa = guess_ab(), roxi = guess_ab(), + siso = guess_ab(), teic = guess_ab(), tetr = guess_ab(), + tica = guess_ab(), tige = guess_ab(), tobr = guess_ab(), + trim = guess_ab(), trsu = guess_ab(), vanc = guess_ab()) brmo(..., country = "nl") @@ -172,7 +177,7 @@ When \code{country} will be left blank, guidelines will be taken from EUCAST Exp } \section{Antibiotics}{ -To define antibiotics column names, input a text (case-insensitive) or use \code{NULL} to skip a column (e.g. \code{tica = NULL}). Non-existing columns will anyway be skipped with a warning. +To define antibiotics column names, leave as it is to determine it automatically with \code{\link{guess_ab}} or input a text (case-insensitive) or use \code{NULL} to skip a column (e.g. \code{tica = NULL}). Non-existing columns will anyway be skipped with a warning. Abbrevations of the column containing antibiotics in the form: \strong{abbreviation}: generic name (\emph{ATC code}) diff --git a/tests/testthat/test-eucast_rules.R b/tests/testthat/test-eucast_rules.R index 0127f575..5da53630 100755 --- a/tests/testthat/test-eucast_rules.R +++ b/tests/testthat/test-eucast_rules.R @@ -86,12 +86,14 @@ test_that("EUCAST rules work", { # amox is inferred by benzylpenicillin in Kingella kingae expect_equal( - as.list(eucast_rules( - data.frame(mo = as.mo("Kingella kingae"), - peni = "S", - amox = "-", - stringsAsFactors = FALSE) - , info = FALSE))$amox, + suppressWarnings( + as.list(eucast_rules( + data.frame(mo = as.mo("Kingella kingae"), + peni = "S", + amox = "-", + stringsAsFactors = FALSE) + , info = FALSE))$amox + ), "S") # also test norf diff --git a/tests/testthat/test-mdro.R b/tests/testthat/test-mdro.R index 608c902f..1606f590 100755 --- a/tests/testthat/test-mdro.R +++ b/tests/testthat/test-mdro.R @@ -42,7 +42,13 @@ test_that("mdro works", { expect_equal(outcome %>% freq() %>% pull(count), c(1989, 9, 2)) # 1989 neg, 9 pos, 2 unconfirmed - expect_equal(brmo(septic_patients, info = FALSE), mdro(septic_patients, "nl", info = FALSE)) + expect_equal( + suppressWarnings( + brmo(septic_patients, info = FALSE)), + suppressWarnings( + mdro(septic_patients, "nl", info = FALSE) + ) + ) # still working on German guidelines expect_error(suppressWarnings(mrgn(septic_patients, info = TRUE)))