AMR/tests/testthat/test-mo.R

295 lines
14 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
2019-01-02 23:24:07 +01:00
# SOURCE #
# https://gitlab.com/msberends/AMR #
# #
# 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) #
# #
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-08-31 13:36:19 +02:00
context("mo.R")
test_that("as.mo works", {
clear_mo_history(force = TRUE)
2019-09-23 20:55:54 +02:00
2018-09-09 12:11:44 +02:00
library(dplyr)
MOs <- AMR::microorganisms %>% filter(!is.na(mo), nchar(mo) > 3)
2018-09-09 12:11:44 +02:00
expect_identical(as.character(MOs$mo), as.character(as.mo(MOs$mo)))
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
expect_identical(
as.character(as.mo(c("E. coli", "H. influenzae"))),
2019-09-18 15:46:09 +02:00
c("B_ESCHR_COLI", "B_HMPHL_INFL"))
2019-09-23 20:55:54 +02:00
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("Escherichia coli")), "B_ESCHR_COLI")
expect_equal(as.character(as.mo("Escherichia coli")), "B_ESCHR_COLI")
expect_equal(as.character(as.mo(22242416)), "B_ESCHR_COLI")
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("Escherichia species")), "B_ESCHR")
expect_equal(as.character(as.mo("Escherichia")), "B_ESCHR")
2019-02-21 23:32:30 +01:00
expect_equal(as.character(as.mo("Esch spp.")), "B_ESCHR")
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo(" B_ESCHR_COLI ")), "B_ESCHR_COLI")
expect_equal(as.character(as.mo("e coli")), "B_ESCHR_COLI") # not Campylobacter
expect_equal(as.character(as.mo("klpn")), "B_KLBSL_PNMN")
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("Klebsiella")), "B_KLBSL")
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("K. pneu rhino")), "B_KLBSL_PNMN_RHNS") # K. pneumoniae subspp. rhinoscleromatis
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("Bartonella")), "B_BRTNL")
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("C. difficile")), "B_CTRDM_DFFC")
expect_equal(as.character(as.mo("L. pneumophila")), "B_LGNLL_PNMP")
2019-02-18 02:33:37 +01:00
expect_equal(as.character(as.mo("Strepto")), "B_STRPT")
expect_equal(as.character(as.mo("Streptococcus")), "B_STRPT") # not Peptostreptoccus
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("Estreptococos grupo B")), "B_STRPT_GRPB")
expect_equal(as.character(as.mo("Group B Streptococci")), "B_STRPT_GRPB")
expect_equal(as.character(suppressWarnings(as.mo("B_STRPTC"))), "B_STRPT") # old MO code (<=v0.5.0)
expect_equal(as.character(suppressWarnings(as.mo("B_STRPT_PNE"))), "B_STRPT_PNMN") # old MO code (<=v0.8.0)
2019-09-23 20:55:54 +02:00
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo(c("GAS", "GBS"))), c("B_STRPT_GRPA", "B_STRPT_GRPB"))
2019-09-23 20:55:54 +02:00
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("S. pyo")), "B_STRPT_PYGN") # not Actinomyces pyogenes
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# GLIMS
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("bctfgr")), "B_BCTRD_FRGL")
2019-09-23 20:55:54 +02:00
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("MRSE")), "B_STPHY_EPDR")
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("VRE")), "B_ENTRC")
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("MRPA")), "B_PSDMN_ARGN")
expect_equal(as.character(as.mo("PISP")), "B_STRPT_PNMN")
expect_equal(as.character(as.mo("PRSP")), "B_STRPT_PNMN")
expect_equal(as.character(as.mo("VISP")), "B_STRPT_PNMN")
expect_equal(as.character(as.mo("VRSP")), "B_STRPT_PNMN")
2019-09-23 20:55:54 +02:00
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("CNS")), "B_STPHY_CONS")
expect_equal(as.character(as.mo("CoNS")), "B_STPHY_CONS")
expect_equal(as.character(as.mo("CPS")), "B_STPHY_COPS")
expect_equal(as.character(as.mo("CoPS")), "B_STPHY_COPS")
expect_equal(as.character(as.mo("VGS")), "B_STRPT_VIRI")
expect_equal(as.character(as.mo("streptococcus milleri")), "B_STRPT_MILL")
2019-08-13 16:15:08 +02:00
2019-09-23 20:55:54 +02:00
2018-11-02 10:27:57 +01:00
expect_equal(as.character(as.mo(c("Gram negative", "Gram positive"))), c("B_GRAMN", "B_GRAMP"))
2019-09-23 20:55:54 +02:00
2018-11-02 10:27:57 +01:00
# prevalent MO
2018-08-31 13:36:19 +02:00
expect_identical(
suppressWarnings(as.character(
2018-08-31 13:36:19 +02:00
as.mo(c("stau",
"STAU",
"staaur",
"S. aureus",
"S aureus",
2019-03-12 12:19:27 +01:00
"Sthafilokkockus aureeuzz",
"Staphylococcus aureus",
"MRSA",
"VISA")))),
2019-09-18 15:46:09 +02:00
rep("B_STPHY_AURS", 9))
2019-02-21 23:32:30 +01:00
expect_identical(
as.character(
as.mo(c('EHEC', 'EPEC', 'EIEC', 'STEC', 'ATEC', 'UPEC'))),
2019-09-18 15:46:09 +02:00
rep("B_ESCHR_COLI", 6))
2018-11-02 10:27:57 +01:00
# unprevalent MO
expect_identical(
as.character(
2019-02-18 02:33:37 +01:00
as.mo(c("burnod",
2018-11-02 10:27:57 +01:00
"B. nodosa",
"B nodosa",
"Burkholderia nodosa"))),
2019-09-18 15:46:09 +02:00
rep("B_BRKHL_NODS", 4))
2019-09-23 20:55:54 +02:00
2018-11-02 10:27:57 +01:00
# empty values
expect_identical(as.character(as.mo(c("", NA, NaN))), rep(NA_character_, 3))
# too few characters
expect_warning(as.mo("ab"))
2019-09-23 20:55:54 +02:00
expect_equal(suppressWarnings(as.character(as.mo(c("Qq species", "", "CRSM", "K. pneu rhino", "esco")))),
2019-09-18 15:46:09 +02:00
c("UNKNOWN", NA_character_, "B_STNTR_MLTP", "B_KLBSL_PNMN_RHNS", "B_ESCHR_COLI"))
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# check for Becker classification
2019-09-18 15:46:09 +02:00
expect_identical(as.character(as.mo("S. epidermidis", Becker = FALSE)), "B_STPHY_EPDR")
expect_identical(as.character(as.mo("S. epidermidis", Becker = TRUE)), "B_STPHY_CONS")
expect_identical(as.character(as.mo("STAEPI", Becker = TRUE)), "B_STPHY_CONS")
expect_identical(as.character(as.mo("S. intermedius", Becker = FALSE)), "B_STPHY_INTR")
expect_identical(as.character(as.mo("Sta intermedius",Becker = FALSE)), "B_STPHY_INTR")
expect_identical(as.character(as.mo("Sta intermedius",Becker = TRUE)), "B_STPHY_COPS")
expect_identical(as.character(as.mo("STAINT", Becker = TRUE)), "B_STPHY_COPS")
2018-08-31 13:36:19 +02:00
# aureus must only be influenced if Becker = "all"
2019-09-18 15:46:09 +02:00
expect_identical(as.character(as.mo("STAAUR", Becker = FALSE)), "B_STPHY_AURS")
expect_identical(as.character(as.mo("STAAUR", Becker = TRUE)), "B_STPHY_AURS")
expect_identical(as.character(as.mo("STAAUR", Becker = "all")), "B_STPHY_COPS")
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# check for Lancefield classification
2019-09-18 15:46:09 +02:00
expect_identical(as.character(as.mo("S. pyogenes", Lancefield = FALSE)), "B_STRPT_PYGN")
expect_identical(as.character(as.mo("S. pyogenes", Lancefield = TRUE)), "B_STRPT_GRPA")
expect_identical(as.character(as.mo("STCPYO", Lancefield = TRUE)), "B_STRPT_GRPA") # group A
expect_identical(as.character(as.mo("S. agalactiae", Lancefield = FALSE)), "B_STRPT_AGLC")
expect_identical(as.character(as.mo("S. agalactiae", Lancefield = TRUE)), "B_STRPT_GRPB") # group B
expect_identical(as.character(suppressWarnings(as.mo("estreptococos grupo B"))), "B_STRPT_GRPB")
expect_identical(as.character(as.mo("S. equisimilis", Lancefield = FALSE)), "B_STRPT_DYSG_EQSM")
expect_identical(as.character(as.mo("S. equisimilis", Lancefield = TRUE)), "B_STRPT_GRPC") # group C
# Enterococci must only be influenced if Lancefield = "all"
2019-09-18 15:46:09 +02:00
expect_identical(as.character(as.mo("E. faecium", Lancefield = FALSE)), "B_ENTRC_FACM")
expect_identical(as.character(as.mo("E. faecium", Lancefield = TRUE)), "B_ENTRC_FACM")
expect_identical(as.character(as.mo("E. faecium", Lancefield = "all")), "B_STRPT_GRPD") # group D
expect_identical(as.character(as.mo("S. anginosus", Lancefield = FALSE)), "B_STRPT_ANGN")
expect_identical(as.character(as.mo("S. anginosus", Lancefield = TRUE)), "B_STRPT_GRPF") # group F
expect_identical(as.character(as.mo("S. sanguinis", Lancefield = FALSE)), "B_STRPT_SNGN")
expect_identical(as.character(as.mo("S. sanguinis", Lancefield = TRUE)), "B_STRPT_GRPH") # group H
expect_identical(as.character(as.mo("S. salivarius", Lancefield = FALSE)), "B_STRPT_SLVR")
expect_identical(as.character(as.mo("S. salivarius", Lancefield = TRUE)), "B_STRPT_GRPK") # group K
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
library(dplyr)
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# select with one column
expect_identical(
example_isolates[1:10,] %>%
2018-08-31 13:36:19 +02:00
left_join_microorganisms() %>%
select(genus) %>%
as.mo() %>%
as.character(),
2018-09-24 23:33:29 +02:00
c("B_ESCHR", "B_ESCHR", "B_STPHY", "B_STPHY", "B_STPHY",
"B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY"))
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# select with two columns
expect_identical(
example_isolates[1:10,] %>%
2018-08-31 13:36:19 +02:00
pull(mo),
example_isolates[1:10,] %>%
2018-08-31 13:36:19 +02:00
left_join_microorganisms() %>%
select(genus, species) %>%
as.mo())
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# unknown results
expect_warning(as.mo(c("INVALID", "Yeah, unknown")))
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# too many columns
expect_error(example_isolates %>% select(1:3) %>% as.mo())
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# print
2019-09-18 15:46:09 +02:00
expect_output(print(as.mo(c("B_ESCHR_COLI", NA))))
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# test pull
expect_equal(nrow(example_isolates %>% mutate(mo = as.mo(mo))),
2018-08-31 13:36:19 +02:00
2000)
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# test data.frame
2019-09-18 15:46:09 +02:00
expect_equal(nrow(data.frame(test = as.mo("B_ESCHR_COLI"))),
2018-08-31 13:36:19 +02:00
1)
2019-09-23 20:55:54 +02:00
2018-08-31 13:36:19 +02:00
# check empty values
expect_equal(as.character(suppressWarnings(as.mo(""))),
NA_character_)
2019-09-23 20:55:54 +02:00
2018-10-01 11:39:43 +02:00
# check less prevalent MOs
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("Gomphosphaeria aponina delicatula")), "B_GMPHS_APNN_DLCT")
expect_equal(as.character(as.mo("Gomphosphaeria apo del")), "B_GMPHS_APNN_DLCT")
expect_equal(as.character(as.mo("G apo deli")), "B_GMPHS_APNN_DLCT")
expect_equal(as.character(as.mo("Gomphosphaeria aponina")), "B_GMPHS_APNN")
2018-10-01 11:39:43 +02:00
expect_equal(as.character(as.mo("Gomphosphaeria species")), "B_GMPHS")
expect_equal(as.character(as.mo("Gomphosphaeria")), "B_GMPHS")
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo(" B_GMPHS_APNN ")), "B_GMPHS_APNN")
expect_equal(as.character(as.mo("g aponina")), "B_GMPHS_APNN")
2019-09-23 20:55:54 +02:00
2018-10-01 11:39:43 +02:00
# check old names
2019-09-18 15:46:09 +02:00
expect_equal(suppressMessages(as.character(as.mo("Escherichia blattae"))), "B_SHMWL_BLTT")
2019-02-28 13:56:28 +01:00
print(mo_renamed())
2019-09-23 20:55:54 +02:00
2018-10-01 11:39:43 +02:00
# check uncertain names
2019-09-18 15:46:09 +02:00
expect_equal(suppressWarnings(as.character(as.mo("staaur extratest", allow_uncertain = TRUE))), "B_STPHY_AURS")
expect_equal(suppressWarnings(as.character(as.mo("staaur extratest", allow_uncertain = FALSE))), "UNKNOWN")
2018-10-01 11:39:43 +02:00
expect_warning(as.mo("esco extra_text", allow_uncertain = TRUE))
2019-09-18 15:46:09 +02:00
expect_equal(suppressWarnings(as.character(as.mo("unexisting aureus", allow_uncertain = 3))), "B_STPHY_AURS")
2019-05-13 12:21:57 +02:00
expect_equal(suppressWarnings(as.character(as.mo("unexisting staphy", allow_uncertain = 3))), "B_STPHY")
2019-09-18 15:46:09 +02:00
expect_equal(suppressWarnings(as.character(as.mo("Staphylococcus aureus unexisting", allow_uncertain = 3))), "B_STPHY_AURS")
2019-09-23 20:55:54 +02:00
2018-10-01 14:44:40 +02:00
# predefined reference_df
expect_equal(as.character(as.mo("TestingOwnID",
2019-09-18 15:46:09 +02:00
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COLI"))),
"B_ESCHR_COLI")
2018-10-01 14:44:40 +02:00
expect_equal(as.character(as.mo(c("TestingOwnID", "E. coli"),
2019-09-18 15:46:09 +02:00
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COLI"))),
c("B_ESCHR_COLI", "B_ESCHR_COLI"))
2018-10-09 15:41:44 +02:00
expect_warning(as.mo("TestingOwnID", reference_df = NULL))
2019-01-21 15:53:01 +01:00
expect_error(as.mo("E. coli", reference_df = data.frame(mycol = "TestingOwnID")))
2019-09-23 20:55:54 +02:00
2019-01-21 15:53:01 +01:00
# combination of existing mo and other code
2019-09-23 20:55:54 +02:00
expect_identical(as.character(as.mo(c("B_ESCHR_COL", "ESCCOL"))),
2019-09-18 15:46:09 +02:00
c("B_ESCHR_COLI", "B_ESCHR_COLI"))
2019-09-23 20:55:54 +02:00
2018-11-30 12:05:59 +01:00
# from different sources
expect_equal(as.character(as.mo(
2019-09-18 15:46:09 +02:00
c("PRTMIR", "bclcer", "B_ESCHR_COLI"))),
c("B_PROTS_MRBL", "B_BCLLS_CERS", "B_ESCHR_COLI"))
2019-09-23 20:55:54 +02:00
2018-12-06 14:36:39 +01:00
# hard to find
expect_equal(as.character(suppressWarnings(as.mo(
c("Microbacterium paraoxidans",
"Streptococcus suis (bovis gr)",
2018-12-07 12:04:55 +01:00
"Raoultella (here some text) terrigena")))),
2019-09-18 15:46:09 +02:00
c("B_MCRBC_PRXY", "B_STRPT_SUIS", "B_RLTLL_TRRG"))
expect_output(print(mo_uncertainties()))
2019-09-23 20:55:54 +02:00
2018-12-06 14:36:39 +01:00
# Salmonella (City) are all actually Salmonella enterica spp (City)
expect_equal(as.character(suppressWarnings(as.mo("Salmonella Goettingen"))),
2019-09-18 15:46:09 +02:00
"B_SLMNL_ENTR")
2019-02-21 23:32:30 +01:00
expect_equal(as.character(as.mo("Salmonella Group A")), "B_SLMNL")
2019-09-23 20:55:54 +02:00
2019-02-21 23:32:30 +01:00
# no virusses
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo("Virus")), NA_character_)
2019-09-23 20:55:54 +02:00
2019-02-21 23:32:30 +01:00
# summary
expect_equal(length(summary(example_isolates$mo)), 6)
2019-09-23 20:55:54 +02:00
2019-06-02 19:23:19 +02:00
# WHONET codes and NA/NaN
expect_equal(as.character(as.mo(c("xxx", "na", "nan"), debug = TRUE)),
rep(NA_character_, 3))
expect_equal(as.character(as.mo("con")), "UNKNOWN")
expect_equal(as.character(as.mo("xxx")), NA_character_)
2019-09-18 15:46:09 +02:00
expect_equal(as.character(as.mo(c("xxx", "con", "eco"))), c(NA_character_, "UNKNOWN", "B_ESCHR_COLI"))
2019-09-23 20:55:54 +02:00
expect_equal(as.character(as.mo(c("other", "none", "unknown"))),
2019-05-13 12:21:57 +02:00
rep("UNKNOWN", 3))
2019-09-23 20:55:54 +02:00
2019-05-13 12:21:57 +02:00
expect_null(mo_failures())
expect_true(example_isolates %>% pull(mo) %>% is.mo())
2019-09-23 20:55:54 +02:00
2019-06-22 20:14:06 +02:00
# expect_equal(get_mo_code("test", "mo"), "test")
# expect_equal(length(get_mo_code("Escherichia", "genus")),
# nrow(AMR::microorganisms[base::which(AMR::microorganisms[, "genus"] %in% "Escherichia"),]))
2019-09-23 20:55:54 +02:00
2019-05-13 12:21:57 +02:00
expect_error(translate_allow_uncertain(5))
2019-09-23 20:55:54 +02:00
# very old MO codes (<= v0.5.0)
2019-09-18 15:46:09 +02:00
expect_equal(suppressWarnings(as.character(as.mo("F_CCCCS_NEO"))), "F_CRYPT_NFRM")
expect_equal(suppressWarnings(as.character(as.mo("F_CANDD_GLB"))), "F_CANDD_GLBR")
# debug mode
expect_output(print(suppressMessages(suppressWarnings(as.mo("kshgcjkhsdgkshjdfsfvsdfv", debug = TRUE, allow_uncertain = 3)))))
2019-09-23 20:55:54 +02:00
# ..coccus
expect_equal(as.character(as.mo(c("meningococ", "gonococ", "pneumococ"))),
2019-09-18 15:46:09 +02:00
c("B_NESSR_MNNG", "B_NESSR_GNRR", "B_STRPT_PNMN"))
# yeasts and fungi
expect_equal(suppressWarnings(as.character(as.mo(c("yeasts", "fungi")))),
c("F_YEAST", "F_FUNGUS"))
# print tibble
2019-09-18 15:46:09 +02:00
expect_output(print(tibble(mo = as.mo("B_ESCHR_COLI"))))
# assigning and subsetting
x <- example_isolates$mo
expect_s3_class(x[1], "mo")
expect_s3_class(x[[1]], "mo")
expect_s3_class(c(x[1], x[9]), "mo")
expect_warning(x[1] <- "invalid code")
expect_warning(x[[1]] <- "invalid code")
expect_warning(c(x[1], "test"))
2018-08-31 13:36:19 +02:00
})