2019-08-25 22:53:22 +02:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# This R package is free software; you can freely use and distribute #
# it for both personal and commercial purposes under the terms of the #
# GNU General Public License version 2.0 (GNU GPL-2), as published by #
# the Free Software Foundation. #
# #
# This R package was created for academic research and was publicly #
# released in the hope that it will be useful, but it comes WITHOUT #
# ANY WARRANTY OR LIABILITY. #
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
#' Determine bug-drug combinations
#'
#' Determine antimicrobial resistance (AMR) of all bug-drug combinations in your data set where at least 30 (default) isolates are available per species. Use \code{format} on the result to prettify it to a printable format, see Examples.
#' @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-09-23 13:53:50 +02:00
#' @param FUN the function to call on the \code{mo} column to transform the microorganism IDs, defaults to \code{\link{mo_shortname}}
#' @param ... argumments passed on to \code{FUN}
2019-08-25 22:53:22 +02:00
#' @inheritParams rsi_df
2019-09-23 13:53:50 +02:00
#' @inheritParams base::formatC
2019-08-25 22:53:22 +02:00
#' @importFrom dplyr rename
#' @importFrom tidyr spread
#' @importFrom clean freq
2019-08-27 22:41:09 +02:00
#' @details The function \code{format} calculates the resistance per bug-drug combination. Use \code{combine_IR = FALSE} (default) to test R vs. S+I and \code{combine_IR = TRUE} to test R+I vs. S.
2019-09-23 13:53:50 +02:00
#'
#' The language of the output can be overwritten with \code{options(AMR_locale)}, please see \link{translate}.
2019-08-25 22:53:22 +02:00
#' @export
2019-08-27 22:41:09 +02:00
#' @rdname bug_drug_combinations
2019-08-25 22:53:22 +02:00
#' @source \strong{M39 Analysis and Presentation of Cumulative Antimicrobial Susceptibility Test Data, 4th Edition}, 2014, \emph{Clinical and Laboratory Standards Institute (CLSI)}. \url{https://clsi.org/standards/products/microbiology/documents/m39/}.
#' @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
#' format(x)
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 ,
minimum = 30 ,
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 )
}
x <- x %>%
2019-09-23 13:53:50 +02:00
mutate ( mo = x %>% pull ( col_mo ) %>% FUN ( ... ) ) %>%
2019-08-25 22:53:22 +02:00
filter ( mo %in% ( clean :: freq ( mo ) %>%
filter ( count >= minimum ) %>%
pull ( item ) ) ) %>%
group_by ( mo ) %>%
AMR :: rsi_df ( translate_ab = FALSE , combine_SI = FALSE ) %>%
select ( - value ) %>%
spread ( interpretation , isolates ) %>%
mutate ( total = S + I + R ) %>%
filter ( total >= minimum ) %>%
rename ( ab = antibiotic )
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
}
#' @importFrom dplyr everything rename
#' @importFrom tidyr spread
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-23 13:53:50 +02:00
format.bug_drug_combinations <- function ( x ,
combine_IR = FALSE ,
add_ab_group = TRUE ,
decimal.mark = getOption ( " OutDec" ) ,
2019-09-23 14:37:24 +02:00
big.mark = ifelse ( decimal.mark == " ," , " ." , " ," ) ,
... ) {
2019-08-27 19:15:04 +02:00
if ( combine_IR == FALSE ) {
2019-08-25 22:53:22 +02:00
x $ isolates <- x $ R
} else {
x $ isolates <- x $ R + x $ I
}
y <- x %>%
2019-09-23 13:53:50 +02:00
mutate ( txt = paste0 ( percent ( isolates / total , force_zero = TRUE , decimal.mark = decimal.mark , big.mark = big.mark ) ,
" (" , trimws ( format ( isolates , big.mark = big.mark ) ) , " /" ,
trimws ( format ( total , big.mark = big.mark ) ) , " )" ) ) %>%
2019-08-25 22:53:22 +02:00
select ( ab , mo , txt ) %>%
spread ( mo , txt ) %>%
mutate_all ( ~ ifelse ( is.na ( .) , " " , .) ) %>%
2019-09-23 13:53:50 +02:00
mutate ( ab_group = ab_group ( ab ) ,
ab = paste0 ( ab_name ( ab ) , " (" , as.ab ( ab ) , " , " , ab_atc ( ab ) , " )" ) ) %>%
2019-08-25 22:53:22 +02:00
select ( ab_group , ab , everything ( ) ) %>%
arrange ( ab_group , ab ) %>%
mutate ( ab_group = ifelse ( ab_group != lag ( ab_group ) | is.na ( lag ( ab_group ) ) , ab_group , " " ) )
if ( add_ab_group == FALSE ) {
2019-09-23 13:53:50 +02:00
y <- y %>% select ( - ab_group ) %>% rename ( " Antibiotic" = ab )
colnames ( y ) [1 ] <- translate_AMR ( colnames ( y ) [1 ] , language = get_locale ( ) , only_unknown = FALSE )
} else {
y <- y %>% rename ( " Group" = ab_group ,
" Antibiotic" = ab )
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
#' @importFrom crayon blue
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 ) )
message ( blue ( " NOTE: Use 'format()' on this result to get a format that is ready for export or printing." ) )
}