AMR/inst/tinytest/test-rsi.R

250 lines
7.8 KiB
R
Raw Normal View History

2021-05-15 21:36:22 +02:00
# ==================================================================== #
# TITLE #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
2021-05-15 21:36:22 +02:00
# #
# SOURCE #
# https://github.com/msberends/AMR #
# #
2022-10-05 09:12:22 +02:00
# CITE AS #
# Berends MS, Luz CF, Friedrich AW, Sinha BNM, Albers CJ, Glasner C #
# (2022). AMR: An R Package for Working with Antimicrobial Resistance #
# Data. Journal of Statistical Software, 104(3), 1-31. #
# doi:10.18637/jss.v104.i03 #
# #
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. #
2021-05-15 21:36:22 +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. #
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
2023-01-21 23:47:20 +01:00
# we must only have EUCAST and CLSI, because otherwise the rules in as.sir() will fail
2022-08-28 10:31:50 +02:00
expect_identical(
2023-01-21 23:47:20 +01:00
unique(gsub("[^A-Z]", "", AMR::clinical_breakpoints$guideline)),
2022-08-28 10:31:50 +02:00
c("EUCAST", "CLSI")
)
2022-05-10 17:01:37 +02:00
2023-01-21 23:47:20 +01:00
expect_true(as.sir("S") < as.sir("I"))
expect_true(as.sir("I") < as.sir("R"))
expect_true(is.sir(as.sir("S")))
2021-05-15 21:36:22 +02:00
x <- example_isolates$AMX
2023-01-21 23:47:20 +01:00
expect_inherits(x[1], "sir")
expect_inherits(x[[1]], "sir")
expect_inherits(c(x[1], x[9]), "sir")
expect_inherits(unique(x[1], x[9]), "sir")
2021-05-15 21:36:22 +02:00
pdf(NULL) # prevent Rplots.pdf being created
2023-01-21 23:47:20 +01:00
expect_silent(barplot(as.sir(c("S", "I", "R"))))
expect_silent(plot(as.sir(c("S", "I", "R"))))
2021-07-12 20:24:49 +02:00
if (AMR:::pkg_is_available("ggplot2")) {
2023-02-18 14:56:06 +01:00
expect_inherits(ggplot2::autoplot(as.sir(c("S", "I", "R"))), "gg")
2021-07-12 20:24:49 +02:00
}
2023-01-21 23:47:20 +01:00
expect_stdout(print(as.sir(c("S", "I", "R"))))
expect_equal(as.character(as.sir(c(1:3))), c("S", "I", "R"))
expect_equal(as.character(as.sir(c(1:3))), c("S", "I", "R"))
expect_equal(suppressWarnings(as.logical(as.sir("INVALID VALUE"))), NA)
2022-08-28 10:31:50 +02:00
expect_equal(
2023-01-21 23:47:20 +01:00
summary(as.sir(c("S", "R"))),
2022-08-28 10:31:50 +02:00
structure(c(
2023-01-21 23:47:20 +01:00
"Class" = "sir",
2022-08-28 10:31:50 +02:00
"%R" = "50.0% (n=1)",
"%SI" = "50.0% (n=1)",
"- %S" = "50.0% (n=1)",
"- %I" = " 0.0% (n=0)"
), class = c("summaryDefault", "table"))
)
expect_identical(
2023-01-21 23:47:20 +01:00
as.logical(lapply(example_isolates, is_sir_eligible)),
as.logical(lapply(example_isolates, is.sir))
2022-08-28 10:31:50 +02:00
)
2023-01-21 23:47:20 +01:00
expect_error(as.sir.mic(as.mic(16)))
expect_error(as.sir.disk(as.disk(16)))
2021-05-15 21:36:22 +02:00
expect_error(get_guideline("this one does not exist"))
2023-02-18 14:56:06 +01:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
2023-01-21 23:47:20 +01:00
# 40 sir columns
2022-08-28 10:31:50 +02:00
expect_equal(
example_isolates %>%
mutate_at(vars(PEN:RIF), as.character) %>%
2023-01-21 23:47:20 +01:00
lapply(is_sir_eligible) %>%
2022-08-28 10:31:50 +02:00
as.logical() %>%
sum(),
40
)
2023-01-21 23:47:20 +01:00
expect_equal(sum(is.sir(example_isolates)), 40)
2022-08-28 10:31:50 +02:00
2023-01-21 23:47:20 +01:00
expect_stdout(print(tibble(ab = as.sir("S"))))
2022-11-17 15:23:29 +01:00
expect_true(example_isolates %>%
select(AMC, MEM) %>%
2023-01-21 23:47:20 +01:00
mutate(MEM = as.sir(ifelse(AMC == "S", "S", MEM))) %>%
2022-11-17 15:23:29 +01:00
pull(MEM) %>%
2023-01-21 23:47:20 +01:00
is.sir())
2021-05-15 21:36:22 +02:00
}
2023-02-18 14:56:06 +01:00
if (AMR:::pkg_is_available("skimr", min_version = "2.0.0", also_load = TRUE)) {
2022-08-28 10:31:50 +02:00
expect_inherits(
skim(example_isolates),
"data.frame"
)
2023-02-18 14:56:06 +01:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
2022-08-28 10:31:50 +02:00
expect_inherits(
example_isolates %>%
mutate(
m = as.mic(2),
d = as.disk(20)
) %>%
skim(),
"data.frame"
)
2021-05-15 21:36:22 +02:00
}
}
2023-01-21 23:47:20 +01:00
expect_equal(as.sir(c("", "-", NA, "NULL")), c(NA_sir_, NA_sir_, NA_sir_, NA_sir_))
2021-05-15 21:36:22 +02:00
# S. pneumoniae/ampicillin in EUCAST 2020: 0.5-2 ug/ml (R is only > 2)
2022-10-29 14:15:23 +02:00
expect_equal(suppressMessages(
2022-08-28 10:31:50 +02:00
as.character(
2023-01-21 23:47:20 +01:00
as.sir(
2022-08-28 10:31:50 +02:00
x = as.mic(c(0.125, 0.5, 1, 2, 4)),
mo = "B_STRPT_PNMN",
ab = "AMP",
guideline = "EUCAST 2020"
)
2022-10-29 14:15:23 +02:00
)),
2022-08-28 10:31:50 +02:00
c("S", "S", "I", "I", "R")
)
2021-05-15 21:36:22 +02:00
# S. pneumoniae/amoxicillin in CLSI 2019: 2-8 ug/ml (R is 8 and > 8)
2022-10-29 14:15:23 +02:00
expect_equal(suppressMessages(
2022-08-28 10:31:50 +02:00
as.character(
2023-01-21 23:47:20 +01:00
as.sir(
2022-08-28 10:31:50 +02:00
x = as.mic(c(1, 2, 4, 8, 16)),
mo = "B_STRPT_PNMN",
ab = "AMX",
guideline = "CLSI 2019"
)
2022-10-29 14:15:23 +02:00
)),
2022-08-28 10:31:50 +02:00
c("S", "S", "I", "R", "R")
)
2021-05-15 21:36:22 +02:00
2023-01-21 23:47:20 +01:00
expect_true(is.data.frame(sir_interpretation_history(clean = FALSE)))
expect_true(is.data.frame(sir_interpretation_history(clean = TRUE)))
expect_true(is.null(sir_interpretation_history()))
2022-09-01 15:20:57 +02:00
2021-05-15 21:36:22 +02:00
# cutoffs at MIC = 8
2022-08-28 10:31:50 +02:00
expect_equal(
2023-01-21 23:47:20 +01:00
suppressMessages(as.sir(as.mic(2), "E. coli", "ampicillin", guideline = "EUCAST 2020")),
as.sir("S")
2022-08-28 10:31:50 +02:00
)
expect_equal(
2023-01-21 23:47:20 +01:00
suppressMessages(as.sir(as.mic(32), "E. coli", "ampicillin", guideline = "EUCAST 2020")),
as.sir("R")
2022-08-28 10:31:50 +02:00
)
2023-02-18 14:56:06 +01:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
2021-05-15 21:36:22 +02:00
expect_true(suppressWarnings(example_isolates %>%
2022-08-28 10:31:50 +02:00
mutate(amox_mic = as.mic(2)) %>%
select(mo, amox_mic) %>%
2023-01-21 23:47:20 +01:00
as.sir() %>%
2022-08-28 10:31:50 +02:00
pull(amox_mic) %>%
2023-01-21 23:47:20 +01:00
is.sir()))
2021-05-15 21:36:22 +02:00
}
2022-08-28 10:31:50 +02:00
expect_equal(
as.character(
2023-01-21 23:47:20 +01:00
as.sir(
2022-08-28 10:31:50 +02:00
x = as.disk(22),
mo = "B_STRPT_PNMN",
ab = "ERY",
guideline = "CLSI"
)
),
"S"
)
expect_equal(
as.character(
2023-01-21 23:47:20 +01:00
as.sir(
2022-08-28 10:31:50 +02:00
x = as.disk(18),
mo = "B_STRPT_PNMN",
ab = "ERY",
guideline = "CLSI"
)
),
"I"
)
expect_equal(
as.character(
2023-01-21 23:47:20 +01:00
as.sir(
2022-08-28 10:31:50 +02:00
x = as.disk(10),
mo = "B_STRPT_PNMN",
ab = "ERY",
guideline = "CLSI"
)
),
"R"
)
2023-02-18 14:56:06 +01:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
2021-05-15 21:36:22 +02:00
expect_true(example_isolates %>%
2022-08-28 10:31:50 +02:00
mutate(amox_disk = as.disk(15)) %>%
select(mo, amox_disk) %>%
2023-01-21 23:47:20 +01:00
as.sir(guideline = "CLSI") %>%
2022-08-28 10:31:50 +02:00
pull(amox_disk) %>%
2023-01-21 23:47:20 +01:00
is.sir())
2023-01-30 17:24:03 +01:00
# used by group_by() on sir_calc_df(), check some internals to see if grouped calculation without tidyverse works
groups <- example_isolates %>%
group_by(mo) %>%
attributes() %>%
.$groups
expect_equal(nrow(groups),
90)
expect_equal(class(groups$.rows),
c("vctrs_list_of", "vctrs_vctr", "list"))
expect_equal(groups$.rows[[1]],
c(101, 524, 1368))
expect_equal(example_isolates[c(101, 524, 1368), "mo", drop = TRUE],
rep(groups$mo[1], 3))
2021-05-15 21:36:22 +02:00
}
# frequency tables
2021-05-21 20:20:51 +02:00
if (AMR:::pkg_is_available("cleaner")) {
2021-05-15 21:36:22 +02:00
expect_inherits(cleaner::freq(example_isolates$AMX), "freq")
}
2022-08-28 10:31:50 +02:00
df <- data.frame(
microorganism = "Escherichia coli",
AMP = as.mic(8),
CIP = as.mic(0.256),
GEN = as.disk(18),
TOB = as.disk(16),
ERY = "R", # note about assigning <rsi> class
CLR = "V"
) # note about cleaning
expect_inherits(
2023-01-21 23:47:20 +01:00
suppressWarnings(as.sir(df)),
2022-08-28 10:31:50 +02:00
"data.frame"
)
expect_inherits(
2023-01-21 23:47:20 +01:00
suppressWarnings(as.sir(data.frame(
2022-08-28 10:31:50 +02:00
mo = "Escherichia coli",
2023-01-21 23:47:20 +01:00
amoxi = c("S", "I", "R", "invalid")
2022-08-28 10:31:50 +02:00
))$amoxi),
2023-01-21 23:47:20 +01:00
"sir"
2022-08-28 10:31:50 +02:00
)
2023-02-14 15:59:40 +01:00
# expect_warning(as.sir(data.frame(mo = "E. coli", NIT = c("<= 2", 32))))
2023-01-21 23:47:20 +01:00
expect_message(as.sir(data.frame(
2022-08-28 10:31:50 +02:00
mo = "E. coli",
NIT = c("<= 2", 32),
uti = TRUE
)))
2023-01-21 23:47:20 +01:00
expect_message(as.sir(data.frame(
2022-08-28 10:31:50 +02:00
mo = "E. coli",
NIT = c("<= 2", 32),
specimen = c("urine", "blood")
)))