AMR/inst/tinytest/test-eucast_rules.R

220 lines
6.9 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/ #
# ==================================================================== #
# thoroughly check input table
2022-08-28 10:31:50 +02:00
expect_equal(
colnames(AMR:::EUCAST_RULES_DF),
c(
"if_mo_property", "like.is.one_of", "this_value",
"and_these_antibiotics", "have_these_values",
"then_change_these_antibiotics", "to_value",
"reference.rule", "reference.rule_group",
"reference.version",
"note"
)
)
MOs_mentioned <- unique(AMR:::EUCAST_RULES_DF$this_value)
2022-10-05 09:12:22 +02:00
MOs_mentioned <- sort(trimws(unlist(strsplit(MOs_mentioned[!AMR:::is_valid_regex(MOs_mentioned)], ",", fixed = TRUE))))
MOs_test <- suppressWarnings(suppressMessages(mo_name(MOs_mentioned, keep_synonyms = TRUE, language = NULL)))
2021-05-15 21:36:22 +02:00
expect_true(length(MOs_mentioned[MOs_test != MOs_mentioned]) == 0)
expect_error(suppressWarnings(eucast_rules(example_isolates, col_mo = "Non-existing")))
expect_error(eucast_rules(x = "text"))
expect_error(eucast_rules(data.frame(a = "test")))
expect_error(eucast_rules(data.frame(mo = "test"), rules = "invalid rules set"))
# expect_warning(eucast_rules(data.frame(mo = "Escherichia coli", vancomycin = "S", stringsAsFactors = TRUE)))
2021-05-15 21:36:22 +02:00
2022-08-28 10:31:50 +02:00
expect_identical(
colnames(example_isolates),
colnames(suppressWarnings(eucast_rules(example_isolates, info = FALSE)))
)
2021-05-24 11:01:32 +02:00
2021-05-15 21:36:22 +02:00
expect_stdout(suppressMessages(eucast_rules(example_isolates, info = TRUE)))
2022-08-28 10:31:50 +02:00
a <- data.frame(
mo = c(
"Klebsiella pneumoniae",
"Pseudomonas aeruginosa",
"Enterobacter cloacae"
),
amox = "-", # Amoxicillin
stringsAsFactors = FALSE
)
b <- data.frame(
mo = c(
"Klebsiella pneumoniae",
"Pseudomonas aeruginosa",
"Enterobacter cloacae"
),
amox = "R", # Amoxicillin
stringsAsFactors = FALSE
)
2021-05-15 21:36:22 +02:00
expect_identical(suppressWarnings(eucast_rules(a, "mo", info = FALSE)), b)
expect_stdout(suppressMessages(suppressWarnings(eucast_rules(a, "mo", info = TRUE))))
2022-08-28 10:31:50 +02:00
a <- data.frame(
mo = c(
"Staphylococcus aureus",
"Streptococcus group A"
),
COL = "-", # Colistin
stringsAsFactors = FALSE
)
b <- data.frame(
mo = c(
"Staphylococcus aureus",
"Streptococcus group A"
),
COL = "R", # Colistin
stringsAsFactors = FALSE
)
2021-05-15 21:36:22 +02:00
expect_equal(suppressWarnings(eucast_rules(a, "mo", info = FALSE)), b)
# piperacillin must be R in Enterobacteriaceae when tica is R
2021-10-05 09:58:08 +02:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
2022-08-28 10:31:50 +02:00
expect_equal(
suppressWarnings(
example_isolates %>%
filter(mo_family(mo) == "Enterobacteriaceae") %>%
mutate(
2023-01-21 23:47:20 +01:00
TIC = as.sir("R"),
PIP = as.sir("S")
2022-08-28 10:31:50 +02:00
) %>%
eucast_rules(col_mo = "mo", version_expertrules = 3.1, info = FALSE) %>%
pull(PIP) %>%
unique() %>%
as.character()
),
"R"
)
2021-05-15 21:36:22 +02:00
}
2021-05-24 11:01:32 +02:00
# azithromycin and clarythromycin must be equal to Erythromycin
2023-01-21 23:47:20 +01:00
a <- suppressWarnings(as.sir(eucast_rules(data.frame(
2022-08-28 10:31:50 +02:00
mo = example_isolates$mo,
ERY = example_isolates$ERY,
2023-01-21 23:47:20 +01:00
AZM = as.sir("R"),
2022-08-28 10:31:50 +02:00
CLR = factor("R"),
stringsAsFactors = FALSE
),
version_expertrules = 3.1,
2023-01-21 23:47:20 +01:00
only_sir_columns = FALSE
2022-08-28 10:31:50 +02:00
)$CLR))
2021-05-15 21:36:22 +02:00
b <- example_isolates$ERY
2022-08-28 10:31:50 +02:00
expect_identical(
a[!is.na(b)],
b[!is.na(b)]
)
2021-05-15 21:36:22 +02:00
# amox is inferred by benzylpenicillin in Kingella kingae
expect_equal(
suppressWarnings(
as.list(eucast_rules(
2022-08-28 10:31:50 +02:00
data.frame(
mo = as.mo("Kingella kingae"),
PEN = "S",
AMX = "-",
stringsAsFactors = FALSE
),
info = FALSE
))$AMX
2021-05-15 21:36:22 +02:00
),
2022-08-28 10:31:50 +02:00
"S"
)
2021-05-15 21:36:22 +02:00
# also test norf
2021-10-05 09:58:08 +02:00
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
2021-05-15 21:36:22 +02:00
expect_stdout(suppressWarnings(eucast_rules(example_isolates %>% mutate(NOR = "S", NAL = "S"), info = TRUE)))
}
# check verbose output
expect_stdout(suppressWarnings(eucast_rules(example_isolates, verbose = TRUE, rules = "all", info = TRUE)))
# AmpC de-repressed cephalo mutants
2021-05-24 11:01:32 +02:00
2021-05-15 21:36:22 +02:00
expect_identical(
2022-08-28 10:31:50 +02:00
eucast_rules(data.frame(
mo = c("Escherichia coli", "Enterobacter cloacae"),
2023-01-21 23:47:20 +01:00
cefotax = as.sir(c("S", "S"))
2022-08-28 10:31:50 +02:00
),
ampc_cephalosporin_resistance = TRUE,
info = FALSE
)$cefotax,
2023-01-21 23:47:20 +01:00
as.sir(c("S", "R"))
2022-08-28 10:31:50 +02:00
)
2021-05-24 11:01:32 +02:00
2021-05-15 21:36:22 +02:00
expect_identical(
2022-08-28 10:31:50 +02:00
eucast_rules(data.frame(
mo = c("Escherichia coli", "Enterobacter cloacae"),
2023-01-21 23:47:20 +01:00
cefotax = as.sir(c("S", "S"))
2022-08-28 10:31:50 +02:00
),
ampc_cephalosporin_resistance = NA,
info = FALSE
)$cefotax,
2023-01-21 23:47:20 +01:00
as.sir(c("S", NA))
2022-08-28 10:31:50 +02:00
)
2021-05-24 11:01:32 +02:00
2021-05-15 21:36:22 +02:00
expect_identical(
2022-08-28 10:31:50 +02:00
eucast_rules(data.frame(
mo = c("Escherichia coli", "Enterobacter cloacae"),
2023-01-21 23:47:20 +01:00
cefotax = as.sir(c("S", "S"))
2022-08-28 10:31:50 +02:00
),
ampc_cephalosporin_resistance = NULL,
info = FALSE
)$cefotax,
2023-01-21 23:47:20 +01:00
as.sir(c("S", "S"))
2022-08-28 10:31:50 +02:00
)
2021-05-15 21:36:22 +02:00
# EUCAST dosage -----------------------------------------------------------
expect_equal(nrow(eucast_dosage(c("tobra", "genta", "cipro"))), 3)
expect_inherits(eucast_dosage(c("tobra", "genta", "cipro")), "data.frame")
2022-08-28 10:31:50 +02:00
x <- custom_eucast_rules(
AMC == "R" & genus == "Klebsiella" ~ aminopenicillins == "R",
AMC == "I" & genus == "Klebsiella" ~ aminopenicillins == "I",
AMX == "S" ~ AMC == "S"
)
2021-05-15 21:36:22 +02:00
expect_stdout(print(x))
expect_stdout(print(c(x, x)))
expect_stdout(print(as.list(x, x)))
# this custom rules makes 8 changes
expect_equal(nrow(eucast_rules(example_isolates,
2022-08-28 10:31:50 +02:00
rules = "custom",
custom_rules = x,
info = FALSE,
verbose = TRUE
)),
8,
tolerance = 0.5
)