2021-05-15 21:36:22 +02:00
# ==================================================================== #
2023-07-08 17:30:05 +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
# #
2023-07-08 17:30:05 +02:00
# SOURCE CODE: #
2021-05-15 21:36:22 +02:00
# https://github.com/msberends/AMR #
# #
2023-07-08 17:30:05 +02:00
# PLEASE CITE THIS SOFTWARE AS: #
2022-10-05 09:12:22 +02:00
# 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. #
2023-05-27 10:39:22 +02:00
# https://doi.org/10.18637/jss.v104.i03 #
2022-10-05 09:12:22 +02:00
# #
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/ #
# ==================================================================== #
expect_equal ( mo_kingdom ( " Escherichia coli" ) , " Bacteria" )
expect_equal ( mo_kingdom ( " Escherichia coli" ) , mo_domain ( " Escherichia coli" ) )
2022-10-05 09:12:22 +02:00
expect_equal ( mo_phylum ( " Escherichia coli" ) , " Pseudomonadota" )
2021-05-15 21:36:22 +02:00
expect_equal ( mo_class ( " Escherichia coli" ) , " Gammaproteobacteria" )
expect_equal ( mo_order ( " Escherichia coli" ) , " Enterobacterales" )
2021-07-08 22:23:28 +02:00
expect_equal ( mo_family ( " Escherichia coli" ) , " Enterobacteriaceae" )
expect_equal ( mo_fullname ( " Escherichia coli" ) , " Escherichia coli" )
expect_equal ( mo_genus ( " Escherichia coli" ) , " Escherichia" )
expect_equal ( mo_name ( " Escherichia coli" ) , " Escherichia coli" )
expect_equal ( mo_shortname ( " Escherichia coli" ) , " E. coli" )
expect_equal ( mo_shortname ( " Escherichia" ) , " Escherichia" )
expect_equal ( mo_shortname ( " Staphylococcus aureus" ) , " S. aureus" )
expect_equal ( mo_shortname ( " Staphylococcus aureus" , Becker = TRUE ) , " S. aureus" )
expect_equal ( mo_shortname ( " Staphylococcus aureus" , Becker = " all" , language = " en" ) , " CoPS" )
expect_equal ( mo_shortname ( " Streptococcus agalactiae" ) , " S. agalactiae" )
expect_equal ( mo_shortname ( " Streptococcus agalactiae" , Lancefield = TRUE ) , " GBS" )
2021-07-04 20:25:30 +02:00
2022-12-09 10:30:25 +01:00
# check gram stain determination, to prevent we lag after a taxonomic renaming
current_grampos_phyla <- c (
" Actinomycetota" , # since 2021, old name was Actinobacteria
" Chloroflexota" , # since 2021, old name was Chloroflexi
" Bacillota" , # since 2021, old name was Firmicutes
" Mycoplasmatota" # since 2021, old name was Tenericutes
)
expect_true ( all ( current_grampos_phyla %in% microorganisms $ phylum , na.rm = TRUE ) )
current_grampos_classes <- c (
" " ,
" Acidimicrobiia" ,
" Actinomycetes" ,
" Anaerolineae" ,
" Ardenticatenia" ,
" Bacilli" ,
" Caldilineae" ,
" Chloroflexia" ,
" Clostridia" ,
" Coriobacteriia" ,
" Culicoidibacteria" ,
" Dehalococcoidia" ,
" Erysipelotrichia" ,
" Ktedonobacteria" ,
" Limnochordia" ,
2022-12-17 14:31:33 +01:00
" Limnocylindria" ,
2022-12-09 10:30:25 +01:00
" Mollicutes" ,
" Negativicutes" ,
" Nitriliruptoria" ,
" Rubrobacteria" ,
" Tepidiformia" ,
" Thermoflexia" ,
" Thermoleophilia" ,
" Thermolithobacteria" ,
" Tissierellia"
)
expect_identical ( sort ( unique ( microorganisms [which ( microorganisms $ phylum %in% current_grampos_phyla ) , " class" , drop = TRUE ] ) ) ,
current_grampos_classes )
2021-05-15 21:36:22 +02:00
expect_equal ( mo_species ( " Escherichia coli" ) , " coli" )
expect_equal ( mo_subspecies ( " Escherichia coli" ) , " " )
expect_equal ( mo_type ( " Escherichia coli" , language = " en" ) , " Bacteria" )
expect_equal ( mo_gramstain ( " Escherichia coli" , language = " en" ) , " Gram-negative" )
expect_inherits ( mo_taxonomy ( " Escherichia coli" ) , " list" )
2022-08-28 10:31:50 +02:00
expect_equal ( names ( mo_taxonomy ( " Escherichia coli" ) ) , c (
" kingdom" , " phylum" , " class" , " order" ,
" family" , " genus" , " species" , " subspecies"
) )
2021-05-15 21:36:22 +02:00
expect_equal ( mo_synonyms ( " Escherichia coli" ) , NULL )
expect_true ( length ( mo_synonyms ( " Candida albicans" ) ) > 1 )
expect_inherits ( mo_synonyms ( c ( " Candida albicans" , " Escherichia coli" ) ) , " list" )
2022-08-28 10:31:50 +02:00
expect_equal ( names ( mo_info ( " Escherichia coli" ) ) , c (
2023-01-20 09:45:03 +01:00
" mo" ,
2023-05-12 10:07:55 +02:00
" kingdom" , " phylum" , " class" , " order" , " family" , " genus" , " species" , " subspecies" ,
2023-05-11 21:56:27 +02:00
" status" , " synonyms" , " gramstain" , " oxygen_tolerance" ,
2023-05-12 10:07:55 +02:00
" url" , " ref" , " snomed" , " lpsn" , " gbif"
2022-08-28 10:31:50 +02:00
) )
2021-05-15 21:36:22 +02:00
expect_inherits ( mo_info ( c ( " Escherichia coli" , " Staphylococcus aureus" ) ) , " list" )
2023-05-11 21:56:27 +02:00
expect_identical ( mo_oxygen_tolerance ( c ( " Klebsiella pneumoniae" , " Clostridioides difficile" ) ) ,
c ( " aerobe" , " anaerobe" ) )
2023-01-06 13:45:20 +01:00
expect_equal ( as.character ( table ( mo_pathogenicity ( example_isolates $ mo ) ) ) ,
2023-01-06 13:35:37 +01:00
c ( " 1561" , " 422" , " 1" , " 16" ) )
2021-05-15 21:36:22 +02:00
expect_equal ( mo_ref ( " Escherichia coli" ) , " Castellani et al., 1919" )
expect_equal ( mo_authors ( " Escherichia coli" ) , " Castellani et al." )
expect_equal ( mo_year ( " Escherichia coli" ) , 1919 )
2022-10-05 09:12:22 +02:00
expect_true ( mo_url ( " Candida albicans" ) %like% " gbif.org" )
2021-05-15 21:36:22 +02:00
expect_true ( mo_url ( " Escherichia coli" ) %like% " lpsn.dsmz.de" )
# test integrity
2022-12-09 10:30:25 +01:00
expect_identical ( microorganisms $ fullname , mo_fullname ( microorganisms $ fullname , language = " en" , keep_synonyms = TRUE ) )
2021-05-15 21:36:22 +02:00
# check languages
expect_equal ( mo_type ( " Escherichia coli" , language = " de" ) , " Bakterien" )
expect_equal ( mo_gramstain ( " Escherichia coli" , language = " nl" ) , " Gram-negatief" )
2022-10-05 09:12:22 +02:00
gr <- mo_gramstain ( " Escherichia coli" , language = NULL )
for ( l in AMR ::: LANGUAGES_SUPPORTED [ -1 ] ) {
expect_false ( mo_gramstain ( " Escherichia coli" , language = l ) == gr , info = paste ( " Gram-stain in language" , l ) )
}
2021-05-15 21:36:22 +02:00
2022-10-19 11:47:57 +02:00
# test languages
2021-05-15 21:36:22 +02:00
expect_error ( mo_gramstain ( " Escherichia coli" , language = " UNKNOWN" ) )
2022-12-17 14:31:33 +01:00
dutch <- suppressWarnings ( mo_name ( microorganisms $ fullname [which ( microorganisms $ fullname %unlike% " unknown|coagulase|Fungi|[(]class[)]|[{]" ) ] , language = " nl" , keep_synonyms = TRUE ) ) # should be transformable to English again
2022-10-19 11:47:57 +02:00
expect_identical ( suppressWarnings ( mo_name ( dutch , language = NULL , keep_synonyms = TRUE ) ) ,
2022-12-17 14:31:33 +01:00
microorganisms $ fullname [which ( microorganisms $ fullname %unlike% " unknown|coagulase|Fungi|[(]class[)]|[{]" ) ] ) # gigantic test - will run ALL names
2021-05-15 21:36:22 +02:00
# manual property function
2022-10-05 09:12:22 +02:00
expect_error ( mo_property ( " Escherichia coli" , property = c ( " genus" , " fullname" ) ) )
2021-05-15 21:36:22 +02:00
expect_error ( mo_property ( " Escherichia coli" , property = " UNKNOWN" ) )
2022-08-28 10:31:50 +02:00
expect_identical (
mo_property ( " Escherichia coli" , property = " fullname" ) ,
mo_fullname ( " Escherichia coli" )
)
expect_identical (
mo_property ( " Escherichia coli" , property = " genus" ) ,
mo_genus ( " Escherichia coli" )
)
expect_identical (
mo_property ( " Escherichia coli" , property = " species" ) ,
mo_species ( " Escherichia coli" )
)
expect_identical (
2022-10-05 09:12:22 +02:00
mo_property ( " Escherichia coli" , property = " lpsn" ) ,
2022-08-28 10:31:50 +02:00
mo_lpsn ( " Escherichia coli" )
)
2022-10-05 09:12:22 +02:00
expect_identical (
mo_property ( " Escherichia coli" , property = " gbif" ) ,
mo_gbif ( " Escherichia coli" )
)
2021-05-15 21:36:22 +02:00
2022-10-10 10:12:08 +02:00
expect_true ( " Escherichia blattae" %in% mo_synonyms ( " Shimwellia blattae" ) )
expect_true ( is.list ( mo_synonyms ( rep ( " Shimwellia blattae" , 2 ) ) ) )
expect_identical ( mo_current ( c ( " Escherichia blattae" , " Escherichia coli" ) ) ,
c ( " Shimwellia blattae" , " Escherichia coli" ) )
2022-10-05 09:12:22 +02:00
expect_identical ( mo_ref ( " Chlamydia psittaci" ) , " Garcia-Lopez et al., 2019" )
expect_identical ( mo_ref ( " Chlamydophila psittaci" , keep_synonyms = TRUE ) , " Everett et al., 1999" )
2021-05-15 21:36:22 +02:00
2023-02-10 16:47:25 +01:00
expect_true ( 112283007 %in% mo_snomed ( " Escherichia coli" ) [ [1 ] ] )
2021-05-15 21:36:22 +02:00
# outcome of mo_fullname must always return the fullname from the data set
2022-08-28 10:31:50 +02:00
x <- data.frame (
mo = microorganisms $ mo ,
# fullname from the original data:
f1 = microorganisms $ fullname ,
# newly created fullname based on MO code:
2023-06-22 15:10:59 +02:00
f2 = mo_fullname ( microorganisms $ mo , language = " en" , keep_synonyms = TRUE ) ,
2022-08-28 10:31:50 +02:00
stringsAsFactors = FALSE
)
2021-05-15 21:36:22 +02:00
expect_equal ( nrow ( subset ( x , f1 != f2 ) ) , 0 )
# is gram pos/neg (also return FALSE for all non-bacteria)
2022-08-28 10:31:50 +02:00
expect_equal (
mo_is_gram_negative ( c ( " Escherichia coli" , " Staphylococcus aureus" , " Candida albicans" ) ) ,
c ( TRUE , FALSE , FALSE )
)
expect_equal (
mo_is_gram_positive ( c ( " Escherichia coli" , " Staphylococcus aureus" , " Candida albicans" ) ) ,
c ( FALSE , TRUE , FALSE )
)
2021-05-15 21:36:22 +02:00
# is intrinsic resistant
2022-08-28 10:31:50 +02:00
expect_equal (
mo_is_intrinsic_resistant (
c ( " Escherichia coli" , " Staphylococcus aureus" , " Candida albicans" ) ,
" vanco"
) ,
c ( TRUE , FALSE , FALSE )
)
2021-05-15 21:36:22 +02:00
# with reference data
2022-08-28 10:31:50 +02:00
expect_equal (
mo_name ( " test" , reference_df = data.frame ( col1 = " test" , mo = " B_ESCHR_COLI" ) ) ,
" Escherichia coli"
)
2023-02-18 14:56:06 +01:00
if ( AMR ::: pkg_is_available ( " dplyr" , min_version = " 1.0.0" , also_load = TRUE ) ) {
2021-05-15 21:36:22 +02:00
expect_equal ( example_isolates %>% filter ( mo_is_gram_negative ( ) ) %>% nrow ( ) ,
2022-08-28 10:31:50 +02:00
730 ,
tolerance = 0.5
)
2021-05-15 21:36:22 +02:00
expect_equal ( example_isolates %>% filter ( mo_is_gram_positive ( ) ) %>% nrow ( ) ,
2022-08-28 10:31:50 +02:00
1238 ,
tolerance = 0.5
)
2021-05-15 21:36:22 +02:00
expect_equal ( example_isolates %>% filter ( mo_is_intrinsic_resistant ( ab = " Vancomycin" ) ) %>% nrow ( ) ,
2022-08-28 10:31:50 +02:00
710 ,
tolerance = 0.5
)
2021-05-15 21:36:22 +02:00
}