AMR/R/disk.R

149 lines
4.6 KiB
R
Raw Normal View History

2019-05-10 16:44:59 +02:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# LICENCE #
# (c) 2018-2020 Berends MS, Luz CF et al. #
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. #
2019-05-10 16:44:59 +02:00
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
# ==================================================================== #
#' Class 'disk'
#'
2020-02-17 14:38:01 +01:00
#' This transforms a vector to a new class [`disk`], which is a growth zone size (around an antibiotic disk) in millimetres between 6 and 50.
#' @inheritSection lifecycle Stable lifecycle
2019-05-10 16:44:59 +02:00
#' @rdname as.disk
#' @param x vector
#' @param na.rm a logical indicating whether missing values should be removed
#' @details Interpret disk values as RSI values with [as.rsi()]. It supports guidelines from EUCAST and CLSI.
2019-12-21 10:56:06 +01:00
#' @return An [`integer`] with additional new class [`disk`]
2019-11-06 14:43:23 +01:00
#' @aliases disk
2019-05-10 16:44:59 +02:00
#' @export
#' @seealso [as.rsi()]
2019-05-10 16:44:59 +02:00
#' @inheritSection AMR Read more on our website!
#' @examples
2020-05-16 21:40:50 +02:00
#' \dontrun{
2020-02-17 14:38:01 +01:00
#' # transform existing disk zones to the `disk` class
2020-02-17 15:07:25 +01:00
#' library(dplyr)
2020-02-17 14:38:01 +01:00
#' df <- data.frame(microorganism = "E. coli",
#' AMP = 20,
#' CIP = 14,
#' GEN = 18,
#' TOB = 16)
2020-02-17 15:07:25 +01:00
#' df <- df %>% mutate_at(vars(AMP:TOB), as.disk)
2020-02-17 14:38:01 +01:00
#' df
#'
#' # interpret disk values, see ?as.rsi
#' as.rsi(x = as.disk(18),
#' mo = "Strep pneu", # `mo` will be coerced with as.mo()
#' ab = "ampicillin", # and `ab` with as.ab()
2019-05-10 16:44:59 +02:00
#' guideline = "EUCAST")
2020-02-17 14:38:01 +01:00
#'
#' as.rsi(df)
2020-05-16 21:40:50 +02:00
#' }
2019-05-10 16:44:59 +02:00
as.disk <- function(x, na.rm = FALSE) {
2020-05-19 12:08:49 +02:00
if (!is.disk(x)) {
2019-05-10 16:44:59 +02:00
x <- x %>% unlist()
if (na.rm == TRUE) {
x <- x[!is.na(x)]
}
x.bak <- x
na_before <- length(x[is.na(x)])
# force it to be integer
x <- suppressWarnings(as.integer(x))
2019-12-21 10:56:06 +01:00
# disks can never be less than 6 mm (size of smallest disk) or more than 50 mm
x[x < 6 | x > 50] <- NA_integer_
2019-05-10 16:44:59 +02:00
na_after <- length(x[is.na(x)])
if (na_before != na_after) {
list_missing <- x.bak[is.na(x) & !is.na(x.bak)] %>%
unique() %>%
sort()
2019-10-11 17:21:02 +02:00
list_missing <- paste0('"', list_missing, '"', collapse = ", ")
warning(na_after - na_before, " results truncated (",
2019-05-10 16:44:59 +02:00
round(((na_after - na_before) / length(x)) * 100),
2019-10-11 17:21:02 +02:00
"%) that were invalid disk zones: ",
2019-05-10 16:44:59 +02:00
list_missing, call. = FALSE)
}
}
2020-05-19 12:08:49 +02:00
structure(as.integer(x),
class = c("disk", "integer"))
2019-05-10 16:44:59 +02:00
}
2020-02-20 13:19:23 +01:00
all_valid_disks <- function(x) {
x_disk <- suppressWarnings(as.disk(x[!is.na(x)]))
!any(is.na(x_disk)) & !all(is.na(x))
}
2019-05-10 16:44:59 +02:00
#' @rdname as.disk
#' @export
is.disk <- function(x) {
2020-02-10 14:18:15 +01:00
inherits(x, "disk")
2019-05-10 16:44:59 +02:00
}
#' @exportMethod print.disk
#' @export
#' @noRd
print.disk <- function(x, ...) {
cat("Class 'disk'\n")
print(as.integer(x), quote = FALSE)
}
2020-03-14 14:05:43 +01:00
#' @exportMethod [.disk
#' @export
#' @noRd
"[.disk" <- function(x, ...) {
y <- NextMethod()
attributes(y) <- attributes(x)
y
}
#' @exportMethod [[.disk
#' @export
#' @noRd
"[[.disk" <- function(x, ...) {
y <- NextMethod()
attributes(y) <- attributes(x)
y
}
#' @exportMethod [<-.disk
#' @export
#' @noRd
"[<-.disk" <- function(i, j, ..., value) {
2020-04-13 21:09:56 +02:00
value <- as.disk(value)
2020-03-14 14:05:43 +01:00
y <- NextMethod()
attributes(y) <- attributes(i)
y
}
#' @exportMethod [[<-.disk
#' @export
#' @noRd
"[[<-.disk" <- function(i, j, ..., value) {
2020-04-13 21:09:56 +02:00
value <- as.disk(value)
2020-03-14 14:05:43 +01:00
y <- NextMethod()
attributes(y) <- attributes(i)
y
}
#' @exportMethod c.disk
#' @export
#' @noRd
c.disk <- function(x, ...) {
y <- NextMethod()
2020-04-13 21:09:56 +02:00
y <- as.disk(y)
2020-03-14 14:05:43 +01:00
attributes(y) <- attributes(x)
y
}