2019-08-25 22:53:22 +02:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2019-08-25 22:53:22 +02:00
# #
# 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. #
2020-07-08 14:48:06 +02:00
# Visit our website for more info: https://msberends.github.io/AMR. #
2019-08-25 22:53:22 +02:00
# ==================================================================== #
#' 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-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-08-25 22:53:22 +02:00
#' @export
2019-08-27 22:41:09 +02:00
#' @rdname bug_drug_combinations
2020-09-18 16:05:53 +02:00
#' @return The function [bug_drug_combinations()] returns a [data.frame] with columns "mo", "ab", "S", "I", "R" and "total".
2019-11-28 22:32:17 +01:00
#' @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,
2020-08-26 16:13:40 +02:00
#' FUN = function(x) ifelse(x == as.mo("E. coli"),
2019-09-23 13:53:50 +02:00
#' "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 ,
... ) {
2020-06-22 11:18:40 +02:00
stop_ifnot ( is.data.frame ( x ) , " `x` must be a data frame" )
stop_ifnot ( any ( sapply ( x , is.rsi ) , na.rm = TRUE ) , " No columns with class <rsi> found. See ?as.rsi." )
2019-08-25 22:53:22 +02:00
# try to find columns based on type
# -- mo
if ( is.null ( col_mo ) ) {
col_mo <- search_type_in_df ( x = x , type = " mo" )
}
2020-06-22 11:18:40 +02:00
stop_if ( is.null ( col_mo ) , " `col_mo` must be set" )
2019-08-25 22:53:22 +02:00
2020-06-17 15:14:37 +02:00
x_class <- class ( x )
2020-05-18 13:59:34 +02:00
x <- as.data.frame ( x , stringsAsFactors = FALSE )
2020-09-03 12:31:48 +02:00
x [ , col_mo ] <- FUN ( x [ , col_mo , drop = TRUE ] , ... )
2020-06-17 15:14:37 +02:00
x <- x [ , c ( col_mo , names ( which ( sapply ( x , is.rsi ) ) ) ) , drop = FALSE ]
2020-05-18 13:59:34 +02:00
unique_mo <- sort ( unique ( x [ , col_mo , drop = TRUE ] ) )
2020-07-13 09:17:24 +02:00
2020-05-18 13:59:34 +02:00
out <- data.frame (
mo = character ( 0 ) ,
ab = character ( 0 ) ,
S = integer ( 0 ) ,
I = integer ( 0 ) ,
R = integer ( 0 ) ,
total = integer ( 0 ) )
2020-07-13 09:17:24 +02:00
2020-05-18 13:59:34 +02:00
for ( i in seq_len ( length ( unique_mo ) ) ) {
# filter on MO group and only select R/SI columns
2020-06-17 15:14:37 +02:00
x_mo_filter <- x [which ( x [ , col_mo , drop = TRUE ] == unique_mo [i ] ) , names ( which ( sapply ( x , is.rsi ) ) ) , drop = FALSE ]
2020-05-18 13:59:34 +02:00
# turn and merge everything
pivot <- lapply ( x_mo_filter , function ( x ) {
m <- as.matrix ( table ( x ) )
data.frame ( S = m [ " S" , ] , I = m [ " I" , ] , R = m [ " R" , ] , stringsAsFactors = FALSE )
} )
merged <- do.call ( rbind , pivot )
out_group <- data.frame ( mo = unique_mo [i ] ,
ab = rownames ( merged ) ,
S = merged $ S ,
I = merged $ I ,
R = merged $ R ,
total = merged $ S + merged $ I + merged $ R )
out <- rbind ( out , out_group )
2020-05-16 13:05:47 +02:00
}
2020-07-13 09:17:24 +02:00
2020-06-17 15:14:37 +02:00
structure ( .Data = out , class = c ( " bug_drug_combinations" , x_class ) )
2019-08-25 22:53:22 +02:00
}
2020-05-28 16:48:55 +02:00
#' @method 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-06-17 15:14:37 +02:00
x <- as.data.frame ( x , stringsAsFactors = FALSE )
2020-05-18 13:59:34 +02:00
x <- subset ( x , 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-18 13:59:34 +02:00
x <- subset ( x , 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 ) ) ) {
2020-07-02 21:12:52 +02:00
ab_txt [i ] <- gsub ( " ab" , as.character ( as.ab ( ab [i ] ) ) , ab_txt [i ] )
2019-09-25 15:43:22 +02:00
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 ) {
2020-05-18 13:59:34 +02:00
cols <- colnames ( .data )
.data <- as.data.frame ( sapply ( .data , function ( x ) ifelse ( is.na ( x ) , " " , x ) , simplify = FALSE ) )
colnames ( .data ) <- cols
.data
2020-05-16 13:05:47 +02:00
}
create_var <- function ( .data , ... ) {
dots <- list ( ... )
for ( i in seq_len ( length ( dots ) ) ) {
.data [ , names ( dots ) [i ] ] <- dots [ [i ] ]
}
.data
}
2020-09-18 16:05:53 +02:00
y <- x %pm>%
2020-05-16 13:05:47 +02:00
create_var ( ab = as.ab ( x $ ab ) ,
2020-09-18 16:05:53 +02:00
ab_txt = give_ab_name ( ab = x $ ab , format = translate_ab , language = language ) ) %pm>%
pm_group_by ( ab , ab_txt , mo ) %pm>%
pm_summarise ( isolates = sum ( isolates , na.rm = TRUE ) ,
total = sum ( total , na.rm = TRUE ) ) %pm>%
pm_ungroup ( )
2020-05-16 13:05:47 +02:00
2020-09-18 16:05:53 +02:00
y <- y %pm>%
2020-05-16 13:05:47 +02:00
create_var ( txt = paste0 ( percentage ( y $ isolates / y $ total , decimal.mark = decimal.mark , big.mark = big.mark ) ,
2020-07-13 09:17:24 +02:00
" (" , trimws ( format ( y $ isolates , big.mark = big.mark ) ) , " /" ,
2020-09-18 16:05:53 +02:00
trimws ( format ( y $ total , big.mark = big.mark ) ) , " )" ) ) %pm>%
pm_select ( ab , ab_txt , mo , txt ) %pm>%
pm_arrange ( mo )
2020-07-13 09:17:24 +02:00
2020-05-18 13:59:34 +02:00
# replace tidyr::pivot_wider() from here
for ( i in unique ( y $ mo ) ) {
mo_group <- y [which ( y $ mo == i ) , c ( " ab" , " txt" ) ]
colnames ( mo_group ) <- c ( " ab" , i )
rownames ( mo_group ) <- NULL
2020-09-18 16:05:53 +02:00
y <- y %pm>%
pm_left_join ( mo_group , by = " ab" )
2020-05-18 13:59:34 +02:00
}
2020-09-18 16:05:53 +02:00
y <<- y
y <- y %pm>%
pm_distinct ( ab , .keep_all = TRUE ) %pm>%
pm_select ( - mo , - txt ) %pm>%
2020-05-18 13:59:34 +02:00
# replace tidyr::pivot_wider() until here
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" ) ] ) ]
}
2020-07-13 09:17:24 +02:00
2020-09-18 16:05:53 +02:00
y <- y %pm>%
create_var ( ab_group = ab_group ( y $ ab , language = language ) ) %pm>%
select_ab_vars ( ) %pm>%
pm_arrange ( ab_group , ab_txt )
y <- y %pm>%
2020-09-19 11:54:01 +02:00
create_var ( ab_group = ifelse ( y $ ab_group != pm_lag ( y $ ab_group ) | is.na ( pm_lag ( y $ ab_group ) ) , y $ ab_group , " " ) )
2020-07-13 09:17:24 +02:00
2019-08-25 22:53:22 +02:00
if ( add_ab_group == FALSE ) {
2020-09-18 16:05:53 +02:00
y <- y %pm>%
pm_select ( - ab_group ) %pm>%
pm_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 {
2020-09-18 16:05:53 +02:00
y <- y %pm>%
pm_rename ( " Group" = ab_group ,
" 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
}
2020-05-18 13:59:34 +02:00
rownames ( y ) <- NULL
2019-08-25 22:53:22 +02:00
y
}
2020-05-28 16:48:55 +02:00
#' @method 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 , ... ) {
2020-06-17 15:14:37 +02:00
x_class <- class ( x )
print ( structure ( x , class = x_class [x_class != " bug_drug_combinations" ] ) ,
... )
message ( font_blue ( " NOTE: Use 'format()' on this result to get a publishable/printable format." ) )
2019-08-25 22:53:22 +02:00
}