AMR/R/ab_property.R

472 lines
16 KiB
R
Raw Normal View History

2019-05-10 16:44:59 +02:00
# ==================================================================== #
# TITLE: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2019-05-10 16:44:59 +02:00
# #
# SOURCE CODE: #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2019-05-10 16:44:59 +02:00
# #
# PLEASE CITE THIS SOFTWARE AS: #
# Berends MS, Luz CF, Friedrich AW, et al. (2022). #
# AMR: An R Package for Working with Antimicrobial Resistance Data. #
# Journal of Statistical Software, 104(3), 1-31. #
2023-05-27 10:39:22 +02:00
# https://doi.org/10.18637/jss.v104.i03 #
2022-10-05 09:12:22 +02:00
# #
2022-12-27 15:16:15 +01:00
# Developed at the University of Groningen and the University Medical #
# Center Groningen in The Netherlands, in collaboration with many #
# colleagues from around the world, see our website. #
2019-05-10 16:44:59 +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 #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
2019-05-10 16:44:59 +02:00
# ==================================================================== #
#' Get Properties of an Antibiotic
2019-05-10 16:44:59 +02:00
#'
#' Use these functions to return a specific property of an antibiotic from the [antibiotics] data set. All input values will be evaluated internally with [as.ab()].
2022-11-13 13:44:25 +01:00
#' @param x any (vector of) text that can be coerced to a valid antibiotic drug code with [as.ab()]
2021-05-12 18:15:03 +02:00
#' @param tolower a [logical] to indicate whether the first [character] of every output should be transformed to a lower case [character]. This will lead to e.g. "polymyxin B" and not "polymyxin b".
#' @param property one of the column names of one of the [antibiotics] data set: `vector_or(colnames(antibiotics), sort = FALSE)`.
#' @param language language of the returned text - the default is the current system language (see [get_AMR_locale()]) and can also be set with the [package option][AMR-options] [`AMR_locale`][AMR-options]. Use `language = NULL` or `language = ""` to prevent translation.
#' @param administration way of administration, either `"oral"` or `"iv"`
#' @param open browse the URL using [utils::browseURL()]
2023-01-05 14:43:18 +01:00
#' @param ... in case of [set_ab_names()] and `data` is a [data.frame]: columns to select (supports tidy selection such as `column1:column4`), otherwise other arguments passed on to [as.ab()]
2021-09-29 12:12:35 +02:00
#' @param data a [data.frame] of which the columns need to be renamed, or a [character] vector of column names
#' @param snake_case a [logical] to indicate whether the names should be in so-called [snake case](https://en.wikipedia.org/wiki/Snake_case): in lower case and all spaces/slashes replaced with an underscore (`_`)
2021-08-17 14:34:11 +02:00
#' @param only_first a [logical] to indicate whether only the first ATC code must be returned, with giving preference to J0-codes (i.e., the antimicrobial drug group)
#' @details All output [will be translated][translate] where possible.
2022-08-28 10:31:50 +02:00
#'
2020-05-25 01:01:14 +02:00
#' The function [ab_url()] will return the direct URL to the official WHO website. A warning will be returned if the required ATC code is not available.
2022-08-28 10:31:50 +02:00
#'
#' The function [set_ab_names()] is a special column renaming function for [data.frame]s. It renames columns names that resemble antimicrobial drugs. It always makes sure that the new column names are unique. If `property = "atc"` is set, preference is given to ATC codes from the J-group.
2019-05-10 16:44:59 +02:00
#' @inheritSection as.ab Source
#' @rdname ab_property
#' @name ab_property
2022-08-28 10:31:50 +02:00
#' @return
#' - An [integer] in case of [ab_cid()]
#' - A named [list] in case of [ab_info()] and multiple [ab_atc()]/[ab_synonyms()]/[ab_tradenames()]
#' - A [double] in case of [ab_ddd()]
#' - A [data.frame] in case of [set_ab_names()]
#' - A [character] in all other cases
2019-05-10 16:44:59 +02:00
#' @export
#' @seealso [antibiotics]
#' @inheritSection AMR Reference Data Publicly Available
2019-05-10 16:44:59 +02:00
#' @examples
#' # all properties:
#' ab_name("AMX")
#' ab_atc("AMX")
#' ab_cid("AMX")
#' ab_synonyms("AMX")
#' ab_tradenames("AMX")
#' ab_group("AMX")
#' ab_atc_group1("AMX")
#' ab_atc_group2("AMX")
#' ab_url("AMX")
2019-05-10 16:44:59 +02:00
#'
2023-05-24 15:55:53 +02:00
#' # smart lowercase transformation
#' ab_name(x = c("AMC", "PLB"))
#' ab_name(x = c("AMC", "PLB"), tolower = TRUE)
2019-05-10 16:44:59 +02:00
#'
2020-01-26 20:20:00 +01:00
#' # defined daily doses (DDD)
#' ab_ddd("AMX", "oral")
#' ab_ddd_units("AMX", "oral")
#' ab_ddd("AMX", "iv")
#' ab_ddd_units("AMX", "iv")
2019-05-10 16:44:59 +02:00
#'
2022-08-28 10:31:50 +02:00
#' ab_info("AMX") # all properties as a list
2019-05-16 21:20:00 +02:00
#'
2020-01-26 20:20:00 +01:00
#' # all ab_* functions use as.ab() internally, so you can go from 'any' to 'any':
#' ab_atc("AMP")
#' ab_group("J01CA01")
#' ab_loinc("ampicillin")
#' ab_name("21066-6")
#' ab_name(6249)
#' ab_name("J01CA01")
2022-08-28 10:31:50 +02:00
#'
#' # spelling from different languages and dyslexia are no problem
#' ab_atc("ceftriaxon")
#' ab_atc("cephtriaxone")
#' ab_atc("cephthriaxone")
#' ab_atc("seephthriaaksone")
2022-08-28 10:31:50 +02:00
#'
#' # use set_ab_names() for renaming columns
#' colnames(example_isolates)
#' colnames(set_ab_names(example_isolates))
#' colnames(set_ab_names(example_isolates, NIT:VAN))
#' \donttest{
#' if (require("dplyr")) {
#' example_isolates %>%
2023-02-08 16:51:41 +01:00
#' set_ab_names()
2022-08-28 10:31:50 +02:00
#'
2021-09-29 12:12:35 +02:00
#' # this does the same:
#' example_isolates %>%
2023-02-08 16:51:41 +01:00
#' rename_with(set_ab_names)
2022-08-28 10:31:50 +02:00
#'
#' # set_ab_names() works with any AB property:
#' example_isolates %>%
2023-02-08 16:51:41 +01:00
#' set_ab_names(property = "atc")
2022-08-28 10:31:50 +02:00
#'
#' example_isolates %>%
2023-02-09 13:07:39 +01:00
#' set_ab_names(where(is.sir)) %>%
#' colnames()
2022-08-28 10:31:50 +02:00
#'
#' example_isolates %>%
#' set_ab_names(NIT:VAN) %>%
#' colnames()
#' }
#' }
ab_name <- function(x, language = get_AMR_locale(), tolower = FALSE, ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2022-10-05 09:12:22 +02:00
language <- validate_language(language)
meet_criteria(tolower, allow_class = "logical", has_length = 1)
2022-08-28 10:31:50 +02:00
2022-08-19 12:33:14 +02:00
x <- translate_into_language(ab_validate(x = x, property = "name", ...), language = language, only_affect_ab_names = TRUE)
2019-05-10 16:44:59 +02:00
if (tolower == TRUE) {
# use perl to only transform the first character
# as we want "polymyxin B", not "polymyxin b"
2019-06-11 14:18:25 +02:00
x <- gsub("^([A-Z])", "\\L\\1", x, perl = TRUE)
2019-05-10 16:44:59 +02:00
}
x
}
2019-05-10 16:44:59 +02:00
#' @rdname ab_property
#' @export
ab_cid <- function(x, ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2019-05-10 16:44:59 +02:00
ab_validate(x = x, property = "cid", ...)
}
#' @rdname ab_property
#' @export
ab_synonyms <- function(x, ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2019-05-10 16:44:59 +02:00
syns <- ab_validate(x = x, property = "synonyms", ...)
names(syns) <- x
if (length(syns) == 1) {
unname(unlist(syns))
} else {
syns
}
}
#' @rdname ab_property
#' @export
ab_tradenames <- function(x, ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2019-05-10 16:44:59 +02:00
ab_synonyms(x, ...)
}
#' @rdname ab_property
#' @export
ab_group <- function(x, language = get_AMR_locale(), ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2022-10-05 09:12:22 +02:00
language <- validate_language(language)
2022-08-19 12:33:14 +02:00
translate_into_language(ab_validate(x = x, property = "group", ...), language = language, only_affect_ab_names = TRUE)
2019-05-10 16:44:59 +02:00
}
2021-08-17 14:34:11 +02:00
#' @rdname ab_property
#' @aliases ATC
#' @export
ab_atc <- function(x, only_first = FALSE, ...) {
meet_criteria(x, allow_NA = TRUE)
meet_criteria(only_first, allow_class = "logical", has_length = 1)
2022-08-28 10:31:50 +02:00
2021-08-17 14:34:11 +02:00
atcs <- ab_validate(x = x, property = "atc", ...)
2022-08-28 10:31:50 +02:00
2021-08-17 14:34:11 +02:00
if (only_first == TRUE) {
2022-08-28 10:31:50 +02:00
atcs <- vapply(
FUN.VALUE = character(1),
# get only the first ATC code
atcs,
function(x) {
# try to get the J-group
if (any(x %like% "^J")) {
x[x %like% "^J"][1L]
} else {
as.character(x[1L])
}
}
)
2021-08-17 14:34:11 +02:00
} else if (length(atcs) == 1) {
atcs <- unname(unlist(atcs))
} else {
names(atcs) <- x
}
2022-08-28 10:31:50 +02:00
2021-08-17 14:34:11 +02:00
atcs
}
2019-05-10 16:44:59 +02:00
#' @rdname ab_property
#' @export
ab_atc_group1 <- function(x, language = get_AMR_locale(), ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2022-10-05 09:12:22 +02:00
language <- validate_language(language)
2022-08-19 12:33:14 +02:00
translate_into_language(ab_validate(x = x, property = "atc_group1", ...), language = language, only_affect_ab_names = TRUE)
2019-05-10 16:44:59 +02:00
}
#' @rdname ab_property
#' @export
ab_atc_group2 <- function(x, language = get_AMR_locale(), ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2022-10-05 09:12:22 +02:00
language <- validate_language(language)
2022-08-19 12:33:14 +02:00
translate_into_language(ab_validate(x = x, property = "atc_group2", ...), language = language, only_affect_ab_names = TRUE)
2019-05-10 16:44:59 +02:00
}
2020-01-26 20:20:00 +01:00
#' @rdname ab_property
#' @export
ab_loinc <- function(x, ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2020-01-26 20:20:00 +01:00
loincs <- ab_validate(x = x, property = "loinc", ...)
names(loincs) <- x
if (length(loincs) == 1) {
unname(unlist(loincs))
} else {
loincs
}
}
2019-05-10 16:44:59 +02:00
#' @rdname ab_property
#' @export
ab_ddd <- function(x, administration = "oral", ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
meet_criteria(administration, is_in = c("oral", "iv"), has_length = 1)
2022-08-28 10:31:50 +02:00
x <- as.ab(x, ...)
ddd_prop <- paste0(administration, "_ddd")
2021-08-19 23:43:02 +02:00
out <- ab_validate(x = x, property = ddd_prop)
2022-08-28 10:31:50 +02:00
2021-08-29 23:50:45 +02:00
if (any(ab_name(x, language = NULL) %like% "/" & is.na(out))) {
2022-08-28 10:31:50 +02:00
warning_(
"in `ab_ddd()`: DDDs of some combined products are available for different dose combinations and not (yet) part of the AMR package.",
"Please refer to the WHOCC website:\n",
2024-03-03 23:24:57 +01:00
"atcddd.fhi.no/ddd/list_of_ddds_combined_products/"
2022-08-28 10:31:50 +02:00
)
2021-08-19 23:43:02 +02:00
}
out
}
#' @rdname ab_property
#' @export
ab_ddd_units <- function(x, administration = "oral", ...) {
meet_criteria(x, allow_NA = TRUE)
meet_criteria(administration, is_in = c("oral", "iv"), has_length = 1)
2022-08-28 10:31:50 +02:00
x <- as.ab(x, ...)
ddd_prop <- paste0(administration, "_units")
out <- ab_validate(x = x, property = ddd_prop)
if (any(ab_name(x, language = NULL) %like% "/" & is.na(out))) {
2022-08-28 10:31:50 +02:00
warning_(
"in `ab_ddd_units()`: DDDs of some combined products are available for different dose combinations and not (yet) part of the AMR package.",
2022-08-28 10:31:50 +02:00
"Please refer to the WHOCC website:\n",
2024-03-03 23:24:57 +01:00
"atcddd.fhi.no/ddd/list_of_ddds_combined_products/"
2022-08-28 10:31:50 +02:00
)
}
out
2019-05-10 16:44:59 +02:00
}
2019-05-16 22:07:31 +02:00
#' @rdname ab_property
#' @export
ab_info <- function(x, language = get_AMR_locale(), ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2022-10-05 09:12:22 +02:00
language <- validate_language(language)
2022-08-28 10:31:50 +02:00
2020-02-14 19:54:13 +01:00
x <- as.ab(x, ...)
2022-08-28 10:31:50 +02:00
list(
ab = as.character(x),
cid = ab_cid(x),
name = ab_name(x, language = language),
group = ab_group(x, language = language),
atc = ab_atc(x),
atc_group1 = ab_atc_group1(x, language = language),
atc_group2 = ab_atc_group2(x, language = language),
tradenames = ab_tradenames(x),
loinc = ab_loinc(x),
ddd = list(
oral = list(
amount = ab_ddd(x, administration = "oral"),
units = ab_ddd_units(x, administration = "oral")
),
iv = list(
amount = ab_ddd(x, administration = "iv"),
units = ab_ddd_units(x, administration = "iv")
)
)
)
2019-05-16 21:20:00 +02:00
}
#' @rdname ab_property
#' @export
ab_url <- function(x, open = FALSE, ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
meet_criteria(open, allow_class = "logical", has_length = 1)
2022-08-28 10:31:50 +02:00
2021-08-17 14:34:11 +02:00
ab <- as.ab(x = x, ...)
atcs <- ab_atc(ab, only_first = TRUE)
u <- character(length(atcs))
# veterinary codes
2024-03-03 23:24:57 +01:00
u[atcs %like% "^Q"] <- paste0("https://atcddd.fhi.no/atcvet/atcvet_index/?code=", atcs[atcs %like% "^Q"], "&showdescription=no")
u[atcs %unlike% "^Q"] <- paste0("https://atcddd.fhi.no/atc_ddd_index//?code=", atcs[atcs %unlike% "^Q"], "&showdescription=no")
2021-08-17 14:34:11 +02:00
u[is.na(atcs)] <- NA_character_
names(u) <- ab_name(ab)
2022-08-28 10:31:50 +02:00
2021-08-17 14:34:11 +02:00
NAs <- ab_name(ab, tolower = TRUE, language = NULL)[!is.na(ab) & is.na(atcs)]
if (length(NAs) > 0) {
warning_("in `ab_url()`: no ATC code available for ", vector_and(NAs, quotes = FALSE), ".")
}
2022-08-28 10:31:50 +02:00
if (open == TRUE) {
2022-10-05 09:12:22 +02:00
if (length(u) > 1 && !is.na(u[1L])) {
warning_("in `ab_url()`: only the first URL will be opened, as `browseURL()` only suports one string.")
}
if (!is.na(u[1L])) {
utils::browseURL(u[1L])
}
}
u
}
2019-05-10 16:44:59 +02:00
#' @rdname ab_property
#' @export
ab_property <- function(x, property = "name", language = get_AMR_locale(), ...) {
2020-10-20 21:00:57 +02:00
meet_criteria(x, allow_NA = TRUE)
2022-10-05 09:12:22 +02:00
meet_criteria(property, is_in = colnames(AMR::antibiotics), has_length = 1)
language <- validate_language(language)
2022-08-19 12:33:14 +02:00
translate_into_language(ab_validate(x = x, property = property, ...), language = language)
2019-05-10 16:44:59 +02:00
}
2021-09-29 12:12:35 +02:00
#' @rdname ab_property
#' @aliases ATC
#' @export
set_ab_names <- function(data, ..., property = "name", language = get_AMR_locale(), snake_case = NULL) {
2021-09-29 12:12:35 +02:00
meet_criteria(data, allow_class = c("data.frame", "character"))
2022-10-05 09:12:22 +02:00
meet_criteria(property, is_in = colnames(AMR::antibiotics), has_length = 1, ignore.case = TRUE)
language <- validate_language(language)
2021-09-29 12:12:35 +02:00
meet_criteria(snake_case, allow_class = "logical", has_length = 1, allow_NULL = TRUE)
2022-08-28 10:31:50 +02:00
2021-09-29 12:12:35 +02:00
x_deparsed <- deparse(substitute(data))
if (length(x_deparsed) > 1 || any(x_deparsed %unlike% "[a-z]+")) {
x_deparsed <- "your_data"
}
2022-08-28 10:31:50 +02:00
2021-09-29 12:12:35 +02:00
property <- tolower(property)
if (is.null(snake_case)) {
snake_case <- property == "name"
}
2022-08-28 10:31:50 +02:00
2021-09-29 12:12:35 +02:00
if (is.data.frame(data)) {
2023-02-06 14:34:38 +01:00
if (tryCatch(length(c(...)) > 1, error = function(e) TRUE)) {
2023-02-09 13:07:39 +01:00
df <- tryCatch(suppressWarnings(pm_select(data, ...)),
2023-02-12 17:10:48 +01:00
error = function(e) {
data[, c(...), drop = FALSE]
}
)
2023-02-06 14:34:38 +01:00
} else if (tryCatch(is.character(c(...)), error = function(e) FALSE)) {
df <- data[, c(...), drop = FALSE]
2021-12-05 22:59:06 +01:00
} else {
df <- data
}
2023-01-21 23:47:20 +01:00
vars <- get_column_abx(df, info = FALSE, only_sir_columns = FALSE, sort = FALSE, fn = "set_ab_names")
2021-09-29 12:12:35 +02:00
if (length(vars) == 0) {
message_("No columns with antibiotic results found for `set_ab_names()`, leaving names unchanged.")
return(data)
}
} else {
2022-11-13 13:44:25 +01:00
# quickly get antibiotic drug codes
2021-09-29 12:12:35 +02:00
vars_ab <- as.ab(data, fast_mode = TRUE)
vars <- data[!is.na(vars_ab)]
}
2022-08-28 10:31:50 +02:00
x <- vapply(
FUN.VALUE = character(1),
ab_property(vars, property = property, language = language),
function(x) {
if (property == "atc") {
# try to get the J-group
if (any(x %like% "^J")) {
x[x %like% "^J"][1L]
} else {
as.character(x[1L])
}
} else {
as.character(x[1L])
}
},
USE.NAMES = FALSE
)
2021-09-29 12:12:35 +02:00
if (any(x %in% c("", NA))) {
2022-08-28 10:31:50 +02:00
warning_(
"in `set_ab_names()`: no ", property, " found for column(s): ",
vector_and(vars[x %in% c("", NA)], sort = FALSE)
)
2021-09-29 12:12:35 +02:00
x[x %in% c("", NA)] <- vars[x %in% c("", NA)]
}
2022-08-28 10:31:50 +02:00
2021-09-29 12:12:35 +02:00
if (snake_case == TRUE) {
x <- tolower(gsub("[^a-zA-Z0-9]+", "_", x))
}
2022-08-28 10:31:50 +02:00
2022-10-05 09:12:22 +02:00
if (anyDuplicated(x)) {
2021-09-29 12:12:35 +02:00
# very hacky way of adding the index to each duplicate
# so "Amoxicillin", "Amoxicillin", "Amoxicillin"
# will be "Amoxicillin", "Amoxicillin_2", "Amoxicillin_3"
2022-08-28 10:31:50 +02:00
invisible(lapply(
unique(x),
function(u) {
dups <- which(x == u)
if (length(dups) > 1) {
# there are duplicates
dup_add_int <- dups[2:length(dups)]
2022-10-05 09:12:22 +02:00
x[dup_add_int] <<- paste0(x[dup_add_int], "_", 2:length(dups))
2022-08-28 10:31:50 +02:00
}
}
))
2021-09-29 12:12:35 +02:00
}
if (is.data.frame(data)) {
colnames(data)[colnames(data) %in% vars] <- x
data
} else {
data[which(!is.na(vars_ab))] <- x
data
}
}
2019-05-10 16:44:59 +02:00
ab_validate <- function(x, property, ...) {
2022-10-14 13:02:50 +02:00
if (tryCatch(all(x[!is.na(x)] %in% AMR_env$AB_lookup$ab), error = function(e) FALSE)) {
2023-02-09 13:07:39 +01:00
# special case for ab_* functions where class is already 'ab'
2022-10-14 13:02:50 +02:00
x <- AMR_env$AB_lookup[match(x, AMR_env$AB_lookup$ab), property, drop = TRUE]
} else {
# try to catch an error when inputting an invalid argument
# so the 'call.' can be set to FALSE
2022-10-14 13:02:50 +02:00
tryCatch(x[1L] %in% AMR_env$AB_lookup[1, property, drop = TRUE],
2022-08-28 10:31:50 +02:00
error = function(e) stop(e$message, call. = FALSE)
)
2022-10-14 13:02:50 +02:00
if (!all(x %in% AMR_env$AB_lookup[, property, drop = TRUE])) {
x <- as.ab(x, ...)
2022-10-14 13:02:50 +02:00
if (all(is.na(x)) && is.list(AMR_env$AB_lookup[, property, drop = TRUE])) {
2022-08-28 21:13:26 +02:00
x <- rep(NA_character_, length(x))
} else {
2022-10-14 13:02:50 +02:00
x <- AMR_env$AB_lookup[match(x, AMR_env$AB_lookup$ab), property, drop = TRUE]
2022-08-28 21:13:26 +02:00
}
}
2019-05-10 16:44:59 +02:00
}
2022-08-28 10:31:50 +02:00
2019-06-27 11:57:45 +02:00
if (property == "ab") {
return(set_clean_class(x, new_class = c("ab", "character")))
2019-05-10 16:44:59 +02:00
} else if (property == "cid") {
return(as.integer(x))
} else if (property %like% "ddd") {
return(as.double(x))
} else {
x[is.na(x)] <- NA
2019-05-10 16:44:59 +02:00
return(x)
}
}