AMR/tests/testthat/test-mo.R

249 lines
12 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. #
# Visit our website for more info: https://msberends.gitab.io/AMR. #
# ==================================================================== #
2018-08-31 13:36:19 +02:00
context("mo.R")
test_that("as.mo works", {
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)))
2018-08-31 13:36:19 +02:00
expect_identical(
as.character(as.mo(c("E. coli", "H. influenzae"))),
2018-09-24 23:33:29 +02:00
c("B_ESCHR_COL", "B_HMPHL_INF"))
2018-08-31 13:36:19 +02:00
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("Escherichia coli")), "B_ESCHR_COL")
expect_equal(as.character(as.mo("Escherichia coli")), "B_ESCHR_COL")
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")
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo(" B_ESCHR_COL ")), "B_ESCHR_COL")
expect_equal(as.character(as.mo("e coli")), "B_ESCHR_COL") # not Campylobacter
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("klpn")), "B_KLBSL_PNE")
expect_equal(as.character(as.mo("Klebsiella")), "B_KLBSL")
expect_equal(as.character(as.mo("K. pneu rhino")), "B_KLBSL_PNE_RHI") # K. pneumoniae subspp. rhinoscleromatis
expect_equal(as.character(as.mo("Bartonella")), "B_BRTNL")
2019-02-18 02:33:37 +01:00
expect_equal(as.character(as.mo("C. difficile")), "B_CLSTR_DIF")
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("L. pneumophila")), "B_LGNLL_PNE")
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-02-21 23:32:30 +01:00
expect_equal(as.character(as.mo("B_STRPTC")), "B_STRPT") # old MO code (<=v0.5.0)
2019-02-18 02:33:37 +01:00
expect_equal(as.character(as.mo(c("GAS", "GBS"))), c("B_STRPT_GRA", "B_STRPT_GRB"))
2019-02-18 02:33:37 +01:00
expect_equal(as.character(as.mo("S. pyo")), "B_STRPT_PYO") # not Actinomyces pyogenes
2018-08-31 13:36:19 +02:00
# GLIMS
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("bctfgr")), "B_BCTRD_FRA")
2018-08-31 13:36:19 +02:00
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("MRSE")), "B_STPHY_EPI")
expect_equal(as.character(as.mo("VRE")), "B_ENTRC")
2019-02-18 02:33:37 +01:00
expect_equal(as.character(as.mo("MRPA")), "B_PSDMN_AER")
expect_equal(as.character(as.mo("PISP")), "B_STRPT_PNE")
expect_equal(as.character(as.mo("PRSP")), "B_STRPT_PNE")
expect_equal(as.character(as.mo("VISP")), "B_STRPT_PNE")
expect_equal(as.character(as.mo("VRSP")), "B_STRPT_PNE")
2018-08-31 13:36:19 +02:00
2018-09-24 23:33:29 +02:00
expect_equal(as.character(as.mo("CNS")), "B_STPHY_CNS")
expect_equal(as.character(as.mo("CoNS")), "B_STPHY_CNS")
expect_equal(as.character(as.mo("CPS")), "B_STPHY_CPS")
expect_equal(as.character(as.mo("CoPS")), "B_STPHY_CPS")
2018-11-02 10:27:57 +01:00
expect_equal(as.character(as.mo(c("Gram negative", "Gram positive"))), c("B_GRAMN", "B_GRAMP"))
# prevalent MO
2018-08-31 13:36:19 +02:00
expect_identical(
as.character(
as.mo(c("stau",
"STAU",
"staaur",
"S. aureus",
"S aureus",
"Staphylococcus aureus",
"MRSA",
"VISA"))),
2018-09-24 23:33:29 +02:00
rep("B_STPHY_AUR", 8))
2019-02-21 23:32:30 +01:00
expect_identical(
as.character(
as.mo(c('EHEC', 'EPEC', 'EIEC', 'STEC', 'ATEC'))),
rep("B_ESCHR_COL", 5))
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-02-18 02:33:37 +01:00
rep("B_BRKHL_NOD", 4))
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"))
2018-08-31 13:36:19 +02:00
2018-11-30 12:05:59 +01:00
expect_equal(suppressWarnings(as.character(as.mo(c("Qq species", "", "CRS", "K. pneu rhino", "esco")))),
c(NA_character_, NA_character_, "B_STNTR_MAL", "B_KLBSL_PNE_RHI", "B_ESCHR_COL"))
2018-08-31 13:36:19 +02:00
# check for Becker classification
2019-01-06 16:40:55 +01:00
expect_identical(as.character(as.mo("S. epidermidis", Becker = FALSE)), "B_STPHY_EPI")
expect_identical(as.character(as.mo("S. epidermidis", Becker = TRUE)), "B_STPHY_CNS")
expect_identical(as.character(as.mo("STAEPI", Becker = TRUE)), "B_STPHY_CNS")
2019-02-18 02:33:37 +01:00
expect_identical(as.character(as.mo("S. intermedius", Becker = FALSE)), "B_STPHY_INT")
2019-01-06 16:40:55 +01:00
expect_identical(as.character(as.mo("Sta intermedius",Becker = FALSE)), "B_STPHY_INT")
expect_identical(as.character(as.mo("Sta intermedius",Becker = TRUE)), "B_STPHY_CPS")
expect_identical(as.character(as.mo("STAINT", Becker = TRUE)), "B_STPHY_CPS")
2018-08-31 13:36:19 +02:00
# aureus must only be influenced if Becker = "all"
2019-01-06 16:40:55 +01:00
expect_identical(as.character(as.mo("STAAUR", Becker = FALSE)), "B_STPHY_AUR")
expect_identical(as.character(as.mo("STAAUR", Becker = TRUE)), "B_STPHY_AUR")
expect_identical(as.character(as.mo("STAAUR", Becker = "all")), "B_STPHY_CPS")
2018-08-31 13:36:19 +02:00
# check for Lancefield classification
2019-02-18 02:33:37 +01:00
expect_identical(as.character(as.mo("S. pyogenes", Lancefield = FALSE)), "B_STRPT_PYO")
expect_identical(as.character(as.mo("S. pyogenes", Lancefield = TRUE)), "B_STRPT_GRA")
expect_identical(as.character(as.mo("STCPYO", Lancefield = TRUE)), "B_STRPT_GRA") # group A
expect_identical(as.character(as.mo("S. agalactiae", Lancefield = FALSE)), "B_STRPT_AGA")
expect_identical(as.character(as.mo("S. agalactiae", Lancefield = TRUE)), "B_STRPT_GRB") # group B
2019-02-21 23:32:30 +01:00
expect_identical(as.character(suppressWarnings(as.mo("estreptococos grupo B"))), "B_STRPT_GRB")
2019-02-18 02:33:37 +01:00
expect_identical(as.character(as.mo("S. equisimilis", Lancefield = FALSE)), "B_STRPT_DYS_EQU")
expect_identical(as.character(as.mo("S. equisimilis", Lancefield = TRUE)), "B_STRPT_GRC") # group C
# Enterococci must only be influenced if Lancefield = "all"
2019-01-06 16:40:55 +01:00
expect_identical(as.character(as.mo("E. faecium", Lancefield = FALSE)), "B_ENTRC_IUM")
expect_identical(as.character(as.mo("E. faecium", Lancefield = TRUE)), "B_ENTRC_IUM")
2019-02-18 02:33:37 +01:00
expect_identical(as.character(as.mo("E. faecium", Lancefield = "all")), "B_STRPT_GRD") # group D
expect_identical(as.character(as.mo("S. anginosus", Lancefield = FALSE)), "B_STRPT_ANG")
expect_identical(as.character(as.mo("S. anginosus", Lancefield = TRUE)), "B_STRPT_GRF") # group F
expect_identical(as.character(as.mo("S. sanguinis", Lancefield = FALSE)), "B_STRPT_SAN")
expect_identical(as.character(as.mo("S. sanguinis", Lancefield = TRUE)), "B_STRPT_GRH") # group H
expect_identical(as.character(as.mo("S. salivarius", Lancefield = FALSE)), "B_STRPT_SAL")
expect_identical(as.character(as.mo("S. salivarius", Lancefield = TRUE)), "B_STRPT_GRK") # group K
2018-08-31 13:36:19 +02:00
library(dplyr)
# select with one column
expect_identical(
septic_patients[1:10,] %>%
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"))
2018-08-31 13:36:19 +02:00
# select with two columns
expect_identical(
septic_patients[1:10,] %>%
pull(mo),
septic_patients[1:10,] %>%
left_join_microorganisms() %>%
select(genus, species) %>%
as.mo() %>%
as.character())
# unknown results
expect_warning(as.mo(c("INVALID", "Yeah, unknown")))
# too many columns
expect_error(septic_patients %>% select(1:3) %>% as.mo())
# print
2018-09-24 23:33:29 +02:00
expect_output(print(as.mo(c("B_ESCHR_COL", NA))))
2018-08-31 13:36:19 +02:00
# test pull
expect_equal(nrow(septic_patients %>% mutate(mo = as.mo(mo))),
2000)
# test data.frame
2018-09-24 23:33:29 +02:00
expect_equal(nrow(data.frame(test = as.mo("B_ESCHR_COL"))),
2018-08-31 13:36:19 +02:00
1)
# check empty values
expect_equal(as.character(suppressWarnings(as.mo(""))),
NA_character_)
2018-10-01 11:39:43 +02:00
# check less prevalent MOs
expect_equal(as.character(as.mo("Gomphosphaeria aponina delicatula")), "B_GMPHS_APO_DEL")
2018-11-02 10:27:57 +01:00
expect_equal(as.character(as.mo("Gomphosphaeria apo del")), "B_GMPHS_APO_DEL")
2018-10-01 11:39:43 +02:00
expect_equal(as.character(as.mo("G apo deli")), "B_GMPHS_APO_DEL")
expect_equal(as.character(as.mo("Gomphosphaeria aponina")), "B_GMPHS_APO")
expect_equal(as.character(as.mo("Gomphosphaeria species")), "B_GMPHS")
expect_equal(as.character(as.mo("Gomphosphaeria")), "B_GMPHS")
expect_equal(as.character(as.mo(" B_GMPHS_APO ")), "B_GMPHS_APO")
expect_equal(as.character(as.mo("g aponina")), "B_GMPHS_APO")
# check old names
expect_equal(suppressMessages(as.character(as.mo("Escherichia blattae"))), "B_SHMWL_BLA")
2019-02-18 02:33:37 +01:00
# # - Didymosphaeria spartinae (unprevalent)
# expect_warning(suppressMessages(as.mo("D spartin", allow_uncertain = TRUE)))
# # - was renamed to Leptosphaeria obiones
# expect_equal(suppressWarnings(suppressMessages(as.character(as.mo("D spartin", allow_uncertain = TRUE)))),
# "F_LPTSP_OBI")
2018-10-01 11:39:43 +02:00
# check uncertain names
expect_equal(suppressWarnings(as.character(as.mo("esco extra_text", allow_uncertain = FALSE))), NA_character_)
expect_equal(suppressWarnings(as.character(as.mo("esco extra_text", allow_uncertain = TRUE))), "B_ESCHR_COL")
expect_warning(as.mo("esco extra_text", allow_uncertain = TRUE))
2018-10-01 14:44:40 +02:00
# predefined reference_df
expect_equal(as.character(as.mo("TestingOwnID",
2019-01-21 15:53:01 +01:00
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COL"))),
2018-10-01 14:44:40 +02:00
"B_ESCHR_COL")
expect_equal(as.character(as.mo(c("TestingOwnID", "E. coli"),
2019-01-21 15:53:01 +01:00
reference_df = data.frame(mycol = "TestingOwnID", mo = "B_ESCHR_COL"))),
2018-10-01 14:44:40 +02:00
c("B_ESCHR_COL", "B_ESCHR_COL"))
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")))
2018-10-09 15:41:44 +02:00
2019-01-21 15:53:01 +01:00
# combination of existing mo and other code
2018-10-09 15:41:44 +02:00
expect_identical(as.character(as.mo(c("B_ESCHR_COL", "ESCCOL"))),
c("B_ESCHR_COL", "B_ESCHR_COL"))
2019-02-08 16:06:54 +01:00
# expect_equal(mo_fullname(c("E. spp.",
# "E. spp",
# "E. species")),
# rep("Escherichia species", 3))
2018-10-01 14:44:40 +02:00
2018-11-30 12:05:59 +01:00
# from different sources
expect_equal(as.character(as.mo(
c("PRTMIR", "bclcer", "B_ESCHR_COL"))),
c("B_PROTS_MIR", "B_BCLLS_CER", "B_ESCHR_COL"))
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-02-18 02:33:37 +01:00
c("B_MCRBC", "B_STRPT_SUI", "B_RLTLL_TER"))
2018-12-06 14:36:39 +01:00
# Salmonella (City) are all actually Salmonella enterica spp (City)
2018-12-07 12:04:55 +01:00
expect_equal(as.character(suppressMessages(as.mo("Salmonella Goettingen"))),
2018-12-06 14:36:39 +01:00
"B_SLMNL_ENT")
2019-02-21 23:32:30 +01:00
expect_equal(as.character(as.mo("Salmonella Group A")), "B_SLMNL")
# no virusses
expect_warning(as.mo("Virus"))
# summary
expect_equal(length(summary(septic_patients$mo)), 6)
expect_warning(as.mo("Cutibacterium"))
2018-08-31 13:36:19 +02:00
})