2019-08-25 22:53:22 +02:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
2020-01-05 17:22:09 +01:00
# (c) 2018-2020 Berends MS, Luz CF et al. #
2019-08-25 22:53:22 +02: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. #
2019-08-25 22:53:22 +02:00
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
#' Determine bug-drug combinations
#'
2019-11-28 22:32:17 +01:00
#' Determine antimicrobial resistance (AMR) of all bug-drug combinations in your data set where at least 30 (default) isolates are available per species. Use [format()] on the result to prettify it to a publicable/printable format, see Examples.
2020-01-05 17:22:09 +01:00
#' @inheritSection lifecycle Stable lifecycle
2019-08-25 22:53:22 +02:00
#' @inheritParams eucast_rules
2019-08-27 19:15:04 +02:00
#' @param combine_IR logical to indicate whether values R and I should be summed
2019-08-27 22:41:09 +02:00
#' @param add_ab_group logical to indicate where the group of the antimicrobials must be included as a first column
2019-11-28 22:32:17 +01:00
#' @param remove_intrinsic_resistant logical to indicate that rows with 100% resistance for all tested antimicrobials must be removed from the table
#' @param FUN the function to call on the `mo` column to transform the microorganism IDs, defaults to [mo_shortname()]
#' @param translate_ab a character of length 1 containing column names of the [antibiotics] data set
#' @param ... arguments passed on to `FUN`
2019-08-25 22:53:22 +02:00
#' @inheritParams rsi_df
2019-09-23 13:53:50 +02:00
#' @inheritParams base::formatC
2019-11-11 10:46:39 +01:00
#' @importFrom tidyr pivot_longer
2019-11-28 22:32:17 +01:00
#' @details The function [format()] calculates the resistance per bug-drug combination. Use `combine_IR = FALSE` (default) to test R vs. S+I and `combine_IR = TRUE` to test R+I vs. S.
2019-09-23 13:53:50 +02:00
#'
2019-11-28 22:32:17 +01:00
#' The language of the output can be overwritten with `options(AMR_locale)`, please see [translate].
2019-08-25 22:53:22 +02:00
#' @export
2019-08-27 22:41:09 +02:00
#' @rdname bug_drug_combinations
2019-11-28 22:32:17 +01:00
#' @return The function [bug_drug_combinations()] returns a [`data.frame`] with columns "mo", "ab", "S", "I", "R" and "total".
#' @source \strong{M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition}, 2014, *Clinical and Laboratory Standards Institute (CLSI)*. <https://clsi.org/standards/products/microbiology/documents/m39/>.
2019-08-25 22:53:22 +02:00
#' @inheritSection AMR Read more on our website!
#' @examples
#' \donttest{
2019-08-27 16:45:42 +02:00
#' x <- bug_drug_combinations(example_isolates)
2019-08-25 22:53:22 +02:00
#' x
2019-10-11 17:21:02 +02:00
#' format(x, translate_ab = "name (atc)")
2019-09-23 13:53:50 +02:00
#'
#' # Use FUN to change to transformation of microorganism codes
#' x <- bug_drug_combinations(example_isolates,
#' FUN = mo_gramstain)
#'
#' x <- bug_drug_combinations(example_isolates,
#' FUN = function(x) ifelse(x == "B_ESCHR_COLI",
#' "E. coli",
#' "Others"))
2019-08-25 22:53:22 +02:00
#' }
2019-09-23 13:53:50 +02:00
bug_drug_combinations <- function ( x ,
col_mo = NULL ,
FUN = mo_shortname ,
... ) {
2019-08-25 22:53:22 +02:00
if ( ! is.data.frame ( x ) ) {
stop ( " `x` must be a data frame." , call. = FALSE )
}
# try to find columns based on type
# -- mo
if ( is.null ( col_mo ) ) {
col_mo <- search_type_in_df ( x = x , type = " mo" )
}
if ( is.null ( col_mo ) ) {
stop ( " `col_mo` must be set." , call. = FALSE )
}
2020-05-16 13:05:47 +02:00
select_rsi <- function ( .data ) {
.data [ , c ( col_mo , names ( which ( sapply ( .data , is.rsi ) ) ) ) ]
}
x <- x %>% as.data.frame ( stringsAsFactors = FALSE )
x $ mo <- FUN ( x [ , col_mo , drop = TRUE ] )
x <- x %>%
select_rsi ( ) %>%
2019-11-11 10:46:39 +01:00
pivot_longer ( - mo , names_to = " ab" ) %>%
2019-10-08 22:21:33 +02:00
group_by ( mo , ab ) %>%
summarise ( S = sum ( value == " S" , na.rm = TRUE ) ,
I = sum ( value == " I" , na.rm = TRUE ) ,
R = sum ( value == " R" , na.rm = TRUE ) ) %>%
ungroup ( ) %>%
2019-08-25 22:53:22 +02:00
mutate ( total = S + I + R ) %>%
2019-10-08 22:21:33 +02:00
as.data.frame ( stringsAsFactors = FALSE )
2019-08-27 22:41:09 +02:00
structure ( .Data = x , class = c ( " bug_drug_combinations" , class ( x ) ) )
2019-08-25 22:53:22 +02:00
}
2019-11-11 10:46:39 +01:00
#' @importFrom tidyr pivot_wider
2019-08-27 22:41:09 +02:00
#' @exportMethod format.bug_drug_combinations
2019-08-25 22:53:22 +02:00
#' @export
2019-08-27 22:41:09 +02:00
#' @rdname bug_drug_combinations
2019-09-25 15:43:22 +02:00
format.bug_drug_combinations <- function ( x ,
translate_ab = " name (ab, atc)" ,
language = get_locale ( ) ,
minimum = 30 ,
combine_SI = TRUE ,
combine_IR = FALSE ,
2019-09-23 13:53:50 +02:00
add_ab_group = TRUE ,
2019-09-25 15:43:22 +02:00
remove_intrinsic_resistant = FALSE ,
2019-09-23 13:53:50 +02:00
decimal.mark = getOption ( " OutDec" ) ,
2019-09-23 14:37:24 +02:00
big.mark = ifelse ( decimal.mark == " ," , " ." , " ," ) ,
... ) {
2020-05-16 13:05:47 +02:00
x <- x %>% subset ( total >= minimum )
2019-10-08 22:21:33 +02:00
2019-09-25 15:43:22 +02:00
if ( remove_intrinsic_resistant == TRUE ) {
2020-05-16 13:05:47 +02:00
x <- x %>% subset ( R != total )
2019-09-25 15:43:22 +02:00
}
2019-10-11 17:21:02 +02:00
if ( combine_SI == TRUE | combine_IR == FALSE ) {
2019-08-25 22:53:22 +02:00
x $ isolates <- x $ R
} else {
x $ isolates <- x $ R + x $ I
}
2019-09-25 15:43:22 +02:00
give_ab_name <- function ( ab , format , language ) {
format <- tolower ( format )
ab_txt <- rep ( format , length ( ab ) )
2019-10-11 17:21:02 +02:00
for ( i in seq_len ( length ( ab_txt ) ) ) {
2019-09-25 15:43:22 +02:00
ab_txt [i ] <- gsub ( " ab" , ab [i ] , ab_txt [i ] )
ab_txt [i ] <- gsub ( " cid" , ab_cid ( ab [i ] ) , ab_txt [i ] )
ab_txt [i ] <- gsub ( " group" , ab_group ( ab [i ] , language = language ) , ab_txt [i ] )
ab_txt [i ] <- gsub ( " atc_group1" , ab_atc_group1 ( ab [i ] , language = language ) , ab_txt [i ] )
ab_txt [i ] <- gsub ( " atc_group2" , ab_atc_group2 ( ab [i ] , language = language ) , ab_txt [i ] )
ab_txt [i ] <- gsub ( " atc" , ab_atc ( ab [i ] ) , ab_txt [i ] )
ab_txt [i ] <- gsub ( " name" , ab_name ( ab [i ] , language = language ) , ab_txt [i ] )
ab_txt [i ]
}
ab_txt
}
2019-11-11 10:46:39 +01:00
2020-05-16 13:05:47 +02:00
remove_NAs <- function ( .data ) {
as.data.frame ( sapply ( .data , function ( x ) ifelse ( is.na ( x ) , " " , x ) , simplify = FALSE ) )
}
create_var <- function ( .data , ... ) {
dots <- list ( ... )
for ( i in seq_len ( length ( dots ) ) ) {
.data [ , names ( dots ) [i ] ] <- dots [ [i ] ]
}
.data
}
2019-08-25 22:53:22 +02:00
y <- x %>%
2020-05-16 13:05:47 +02:00
create_var ( ab = as.ab ( x $ ab ) ,
ab_txt = give_ab_name ( ab = x $ ab , format = translate_ab , language = language ) ) %>%
2019-09-25 15:43:22 +02:00
group_by ( ab , ab_txt , mo ) %>%
summarise ( isolates = sum ( isolates , na.rm = TRUE ) ,
total = sum ( total , na.rm = TRUE ) ) %>%
2020-05-16 13:05:47 +02:00
ungroup ( )
y <- y %>%
create_var ( txt = paste0 ( percentage ( y $ isolates / y $ total , decimal.mark = decimal.mark , big.mark = big.mark ) ,
" (" , trimws ( format ( y $ isolates , big.mark = big.mark ) ) , " /" ,
trimws ( format ( y $ total , big.mark = big.mark ) ) , " )" ) ) %>%
2019-11-11 10:46:39 +01:00
select ( ab , ab_txt , mo , txt ) %>%
arrange ( mo ) %>%
pivot_wider ( names_from = mo , values_from = txt ) %>%
2020-05-16 13:05:47 +02:00
remove_NAs ( )
2019-08-25 22:53:22 +02:00
2020-05-16 13:05:47 +02:00
select_ab_vars <- function ( .data ) {
.data [ , c ( " ab_group" , " ab_txt" , colnames ( .data ) [ ! colnames ( .data ) %in% c ( " ab_group" , " ab_txt" , " ab" ) ] ) ]
}
y <- y %>%
create_var ( ab_group = ab_group ( y $ ab , language = language ) ) %>%
select_ab_vars ( ) %>%
arrange ( ab_group , ab_txt )
y <- y %>%
create_var ( ab_group = ifelse ( y $ ab_group != lag ( y $ ab_group ) | is.na ( lag ( y $ ab_group ) ) , y $ ab_group , " " ) )
2019-08-25 22:53:22 +02:00
if ( add_ab_group == FALSE ) {
2019-09-25 15:43:22 +02:00
y <- y %>% select ( - ab_group ) %>% rename ( " Drug" = ab_txt )
2019-09-23 13:53:50 +02:00
colnames ( y ) [1 ] <- translate_AMR ( colnames ( y ) [1 ] , language = get_locale ( ) , only_unknown = FALSE )
} else {
y <- y %>% rename ( " Group" = ab_group ,
2019-09-25 15:43:22 +02:00
" Drug" = ab_txt )
2019-09-23 13:53:50 +02:00
colnames ( y ) [1 : 2 ] <- translate_AMR ( colnames ( y ) [1 : 2 ] , language = get_locale ( ) , only_unknown = FALSE )
2019-08-25 22:53:22 +02:00
}
y
}
2019-08-27 22:41:09 +02:00
#' @exportMethod print.bug_drug_combinations
2019-08-25 22:53:22 +02:00
#' @export
2019-08-27 22:41:09 +02:00
print.bug_drug_combinations <- function ( x , ... ) {
2019-08-25 22:53:22 +02:00
print ( as.data.frame ( x , stringsAsFactors = FALSE ) )
2020-05-16 13:05:47 +02:00
message ( font_blue ( " NOTE: Use 'format()' on this result to get a publicable/printable format." ) )
2019-08-25 22:53:22 +02:00
}