2018-08-10 15:01:05 +02:00
# ==================================================================== #
# TITLE #
2021-02-02 23:57:35 +01:00
# Antimicrobial Resistance (AMR) Data Analysis for R #
2018-08-10 15:01:05 +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-08-10 15:01:05 +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 #
# Diagnostics & Advice, and University Medical Center Groningen. #
2018-08-10 15:01:05 +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-08-10 15:01:05 +02:00
# ==================================================================== #
2021-01-18 16:57:56 +01:00
#' Interpret MIC and Disk Values, or Clean Raw R/SI Data
2018-08-10 15:01:05 +02:00
#'
2021-05-12 18:15:03 +02:00
#' Interpret minimum inhibitory concentration (MIC) values and disk diffusion diameters according to EUCAST or CLSI, or clean up existing R/SI values. This transforms the input to a new class [`rsi`], which is an ordered [factor] with levels `S < I < R`.
2021-01-18 16:57:56 +01:00
#' @inheritSection lifecycle Stable Lifecycle
2018-08-25 22:01:14 +02:00
#' @rdname as.rsi
2021-05-12 18:15:03 +02:00
#' @param x vector of values (for class [`mic`]: MIC values in mg/L, for class [`disk`]: a disk diffusion radius in millimetres)
#' @param mo any (vector of) text that can be coerced to valid microorganism codes with [as.mo()], can be left empty to determine it automatically
2020-02-17 14:38:01 +01:00
#' @param ab any (vector of) text that can be coerced to a valid antimicrobial code with [as.ab()]
2021-05-12 18:15:03 +02:00
#' @param uti (Urinary Tract Infection) A vector with [logical]s (`TRUE` or `FALSE`) to specify whether a UTI specific interpretation from the guideline should be chosen. For using [as.rsi()] on a [data.frame], this can also be a column containing [logical]s or when left blank, the data set will be searched for a column 'specimen', and rows within this column containing 'urin' (such as 'urine', 'urina') will be regarded isolates from a UTI. See *Examples*.
2019-05-10 16:44:59 +02:00
#' @inheritParams first_isolate
2021-01-18 16:57:56 +01:00
#' @param guideline defaults to the latest included EUCAST guideline, see *Details* for all options
2021-05-12 18:15:03 +02:00
#' @param conserve_capped_values a [logical] to indicate that MIC values starting with `">"` (but not `">="`) must always return "R" , and that MIC values starting with `"<"` (but not `"<="`) must always return "S"
2021-12-11 13:41:31 +01:00
#' @param add_intrinsic_resistance *(only useful when using a EUCAST guideline)* a [logical] to indicate whether intrinsic antibiotic resistance must also be considered for applicable bug-drug combinations, meaning that e.g. ampicillin will always return "R" in *Klebsiella* species. Determination is based on the [intrinsic_resistant] data set, that itself is based on `r format_eucast_version_nr(3.3)`.
2020-12-22 00:51:17 +01:00
#' @param reference_data a [data.frame] to be used for interpretation, which defaults to the [rsi_translation] data set. Changing this argument allows for using own interpretation guidelines. This argument must contain a data set that is equal in structure to the [rsi_translation] data set (same column names and column types). Please note that the `guideline` argument will be ignored when `reference_data` is manually set.
2021-01-18 16:57:56 +01:00
#' @param threshold maximum fraction of invalid antimicrobial interpretations of `x`, see *Examples*
2021-12-11 13:41:31 +01:00
#' @param ... for using on a [data.frame]: names of columns to apply [as.rsi()] on (supports tidy selection such as `column1:column4`). Otherwise: arguments passed on to methods.
2020-05-16 13:05:47 +02:00
#' @details
2021-01-18 16:57:56 +01:00
#' ## How it Works
2020-01-27 11:05:39 +01:00
#'
2020-08-10 11:44:58 +02:00
#' The [as.rsi()] function works in four ways:
2020-05-16 13:05:47 +02:00
#'
2020-08-10 11:44:58 +02:00
#' 1. For **cleaning raw / untransformed data**. The data will be cleaned to only contain values S, I and R and will try its best to determine this with some intelligence. For example, mixed values with R/SI interpretations and MIC values such as `"<0.25; S"` will be coerced to `"S"`. Combined interpretations for multiple test methods (as seen in laboratory records) such as `"S; S"` will be coerced to `"S"`, but a value like `"S; I"` will return `NA` with a warning that the input is unclear.
#'
2020-12-22 00:51:17 +01:00
#' 2. For **interpreting minimum inhibitory concentration (MIC) values** according to EUCAST or CLSI. You must clean your MIC values first using [as.mic()], that also gives your columns the new data class [`mic`]. Also, be sure to have a column with microorganism names or codes. It will be found automatically, but can be set manually using the `mo` argument.
2020-08-10 11:44:58 +02:00
#' * Using `dplyr`, R/SI interpretation can be done very easily with either:
#' ```
2021-05-12 18:15:03 +02:00
#' your_data %>% mutate_if(is.mic, as.rsi) # until dplyr 1.0.0
#' your_data %>% mutate(across(where(is.mic), as.rsi)) # since dplyr 1.0.0
2020-08-10 11:44:58 +02:00
#' ```
#' * Operators like "<=" will be stripped before interpretation. When using `conserve_capped_values = TRUE`, an MIC value of e.g. ">2" will always return "R", even if the breakpoint according to the chosen guideline is ">=4". This is to prevent that capped values from raw laboratory data would not be treated conservatively. The default behaviour (`conserve_capped_values = FALSE`) considers ">2" to be lower than ">=4" and might in this case return "S" or "I".
#'
2020-12-22 00:51:17 +01:00
#' 3. For **interpreting disk diffusion diameters** according to EUCAST or CLSI. You must clean your disk zones first using [as.disk()], that also gives your columns the new data class [`disk`]. Also, be sure to have a column with microorganism names or codes. It will be found automatically, but can be set manually using the `mo` argument.
2020-08-10 11:44:58 +02:00
#' * Using `dplyr`, R/SI interpretation can be done very easily with either:
#' ```
2021-05-12 18:15:03 +02:00
#' your_data %>% mutate_if(is.disk, as.rsi) # until dplyr 1.0.0
#' your_data %>% mutate(across(where(is.disk), as.rsi)) # since dplyr 1.0.0
2020-08-10 11:44:58 +02:00
#' ```
#'
2021-12-11 13:41:31 +01:00
#' 4. For **interpreting a complete data set**, with automatic determination of MIC values, disk diffusion diameters, microorganism names or codes, and antimicrobial test results. This is done very simply by running `as.rsi(your_data)`.
2020-08-10 11:44:58 +02:00
#'
2021-01-18 16:57:56 +01:00
#' ## Supported Guidelines
2020-08-10 11:44:58 +02:00
#'
2021-06-01 15:33:06 +02:00
#' For interpreting MIC values as well as disk diffusion diameters, currently implemented guidelines are EUCAST (`r min(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "EUCAST")$guideline)))`-`r max(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "EUCAST")$guideline)))`) and CLSI (`r min(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "CLSI")$guideline)))`-`r max(as.integer(gsub("[^0-9]", "", subset(rsi_translation, guideline %like% "CLSI")$guideline)))`).
2020-08-10 11:44:58 +02:00
#'
2022-03-03 21:11:02 +01:00
#' Thus, the `guideline` argument must be set to e.g., ``r paste0('"', subset(rsi_translation, guideline %like% "EUCAST")$guideline[1], '"')`` or ``r paste0('"', subset(rsi_translation, guideline %like% "CLSI")$guideline[1], '"')``. By simply using `"EUCAST"` (the default) or `"CLSI"` as input, the latest included version of that guideline will automatically be selected. You can set your own data set using the `reference_data` argument. The `guideline` argument will then be ignored.
2020-08-10 11:44:58 +02:00
#'
2021-01-18 16:57:56 +01:00
#' ## After Interpretation
2020-08-10 11:44:58 +02:00
#'
#' After using [as.rsi()], you can use the [eucast_rules()] defined by EUCAST to (1) apply inferred susceptibility and resistance based on results of other antimicrobials and (2) apply intrinsic resistance based on taxonomic properties of a microorganism.
#'
2021-01-18 16:57:56 +01:00
#' ## Machine-Readable Interpretation Guidelines
2020-07-29 11:46:59 +02:00
#'
2021-10-06 13:23:57 +02:00
#' The repository of this package [contains a machine-readable version](https://github.com/msberends/AMR/blob/main/data-raw/rsi_translation.txt) of all guidelines. This is a CSV file consisting of `r format(nrow(AMR::rsi_translation), big.mark = ",")` rows and `r ncol(AMR::rsi_translation)` columns. This file is machine-readable, since it contains one row for every unique combination of the test method (MIC or disk diffusion), the antimicrobial agent and the microorganism. **This allows for easy implementation of these rules in laboratory information systems (LIS)**. Note that it only contains interpretation guidelines for humans - interpretation guidelines from CLSI for animals were removed.
2019-04-09 14:59:17 +02:00
#'
2020-08-10 11:44:58 +02:00
#' ## Other
2021-02-02 23:57:35 +01:00
#'
2021-05-12 18:15:03 +02:00
#' The function [is.rsi()] detects if the input contains class `<rsi>`. If the input is a [data.frame], it iterates over all columns and returns a [logical] vector.
2019-05-10 16:44:59 +02:00
#'
2021-05-12 18:15:03 +02:00
#' The function [is.rsi.eligible()] returns `TRUE` when a columns contains at most 5% invalid antimicrobial interpretations (not S and/or I and/or R), and `FALSE` otherwise. The threshold of 5% can be set with the `threshold` argument. If the input is a [data.frame], it iterates over all columns and returns a [logical] vector.
2019-11-29 19:43:23 +01:00
#' @section Interpretation of R and S/I:
2020-10-08 11:16:03 +02:00
#' In 2019, the European Committee on Antimicrobial Susceptibility Testing (EUCAST) has decided to change the definitions of susceptibility testing categories R and S/I as shown below (<https://www.eucast.org/newsiandr/>).
2019-05-13 10:10:16 +02:00
#'
2019-11-29 19:43:23 +01:00
#' - **R = Resistant**\cr
#' A microorganism is categorised as *Resistant* when there is a high likelihood of therapeutic failure even when there is increased exposure. Exposure is a function of how the mode of administration, dose, dosing interval, infusion time, as well as distribution and excretion of the antimicrobial agent will influence the infecting organism at the site of infection.
#' - **S = Susceptible**\cr
#' A microorganism is categorised as *Susceptible, standard dosing regimen*, when there is a high likelihood of therapeutic success using a standard dosing regimen of the agent.
2021-08-16 21:54:34 +02:00
#' - **I = Susceptible, Increased exposure**\cr
2019-11-29 19:43:23 +01:00
#' A microorganism is categorised as *Susceptible, Increased exposure* when there is a high likelihood of therapeutic success because exposure to the agent is increased by adjusting the dosing regimen or by its concentration at the site of infection.
2019-05-13 10:10:16 +02:00
#'
2021-06-01 15:33:06 +02:00
#' This AMR package honours this (new) insight. Use [susceptibility()] (equal to [proportion_SI()]) to determine antimicrobial susceptibility and [count_susceptible()] (equal to [count_SI()]) to count susceptible isolates.
2021-05-12 18:15:03 +02:00
#' @return Ordered [factor] with new class `<rsi>`
2019-11-28 23:00:37 +01:00
#' @aliases rsi
2018-08-10 15:01:05 +02:00
#' @export
2020-08-10 11:44:58 +02:00
#' @seealso [as.mic()], [as.disk()], [as.mo()]
2021-01-18 16:57:56 +01:00
#' @inheritSection AMR Reference Data Publicly Available
#' @inheritSection AMR Read more on Our Website!
2018-08-25 22:01:14 +02:00
#' @examples
2020-06-22 13:18:40 +02:00
#' summary(example_isolates) # see all R/SI results at a glance
2021-05-24 09:00:11 +02:00
#' \donttest{
2020-09-30 10:24:53 +02:00
#' if (require("skimr")) {
2020-10-21 11:50:43 +02:00
#' # class <rsi> supported in skim() too:
2020-09-30 10:24:53 +02:00
#' skim(example_isolates)
#' }
2021-05-24 09:00:11 +02:00
#' }
2020-02-16 22:43:56 +01:00
#' # For INTERPRETING disk diffusion and MIC values -----------------------
2020-02-20 13:19:23 +01:00
#'
#' # a whole data set, even with combined MIC values and disk zones
2020-10-21 11:50:43 +02:00
#' df <- data.frame(microorganism = "Escherichia coli",
2020-02-20 13:19:23 +01:00
#' AMP = as.mic(8),
#' CIP = as.mic(0.256),
#' GEN = as.disk(18),
#' TOB = as.disk(16),
2020-12-22 00:51:17 +01:00
#' NIT = as.mic(32),
#' ERY = "R")
2020-02-20 13:19:23 +01:00
#' as.rsi(df)
2020-02-16 22:43:56 +01:00
#'
2020-02-20 13:19:23 +01:00
#' # for single values
2019-05-10 16:44:59 +02:00
#' as.rsi(x = as.mic(2),
#' mo = as.mo("S. pneumoniae"),
2020-02-14 19:54:13 +01:00
#' ab = "AMP",
2019-05-10 16:44:59 +02:00
#' guideline = "EUCAST")
2020-02-14 19:54:13 +01:00
#'
#' as.rsi(x = as.disk(18),
#' mo = "Strep pneu", # `mo` will be coerced with as.mo()
#' ab = "ampicillin", # and `ab` with as.ab()
2019-05-10 16:44:59 +02:00
#' guideline = "EUCAST")
2020-02-16 22:43:56 +01:00
#'
2020-09-29 23:35:46 +02:00
#' \donttest{
#' # the dplyr way
#' if (require("dplyr")) {
#' df %>% mutate_if(is.mic, as.rsi)
#' df %>% mutate_if(function(x) is.mic(x) | is.disk(x), as.rsi)
2021-05-13 15:56:12 +02:00
#' df %>% mutate(across(where(is.mic), as.rsi))
2020-09-29 23:35:46 +02:00
#' df %>% mutate_at(vars(AMP:TOB), as.rsi)
2020-09-30 10:24:53 +02:00
#' df %>% mutate(across(AMP:TOB, as.rsi))
2020-09-29 23:35:46 +02:00
#'
#' df %>%
2020-10-21 11:50:43 +02:00
#' mutate_at(vars(AMP:TOB), as.rsi, mo = .$microorganism)
2020-09-29 23:35:46 +02:00
#'
#' # to include information about urinary tract infections (UTI)
#' data.frame(mo = "E. coli",
#' NIT = c("<= 2", 32),
#' from_the_bladder = c(TRUE, FALSE)) %>%
#' as.rsi(uti = "from_the_bladder")
#'
#' data.frame(mo = "E. coli",
#' NIT = c("<= 2", 32),
#' specimen = c("urine", "blood")) %>%
#' as.rsi() # automatically determines urine isolates
#'
#' df %>%
#' mutate_at(vars(AMP:NIT), as.rsi, mo = "E. coli", uti = TRUE)
#' }
2019-05-10 16:44:59 +02:00
#'
2020-02-16 22:43:56 +01:00
#' # For CLEANING existing R/SI values ------------------------------------
#'
#' as.rsi(c("S", "I", "R", "A", "B", "C"))
#' as.rsi("<= 0.002; S") # will return "S"
2020-09-24 12:38:13 +02:00
2020-02-16 22:43:56 +01:00
#' rsi_data <- as.rsi(c(rep("S", 474), rep("I", 36), rep("R", 370)))
#' is.rsi(rsi_data)
2018-08-25 22:01:14 +02:00
#' plot(rsi_data) # for percentages
#' barplot(rsi_data) # for frequencies
#'
2020-09-29 23:35:46 +02:00
#' # the dplyr way
#' if (require("dplyr")) {
#' example_isolates %>%
#' mutate_at(vars(PEN:RIF), as.rsi)
#' # same:
#' example_isolates %>%
#' as.rsi(PEN:RIF)
#'
#' # fastest way to transform all columns with already valid AMR results to class `rsi`:
#' example_isolates %>%
#' mutate_if(is.rsi.eligible, as.rsi)
#'
#' # note: from dplyr 1.0.0 on, this will be:
#' # example_isolates %>%
2021-05-13 15:56:12 +02:00
#' # mutate(across(where(is.rsi.eligible), as.rsi))
2020-09-29 23:35:46 +02:00
#' }
2020-05-16 21:40:50 +02:00
#' }
2019-05-10 16:44:59 +02:00
as.rsi <- function ( x , ... ) {
UseMethod ( " as.rsi" )
}
2021-11-28 23:01:26 +01:00
#' @rdname as.rsi
#' @details `NA_rsi_` is a missing value of the new `<rsi>` class.
#' @export
NA_rsi_ <- set_clean_class ( factor ( NA , levels = c ( " S" , " I" , " R" ) , ordered = TRUE ) ,
new_class = c ( " rsi" , " ordered" , " factor" ) )
2020-07-29 13:48:50 +02:00
#' @rdname as.rsi
#' @export
is.rsi <- function ( x ) {
2021-02-02 23:57:35 +01:00
if ( inherits ( x , " data.frame" ) ) {
unname ( vapply ( FUN.VALUE = logical ( 1 ) , x , is.rsi ) )
} else {
inherits ( x , " rsi" )
}
2020-07-29 13:48:50 +02:00
}
#' @rdname as.rsi
#' @export
is.rsi.eligible <- function ( x , threshold = 0.05 ) {
2020-10-19 17:09:19 +02:00
meet_criteria ( threshold , allow_class = " numeric" , has_length = 1 )
2021-02-04 16:48:16 +01:00
if ( inherits ( x , " data.frame" ) ) {
2021-02-08 14:18:42 +01:00
# iterate this function over all columns
2021-02-04 16:48:16 +01:00
return ( unname ( vapply ( FUN.VALUE = logical ( 1 ) , x , is.rsi.eligible ) ) )
}
2020-07-29 13:48:50 +02:00
stop_if ( NCOL ( x ) > 1 , " `x` must be a one-dimensional vector." )
2021-02-02 23:57:35 +01:00
if ( any ( c ( " numeric" ,
2020-07-29 13:48:50 +02:00
" integer" ,
" mo" ,
2021-01-22 10:20:41 +01:00
" ab" ,
2020-07-29 13:48:50 +02:00
" Date" ,
2021-01-22 10:20:41 +01:00
" POSIXt" ,
2020-07-29 13:48:50 +02:00
" raw" ,
2021-01-22 10:20:41 +01:00
" hms" ,
" mic" ,
" disk" )
2020-07-29 13:48:50 +02:00
%in% class ( x ) ) ) {
# no transformation needed
2021-01-22 10:20:41 +01:00
return ( FALSE )
2021-02-02 23:57:35 +01:00
} else if ( all ( x %in% c ( " R" , " S" , " I" , NA ) ) & ! all ( is.na ( x ) ) ) {
return ( TRUE )
} else if ( ! any ( c ( " R" , " S" , " I" ) %in% x , na.rm = TRUE ) & ! all ( is.na ( x ) ) ) {
2021-01-22 10:20:41 +01:00
return ( FALSE )
2020-07-29 13:48:50 +02:00
} else {
2022-03-02 15:38:55 +01:00
x <- x [ ! is.na ( x ) & ! is.null ( x ) & ! x %in% c ( " " , " -" , " NULL" ) ]
2020-07-29 13:48:50 +02:00
if ( length ( x ) == 0 ) {
2022-03-02 15:38:55 +01:00
# no other values than empty
2021-02-02 23:57:35 +01:00
cur_col <- get_current_column ( )
if ( ! is.null ( cur_col ) ) {
ab <- suppressWarnings ( as.ab ( cur_col , fast_mode = TRUE , info = FALSE ) )
if ( ! is.na ( ab ) ) {
# this is a valid antibiotic code
2021-02-08 14:18:42 +01:00
message_ ( " Column '" , font_bold ( cur_col ) , " ' is as.rsi()-eligible (despite only having empty values), since it seems to be " ,
ab_name ( ab , language = NULL , tolower = TRUE ) , " (" , ab , " )" )
2021-02-02 23:57:35 +01:00
return ( TRUE )
}
}
# all values empty and no antibiotic col name - return FALSE
2020-07-29 13:48:50 +02:00
return ( FALSE )
}
2021-02-02 23:57:35 +01:00
# transform all values and see if it meets the set threshold
2020-07-29 13:48:50 +02:00
checked <- suppressWarnings ( as.rsi ( x ) )
outcome <- sum ( is.na ( checked ) ) / length ( x )
outcome <= threshold
}
}
2019-05-10 16:44:59 +02:00
#' @export
2022-03-02 15:38:55 +01:00
# extra param: warn (logical, to never throw a warning)
2019-05-10 16:44:59 +02:00
as.rsi.default <- function ( x , ... ) {
2018-08-25 22:01:14 +02:00
if ( is.rsi ( x ) ) {
2020-11-16 16:57:55 +01:00
return ( x )
}
2021-11-28 23:01:26 +01:00
x.bak <- x
x <- as.character ( x ) # this is needed to prevent the vctrs pkg from throwing an error
if ( inherits ( x.bak , c ( " integer" , " numeric" , " double" ) ) && all ( x %in% c ( 1 : 3 , NA ) ) ) {
2021-05-04 15:20:43 +02:00
# support haven package for importing e.g., from SPSS - it adds the 'labels' attribute
2021-11-28 23:01:26 +01:00
lbls <- attributes ( x.bak ) $ labels
2021-05-04 15:20:43 +02:00
if ( ! is.null ( lbls ) && all ( c ( " R" , " S" , " I" ) %in% names ( lbls ) ) && all ( c ( 1 : 3 ) %in% lbls ) ) {
x [x.bak == 1 ] <- names ( lbls [lbls == 1 ] )
x [x.bak == 2 ] <- names ( lbls [lbls == 2 ] )
x [x.bak == 3 ] <- names ( lbls [lbls == 3 ] )
} else {
x [x.bak == 1 ] <- " S"
x [x.bak == 2 ] <- " I"
x [x.bak == 3 ] <- " R"
}
2022-03-02 15:38:55 +01:00
2021-05-04 15:20:43 +02:00
} else if ( ! all ( is.na ( x ) ) && ! identical ( levels ( x ) , c ( " R" , " S" , " I" ) ) && ! all ( x %in% c ( " R" , " S" , " I" , NA ) ) ) {
2022-03-02 15:38:55 +01:00
2021-04-23 09:59:36 +02:00
if ( all ( x %unlike% " (R|S|I)" , na.rm = TRUE ) ) {
2021-02-25 10:33:08 +01:00
# check if they are actually MICs or disks
2020-09-24 00:30:11 +02:00
if ( all_valid_mics ( x ) ) {
2022-03-02 15:38:55 +01:00
warning_ ( " in `as.rsi()`: the input seems to contain MIC values. You can transform them with `as.mic()` before running `as.rsi()` to interpret them." )
2020-09-24 00:30:11 +02:00
} else if ( all_valid_disks ( x ) ) {
2022-03-02 15:38:55 +01:00
warning_ ( " in `as.rsi()`: the input seems to contain disk diffusion values. You can transform them with `as.disk()` before running `as.rsi()` to interpret them." )
2020-02-20 17:21:01 +01:00
}
}
2019-10-23 14:48:25 +02:00
2021-07-03 21:56:53 +02:00
# trim leading and trailing spaces, new lines, etc.
x <- trimws2 ( as.character ( unlist ( x ) ) )
2022-03-02 15:38:55 +01:00
x [x %in% c ( NA , " " , " -" , " NULL" ) ] <- NA_character_
2018-08-25 22:01:14 +02:00
x.bak <- x
2022-03-02 15:38:55 +01:00
na_before <- length ( x [is.na ( x ) ] )
2021-07-03 21:56:53 +02:00
# correct for translations
trans_R <- unlist ( TRANSLATIONS [which ( TRANSLATIONS $ pattern == " Resistant" ) ,
LANGUAGES_SUPPORTED [LANGUAGES_SUPPORTED %in% colnames ( TRANSLATIONS ) ] ] )
trans_S <- unlist ( TRANSLATIONS [which ( TRANSLATIONS $ pattern == " Susceptible" ) ,
LANGUAGES_SUPPORTED [LANGUAGES_SUPPORTED %in% colnames ( TRANSLATIONS ) ] ] )
2021-07-23 21:42:11 +02:00
trans_I <- unlist ( TRANSLATIONS [which ( TRANSLATIONS $ pattern %in% c ( " Incr. exposure" , " Susceptible, incr. exp." , " Intermediate" ) ) ,
2021-07-03 21:56:53 +02:00
LANGUAGES_SUPPORTED [LANGUAGES_SUPPORTED %in% colnames ( TRANSLATIONS ) ] ] )
x <- gsub ( paste0 ( unique ( trans_R [ ! is.na ( trans_R ) ] ) , collapse = " |" ) , " R" , x , ignore.case = TRUE )
x <- gsub ( paste0 ( unique ( trans_S [ ! is.na ( trans_S ) ] ) , collapse = " |" ) , " S" , x , ignore.case = TRUE )
x <- gsub ( paste0 ( unique ( trans_I [ ! is.na ( trans_I ) ] ) , collapse = " |" ) , " I" , x , ignore.case = TRUE )
# replace all English textual input
2021-07-23 21:42:11 +02:00
x [x %like% " ([^a-z]|^)res(is(tant)?)?" ] <- " R"
x [x %like% " ([^a-z]|^)sus(cep(tible)?)?" ] <- " S"
x [x %like% " ([^a-z]|^)int(er(mediate)?)?|incr.*exp" ] <- " I"
2021-11-28 23:01:26 +01:00
# remove other invalid characters
2018-08-25 22:01:14 +02:00
# set to capitals
x <- toupper ( x )
2022-02-26 21:58:23 +01:00
x <- gsub ( " [^RSIHDU]+" , " " , x , perl = TRUE )
# some labs now report "H" instead of "I" to not interfere with EUCAST prior to 2019
x <- gsub ( " ^H$" , " I" , x , perl = TRUE )
# and MIPS uses D for Dose-dependent (which is I, but it will throw a note)
x <- gsub ( " ^D$" , " I" , x , perl = TRUE )
# and MIPS uses U for "susceptible urine"
x <- gsub ( " ^U$" , " S" , x , perl = TRUE )
2018-08-25 22:01:14 +02:00
# in cases of "S;S" keep S, but in case of "S;I" make it NA
2019-10-11 17:21:02 +02:00
x <- gsub ( " ^S+$" , " S" , x )
x <- gsub ( " ^I+$" , " I" , x )
x <- gsub ( " ^R+$" , " R" , x )
2021-11-28 23:01:26 +01:00
x [ ! x %in% c ( " S" , " I" , " R" ) ] <- NA_character_
2020-06-26 10:21:22 +02:00
na_after <- length ( x [is.na ( x ) | x == " " ] )
2019-08-09 14:28:46 +02:00
if ( ! isFALSE ( list ( ... ) $ warn ) ) { # so as.rsi(..., warn = FALSE) will never throw a warning
if ( na_before != na_after ) {
2020-09-18 16:05:53 +02:00
list_missing <- x.bak [is.na ( x ) & ! is.na ( x.bak ) & x.bak != " " ] %pm>%
unique ( ) %pm>%
2021-02-04 16:48:16 +01:00
sort ( ) %pm>%
vector_and ( quotes = TRUE )
2022-03-02 15:38:55 +01:00
warning_ ( " in `as.rsi()`: " , na_after - na_before , " results truncated (" ,
2020-11-10 16:35:56 +01:00
round ( ( ( na_after - na_before ) / length ( x ) ) * 100 ) ,
" %) that were invalid antimicrobial interpretations: " ,
list_missing , call = FALSE )
2022-02-26 21:58:23 +01:00
}
2022-03-02 15:38:55 +01:00
if ( any ( toupper ( x.bak [ ! is.na ( x.bak ) ] ) == " U" ) && message_not_thrown_before ( " as.rsi" , " U" ) ) {
warning_ ( " in `as.rsi()`: 'U' was interpreted as 'S', following some laboratory systems" )
2022-02-26 21:58:23 +01:00
}
2022-03-02 15:38:55 +01:00
if ( any ( toupper ( x.bak [ ! is.na ( x.bak ) ] ) == " D" ) && message_not_thrown_before ( " as.rsi" , " D" ) ) {
warning_ ( " in `as.rsi()`: 'D' (dose-dependent) was interpreted as 'I', following some laboratory systems" )
2022-02-26 21:58:23 +01:00
}
2022-03-02 15:38:55 +01:00
if ( any ( toupper ( x.bak [ ! is.na ( x.bak ) ] ) == " H" ) && message_not_thrown_before ( " as.rsi" , " H" ) ) {
warning_ ( " in `as.rsi()`: 'H' was interpreted as 'I', following some laboratory systems" )
2019-08-09 14:28:46 +02:00
}
2018-08-25 22:01:14 +02:00
}
2018-08-23 00:40:36 +02:00
}
2020-11-16 16:57:55 +01:00
set_clean_class ( factor ( x , levels = c ( " S" , " I" , " R" ) , ordered = TRUE ) ,
new_class = c ( " rsi" , " ordered" , " factor" ) )
2018-08-25 22:01:14 +02:00
}
2018-08-23 00:40:36 +02:00
2019-05-10 16:44:59 +02:00
#' @rdname as.rsi
#' @export
2020-07-29 11:46:59 +02:00
as.rsi.mic <- function ( x ,
2020-08-10 11:44:58 +02:00
mo = NULL ,
2020-07-29 11:46:59 +02:00
ab = deparse ( substitute ( x ) ) ,
guideline = " EUCAST" ,
uti = FALSE ,
conserve_capped_values = FALSE ,
2020-08-16 21:38:42 +02:00
add_intrinsic_resistance = FALSE ,
2020-11-11 16:49:27 +01:00
reference_data = AMR :: rsi_translation ,
2020-07-29 11:46:59 +02:00
... ) {
2022-03-02 15:38:55 +01:00
as_rsi_method ( method_short = " mic" ,
method_long = " MIC values" ,
x = x ,
mo = mo ,
ab = ab ,
guideline = guideline ,
uti = uti ,
conserve_capped_values = conserve_capped_values ,
add_intrinsic_resistance = add_intrinsic_resistance ,
reference_data = reference_data ,
... )
2019-05-10 16:44:59 +02:00
}
#' @rdname as.rsi
#' @export
2020-07-29 11:46:59 +02:00
as.rsi.disk <- function ( x ,
2020-08-10 11:44:58 +02:00
mo = NULL ,
2020-07-29 11:46:59 +02:00
ab = deparse ( substitute ( x ) ) ,
guideline = " EUCAST" ,
uti = FALSE ,
2020-08-16 21:38:42 +02:00
add_intrinsic_resistance = FALSE ,
2020-11-11 16:49:27 +01:00
reference_data = AMR :: rsi_translation ,
2020-07-29 11:46:59 +02:00
... ) {
2022-03-02 15:38:55 +01:00
as_rsi_method ( method_short = " disk" ,
method_long = " disk diffusion zones" ,
x = x ,
mo = mo ,
ab = ab ,
guideline = guideline ,
uti = uti ,
conserve_capped_values = FALSE ,
add_intrinsic_resistance = add_intrinsic_resistance ,
reference_data = reference_data ,
... )
2020-02-20 13:19:23 +01:00
}
#' @rdname as.rsi
#' @export
2020-09-24 12:38:13 +02:00
as.rsi.data.frame <- function ( x ,
... ,
2020-07-29 11:46:59 +02:00
col_mo = NULL ,
guideline = " EUCAST" ,
uti = NULL ,
conserve_capped_values = FALSE ,
2020-11-11 16:49:27 +01:00
add_intrinsic_resistance = FALSE ,
2021-01-24 14:48:56 +01:00
reference_data = AMR :: rsi_translation ) {
2020-10-19 17:09:19 +02:00
meet_criteria ( x , allow_class = " data.frame" ) # will also check for dimensions > 0
meet_criteria ( col_mo , allow_class = " character" , is_in = colnames ( x ) , allow_NULL = TRUE )
meet_criteria ( guideline , allow_class = " character" , has_length = 1 )
2020-10-21 11:50:43 +02:00
meet_criteria ( uti , allow_class = c ( " logical" , " character" ) , allow_NULL = TRUE )
2020-10-19 17:09:19 +02:00
meet_criteria ( conserve_capped_values , allow_class = " logical" , has_length = 1 )
meet_criteria ( add_intrinsic_resistance , allow_class = " logical" , has_length = 1 )
2020-11-11 16:49:27 +01:00
meet_criteria ( reference_data , allow_class = " data.frame" )
2022-03-02 15:38:55 +01:00
2020-12-22 00:51:17 +01:00
x.bak <- x
2020-11-10 19:59:14 +01:00
for ( i in seq_len ( ncol ( x ) ) ) {
2021-12-11 13:41:31 +01:00
# don't keep factors, overwriting them is hard
2020-11-10 19:59:14 +01:00
if ( is.factor ( x [ , i , drop = TRUE ] ) ) {
x [ , i ] <- as.character ( x [ , i , drop = TRUE ] )
}
}
2020-10-20 21:00:57 +02:00
# -- MO
col_mo.bak <- col_mo
if ( is.null ( col_mo ) ) {
col_mo <- search_type_in_df ( x = x , type = " mo" , info = FALSE )
}
2022-03-02 15:38:55 +01:00
2020-02-20 13:19:23 +01:00
# -- UTIs
col_uti <- uti
if ( is.null ( col_uti ) ) {
col_uti <- search_type_in_df ( x = x , type = " uti" )
}
if ( ! is.null ( col_uti ) ) {
if ( is.logical ( col_uti ) ) {
2021-05-12 18:15:03 +02:00
# already a [logical] vector as input
2020-02-20 13:19:23 +01:00
if ( length ( col_uti ) == 1 ) {
uti <- rep ( col_uti , NROW ( x ) )
} else {
uti <- col_uti
}
} else {
# column found, transform to logical
2020-10-21 11:50:43 +02:00
stop_if ( length ( col_uti ) != 1 | ! col_uti %in% colnames ( x ) ,
2021-05-12 18:15:03 +02:00
" argument `uti` must be a [logical] vector, of must be a single column name of `x`" )
2020-02-20 13:19:23 +01:00
uti <- as.logical ( x [ , col_uti , drop = TRUE ] )
}
} else {
# look for specimen column and make logicals of the urines
col_specimen <- suppressMessages ( search_type_in_df ( x = x , type = " specimen" ) )
if ( ! is.null ( col_specimen ) ) {
uti <- x [ , col_specimen , drop = TRUE ] %like% " urin"
values <- sort ( unique ( x [uti , col_specimen , drop = TRUE ] ) )
if ( length ( values ) > 1 ) {
plural <- c ( " s" , " " , " " )
} else {
plural <- c ( " " , " s" , " a " )
}
2020-10-27 15:56:51 +01:00
message_ ( " Assuming value" , plural [1 ] , " " ,
2021-02-04 16:48:16 +01:00
vector_and ( values , quotes = TRUE ) ,
2020-12-22 00:51:17 +01:00
" in column '" , font_bold ( col_specimen ) ,
" ' reflect" , plural [2 ] , " " , plural [3 ] , " urinary tract infection" , plural [1 ] ,
2020-10-27 15:56:51 +01:00
" .\n Use `as.rsi(uti = FALSE)` to prevent this." )
2020-02-20 13:19:23 +01:00
} else {
# no data about UTI's found
uti <- FALSE
}
}
2022-03-02 15:38:55 +01:00
2020-02-20 13:19:23 +01:00
i <- 0
2021-12-05 23:11:10 +01:00
if ( tryCatch ( length ( list ( ... ) ) > 0 , error = function ( e ) TRUE ) ) {
sel <- colnames ( pm_select ( x , ... ) )
} else {
sel <- colnames ( x )
}
2020-10-20 21:00:57 +02:00
if ( ! is.null ( col_mo ) ) {
sel <- sel [sel != col_mo ]
}
2022-03-02 15:38:55 +01:00
2020-12-28 22:24:33 +01:00
ab_cols <- colnames ( x ) [vapply ( FUN.VALUE = logical ( 1 ) , x , function ( y ) {
2020-02-20 13:19:23 +01:00
i <<- i + 1
check <- is.mic ( y ) | is.disk ( y )
ab <- colnames ( x ) [i ]
2020-10-20 21:00:57 +02:00
if ( ! is.null ( col_mo ) && ab == col_mo ) {
return ( FALSE )
}
if ( ! is.null ( col_uti ) && ab == col_uti ) {
return ( FALSE )
}
2020-09-24 12:38:13 +02:00
if ( length ( sel ) == 0 || ( length ( sel ) > 0 && ab %in% sel ) ) {
ab_coerced <- suppressWarnings ( as.ab ( ab ) )
2020-09-25 14:44:50 +02:00
if ( is.na ( ab_coerced ) || ( length ( sel ) > 0 & ! ab %in% sel ) ) {
2020-09-24 12:38:13 +02:00
# not even a valid AB code
return ( FALSE )
} else {
return ( TRUE )
}
2020-02-20 13:19:23 +01:00
} else {
2020-09-24 12:38:13 +02:00
return ( FALSE )
2020-02-20 13:19:23 +01:00
}
} ) ]
2022-03-02 15:38:55 +01:00
2020-06-22 11:18:40 +02:00
stop_if ( length ( ab_cols ) == 0 ,
2020-09-24 12:38:13 +02:00
" no columns with MIC values, disk zones or antibiotic column names found in this data set. Use as.mic() or as.disk() to transform antimicrobial columns." )
2020-02-20 13:19:23 +01:00
# set type per column
types <- character ( length ( ab_cols ) )
2020-12-28 22:24:33 +01:00
types [vapply ( FUN.VALUE = logical ( 1 ) , x.bak [ , ab_cols , drop = FALSE ] , is.disk ) ] <- " disk"
types [vapply ( FUN.VALUE = logical ( 1 ) , x.bak [ , ab_cols , drop = FALSE ] , is.mic ) ] <- " mic"
types [types == " " & vapply ( FUN.VALUE = logical ( 1 ) , x [ , ab_cols , drop = FALSE ] , all_valid_disks ) ] <- " disk"
types [types == " " & vapply ( FUN.VALUE = logical ( 1 ) , x [ , ab_cols , drop = FALSE ] , all_valid_mics ) ] <- " mic"
types [types == " " & ! vapply ( FUN.VALUE = logical ( 1 ) , x.bak [ , ab_cols , drop = FALSE ] , is.rsi ) ] <- " rsi"
2020-09-29 23:35:46 +02:00
if ( any ( types %in% c ( " mic" , " disk" ) , na.rm = TRUE ) ) {
2020-10-20 21:00:57 +02:00
# now we need an mo column
stop_if ( is.null ( col_mo ) , " `col_mo` must be set" )
# if not null, we already found it, now find again so a message will show
if ( is.null ( col_mo.bak ) ) {
2020-09-29 23:35:46 +02:00
col_mo <- search_type_in_df ( x = x , type = " mo" )
}
2021-08-16 21:54:34 +02:00
x_mo <- as.mo ( x [ , col_mo , drop = TRUE ] )
2020-09-29 23:35:46 +02:00
}
2022-03-02 15:38:55 +01:00
2020-02-20 13:19:23 +01:00
for ( i in seq_len ( length ( ab_cols ) ) ) {
if ( types [i ] == " mic" ) {
2020-11-11 16:49:27 +01:00
x [ , ab_cols [i ] ] <- as.rsi ( x = x %pm>%
pm_pull ( ab_cols [i ] ) %pm>%
as.character ( ) %pm>%
as.mic ( ) ,
mo = x_mo ,
ab = ab_cols [i ] ,
guideline = guideline ,
uti = uti ,
conserve_capped_values = conserve_capped_values ,
add_intrinsic_resistance = add_intrinsic_resistance ,
2020-12-22 00:51:17 +01:00
reference_data = reference_data ,
is_data.frame = TRUE )
2020-02-20 13:19:23 +01:00
} else if ( types [i ] == " disk" ) {
2020-11-11 16:49:27 +01:00
x [ , ab_cols [i ] ] <- as.rsi ( x = x %pm>%
pm_pull ( ab_cols [i ] ) %pm>%
as.character ( ) %pm>%
as.disk ( ) ,
mo = x_mo ,
ab = ab_cols [i ] ,
guideline = guideline ,
uti = uti ,
add_intrinsic_resistance = add_intrinsic_resistance ,
2020-12-22 00:51:17 +01:00
reference_data = reference_data ,
is_data.frame = TRUE )
2020-09-24 12:38:13 +02:00
} else if ( types [i ] == " rsi" ) {
2020-12-22 00:51:17 +01:00
show_message <- FALSE
2020-10-20 21:00:57 +02:00
ab <- ab_cols [i ]
ab_coerced <- suppressWarnings ( as.ab ( ab ) )
2021-08-16 21:54:34 +02:00
if ( ! all ( x [ , ab_cols [i ] , drop = TRUE ] %in% c ( " R" , " S" , " I" , NA ) , na.rm = TRUE ) ) {
2020-12-22 00:51:17 +01:00
show_message <- TRUE
2020-11-23 21:50:27 +01:00
# only print message if values are not already clean
2020-12-22 00:51:17 +01:00
message_ ( " => Cleaning values in column '" , font_bold ( ab ) , " ' (" ,
2021-08-16 21:54:34 +02:00
ifelse ( ab_coerced != toupper ( ab ) , paste0 ( ab_coerced , " , " ) , " " ) ,
2020-12-22 00:51:17 +01:00
ab_name ( ab_coerced , tolower = TRUE ) , " )... " ,
appendLF = FALSE ,
as_note = FALSE )
} else if ( ! is.rsi ( x.bak [ , ab_cols [i ] , drop = TRUE ] ) ) {
show_message <- TRUE
# only print message if class not already set
message_ ( " => Assigning class <rsi> to already clean column '" , font_bold ( ab ) , " ' (" ,
2021-08-16 21:54:34 +02:00
ifelse ( ab_coerced != toupper ( ab ) , paste0 ( ab_coerced , " , " ) , " " ) ,
2020-11-23 21:50:27 +01:00
ab_name ( ab_coerced , tolower = TRUE ) , " )... " ,
appendLF = FALSE ,
as_note = FALSE )
}
x [ , ab_cols [i ] ] <- as.rsi.default ( x = as.character ( x [ , ab_cols [i ] , drop = TRUE ] ) )
2020-12-22 00:51:17 +01:00
if ( show_message == TRUE ) {
2020-11-23 21:50:27 +01:00
message_ ( " OK." , add_fn = list ( font_green , font_bold ) , as_note = FALSE )
}
2020-02-20 13:19:23 +01:00
}
}
x
2019-05-10 16:44:59 +02:00
}
2020-11-11 16:49:27 +01:00
get_guideline <- function ( guideline , reference_data ) {
2020-11-11 18:54:03 +01:00
if ( ! identical ( reference_data , AMR :: rsi_translation ) ) {
return ( guideline )
}
2019-05-13 10:10:16 +02:00
guideline_param <- toupper ( guideline )
if ( guideline_param %in% c ( " CLSI" , " EUCAST" ) ) {
2020-11-11 16:49:27 +01:00
guideline_param <- rev ( sort ( subset ( reference_data , guideline %like% guideline_param ) $ guideline ) ) [1L ]
2019-05-10 16:44:59 +02:00
}
2021-04-23 09:59:36 +02:00
if ( guideline_param %unlike% " " ) {
2020-02-21 13:13:34 +01:00
# like 'EUCAST2020', should be 'EUCAST 2020'
guideline_param <- gsub ( " ([a-z]+)([0-9]+)" , " \\1 \\2" , guideline_param , ignore.case = TRUE )
}
2019-10-23 14:48:25 +02:00
2020-11-11 16:49:27 +01:00
stop_ifnot ( guideline_param %in% reference_data $ guideline ,
2020-06-22 11:18:40 +02:00
" invalid guideline: '" , guideline ,
2021-02-04 16:48:16 +01:00
" '.\nValid guidelines are: " , vector_and ( reference_data $ guideline , quotes = TRUE , reverse = TRUE ) , call = FALSE )
2019-10-23 14:48:25 +02:00
guideline_param
}
2022-03-02 15:38:55 +01:00
as_rsi_method <- function ( method_short = " mic" ,
method_long = " MIC values" ,
x = x ,
mo = NULL ,
ab = deparse ( substitute ( x ) ) ,
guideline = " EUCAST" ,
uti = FALSE ,
conserve_capped_values = FALSE ,
add_intrinsic_resistance = FALSE ,
reference_data = AMR :: rsi_translation ,
... ) {
meet_criteria ( x )
meet_criteria ( mo , allow_class = c ( " mo" , " character" ) , allow_NULL = TRUE )
meet_criteria ( ab , allow_class = c ( " ab" , " character" ) )
meet_criteria ( guideline , allow_class = " character" , has_length = 1 )
meet_criteria ( uti , allow_class = " logical" , has_length = c ( 1 , length ( x ) ) )
meet_criteria ( conserve_capped_values , allow_class = " logical" , has_length = 1 )
meet_criteria ( add_intrinsic_resistance , allow_class = " logical" , has_length = 1 )
meet_criteria ( reference_data , allow_class = " data.frame" )
check_reference_data ( reference_data )
# for dplyr's across()
cur_column_dplyr <- import_fn ( " cur_column" , " dplyr" , error_on_fail = FALSE )
if ( ! is.null ( cur_column_dplyr ) && tryCatch ( is.data.frame ( get_current_data ( " ab" , call = 0 ) ) , error = function ( e ) FALSE ) ) {
# try to get current column, which will only be available when in across()
ab <- tryCatch ( cur_column_dplyr ( ) ,
error = function ( e ) ab )
}
# for auto-determining mo
mo_var_found <- " "
if ( is.null ( mo ) ) {
tryCatch ( {
df <- get_current_data ( arg_name = " mo" , call = -3 ) # will return an error if not found
mo <- NULL
try ( {
mo <- suppressMessages ( search_type_in_df ( df , " mo" ) )
} , silent = TRUE )
if ( ! is.null ( df ) && ! is.null ( mo ) && is.data.frame ( df ) ) {
mo_var_found <- paste0 ( " based on column '" , font_bold ( mo ) , " '" )
mo <- df [ , mo , drop = TRUE ]
}
} , error = function ( e ) {
mo <- NULL
} )
}
if ( is.null ( mo ) ) {
stop_ ( " No information was supplied about the microorganisms (missing argument `mo` and no column of class <mo> found). See ?as.rsi.\n\n" ,
" To transform certain columns with e.g. mutate(), use `data %>% mutate(across(..., as.rsi, mo = x))`, where x is your column with microorganisms.\n" ,
" To tranform all " , method_long , " in a data set, use `data %>% as.rsi()` or `data %>% mutate(across(where(is." , method_short , " ), as.rsi))`." , call = FALSE )
}
if ( length ( ab ) == 1 && ab %like% paste0 ( " as." , method_short ) ) {
stop_ ( ' No unambiguous name was supplied about the antibiotic (argument `ab`). See ?as.rsi.' , call = FALSE )
}
ab_coerced <- suppressWarnings ( as.ab ( ab ) )
mo_coerced <- suppressWarnings ( as.mo ( mo ) )
guideline_coerced <- get_guideline ( guideline , reference_data )
if ( is.na ( ab_coerced ) ) {
message_ ( " Returning NAs for unknown drug: '" , font_bold ( ab ) ,
" '. Rename this column to a drug name or code, and check the output with `as.ab()`." ,
add_fn = font_red ,
as_note = FALSE )
return ( as.rsi ( rep ( NA , length ( x ) ) ) )
}
if ( length ( mo_coerced ) == 1 ) {
mo_coerced <- rep ( mo_coerced , length ( x ) )
}
if ( length ( uti ) == 1 ) {
uti <- rep ( uti , length ( x ) )
}
agent_formatted <- paste0 ( " '" , font_bold ( ab ) , " '" )
agent_name <- ab_name ( ab_coerced , tolower = TRUE , language = NULL )
if ( generalise_antibiotic_name ( ab ) != generalise_antibiotic_name ( agent_name ) ) {
agent_formatted <- paste0 ( agent_formatted , " (" , ab_coerced , " , " , agent_name , " )" )
}
message_ ( " => Interpreting " , method_long , " of " , ifelse ( isTRUE ( list ( ... ) $ is_data.frame ) , " column " , " " ) ,
agent_formatted ,
mo_var_found ,
" according to " , ifelse ( identical ( reference_data , AMR :: rsi_translation ) ,
font_bold ( guideline_coerced ) ,
" manually defined 'reference_data'" ) ,
" ... " ,
appendLF = FALSE ,
as_note = FALSE )
result <- exec_as.rsi ( method = method_short ,
x = x ,
mo = mo_coerced ,
ab = ab_coerced ,
guideline = guideline_coerced ,
uti = uti ,
conserve_capped_values = conserve_capped_values ,
add_intrinsic_resistance = add_intrinsic_resistance ,
reference_data = reference_data ) # exec_as.rsi will return message 'OK'
result
}
2020-08-16 21:38:42 +02:00
exec_as.rsi <- function ( method ,
x ,
mo ,
ab ,
guideline ,
uti ,
conserve_capped_values ,
2020-11-11 18:54:03 +01:00
add_intrinsic_resistance ,
reference_data ) {
2020-09-18 16:05:53 +02:00
metadata_mo <- get_mo_failures_uncertainties_renamed ( )
2020-11-11 16:49:27 +01:00
x_bak <- data.frame ( x_mo = paste0 ( x , mo ) , stringsAsFactors = FALSE )
2022-03-10 19:33:25 +01:00
df <- unique ( data.frame ( x , mo , x_mo = paste0 ( x , mo ) , stringsAsFactors = FALSE ) )
2020-08-15 12:54:47 +02:00
x <- df $ x
mo <- df $ mo
2019-10-23 14:48:25 +02:00
if ( method == " mic" ) {
2020-02-20 13:19:23 +01:00
x <- as.mic ( x ) # when as.rsi.mic is called directly
2019-10-23 14:48:25 +02:00
} else if ( method == " disk" ) {
2020-02-20 13:19:23 +01:00
x <- as.disk ( x ) # when as.rsi.disk is called directly
2019-10-23 14:48:25 +02:00
}
2022-03-02 15:38:55 +01:00
rise_warning <- FALSE
2020-02-20 13:19:23 +01:00
method_param <- toupper ( method )
2019-10-23 14:48:25 +02:00
2021-03-08 00:13:13 +01:00
genera <- mo_genus ( mo , language = NULL )
mo_genus <- as.mo ( genera , language = NULL )
mo_family <- as.mo ( mo_family ( mo , language = NULL ) )
mo_order <- as.mo ( mo_order ( mo , language = NULL ) )
2020-09-25 14:44:50 +02:00
if ( any ( genera == " Staphylococcus" , na.rm = TRUE ) ) {
mo_becker <- as.mo ( mo , Becker = TRUE )
} else {
mo_becker <- mo
}
if ( any ( genera == " Streptococcus" , na.rm = TRUE ) ) {
mo_lancefield <- as.mo ( mo , Lancefield = TRUE )
} else {
mo_lancefield <- mo
}
2020-04-29 14:33:44 +02:00
mo_other <- as.mo ( rep ( " UNKNOWN" , length ( mo ) ) )
2019-10-23 14:48:25 +02:00
2020-11-11 16:49:27 +01:00
guideline_coerced <- get_guideline ( guideline , reference_data )
2019-10-23 14:48:25 +02:00
if ( guideline_coerced != guideline ) {
2021-12-11 13:41:31 +01:00
if ( message_not_thrown_before ( " as.rsi" , " msg1" ) ) {
2020-12-24 23:29:10 +01:00
message_ ( " Using guideline " , font_bold ( guideline_coerced ) , " as input for `guideline`." )
}
2019-10-23 14:48:25 +02:00
}
2020-02-17 14:38:01 +01:00
2019-05-10 16:44:59 +02:00
new_rsi <- rep ( NA_character_ , length ( x ) )
2020-02-20 13:19:23 +01:00
ab_param <- ab
2020-11-11 18:54:03 +01:00
if ( identical ( reference_data , AMR :: rsi_translation ) ) {
trans <- reference_data %pm>%
subset ( guideline == guideline_coerced & method == method_param & ab == ab_param )
} else {
trans <- reference_data %pm>%
subset ( method == method_param & ab == ab_param )
}
2020-05-16 13:05:47 +02:00
trans $ lookup <- paste ( trans $ mo , trans $ ab )
2020-02-17 14:38:01 +01:00
2019-05-10 16:44:59 +02:00
lookup_mo <- paste ( mo , ab )
lookup_genus <- paste ( mo_genus , ab )
lookup_family <- paste ( mo_family , ab )
lookup_order <- paste ( mo_order , ab )
lookup_becker <- paste ( mo_becker , ab )
lookup_lancefield <- paste ( mo_lancefield , ab )
2020-02-14 19:54:13 +01:00
lookup_other <- paste ( mo_other , ab )
2019-10-23 14:48:25 +02:00
2020-12-07 16:06:42 +01:00
any_is_intrinsic_resistant <- FALSE
2019-10-11 17:21:02 +02:00
for ( i in seq_len ( length ( x ) ) ) {
2020-12-07 16:06:42 +01:00
is_intrinsic_r <- paste ( mo [i ] , ab ) %in% INTRINSIC_R
any_is_intrinsic_resistant <- any_is_intrinsic_resistant | is_intrinsic_r
if ( isTRUE ( add_intrinsic_resistance ) & is_intrinsic_r ) {
2021-04-23 09:59:36 +02:00
if ( guideline_coerced %unlike% " EUCAST" ) {
2021-12-11 13:41:31 +01:00
if ( message_not_thrown_before ( " as.rsi" , " msg2" ) ) {
2022-03-02 15:38:55 +01:00
warning_ ( " in `as.rsi()`: using 'add_intrinsic_resistance' is only useful when using EUCAST guidelines, since the rules for intrinsic resistance are based on EUCAST." )
2020-12-24 23:29:10 +01:00
}
2020-08-16 21:38:42 +02:00
} else {
2020-12-07 16:06:42 +01:00
new_rsi [i ] <- " R"
next
2020-08-16 21:38:42 +02:00
}
}
2020-09-18 16:05:53 +02:00
get_record <- trans %pm>%
2022-03-02 15:38:55 +01:00
# no subsetting to UTI here
2020-05-16 13:05:47 +02:00
subset ( lookup %in% c ( lookup_mo [i ] ,
2019-05-10 16:44:59 +02:00
lookup_genus [i ] ,
lookup_family [i ] ,
lookup_order [i ] ,
lookup_becker [i ] ,
2020-02-14 19:54:13 +01:00
lookup_lancefield [i ] ,
2020-02-20 13:19:23 +01:00
lookup_other [i ] ) )
2022-03-02 15:38:55 +01:00
if ( any ( get_record $ uti == TRUE , na.rm = TRUE ) && message_not_thrown_before ( " as.rsi" , " msg3" , ab ) ) {
warning_ ( " in `as.rsi()`: interpretation of " , font_bold ( ab_name ( ab , tolower = TRUE ) ) , " is only available for (uncomplicated) urinary tract infections (UTI) for some microorganisms. Use argument `uti` to set which isolates are from urine. See ?as.rsi." )
rise_warning <- TRUE
}
2020-02-20 13:19:23 +01:00
if ( isTRUE ( uti [i ] ) ) {
2020-09-18 16:05:53 +02:00
get_record <- get_record %pm>%
2020-02-20 13:19:23 +01:00
# be as specific as possible (i.e. prefer species over genus):
2020-09-18 16:05:53 +02:00
# pm_desc(uti) = TRUE on top and FALSE on bottom
2021-12-13 10:18:28 +01:00
pm_arrange ( pm_desc ( uti ) , rank_index ) # 'uti' is a column in data set 'rsi_translation'
2020-02-20 13:19:23 +01:00
} else {
2020-09-18 16:05:53 +02:00
get_record <- get_record %pm>%
pm_filter ( uti == FALSE ) %pm>% # 'uti' is a column in rsi_translation
2021-12-13 10:18:28 +01:00
pm_arrange ( rank_index )
2020-02-20 13:19:23 +01:00
}
2022-03-02 15:38:55 +01:00
2021-03-08 00:13:13 +01:00
get_record <- get_record [1L , , drop = FALSE ]
2019-05-10 16:44:59 +02:00
if ( NROW ( get_record ) > 0 ) {
2021-03-08 00:13:13 +01:00
if ( is.na ( x [i ] ) | ( is.na ( get_record $ breakpoint_S ) & is.na ( get_record $ breakpoint_R ) ) ) {
2019-10-23 14:48:25 +02:00
new_rsi [i ] <- NA_character_
} else if ( method == " mic" ) {
2021-03-08 00:13:13 +01:00
new_rsi [i ] <- quick_case_when ( isTRUE ( conserve_capped_values ) & x [i ] %like% " ^<[0-9]" ~ " S" ,
2021-03-07 16:15:43 +01:00
isTRUE ( conserve_capped_values ) & x [i ] %like% " ^>[0-9]" ~ " R" ,
2021-12-13 10:18:28 +01:00
# these basically call `<=.mic()` and `>=.mic()`:
2021-03-08 00:13:13 +01:00
x [i ] <= get_record $ breakpoint_S ~ " S" ,
2021-12-13 10:18:28 +01:00
x [i ] >= get_record $ breakpoint_R ~ " R" ,
2020-09-18 16:05:53 +02:00
# return "I" when not match the bottom or top
! is.na ( get_record $ breakpoint_S ) & ! is.na ( get_record $ breakpoint_R ) ~ " I" ,
# and NA otherwise
TRUE ~ NA_character_ )
2019-05-10 16:44:59 +02:00
} else if ( method == " disk" ) {
2021-03-08 00:13:13 +01:00
new_rsi [i ] <- quick_case_when ( isTRUE ( as.double ( x [i ] ) >= as.double ( get_record $ breakpoint_S ) ) ~ " S" ,
2021-12-13 10:18:28 +01:00
isTRUE ( as.double ( x [i ] ) <= as.double ( get_record $ breakpoint_R ) ) ~ " R" ,
2020-09-18 16:05:53 +02:00
# return "I" when not match the bottom or top
! is.na ( get_record $ breakpoint_S ) & ! is.na ( get_record $ breakpoint_R ) ~ " I" ,
# and NA otherwise
TRUE ~ NA_character_ )
2019-05-10 16:44:59 +02:00
}
}
}
2020-08-15 12:54:47 +02:00
2020-12-07 16:06:42 +01:00
if ( any_is_intrinsic_resistant & guideline_coerced %like% " EUCAST" & ! isTRUE ( add_intrinsic_resistance ) ) {
# found some intrinsic resistance, but was not applied
2022-03-02 15:38:55 +01:00
if ( message_not_thrown_before ( " as.rsi" , " msg4" ) ) {
warning_ ( " in `as.rsi()`: found intrinsic resistance in some bug/drug combinations, although it was not applied.\nUse `as.rsi(..., add_intrinsic_resistance = TRUE)` to apply it." )
2020-12-24 23:29:10 +01:00
}
2022-03-02 15:38:55 +01:00
rise_warning <- TRUE
2020-12-07 16:06:42 +01:00
}
2020-09-18 16:05:53 +02:00
new_rsi <- x_bak %pm>%
2022-03-10 19:33:25 +01:00
pm_left_join ( data.frame ( x_mo = paste0 ( x , mo ) , new_rsi ,
2020-11-11 16:49:27 +01:00
stringsAsFactors = FALSE ) ,
by = " x_mo" ) %pm>%
2020-09-18 16:05:53 +02:00
pm_pull ( new_rsi )
2020-08-15 12:54:47 +02:00
2022-03-02 15:38:55 +01:00
if ( isTRUE ( rise_warning ) ) {
message_ ( " WARNING." , add_fn = list ( font_yellow , font_bold ) , as_note = FALSE )
} else {
2020-11-11 16:49:27 +01:00
message_ ( " OK." , add_fn = list ( font_green , font_bold ) , as_note = FALSE )
2020-02-20 13:19:23 +01:00
}
2020-08-15 12:54:47 +02:00
2020-09-18 16:05:53 +02:00
load_mo_failures_uncertainties_renamed ( metadata_mo )
2020-11-16 16:57:55 +01:00
set_clean_class ( factor ( new_rsi , levels = c ( " S" , " I" , " R" ) , ordered = TRUE ) ,
new_class = c ( " rsi" , " ordered" , " factor" ) )
2019-05-10 16:44:59 +02:00
}
2020-08-28 21:55:47 +02:00
# will be exported using s3_register() in R/zzz.R
2020-08-26 11:33:54 +02:00
pillar_shaft.rsi <- function ( x , ... ) {
out <- trimws ( format ( x ) )
2021-01-28 16:09:30 +01:00
if ( has_colour ( ) ) {
# colours will anyway not work when has_colour() == FALSE,
# but then the indentation should also not be applied
out [is.na ( x ) ] <- font_grey ( " NA" )
out [x == " R" ] <- font_rsi_R_bg ( font_black ( " R " ) )
out [x == " S" ] <- font_rsi_S_bg ( font_black ( " S " ) )
out [x == " I" ] <- font_rsi_I_bg ( font_black ( " I " ) )
}
2020-08-28 21:55:47 +02:00
create_pillar_column ( out , align = " left" , width = 5 )
2020-08-26 11:33:54 +02:00
}
2020-08-28 21:55:47 +02:00
# will be exported using s3_register() in R/zzz.R
2020-08-26 11:33:54 +02:00
type_sum.rsi <- function ( x , ... ) {
" rsi"
}
2020-08-28 21:55:47 +02:00
# will be exported using s3_register() in R/zzz.R
freq.rsi <- function ( x , ... ) {
x_name <- deparse ( substitute ( x ) )
x_name <- gsub ( " .*[$]" , " " , x_name )
2020-10-19 17:09:19 +02:00
if ( x_name %in% c ( " x" , " ." ) ) {
# try again going through system calls
2020-12-28 22:24:33 +01:00
x_name <- stats :: na.omit ( vapply ( FUN.VALUE = character ( 1 ) ,
sys.calls ( ) ,
2020-10-20 21:00:57 +02:00
function ( call ) {
call_txt <- as.character ( call )
ifelse ( call_txt [1 ] %like% " freq$" , call_txt [length ( call_txt ) ] , character ( 0 ) )
} ) ) [1L ]
2020-10-19 17:09:19 +02:00
}
2020-08-28 21:55:47 +02:00
ab <- suppressMessages ( suppressWarnings ( as.ab ( x_name ) ) )
digits <- list ( ... ) $ digits
if ( is.null ( digits ) ) {
digits <- 2
}
if ( ! is.na ( ab ) ) {
2020-12-17 16:22:25 +01:00
cleaner :: freq.default ( x = x , ... ,
.add_header = list (
2021-08-17 14:34:11 +02:00
Drug = paste0 ( ab_name ( ab , language = NULL ) , " (" , ab , " , " , paste ( ab_atc ( ab ) , collapse = " /" ) , " )" ) ,
2020-12-17 16:22:25 +01:00
`Drug group` = ab_group ( ab , language = NULL ) ,
2022-03-10 19:33:25 +01:00
`%SI` = trimws ( percentage ( susceptibility ( x , minimum = 0 , as_percent = FALSE ) ,
digits = digits ) ) ) )
2020-08-28 21:55:47 +02:00
} else {
2020-12-17 16:22:25 +01:00
cleaner :: freq.default ( x = x , ... ,
.add_header = list (
2022-03-10 19:33:25 +01:00
`%SI` = trimws ( percentage ( susceptibility ( x , minimum = 0 , as_percent = FALSE ) ,
digits = digits ) ) ) )
2020-08-28 21:55:47 +02:00
}
}
2020-09-28 01:08:55 +02:00
# will be exported using s3_register() in R/zzz.R
get_skimmers.rsi <- function ( column ) {
2020-09-28 11:00:59 +02:00
# get the variable name 'skim_variable'
name_call <- function ( .data ) {
2020-09-28 01:08:55 +02:00
calls <- sys.calls ( )
2021-05-06 15:17:11 +02:00
frms <- sys.frames ( )
2020-09-28 11:00:59 +02:00
calls_txt <- vapply ( calls , function ( x ) paste ( deparse ( x ) , collapse = " " ) , FUN.VALUE = character ( 1 ) )
if ( any ( calls_txt %like% " skim_variable" , na.rm = TRUE ) ) {
ind <- which ( calls_txt %like% " skim_variable" ) [1L ]
2021-05-06 15:17:11 +02:00
vars <- tryCatch ( eval ( parse ( text = " .data$skim_variable$rsi" ) , envir = frms [ [ind ] ] ) ,
2020-09-28 11:00:59 +02:00
error = function ( e ) NULL )
2021-05-06 15:17:11 +02:00
tryCatch ( ab_name ( as.character ( calls [ [length ( calls ) ] ] [ [2 ] ] ) , language = NULL ) ,
error = function ( e ) NA_character_ )
2020-09-28 11:00:59 +02:00
} else {
2020-09-28 01:08:55 +02:00
NA_character_
}
}
2020-12-17 16:22:25 +01:00
skimr :: sfl (
2020-09-28 01:08:55 +02:00
skim_type = " rsi" ,
2020-09-28 11:00:59 +02:00
ab_name = name_call ,
2020-09-28 01:08:55 +02:00
count_R = count_R ,
count_S = count_susceptible ,
count_I = count_I ,
prop_R = ~ proportion_R ( ., minimum = 0 ) ,
prop_S = ~ susceptibility ( ., minimum = 0 ) ,
prop_I = ~ proportion_I ( ., minimum = 0 )
)
}
2020-05-28 16:48:55 +02:00
#' @method print rsi
2018-08-25 22:01:14 +02:00
#' @export
#' @noRd
print.rsi <- function ( x , ... ) {
2020-05-27 16:37:49 +02:00
cat ( " Class <rsi>\n" )
2018-08-25 22:01:14 +02:00
print ( as.character ( x ) , quote = FALSE )
}
2020-05-28 16:48:55 +02:00
#' @method droplevels rsi
2018-12-29 22:24:19 +01:00
#' @export
#' @noRd
2020-12-17 16:22:25 +01:00
droplevels.rsi <- function ( x , exclude = if ( any ( is.na ( levels ( x ) ) ) ) NULL else NA , ... ) {
2018-12-29 22:24:19 +01:00
x <- droplevels.factor ( x , exclude = exclude , ... )
2019-10-11 17:21:02 +02:00
class ( x ) <- c ( " rsi" , " ordered" , " factor" )
2018-12-29 22:24:19 +01:00
x
}
2020-05-28 16:48:55 +02:00
#' @method summary rsi
2018-08-25 22:01:14 +02:00
#' @export
#' @noRd
summary.rsi <- function ( object , ... ) {
x <- object
2020-06-22 11:18:40 +02:00
n <- sum ( ! is.na ( x ) )
S <- sum ( x == " S" , na.rm = TRUE )
I <- sum ( x == " I" , na.rm = TRUE )
R <- sum ( x == " R" , na.rm = TRUE )
2020-08-10 11:44:58 +02:00
pad <- function ( x ) {
if ( x == " 0%" ) {
x <- " 0.0%"
}
if ( nchar ( x ) < 5 ) {
x <- paste0 ( rep ( " " , 5 - nchar ( x ) ) , x )
}
x
}
2020-07-22 10:24:23 +02:00
value <- c (
2019-10-11 17:21:02 +02:00
" Class" = " rsi" ,
2020-08-10 11:44:58 +02:00
" %R" = paste0 ( pad ( percentage ( R / n , digits = 1 ) ) , " (n=" , R , " )" ) ,
" %SI" = paste0 ( pad ( percentage ( ( S + I ) / n , digits = 1 ) ) , " (n=" , S + I , " )" ) ,
" - %S" = paste0 ( pad ( percentage ( S / n , digits = 1 ) ) , " (n=" , S , " )" ) ,
" - %I" = paste0 ( pad ( percentage ( I / n , digits = 1 ) ) , " (n=" , I , " )" )
2018-08-10 15:01:05 +02:00
)
2020-07-22 10:24:23 +02:00
class ( value ) <- c ( " summaryDefault" , " table" )
value
2018-08-25 22:01:14 +02:00
}
2018-08-10 15:01:05 +02:00
2020-05-28 16:48:55 +02:00
#' @method [<- rsi
2020-04-13 21:09:56 +02:00
#' @export
#' @noRd
" [<-.rsi" <- function ( i , j , ... , value ) {
value <- as.rsi ( value )
y <- NextMethod ( )
attributes ( y ) <- attributes ( i )
y
}
2020-05-28 16:48:55 +02:00
#' @method [[<- rsi
2020-04-13 21:09:56 +02:00
#' @export
#' @noRd
" [[<-.rsi" <- function ( i , j , ... , value ) {
value <- as.rsi ( value )
y <- NextMethod ( )
attributes ( y ) <- attributes ( i )
y
}
2020-05-28 16:48:55 +02:00
#' @method c rsi
2020-04-13 21:09:56 +02:00
#' @export
#' @noRd
2021-04-26 23:57:37 +02:00
c.rsi <- function ( ... ) {
as.rsi ( unlist ( lapply ( list ( ... ) , as.character ) ) )
2020-04-13 21:09:56 +02:00
}
2020-09-25 14:44:50 +02:00
#' @method unique rsi
#' @export
#' @noRd
unique.rsi <- function ( x , incomparables = FALSE , ... ) {
y <- NextMethod ( )
attributes ( y ) <- attributes ( x )
y
}
2020-11-11 16:49:27 +01:00
2021-07-06 16:35:14 +02:00
#' @method rep rsi
#' @export
#' @noRd
rep.rsi <- function ( x , ... ) {
y <- NextMethod ( )
attributes ( y ) <- attributes ( x )
y
}
2020-11-11 16:49:27 +01:00
check_reference_data <- function ( reference_data ) {
if ( ! identical ( reference_data , AMR :: rsi_translation ) ) {
2020-12-28 22:24:33 +01:00
class_rsi <- vapply ( FUN.VALUE = character ( 1 ) , rsi_translation , function ( x ) paste0 ( " <" , class ( x ) , " >" , collapse = " and " ) )
class_ref <- vapply ( FUN.VALUE = character ( 1 ) , reference_data , function ( x ) paste0 ( " <" , class ( x ) , " >" , collapse = " and " ) )
2020-11-11 16:49:27 +01:00
if ( ! all ( names ( class_rsi ) == names ( class_ref ) ) ) {
2020-12-03 16:59:04 +01:00
stop_ ( " `reference_data` must have the same column names as the 'rsi_translation' data set." , call = -2 )
2020-11-11 16:49:27 +01:00
}
if ( ! all ( class_rsi == class_ref ) ) {
class_rsi [class_rsi != class_ref ] [1 ]
2020-12-03 16:59:04 +01:00
stop_ ( " `reference_data` must be the same structure as the 'rsi_translation' data set. Column '" , names ( class_ref [class_rsi != class_ref ] [1 ] ) , " ' is of class " , class_ref [class_rsi != class_ref ] [1 ] , " , but should be of class " , class_rsi [class_rsi != class_ref ] [1 ] , " ." , call = -2 )
2020-11-11 16:49:27 +01:00
}
}
}