AMR/tests/testthat/test-eucast_rules.R

165 lines
7.6 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Data Analysis for R #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
2020-07-08 14:48:06 +02:00
# https://github.com/msberends/AMR #
# #
# LICENCE #
2020-12-27 00:30:28 +01:00
# (c) 2018-2021 Berends MS, Luz CF et al. #
2020-10-08 11:16:03 +02:00
# Developed at the University of Groningen, the Netherlands, in #
# collaboration with non-profit organisations Certe Medical #
# Diagnostics & Advice, and University Medical Center Groningen. #
# #
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. #
# 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/ #
# ==================================================================== #
2018-11-16 20:50:50 +01:00
context("eucast_rules.R")
2018-03-27 17:43:42 +02:00
test_that("EUCAST rules work", {
2019-10-15 14:35:23 +02:00
skip_on_cran()
2020-05-16 13:05:47 +02:00
2019-04-05 18:47:39 +02:00
# thoroughly check input table
2019-06-01 20:54:56 +02:00
expect_equal(colnames(eucast_rules_file),
c("if_mo_property", "like.is.one_of", "this_value",
2019-04-05 18:47:39 +02:00
"and_these_antibiotics", "have_these_values",
"then_change_these_antibiotics", "to_value",
2020-09-19 15:15:57 +02:00
"reference.rule", "reference.rule_group",
2020-09-24 00:30:11 +02:00
"reference.version",
"note"))
MOs_mentioned <- unique(eucast_rules_file$this_value)
MOs_mentioned <- sort(trimws(unlist(strsplit(MOs_mentioned[!is_possibly_regex(MOs_mentioned)], ",", fixed = TRUE))))
MOs_test <- suppressWarnings(suppressMessages(mo_name(MOs_mentioned)))
expect_length(MOs_mentioned[MOs_test != MOs_mentioned], 0)
2020-05-16 13:05:47 +02:00
expect_error(suppressWarnings(eucast_rules(example_isolates, col_mo = "Non-existing")))
2019-05-13 12:21:57 +02:00
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"))
2019-08-07 15:37:39 +02:00
expect_warning(eucast_rules(data.frame(mo = "Escherichia coli", vancomycin = "S", stringsAsFactors = TRUE)))
2020-05-16 13:05:47 +02:00
expect_identical(colnames(example_isolates),
2021-01-18 18:45:43 +01:00
colnames(suppressWarnings(eucast_rules(example_isolates, info = FALSE))))
expect_output(suppressMessages(eucast_rules(example_isolates, info = TRUE)))
2020-05-16 13:05:47 +02:00
2019-02-27 17:39:39 +01:00
a <- data.frame(mo = c("Klebsiella pneumoniae",
"Pseudomonas aeruginosa",
2021-01-18 18:45:43 +01:00
"Enterobacter cloacae"),
2021-03-11 21:42:30 +01:00
amox = "-", # Amoxicillin
2018-03-27 17:43:42 +02:00
stringsAsFactors = FALSE)
2019-02-27 17:39:39 +01:00
b <- data.frame(mo = c("Klebsiella pneumoniae",
"Pseudomonas aeruginosa",
2021-01-18 18:45:43 +01:00
"Enterobacter cloacae"),
2018-10-17 17:32:34 +02:00
amox = "R", # Amoxicillin
2018-03-27 17:43:42 +02:00
stringsAsFactors = FALSE)
2018-11-16 20:50:50 +01:00
expect_identical(suppressWarnings(eucast_rules(a, "mo", info = FALSE)), b)
2021-01-18 18:45:43 +01:00
expect_output(suppressMessages(suppressWarnings(eucast_rules(a, "mo", info = TRUE))))
2020-05-16 13:05:47 +02:00
2019-02-27 17:39:39 +01:00
a <- data.frame(mo = c("Staphylococcus aureus",
"Streptococcus group A"),
2019-05-10 16:44:59 +02:00
COL = "-", # Colistin
2018-03-27 17:43:42 +02:00
stringsAsFactors = FALSE)
2019-02-27 17:39:39 +01:00
b <- data.frame(mo = c("Staphylococcus aureus",
"Streptococcus group A"),
2019-05-10 16:44:59 +02:00
COL = "R", # Colistin
2018-03-27 17:43:42 +02:00
stringsAsFactors = FALSE)
2018-11-16 20:50:50 +01:00
expect_equal(suppressWarnings(eucast_rules(a, "mo", info = FALSE)), b)
2020-05-16 13:05:47 +02:00
2018-11-01 20:23:33 +01:00
# piperacillin must be R in Enterobacteriaceae when tica is R
2020-09-26 16:26:01 +02:00
library(dplyr, warn.conflicts = FALSE)
expect_equal(suppressWarnings(
example_isolates %>%
2021-04-07 08:37:42 +02:00
filter(mo_family(mo) == "Enterobacteriaceae") %>%
2019-05-10 16:44:59 +02:00
mutate(TIC = as.rsi("R"),
PIP = as.rsi("S")) %>%
2021-01-18 18:45:43 +01:00
eucast_rules(col_mo = "mo", version_expertrules = 3.1, info = FALSE) %>%
2019-05-10 16:44:59 +02:00
pull(PIP) %>%
unique() %>%
as.character()),
"R")
2020-05-16 13:05:47 +02:00
2020-09-26 16:26:01 +02:00
# Azithromycin and Clarythromycin must be equal to Erythromycin
a <- suppressWarnings(as.rsi(eucast_rules(data.frame(mo = example_isolates$mo,
ERY = example_isolates$ERY,
AZM = as.rsi("R"),
CLR = factor("R"),
stringsAsFactors = FALSE),
version_expertrules = 3.1,
only_rsi_columns = FALSE)$CLR))
2020-05-16 13:05:47 +02:00
b <- example_isolates$ERY
2018-10-17 17:32:34 +02:00
expect_identical(a[!is.na(b)],
b[!is.na(b)])
2020-05-16 13:05:47 +02:00
2018-10-17 17:32:34 +02:00
# amox is inferred by benzylpenicillin in Kingella kingae
expect_equal(
2019-01-03 23:56:19 +01:00
suppressWarnings(
as.list(eucast_rules(
data.frame(mo = as.mo("Kingella kingae"),
2019-05-10 16:44:59 +02:00
PEN = "S",
AMX = "-",
2019-01-03 23:56:19 +01:00
stringsAsFactors = FALSE)
2019-05-10 16:44:59 +02:00
, info = FALSE))$AMX
2019-01-03 23:56:19 +01:00
),
2018-10-17 17:32:34 +02:00
"S")
2020-05-16 13:05:47 +02:00
2018-11-30 12:05:59 +01:00
# also test norf
expect_output(suppressWarnings(eucast_rules(example_isolates %>% mutate(NOR = "S", NAL = "S"), info = TRUE)))
2020-05-16 13:05:47 +02:00
2018-11-30 12:05:59 +01:00
# check verbose output
2020-12-11 12:17:23 +01:00
expect_output(suppressWarnings(eucast_rules(example_isolates, verbose = TRUE, rules = "all", info = TRUE)))
2020-05-16 13:05:47 +02:00
# AmpC de-repressed cephalo mutants
expect_identical(
eucast_rules(data.frame(mo = c("Escherichia coli", "Enterobacter cloacae"),
cefotax = as.rsi(c("S", "S"))),
2021-03-11 21:42:30 +01:00
ampc_cephalosporin_resistance = TRUE,
info = FALSE)$cefotax,
as.rsi(c("S", "R")))
expect_identical(
eucast_rules(data.frame(mo = c("Escherichia coli", "Enterobacter cloacae"),
cefotax = as.rsi(c("S", "S"))),
2020-12-27 15:07:01 +01:00
ampc_cephalosporin_resistance = NA,
info = FALSE)$cefotax,
as.rsi(c("S", NA)))
expect_identical(
eucast_rules(data.frame(mo = c("Escherichia coli", "Enterobacter cloacae"),
cefotax = as.rsi(c("S", "S"))),
2020-12-27 15:07:01 +01:00
ampc_cephalosporin_resistance = NULL,
info = FALSE)$cefotax,
as.rsi(c("S", "S")))
# EUCAST dosage -----------------------------------------------------------
expect_equal(nrow(eucast_dosage(c("tobra", "genta", "cipro"))), 3)
expect_s3_class(eucast_dosage(c("tobra", "genta", "cipro")), "data.frame")
2018-03-27 17:43:42 +02:00
})
2021-04-07 08:37:42 +02:00
test_that("Custom EUCAST rules work", {
skip_on_cran()
x <- custom_eucast_rules(AMC == "R" & genus == "Klebsiella" ~ aminopenicillins == "R",
AMC == "I" & genus == "Klebsiella" ~ aminopenicillins == "I")
expect_output(print(x))
expect_output(print(c(x, x)))
expect_output(print(as.list(x, x)))
# this custom rules makes 8 changes
expect_equal(nrow(eucast_rules(example_isolates,
rules = "custom",
custom_rules = x,
info = FALSE,
verbose = TRUE)),
8)
})