2018-04-18 12:24:54 +02:00
# ==================================================================== #
# TITLE #
2021-02-02 23:57:35 +01:00
# Antimicrobial Resistance (AMR) Data Analysis for R #
2018-04-18 12:24:54 +02:00
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2018-04-18 12:24:54 +02:00
# #
# LICENCE #
2021-12-23 18:56:28 +01:00
# (c) 2018-2022 Berends MS, Luz CF et al. #
2020-10-08 11:16:03 +02:00
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
2022-08-28 10:31:50 +02:00
# Diagnostics & Advice, and University Medical Center Groningen. #
2018-04-18 12:24:54 +02:00
# #
2019-01-02 23:24:07 +01:00
# 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. #
2020-01-05 17:22:09 +01:00
# We created this package for both routine data analysis and academic #
# research and it was publicly released in the hope that it will be #
# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. #
2020-10-08 11:16:03 +02:00
# #
# Visit our website for the full manual and a complete tutorial about #
2021-02-02 23:57:35 +01:00
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
2018-04-18 12:24:54 +02:00
# ==================================================================== #
2021-01-18 16:57:56 +01:00
#' Determine Multidrug-Resistant Organisms (MDRO)
2018-04-18 12:24:54 +02:00
#'
2021-01-17 00:26:48 +01:00
#' Determine which isolates are multidrug-resistant organisms (MDRO) according to international, national and custom guidelines.
2021-01-12 22:08:04 +01:00
#' @param x a [data.frame] with antibiotics columns, like `AMX` or `amox`. Can be left blank for automatic determination.
2021-01-18 16:57:56 +01:00
#' @param guideline a specific guideline to follow, see sections *Supported international / national guidelines* and *Using Custom Guidelines* below. When left empty, the publication by Magiorakos *et al.* (see below) will be followed.
#' @param ... in case of [custom_mdro_guideline()]: a set of rules, see section *Using Custom Guidelines* below. Otherwise: column name of an antibiotic, see section *Antibiotics* below.
#' @param as_factor a [logical] to indicate whether the returned value should be an ordered [factor] (`TRUE`, default), or otherwise a [character] vector
2018-11-16 20:50:50 +01:00
#' @inheritParams eucast_rules
2019-11-28 22:32:17 +01:00
#' @param pct_required_classes minimal required percentage of antimicrobial classes that must be available per isolate, rounded down. For example, with the default guideline, 17 antimicrobial classes must be available for *S. aureus*. Setting this `pct_required_classes` argument to `0.5` (default) means that for every *S. aureus* isolate at least 8 different classes must be available. Any lower number of available classes will return `NA` for that isolate.
2020-09-29 23:35:46 +02:00
#' @param combine_SI a [logical] to indicate whether all values of S and I must be merged into one, so resistance is only considered when isolates are R, not I. As this is the default behaviour of the [mdro()] function, it follows the redefinition by EUCAST about the interpretation of I (increased exposure) in 2019, see section 'Interpretation of S, I and R' below. When using `combine_SI = FALSE`, resistance is considered when isolates are R or I.
2021-05-12 18:15:03 +02:00
#' @param verbose a [logical] to turn Verbose mode on and off (default is off). In Verbose mode, the function does not return the MDRO results, but instead returns a data set in logbook form with extensive info about which isolates would be MDRO-positive, or why they are not.
2018-11-16 20:50:50 +01:00
#' @inheritSection eucast_rules Antibiotics
2022-08-28 10:31:50 +02:00
#' @details
2021-05-17 19:43:01 +02:00
#' These functions are context-aware. This means that the `x` argument can be left blank if used inside a [data.frame] call, see *Examples*.
2022-08-28 10:31:50 +02:00
#'
2019-11-28 22:32:17 +01:00
#' For the `pct_required_classes` argument, values above 1 will be divided by 100. This is to support both fractions (`0.75` or `3/4`) and percentages (`75`).
2022-08-28 10:31:50 +02:00
#'
2021-01-17 00:26:48 +01:00
#' **Note:** Every test that involves the Enterobacteriaceae family, will internally be performed using its newly named *order* Enterobacterales, since the Enterobacteriaceae family has been taxonomically reclassified by Adeolu *et al.* in 2016. Before that, Enterobacteriaceae was the only family under the Enterobacteriales (with an i) order. All species under the old Enterobacteriaceae family are still under the new Enterobacterales (without an i) order, but divided into multiple families. The way tests are performed now by this [mdro()] function makes sure that results from before 2016 and after 2016 are identical.
2022-08-28 10:31:50 +02:00
#'
2021-01-18 16:57:56 +01:00
#' @section Supported International / National Guidelines:
2022-08-28 10:31:50 +02:00
#'
2019-11-03 22:24:42 +01:00
#' Currently supported guidelines are (case-insensitive):
2022-08-28 10:31:50 +02:00
#'
2020-12-07 16:06:42 +01:00
#' * `guideline = "CMI2012"` (default)
#'
2019-11-28 22:32:17 +01:00
#' Magiorakos AP, Srinivasan A *et al.* "Multidrug-resistant, extensively drug-resistant and pandrug-resistant bacteria: an international expert proposal for interim standard definitions for acquired resistance." Clinical Microbiology and Infection (2012) ([link](https://www.clinicalmicrobiologyandinfection.com/article/S1198-743X(14)61632-3/fulltext))
2022-08-28 10:31:50 +02:00
#'
2021-12-11 13:41:31 +01:00
#' * `guideline = "EUCAST3.3"` (or simply `guideline = "EUCAST"`)
#'
#' The European international guideline - EUCAST Expert Rules Version 3.3 "Intrinsic Resistance and Unusual Phenotypes" ([link](https://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/2021/Intrinsic_Resistance_and_Unusual_Phenotypes_Tables_v3.3_20211018.pdf))
2022-08-28 10:31:50 +02:00
#'
2021-12-11 13:41:31 +01:00
#' * `guideline = "EUCAST3.2"`
2020-12-07 16:06:42 +01:00
#'
2020-09-29 23:35:46 +02:00
#' The European international guideline - EUCAST Expert Rules Version 3.2 "Intrinsic Resistance and Unusual Phenotypes" ([link](https://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/2020/Intrinsic_Resistance_and_Unusual_Phenotypes_Tables_v3.2_20200225.pdf))
2022-08-28 10:31:50 +02:00
#'
2020-12-07 16:06:42 +01:00
#' * `guideline = "EUCAST3.1"`
#'
2020-09-29 23:35:46 +02:00
#' The European international guideline - EUCAST Expert Rules Version 3.1 "Intrinsic Resistance and Exceptional Phenotypes Tables" ([link](https://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/Expert_rules_intrinsic_exceptional_V3.1.pdf))
2022-08-28 10:31:50 +02:00
#'
2020-12-07 16:06:42 +01:00
#' * `guideline = "TB"`
#'
2022-03-24 23:05:04 +01:00
#' The international guideline for multi-drug resistant tuberculosis - World Health Organization "Companion handbook to the WHO guidelines for the programmatic management of drug-resistant tuberculosis" ([link](https://www.who.int/publications/i/item/9789241548809))
2022-08-28 10:31:50 +02:00
#'
2020-12-07 16:06:42 +01:00
#' * `guideline = "MRGN"`
#'
2021-01-06 11:16:17 +01:00
#' The German national guideline - Mueller et al. (2015) Antimicrobial Resistance and Infection Control 4:7; \doi{10.1186/s13756-015-0047-6}
2022-08-28 10:31:50 +02:00
#'
2020-12-07 16:06:42 +01:00
#' * `guideline = "BRMO"`
2019-05-23 16:58:59 +02:00
#'
2020-12-07 16:06:42 +01:00
#' The Dutch national guideline - Rijksinstituut voor Volksgezondheid en Milieu "WIP-richtlijn BRMO (Bijzonder Resistente Micro-Organismen) (ZKH)" ([link](https://www.rivm.nl/wip-richtlijn-brmo-bijzonder-resistente-micro-organismen-zkh))
2022-08-28 10:31:50 +02:00
#'
2020-07-08 14:48:06 +02:00
#' Please suggest your own (country-specific) guidelines by letting us know: <https://github.com/msberends/AMR/issues/new>.
2022-08-28 10:31:50 +02:00
#'
2021-01-18 16:57:56 +01:00
#' @section Using Custom Guidelines:
2022-08-28 10:31:50 +02:00
#'
2021-01-17 00:26:48 +01:00
#' Custom guidelines can be set with the [custom_mdro_guideline()] function. This is of great importance if you have custom rules to determine MDROs in your hospital, e.g., rules that are dependent on ward, state of contact isolation or other variables in your data.
2022-08-28 10:31:50 +02:00
#'
2021-04-20 10:46:17 +02:00
#' If you are familiar with the [`case_when()`][dplyr::case_when()] function of the `dplyr` package, you will recognise the input method to set your own rules. Rules must be set using what \R considers to be the 'formula notation'. The rule is written *before* the tilde (`~`) and the consequence of the rule is written *after* the tilde:
2022-08-28 10:31:50 +02:00
#'
2021-01-17 00:26:48 +01:00
#' ```
2021-01-18 16:57:56 +01:00
#' custom <- custom_mdro_guideline(CIP == "R" & age > 60 ~ "Elderly Type A",
#' ERY == "R" & age > 60 ~ "Elderly Type B")
2021-01-17 00:26:48 +01:00
#' ```
2022-08-28 10:31:50 +02:00
#'
#' If a row/an isolate matches the first rule, the value after the first `~` (in this case *'Elderly Type A'*) will be set as MDRO value. Otherwise, the second rule will be tried and so on. The number of rules is unlimited.
#'
2021-01-17 00:26:48 +01:00
#' You can print the rules set in the console for an overview. Colours will help reading it if your console supports colours.
2022-08-28 10:31:50 +02:00
#'
2021-01-17 00:26:48 +01:00
#' ```
#' custom
#' #> A set of custom MDRO rules:
2021-01-18 16:57:56 +01:00
#' #> 1. CIP is "R" and age is higher than 60 -> Elderly Type A
#' #> 2. ERY is "R" and age is higher than 60 -> Elderly Type B
#' #> 3. Otherwise -> Negative
2022-08-28 10:31:50 +02:00
#' #>
2021-01-17 00:26:48 +01:00
#' #> Unmatched rows will return NA.
#' ```
2022-08-28 10:31:50 +02:00
#'
2021-01-17 00:26:48 +01:00
#' The outcome of the function can be used for the `guideline` argument in the [mdro()] function:
2022-08-28 10:31:50 +02:00
#'
2021-01-17 00:26:48 +01:00
#' ```
2021-04-07 08:37:42 +02:00
#' x <- mdro(example_isolates,
#' guideline = custom)
2021-01-17 00:26:48 +01:00
#' table(x)
2021-04-07 08:37:42 +02:00
#' #> Negative Elderly Type A Elderly Type B
#' #> 1070 198 732
#' ```
2022-08-28 10:31:50 +02:00
#'
2021-04-07 08:37:42 +02:00
#' Rules can also be combined with other custom rules by using [c()]:
2022-08-28 10:31:50 +02:00
#'
2021-04-07 08:37:42 +02:00
#' ```
#' x <- mdro(example_isolates,
2022-08-28 10:31:50 +02:00
#' guideline = c(custom,
2021-04-07 08:37:42 +02:00
#' custom_mdro_guideline(ERY == "R" & age > 50 ~ "Elderly Type C")))
#' table(x)
2022-08-28 10:31:50 +02:00
#' #> Negative Elderly Type A Elderly Type B Elderly Type C
2021-04-07 08:37:42 +02:00
#' #> 961 198 732 109
2021-01-17 00:26:48 +01:00
#' ```
2022-08-28 10:31:50 +02:00
#'
2021-01-24 14:48:56 +01:00
#' The rules set (the `custom` object in this case) could be exported to a shared file location using [saveRDS()] if you collaborate with multiple users. The custom rules set could then be imported using [readRDS()].
2019-11-29 19:43:23 +01:00
#' @inheritSection as.rsi Interpretation of R and S/I
2019-11-28 22:32:17 +01:00
#' @return
#' - CMI 2012 paper - function [mdr_cmi2012()] or [mdro()]:\cr
2020-09-18 16:05:53 +02:00
#' Ordered [factor] with levels `Negative` < `Multi-drug-resistant (MDR)` < `Extensively drug-resistant (XDR)` < `Pandrug-resistant (PDR)`
2019-11-28 22:32:17 +01:00
#' - TB guideline - function [mdr_tb()] or [`mdro(..., guideline = "TB")`][mdro()]:\cr
2020-09-18 16:05:53 +02:00
#' Ordered [factor] with levels `Negative` < `Mono-resistant` < `Poly-resistant` < `Multi-drug-resistant` < `Extensively drug-resistant`
2019-11-28 22:32:17 +01:00
#' - German guideline - function [mrgn()] or [`mdro(..., guideline = "MRGN")`][mdro()]:\cr
2020-09-18 16:05:53 +02:00
#' Ordered [factor] with levels `Negative` < `3MRGN` < `4MRGN`
2021-01-17 00:26:48 +01:00
#' - Everything else, except for custom guidelines:\cr
2020-09-18 16:05:53 +02:00
#' Ordered [factor] with levels `Negative` < `Positive, unconfirmed` < `Positive`. The value `"Positive, unconfirmed"` means that, according to the guideline, it is not entirely sure if the isolate is multi-drug resistant and this should be confirmed with additional (e.g. molecular) tests
2018-11-16 20:50:50 +01:00
#' @rdname mdro
2019-11-06 14:43:23 +01:00
#' @aliases MDR XDR PDR BRMO 3MRGN 4MRGN
2018-04-18 12:24:54 +02:00
#' @export
2019-07-04 15:26:07 +02:00
#' @source
2021-05-12 18:15:03 +02:00
#' See the supported guidelines above for the [list] of publications used for this function.
2018-04-25 15:33:58 +02:00
#' @examples
2022-08-21 16:37:20 +02:00
#' out <- mdro(example_isolates, guideline = "EUCAST")
#' str(out)
#' table(out)
2022-08-28 10:31:50 +02:00
#'
2022-08-21 16:37:20 +02:00
#' out <- mdro(example_isolates,
2022-08-28 10:31:50 +02:00
#' guideline = custom_mdro_guideline(
#' AMX == "R" ~ "Custom MDRO 1",
#' VAN == "R" ~ "Custom MDRO 2"
#' )
#' )
2022-08-21 16:37:20 +02:00
#' table(out)
2022-08-28 10:31:50 +02:00
#'
2020-09-29 23:35:46 +02:00
#' \donttest{
#' if (require("dplyr")) {
#' example_isolates %>%
#' mdro() %>%
#' table()
2022-08-28 10:31:50 +02:00
#'
2020-12-07 16:06:42 +01:00
#' # no need to define `x` when used inside dplyr verbs:
2020-09-29 23:35:46 +02:00
#' example_isolates %>%
2022-08-21 16:37:20 +02:00
#' mutate(MDRO = mdro()) %>%
#' pull(MDRO) %>%
#' table()
2020-09-29 23:35:46 +02:00
#' }
2019-10-26 21:56:41 +02:00
#' }
2021-02-08 14:18:42 +01:00
mdro <- function ( x = NULL ,
2019-11-28 22:32:17 +01:00
guideline = " CMI2012" ,
2018-10-23 11:15:05 +02:00
col_mo = NULL ,
2020-02-21 21:13:38 +01:00
info = interactive ( ) ,
2019-11-03 22:24:42 +01:00
pct_required_classes = 0.5 ,
2019-11-06 14:43:23 +01:00
combine_SI = TRUE ,
verbose = FALSE ,
2021-02-08 14:18:42 +01:00
only_rsi_columns = FALSE ,
2019-05-10 16:44:59 +02:00
... ) {
2021-02-09 12:28:15 +01:00
if ( is_null_or_grouped_tbl ( x ) ) {
# when `x` is left blank, auto determine it (get_current_data() also contains dplyr::cur_data_all())
# is also fix for using a grouped df as input (a dot as first argument)
2021-06-22 12:16:42 +02:00
x <- tryCatch ( get_current_data ( arg_name = " x" , call = -2 ) , error = function ( e ) x )
2021-02-09 12:28:15 +01:00
}
meet_criteria ( x , allow_class = " data.frame" ) # also checks dimensions to be >0
2021-01-17 00:26:48 +01:00
meet_criteria ( guideline , allow_class = c ( " list" , " character" ) , allow_NULL = TRUE )
2021-01-17 10:35:21 +01:00
if ( ! is.list ( guideline ) ) {
meet_criteria ( guideline , allow_class = " character" , has_length = 1 , allow_NULL = TRUE )
}
2020-10-19 17:09:19 +02:00
meet_criteria ( col_mo , allow_class = " character" , has_length = 1 , is_in = colnames ( x ) , allow_NULL = TRUE )
meet_criteria ( info , allow_class = " logical" , has_length = 1 )
meet_criteria ( pct_required_classes , allow_class = " numeric" , has_length = 1 )
meet_criteria ( combine_SI , allow_class = " logical" , has_length = 1 )
meet_criteria ( verbose , allow_class = " logical" , has_length = 1 )
2021-02-08 14:18:42 +01:00
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
2021-02-08 14:18:42 +01:00
info.bak <- info
2021-07-23 21:42:11 +02:00
# don't thrown info's more than once per call
2022-08-21 16:37:20 +02:00
if ( isTRUE ( info ) ) {
info <- message_not_thrown_before ( " mdro" )
}
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( interactive ( ) && isTRUE ( verbose ) && isTRUE ( info ) ) {
2022-08-28 10:31:50 +02:00
txt <- paste0 (
" WARNING: In Verbose mode, the mdro() function does not return the MDRO results, but instead returns a data set in logbook form with extensive info about which isolates would be MDRO-positive, or why they are not." ,
" \n\nThis may overwrite your existing data if you use e.g.:" ,
" \ndata <- mdro(data, verbose = TRUE)\n\nDo you want to continue?"
)
2020-09-29 23:35:46 +02:00
showQuestion <- import_fn ( " showQuestion" , " rstudioapi" , error_on_fail = FALSE )
if ( ! is.null ( showQuestion ) ) {
2020-05-16 21:40:50 +02:00
q_continue <- showQuestion ( " Using verbose = TRUE with mdro()" , txt )
2019-11-03 22:24:42 +01:00
} else {
2020-05-16 13:05:47 +02:00
q_continue <- utils :: menu ( choices = c ( " OK" , " Cancel" ) , graphics = FALSE , title = txt )
2019-11-03 22:24:42 +01:00
}
if ( q_continue %in% c ( FALSE , 2 ) ) {
2020-10-27 15:56:51 +01:00
message_ ( " Cancelled, returning original data" , add_fn = font_red , as_note = FALSE )
2019-11-03 22:24:42 +01:00
return ( x )
}
}
2022-08-28 10:31:50 +02:00
2021-02-08 14:18:42 +01:00
group_msg <- " "
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info.bak ) ) {
2021-02-08 14:18:42 +01:00
# print group name if used in dplyr::group_by()
cur_group <- import_fn ( " cur_group" , " dplyr" , error_on_fail = FALSE )
if ( ! is.null ( cur_group ) ) {
group_df <- tryCatch ( cur_group ( ) , error = function ( e ) data.frame ( ) )
if ( NCOL ( group_df ) > 0 ) {
# transform factors to characters
group <- vapply ( FUN.VALUE = character ( 1 ) , group_df , function ( x ) {
if ( is.numeric ( x ) ) {
format ( x )
} else if ( is.logical ( x ) ) {
as.character ( x )
} else {
paste0 ( ' "' , x , ' "' )
}
} )
group_msg <- paste0 ( " \nGroup: " , paste0 ( names ( group ) , " = " , group , collapse = " , " ) , " \n" )
}
}
}
2021-02-09 12:28:15 +01:00
2021-05-12 18:15:03 +02:00
# force regular [data.frame], not a tibble or data.table
2020-05-16 13:05:47 +02:00
x <- as.data.frame ( x , stringsAsFactors = FALSE )
2022-08-28 10:31:50 +02:00
2019-11-03 22:24:42 +01:00
if ( pct_required_classes > 1 ) {
# allow pct_required_classes = 75 -> pct_required_classes = 0.75
pct_required_classes <- pct_required_classes / 100
}
2022-08-28 10:31:50 +02:00
2019-07-04 15:26:07 +02:00
if ( ! is.null ( list ( ... ) $ country ) ) {
2022-03-02 15:38:55 +01:00
warning_ ( " in `mdro()`: using `country` is deprecated, use `guideline` instead. See ?mdro" )
2019-07-04 15:26:07 +02:00
guideline <- list ( ... ) $ country
}
2022-08-28 10:31:50 +02:00
2021-01-15 22:44:52 +01:00
guideline.bak <- guideline
2021-01-17 00:26:48 +01:00
if ( is.list ( guideline ) ) {
# Custom MDRO guideline ---------------------------------------------------
stop_ifnot ( inherits ( guideline , " custom_mdro_guideline" ) , " use `custom_mdro_guideline()` to create custom guidelines" )
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info ) ) {
2022-08-28 10:31:50 +02:00
txt <- paste0 (
" Determining MDROs based on custom rules" ,
ifelse ( isTRUE ( attributes ( guideline ) $ as_factor ) ,
paste0 ( " , resulting in factor levels: " , paste0 ( attributes ( guideline ) $ values , collapse = " < " ) ) ,
" "
) ,
" ."
)
2021-01-18 16:57:56 +01:00
txt <- word_wrap ( txt )
cat ( txt , " \n" , sep = " " )
2021-01-17 00:26:48 +01:00
}
2021-04-07 08:37:42 +02:00
x <- run_custom_mdro_guideline ( df = x , guideline = guideline , info = info )
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info.bak ) ) {
2021-02-08 14:18:42 +01:00
cat ( group_msg )
2021-01-17 00:26:48 +01:00
if ( sum ( ! is.na ( x $ MDRO ) ) == 0 ) {
2021-01-18 16:57:56 +01:00
cat ( word_wrap ( font_bold ( paste0 ( " => Found 0 MDROs since no isolates are covered by the custom guideline" ) ) ) )
2021-01-17 00:26:48 +01:00
} else {
2022-08-28 10:31:50 +02:00
cat ( word_wrap ( font_bold ( paste0 (
" => Found " , sum ( x $ MDRO != " Negative" , na.rm = TRUE ) ,
" custom defined MDROs out of " , sum ( ! is.na ( x $ MDRO ) ) ,
" isolates (" ,
trimws ( percentage ( sum ( x $ MDRO != " Negative" , na.rm = TRUE ) / sum ( ! is.na ( x $ MDRO ) ) ) ) ,
" )\n"
) ) ) )
2021-01-17 00:26:48 +01:00
}
}
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2022-08-28 10:31:50 +02:00
return ( x [ , c (
" row_number" ,
" MDRO" ,
" reason" ,
" columns_nonsusceptible"
) ] )
2021-01-17 00:26:48 +01:00
} else {
return ( x $ MDRO )
}
}
2020-09-29 23:35:46 +02:00
guideline <- tolower ( gsub ( " [^a-zA-Z0-9.]+" , " " , guideline ) )
2019-05-23 16:58:59 +02:00
if ( is.null ( guideline ) ) {
2019-10-26 21:56:41 +02:00
# default to the paper by Magiorakos et al. (2012)
guideline <- " cmi2012"
2019-05-23 16:58:59 +02:00
}
2020-09-29 23:35:46 +02:00
if ( guideline == " eucast" ) {
# turn into latest EUCAST guideline
2021-12-11 15:29:30 +01:00
guideline <- " eucast3.3"
2020-09-29 23:35:46 +02:00
}
if ( guideline == " nl" ) {
guideline <- " brmo"
2019-07-04 15:26:07 +02:00
}
2020-09-29 23:35:46 +02:00
if ( guideline == " de" ) {
guideline <- " mrgn"
2019-07-04 15:26:07 +02:00
}
2022-08-28 10:31:50 +02:00
stop_ifnot (
guideline %in% c ( " brmo" , " mrgn" , " eucast3.1" , " eucast3.2" , " eucast3.3" , " tb" , " cmi2012" ) ,
" invalid guideline: " , guideline.bak
)
2020-09-29 23:35:46 +02:00
guideline <- list ( code = guideline )
2022-08-28 10:31:50 +02:00
2018-10-23 11:15:05 +02:00
# try to find columns based on type
# -- mo
2019-01-15 12:45:24 +01:00
if ( is.null ( col_mo ) ) {
2020-09-24 00:30:11 +02:00
col_mo <- search_type_in_df ( x = x , type = " mo" , info = info )
2019-05-23 16:58:59 +02:00
}
2022-09-16 23:15:23 +02:00
if ( is.null ( col_mo ) && guideline $ code == " tb" ) {
2022-08-28 10:31:50 +02:00
message_ (
" No column found as input for `col_mo`, " ,
font_bold ( paste0 ( " assuming all rows contain " , font_italic ( " Mycobacterium tuberculosis" ) , " ." ) )
)
2021-01-15 22:44:52 +01:00
x $ mo <- as.mo ( " Mycobacterium tuberculosis" ) # consider overkill at all times: MO_lookup[which(MO_lookup$fullname == "Mycobacterium tuberculosis"), "mo", drop = TRUE]
col_mo <- " mo"
2018-12-22 22:39:34 +01:00
}
2020-06-22 11:18:40 +02:00
stop_if ( is.null ( col_mo ) , " `col_mo` must be set" )
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
if ( guideline $ code == " cmi2012" ) {
guideline $ name <- " Multidrug-resistant, extensively drug-resistant and pandrug-resistant bacteria: an international expert proposal for interim standard definitions for acquired resistance."
guideline $ author <- " Magiorakos AP, Srinivasan A, Carey RB, ..., Vatopoulos A, Weber JT, Monnet DL"
2020-12-24 23:29:10 +01:00
guideline $ version <- NA
2021-01-06 11:16:17 +01:00
guideline $ source_url <- " Clinical Microbiology and Infection 18:3, 2012; doi: 10.1111/j.1469-0691.2011.03570.x"
2020-12-07 16:06:42 +01:00
guideline $ type <- " MDRs/XDRs/PDRs"
2020-09-29 23:35:46 +02:00
} else if ( guideline $ code == " eucast3.1" ) {
2019-05-20 19:12:41 +02:00
guideline $ name <- " EUCAST Expert Rules, \"Intrinsic Resistance and Exceptional Phenotypes Tables\""
2019-05-23 16:58:59 +02:00
guideline $ author <- " EUCAST (European Committee on Antimicrobial Susceptibility Testing)"
2020-09-29 23:35:46 +02:00
guideline $ version <- " 3.1, 2016"
2020-12-28 22:24:33 +01:00
guideline $ source_url <- " https://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/Expert_rules_intrinsic_exceptional_V3.1.pdf"
2020-12-07 16:06:42 +01:00
guideline $ type <- " EUCAST Exceptional Phenotypes"
} else if ( guideline $ code == " eucast3.2" ) {
guideline $ name <- " EUCAST Expert Rules, \"Intrinsic Resistance and Unusual Phenotypes\""
guideline $ author <- " EUCAST (European Committee on Antimicrobial Susceptibility Testing)"
2021-12-11 13:41:31 +01:00
guideline $ version <- " 3.2, February 2020"
2020-12-28 22:24:33 +01:00
guideline $ source_url <- " https://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/2020/Intrinsic_Resistance_and_Unusual_Phenotypes_Tables_v3.2_20200225.pdf"
2020-12-07 16:06:42 +01:00
guideline $ type <- " EUCAST Unusual Phenotypes"
2021-12-11 13:41:31 +01:00
} else if ( guideline $ code == " eucast3.3" ) {
guideline $ name <- " EUCAST Expert Rules, \"Intrinsic Resistance and Unusual Phenotypes\""
guideline $ author <- " EUCAST (European Committee on Antimicrobial Susceptibility Testing)"
guideline $ version <- " 3.3, October 2021"
guideline $ source_url <- " https://www.eucast.org/fileadmin/src/media/PDFs/EUCAST_files/Expert_Rules/2021/Intrinsic_Resistance_and_Unusual_Phenotypes_Tables_v3.3_20211018.pdf"
guideline $ type <- " EUCAST Unusual Phenotypes"
2019-05-23 16:58:59 +02:00
} 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)"
2020-09-29 23:35:46 +02:00
guideline $ version <- " WHO/HTM/TB/2014.11, 2014"
2022-03-24 23:05:04 +01:00
guideline $ source_url <- " https://www.who.int/publications/i/item/9789241548809"
2020-12-07 16:06:42 +01:00
guideline $ type <- " MDR-TB's"
2022-08-28 10:31:50 +02:00
2018-04-25 15:33:58 +02:00
# support per country:
2019-07-04 15:26:07 +02:00
} else if ( guideline $ code == " mrgn" ) {
2019-10-07 14:32:06 +02:00
guideline $ name <- " Cross-border comparison of the Dutch and German guidelines on multidrug-resistant Gram-negative microorganisms"
2019-10-26 21:56:41 +02:00
guideline $ author <- " M\u00fcller J, Voss A, K\u00f6ck R, ..., Kern WV, Wendt C, Friedrich AW"
2020-12-24 23:29:10 +01:00
guideline $ version <- NA
2021-01-06 11:16:17 +01:00
guideline $ source_url <- " Antimicrobial Resistance and Infection Control 4:7, 2015; doi: 10.1186/s13756-015-0047-6"
2020-12-07 16:06:42 +01:00
guideline $ type <- " MRGNs"
2019-07-04 15:26:07 +02:00
} else if ( guideline $ code == " brmo" ) {
2019-05-23 16:58:59 +02:00
guideline $ name <- " WIP-Richtlijn Bijzonder Resistente Micro-organismen (BRMO)"
guideline $ author <- " RIVM (Rijksinstituut voor de Volksgezondheid)"
2019-05-20 19:12:41 +02:00
guideline $ version <- " Revision as of December 2017"
2020-12-28 22:24:33 +01:00
guideline $ source_url <- " https://www.rivm.nl/Documenten_en_publicaties/Professioneel_Praktisch/Richtlijnen/Infectieziekten/WIP_Richtlijnen/WIP_Richtlijnen/Ziekenhuizen/WIP_richtlijn_BRMO_Bijzonder_Resistente_Micro_Organismen_ZKH"
2020-12-07 16:06:42 +01:00
guideline $ type <- " BRMOs"
2018-04-18 12:24:54 +02:00
} else {
2019-05-23 16:58:59 +02:00
stop ( " This guideline is currently unsupported: " , guideline $ code , call. = FALSE )
2018-04-18 12:24:54 +02:00
}
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
if ( guideline $ code == " cmi2012" ) {
2022-08-28 10:31:50 +02:00
cols_ab <- get_column_abx (
x = x ,
soft_dependencies = c (
# [table] 1 (S aureus):
" GEN" ,
" RIF" ,
" CPT" ,
" OXA" ,
" CIP" ,
" MFX" ,
" SXT" ,
" FUS" ,
" VAN" ,
" TEC" ,
" TLV" ,
" TGC" ,
" CLI" ,
" DAP" ,
" ERY" ,
" LNZ" ,
" CHL" ,
" FOS" ,
" QDA" ,
" TCY" ,
" DOX" ,
" MNO" ,
# [table] 2 (Enterococcus)
" GEH" ,
" STH" ,
" IPM" ,
" MEM" ,
" DOR" ,
" CIP" ,
" LVX" ,
" MFX" ,
" VAN" ,
" TEC" ,
" TGC" ,
" DAP" ,
" LNZ" ,
" AMP" ,
" QDA" ,
" DOX" ,
" MNO" ,
# [table] 3 (Enterobacteriaceae)
" GEN" ,
" TOB" ,
" AMK" ,
" NET" ,
" CPT" ,
" TCC" ,
" TZP" ,
" ETP" ,
" IPM" ,
" MEM" ,
" DOR" ,
" CZO" ,
" CXM" ,
" CTX" ,
" CAZ" ,
" FEP" ,
" FOX" ,
" CTT" ,
" CIP" ,
" SXT" ,
" TGC" ,
" ATM" ,
" AMP" ,
" AMC" ,
" SAM" ,
" CHL" ,
" FOS" ,
" COL" ,
" TCY" ,
" DOX" ,
" MNO" ,
# [table] 4 (Pseudomonas)
" GEN" ,
" TOB" ,
" AMK" ,
" NET" ,
" IPM" ,
" MEM" ,
" DOR" ,
" CAZ" ,
" FEP" ,
" CIP" ,
" LVX" ,
" TCC" ,
" TZP" ,
" ATM" ,
" FOS" ,
" COL" ,
" PLB" ,
# [table] 5 (Acinetobacter)
" GEN" ,
" TOB" ,
" AMK" ,
" NET" ,
" IPM" ,
" MEM" ,
" DOR" ,
" CIP" ,
" LVX" ,
" TZP" ,
" TCC" ,
" CTX" ,
" CRO" ,
" CAZ" ,
" FEP" ,
" SXT" ,
" SAM" ,
" COL" ,
" PLB" ,
" TCY" ,
" DOX" ,
" MNO"
) ,
verbose = verbose ,
info = info ,
only_rsi_columns = only_rsi_columns ,
fn = " mdro" ,
...
)
2020-09-29 23:35:46 +02:00
} else if ( guideline $ code == " eucast3.2" ) {
2022-08-28 10:31:50 +02:00
cols_ab <- get_column_abx (
x = x ,
soft_dependencies = c (
" AMP" ,
" AMX" ,
" CIP" ,
" DAL" ,
" DAP" ,
" ERV" ,
" FDX" ,
" GEN" ,
" LNZ" ,
" MEM" ,
" MTR" ,
" OMC" ,
" ORI" ,
" PEN" ,
" QDA" ,
" RIF" ,
" TEC" ,
" TGC" ,
" TLV" ,
" TOB" ,
" TZD" ,
" VAN"
) ,
verbose = verbose ,
info = info ,
only_rsi_columns = only_rsi_columns ,
fn = " mdro" ,
...
)
2021-12-11 13:41:31 +01:00
} else if ( guideline $ code == " eucast3.3" ) {
2022-08-28 10:31:50 +02:00
cols_ab <- get_column_abx (
x = x ,
soft_dependencies = c (
" AMP" ,
" AMX" ,
" CIP" ,
" DAL" ,
" DAP" ,
" ERV" ,
" FDX" ,
" GEN" ,
" LNZ" ,
" MEM" ,
" MTR" ,
" OMC" ,
" ORI" ,
" PEN" ,
" QDA" ,
" RIF" ,
" TEC" ,
" TGC" ,
" TLV" ,
" TOB" ,
" TZD" ,
" VAN"
) ,
verbose = verbose ,
info = info ,
only_rsi_columns = only_rsi_columns ,
fn = " mdro" ,
...
)
2019-10-26 21:56:41 +02:00
} else if ( guideline $ code == " tb" ) {
2022-08-28 10:31:50 +02:00
cols_ab <- get_column_abx (
x = x ,
soft_dependencies = c (
" CAP" ,
" ETH" ,
" GAT" ,
" INH" ,
" PZA" ,
" RIF" ,
" RIB" ,
" RFP"
) ,
verbose = verbose ,
info = info ,
only_rsi_columns = only_rsi_columns ,
fn = " mdro" ,
...
)
2019-10-07 14:32:06 +02:00
} else if ( guideline $ code == " mrgn" ) {
2022-08-28 10:31:50 +02:00
cols_ab <- get_column_abx (
x = x ,
soft_dependencies = c (
" PIP" ,
" CTX" ,
" CAZ" ,
" IPM" ,
" MEM" ,
" CIP"
) ,
verbose = verbose ,
info = info ,
only_rsi_columns = only_rsi_columns ,
fn = " mdro" ,
...
)
2019-05-23 16:58:59 +02:00
} else {
2022-08-28 10:31:50 +02:00
cols_ab <- get_column_abx (
x = x ,
verbose = verbose ,
info = info ,
only_rsi_columns = only_rsi_columns ,
fn = " mdro" ,
...
)
2019-05-23 16:58:59 +02:00
}
2022-09-16 23:15:23 +02:00
if ( ! " AMP" %in% names ( cols_ab ) && " AMX" %in% names ( cols_ab ) ) {
2021-11-28 23:01:26 +01:00
# ampicillin column is missing, but amoxicillin is available
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info ) ) {
2021-11-28 23:01:26 +01:00
message_ ( " Using column '" , cols_ab [names ( cols_ab ) == " AMX" ] , " ' as input for ampicillin since many EUCAST rules depend on it." )
}
cols_ab <- c ( cols_ab , c ( AMP = unname ( cols_ab [names ( cols_ab ) == " AMX" ] ) ) )
}
2021-02-08 14:18:42 +01:00
2020-12-28 22:24:33 +01:00
# nolint start
2019-05-20 19:12:41 +02:00
AMC <- cols_ab [ " AMC" ]
AMK <- cols_ab [ " AMK" ]
AMP <- cols_ab [ " AMP" ]
AMX <- cols_ab [ " AMX" ]
ATM <- cols_ab [ " ATM" ]
AZL <- cols_ab [ " AZL" ]
AZM <- cols_ab [ " AZM" ]
2020-09-29 23:35:46 +02:00
BPR <- cols_ab [ " BPR" ]
CAC <- cols_ab [ " CAC" ]
CAT <- cols_ab [ " CAT" ]
2019-05-20 19:12:41 +02:00
CAZ <- cols_ab [ " CAZ" ]
2020-09-29 23:35:46 +02:00
CCV <- cols_ab [ " CCV" ]
CDR <- cols_ab [ " CDR" ]
CDZ <- cols_ab [ " CDZ" ]
CEC <- cols_ab [ " CEC" ]
2019-05-20 19:12:41 +02:00
CED <- cols_ab [ " CED" ]
2020-09-29 23:35:46 +02:00
CEI <- cols_ab [ " CEI" ]
CEP <- cols_ab [ " CEP" ]
CFM <- cols_ab [ " CFM" ]
CFM1 <- cols_ab [ " CFM1" ]
CFP <- cols_ab [ " CFP" ]
CFR <- cols_ab [ " CFR" ]
CFS <- cols_ab [ " CFS" ]
2019-05-20 19:12:41 +02:00
CHL <- cols_ab [ " CHL" ]
2020-09-29 23:35:46 +02:00
CID <- cols_ab [ " CID" ]
2019-05-20 19:12:41 +02:00
CIP <- cols_ab [ " CIP" ]
CLI <- cols_ab [ " CLI" ]
CLR <- cols_ab [ " CLR" ]
2020-09-29 23:35:46 +02:00
CMX <- cols_ab [ " CMX" ]
CMZ <- cols_ab [ " CMZ" ]
CND <- cols_ab [ " CND" ]
2019-05-20 19:12:41 +02:00
COL <- cols_ab [ " COL" ]
2020-09-29 23:35:46 +02:00
CPD <- cols_ab [ " CPD" ]
CPM <- cols_ab [ " CPM" ]
CPO <- cols_ab [ " CPO" ]
CPR <- cols_ab [ " CPR" ]
2019-10-26 21:56:41 +02:00
CPT <- cols_ab [ " CPT" ]
2020-09-29 23:35:46 +02:00
CRD <- cols_ab [ " CRD" ]
2019-05-20 19:12:41 +02:00
CRO <- cols_ab [ " CRO" ]
2020-09-29 23:35:46 +02:00
CSL <- cols_ab [ " CSL" ]
CTB <- cols_ab [ " CTB" ]
CTF <- cols_ab [ " CTF" ]
CTL <- cols_ab [ " CTL" ]
2019-10-26 21:56:41 +02:00
CTT <- cols_ab [ " CTT" ]
2019-05-20 19:12:41 +02:00
CTX <- cols_ab [ " CTX" ]
2020-09-29 23:35:46 +02:00
CTZ <- cols_ab [ " CTZ" ]
2019-05-20 19:12:41 +02:00
CXM <- cols_ab [ " CXM" ]
2020-09-29 23:35:46 +02:00
CZD <- cols_ab [ " CZD" ]
2019-05-20 19:12:41 +02:00
CZO <- cols_ab [ " CZO" ]
2020-09-29 23:35:46 +02:00
CZX <- cols_ab [ " CZX" ]
DAL <- cols_ab [ " DAL" ]
2019-05-20 19:12:41 +02:00
DAP <- cols_ab [ " DAP" ]
2020-09-29 23:35:46 +02:00
DIT <- cols_ab [ " DIT" ]
DIZ <- cols_ab [ " DIZ" ]
2019-10-26 21:56:41 +02:00
DOR <- cols_ab [ " DOR" ]
2019-05-20 19:12:41 +02:00
DOX <- cols_ab [ " DOX" ]
2020-09-29 23:35:46 +02:00
ENX <- cols_ab [ " ENX" ]
ERV <- cols_ab [ " ERV" ]
2019-05-20 19:12:41 +02:00
ERY <- cols_ab [ " ERY" ]
ETP <- cols_ab [ " ETP" ]
2020-09-29 23:35:46 +02:00
FDX <- cols_ab [ " FDX" ]
2019-05-20 19:12:41 +02:00
FEP <- cols_ab [ " FEP" ]
FLC <- cols_ab [ " FLC" ]
2020-09-29 23:35:46 +02:00
FLE <- cols_ab [ " FLE" ]
2019-05-20 19:12:41 +02:00
FOS <- cols_ab [ " FOS" ]
FOX <- cols_ab [ " FOX" ]
FUS <- cols_ab [ " FUS" ]
2020-09-29 23:35:46 +02:00
GAT <- cols_ab [ " GAT" ]
2019-10-26 21:56:41 +02:00
GEH <- cols_ab [ " GEH" ]
2020-09-29 23:35:46 +02:00
GEM <- cols_ab [ " GEM" ]
2019-05-20 19:12:41 +02:00
GEN <- cols_ab [ " GEN" ]
2020-09-29 23:35:46 +02:00
GRX <- cols_ab [ " GRX" ]
HAP <- cols_ab [ " HAP" ]
2019-05-20 19:12:41 +02:00
IPM <- cols_ab [ " IPM" ]
KAN <- cols_ab [ " KAN" ]
2020-09-29 23:35:46 +02:00
LEX <- cols_ab [ " LEX" ]
2019-05-20 19:12:41 +02:00
LIN <- cols_ab [ " LIN" ]
LNZ <- cols_ab [ " LNZ" ]
2020-09-29 23:35:46 +02:00
LOM <- cols_ab [ " LOM" ]
LOR <- cols_ab [ " LOR" ]
LTM <- cols_ab [ " LTM" ]
2019-05-20 19:12:41 +02:00
LVX <- cols_ab [ " LVX" ]
2020-09-29 23:35:46 +02:00
MAN <- cols_ab [ " MAN" ]
2019-05-20 19:12:41 +02:00
MEM <- cols_ab [ " MEM" ]
2020-09-29 23:35:46 +02:00
MEV <- cols_ab [ " MEV" ]
2019-05-20 19:12:41 +02:00
MEZ <- cols_ab [ " MEZ" ]
MFX <- cols_ab [ " MFX" ]
MNO <- cols_ab [ " MNO" ]
2020-09-29 23:35:46 +02:00
MTR <- cols_ab [ " MTR" ]
2019-05-20 19:12:41 +02:00
NAL <- cols_ab [ " NAL" ]
NEO <- cols_ab [ " NEO" ]
NET <- cols_ab [ " NET" ]
NIT <- cols_ab [ " NIT" ]
NOR <- cols_ab [ " NOR" ]
NOV <- cols_ab [ " NOV" ]
OFX <- cols_ab [ " OFX" ]
2020-09-29 23:35:46 +02:00
OMC <- cols_ab [ " OMC" ]
ORI <- cols_ab [ " ORI" ]
2019-10-26 21:56:41 +02:00
OXA <- cols_ab [ " OXA" ]
2020-09-29 23:35:46 +02:00
PAZ <- cols_ab [ " PAZ" ]
PEF <- cols_ab [ " PEF" ]
2019-05-20 19:12:41 +02:00
PEN <- cols_ab [ " PEN" ]
PIP <- cols_ab [ " PIP" ]
PLB <- cols_ab [ " PLB" ]
PRI <- cols_ab [ " PRI" ]
2020-09-29 23:35:46 +02:00
PRU <- cols_ab [ " PRU" ]
2019-05-20 19:12:41 +02:00
QDA <- cols_ab [ " QDA" ]
2020-09-29 23:35:46 +02:00
RFL <- cols_ab [ " RFL" ]
2019-05-20 19:12:41 +02:00
RID <- cols_ab [ " RID" ]
RIF <- cols_ab [ " RIF" ]
RXT <- cols_ab [ " RXT" ]
2019-10-26 21:56:41 +02:00
SAM <- cols_ab [ " SAM" ]
2019-05-20 19:12:41 +02:00
SIS <- cols_ab [ " SIS" ]
2020-09-29 23:35:46 +02:00
SPT <- cols_ab [ " SPT" ]
SPX <- cols_ab [ " SPX" ]
2019-10-26 21:56:41 +02:00
STH <- cols_ab [ " STH" ]
2019-05-20 19:12:41 +02:00
SXT <- cols_ab [ " SXT" ]
2019-10-26 21:56:41 +02:00
TCC <- cols_ab [ " TCC" ]
2019-05-20 19:12:41 +02:00
TCY <- cols_ab [ " TCY" ]
TEC <- cols_ab [ " TEC" ]
TGC <- cols_ab [ " TGC" ]
TIC <- cols_ab [ " TIC" ]
2019-10-26 21:56:41 +02:00
TLV <- cols_ab [ " TLV" ]
2019-05-20 19:12:41 +02:00
TMP <- cols_ab [ " TMP" ]
2020-09-29 23:35:46 +02:00
TMX <- cols_ab [ " TMX" ]
2019-05-20 19:12:41 +02:00
TOB <- cols_ab [ " TOB" ]
2020-09-29 23:35:46 +02:00
TVA <- cols_ab [ " TVA" ]
TZD <- cols_ab [ " TZD" ]
2019-05-20 19:12:41 +02:00
TZP <- cols_ab [ " TZP" ]
VAN <- cols_ab [ " VAN" ]
2019-05-23 16:58:59 +02:00
# 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 ) ]
2020-06-22 11:18:40 +02:00
stop_if ( guideline $ code == " tb" & length ( abx_tb ) == 0 , " no antimycobacterials found in data set" )
2020-12-28 22:24:33 +01:00
# nolint end
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( isTRUE ( combine_SI ) ) {
2019-11-06 14:43:23 +01:00
search_result <- " R"
} else {
search_result <- c ( " R" , " I" )
}
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info ) ) {
if ( isTRUE ( combine_SI ) ) {
2020-05-16 13:05:47 +02:00
cat ( font_red ( " \nOnly results with 'R' are considered as resistance. Use `combine_SI = FALSE` to also consider 'I' as resistance.\n" ) )
2019-11-06 14:43:23 +01:00
} else {
2020-05-16 13:05:47 +02:00
cat ( font_red ( " \nResults with 'R' or 'I' are considered as resistance. Use `combine_SI = TRUE` to only consider 'R' as resistance.\n" ) )
2019-11-06 14:43:23 +01:00
}
2020-12-24 23:29:10 +01:00
cat ( " \n" , word_wrap ( " Determining multidrug-resistant organisms (MDRO), according to:" ) , " \n" ,
2022-08-28 10:31:50 +02:00
word_wrap ( paste0 ( font_bold ( " Guideline: " ) , font_italic ( guideline $ name ) ) , extra_indent = 11 , as_note = FALSE ) , " \n" ,
word_wrap ( paste0 ( font_bold ( " Author(s): " ) , guideline $ author ) , extra_indent = 11 , as_note = FALSE ) , " \n" ,
ifelse ( ! is.na ( guideline $ version ) ,
paste0 ( word_wrap ( paste0 ( font_bold ( " Version: " ) , guideline $ version ) , extra_indent = 11 , as_note = FALSE ) , " \n" ) ,
" "
) ,
paste0 ( font_bold ( " Source: " ) , guideline $ source_url ) ,
" \n\n" ,
sep = " "
)
2019-10-26 21:56:41 +02:00
}
2022-08-28 10:31:50 +02:00
2019-04-09 14:59:17 +02:00
ab_missing <- function ( ab ) {
isTRUE ( ab %in% c ( NULL , NA ) ) | length ( ab ) == 0
}
2019-10-26 21:56:41 +02:00
ab_NA <- function ( x ) {
x [ ! is.na ( x ) ]
}
2021-01-17 00:26:48 +01:00
try_ab <- function ( expr ) {
out <- tryCatch ( expr , error = function ( e ) FALSE )
out [is.na ( out ) ] <- FALSE
out
}
2022-08-28 10:31:50 +02:00
2018-04-25 15:33:58 +02:00
# antibiotic classes
2020-12-28 22:24:33 +01:00
# nolint start
2019-05-10 16:44:59 +02:00
aminoglycosides <- c ( TOB , GEN )
2020-09-29 23:35:46 +02:00
cephalosporins <- c ( CDZ , CAC , CEC , CFR , RID , MAN , CTZ , CZD , CZO , CDR , DIT , FEP , CAT , CFM , CMX , CMZ , DIZ , CID , CFP , CSL , CND , CTX , CTT , CTF , FOX , CPM , CPO , CPD , CPR , CRD , CFS , CPT , CAZ , CCV , CTL , CTB , CZX , BPR , CFM1 , CEI , CRO , CXM , LEX , CEP , HAP , CED , LTM , LOR )
cephalosporins_1st <- c ( CAC , CFR , RID , CTZ , CZD , CZO , CRD , CTL , LEX , CEP , HAP , CED )
cephalosporins_2nd <- c ( CEC , MAN , CMZ , CID , CND , CTT , CTF , FOX , CPR , CXM , LOR )
cephalosporins_3rd <- c ( CDZ , CDR , DIT , CAT , CFM , CMX , DIZ , CFP , CSL , CTX , CPM , CPD , CFS , CAZ , CCV , CTB , CZX , CRO , LTM )
carbapenems <- c ( DOR , ETP , IPM , MEM , MEV )
fluoroquinolones <- c ( CIP , ENX , FLE , GAT , GEM , GRX , LVX , LOM , MFX , NOR , OFX , PAZ , PEF , PRU , RFL , SPX , TMX , TVA )
2020-12-28 22:24:33 +01:00
# nolint end
2022-08-28 10:31:50 +02:00
2018-04-25 15:33:58 +02:00
# helper function for editing the table
2018-11-16 20:50:50 +01:00
trans_tbl <- function ( to , rows , cols , any_all ) {
2019-04-09 14:59:17 +02:00
cols <- cols [ ! ab_missing ( cols ) ]
2019-05-10 16:44:59 +02:00
cols <- cols [ ! is.na ( cols ) ]
2022-09-16 23:15:23 +02:00
if ( length ( rows ) > 0 && length ( cols ) > 0 ) {
2022-08-28 10:31:50 +02:00
x [ , cols ] <- as.data.frame ( lapply (
x [ , cols , drop = FALSE ] ,
function ( col ) as.rsi ( col )
) ,
stringsAsFactors = FALSE
)
x [rows , " columns_nonsusceptible" ] <<- vapply (
FUN.VALUE = character ( 1 ) ,
rows ,
function ( row , group_vct = cols ) {
cols_nonsus <- vapply (
FUN.VALUE = logical ( 1 ) ,
x [row , group_vct , drop = FALSE ] ,
function ( y ) y %in% search_result
)
paste ( sort ( c (
2022-09-16 23:15:23 +02:00
unlist ( strsplit ( x [row , " columns_nonsusceptible" , drop = TRUE ] , " , " , fixed = TRUE ) ) ,
2022-08-28 10:31:50 +02:00
names ( cols_nonsus ) [cols_nonsus ]
) ) ,
collapse = " , "
)
}
)
2018-11-16 20:50:50 +01:00
if ( any_all == " any" ) {
2020-05-16 13:05:47 +02:00
search_function <- any
2018-11-16 20:50:50 +01:00
} else if ( any_all == " all" ) {
2020-05-16 13:05:47 +02:00
search_function <- all
2018-11-16 20:50:50 +01:00
}
2020-11-11 16:49:27 +01:00
x_transposed <- as.list ( as.data.frame ( t ( x [ , cols , drop = FALSE ] ) ,
2022-08-28 10:31:50 +02:00
stringsAsFactors = FALSE
) )
rows_affected <- vapply (
FUN.VALUE = logical ( 1 ) ,
x_transposed ,
function ( y ) search_function ( y %in% search_result , na.rm = TRUE )
)
2021-11-28 23:01:26 +01:00
rows_affected <- x [which ( rows_affected ) , " row_number" , drop = TRUE ]
rows_to_change <- rows [rows %in% rows_affected ]
x [rows_to_change , " MDRO" ] <<- to
2022-08-28 10:31:50 +02:00
x [rows_to_change , " reason" ] <<- paste0 (
any_all ,
" of the required antibiotics " ,
ifelse ( any_all == " any" , " is" , " are" ) ,
" R" ,
ifelse ( ! isTRUE ( combine_SI ) , " or I" , " " )
)
2018-04-25 15:33:58 +02:00
}
}
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
trans_tbl2 <- function ( txt , rows , lst ) {
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info ) ) {
2020-10-27 15:56:51 +01:00
message_ ( txt , " ..." , appendLF = FALSE , as_note = FALSE )
2019-10-30 23:02:50 +01:00
}
2019-11-03 22:24:42 +01:00
if ( length ( rows ) > 0 ) {
# function specific for the CMI paper of 2012 (Magiorakos et al.)
lst_vector <- unlist ( lst ) [ ! is.na ( unlist ( lst ) ) ]
2022-08-28 10:31:50 +02:00
x [ , lst_vector ] <- as.data.frame ( lapply (
x [ , lst_vector , drop = FALSE ] ,
function ( col ) as.rsi ( col )
) ,
stringsAsFactors = FALSE
)
2019-11-03 22:24:42 +01:00
x [rows , " classes_in_guideline" ] <<- length ( lst )
2022-08-28 10:31:50 +02:00
x [rows , " classes_available" ] <<- vapply (
FUN.VALUE = double ( 1 ) ,
rows ,
function ( row , group_tbl = lst ) {
sum ( vapply (
FUN.VALUE = logical ( 1 ) ,
group_tbl ,
function ( group ) any ( unlist ( x [row , group [ ! is.na ( group ) ] , drop = TRUE ] ) %in% c ( " S" , " I" , " R" ) )
) )
}
)
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2022-08-28 10:31:50 +02:00
x [rows , " columns_nonsusceptible" ] <<- vapply (
FUN.VALUE = character ( 1 ) ,
rows ,
function ( row , group_vct = lst_vector ) {
cols_nonsus <- vapply ( FUN.VALUE = logical ( 1 ) , x [row , group_vct , drop = FALSE ] , function ( y ) y %in% search_result )
paste ( sort ( names ( cols_nonsus ) [cols_nonsus ] ) , collapse = " , " )
}
)
2019-11-05 11:28:52 +01:00
}
2022-08-28 10:31:50 +02:00
x [rows , " classes_affected" ] <<- vapply (
FUN.VALUE = double ( 1 ) ,
rows ,
function ( row , group_tbl = lst ) {
sum ( vapply (
FUN.VALUE = logical ( 1 ) ,
group_tbl ,
function ( group ) {
any ( unlist ( x [row , group [ ! is.na ( group ) ] , drop = TRUE ] ) %in% search_result , na.rm = TRUE )
}
) ,
na.rm = TRUE
)
}
)
2019-11-06 14:43:23 +01:00
# for PDR; all agents are R (or I if combine_SI = FALSE)
2020-11-11 16:49:27 +01:00
x_transposed <- as.list ( as.data.frame ( t ( x [rows , lst_vector , drop = FALSE ] ) ,
2022-08-28 10:31:50 +02:00
stringsAsFactors = FALSE
) )
2020-12-28 22:24:33 +01:00
row_filter <- vapply ( FUN.VALUE = logical ( 1 ) , x_transposed , function ( y ) all ( y %in% search_result , na.rm = TRUE ) )
2020-07-13 09:17:24 +02:00
x [which ( row_filter ) , " classes_affected" ] <<- 999
2019-11-03 22:24:42 +01:00
}
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info ) ) {
2020-11-11 16:49:27 +01:00
message_ ( " OK." , add_fn = list ( font_green , font_bold ) , as_note = FALSE )
2019-10-30 23:02:50 +01:00
}
2019-10-26 21:56:41 +02:00
}
2022-08-28 10:31:50 +02:00
2020-10-19 17:09:19 +02:00
x [ , col_mo ] <- as.mo ( as.character ( x [ , col_mo , drop = TRUE ] ) )
2021-04-23 09:59:36 +02:00
# rename col_mo to prevent interference with joined columns
colnames ( x ) [colnames ( x ) == col_mo ] <- " .col_mo"
col_mo <- " .col_mo"
2020-05-16 13:05:47 +02:00
# join to microorganisms data set
x <- left_join_microorganisms ( x , by = col_mo )
x $ MDRO <- ifelse ( ! is.na ( x $ genus ) , 1 , NA_integer_ )
x $ row_number <- seq_len ( nrow ( x ) )
x $ reason <- paste0 ( " not covered by " , toupper ( guideline $ code ) , " guideline" )
x $ columns_nonsusceptible <- " "
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
if ( guideline $ code == " cmi2012" ) {
# CMI, 2012 ---------------------------------------------------------------
# Non-susceptible = R and I
# (see header 'Approaches to Creating Definitions for MDR, XDR and PDR' in paper)
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
# take amoxicillin if ampicillin is unavailable
2022-09-16 23:15:23 +02:00
if ( is.na ( AMP ) && ! is.na ( AMX ) ) {
if ( isTRUE ( verbose ) ) {
2020-10-27 15:56:51 +01:00
message_ ( " Filling ampicillin (AMP) results with amoxicillin (AMX) results" )
2019-11-03 22:24:42 +01:00
}
AMP <- AMX
}
2019-10-26 21:56:41 +02:00
# take ceftriaxone if cefotaxime is unavailable and vice versa
2022-09-16 23:15:23 +02:00
if ( is.na ( CRO ) && ! is.na ( CTX ) ) {
if ( isTRUE ( verbose ) ) {
2020-10-27 15:56:51 +01:00
message_ ( " Filling ceftriaxone (CRO) results with cefotaxime (CTX) results" )
2019-11-03 22:24:42 +01:00
}
CRO <- CTX
}
2022-09-16 23:15:23 +02:00
if ( is.na ( CTX ) && ! is.na ( CRO ) ) {
if ( isTRUE ( verbose ) ) {
2020-10-27 15:56:51 +01:00
message_ ( " Filling cefotaxime (CTX) results with ceftriaxone (CRO) results" )
2019-11-03 22:24:42 +01:00
}
CTX <- CRO
}
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
# intrinsic resistant must not be considered for the determination of MDR,
# so let's just remove them, meticulously following the paper
x [which ( x $ genus == " Enterococcus" & x $ species == " faecium" ) , ab_NA ( IPM ) ] <- NA
x [which ( x $ genus == " Enterococcus" & x $ species == " faecalis" ) , ab_NA ( QDA ) ] <- NA
2022-08-28 10:31:50 +02:00
x [which ( ( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) ) , ab_NA ( c ( GEN , TOB , NET ) ) ] <- NA
2019-10-26 21:56:41 +02:00
x [which ( x $ genus == " Escherichia" & x $ species == " hermannii" ) , ab_NA ( c ( TCC , TZP ) ) ] <- NA
2022-08-28 10:31:50 +02:00
x [which ( ( x $ genus == " Citrobacter" & x $ species == " freundii" ) |
( x $ genus == " Enterobacter" & x $ species == " aerogenes" ) |
( x $ genus == " Klebsiella" & x $ species == " aerogenes" ) # new name (2017)
| ( x $ genus == " Enterobacter" & x $ species == " cloacae" ) |
( x $ genus == " Hafnia" & x $ species == " alvei" ) |
( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Serratia" & x $ species == " marcescens" ) ) , ab_NA ( CZO ) ] <- NA
x [which ( ( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Serratia" & x $ species == " marcescens" ) ) , ab_NA ( CXM ) ] <- NA
x [which ( ( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " mirabilis" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) ) , ab_NA ( TGC ) ] <- NA
x [which ( ( x $ genus == " Citrobacter" & x $ species == " koseri" ) |
( x $ genus == " Citrobacter" & x $ species == " freundii" ) |
( x $ genus == " Enterobacter" & x $ species == " aerogenes" ) |
( x $ genus == " Klebsiella" & x $ species == " aerogenes" ) # new name (2017)
| ( x $ genus == " Enterobacter" & x $ species == " cloacae" ) |
( x $ genus == " Escherichia" & x $ species == " hermannii" ) |
( x $ genus == " Hafnia" & x $ species == " alvei" ) |
( x $ genus == " Klebsiella" ) |
( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) |
( x $ genus == " Serratia" & x $ species == " marcescens" ) ) , ab_NA ( AMP ) ] <- NA
x [which ( ( x $ genus == " Citrobacter" & x $ species == " freundii" ) |
( x $ genus == " Enterobacter" & x $ species == " aerogenes" ) |
( x $ genus == " Klebsiella" & x $ species == " aerogenes" ) # new name (2017)
| ( x $ genus == " Enterobacter" & x $ species == " cloacae" ) |
( x $ genus == " Hafnia" & x $ species == " alvei" ) |
( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) |
( x $ genus == " Serratia" & x $ species == " marcescens" ) ) , ab_NA ( AMC ) ] <- NA
x [which ( ( x $ genus == " Citrobacter" & x $ species == " freundii" ) |
( x $ genus == " Citrobacter" & x $ species == " koseri" ) |
( x $ genus == " Enterobacter" & x $ species == " aerogenes" ) |
( x $ genus == " Klebsiella" & x $ species == " aerogenes" ) # new name (2017)
| ( x $ genus == " Enterobacter" & x $ species == " cloacae" ) |
( x $ genus == " Hafnia" & x $ species == " alvei" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Serratia" & x $ species == " marcescens" ) ) , ab_NA ( SAM ) ] <- NA
x [which ( ( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " mirabilis" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) |
( x $ genus == " Serratia" & x $ species == " marcescens" ) ) , ab_NA ( COL ) ] <- NA
x [which ( ( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " mirabilis" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) ) , ab_NA ( TCY ) ] <- NA
x [which ( ( x $ genus == " Morganella" & x $ species == " morganii" ) |
( x $ genus == " Proteus" & x $ species == " penneri" ) |
( x $ genus == " Proteus" & x $ species == " vulgaris" ) |
( x $ genus == " Providencia" & x $ species == " rettgeri" ) |
( x $ genus == " Providencia" & x $ species == " stuartii" ) ) , ab_NA ( c ( DOX , MNO ) ) ] <- NA
2019-11-03 22:24:42 +01:00
x $ classes_in_guideline <- NA_integer_
x $ classes_available <- NA_integer_
x $ classes_affected <- NA_integer_
2022-08-28 10:31:50 +02:00
2019-10-26 21:56:41 +02:00
# now add the MDR levels to the data
2022-08-28 10:31:50 +02:00
trans_tbl (
2 ,
which ( x $ genus == " Staphylococcus" & x $ species == " aureus" ) ,
c ( OXA , FOX ) ,
" any"
)
trans_tbl2 (
paste ( " Table 1 -" , font_italic ( " Staphylococcus aureus" ) ) ,
which ( x $ genus == " Staphylococcus" & x $ species == " aureus" ) ,
list (
GEN ,
RIF ,
CPT ,
c ( OXA , FOX ) ,
c ( CIP , MFX ) ,
SXT ,
FUS ,
c ( VAN , TEC , TLV ) ,
TGC ,
CLI ,
DAP ,
ERY ,
LNZ ,
CHL ,
FOS ,
QDA ,
c ( TCY , DOX , MNO )
)
)
trans_tbl2 (
paste ( " Table 2 -" , font_italic ( " Enterococcus" ) , " spp." ) ,
which ( x $ genus == " Enterococcus" ) ,
list (
GEH ,
STH ,
c ( IPM , MEM , DOR ) ,
c ( CIP , LVX , MFX ) ,
c ( VAN , TEC ) ,
TGC ,
DAP ,
LNZ ,
AMP ,
QDA ,
c ( DOX , MNO )
)
)
trans_tbl2 (
paste0 ( " Table 3 - " , font_italic ( " Enterobacteriaceae" ) ) ,
# this new order was previously 'Enterobacteriales' and contained only the family 'Enterobacteriaceae':
which ( x $ order == " Enterobacterales" ) ,
list (
c ( GEN , TOB , AMK , NET ) ,
CPT ,
c ( TCC , TZP ) ,
c ( ETP , IPM , MEM , DOR ) ,
CZO ,
CXM ,
c ( CTX , CAZ , FEP ) ,
c ( FOX , CTT ) ,
CIP ,
SXT ,
TGC ,
ATM ,
AMP ,
c ( AMC , SAM ) ,
CHL ,
FOS ,
COL ,
c ( TCY , DOX , MNO )
)
)
trans_tbl2 (
paste ( " Table 4 -" , font_italic ( " Pseudomonas aeruginosa" ) ) ,
which ( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" ) ,
list (
c ( GEN , TOB , AMK , NET ) ,
c ( IPM , MEM , DOR ) ,
c ( CAZ , FEP ) ,
c ( CIP , LVX ) ,
c ( TCC , TZP ) ,
ATM ,
FOS ,
c ( COL , PLB )
)
)
trans_tbl2 (
paste ( " Table 5 -" , font_italic ( " Acinetobacter" ) , " spp." ) ,
which ( x $ genus == " Acinetobacter" ) ,
list (
c ( GEN , TOB , AMK , NET ) ,
c ( IPM , MEM , DOR ) ,
c ( CIP , LVX ) ,
c ( TZP , TCC ) ,
c ( CTX , CRO , CAZ , FEP ) ,
SXT ,
SAM ,
c ( COL , PLB ) ,
c ( TCY , DOX , MNO )
)
)
# now set MDROs:
2019-11-03 22:24:42 +01:00
# MDR (=2): >=3 classes affected
x [which ( x $ classes_affected >= 3 ) , " MDRO" ] <- 2
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2022-08-28 10:31:50 +02:00
x [which ( x $ classes_affected >= 3 ) , " reason" ] <- paste0 (
" at least 3 classes contain R" ,
ifelse ( ! isTRUE ( combine_SI ) , " or I" , " " ) , " : " ,
x $ classes_affected [which ( x $ classes_affected >= 3 ) ] ,
" out of " , x $ classes_available [which ( x $ classes_affected >= 3 ) ] , " available classes"
)
2019-11-03 22:24:42 +01:00
}
2022-08-28 10:31:50 +02:00
2019-11-03 22:24:42 +01:00
# XDR (=3): all but <=2 classes affected
x [which ( ( x $ classes_in_guideline - x $ classes_affected ) <= 2 ) , " MDRO" ] <- 3
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2022-08-28 10:31:50 +02:00
x [which ( x $ MDRO == 3 ) , " reason" ] <- paste0 (
" less than 3 classes remain susceptible (" , x $ classes_in_guideline [which ( ( x $ classes_in_guideline - x $ classes_affected ) <= 2 ) ] - x $ classes_affected [which ( x $ MDRO == 3 ) ] ,
" out of " , x $ classes_in_guideline [which ( x $ MDRO == 3 ) ] , " classes)"
)
2019-11-03 22:24:42 +01:00
}
2022-08-28 10:31:50 +02:00
# PDR (=4): all agents are R
2019-11-03 22:24:42 +01:00
x [which ( x $ classes_affected == 999 & x $ classes_in_guideline == x $ classes_available ) , " MDRO" ] <- 4
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2022-08-28 10:31:50 +02:00
x [which ( x $ MDRO == 4 ) , " reason" ] <- paste (
" all antibiotics in all" ,
x $ classes_in_guideline [which ( x $ MDRO == 4 ) ] ,
" classes were tested R" ,
ifelse ( ! isTRUE ( combine_SI ) , " or I" , " " )
)
2019-11-03 22:24:42 +01:00
}
2022-08-28 10:31:50 +02:00
2019-11-03 22:24:42 +01:00
# not enough classes available
2020-09-03 12:31:48 +02:00
x [which ( x $ MDRO %in% c ( 1 , 3 ) & x $ classes_available < floor ( x $ classes_in_guideline * pct_required_classes ) ) , " MDRO" ] <- -1
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2022-08-28 10:31:50 +02:00
x [which ( x $ MDRO == -1 ) , " reason" ] <- paste0 (
" not enough classes available: " , x $ classes_available [which ( x $ MDRO == -1 ) ] ,
" of required " , ( floor ( x $ classes_in_guideline * pct_required_classes ) ) [which ( x $ MDRO == -1 ) ] ,
" (~" , percentage ( pct_required_classes ) , " of " , x $ classes_in_guideline [which ( x $ MDRO == -1 ) ] , " )"
)
2019-11-03 22:24:42 +01:00
}
2022-08-28 10:31:50 +02:00
2019-11-05 11:28:52 +01:00
# add antibiotic names of resistant ones to verbose output
2019-10-26 21:56:41 +02:00
}
2022-08-28 10:31:50 +02:00
2020-09-29 23:35:46 +02:00
if ( guideline $ code == " eucast3.1" ) {
# EUCAST 3.1 --------------------------------------------------------------
2018-04-25 15:33:58 +02:00
# Table 5
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ order == " Enterobacterales" |
( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" ) |
x $ genus == " Acinetobacter" ) ,
COL ,
" all"
)
trans_tbl (
3 ,
which ( x $ genus == " Salmonella" & x $ species == " Typhi" ) ,
c ( carbapenems , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Haemophilus" & x $ species == " influenzae" ) ,
c ( cephalosporins_3rd , carbapenems , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Moraxella" & x $ species == " catarrhalis" ) ,
c ( cephalosporins_3rd , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Neisseria" & x $ species == " meningitidis" ) ,
c ( cephalosporins_3rd , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Neisseria" & x $ species == " gonorrhoeae" ) ,
AZM ,
" any"
)
2018-04-25 15:33:58 +02:00
# Table 6
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ fullname %like% " ^(Coagulase-negative|Staphylococcus (aureus|epidermidis|hominis|haemolyticus|intermedius|pseudointermedius))" ) ,
c ( VAN , TEC , DAP , LNZ , QDA , TGC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Corynebacterium" ) ,
c ( VAN , TEC , DAP , LNZ , QDA , TGC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Streptococcus" & x $ species == " pneumoniae" ) ,
c ( carbapenems , VAN , TEC , DAP , LNZ , QDA , TGC , RIF ) ,
" any"
)
trans_tbl (
3 , # Sr. groups A/B/C/G
which ( x $ fullname %like% " ^Streptococcus (group (A|B|C|G)|pyogenes|agalactiae|equisimilis|equi|zooepidemicus|dysgalactiae|anginosus)" ) ,
c ( PEN , cephalosporins , VAN , TEC , DAP , LNZ , QDA , TGC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" ) ,
c ( DAP , LNZ , TGC , TEC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" & x $ species == " faecalis" ) ,
c ( AMP , AMX ) ,
" any"
)
2018-04-25 15:33:58 +02:00
# Table 7
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Bacteroides" ) ,
MTR ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Clostridium" & x $ species == " difficile" ) ,
c ( MTR , VAN ) ,
" any"
)
2018-04-25 15:33:58 +02:00
}
2022-08-28 10:31:50 +02:00
2020-09-29 23:35:46 +02:00
if ( guideline $ code == " eucast3.2" ) {
# EUCAST 3.2 --------------------------------------------------------------
# Table 6
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( ( x $ order == " Enterobacterales" &
! x $ family == " Morganellaceae" &
! ( x $ genus == " Serratia" & x $ species == " marcescens" ) ) |
( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" ) |
x $ genus == " Acinetobacter" ) ,
COL ,
" all"
)
trans_tbl (
3 ,
which ( x $ genus == " Salmonella" & x $ species == " Typhi" ) ,
c ( carbapenems ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Haemophilus" & x $ species == " influenzae" ) ,
c ( cephalosporins_3rd , carbapenems , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Moraxella" & x $ species == " catarrhalis" ) ,
c ( cephalosporins_3rd , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Neisseria" & x $ species == " meningitidis" ) ,
c ( cephalosporins_3rd , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Neisseria" & x $ species == " gonorrhoeae" ) ,
SPT ,
" any"
)
2020-09-29 23:35:46 +02:00
# Table 7
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Staphylococcus" & x $ species == " aureus" ) ,
c ( VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ mo %in% MO_CONS ) , # coagulase-negative Staphylococcus
c ( VAN , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Corynebacterium" ) ,
c ( VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Streptococcus" & x $ species == " pneumoniae" ) ,
c ( carbapenems , VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC , RIF ) ,
" any"
)
trans_tbl (
3 , # Sr. groups A/B/C/G
which ( x $ mo %in% MO_STREP_ABCG ) ,
c ( PEN , cephalosporins , VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" ) ,
c ( DAP , LNZ , TGC , ERV , OMC , TEC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" & x $ species == " faecalis" ) ,
c ( AMP , AMX ) ,
" any"
)
2021-12-11 13:41:31 +01:00
# Table 8
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Bacteroides" ) ,
MTR ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Clostridium" & x $ species == " difficile" ) ,
c ( MTR , VAN , FDX ) ,
" any"
)
2021-12-11 13:41:31 +01:00
}
2022-08-28 10:31:50 +02:00
2021-12-11 13:41:31 +01:00
if ( guideline $ code == " eucast3.3" ) {
# EUCAST 3.3 --------------------------------------------------------------
# note: this guideline is equal to EUCAST 3.2 - no MDRO insights changed
# Table 6
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( ( x $ order == " Enterobacterales" &
! x $ family == " Morganellaceae" &
! ( x $ genus == " Serratia" & x $ species == " marcescens" ) ) |
( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" ) |
x $ genus == " Acinetobacter" ) ,
COL ,
" all"
)
trans_tbl (
3 ,
which ( x $ genus == " Salmonella" & x $ species == " Typhi" ) ,
c ( carbapenems ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Haemophilus" & x $ species == " influenzae" ) ,
c ( cephalosporins_3rd , carbapenems , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Moraxella" & x $ species == " catarrhalis" ) ,
c ( cephalosporins_3rd , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Neisseria" & x $ species == " meningitidis" ) ,
c ( cephalosporins_3rd , fluoroquinolones ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Neisseria" & x $ species == " gonorrhoeae" ) ,
SPT ,
" any"
)
2021-12-11 13:41:31 +01:00
# Table 7
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Staphylococcus" & x $ species == " aureus" ) ,
c ( VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ mo %in% MO_CONS ) , # coagulase-negative Staphylococcus
c ( VAN , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Corynebacterium" ) ,
c ( VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Streptococcus" & x $ species == " pneumoniae" ) ,
c ( carbapenems , VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC , RIF ) ,
" any"
)
trans_tbl (
3 , # Sr. groups A/B/C/G
which ( x $ mo %in% MO_STREP_ABCG ) ,
c ( PEN , cephalosporins , VAN , TEC , TLV , DAL , ORI , DAP , LNZ , TZD , QDA , TGC , ERV , OMC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" ) ,
c ( DAP , LNZ , TGC , ERV , OMC , TEC ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" & x $ species == " faecalis" ) ,
c ( AMP , AMX ) ,
" any"
)
2020-09-29 23:35:46 +02:00
# Table 8
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Bacteroides" ) ,
MTR ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Clostridium" & x $ species == " difficile" ) ,
c ( MTR , VAN , FDX ) ,
" any"
)
2020-09-29 23:35:46 +02:00
}
2022-08-28 10:31:50 +02:00
2019-07-04 15:26:07 +02:00
if ( guideline $ code == " mrgn" ) {
2018-04-25 15:33:58 +02:00
# Germany -----------------------------------------------------------------
2022-08-28 10:31:50 +02:00
2019-10-07 14:32:06 +02:00
# Table 1
2022-08-28 10:31:50 +02:00
trans_tbl (
2 , # 3MRGN
which ( ( x $ order == " Enterobacterales" | # following in fact the old Enterobacteriaceae classification
( x $ genus == " Acinetobacter" & x $ species == " baumannii" ) ) &
try_ab ( x [ , PIP , drop = TRUE ] == " R" ) &
( try_ab ( x [ , CTX , drop = TRUE ] == " R" ) | try_ab ( x [ , CAZ , drop = TRUE ] == " R" ) ) &
( try_ab ( x [ , IPM , drop = TRUE ] != " R" ) | try_ab ( x [ , MEM , drop = TRUE ] != " R" ) ) &
try_ab ( x [ , CIP , drop = TRUE ] == " R" ) ) ,
c ( PIP , CTX , CAZ , IPM , MEM , CIP ) ,
" any"
)
trans_tbl (
3 , # 4MRGN, overwrites 3MRGN if applicable
which ( ( x $ order == " Enterobacterales" | # following in fact the old Enterobacteriaceae classification
( x $ genus == " Acinetobacter" & x $ species == " baumannii" ) ) &
try_ab ( x [ , PIP , drop = TRUE ] == " R" ) &
( try_ab ( x [ , CTX , drop = TRUE ] == " R" ) | try_ab ( x [ , CAZ , drop = TRUE ] == " R" ) ) &
( try_ab ( x [ , IPM , drop = TRUE ] == " R" ) | try_ab ( x [ , MEM , drop = TRUE ] == " R" ) ) &
try_ab ( x [ , CIP , drop = TRUE ] == " R" ) ) ,
c ( PIP , CTX , CAZ , IPM , MEM , CIP ) ,
" any"
)
trans_tbl (
3 , # 4MRGN, overwrites 3MRGN if applicable
which ( ( x $ order == " Enterobacterales" | # following in fact the old Enterobacteriaceae classification
( x $ genus == " Acinetobacter" & x $ species == " baumannii" ) ) &
( try_ab ( x [ , IPM , drop = TRUE ] == " R" ) | try_ab ( x [ , MEM , drop = TRUE ] == " R" ) ) ) ,
c ( IPM , MEM ) ,
" any"
)
trans_tbl (
2 , # 3MRGN, if only 1 group is S
which ( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" &
try_ab ( x [ , PIP , drop = TRUE ] == " S" ) +
try_ab ( x [ , CTX , drop = TRUE ] == " S" ) +
try_ab ( x [ , CAZ , drop = TRUE ] == " S" ) +
try_ab ( x [ , IPM , drop = TRUE ] == " S" ) +
try_ab ( x [ , MEM , drop = TRUE ] == " S" ) +
try_ab ( x [ , CIP , drop = TRUE ] == " S" ) == 1 ) ,
c ( PIP , CTX , CAZ , IPM , MEM , CIP ) ,
" any"
)
trans_tbl (
3 , # 4MRGN otherwise
which ( ( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" ) &
try_ab ( x [ , PIP , drop = TRUE ] == " R" ) &
( try_ab ( x [ , CTX , drop = TRUE ] == " R" ) | try_ab ( x [ , CAZ , drop = TRUE ] == " R" ) ) &
( try_ab ( x [ , IPM , drop = TRUE ] == " R" ) | try_ab ( x [ , MEM , drop = TRUE ] == " R" ) ) &
try_ab ( x [ , CIP , drop = TRUE ] == " R" ) ) ,
c ( PIP , CTX , CAZ , IPM , MEM , CIP ) ,
" any"
)
2021-01-15 22:44:52 +01:00
x [which ( x $ MDRO == 2 ) , " reason" ] <- " 3MRGN"
x [which ( x $ MDRO == 3 ) , " reason" ] <- " 4MRGN"
2018-04-19 14:10:57 +02:00
}
2022-08-28 10:31:50 +02:00
2019-07-04 15:26:07 +02:00
if ( guideline $ code == " brmo" ) {
2018-04-25 15:33:58 +02:00
# Netherlands -------------------------------------------------------------
2019-05-20 19:12:41 +02:00
aminoglycosides <- aminoglycosides [ ! is.na ( aminoglycosides ) ]
fluoroquinolones <- fluoroquinolones [ ! is.na ( fluoroquinolones ) ]
carbapenems <- carbapenems [ ! is.na ( carbapenems ) ]
amino <- AMX %or% AMP
third <- CAZ %or% CTX
ESBLs <- c ( amino , third )
ESBLs <- ESBLs [ ! is.na ( ESBLs ) ]
if ( length ( ESBLs ) != 2 ) {
ESBLs <- character ( 0 )
}
2022-08-28 10:31:50 +02:00
2018-04-18 12:24:54 +02:00
# Table 1
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ order == " Enterobacterales" ) , # following in fact the old Enterobacteriaceae classification
c ( aminoglycosides , fluoroquinolones ) ,
" all"
)
trans_tbl (
2 ,
which ( x $ order == " Enterobacterales" ) , # following in fact the old Enterobacteriaceae classification
carbapenems ,
" any"
)
trans_tbl (
2 ,
which ( x $ order == " Enterobacterales" ) , # following in fact the old Enterobacteriaceae classification
ESBLs ,
" all"
)
2018-04-18 12:24:54 +02:00
# Table 2
2022-08-28 10:31:50 +02:00
trans_tbl (
2 ,
which ( x $ genus == " Acinetobacter" ) ,
c ( carbapenems ) ,
" any"
)
trans_tbl (
3 ,
which ( x $ genus == " Acinetobacter" ) ,
c ( aminoglycosides , fluoroquinolones ) ,
" all"
)
trans_tbl (
3 ,
which ( x $ genus == " Stenotrophomonas" & x $ species == " maltophilia" ) ,
SXT ,
" all"
)
2022-09-16 23:15:23 +02:00
if ( ! ab_missing ( MEM ) && ! ab_missing ( IPM ) &&
! ab_missing ( GEN ) && ! ab_missing ( TOB ) &&
! ab_missing ( CIP ) &&
! ab_missing ( CAZ ) &&
2022-08-28 10:31:50 +02:00
! ab_missing ( TZP ) ) {
2019-05-23 16:58:59 +02:00
x $ psae <- 0
2021-01-15 22:44:52 +01:00
x [which ( x [ , MEM , drop = TRUE ] == " R" | x [ , IPM , drop = TRUE ] == " R" ) , " psae" ] <- 1 + x [which ( x [ , MEM , drop = TRUE ] == " R" | x [ , IPM , drop = TRUE ] == " R" ) , " psae" ]
x [which ( x [ , GEN , drop = TRUE ] == " R" & x [ , TOB , drop = TRUE ] == " R" ) , " psae" ] <- 1 + x [which ( x [ , GEN , drop = TRUE ] == " R" & x [ , TOB , drop = TRUE ] == " R" ) , " psae" ]
x [which ( x [ , CIP , drop = TRUE ] == " R" ) , " psae" ] <- 1 + x [which ( x [ , CIP , drop = TRUE ] == " R" ) , " psae" ]
x [which ( x [ , CAZ , drop = TRUE ] == " R" ) , " psae" ] <- 1 + x [which ( x [ , CAZ , drop = TRUE ] == " R" ) , " psae" ]
x [which ( x [ , TZP , drop = TRUE ] == " R" ) , " psae" ] <- 1 + x [which ( x [ , TZP , drop = TRUE ] == " R" ) , " psae" ]
2018-11-16 20:50:50 +01:00
} else {
2019-05-23 16:58:59 +02:00
x $ psae <- 0
2018-11-16 20:50:50 +01:00
}
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Pseudomonas" & x $ species == " aeruginosa" & x $ psae >= 3 ) ,
c ( CAZ , CIP , GEN , IPM , MEM , TOB , TZP ) ,
" any"
)
2019-05-23 16:58:59 +02:00
x [which (
2022-08-28 10:31:50 +02:00
x $ genus == " Pseudomonas" & x $ species == " aeruginosa" &
x $ psae >= 3
) , " reason" ] <- paste0 ( " at least 3 classes contain R" , ifelse ( ! isTRUE ( combine_SI ) , " or I" , " " ) )
2018-04-18 12:24:54 +02:00
# Table 3
2022-08-28 10:31:50 +02:00
trans_tbl (
3 ,
which ( x $ genus == " Streptococcus" & x $ species == " pneumoniae" ) ,
PEN ,
" all"
)
trans_tbl (
3 ,
which ( x $ genus == " Streptococcus" & x $ species == " pneumoniae" ) ,
VAN ,
" all"
)
trans_tbl (
3 ,
which ( x $ genus == " Enterococcus" & x $ species == " faecium" ) ,
c ( PEN , VAN ) ,
" all"
)
2018-04-18 12:24:54 +02:00
}
2022-08-28 10:31:50 +02:00
2020-05-16 13:05:47 +02:00
if ( guideline $ code == " tb" ) {
# Tuberculosis ------------------------------------------------------------
prepare_drug <- function ( ab ) {
# returns vector values of drug
# if `ab` is a column name, looks up the values in `x`
2022-09-16 23:15:23 +02:00
if ( length ( ab ) == 1 && is.character ( ab ) ) {
2020-05-16 13:05:47 +02:00
if ( ab %in% colnames ( x ) ) {
ab <- x [ , ab , drop = TRUE ]
}
2019-05-23 16:58:59 +02:00
}
2020-05-16 13:05:47 +02:00
ab <- as.character ( as.rsi ( ab ) )
ab [is.na ( ab ) ] <- " "
ab
2019-05-23 16:58:59 +02:00
}
2020-05-16 13:05:47 +02:00
drug_is_R <- function ( ab ) {
2021-05-12 18:15:03 +02:00
# returns [logical] vector
2020-05-16 13:05:47 +02:00
ab <- prepare_drug ( ab )
if ( length ( ab ) == 0 ) {
rep ( FALSE , NROW ( x ) )
} else if ( length ( ab ) == 1 ) {
rep ( ab , NROW ( x ) ) == " R"
} else {
ab == " R"
}
2019-05-23 16:58:59 +02:00
}
2020-05-16 13:05:47 +02:00
drug_is_not_R <- function ( ab ) {
2021-05-12 18:15:03 +02:00
# returns [logical] vector
2020-05-16 13:05:47 +02:00
ab <- prepare_drug ( ab )
if ( length ( ab ) == 0 ) {
rep ( TRUE , NROW ( x ) )
} else if ( length ( ab ) == 1 ) {
rep ( ab , NROW ( x ) ) != " R"
} else {
ab != " R"
}
2019-05-23 16:58:59 +02:00
}
2022-08-28 10:31:50 +02:00
2020-05-16 13:05:47 +02:00
x $ mono_count <- 0
2020-07-22 10:24:23 +02:00
x [drug_is_R ( INH ) , " mono_count" ] <- x [drug_is_R ( INH ) , " mono_count" , drop = TRUE ] + 1
x [drug_is_R ( RIF ) , " mono_count" ] <- x [drug_is_R ( RIF ) , " mono_count" , drop = TRUE ] + 1
x [drug_is_R ( ETH ) , " mono_count" ] <- x [drug_is_R ( ETH ) , " mono_count" , drop = TRUE ] + 1
x [drug_is_R ( PZA ) , " mono_count" ] <- x [drug_is_R ( PZA ) , " mono_count" , drop = TRUE ] + 1
x [drug_is_R ( RIB ) , " mono_count" ] <- x [drug_is_R ( RIB ) , " mono_count" , drop = TRUE ] + 1
x [drug_is_R ( RFP ) , " mono_count" ] <- x [drug_is_R ( RFP ) , " mono_count" , drop = TRUE ] + 1
2022-08-28 10:31:50 +02:00
2020-05-16 13:05:47 +02:00
x $ mono <- x $ mono_count > 0
x $ poly <- x $ mono_count > 1 & drug_is_not_R ( RIF ) & drug_is_not_R ( INH )
x $ mdr <- drug_is_R ( RIF ) & drug_is_R ( INH )
x $ xdr <- drug_is_R ( LVX ) | drug_is_R ( MFX ) | drug_is_R ( GAT )
x $ second <- drug_is_R ( CAP ) | drug_is_R ( KAN ) | drug_is_R ( AMK )
x $ xdr <- x $ mdr & x $ xdr & x $ second
x $ MDRO <- ifelse ( x $ xdr , 5 ,
2022-08-28 10:31:50 +02:00
ifelse ( x $ mdr , 4 ,
ifelse ( x $ poly , 3 ,
ifelse ( x $ mono , 2 ,
1
)
)
)
)
2020-05-16 13:05:47 +02:00
# keep all real TB, make other species NA
x $ MDRO <- ifelse ( x $ fullname == " Mycobacterium tuberculosis" , x $ MDRO , NA_real_ )
2021-01-15 22:44:52 +01:00
x $ reason <- " PDR/MDR/XDR criteria were met"
2019-10-26 21:56:41 +02:00
}
2022-08-28 10:31:50 +02:00
2019-11-05 11:28:52 +01:00
# some more info on negative results
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2019-11-05 11:28:52 +01:00
if ( guideline $ code == " cmi2012" ) {
2022-08-28 10:31:50 +02:00
x [which ( x $ MDRO == 1 & ! is.na ( x $ classes_affected ) ) , " reason" ] <- paste0 (
x $ classes_affected [which ( x $ MDRO == 1 & ! is.na ( x $ classes_affected ) ) ] ,
" of " ,
x $ classes_available [which ( x $ MDRO == 1 & ! is.na ( x $ classes_affected ) ) ] ,
" available classes contain R" ,
ifelse ( ! isTRUE ( combine_SI ) , " or I" , " " ) ,
" (3 required for MDR)"
)
2019-11-05 11:28:52 +01:00
} else {
x [which ( x $ MDRO == 1 ) , " reason" ] <- " too few antibiotics are R"
}
}
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info.bak ) ) {
2021-11-28 23:01:26 +01:00
cat ( group_msg )
if ( sum ( ! is.na ( x $ MDRO ) ) == 0 ) {
cat ( font_bold ( paste0 ( " => Found 0 MDROs since no isolates are covered by the guideline" ) ) )
} else {
2022-08-28 10:31:50 +02:00
cat ( font_bold ( paste0 (
2022-09-16 23:15:23 +02:00
" => Found " , sum ( x $ MDRO %in% 2 : 5 , na.rm = TRUE ) , " " , guideline $ type , " out of " , sum ( ! is.na ( x $ MDRO ) ) ,
" isolates (" , trimws ( percentage ( sum ( x $ MDRO %in% 2 : 5 , na.rm = TRUE ) / sum ( ! is.na ( x $ MDRO ) ) ) ) , " )"
2022-08-28 10:31:50 +02:00
) ) )
2021-11-28 23:01:26 +01:00
}
}
2022-08-28 10:31:50 +02:00
2021-11-28 23:01:26 +01:00
# Fill in blanks ----
# for rows that have no results
x_transposed <- as.list ( as.data.frame ( t ( x [ , cols_ab , drop = FALSE ] ) ,
2022-08-28 10:31:50 +02:00
stringsAsFactors = FALSE
) )
rows_empty <- which ( vapply (
FUN.VALUE = logical ( 1 ) ,
x_transposed ,
function ( y ) all ( is.na ( y ) )
) )
2021-11-28 23:01:26 +01:00
if ( length ( rows_empty ) > 0 ) {
cat ( font_italic ( paste0 ( " (" , length ( rows_empty ) , " isolates had no test results)\n" ) ) )
x [rows_empty , " MDRO" ] <- NA
x [rows_empty , " reason" ] <- " none of the antibiotics have test results"
} else {
cat ( " \n" )
}
2022-08-28 10:31:50 +02:00
2019-11-03 22:24:42 +01:00
# Results ----
2019-10-26 21:56:41 +02:00
if ( guideline $ code == " cmi2012" ) {
2019-11-04 11:35:34 +01:00
if ( any ( x $ MDRO == -1 , na.rm = TRUE ) ) {
2021-12-11 13:41:31 +01:00
if ( message_not_thrown_before ( " mdro" , " availability" ) ) {
2022-08-28 10:31:50 +02:00
warning_ (
" in `mdro()`: NA introduced for isolates where the available percentage of antimicrobial classes was below " ,
percentage ( pct_required_classes ) , " (set with `pct_required_classes`)"
)
2021-02-08 14:18:42 +01:00
}
2019-11-03 22:24:42 +01:00
# set these -1s to NA
x [which ( x $ MDRO == -1 ) , " MDRO" ] <- NA_integer_
}
2022-08-28 10:31:50 +02:00
x $ MDRO <- factor (
x = x $ MDRO ,
levels = 1 : 4 ,
labels = c (
" Negative" , " Multi-drug-resistant (MDR)" ,
" Extensively drug-resistant (XDR)" , " Pandrug-resistant (PDR)"
) ,
ordered = TRUE
)
2019-10-26 21:56:41 +02:00
} else if ( guideline $ code == " tb" ) {
2022-08-28 10:31:50 +02:00
x $ MDRO <- factor (
x = x $ MDRO ,
levels = 1 : 5 ,
labels = c (
" Negative" , " Mono-resistant" , " Poly-resistant" ,
" Multi-drug-resistant" , " Extensively drug-resistant"
) ,
ordered = TRUE
)
2019-10-07 14:32:06 +02:00
} else if ( guideline $ code == " mrgn" ) {
2022-08-28 10:31:50 +02:00
x $ MDRO <- factor (
x = x $ MDRO ,
levels = 1 : 3 ,
labels = c ( " Negative" , " 3MRGN" , " 4MRGN" ) ,
ordered = TRUE
)
2019-05-23 16:58:59 +02:00
} else {
2022-08-28 10:31:50 +02:00
x $ MDRO <- factor (
x = x $ MDRO ,
levels = 1 : 3 ,
labels = c ( " Negative" , " Positive, unconfirmed" , " Positive" ) ,
ordered = TRUE
)
2019-05-23 16:58:59 +02:00
}
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( isTRUE ( verbose ) ) {
2021-04-23 09:59:36 +02:00
colnames ( x ) [colnames ( x ) == col_mo ] <- " microorganism"
x $ microorganism <- mo_name ( x $ microorganism , language = NULL )
2022-08-28 10:31:50 +02:00
x [ , c (
" row_number" ,
" microorganism" ,
" MDRO" ,
" reason" ,
" columns_nonsusceptible"
) ,
drop = FALSE
]
2019-11-03 22:24:42 +01:00
} else {
x $ MDRO
}
2018-04-18 12:24:54 +02:00
}
2021-01-17 00:26:48 +01:00
#' @rdname mdro
#' @export
2021-01-18 16:57:56 +01:00
custom_mdro_guideline <- function ( ... , as_factor = TRUE ) {
2021-04-07 08:37:42 +02:00
meet_criteria ( as_factor , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
2021-01-18 16:57:56 +01:00
dots <- tryCatch ( list ( ... ) ,
2022-08-28 10:31:50 +02:00
error = function ( e ) " error"
)
stop_if (
identical ( dots , " error" ) ,
" rules must be a valid formula inputs (e.g., using '~'), see `?mdro`"
)
2021-01-17 00:26:48 +01:00
n_dots <- length ( dots )
stop_if ( n_dots == 0 , " no custom rules were set. Please read the documentation using `?mdro`." )
out <- vector ( " list" , n_dots )
for ( i in seq_len ( n_dots ) ) {
2022-08-28 10:31:50 +02:00
stop_ifnot (
inherits ( dots [ [i ] ] , " formula" ) ,
" rule " , i , " must be a valid formula input (e.g., using '~'), see `?mdro`"
)
2021-01-18 16:57:56 +01:00
# Query
qry <- dots [ [i ] ] [ [2 ] ]
if ( inherits ( qry , " call" ) ) {
qry <- as.expression ( qry )
}
qry <- as.character ( qry )
2021-02-08 14:18:42 +01:00
# these will prevent vectorisation, so replace them:
2021-01-18 16:57:56 +01:00
qry <- gsub ( " &&" , " &" , qry , fixed = TRUE )
qry <- gsub ( " ||" , " |" , qry , fixed = TRUE )
# support filter()-like writing: custom_mdro_guideline('CIP == "R", AMX == "S"' ~ "result 1")
qry <- gsub ( " *, *" , " & " , qry )
2021-02-08 14:18:42 +01:00
# format nicely, setting spaces around operators
2021-01-18 16:57:56 +01:00
qry <- gsub ( " *([&|+-/*^><==]+) *" , " \\1 " , qry )
qry <- gsub ( " '" , " \"" , qry , fixed = TRUE )
out [ [i ] ] $ query <- as.expression ( qry )
2022-08-28 10:31:50 +02:00
2021-01-18 16:57:56 +01:00
# Value
2021-01-17 00:26:48 +01:00
val <- tryCatch ( eval ( dots [ [i ] ] [ [3 ] ] ) , error = function ( e ) NULL )
2021-01-18 16:57:56 +01:00
stop_if ( is.null ( val ) , " rule " , i , " must return a valid value, it now returns an error: " , tryCatch ( eval ( dots [ [i ] ] [ [3 ] ] ) , error = function ( e ) e $ message ) )
stop_if ( length ( val ) > 1 , " rule " , i , " must return a value of length 1, not " , length ( val ) )
2021-01-17 00:26:48 +01:00
out [ [i ] ] $ value <- as.character ( val )
}
2022-08-28 10:31:50 +02:00
2021-01-17 00:26:48 +01:00
names ( out ) <- paste0 ( " rule" , seq_len ( n_dots ) )
2021-01-18 16:57:56 +01:00
out <- set_clean_class ( out , new_class = c ( " custom_mdro_guideline" , " list" ) )
2021-04-07 08:37:42 +02:00
attr ( out , " values" ) <- unname ( c ( " Negative" , vapply ( FUN.VALUE = character ( 1 ) , unclass ( out ) , function ( x ) x $ value ) ) )
2021-01-18 16:57:56 +01:00
attr ( out , " as_factor" ) <- as_factor
out
2021-01-17 00:26:48 +01:00
}
2021-04-07 08:37:42 +02:00
#' @method c custom_mdro_guideline
#' @noRd
#' @export
c.custom_mdro_guideline <- function ( x , ... , as_factor = NULL ) {
if ( length ( list ( ... ) ) == 0 ) {
return ( x )
}
if ( ! is.null ( as_factor ) ) {
meet_criteria ( as_factor , allow_class = " logical" , has_length = 1 )
} else {
as_factor <- attributes ( x ) $ as_factor
}
for ( g in list ( ... ) ) {
stop_ifnot ( inherits ( g , " custom_mdro_guideline" ) ,
2022-08-28 10:31:50 +02:00
" for combining custom MDRO guidelines, all rules must be created with `custom_mdro_guideline()`" ,
call = FALSE
)
2021-04-07 08:37:42 +02:00
vals <- attributes ( x ) $ values
if ( ! all ( attributes ( g ) $ values %in% vals ) ) {
vals <- unname ( unique ( c ( vals , attributes ( g ) $ values ) ) )
}
attributes ( g ) <- NULL
x <- c ( unclass ( x ) , unclass ( g ) )
attr ( x , " values" ) <- vals
}
names ( x ) <- paste0 ( " rule" , seq_len ( length ( x ) ) )
x <- set_clean_class ( x , new_class = c ( " custom_mdro_guideline" , " list" ) )
attr ( x , " values" ) <- vals
attr ( x , " as_factor" ) <- as_factor
x
}
#' @method as.list custom_mdro_guideline
#' @noRd
#' @export
as.list.custom_mdro_guideline <- function ( x , ... ) {
c ( x , ... )
}
2021-01-17 00:26:48 +01:00
#' @method print custom_mdro_guideline
#' @export
#' @noRd
print.custom_mdro_guideline <- function ( x , ... ) {
cat ( " A set of custom MDRO rules:\n" )
for ( i in seq_len ( length ( x ) ) ) {
rule <- x [ [i ] ]
2021-04-07 08:37:42 +02:00
rule $ query <- format_custom_query_rule ( rule $ query )
cat ( " " , i , " . " , font_bold ( " If " ) , font_blue ( rule $ query ) , font_bold ( " then: " ) , font_red ( rule $ value ) , " \n" , sep = " " )
2021-01-17 00:26:48 +01:00
}
2021-04-07 08:37:42 +02:00
cat ( " " , i + 1 , " . " , font_bold ( " Otherwise: " ) , font_red ( paste0 ( " Negative" ) ) , " \n" , sep = " " )
2021-01-17 00:26:48 +01:00
cat ( " \nUnmatched rows will return " , font_red ( " NA" ) , " .\n" , sep = " " )
2021-01-18 16:57:56 +01:00
if ( isTRUE ( attributes ( x ) $ as_factor ) ) {
cat ( " Results will be of class <factor>, with ordered levels: " , paste0 ( attributes ( x ) $ values , collapse = " < " ) , " \n" , sep = " " )
} else {
cat ( " Results will be of class <character>.\n" )
}
2021-01-17 00:26:48 +01:00
}
2021-04-07 08:37:42 +02:00
run_custom_mdro_guideline <- function ( df , guideline , info ) {
2021-01-17 00:26:48 +01:00
n_dots <- length ( guideline )
stop_if ( n_dots == 0 , " no custom guidelines set" , call = -2 )
out <- character ( length = NROW ( df ) )
reasons <- character ( length = NROW ( df ) )
for ( i in seq_len ( n_dots ) ) {
2021-01-18 16:57:56 +01:00
qry <- tryCatch ( eval ( parse ( text = guideline [ [i ] ] $ query ) , envir = df , enclos = parent.frame ( ) ) ,
2022-08-28 10:31:50 +02:00
error = function ( e ) {
pkg_env $ err_msg <- e $ message
return ( " error" )
}
)
2021-01-18 16:57:56 +01:00
if ( identical ( qry , " error" ) ) {
2022-08-28 10:31:50 +02:00
warning_ ( " in `custom_mdro_guideline()`: rule " , i ,
" (`" , as.character ( guideline [ [i ] ] $ query ) , " `) was ignored because of this error message: " ,
pkg_env $ err_msg ,
call = FALSE ,
add_fn = font_red
)
2021-01-18 16:57:56 +01:00
next
}
2022-08-28 10:31:50 +02:00
stop_ifnot ( is.logical ( qry ) , " in custom_mdro_guideline(): rule " , i , " (`" , guideline [ [i ] ] $ query ,
" `) must return `TRUE` or `FALSE`, not " ,
format_class ( class ( qry ) , plural = FALSE ) ,
call = FALSE
)
2021-04-07 08:37:42 +02:00
new_mdros <- which ( qry == TRUE & out == " " )
2022-08-28 10:31:50 +02:00
2022-09-16 23:15:23 +02:00
if ( isTRUE ( info ) ) {
2022-08-28 10:31:50 +02:00
cat ( word_wrap (
" - Custom MDRO rule " , i , " : `" , as.character ( guideline [ [i ] ] $ query ) ,
" ` (" , length ( new_mdros ) , " rows matched)"
) , " \n" , sep = " " )
2021-04-07 08:37:42 +02:00
}
2021-01-17 00:26:48 +01:00
val <- guideline [ [i ] ] $ value
2021-04-07 08:37:42 +02:00
out [new_mdros ] <- val
2022-09-16 23:15:23 +02:00
reasons [new_mdros ] <- paste0 (
" matched rule " ,
gsub ( " rule" , " " , names ( guideline ) [i ] , fixed = TRUE ) , " : " , as.character ( guideline [ [i ] ] $ query )
)
2021-01-17 00:26:48 +01:00
}
out [out == " " ] <- " Negative"
reasons [out == " Negative" ] <- " no rules matched"
2022-08-28 10:31:50 +02:00
2021-01-18 16:57:56 +01:00
if ( isTRUE ( attributes ( guideline ) $ as_factor ) ) {
out <- factor ( out , levels = attributes ( guideline ) $ values , ordered = TRUE )
}
2022-08-28 10:31:50 +02:00
2022-08-27 20:49:37 +02:00
columns_nonsusceptible <- as.data.frame ( t ( df [ , is.rsi ( df ) , drop = FALSE ] == " R" ) )
2022-08-28 10:31:50 +02:00
columns_nonsusceptible <- vapply (
FUN.VALUE = character ( 1 ) ,
columns_nonsusceptible ,
function ( x ) paste0 ( rownames ( columns_nonsusceptible ) [which ( x ) ] , collapse = " " )
)
2021-01-17 00:26:48 +01:00
columns_nonsusceptible [is.na ( out ) ] <- NA_character_
2022-08-28 10:31:50 +02:00
data.frame (
row_number = seq_len ( NROW ( df ) ) ,
MDRO = out ,
reason = reasons ,
columns_nonsusceptible = columns_nonsusceptible ,
stringsAsFactors = FALSE
)
2021-01-17 00:26:48 +01:00
}
2018-11-16 20:50:50 +01:00
#' @rdname mdro
2018-04-18 12:24:54 +02:00
#' @export
2021-02-08 14:18:42 +01:00
brmo <- function ( x = NULL , only_rsi_columns = FALSE , ... ) {
meet_criteria ( x , allow_class = " data.frame" , allow_NULL = TRUE )
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
stop_if (
" guideline" %in% names ( list ( ... ) ) ,
" argument `guideline` must not be set since this is a guideline-specific function"
)
2021-02-08 14:18:42 +01:00
mdro ( x = x , only_rsi_columns = only_rsi_columns , guideline = " BRMO" , ... )
2018-04-18 12:24:54 +02:00
}
2018-11-16 20:50:50 +01:00
#' @rdname mdro
2018-04-18 12:24:54 +02:00
#' @export
2021-02-08 14:18:42 +01:00
mrgn <- function ( x = NULL , only_rsi_columns = FALSE , ... ) {
meet_criteria ( x , allow_class = " data.frame" , allow_NULL = TRUE )
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
stop_if (
" guideline" %in% names ( list ( ... ) ) ,
" argument `guideline` must not be set since this is a guideline-specific function"
)
2021-02-08 14:18:42 +01:00
mdro ( x = x , only_rsi_columns = only_rsi_columns , guideline = " MRGN" , ... )
2018-04-25 15:33:58 +02:00
}
2018-11-16 20:50:50 +01:00
#' @rdname mdro
2018-04-25 15:33:58 +02:00
#' @export
2021-02-08 14:18:42 +01:00
mdr_tb <- function ( x = NULL , only_rsi_columns = FALSE , ... ) {
meet_criteria ( x , allow_class = " data.frame" , allow_NULL = TRUE )
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
stop_if (
" guideline" %in% names ( list ( ... ) ) ,
" argument `guideline` must not be set since this is a guideline-specific function"
)
2021-02-08 14:18:42 +01:00
mdro ( x = x , only_rsi_columns = only_rsi_columns , guideline = " TB" , ... )
2019-05-23 16:58:59 +02:00
}
2019-10-26 21:56:41 +02:00
#' @rdname mdro
#' @export
2021-02-08 14:18:42 +01:00
mdr_cmi2012 <- function ( x = NULL , only_rsi_columns = FALSE , ... ) {
meet_criteria ( x , allow_class = " data.frame" , allow_NULL = TRUE )
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
stop_if (
" guideline" %in% names ( list ( ... ) ) ,
" argument `guideline` must not be set since this is a guideline-specific function"
)
2021-02-08 14:18:42 +01:00
mdro ( x = x , only_rsi_columns = only_rsi_columns , guideline = " CMI2012" , ... )
2019-10-26 21:56:41 +02:00
}
2019-05-23 16:58:59 +02:00
#' @rdname mdro
#' @export
2021-02-08 14:18:42 +01:00
eucast_exceptional_phenotypes <- function ( x = NULL , only_rsi_columns = FALSE , ... ) {
meet_criteria ( x , allow_class = " data.frame" , allow_NULL = TRUE )
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 )
2022-08-28 10:31:50 +02:00
stop_if (
" guideline" %in% names ( list ( ... ) ) ,
" argument `guideline` must not be set since this is a guideline-specific function"
)
2021-02-08 14:18:42 +01:00
mdro ( x = x , only_rsi_columns = only_rsi_columns , guideline = " EUCAST" , ... )
2018-04-18 12:24:54 +02:00
}