mirror of
https://github.com/msberends/AMR.git
synced 2025-07-13 00:32:34 +02:00
styled, unit test fix
This commit is contained in:
@ -9,7 +9,7 @@
|
||||
# (c) 2018-2022 Berends MS, Luz CF et al. #
|
||||
# Developed at the University of Groningen, the Netherlands, in #
|
||||
# collaboration with non-profit organisations Certe Medical #
|
||||
# Diagnostics & Advice, and University Medical Center Groningen. #
|
||||
# Diagnostics & Advice, and University Medical Center Groningen. #
|
||||
# #
|
||||
# This R package is free software; you can freely use and distribute #
|
||||
# it for both personal and commercial purposes under the terms of the #
|
||||
@ -28,7 +28,8 @@ expect_identical(as.character(MOs$mo), as.character(as.mo(MOs$mo)))
|
||||
|
||||
expect_identical(
|
||||
as.character(as.mo(c("E. coli", "H. influenzae"))),
|
||||
c("B_ESCHR_COLI", "B_HMPHL_INFL"))
|
||||
c("B_ESCHR_COLI", "B_HMPHL_INFL")
|
||||
)
|
||||
|
||||
expect_equal(as.character(as.mo("Escherichia coli")), "B_ESCHR_COLI")
|
||||
expect_equal(as.character(as.mo("Escherichia coli")), "B_ESCHR_COLI")
|
||||
@ -79,29 +80,39 @@ expect_equal(as.character(as.mo(c("Gram negative", "Gram positive"))), c("B_GRAM
|
||||
# prevalent MO
|
||||
expect_identical(
|
||||
suppressWarnings(as.character(
|
||||
as.mo(c("stau",
|
||||
"STAU",
|
||||
"staaur",
|
||||
"S. aureus",
|
||||
"S aureus",
|
||||
"Sthafilokkockus aureeuzz",
|
||||
"Staphylococcus aureus",
|
||||
"MRSA",
|
||||
"VISA",
|
||||
"meth.-resis. S. aureus (MRSA)")))),
|
||||
rep("B_STPHY_AURS", 10))
|
||||
as.mo(c(
|
||||
"stau",
|
||||
"STAU",
|
||||
"staaur",
|
||||
"S. aureus",
|
||||
"S aureus",
|
||||
"Sthafilokkockus aureeuzz",
|
||||
"Staphylococcus aureus",
|
||||
"MRSA",
|
||||
"VISA",
|
||||
"meth.-resis. S. aureus (MRSA)"
|
||||
))
|
||||
)),
|
||||
rep("B_STPHY_AURS", 10)
|
||||
)
|
||||
expect_identical(
|
||||
as.character(
|
||||
as.mo(c("EHEC", "EPEC", "EIEC", "STEC", "ATEC", "UPEC"))),
|
||||
rep("B_ESCHR_COLI", 6))
|
||||
as.mo(c("EHEC", "EPEC", "EIEC", "STEC", "ATEC", "UPEC"))
|
||||
),
|
||||
rep("B_ESCHR_COLI", 6)
|
||||
)
|
||||
# unprevalent MO
|
||||
expect_identical(
|
||||
as.character(
|
||||
as.mo(c("parnod",
|
||||
"P. nodosa",
|
||||
"P nodosa",
|
||||
"Paraburkholderia nodosa"))),
|
||||
rep("B_PRBRK_NODS", 4))
|
||||
as.mo(c(
|
||||
"parnod",
|
||||
"P. nodosa",
|
||||
"P nodosa",
|
||||
"Paraburkholderia nodosa"
|
||||
))
|
||||
),
|
||||
rep("B_PRBRK_NODS", 4)
|
||||
)
|
||||
|
||||
# empty values
|
||||
expect_identical(as.character(as.mo(c("", " ", NA, NaN))), rep(NA_character_, 4))
|
||||
@ -109,40 +120,42 @@ expect_identical(as.character(as.mo(" ")), NA_character_)
|
||||
# too few characters
|
||||
expect_warning(as.mo("ab"))
|
||||
|
||||
expect_equal(suppressWarnings(as.character(as.mo(c("Qq species", "", "CRSM", "K. pneu rhino", "esco")))),
|
||||
c("UNKNOWN", NA_character_, "B_STNTR_MLTP", "B_KLBSL_PNMN_RHNS", "B_ESCHR_COLI"))
|
||||
expect_equal(
|
||||
suppressWarnings(as.character(as.mo(c("Qq species", "", "CRSM", "K. pneu rhino", "esco")))),
|
||||
c("UNKNOWN", NA_character_, "B_STNTR_MLTP", "B_KLBSL_PNMN_RHNS", "B_ESCHR_COLI")
|
||||
)
|
||||
|
||||
# check for Becker classification
|
||||
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. 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("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")
|
||||
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")
|
||||
# aureus must only be influenced if Becker = "all"
|
||||
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 = TRUE)), "B_STPHY_AURS")
|
||||
expect_identical(as.character(as.mo("STAAUR", Becker = "all")), "B_STPHY_COPS")
|
||||
|
||||
# check for Lancefield classification
|
||||
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(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
|
||||
expect_identical(as.character(as.mo("S. equisimilis", Lancefield = TRUE)), "B_STRPT_GRPC") # group C
|
||||
# Enterococci must only be influenced if Lancefield = "all"
|
||||
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
|
||||
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
|
||||
|
||||
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
|
||||
# select with one column
|
||||
@ -153,9 +166,12 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
|
||||
select(genus) %>%
|
||||
as.mo() %>%
|
||||
as.character(),
|
||||
c("B_ESCHR", "B_ESCHR", "B_STPHY", "B_STPHY", "B_STPHY",
|
||||
"B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY"))
|
||||
|
||||
c(
|
||||
"B_ESCHR", "B_ESCHR", "B_STPHY", "B_STPHY", "B_STPHY",
|
||||
"B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY", "B_STPHY"
|
||||
)
|
||||
)
|
||||
|
||||
# select with two columns
|
||||
expect_identical(
|
||||
example_isolates %>%
|
||||
@ -165,14 +181,17 @@ if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
|
||||
slice(1:10) %>%
|
||||
left_join_microorganisms() %>%
|
||||
select(genus, species) %>%
|
||||
as.mo())
|
||||
|
||||
as.mo()
|
||||
)
|
||||
|
||||
# too many columns
|
||||
expect_error(example_isolates %>% select(1:3) %>% as.mo())
|
||||
|
||||
|
||||
# test pull
|
||||
expect_equal(nrow(example_isolates %>% mutate(mo = as.mo(mo))),
|
||||
2000)
|
||||
expect_equal(
|
||||
nrow(example_isolates %>% mutate(mo = as.mo(mo))),
|
||||
2000
|
||||
)
|
||||
expect_true(example_isolates %>% pull(mo) %>% is.mo())
|
||||
}
|
||||
|
||||
@ -183,12 +202,16 @@ expect_warning(as.mo(c("INVALID", "Yeah, unknown")))
|
||||
expect_stdout(print(as.mo(c("B_ESCHR_COLI", NA))))
|
||||
|
||||
# test data.frame
|
||||
expect_equal(nrow(data.frame(test = as.mo("B_ESCHR_COLI"))),
|
||||
1)
|
||||
expect_equal(
|
||||
nrow(data.frame(test = as.mo("B_ESCHR_COLI"))),
|
||||
1
|
||||
)
|
||||
|
||||
# check empty values
|
||||
expect_equal(as.character(suppressWarnings(as.mo(""))),
|
||||
NA_character_)
|
||||
expect_equal(
|
||||
as.character(suppressWarnings(as.mo(""))),
|
||||
NA_character_
|
||||
)
|
||||
|
||||
# check less prevalent MOs
|
||||
expect_equal(as.character(as.mo("Gomphosphaeria aponina delicatula")), "B_GMPHS_APNN_DLCT")
|
||||
@ -215,38 +238,56 @@ expect_equal(suppressMessages(as.character(as.mo("unexisting staphy", allow_unce
|
||||
expect_equal(suppressMessages(as.character(as.mo(c("s aure THISISATEST", "Staphylococcus aureus unexisting"), allow_uncertain = 3))), c("B_STPHY_AURS_AURS", "B_STPHY_AURS_AURS"))
|
||||
|
||||
# predefined reference_df
|
||||
expect_equal(as.character(as.mo("TestingOwnID",
|
||||
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COLI"))),
|
||||
"B_ESCHR_COLI")
|
||||
expect_equal(as.character(as.mo(c("TestingOwnID", "E. coli"),
|
||||
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COLI"))),
|
||||
c("B_ESCHR_COLI", "B_ESCHR_COLI"))
|
||||
expect_equal(
|
||||
as.character(as.mo("TestingOwnID",
|
||||
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COLI")
|
||||
)),
|
||||
"B_ESCHR_COLI"
|
||||
)
|
||||
expect_equal(
|
||||
as.character(as.mo(c("TestingOwnID", "E. coli"),
|
||||
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COLI")
|
||||
)),
|
||||
c("B_ESCHR_COLI", "B_ESCHR_COLI")
|
||||
)
|
||||
expect_warning(as.mo("TestingOwnID", reference_df = NULL))
|
||||
expect_error(as.mo("E. coli", reference_df = data.frame(mycol = "TestingOwnID")))
|
||||
|
||||
# combination of existing mo and other code
|
||||
expect_identical(as.character(as.mo(c("B_ESCHR_COL", "ESCCOL"))),
|
||||
c("B_ESCHR_COLI", "B_ESCHR_COLI"))
|
||||
expect_identical(
|
||||
as.character(as.mo(c("B_ESCHR_COL", "ESCCOL"))),
|
||||
c("B_ESCHR_COLI", "B_ESCHR_COLI")
|
||||
)
|
||||
|
||||
# from different sources
|
||||
expect_equal(as.character(as.mo(
|
||||
c("PRTMIR", "bclcer", "B_ESCHR_COLI"))),
|
||||
c("B_PROTS_MRBL", "B_BCLLS_CERS", "B_ESCHR_COLI"))
|
||||
expect_equal(
|
||||
as.character(as.mo(
|
||||
c("PRTMIR", "bclcer", "B_ESCHR_COLI")
|
||||
)),
|
||||
c("B_PROTS_MRBL", "B_BCLLS_CERS", "B_ESCHR_COLI")
|
||||
)
|
||||
|
||||
# hard to find
|
||||
expect_equal(as.character(suppressMessages(as.mo(
|
||||
c("Microbacterium paraoxidans",
|
||||
"Streptococcus suis (bovis gr)",
|
||||
"Raoultella (here some text) terrigena")))),
|
||||
c("B_MCRBC_PRXY", "B_STRPT_SUIS", "B_RLTLL_TRRG"))
|
||||
expect_equal(
|
||||
as.character(suppressMessages(as.mo(
|
||||
c(
|
||||
"Microbacterium paraoxidans",
|
||||
"Streptococcus suis (bovis gr)",
|
||||
"Raoultella (here some text) terrigena"
|
||||
)
|
||||
))),
|
||||
c("B_MCRBC_PRXY", "B_STRPT_SUIS", "B_RLTLL_TRRG")
|
||||
)
|
||||
expect_stdout(print(mo_uncertainties()))
|
||||
x <- as.mo("S. aur")
|
||||
# many hits
|
||||
expect_stdout(print(mo_uncertainties()))
|
||||
|
||||
# Salmonella (City) are all actually Salmonella enterica spp (City)
|
||||
expect_equal(suppressMessages(as.mo(c("Salmonella Goettingen", "Salmonella Typhimurium", "Salmonella Group A"))),
|
||||
as.mo(c("Salmonella enterica", "Salmonella enterica", "Salmonella")))
|
||||
expect_equal(
|
||||
suppressMessages(as.mo(c("Salmonella Goettingen", "Salmonella Typhimurium", "Salmonella Group A"))),
|
||||
as.mo(c("Salmonella enterica", "Salmonella enterica", "Salmonella"))
|
||||
)
|
||||
|
||||
# no viruses
|
||||
expect_equal(as.character(as.mo("Virus")), NA_character_)
|
||||
@ -255,13 +296,17 @@ expect_equal(as.character(as.mo("Virus")), NA_character_)
|
||||
expect_equal(length(summary(example_isolates$mo)), 6)
|
||||
|
||||
# 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(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_)
|
||||
expect_equal(as.character(as.mo(c("xxx", "con", "eco"))), c(NA_character_, "UNKNOWN", "B_ESCHR_COLI"))
|
||||
expect_equal(as.character(as.mo(c("other", "none", "unknown"))),
|
||||
rep("UNKNOWN", 3))
|
||||
expect_equal(
|
||||
as.character(as.mo(c("other", "none", "unknown"))),
|
||||
rep("UNKNOWN", 3)
|
||||
)
|
||||
|
||||
expect_null(mo_failures())
|
||||
|
||||
@ -271,11 +316,15 @@ expect_error(translate_allow_uncertain(5))
|
||||
expect_stdout(print(suppressMessages(suppressWarnings(as.mo("kshgcjkhsdgkshjdfsfvsdfv", debug = TRUE, allow_uncertain = 3)))))
|
||||
|
||||
# ..coccus
|
||||
expect_equal(as.character(as.mo(c("meningococ", "gonococ", "pneumococ"))),
|
||||
c("B_NESSR_MNNG", "B_NESSR_GNRR", "B_STRPT_PNMN"))
|
||||
expect_equal(
|
||||
as.character(as.mo(c("meningococ", "gonococ", "pneumococ"))),
|
||||
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"))
|
||||
expect_equal(
|
||||
suppressWarnings(as.character(as.mo(c("yeasts", "fungi")))),
|
||||
c("F_YEAST", "F_FUNGUS")
|
||||
)
|
||||
|
||||
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
|
||||
# print tibble
|
||||
@ -292,8 +341,10 @@ expect_warning(x[[1]] <- "invalid code")
|
||||
expect_warning(c(x[1], "test"))
|
||||
|
||||
# ignoring patterns
|
||||
expect_equal(as.character(as.mo(c("E. coli", "E. coli ignorethis"), ignore_pattern = "this")),
|
||||
c("B_ESCHR_COLI", NA))
|
||||
expect_equal(
|
||||
as.character(as.mo(c("E. coli", "E. coli ignorethis"), ignore_pattern = "this")),
|
||||
c("B_ESCHR_COLI", NA)
|
||||
)
|
||||
|
||||
# frequency tables
|
||||
if (AMR:::pkg_is_available("cleaner")) {
|
||||
|
Reference in New Issue
Block a user