1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-28 21:26:12 +01:00
AMR/R/mic.R

327 lines
12 KiB
R
Raw Normal View History

2018-02-21 11:52:31 +01:00
# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
2018-02-21 11:52:31 +01:00
# #
# LICENCE #
2019-01-02 23:24:07 +01:00
# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
2018-02-21 11:52:31 +01:00
# #
2019-01-02 23:24:07 +01: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. #
# #
# 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. #
2019-04-05 18:47:39 +02:00
# Visit our website for more info: https://msberends.gitlab.io/AMR. #
2018-02-21 11:52:31 +01:00
# ==================================================================== #
#' Class 'mic'
#'
#' This transforms a vector to a new class \code{mic}, which is an ordered factor with valid MIC values as levels. Invalid MIC values will be translated as \code{NA} with a warning.
2018-02-21 11:52:31 +01:00
#' @rdname as.mic
#' @param x vector
#' @param na.rm a logical indicating whether missing values should be removed
2019-05-10 16:44:59 +02:00
#' @details Interpret MIC values as RSI values with \code{\link{as.rsi}}. It supports guidelines from EUCAST and CLSI.
2018-10-17 17:32:34 +02:00
#' @return Ordered factor with new class \code{mic}
2018-07-13 17:23:46 +02:00
#' @keywords mic
2018-02-21 11:52:31 +01:00
#' @export
#' @importFrom dplyr %>%
#' @seealso \code{\link{as.rsi}}
2019-01-02 23:24:07 +01:00
#' @inheritSection AMR Read more on our website!
2018-02-22 20:48:48 +01:00
#' @examples
#' mic_data <- as.mic(c(">=32", "1.0", "1", "1.00", 8, "<=0.128", "8", "16", "16"))
#' is.mic(mic_data)
2018-04-02 16:05:09 +02:00
#'
2018-06-19 10:05:38 +02:00
#' # this can also coerce combined MIC/RSI values:
#' as.mic("<=0.002; S") # will return <=0.002
2018-06-19 10:05:38 +02:00
#'
2019-05-10 16:44:59 +02:00
#' # interpret MIC values
#' as.rsi(x = as.mic(2),
#' mo = as.mo("S. pneumoniae"),
#' ab = "AMX",
#' guideline = "EUCAST")
#' as.rsi(x = as.mic(4),
#' mo = as.mo("S. pneumoniae"),
#' ab = "AMX",
#' guideline = "EUCAST")
#'
2018-02-22 20:48:48 +01:00
#' plot(mic_data)
2018-03-13 14:34:10 +01:00
#' barplot(mic_data)
2019-07-29 17:34:57 +02:00
#'
#' library(clean)
2018-08-01 22:37:28 +02:00
#' freq(mic_data)
2018-02-21 11:52:31 +01:00
as.mic <- function(x, na.rm = FALSE) {
if (is.mic(x)) {
x
} else {
x <- x %>% unlist()
if (na.rm == TRUE) {
x <- x[!is.na(x)]
}
x.bak <- x
2018-04-02 16:05:09 +02:00
2018-06-19 10:05:38 +02:00
# comma to period
2019-10-11 17:21:02 +02:00
x <- gsub(",", ".", x, fixed = TRUE)
2018-06-19 10:05:38 +02:00
# remove space between operator and number ("<= 0.002" -> "<=0.002")
2019-10-11 17:21:02 +02:00
x <- gsub("(<|=|>) +", "\\1", x)
# transform => to >= and =< to <=
2019-10-11 17:21:02 +02:00
x <- gsub("=>", ">=", x, fixed = TRUE)
x <- gsub("=<", "<=", x, fixed = TRUE)
2018-02-21 11:52:31 +01:00
# starting dots must start with 0
2019-10-11 17:21:02 +02:00
x <- gsub("^[.]+", "0.", x)
2018-02-21 11:52:31 +01:00
# <=0.2560.512 should be 0.512
2019-10-11 17:21:02 +02:00
x <- gsub(".*[.].*[.]", "0.", x)
2018-02-21 11:52:31 +01:00
# remove ending .0
2019-10-11 17:21:02 +02:00
x <- gsub("[.]+0$", "", x)
2018-02-21 11:52:31 +01:00
# remove all after last digit
2019-10-11 17:21:02 +02:00
x <- gsub("[^0-9]+$", "", x)
# keep only one zero before dot
x <- gsub("0+[.]", "0.", x)
# starting 00 is probably 0.0 if there's no dot yet
x[!x %like% "[.]"] <- gsub("^00", "0.0", x[!x %like% "[.]"])
2018-02-21 11:52:31 +01:00
# remove last zeroes
2019-10-11 17:21:02 +02:00
x <- gsub("([.].?)0+$", "\\1", x)
x <- gsub("(.*[.])0+$", "\\10", x)
2018-12-29 22:24:19 +01:00
# remove ending .0 again
2019-10-11 17:21:02 +02:00
x[x %like% "[.]"] <- gsub("0+$", "", x[x %like% "[.]"])
2018-06-19 10:05:38 +02:00
# force to be character
x <- as.character(x)
# trim it
x <- trimws(x)
2018-04-02 16:05:09 +02:00
2019-05-10 16:44:59 +02:00
## previously unempty values now empty - should return a warning later on
x[x.bak != "" & x == ""] <- "invalid"
2019-05-10 16:44:59 +02:00
# these are allowed MIC values and will become factor levels
lvls <- c("<0.001", "<=0.001", "0.001", ">=0.001", ">0.001",
"<0.002", "<=0.002", "0.002", ">=0.002", ">0.002",
2018-02-21 11:52:31 +01:00
"<0.003", "<=0.003", "0.003", ">=0.003", ">0.003",
"<0.004", "<=0.004", "0.004", ">=0.004", ">0.004",
"<0.005", "<=0.005", "0.005", ">=0.005", ">0.005",
2018-02-21 11:52:31 +01:00
"<0.006", "<=0.006", "0.006", ">=0.006", ">0.006",
"<0.007", "<=0.007", "0.007", ">=0.007", ">0.007",
2018-02-21 11:52:31 +01:00
"<0.008", "<=0.008", "0.008", ">=0.008", ">0.008",
"<0.009", "<=0.009", "0.009", ">=0.009", ">0.009",
"<0.01", "<=0.01", "0.01", ">=0.01", ">0.01",
2018-02-21 11:52:31 +01:00
"<0.012", "<=0.012", "0.012", ">=0.012", ">0.012",
2018-07-28 10:48:27 +02:00
"<0.0125", "<=0.0125", "0.0125", ">=0.0125", ">0.0125",
2018-02-21 11:52:31 +01:00
"<0.016", "<=0.016", "0.016", ">=0.016", ">0.016",
"<0.019", "<=0.019", "0.019", ">=0.019", ">0.019",
"<0.02", "<=0.02", "0.02", ">=0.02", ">0.02",
2018-02-21 11:52:31 +01:00
"<0.023", "<=0.023", "0.023", ">=0.023", ">0.023",
2018-03-13 11:57:30 +01:00
"<0.025", "<=0.025", "0.025", ">=0.025", ">0.025",
"<0.028", "<=0.028", "0.028", ">=0.028", ">0.028",
2018-02-21 11:52:31 +01:00
"<0.03", "<=0.03", "0.03", ">=0.03", ">0.03",
"<0.031", "<=0.031", "0.031", ">=0.031", ">0.031",
2018-02-21 11:52:31 +01:00
"<0.032", "<=0.032", "0.032", ">=0.032", ">0.032",
"<0.038", "<=0.038", "0.038", ">=0.038", ">0.038",
"<0.04", "<=0.04", "0.04", ">=0.04", ">0.04",
2018-02-21 11:52:31 +01:00
"<0.047", "<=0.047", "0.047", ">=0.047", ">0.047",
"<0.05", "<=0.05", "0.05", ">=0.05", ">0.05",
2018-07-28 10:48:27 +02:00
"<0.054", "<=0.054", "0.054", ">=0.054", ">0.054",
2018-02-21 11:52:31 +01:00
"<0.06", "<=0.06", "0.06", ">=0.06", ">0.06",
"<0.0625", "<=0.0625", "0.0625", ">=0.0625", ">0.0625",
2018-03-13 11:57:30 +01:00
"<0.063", "<=0.063", "0.063", ">=0.063", ">0.063",
2018-02-21 11:52:31 +01:00
"<0.064", "<=0.064", "0.064", ">=0.064", ">0.064",
"<0.075", "<=0.075", "0.075", ">=0.075", ">0.075",
"<0.08", "<=0.08", "0.08", ">=0.08", ">0.08",
2018-02-21 11:52:31 +01:00
"<0.09", "<=0.09", "0.09", ">=0.09", ">0.09",
"<0.094", "<=0.094", "0.094", ">=0.094", ">0.094",
"<0.095", "<=0.095", "0.095", ">=0.095", ">0.095",
"<0.1", "<=0.1", "0.1", ">=0.1", ">0.1",
2018-02-21 11:52:31 +01:00
"<0.12", "<=0.12", "0.12", ">=0.12", ">0.12",
"<0.125", "<=0.125", "0.125", ">=0.125", ">0.125",
"<0.128", "<=0.128", "0.128", ">=0.128", ">0.128",
"<0.15", "<=0.15", "0.15", ">=0.15", ">0.15",
2018-03-13 11:57:30 +01:00
"<0.16", "<=0.16", "0.16", ">=0.16", ">0.16",
"<0.17", "<=0.17", "0.17", ">=0.17", ">0.17",
"<0.18", "<=0.18", "0.18", ">=0.18", ">0.18",
2018-02-21 11:52:31 +01:00
"<0.19", "<=0.19", "0.19", ">=0.19", ">0.19",
"<0.2", "<=0.2", "0.2", ">=0.2", ">0.2",
2018-07-28 10:48:27 +02:00
"<0.23", "<=0.23", "0.23", ">=0.23", ">0.23",
2018-02-21 11:52:31 +01:00
"<0.25", "<=0.25", "0.25", ">=0.25", ">0.25",
"<0.256", "<=0.256", "0.256", ">=0.256", ">0.256",
2018-07-28 10:48:27 +02:00
"<0.28", "<=0.28", "0.28", ">=0.28", ">0.28",
"<0.3", "<=0.3", "0.3", ">=0.3", ">0.3",
2018-03-13 11:57:30 +01:00
"<0.32", "<=0.32", "0.32", ">=0.32", ">0.32",
"<0.35", "<=0.35", "0.35", ">=0.35", ">0.35",
2018-07-28 10:48:27 +02:00
"<0.36", "<=0.36", "0.36", ">=0.36", ">0.36",
2018-02-21 11:52:31 +01:00
"<0.38", "<=0.38", "0.38", ">=0.38", ">0.38",
"<0.47", "<=0.47", "0.47", ">=0.47", ">0.47",
2018-02-21 11:52:31 +01:00
"<0.5", "<=0.5", "0.5", ">=0.5", ">0.5",
"<0.512", "<=0.512", "0.512", ">=0.512", ">0.512",
2018-03-13 11:57:30 +01:00
"<0.64", "<=0.64", "0.64", ">=0.64", ">0.64",
"<0.73", "<=0.73", "0.73", ">=0.73", ">0.73",
2018-02-21 11:52:31 +01:00
"<0.75", "<=0.75", "0.75", ">=0.75", ">0.75",
"<0.8", "<=0.8", "0.8", ">=0.8", ">0.8",
"<0.94", "<=0.94", "0.94", ">=0.94", ">0.94",
2018-02-21 11:52:31 +01:00
"<1", "<=1", "1", ">=1", ">1",
"<1.5", "<=1.5", "1.5", ">=1.5", ">1.5",
"<2", "<=2", "2", ">=2", ">2",
"<3", "<=3", "3", ">=3", ">3",
"<4", "<=4", "4", ">=4", ">4",
2018-07-28 10:48:27 +02:00
"<5", "<=5", "5", ">=5", ">5",
2018-02-21 11:52:31 +01:00
"<6", "<=6", "6", ">=6", ">6",
2018-07-28 10:48:27 +02:00
"<7", "<=7", "7", ">=7", ">7",
2018-02-21 11:52:31 +01:00
"<8", "<=8", "8", ">=8", ">8",
"<10", "<=10", "10", ">=10", ">10",
"<12", "<=12", "12", ">=12", ">12",
"<16", "<=16", "16", ">=16", ">16",
"<20", "<=20", "20", ">=20", ">20",
"<24", "<=24", "24", ">=24", ">24",
"<32", "<=32", "32", ">=32", ">32",
"<40", "<=40", "40", ">=40", ">40",
"<48", "<=48", "48", ">=48", ">48",
"<64", "<=64", "64", ">=64", ">64",
"<80", "<=80", "80", ">=80", ">80",
"<96", "<=96", "96", ">=96", ">96",
"<128", "<=128", "128", ">=128", ">128",
2019-05-10 16:44:59 +02:00
"129",
2018-02-21 11:52:31 +01:00
"<160", "<=160", "160", ">=160", ">160",
"<256", "<=256", "256", ">=256", ">256",
2019-05-10 16:44:59 +02:00
"257",
2018-02-21 11:52:31 +01:00
"<320", "<=320", "320", ">=320", ">320",
"<512", "<=512", "512", ">=512", ">512",
2019-05-10 16:44:59 +02:00
"513",
"<1024", "<=1024", "1024", ">=1024", ">1024",
"1025")
2018-04-02 16:05:09 +02:00
2019-10-11 17:21:02 +02:00
na_before <- x[is.na(x) | x == ""] %>% length()
2018-02-21 11:52:31 +01:00
x[!x %in% lvls] <- NA
2019-10-11 17:21:02 +02:00
na_after <- x[is.na(x) | x == ""] %>% length()
2018-04-02 16:05:09 +02:00
2018-02-21 11:52:31 +01:00
if (na_before != na_after) {
2019-10-11 17:21:02 +02:00
list_missing <- x.bak[is.na(x) & !is.na(x.bak) & x.bak != ""] %>%
2018-02-21 11:52:31 +01:00
unique() %>%
sort()
2019-10-11 17:21:02 +02:00
list_missing <- paste0('"', list_missing, '"', collapse = ", ")
warning(na_after - na_before, " results truncated (",
round(((na_after - na_before) / length(x)) * 100),
2019-10-11 17:21:02 +02:00
"%) that were invalid MICs: ",
2018-02-21 11:52:31 +01:00
list_missing, call. = FALSE)
}
2018-04-02 16:05:09 +02:00
2019-08-07 15:37:39 +02:00
structure(.Data = factor(x, levels = lvls, ordered = TRUE),
2019-10-11 17:21:02 +02:00
class = c("mic", "ordered", "factor"))
2018-02-21 11:52:31 +01:00
}
}
#' @rdname as.mic
#' @export
#' @importFrom dplyr %>%
is.mic <- function(x) {
2019-10-11 17:21:02 +02:00
class(x) %>% identical(c("mic", "ordered", "factor"))
2018-02-21 11:52:31 +01:00
}
#' @exportMethod as.double.mic
#' @export
#' @noRd
as.double.mic <- function(x, ...) {
2019-10-11 17:21:02 +02:00
as.double(gsub("(<|=|>)+", "", as.character(x)))
2018-02-21 11:52:31 +01:00
}
#' @exportMethod as.integer.mic
#' @export
#' @noRd
as.integer.mic <- function(x, ...) {
2019-10-11 17:21:02 +02:00
as.integer(gsub("(<|=|>)+", "", as.character(x)))
2018-02-21 11:52:31 +01:00
}
#' @exportMethod as.numeric.mic
#' @export
#' @noRd
as.numeric.mic <- function(x, ...) {
2019-10-11 17:21:02 +02:00
as.numeric(gsub("(<|=|>)+", "", as.character(x)))
2018-02-21 11:52:31 +01:00
}
2018-12-29 22:24:19 +01:00
#' @exportMethod droplevels.mic
#' @export
#' @noRd
2019-10-11 17:21:02 +02:00
droplevels.mic <- function(x, exclude = ifelse(anyNA(levels(x)), NULL, NA), ...) {
2018-12-29 22:24:19 +01:00
x <- droplevels.factor(x, exclude = exclude, ...)
2019-10-11 17:21:02 +02:00
class(x) <- c("mic", "ordered", "factor")
2018-12-29 22:24:19 +01:00
x
}
2018-02-21 11:52:31 +01:00
#' @exportMethod print.mic
#' @export
#' @importFrom dplyr %>% tibble group_by summarise pull
#' @noRd
print.mic <- function(x, ...) {
2018-07-15 22:56:41 +02:00
cat("Class 'mic'\n")
2018-08-01 22:37:28 +02:00
print(as.character(x), quote = FALSE)
2018-02-21 11:52:31 +01:00
}
#' @exportMethod summary.mic
#' @export
2018-04-03 16:07:32 +02:00
#' @importFrom dplyr %>%
2018-02-21 11:52:31 +01:00
#' @noRd
summary.mic <- function(object, ...) {
x <- object
n_total <- x %>% length()
x <- x[!is.na(x)]
n <- x %>% length()
2018-12-07 12:04:55 +01:00
c(
2019-10-11 17:21:02 +02:00
"Class" = "mic",
2018-12-07 12:04:55 +01:00
"<NA>" = n_total - n,
"Min." = sort(x)[1] %>% as.character(),
"Max." = sort(x)[n] %>% as.character()
)
2018-02-21 11:52:31 +01:00
}
#' @exportMethod plot.mic
#' @export
2019-06-16 22:14:43 +02:00
#' @importFrom graphics barplot axis par
2018-02-21 11:52:31 +01:00
#' @noRd
plot.mic <- function(x,
2019-10-11 17:21:02 +02:00
main = paste("MIC values of", deparse(substitute(x))),
ylab = "Frequency",
xlab = "MIC value",
axes = FALSE,
...) {
barplot(table(droplevels.factor(x)),
ylab = ylab,
xlab = xlab,
axes = axes,
main = main,
...)
axis(2, seq(0, max(table(droplevels.factor(x)))))
2018-03-13 14:34:10 +01:00
}
#' @exportMethod barplot.mic
#' @export
2018-03-13 15:40:10 +01:00
#' @importFrom graphics barplot axis
2018-03-13 14:34:10 +01:00
#' @noRd
barplot.mic <- function(height,
2019-10-11 17:21:02 +02:00
main = paste("MIC values of", deparse(substitute(height))),
ylab = "Frequency",
xlab = "MIC value",
axes = FALSE,
...) {
barplot(table(droplevels.factor(height)),
ylab = ylab,
xlab = xlab,
axes = axes,
main = main,
2018-03-13 14:34:10 +01:00
...)
axis(2, seq(0, max(table(droplevels.factor(height)))))
2018-02-21 11:52:31 +01:00
}
2019-08-07 15:37:39 +02:00
#' @importFrom pillar type_sum
#' @export
type_sum.mic <- function(x) {
"mic"
}
#' @importFrom pillar pillar_shaft
#' @export
pillar_shaft.mic <- function(x, ...) {
out <- trimws(format(x))
out[is.na(x)] <- pillar::style_na(NA)
2019-08-07 15:37:39 +02:00
pillar::new_pillar_shaft_simple(out, align = "right", min_width = 4)
}