2020-06-17 01:39:30 +02:00
# ==================================================================== #
# TITLE #
2021-02-02 23:57:35 +01:00
# Antimicrobial Resistance (AMR) Data Analysis for R #
2020-06-17 01:39:30 +02:00
# #
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2020-06-17 01:39:30 +02:00
# #
# LICENCE #
2020-12-27 00:30:28 +01:00
# (c) 2018-2021 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. #
2020-06-17 01:39:30 +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. #
# 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/ #
2020-06-17 01:39:30 +02:00
# ==================================================================== #
2021-01-18 16:57:56 +01:00
#' Antibiotic Class Selectors
2020-06-17 01:39:30 +02:00
#'
2021-01-28 16:09:30 +01:00
#' These functions help to select the columns of antibiotics that are of a specific antibiotic class, without the need to define the columns or antibiotic abbreviations. \strong{\Sexpr{ifelse(as.double(R.Version()$major) + (as.double(R.Version()$minor) / 10) < 3.2, paste0("NOTE: THESE FUNCTIONS DO NOT WORK ON YOUR CURRENT R VERSION. These functions require R version 3.2 or later - you have ", R.version.string, "."), "")}}
2021-01-18 16:57:56 +01:00
#' @inheritSection lifecycle Stable Lifecycle
2021-02-08 14:18:42 +01:00
#' @param only_rsi_columns a logical to indicate whether only columns of class [`<rsi>`]([rsi]) must be selected (defaults to `FALSE`)
2020-06-17 15:14:37 +02:00
#' @inheritParams filter_ab_class
2021-01-06 11:16:17 +01:00
#' @details \strong{\Sexpr{ifelse(as.double(R.Version()$major) + (as.double(R.Version()$minor) / 10) < 3.2, paste0("NOTE: THESE FUNCTIONS DO NOT WORK ON YOUR CURRENT R VERSION. These functions require R version 3.2 or later - you have ", R.version.string, "."), "")}}
2021-01-04 12:29:25 +01:00
#'
#' All columns will be searched for known antibiotic names, abbreviations, brand names and codes (ATC, EARS-Net, WHO, etc.) in the [antibiotics] data set. This means that a selector like e.g. [aminoglycosides()] will pick up column names like 'gen', 'genta', 'J01GB03', 'tobra', 'Tobracin', etc.
2020-06-17 01:39:30 +02:00
#' @rdname antibiotic_class_selectors
#' @seealso [filter_ab_class()] for the `filter()` equivalent.
#' @name antibiotic_class_selectors
#' @export
2021-01-18 16:57:56 +01:00
#' @inheritSection AMR Reference Data Publicly Available
#' @inheritSection AMR Read more on Our Website!
2020-06-17 01:39:30 +02:00
#' @examples
2021-01-24 14:48:56 +01:00
#' # `example_isolates` is a data set available in the AMR package.
2020-12-27 20:32:40 +01:00
#' # See ?example_isolates.
#'
#' # this will select columns 'IPM' (imipenem) and 'MEM' (meropenem):
2021-02-17 10:58:13 +01:00
#' example_isolates[, carbapenems()]
2020-12-27 20:32:40 +01:00
#' # this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB':
#' example_isolates[, c("mo", aminoglycosides())]
#'
2020-09-29 23:35:46 +02:00
#' if (require("dplyr")) {
2020-06-17 01:39:30 +02:00
#'
#' # this will select columns 'IPM' (imipenem) and 'MEM' (meropenem):
#' example_isolates %>%
#' select(carbapenems())
#'
#' # this will select columns 'mo', 'AMK', 'GEN', 'KAN' and 'TOB':
#' example_isolates %>%
#' select(mo, aminoglycosides())
#'
2020-06-17 15:14:37 +02:00
#' # this will select columns 'mo' and all antimycobacterial drugs ('RIF'):
#' example_isolates %>%
#' select(mo, ab_class("mycobact"))
#'
#'
#' # get bug/drug combinations for only macrolides in Gram-positives:
#' example_isolates %>%
2020-11-16 11:03:24 +01:00
#' filter(mo_is_gram_positive()) %>%
2020-06-17 15:14:37 +02:00
#' select(mo, macrolides()) %>%
#' bug_drug_combinations() %>%
#' format()
#'
2020-06-17 01:39:30 +02:00
#'
2020-09-29 23:35:46 +02:00
#' data.frame(some_column = "some_value",
2020-06-17 01:39:30 +02:00
#' J01CA01 = "S") %>% # ATC code of ampicillin
2020-09-29 23:35:46 +02:00
#' select(penicillins()) # only the 'J01CA01' column will be selected
2020-12-27 23:19:41 +01:00
#'
#'
#' # with dplyr 1.0.0 and higher (that adds 'across()'), this is equal:
#' # (though the row names on the first are more correct)
#' example_isolates %>% filter_carbapenems("R", "all")
#' example_isolates %>% filter(across(carbapenems(), ~. == "R"))
2020-06-17 01:39:30 +02:00
#' }
2021-02-02 23:57:35 +01:00
ab_class <- function ( ab_class ,
2021-02-08 14:18:42 +01:00
only_rsi_columns = FALSE ) {
2021-02-04 16:48:16 +01:00
ab_selector ( ab_class , function_name = " ab_class" , only_rsi_columns = only_rsi_columns )
2020-06-17 15:14:37 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
aminoglycosides <- function ( only_rsi_columns = FALSE ) {
2021-02-04 16:48:16 +01:00
ab_selector ( " aminoglycoside" , function_name = " aminoglycosides" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
carbapenems <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " carbapenem" , function_name = " carbapenems" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
cephalosporins <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " cephalosporin" , function_name = " cephalosporins" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
cephalosporins_1st <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " cephalosporins.*1" , function_name = " cephalosporins_1st" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
cephalosporins_2nd <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " cephalosporins.*2" , function_name = " cephalosporins_2nd" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
cephalosporins_3rd <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " cephalosporins.*3" , function_name = " cephalosporins_3rd" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
cephalosporins_4th <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " cephalosporins.*4" , function_name = " cephalosporins_4th" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
cephalosporins_5th <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " cephalosporins.*5" , function_name = " cephalosporins_5th" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
fluoroquinolones <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " fluoroquinolone" , function_name = " fluoroquinolones" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
glycopeptides <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " glycopeptide" , function_name = " glycopeptides" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
macrolides <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " macrolide" , function_name = " macrolides" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
oxazolidinones <- function ( only_rsi_columns = FALSE ) {
ab_selector ( " oxazolidinone" , function_name = " oxazolidinones" , only_rsi_columns = only_rsi_columns )
}
#' @rdname antibiotic_class_selectors
#' @export
penicillins <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " penicillin" , function_name = " penicillins" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
#' @rdname antibiotic_class_selectors
#' @export
2021-02-08 14:18:42 +01:00
tetracyclines <- function ( only_rsi_columns = FALSE ) {
2021-02-02 23:57:35 +01:00
ab_selector ( " tetracycline" , function_name = " tetracyclines" , only_rsi_columns = only_rsi_columns )
2020-06-17 01:39:30 +02:00
}
2021-02-02 23:57:35 +01:00
ab_selector <- function ( ab_class ,
function_name ,
only_rsi_columns ) {
2020-10-19 17:09:19 +02:00
meet_criteria ( ab_class , allow_class = " character" , has_length = 1 , .call_depth = 1 )
meet_criteria ( function_name , allow_class = " character" , has_length = 1 , .call_depth = 1 )
2021-02-08 14:18:42 +01:00
meet_criteria ( only_rsi_columns , allow_class = " logical" , has_length = 1 , .call_depth = 1 )
2021-01-04 12:29:25 +01:00
2021-01-04 13:39:06 +01:00
if ( as.double ( R.Version ( ) $ major ) + ( as.double ( R.Version ( ) $ minor ) / 10 ) < 3.2 ) {
2021-01-06 11:16:17 +01:00
warning_ ( " antibiotic class selectors such as " , function_name ,
" () require R version 3.2 or later - you have " , R.version.string ,
call = FALSE )
2021-01-04 12:29:25 +01:00
return ( NULL )
}
2021-01-03 23:40:05 +01:00
vars_df <- get_current_data ( arg_name = NA , call = -3 )
2021-02-08 14:18:42 +01:00
2021-01-22 10:20:41 +01:00
# improve speed here so it will only run once when e.g. in one select call
if ( ! identical ( pkg_env $ ab_selector , unique_call_id ( ) ) ) {
2021-02-02 23:57:35 +01:00
ab_in_data <- get_column_abx ( vars_df , info = FALSE , only_rsi_columns = only_rsi_columns )
2021-01-22 10:20:41 +01:00
pkg_env $ ab_selector <- unique_call_id ( )
pkg_env $ ab_selector_cols <- ab_in_data
} else {
ab_in_data <- pkg_env $ ab_selector_cols
}
2020-06-17 15:14:37 +02:00
2020-06-17 01:39:30 +02:00
if ( length ( ab_in_data ) == 0 ) {
2020-10-27 15:56:51 +01:00
message_ ( " No antimicrobial agents found." )
2020-06-17 01:39:30 +02:00
return ( NULL )
}
2020-06-17 15:14:37 +02:00
2020-06-17 01:39:30 +02:00
ab_reference <- subset ( antibiotics ,
group %like% ab_class |
atc_group1 %like% ab_class |
atc_group2 %like% ab_class )
ab_group <- find_ab_group ( ab_class )
2020-06-17 15:14:37 +02:00
if ( ab_group == " " ) {
ab_group <- paste0 ( " '" , ab_class , " '" )
examples <- " "
} else {
examples <- paste0 ( " (such as " , find_ab_names ( ab_class , 2 ) , " )" )
}
2020-06-17 01:39:30 +02:00
# get the columns with a group names in the chosen ab class
agents <- ab_in_data [names ( ab_in_data ) %in% ab_reference $ ab ]
2020-12-27 23:19:41 +01:00
if ( message_not_thrown_before ( function_name ) ) {
if ( length ( agents ) == 0 ) {
message_ ( " No antimicrobial agents of class " , ab_group , " found" , examples , " ." )
} else {
2021-02-08 14:18:42 +01:00
agents_formatted <- paste0 ( " '" , font_bold ( agents , collapse = NULL ) , " '" )
2021-01-06 11:16:17 +01:00
agents_names <- ab_name ( names ( agents ) , tolower = TRUE , language = NULL )
2021-01-24 23:27:11 +01:00
need_name <- tolower ( gsub ( " [^a-zA-Z]" , " " , agents ) ) != tolower ( gsub ( " [^a-zA-Z]" , " " , agents_names ) )
2021-01-22 10:20:41 +01:00
agents_formatted [need_name ] <- paste0 ( agents_formatted [need_name ] ,
" (" , agents_names [need_name ] , " )" )
2021-02-08 14:18:42 +01:00
message_ ( " Selecting " , ab_group , " : " ,
ifelse ( length ( agents ) == 1 , " column " , " columns " ) ,
vector_and ( agents_formatted , quotes = FALSE ) ,
2020-12-27 23:19:41 +01:00
as_note = FALSE ,
2021-02-08 14:18:42 +01:00
extra_indent = 6 )
2020-12-27 23:19:41 +01:00
}
2021-01-22 10:20:41 +01:00
remember_thrown_message ( function_name )
}
2020-06-17 01:39:30 +02:00
unname ( agents )
}