AMR/R/ab_from_text.R

231 lines
11 KiB
R
Raw Normal View History

2020-06-25 17:34:50 +02:00
# ==================================================================== #
# TITLE: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2020-06-25 17:34:50 +02:00
# #
# SOURCE CODE: #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
2020-06-25 17:34:50 +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. #
2020-06-25 17:34:50 +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/ #
2020-06-25 17:34:50 +02:00
# ==================================================================== #
#' Retrieve Antimicrobial Drug Names and Doses from Clinical Text
2022-08-28 10:31:50 +02:00
#'
#' Use this function on e.g. clinical texts from health care records. It returns a [list] with all antimicrobial drugs, doses and forms of administration found in the texts.
2020-06-25 17:34:50 +02:00
#' @param text text to analyse
#' @param type type of property to search for, either `"drug"`, `"dose"` or `"administration"`, see *Examples*
2021-05-12 18:15:03 +02:00
#' @param collapse a [character] to pass on to `paste(, collapse = ...)` to only return one [character] per element of `text`, see *Examples*
#' @param translate_ab if `type = "drug"`: a column name of the [antibiotics] data set to translate the antibiotic abbreviations to, using [ab_property()]. The default is `FALSE`. Using `TRUE` is equal to using "name".
2021-05-12 18:15:03 +02:00
#' @param thorough_search a [logical] to indicate whether the input must be extensively searched for misspelling and other faulty input values. Setting this to `TRUE` will take considerably more time than when using `FALSE`. At default, it will turn `TRUE` when all input elements contain a maximum of three words.
#' @param info a [logical] to indicate whether a progress bar should be printed - the default is `TRUE` only in interactive mode
2020-12-22 00:51:17 +01:00
#' @param ... arguments passed on to [as.ab()]
2022-11-13 13:44:25 +01:00
#' @details This function is also internally used by [as.ab()], although it then only searches for the first drug name and will throw a note if more drug names could have been returned. Note: the [as.ab()] function may use very long regular expression to match brand names of antimicrobial drugs. This may fail on some systems.
2022-08-28 10:31:50 +02:00
#'
2022-10-10 15:44:59 +02:00
#' ### Argument `type`
#' At default, the function will search for antimicrobial drug names. All text elements will be searched for official names, ATC codes and brand names. As it uses [as.ab()] internally, it will correct for misspelling.
2022-08-28 10:31:50 +02:00
#'
2021-05-12 18:15:03 +02:00
#' With `type = "dose"` (or similar, like "dosing", "doses"), all text elements will be searched for [numeric] values that are higher than 100 and do not resemble years. The output will be [numeric]. It supports any unit (g, mg, IE, etc.) and multiple values in one clinical text, see *Examples*.
2022-08-28 10:31:50 +02:00
#'
#' With `type = "administration"` (or abbreviations, like "admin", "adm"), all text elements will be searched for a form of drug administration. It supports the following forms (including common abbreviations): buccal, implant, inhalation, instillation, intravenous, nasal, oral, parenteral, rectal, sublingual, transdermal and vaginal. Abbreviations for oral (such as 'po', 'per os') will become "oral", all values for intravenous (such as 'iv', 'intraven') will become "iv". It supports multiple values in one clinical text, see *Examples*.
2022-08-28 10:31:50 +02:00
#'
2022-10-10 15:44:59 +02:00
#' ### Argument `collapse`
#' Without using `collapse`, this function will return a [list]. This can be convenient to use e.g. inside a `mutate()`):\cr
2022-08-28 10:31:50 +02:00
#' `df %>% mutate(abx = ab_from_text(clinical_text))`
#'
2020-12-22 00:51:17 +01:00
#' The returned AB codes can be transformed to official names, groups, etc. with all [`ab_*`][ab_property()] functions such as [ab_name()] and [ab_group()], or by using the `translate_ab` argument.
2022-08-28 10:31:50 +02:00
#'
#' With using `collapse`, this function will return a [character]:\cr
2022-08-28 10:31:50 +02:00
#' `df %>% mutate(abx = ab_from_text(clinical_text, collapse = "|"))`
2020-06-25 19:20:50 +02:00
#' @export
#' @return A [list], or a [character] if `collapse` is not `NULL`
2022-08-28 10:31:50 +02:00
#' @examples
#' # mind the bad spelling of amoxicillin in this line,
2020-06-25 17:34:50 +02:00
#' # straight from a true health care record:
#' ab_from_text("28/03/2020 regular amoxicilliin 500mg po tid")
2022-08-28 10:31:50 +02:00
#'
#' ab_from_text("500 mg amoxi po and 400mg cipro iv")
#' ab_from_text("500 mg amoxi po and 400mg cipro iv", type = "dose")
#' ab_from_text("500 mg amoxi po and 400mg cipro iv", type = "admin")
2022-08-28 10:31:50 +02:00
#'
#' ab_from_text("500 mg amoxi po and 400mg cipro iv", collapse = ", ")
2020-12-09 09:40:50 +01:00
#' \donttest{
#' # if you want to know which antibiotic groups were administered, do e.g.:
#' abx <- ab_from_text("500 mg amoxi po and 400mg cipro iv")
#' ab_group(abx[[1]])
2022-08-28 10:31:50 +02:00
#'
2020-07-31 10:50:08 +02:00
#' if (require("dplyr")) {
2022-08-28 10:31:50 +02:00
#' tibble(clinical_text = c(
#' "given 400mg cipro and 500 mg amox",
#' "started on doxy iv today"
#' )) %>%
#' mutate(
#' abx_codes = ab_from_text(clinical_text),
#' abx_doses = ab_from_text(clinical_text, type = "doses"),
#' abx_admin = ab_from_text(clinical_text, type = "admin"),
#' abx_coll = ab_from_text(clinical_text, collapse = "|"),
#' abx_coll_names = ab_from_text(clinical_text,
#' collapse = "|",
#' translate_ab = "name"
#' ),
#' abx_coll_doses = ab_from_text(clinical_text,
#' type = "doses",
#' collapse = "|"
#' ),
#' abx_coll_admin = ab_from_text(clinical_text,
#' type = "admin",
#' collapse = "|"
#' )
#' )
#' }
2020-12-09 09:40:50 +01:00
#' }
ab_from_text <- function(text,
type = c("drug", "dose", "administration"),
collapse = NULL,
translate_ab = FALSE,
thorough_search = NULL,
info = interactive(),
...) {
if (missing(type)) {
type <- type[1L]
2020-06-25 17:34:50 +02:00
}
2022-08-28 10:31:50 +02:00
meet_criteria(text)
meet_criteria(type, allow_class = "character", has_length = 1)
meet_criteria(collapse, has_length = 1, allow_NULL = TRUE)
meet_criteria(translate_ab, allow_NULL = FALSE) # get_translate_ab() will be more informative about what's allowed
meet_criteria(thorough_search, allow_class = "logical", has_length = 1, allow_NULL = TRUE)
meet_criteria(info, allow_class = "logical", has_length = 1)
2022-10-05 09:12:22 +02:00
type <- tolower(trimws2(type))
2022-08-28 10:31:50 +02:00
text <- tolower(as.character(text))
text_split_all <- strsplit(text, "[ ;.,:\\|]")
progress <- progress_ticker(n = length(text_split_all), n_min = 5, print = info)
on.exit(close(progress))
2022-08-28 10:31:50 +02:00
if (type %like% "(drug|ab|anti)") {
translate_ab <- get_translate_ab(translate_ab)
2022-08-28 10:31:50 +02:00
2022-10-05 09:12:22 +02:00
if (isTRUE(thorough_search) ||
(isTRUE(is.null(thorough_search)) && max(vapply(FUN.VALUE = double(1), text_split_all, length), na.rm = TRUE) <= 3)) {
text_split_all <- text_split_all[nchar(text_split_all) >= 4 & grepl("[a-z]+", text_split_all)]
result <- lapply(text_split_all, function(text_split) {
progress$tick()
text_split <- text_split[text_split %like% "[A-Z]" & text_split %unlike% "[0-9]"]
if (length(text_split) == 0) {
return(as.ab(NA_character_))
}
suppressWarnings(
2022-10-05 09:12:22 +02:00
as.ab(text_split, ...)
)
})
} else {
# no thorough search
2022-10-05 09:12:22 +02:00
abbr <- unlist(AMR::antibiotics$abbreviations)
abbr <- abbr[nchar(abbr) >= 4]
2022-10-05 09:12:22 +02:00
names_atc <- substr(c(AMR::antibiotics$name, AMR::antibiotics$atc), 1, 5)
synonyms <- unlist(AMR::antibiotics$synonyms)
synonyms <- synonyms[nchar(synonyms) >= 4]
# regular expression must not be too long, so split synonyms in two:
synonyms_part1 <- synonyms[seq_len(0.5 * length(synonyms))]
synonyms_part2 <- synonyms[!synonyms %in% synonyms_part1]
to_regex <- function(x) {
2022-08-28 10:31:50 +02:00
paste0(
"^(",
paste0(unique(gsub("[^a-z0-9]+", "", sort(tolower(x)))), collapse = "|"),
").*"
)
}
result <- lapply(text_split_all, function(text_split) {
progress$tick()
suppressWarnings(
2022-10-05 09:12:22 +02:00
as.ab(
2022-08-28 10:31:50 +02:00
unique(c(
text_split[text_split %like_case% to_regex(abbr)],
text_split[text_split %like_case% to_regex(names_atc)],
text_split[text_split %like_case% to_regex(synonyms_part1)],
text_split[text_split %like_case% to_regex(synonyms_part2)]
)),
...
)
)
})
}
2022-08-28 10:31:50 +02:00
close(progress)
2022-08-28 10:31:50 +02:00
result <- lapply(result, function(out) {
out <- out[!is.na(out)]
if (length(out) == 0) {
as.ab(NA)
} else {
if (!isFALSE(translate_ab)) {
out <- ab_property(out, property = translate_ab, initial_search = FALSE)
}
out
}
})
} else if (type %like% "dos") {
2022-10-05 09:12:22 +02:00
text_split_all <- strsplit(text, " ", fixed = TRUE)
result <- lapply(text_split_all, function(text_split) {
text_split <- text_split[text_split %like% "^[0-9]{2,}(/[0-9]+)?[a-z]*$"]
# only left part of "/", like 500 in "500/125"
2022-08-28 10:31:50 +02:00
text_split <- gsub("/.*", "", text_split)
text_split <- gsub(",", ".", text_split, fixed = TRUE) # foreign system using comma as decimal sep
text_split <- as.double(gsub("[^0-9.]", "", text_split))
# minimal 100 units/mg and no years that unlikely doses
text_split <- text_split[text_split >= 100 & !text_split %in% c(1951:1999, 2001:2049)]
2022-08-28 10:31:50 +02:00
if (length(text_split) > 0) {
text_split
} else {
NA_real_
}
})
} else if (type %like% "adm") {
result <- lapply(text_split_all, function(text_split) {
text_split <- text_split[text_split %like% "(^iv$|intraven|^po$|per os|oral|implant|inhal|instill|nasal|paren|rectal|sublingual|buccal|trans.*dermal|vaginal)"]
if (length(text_split) > 0) {
text_split <- gsub("(^po$|.*per os.*)", "oral", text_split)
text_split <- gsub("(^iv$|.*intraven.*)", "iv", text_split)
text_split
} else {
NA_character_
}
})
} else {
stop_("`type` must be either 'drug', 'dose' or 'administration'")
}
2022-08-28 10:31:50 +02:00
# collapse text if needed
2020-06-25 17:34:50 +02:00
if (!is.null(collapse)) {
result <- vapply(FUN.VALUE = character(1), result, function(x) {
if (length(x) == 1 & all(is.na(x))) {
NA_character_
} else {
paste0(x, collapse = collapse)
}
})
2020-06-25 17:34:50 +02:00
}
2022-08-28 10:31:50 +02:00
2020-06-25 17:34:50 +02:00
result
}