speed improvement for as.mo, more old taxonomic names

This commit is contained in:
dr. M.S. (Matthijs) Berends 2018-09-27 23:23:48 +02:00
parent 450992baea
commit 2b0080995e
18 changed files with 481 additions and 441 deletions

View File

@ -43,7 +43,7 @@ before_install:
- if [ $TRAVIS_OS_NAME = osx ]; then brew install libgit2; fi - if [ $TRAVIS_OS_NAME = osx ]; then brew install libgit2; fi
install: install:
- Rscript -e "install.packages(c('devtools', 'backports', 'clipr', 'curl', 'data.table', 'dplyr', 'hms', 'knitr', 'readr', 'rlang', 'rvest', 'xml2'))" - Rscript -e "install.packages(c('devtools', 'backports', 'clipr', 'curl', 'data.table', 'dplyr', 'hms', 'knitr', 'readr', 'rlang', 'rvest', 'xml2', 'covr', 'ggplot2', 'rmarkdown', 'testthat', 'tidyr'))"
- if [ $TRAVIS_OS_NAME = osx ]; then Rscript -e "devtools::install_github('r-lib/rlang')"; fi - if [ $TRAVIS_OS_NAME = osx ]; then Rscript -e "devtools::install_github('r-lib/rlang')"; fi
# postrun # postrun

View File

@ -1,6 +1,6 @@
Package: AMR Package: AMR
Version: 0.3.0.9009 Version: 0.3.0.9009
Date: 2018-09-24 Date: 2018-09-27
Title: Antimicrobial Resistance Analysis Title: Antimicrobial Resistance Analysis
Authors@R: c( Authors@R: c(
person( person(

View File

@ -148,9 +148,9 @@
#' Data set with old taxonomic data from ITIS #' Data set with old taxonomic data from ITIS
#' #'
#' A data set containing old, previously valid, taxonomic names. This data set is used internally by \code{\link{as.mo}}. #' A data set containing old (previously valid or accepted) taxonomic names according to ITIS. This data set is used internally by \code{\link{as.mo}}.
#' @inheritSection as.mo ITIS #' @inheritSection as.mo ITIS
#' @format A \code{\link{data.frame}} with 1,682 observations and 5 variables: #' @format A \code{\link{data.frame}} with 2,384 observations and 5 variables:
#' \describe{ #' \describe{
#' \item{\code{tsn}}{Old Taxonomic Serial Number (TSN), as defined by ITIS} #' \item{\code{tsn}}{Old Taxonomic Serial Number (TSN), as defined by ITIS}
#' \item{\code{name}}{Old taxonomic name of the microorganism as found in ITIS, see Source} #' \item{\code{name}}{Old taxonomic name of the microorganism as found in ITIS, see Source}
@ -168,10 +168,21 @@
#' @format A \code{\link{tibble}} with 1,095 observations and 2 variables: #' @format A \code{\link{tibble}} with 1,095 observations and 2 variables:
#' \describe{ #' \describe{
#' \item{\code{umcg}}{Code of microorganism according to UMCG MMB} #' \item{\code{umcg}}{Code of microorganism according to UMCG MMB}
#' \item{\code{certe}}{Code of microorganism according to Certe MMB}
#' }
#' @seealso \code{\link{as.mo}} \code{\link{microorganisms.certe}} \code{\link{microorganisms}}
"microorganisms.umcg"
#' Translation table for Certe
#'
#' A data set containing all bacteria codes of Certe MMB. These codes can be joined to data with an ID from \code{\link{microorganisms}$mo} (using \code{\link{left_join_microorganisms}}). GLIMS codes can also be translated to valid \code{MO}s with \code{\link{guess_mo}}.
#' @format A \code{\link{tibble}} with 2,664 observations and 2 variables:
#' \describe{
#' \item{\code{certe}}{Code of microorganism according to Certe MMB}
#' \item{\code{mo}}{Code of microorganism in \code{\link{microorganisms}}} #' \item{\code{mo}}{Code of microorganism in \code{\link{microorganisms}}}
#' } #' }
#' @seealso \code{\link{as.mo}} \code{\link{microorganisms}} #' @seealso \code{\link{as.mo}} \code{\link{microorganisms}}
"microorganisms.umcg" "microorganisms.certe"
#' Data set with 2000 blood culture isolates of septic patients #' Data set with 2000 blood culture isolates of septic patients
#' #'

View File

@ -57,6 +57,7 @@ globalVariables(c(".",
"real_first_isolate", "real_first_isolate",
"S", "S",
"septic_patients", "septic_patients",
"shortname",
"species", "species",
"tsn", "tsn",
"tsn_new", "tsn_new",

746
R/mo.R
View File

@ -60,7 +60,7 @@
#' \code{guess_mo} is an alias of \code{as.mo}. #' \code{guess_mo} is an alias of \code{as.mo}.
#' @section ITIS: #' @section ITIS:
#' \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr} #' \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr}
#' This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, https://www.itis.gov). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS. #' This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, \url{https://www.itis.gov}). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS.
# (source as section, so it can be inherited by mo_property:) # (source as section, so it can be inherited by mo_property:)
#' @section Source: #' @section Source:
#' [1] Becker K \emph{et al.} \strong{Coagulase-Negative Staphylococci}. 2014. Clin Microbiol Rev. 27(4): 870926. \url{https://dx.doi.org/10.1128/CMR.00109-13} #' [1] Becker K \emph{et al.} \strong{Coagulase-Negative Staphylococci}. 2014. Clin Microbiol Rev. 27(4): 870926. \url{https://dx.doi.org/10.1128/CMR.00109-13}
@ -69,8 +69,6 @@
#' #'
#' [3] Integrated Taxonomic Information System (ITIS). Retrieved September 2018. \url{http://www.itis.gov} #' [3] Integrated Taxonomic Information System (ITIS). Retrieved September 2018. \url{http://www.itis.gov}
#' @export #' @export
#' @importFrom dplyr %>% pull left_join
#' @importFrom data.table as.data.table setkey
#' @return Character (vector) with class \code{"mo"}. Unknown values will return \code{NA}. #' @return Character (vector) with class \code{"mo"}. Unknown values will return \code{NA}.
#' @seealso \code{\link{microorganisms}} for the \code{data.frame} with ITIS content that is being used to determine ID's. \cr #' @seealso \code{\link{microorganisms}} for the \code{data.frame} with ITIS content that is being used to determine ID's. \cr
#' The \code{\link{mo_property}} functions (like \code{\link{mo_genus}}, \code{\link{mo_gramstain}}) to get properties based on the returned code. #' The \code{\link{mo_property}} functions (like \code{\link{mo_genus}}, \code{\link{mo_gramstain}}) to get properties based on the returned code.
@ -126,7 +124,25 @@
#' mutate(mo = guess_mo(paste(genus, species))) #' mutate(mo = guess_mo(paste(genus, species)))
#' } #' }
as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE) { as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE) {
exec_as.mo(x = x, Becker = Becker, Lancefield = Lancefield,
allow_uncertain = allow_uncertain, property = "mo")
}
#' @rdname as.mo
#' @export
is.mo <- function(x) {
# bactid for older releases
# remove when is.bactid will be removed
identical(class(x), "mo") | identical(class(x), "bactid")
}
#' @rdname as.mo
#' @export
guess_mo <- as.mo
#' @importFrom dplyr %>% pull left_join
#' @importFrom data.table as.data.table setkey
exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE, property = "mo") {
if (NCOL(x) == 2) { if (NCOL(x) == 2) {
# support tidyverse selection like: df %>% select(colA, colB) # support tidyverse selection like: df %>% select(colA, colB)
# paste these columns together # paste these columns together
@ -147,374 +163,362 @@ as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE
} }
} }
MOs <- as.data.table(AMR::microorganisms)
setkey(MOs, prevalence, tsn)
MOs_mostprevalent <- MOs[prevalence != 9999,]
MOs_allothers <- NULL # will be set later, if needed
MOs_old <- NULL # will be set later, if needed
if (all(unique(x) %in% MOs[,mo])) {
class(x) <- "mo"
attr(x, 'package') <- 'AMR'
attr(x, 'ITIS') <- TRUE
return(x)
}
if (AMR::is.mo(x) & isTRUE(attributes(x)$ITIS)) {
# check for new mo class, data coming from ITIS
return(x)
}
failures <- character(0) failures <- character(0)
x_input <- x x_input <- x
# only check the uniques, which is way faster # only check the uniques, which is way faster
x <- unique(x) x <- unique(x)
x_backup <- trimws(x, which = "both") MOs <- NULL # will be set later, if needed
x_species <- paste(x_backup, "species") MOs_mostprevalent <- NULL # will be set later, if needed
# translate to English for supported languages of mo_property MOs_allothers <- NULL # will be set later, if needed
x <- gsub("(Gruppe|gruppe|groep|grupo|gruppo|groupe)", "group", x) MOs_old <- NULL # will be set later, if needed
# remove 'empty' genus and species values
x <- gsub("(no MO)", "", x, fixed = TRUE)
# remove dots and other non-text in case of "E. coli" except spaces
x <- gsub("[^a-zA-Z0-9/ \\-]+", "", x)
# but spaces before and after should be omitted
x <- trimws(x, which = "both")
x_trimmed <- x
x_trimmed_species <- paste(x_trimmed, "species")
# replace space by regex sign
x_withspaces <- gsub(" ", ".* ", x, fixed = TRUE)
x <- gsub(" ", ".*", x, fixed = TRUE)
# add start en stop regex
x <- paste0('^', x, '$')
x_withspaces_all <- x_withspaces
x_withspaces_start <- paste0('^', x_withspaces)
x_withspaces <- paste0('^', x_withspaces, '$')
# cat(paste0('x "', x, '"\n')) if (all(x %in% AMR::microorganisms[, property])) {
# cat(paste0('x_species "', x_species, '"\n')) # already existing mo
# cat(paste0('x_withspaces_all "', x_withspaces_all, '"\n')) } else if (all(x %in% AMR::microorganisms.certe[, "certe"])) {
# cat(paste0('x_withspaces_start "', x_withspaces_start, '"\n')) # old Certe codes
# cat(paste0('x_withspaces "', x_withspaces, '"\n')) suppressWarnings(
# cat(paste0('x_backup "', x_backup, '"\n')) x <- data.frame(certe = x, stringsAsFactors = FALSE) %>%
# cat(paste0('x_trimmed "', x_trimmed, '"\n')) left_join(AMR::microorganisms.certe, by = "certe") %>%
# cat(paste0('x_trimmed_species "', x_trimmed_species, '"\n')) left_join(AMR::microorganisms, by = "mo") %>%
pull(property)
)
} else if (all(x %in% AMR::microorganisms.umcg[, "umcg"])) {
# old UMCG codes
suppressWarnings(
x <- data.frame(umcg = x, stringsAsFactors = FALSE) %>%
left_join(AMR::microorganisms.umcg, by = "umcg") %>%
left_join(AMR::microorganisms.certe, by = "certe") %>%
left_join(AMR::microorganisms, by = "mo") %>%
pull(property)
)
} else {
for (i in 1:length(x)) { MOs <- as.data.table(AMR::microorganisms)
if (identical(x_trimmed[i], "") | is.na(x_trimmed[i])) { setkey(MOs, prevalence, tsn)
# empty values MOs_mostprevalent <- MOs[prevalence != 9999,]
x[i] <- NA
next
}
# translate known trivial abbreviations to genus + species ---- x_backup <- trimws(x, which = "both")
if (!is.na(x_trimmed[i])) { x_species <- paste(x_backup, "species")
if (toupper(x_trimmed[i]) == 'MRSA' # translate to English for supported languages of mo_property
| toupper(x_trimmed[i]) == 'VISA' x <- gsub("(Gruppe|gruppe|groep|grupo|gruppo|groupe)", "group", x)
| toupper(x_trimmed[i]) == 'VRSA') { # remove 'empty' genus and species values
x[i] <- 'B_STPHY_AUR' x <- gsub("(no MO)", "", x, fixed = TRUE)
next # remove dots and other non-text in case of "E. coli" except spaces
} x <- gsub("[^a-zA-Z0-9/ \\-]+", "", x)
if (toupper(x_trimmed[i]) == 'MRSE') { # but spaces before and after should be omitted
x[i] <- 'B_STPHY_EPI' x <- trimws(x, which = "both")
next x_trimmed <- x
} x_trimmed_species <- paste(x_trimmed, "species")
if (toupper(x_trimmed[i]) == 'VRE') { # replace space by regex sign
x[i] <- 'B_ENTRC' x_withspaces <- gsub(" ", ".* ", x, fixed = TRUE)
next x <- gsub(" ", ".*", x, fixed = TRUE)
} # add start en stop regex
if (toupper(x_trimmed[i]) == 'MRPA') { x <- paste0('^', x, '$')
# multi resistant P. aeruginosa x_withspaces_all <- x_withspaces
x[i] <- 'B_PDMNS_AER' x_withspaces_start <- paste0('^', x_withspaces)
next x_withspaces <- paste0('^', x_withspaces, '$')
}
if (toupper(x_trimmed[i]) %in% c('PISP', 'PRSP', 'VISP', 'VRSP')) {
# peni I, peni R, vanco I, vanco R: S. pneumoniae
x[i] <- 'B_STRPTC_PNE'
next
}
if (toupper(x_trimmed[i]) %like% '^G[ABCDFGHK]S$') {
x[i] <- gsub("G([ABCDFGHK])S", "B_STRPTC_GR\\1", x_trimmed[i])
next
}
# CoNS/CoPS in different languages (support for German, Dutch, Spanish, Portuguese) ----
if (tolower(x[i]) %like% '[ck]oagulas[ea] negatie?[vf]'
| tolower(x_trimmed[i]) %like% '[ck]oagulas[ea] negatie?[vf]'
| tolower(x[i]) %like% '[ck]o?ns[^a-z]?$') {
# coerce S. coagulase negative
x[i] <- 'B_STPHY_CNS'
next
}
if (tolower(x[i]) %like% '[ck]oagulas[ea] positie?[vf]'
| tolower(x_trimmed[i]) %like% '[ck]oagulas[ea] positie?[vf]'
| tolower(x[i]) %like% '[ck]o?ps[^a-z]?$') {
# coerce S. coagulase positive
x[i] <- 'B_STPHY_CPS'
next
}
}
# FIRST TRY FULLNAMES AND CODES # cat(paste0('x "', x, '"\n'))
# if only genus is available, don't select species # cat(paste0('x_species "', x_species, '"\n'))
if (all(!c(x[i], x_trimmed[i]) %like% " ")) { # cat(paste0('x_withspaces_all "', x_withspaces_all, '"\n'))
found <- MOs[tolower(fullname) %in% tolower(c(x_species[i], x_trimmed_species[i])), mo] # cat(paste0('x_withspaces_start "', x_withspaces_start, '"\n'))
if (length(found) > 0) { # cat(paste0('x_withspaces "', x_withspaces, '"\n'))
x[i] <- found[1L] # cat(paste0('x_backup "', x_backup, '"\n'))
# cat(paste0('x_trimmed "', x_trimmed, '"\n'))
# cat(paste0('x_trimmed_species "', x_trimmed_species, '"\n'))
for (i in 1:length(x)) {
if (identical(x_trimmed[i], "") | is.na(x_trimmed[i])) {
# empty values
x[i] <- NA
next next
} }
if (nchar(x_trimmed[i]) > 4) {
# not when abbr is esco, stau, klpn, etc. # translate known trivial abbreviations to genus + species ----
found <- MOs[tolower(fullname) %like% gsub(" ", ".*", x_trimmed_species[i], fixed = TRUE), mo] if (!is.na(x_trimmed[i])) {
if (toupper(x_trimmed[i]) == 'MRSA'
| toupper(x_trimmed[i]) == 'VISA'
| toupper(x_trimmed[i]) == 'VRSA') {
x[i] <- MOs[mo == 'B_STPHY_AUR', ..property][[1]][1L]
next
}
if (toupper(x_trimmed[i]) == 'MRSE') {
x[i] <- MOs[mo == 'B_STPHY_EPI', ..property][[1]][1L]
next
}
if (toupper(x_trimmed[i]) == 'VRE') {
x[i] <- MOs[mo == 'B_ENTRC', ..property][[1]][1L]
next
}
if (toupper(x_trimmed[i]) == 'MRPA') {
# multi resistant P. aeruginosa
x[i] <- MOs[mo == 'B_PDMNS_AER', ..property][[1]][1L]
next
}
if (toupper(x_trimmed[i]) %in% c('PISP', 'PRSP', 'VISP', 'VRSP')) {
# peni I, peni R, vanco I, vanco R: S. pneumoniae
x[i] <- MOs[mo == 'B_STRPTC_PNE', ..property][[1]][1L]
next
}
if (toupper(x_trimmed[i]) %like% '^G[ABCDFGHK]S$') {
x[i] <- MOs[mo == gsub("G([ABCDFGHK])S", "B_STRPTC_GR\\1", x_trimmed[i]), ..property][[1]][1L]
next
}
# CoNS/CoPS in different languages (support for German, Dutch, Spanish, Portuguese) ----
if (tolower(x[i]) %like% '[ck]oagulas[ea] negatie?[vf]'
| tolower(x_trimmed[i]) %like% '[ck]oagulas[ea] negatie?[vf]'
| tolower(x[i]) %like% '[ck]o?ns[^a-z]?$') {
# coerce S. coagulase negative
x[i] <- MOs[mo == 'B_STPHY_CNS', ..property][[1]][1L]
next
}
if (tolower(x[i]) %like% '[ck]oagulas[ea] positie?[vf]'
| tolower(x_trimmed[i]) %like% '[ck]oagulas[ea] positie?[vf]'
| tolower(x[i]) %like% '[ck]o?ps[^a-z]?$') {
# coerce S. coagulase positive
x[i] <- MOs[mo == 'B_STPHY_CPS', ..property][[1]][1L]
next
}
}
# FIRST TRY FULLNAMES AND CODES
# if only genus is available, don't select species
if (all(!c(x[i], x_trimmed[i]) %like% " ")) {
found <- MOs[tolower(fullname) %in% tolower(c(x_species[i], x_trimmed_species[i])), ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
if (nchar(x_trimmed[i]) > 4) {
# not when abbr is esco, stau, klpn, etc.
found <- MOs[tolower(fullname) %like% gsub(" ", ".*", x_trimmed_species[i], fixed = TRUE), ..property][[1]]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
}
} }
}
# search for GLIMS code ---- # TRY FIRST THOUSAND MOST PREVALENT IN HUMAN INFECTIONS ----
found <- AMR::microorganisms.umcg[which(toupper(AMR::microorganisms.umcg$umcg) == toupper(x_trimmed[i])),]$mo
if (length(found) > 0) {
x[i] <- MOs[mo.old == found, mo][1L]
next
}
# TRY FIRST THOUSAND MOST PREVALENT IN HUMAN INFECTIONS ---- found <- MOs_mostprevalent[tolower(fullname) %in% tolower(c(x_backup[i], x_trimmed[i])), ..property][[1]]
# most probable: is exact match in fullname
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_mostprevalent[tsn == x_trimmed[i], ..property][[1]]
# is a valid TSN
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_mostprevalent[mo == toupper(x_backup[i]), ..property][[1]]
# is a valid mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_mostprevalent[tolower(fullname) %in% tolower(c(x_backup[i], x_trimmed[i])), mo] # try any match keeping spaces ----
# most probable: is exact match in fullname found <- MOs_mostprevalent[fullname %like% x_withspaces[i], ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
found <- MOs_mostprevalent[tsn == x_trimmed[i], mo]
# is a valid TSN
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_mostprevalent[mo == toupper(x_backup[i]), mo]
# is a valid mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_mostprevalent[mo.old == toupper(x_backup[i])
| (substr(x_backup[i], 4, 6) == "SPP" & mo.old == substr(x_backup[i], 1, 3))
| mo.old == substr(x_backup[i], 1, 3), mo]
# is a valid old mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try any match keeping spaces ---- # try any match keeping spaces, not ending with $ ----
found <- MOs_mostprevalent[fullname %like% x_withspaces[i], mo] found <- MOs_mostprevalent[fullname %like% x_withspaces_start[i], ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
# try any match keeping spaces, not ending with $ ---- # try any match diregarding spaces ----
found <- MOs_mostprevalent[fullname %like% x_withspaces_start[i], mo] found <- MOs_mostprevalent[fullname %like% x[i], ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
# try any match diregarding spaces ---- # try fullname without start and stop regex, to also find subspecies ----
found <- MOs_mostprevalent[fullname %like% x[i], mo] # like "K. pneu rhino" -> "Klebsiella pneumoniae (rhinoscleromatis)" = KLEPNERH
if (length(found) > 0) { found <- MOs_mostprevalent[fullname %like% x_withspaces_start[i], ..property][[1]]
x[i] <- found[1L] if (length(found) > 0) {
next x[i] <- found[1L]
} next
}
# try fullname without start and stop regex, to also find subspecies ---- # try splitting of characters and then find ID ----
# like "K. pneu rhino" -> "Klebsiella pneumoniae (rhinoscleromatis)" = KLEPNERH # like esco = E. coli, klpn = K. pneumoniae, stau = S. aureus
found <- MOs_mostprevalent[fullname %like% x_withspaces_start[i], mo] x_split <- x
if (length(found) > 0) { x_length <- nchar(x_trimmed[i])
x[i] <- found[1L] x_split[i] <- paste0(x_trimmed[i] %>% substr(1, x_length / 2) %>% trimws(),
next '.* ',
} x_trimmed[i] %>% substr((x_length / 2) + 1, x_length) %>% trimws())
found <- MOs_mostprevalent[fullname %like% paste0('^', x_split[i]), ..property][[1]]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try splitting of characters and then find ID ---- # try any match with text before and after original search string ----
# like esco = E. coli, klpn = K. pneumoniae, stau = S. aureus # so "negative rods" will be "GNR"
x_split <- x # if (x_trimmed[i] %like% "^Gram") {
x_length <- nchar(x_trimmed[i]) # x_trimmed[i] <- gsub("^Gram", "", x_trimmed[i], ignore.case = TRUE)
x_split[i] <- paste0(x_trimmed[i] %>% substr(1, x_length / 2) %>% trimws(), # # remove leading and trailing spaces again
'.* ', # x_trimmed[i] <- trimws(x_trimmed[i], which = "both")
x_trimmed[i] %>% substr((x_length / 2) + 1, x_length) %>% trimws()) # }
found <- MOs_mostprevalent[fullname %like% paste0('^', x_split[i]), mo] # if (!is.na(x_trimmed[i])) {
if (length(found) > 0) { # found <- MOs_mostprevalent[fullname %like% x_trimmed[i], ..property][[1]]
x[i] <- found[1L] # if (length(found) > 0) {
next # x[i] <- found[1L]
} # next
# }
# }
# try any match with text before and after original search string ---- # THEN TRY ALL OTHERS ----
# so "negative rods" will be "GNR" if (is.null(MOs_allothers)) {
# if (x_trimmed[i] %like% "^Gram") { MOs_allothers <- MOs[prevalence == 9999,]
# x_trimmed[i] <- gsub("^Gram", "", x_trimmed[i], ignore.case = TRUE) }
# # remove leading and trailing spaces again
# x_trimmed[i] <- trimws(x_trimmed[i], which = "both")
# }
# if (!is.na(x_trimmed[i])) {
# found <- MOs_mostprevalent[fullname %like% x_trimmed[i], mo]
# if (length(found) > 0) {
# x[i] <- found[1L]
# next
# }
# }
# THEN TRY ALL OTHERS ---- found <- MOs_allothers[tolower(fullname) == tolower(x_backup[i]), ..property][[1]]
if (is.null(MOs_allothers)) { # most probable: is exact match in fullname
MOs_allothers <- MOs[prevalence == 9999,] if (length(found) > 0) {
} x[i] <- found[1L]
next
}
found <- MOs_allothers[tolower(fullname) == tolower(x_trimmed[i]), ..property][[1]]
# most probable: is exact match in fullname
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_allothers[tsn == x_trimmed[i], ..property][[1]]
# is a valid TSN
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_allothers[mo == toupper(x_backup[i]), ..property][[1]]
# is a valid mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_allothers[tolower(fullname) == tolower(x_backup[i]), mo] # try any match keeping spaces ----
# most probable: is exact match in fullname found <- MOs_allothers[fullname %like% x_withspaces[i], ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
found <- MOs_allothers[tolower(fullname) == tolower(x_trimmed[i]), mo]
# most probable: is exact match in fullname
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_allothers[tsn == x_trimmed[i], mo]
# is a valid TSN
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_allothers[mo == toupper(x_backup[i]), mo]
# is a valid mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
found <- MOs_allothers[mo.old == toupper(x_backup[i])
| (substr(x_backup[i], 4, 6) == "SPP" & mo.old == substr(x_backup[i], 1, 3))
| mo.old == substr(x_backup[i], 1, 3), mo]
# is a valid old mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try any match keeping spaces ---- # try any match keeping spaces, not ending with $ ----
found <- MOs_allothers[fullname %like% x_withspaces[i], mo] found <- MOs_allothers[fullname %like% x_withspaces_start[i], ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
# try any match keeping spaces, not ending with $ ---- # try any match diregarding spaces ----
found <- MOs_allothers[fullname %like% x_withspaces_start[i], mo] found <- MOs_allothers[fullname %like% x[i], ..property][[1]]
if (length(found) > 0) { if (length(found) > 0) {
x[i] <- found[1L] x[i] <- found[1L]
next next
} }
# try any match diregarding spaces ---- # try fullname without start and stop regex, to also find subspecies ----
found <- MOs_allothers[fullname %like% x[i], mo] # like "K. pneu rhino" -> "Klebsiella pneumoniae (rhinoscleromatis)" = KLEPNERH
if (length(found) > 0) { found <- MOs_allothers[fullname %like% x_withspaces_start[i], ..property][[1]]
x[i] <- found[1L] if (length(found) > 0) {
next x[i] <- found[1L]
} next
}
# try fullname without start and stop regex, to also find subspecies ---- # try splitting of characters and then find ID ----
# like "K. pneu rhino" -> "Klebsiella pneumoniae (rhinoscleromatis)" = KLEPNERH # like esco = E. coli, klpn = K. pneumoniae, stau = S. aureus
found <- MOs_allothers[fullname %like% x_withspaces_start[i], mo] x_split <- x
if (length(found) > 0) { x_length <- nchar(x_trimmed[i])
x[i] <- found[1L] x_split[i] <- paste0(x_trimmed[i] %>% substr(1, x_length / 2) %>% trimws(),
next '.* ',
} x_trimmed[i] %>% substr((x_length / 2) + 1, x_length) %>% trimws())
found <- MOs_allothers[fullname %like% paste0('^', x_split[i]), ..property][[1]]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try splitting of characters and then find ID ---- # # try any match with text before and after original search string ----
# like esco = E. coli, klpn = K. pneumoniae, stau = S. aureus # # so "negative rods" will be "GNR"
x_split <- x # if (x_trimmed[i] %like% "^Gram") {
x_length <- nchar(x_trimmed[i]) # x_trimmed[i] <- gsub("^Gram", "", x_trimmed[i], ignore.case = TRUE)
x_split[i] <- paste0(x_trimmed[i] %>% substr(1, x_length / 2) %>% trimws(), # # remove leading and trailing spaces again
'.* ', # x_trimmed[i] <- trimws(x_trimmed[i], which = "both")
x_trimmed[i] %>% substr((x_length / 2) + 1, x_length) %>% trimws()) # }
found <- MOs_allothers[fullname %like% paste0('^', x_split[i]), mo] # if (!is.na(x_trimmed[i])) {
if (length(found) > 0) { # found <- MOs_allothers[fullname %like% x_trimmed[i], ..property][[1]]
x[i] <- found[1L] # if (length(found) > 0) {
next # x[i] <- found[1L]
} # next
# }
# }
# # try any match with text before and after original search string ---- # MISCELLANEOUS ----
# # so "negative rods" will be "GNR"
# if (x_trimmed[i] %like% "^Gram") {
# x_trimmed[i] <- gsub("^Gram", "", x_trimmed[i], ignore.case = TRUE)
# # remove leading and trailing spaces again
# x_trimmed[i] <- trimws(x_trimmed[i], which = "both")
# }
# if (!is.na(x_trimmed[i])) {
# found <- MOs_allothers[fullname %like% x_trimmed[i], mo]
# if (length(found) > 0) {
# x[i] <- found[1L]
# next
# }
# }
# MISCELLANEOUS ---- # look for old taxonomic names ----
if (is.null(MOs_old)) {
# look for old taxonomic names ---- MOs_old <- as.data.table(AMR::microorganisms.old)
if (is.null(MOs_old)) { setkey(MOs_old, name, tsn_new)
MOs_old <- as.data.table(AMR::microorganisms.old) }
setkey(MOs_old, name, tsn_new) found <- MOs_old[tolower(name) == tolower(x_backup[i]) |
} tsn == x_trimmed[i],]
found <- MOs_old[tolower(name) == tolower(x_backup[i]) |
tsn == x_trimmed[i],]
if (NROW(found) > 0) {
x[i] <- MOs[tsn == found[1, tsn_new], mo]
renamed_note(name_old = found[1, name],
name_new = MOs[tsn == found[1, tsn_new], fullname],
authors = found[1, authors],
year = found[1, year])
next
}
# check for uncertain results ----
if (allow_uncertain == TRUE) {
# (1) look again for old taxonomic names, now for G. species ----
found <- MOs_old[name %like% x_withspaces[i]
| name %like% x_withspaces_start[i]
| name %like% x[i],]
if (NROW(found) > 0) { if (NROW(found) > 0) {
x[i] <- MOs[tsn == found[1, tsn_new], mo] x[i] <- MOs[tsn == found[1, tsn_new], ..property][[1]]
warning("Uncertain interpretation: '",
x_backup[i], "' -> '", found[1, name], "'",
call. = FALSE, immediate. = TRUE)
renamed_note(name_old = found[1, name], renamed_note(name_old = found[1, name],
name_new = MOs[tsn == found[1, tsn_new], fullname], name_new = MOs[tsn == found[1, tsn_new], fullname],
authors = found[1, authors], authors = found[1, authors],
year = found[1, year]) year = found[1, year])
next next
} }
# (2) try to strip off one element and check the remains # check for uncertain results ----
x_strip <- x_backup[i] %>% strsplit(" ") %>% unlist() if (allow_uncertain == TRUE) {
x_strip <- x_strip[1:length(x_strip) - 1] # (1) look again for old taxonomic names, now for G. species ----
x[i] <- suppressWarnings(suppressMessages(as.mo(x_strip))) found <- MOs_old[name %like% x_withspaces[i]
if (!is.na(x[i])) { | name %like% x_withspaces_start[i]
warning("Uncertain interpretation: '", | name %like% x[i],]
x_backup[i], "' -> '", MOs[mo == x[i], fullname], "' (", x[i], ")", if (NROW(found) > 0) {
call. = FALSE, immediate. = TRUE) x[i] <- MOs[tsn == found[1, tsn_new], ..property][[1]]
next warning("Uncertain interpretation: '",
x_backup[i], "' -> '", found[1, name], "'",
call. = FALSE, immediate. = TRUE)
renamed_note(name_old = found[1, name],
name_new = MOs[tsn == found[1, tsn_new], fullname],
authors = found[1, authors],
year = found[1, year])
next
}
# (2) try to strip off one element and check the remains
x_strip <- x_backup[i] %>% strsplit(" ") %>% unlist()
x_strip <- x_strip[1:length(x_strip) - 1]
x[i] <- suppressWarnings(suppressMessages(as.mo(x_strip)))
if (!is.na(x[i])) {
warning("Uncertain interpretation: '",
x_backup[i], "' -> '", MOs[mo == x[i], fullname], "' (", x[i], ")",
call. = FALSE, immediate. = TRUE)
next
}
} }
# not found ----
x[i] <- NA_character_
failures <- c(failures, x_backup[i])
} }
# not found ----
x[i] <- NA_character_
failures <- c(failures, x_backup[i])
} }
failures <- failures[!failures %in% c(NA, NULL, NaN)] failures <- failures[!failures %in% c(NA, NULL, NaN)]
@ -529,6 +533,10 @@ as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE
if (Becker == TRUE | Becker == "all") { if (Becker == TRUE | Becker == "all") {
# See Source. It's this figure: # See Source. It's this figure:
# https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4187637/figure/F3/ # https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4187637/figure/F3/
if (is.null(MOs)) {
MOs <- as.data.table(AMR::microorganisms)
setkey(MOs, prevalence, tsn)
}
MOs_staph <- MOs[genus == "Staphylococcus"] MOs_staph <- MOs[genus == "Staphylococcus"]
setkey(MOs_staph, species) setkey(MOs_staph, species)
CoNS <- MOs_staph[species %in% c("arlettae", "auricularis", "capitis", CoNS <- MOs_staph[species %in% c("arlettae", "auricularis", "capitis",
@ -541,65 +549,66 @@ as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE
"pettenkoferi", "piscifermentans", "rostri", "pettenkoferi", "piscifermentans", "rostri",
"saccharolyticus", "saprophyticus", "sciuri", "saccharolyticus", "saprophyticus", "sciuri",
"stepanovicii", "simulans", "succinus", "stepanovicii", "simulans", "succinus",
"vitulinus", "warneri", "xylosus"), mo] "vitulinus", "warneri", "xylosus"), ..property][[1]]
CoPS <- MOs_staph[species %in% c("simiae", "agnetis", "chromogenes", CoPS <- MOs_staph[species %in% c("simiae", "agnetis", "chromogenes",
"delphini", "felis", "lutrae", "delphini", "felis", "lutrae",
"hyicus", "intermedius", "hyicus", "intermedius",
"pseudintermedius", "pseudointermedius", "pseudintermedius", "pseudointermedius",
"schleiferi"), mo] "schleiferi"), ..property][[1]]
x[x %in% CoNS] <- "B_STPHY_CNS" x[x %in% CoNS] <- MOs[mo == 'B_STPHY_CNS', ..property][[1]][1L]
x[x %in% CoPS] <- "B_STPHY_CPS" x[x %in% CoPS] <- MOs[mo == 'B_STPHY_CPS', ..property][[1]][1L]
if (Becker == "all") { if (Becker == "all") {
x[x == "B_STPHY_AUR"] <- "B_STPHY_CPS" x[x == MOs[mo == 'B_STPHY_AUR', ..property][[1]][1L]] <- MOs[mo == 'B_STPHY_CPS', ..property][[1]][1L]
} }
} }
# Lancefield ---- # Lancefield ----
if (Lancefield == TRUE | Lancefield == "all") { if (Lancefield == TRUE | Lancefield == "all") {
# group A if (is.null(MOs)) {
x[x == "B_STRPTC_PYO"] <- "B_STRPTC_GRA" # S. pyogenes MOs <- as.data.table(AMR::microorganisms)
# group B setkey(MOs, prevalence, tsn)
x[x == "B_STRPTC_AGA"] <- "B_STRPTC_GRB" # S. agalactiae }
# group A - S. pyogenes
x[x == MOs[mo == 'B_STRPTC_PYO', ..property][[1]][1L]] <- MOs[mo == 'B_STRPTC_GRA', ..property][[1]][1L]
# group B - S. agalactiae
x[x == MOs[mo == 'B_STRPTC_AGA', ..property][[1]][1L]] <- MOs[mo == 'B_STRPTC_GRB', ..property][[1]][1L]
# group C # group C
S_groupC <- MOs %>% filter(genus == "Streptococcus", S_groupC <- MOs %>% filter(genus == "Streptococcus",
species %in% c("equisimilis", "equi", species %in% c("equisimilis", "equi",
"zooepidemicus", "dysgalactiae")) %>% "zooepidemicus", "dysgalactiae")) %>%
pull(mo) pull(property)
x[x %in% S_groupC] <- "B_STRPTC_GRC" # S. agalactiae x[x %in% S_groupC] <- MOs[mo == 'B_STRPTC_GRC', ..property][[1]][1L]
if (Lancefield == "all") { if (Lancefield == "all") {
x[substr(x, 1, 7) == "B_ENTRC"] <- "B_STRPTC_GRD" # all Enterococci # all Enterococci
x[x %like% "^(Enterococcus|B_ENTRC)"] <- MOs[mo == 'B_STRPTC_GRD', ..property][[1]][1L]
} }
# group F # group F - S. anginosus
x[x == "B_STRPTC_ANG"] <- "B_STRPTC_GRF" # S. anginosus x[x == MOs[mo == 'B_STRPTC_ANG', ..property][[1]][1L]] <- MOs[mo == 'B_STRPTC_GRF', ..property][[1]][1L]
# group H # group H - S. sanguinis
x[x == "B_STRPTC_SAN"] <- "B_STRPTC_GRH" # S. sanguinis x[x == MOs[mo == 'B_STRPTC_SAN', ..property][[1]][1L]] <- MOs[mo == 'B_STRPTC_GRH', ..property][[1]][1L]
# group K # group K - S. salivarius
x[x == "B_STRPTC_SAL"] <- "B_STRPTC_GRK" # S. salivarius x[x == MOs[mo == 'B_STRPTC_SAL', ..property][[1]][1L]] <- MOs[mo == 'B_STRPTC_GRK', ..property][[1]][1L]
} }
# left join the found results to the original input values (x_input) # left join the found results to the original input values (x_input)
DT_found <- data.table(input = as.character(unique(x_input)), df_found <- data.frame(input = as.character(unique(x_input)),
found = x, found = x,
key = "input",
stringsAsFactors = FALSE) stringsAsFactors = FALSE)
DT_input <- data.table(input = as.character(x_input), df_input <- data.frame(input = as.character(x_input),
key = "input",
stringsAsFactors = FALSE) stringsAsFactors = FALSE)
x <- DT_found[DT_input, on = "input", found] x <- df_input %>%
left_join(df_found,
by = "input") %>%
pull(found)
# df_found <- data.frame(input = as.character(unique(x_input)), if (property == "mo") {
# found = x, class(x) <- "mo"
# stringsAsFactors = FALSE) attr(x, 'package') <- 'AMR'
# df_input <- data.frame(input = as.character(x_input), attr(x, 'ITIS') <- TRUE
# stringsAsFactors = FALSE) } else if (property == "tsn") {
# x <- df_input %>% x <- as.integer(x)
# left_join(df_found, }
# by = "input") %>%
# pull(found)
class(x) <- "mo"
attr(x, 'package') <- 'AMR'
attr(x, 'ITIS') <- TRUE
x x
} }
@ -614,19 +623,6 @@ renamed_note <- function(name_old, name_new, authors, year) {
base::message(msg) base::message(msg)
} }
#' @rdname as.mo
#' @export
is.mo <- function(x) {
# bactid for older releases
# remove when is.bactid will be removed
identical(class(x), "mo") | identical(class(x), "bactid")
}
#' @rdname as.mo
#' @export
guess_mo <- as.mo
#' @exportMethod print.mo #' @exportMethod print.mo
#' @export #' @export
#' @noRd #' @noRd

View File

@ -27,7 +27,7 @@
#' @inheritSection as.mo Source #' @inheritSection as.mo Source
#' @rdname mo_property #' @rdname mo_property
#' @name mo_property #' @name mo_property
#' @return A logical (in case of \code{mo_aerobic}), a list (in case of \code{mo_taxonomy}), a character otherwise #' @return A \code{list} (in case of \code{mo_taxonomy}) or a \code{character} otherwise
#' @export #' @export
#' @seealso \code{\link{microorganisms}} #' @seealso \code{\link{microorganisms}}
#' @examples #' @examples
@ -39,7 +39,7 @@
#' mo_family("E. coli") # "Enterobacteriaceae" #' mo_family("E. coli") # "Enterobacteriaceae"
#' mo_genus("E. coli") # "Escherichia" #' mo_genus("E. coli") # "Escherichia"
#' mo_species("E. coli") # "coli" #' mo_species("E. coli") # "coli"
#' mo_subspecies("E. coli") # "" #' mo_subspecies("E. coli") # NA
#' mo_fullname("E. coli") # "Escherichia coli" #' mo_fullname("E. coli") # "Escherichia coli"
#' mo_shortname("E. coli") # "E. coli" #' mo_shortname("E. coli") # "E. coli"
#' mo_gramstain("E. coli") # "Gram negative" #' mo_gramstain("E. coli") # "Gram negative"
@ -98,15 +98,17 @@
#' # Complete taxonomy up to Subkingdom, returns a list #' # Complete taxonomy up to Subkingdom, returns a list
#' mo_taxonomy("E. coli") #' mo_taxonomy("E. coli")
mo_fullname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) { mo_fullname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) {
mo_property(x, "fullname", Becker = Becker, Lancefield = Lancefield, language = language) x <- mo_validate(x = x, property = "fullname", Becker = Becker, Lancefield = Lancefield)
mo_translate(x, language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @importFrom dplyr %>% left_join mutate pull
#' @export #' @export
mo_shortname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) { mo_shortname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) {
if (Becker %in% c(TRUE, "all") | Lancefield == TRUE) { if (Becker %in% c(TRUE, "all") | Lancefield == TRUE) {
res1 <- as.mo(x) res1 <- AMR::as.mo(x)
res2 <- suppressWarnings(as.mo(x, Becker = Becker, Lancefield = Lancefield)) res2 <- suppressWarnings(AMR::as.mo(res1, Becker = Becker, Lancefield = Lancefield))
res2_fullname <- mo_fullname(res2) res2_fullname <- mo_fullname(res2)
res2_fullname[res2_fullname %like% "\\(CoNS\\)"] <- "CoNS" res2_fullname[res2_fullname %like% "\\(CoNS\\)"] <- "CoNS"
res2_fullname[res2_fullname %like% "\\(CoPS\\)"] <- "CoPS" res2_fullname[res2_fullname %like% "\\(CoPS\\)"] <- "CoPS"
@ -115,8 +117,8 @@ mo_shortname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL)
res2_fullname) # turn "Streptococcus group A" and "Streptococcus grupo A" to "GAS" res2_fullname) # turn "Streptococcus group A" and "Streptococcus grupo A" to "GAS"
res2_fullname_vector <- res2_fullname[res2_fullname == mo_fullname(res1)] res2_fullname_vector <- res2_fullname[res2_fullname == mo_fullname(res1)]
res2_fullname[res2_fullname == mo_fullname(res1)] <- paste0(substr(mo_genus(res2_fullname_vector), 1, 1), res2_fullname[res2_fullname == mo_fullname(res1)] <- paste0(substr(mo_genus(res2_fullname_vector), 1, 1),
". ", ". ",
suppressWarnings(mo_species(res2_fullname_vector))) suppressWarnings(mo_species(res2_fullname_vector)))
if (sum(res1 == res2, na.rm = TRUE) > 0) { if (sum(res1 == res2, na.rm = TRUE) > 0) {
res1[res1 == res2] <- paste0(substr(mo_genus(res1[res1 == res2]), 1, 1), res1[res1 == res2] <- paste0(substr(mo_genus(res1[res1 == res2]), 1, 1),
". ", ". ",
@ -126,112 +128,115 @@ mo_shortname <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL)
result <- as.character(res1) result <- as.character(res1)
} else { } else {
x <- AMR::as.mo(x) x <- AMR::as.mo(x)
# return G. species result <- data.frame(mo = x) %>%
result <- paste0(substr(mo_genus(x), 1, 1), ". ", suppressWarnings(mo_species(x))) left_join(AMR::microorganisms, by = "mo") %>%
mutate(shortname = ifelse(!is.na(genus) & !is.na(species), paste0(substr(genus, 1, 1), ". ", species), NA_character_)) %>%
pull(shortname)
} }
result[result %in% c(". ", "(. ")] <- ""
mo_translate(result, language = language) mo_translate(result, language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_subspecies <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) { mo_subspecies <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) {
mo_property(x, "subspecies", Becker = Becker, Lancefield = Lancefield, language = language) mo_translate(exec_as.mo(x,
Becker = Becker,
Lancefield = Lancefield,
property = "subspecies"),
language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_species <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) { mo_species <- function(x, Becker = FALSE, Lancefield = FALSE, language = NULL) {
mo_property(x, "species", Becker = Becker, Lancefield = Lancefield, language = language) x <- mo_validate(x = x, property = "species", Becker = Becker, Lancefield = Lancefield)
mo_translate(x, language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_genus <- function(x, language = NULL) { mo_genus <- function(x, language = NULL) {
mo_property(x, "genus", language = language) x <- mo_validate(x = x, property = "genus")
mo_translate(x, language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_family <- function(x) { mo_family <- function(x) {
mo_property(x, "family") mo_validate(x = x, property = "family")
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_order <- function(x) { mo_order <- function(x) {
mo_property(x, "order") mo_validate(x = x, property = "order")
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_class <- function(x) { mo_class <- function(x) {
mo_property(x, "class") mo_validate(x = x, property = "class")
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_phylum <- function(x) { mo_phylum <- function(x) {
mo_property(x, "phylum") mo_validate(x = x, property = "phylum")
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_subkingdom <- function(x) { mo_subkingdom <- function(x) {
mo_property(x, "subkingdom") mo_validate(x = x, property = "subkingdom")
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_type <- function(x, language = NULL) { mo_type <- function(x, language = NULL) {
mo_property(x, "type", language = language) x <- mo_validate(x = x, property = "type")
mo_translate(x, language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_TSN <- function(x) { mo_TSN <- function(x) {
mo_property(x, "tsn") mo_validate(x = x, property = "tsn")
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_gramstain <- function(x, language = NULL) { mo_gramstain <- function(x, language = NULL) {
mo_property(x, "gramstain", language = language) x <- mo_validate(x = x, property = "gramstain")
mo_translate(x, language = language)
} }
#' @rdname mo_property #' @rdname mo_property
#' @importFrom data.table data.table as.data.table setkey #' @importFrom data.table data.table as.data.table setkey
#' @export #' @export
mo_property <- function(x, property = 'fullname', Becker = FALSE, Lancefield = FALSE, language = NULL) { mo_property <- function(x, property = 'fullname', Becker = FALSE, Lancefield = FALSE, language = NULL) {
property <- tolower(property[1]) if (length(property) != 1L) {
stop("'property' must be of length 1.")
}
if (!property %in% colnames(AMR::microorganisms)) { if (!property %in% colnames(AMR::microorganisms)) {
stop("invalid property: ", property, " - use a column name of the `microorganisms` data set") stop("invalid property: '", property, "' - use a column name of the `microorganisms` data set")
} }
if (Becker == TRUE | Lancefield == TRUE | !is.mo(x)) {
# this will give a warning if x cannot be coerced
x <- AMR::as.mo(x = x, Becker = Becker, Lancefield = Lancefield)
}
A <- data.table(mo = x, stringsAsFactors = FALSE)
B <- as.data.table(AMR::microorganisms)
setkey(B, mo)
result2 <- B[A, on = 'mo', ..property][[1]]
if (property == "tsn") { # this will give a warning if x cannot be coerced
result2 <- as.integer(result2) res <- exec_as.mo(x = x, Becker = Becker, Lancefield = Lancefield, property = property)
} else {
# will else not retain `logical` class if (property != "tsn") {
result2[x %in% c("", NA) | result2 %in% c("", NA, "(no MO)")] <- "" res[x %in% c("", NA) | res %in% c("", NA, "(no MO)")] <- ""
if (property %in% c("fullname", "shortname", "genus", "species", "subspecies", "type", "gramstain")) { if (property %in% c("fullname", "shortname", "genus", "species", "subspecies", "type", "gramstain")) {
result2 <- mo_translate(result2, language = language) res <- mo_translate(res, language = language)
} }
} }
result2 res
} }
#' @rdname mo_property #' @rdname mo_property
#' @export #' @export
mo_taxonomy <- function(x) { mo_taxonomy <- function(x) {
x <- as.mo(x) x <- AMR::as.mo(x)
base::list(subkingdom = mo_subkingdom(x), base::list(subkingdom = mo_subkingdom(x),
phylum = mo_phylum(x), phylum = mo_phylum(x),
class = mo_class(x), class = mo_class(x),
@ -247,7 +252,7 @@ mo_translate <- function(x, language) {
if (is.null(language)) { if (is.null(language)) {
language <- getOption("AMR_locale", default = "en")[1L] language <- getOption("AMR_locale", default = "en")[1L]
} else { } else {
language <- tolower(language[1]) language <- tolower(language[1L])
} }
if (language %in% c("en", "")) { if (language %in% c("en", "")) {
return(x) return(x)
@ -364,3 +369,14 @@ mo_translate <- function(x, language) {
) )
} }
mo_validate <- function(x, property, Becker = FALSE, Lancefield = FALSE) {
if (!all(x %in% AMR::microorganisms[, property]) | Becker %in% c(TRUE, "all") | Lancefield == TRUE) {
exec_as.mo(x,
Becker = Becker,
Lancefield = Lancefield,
property = property)
} else {
x
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -65,7 +65,7 @@ This means that looking up human non-pathogenic microorganisms takes a longer ti
\section{ITIS}{ \section{ITIS}{
\if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr} \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr}
This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, https://www.itis.gov). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS. This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, \url{https://www.itis.gov}). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS.
} }
\section{Source}{ \section{Source}{

View File

@ -34,7 +34,7 @@ A data set containing the complete microbial taxonomy of the kingdoms Bacteria,
\section{ITIS}{ \section{ITIS}{
\if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr} \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr}
This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, https://www.itis.gov). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS. This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, \url{https://www.itis.gov}). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS.
} }
\seealso{ \seealso{

View File

@ -0,0 +1,21 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.R
\docType{data}
\name{microorganisms.certe}
\alias{microorganisms.certe}
\title{Translation table for Certe}
\format{A \code{\link{tibble}} with 2,664 observations and 2 variables:
\describe{
\item{\code{certe}}{Code of microorganism according to Certe MMB}
\item{\code{mo}}{Code of microorganism in \code{\link{microorganisms}}}
}}
\usage{
microorganisms.certe
}
\description{
A data set containing all bacteria codes of Certe MMB. These codes can be joined to data with an ID from \code{\link{microorganisms}$mo} (using \code{\link{left_join_microorganisms}}). GLIMS codes can also be translated to valid \code{MO}s with \code{\link{guess_mo}}.
}
\seealso{
\code{\link{as.mo}} \code{\link{microorganisms}}
}
\keyword{datasets}

View File

@ -4,7 +4,7 @@
\name{microorganisms.old} \name{microorganisms.old}
\alias{microorganisms.old} \alias{microorganisms.old}
\title{Data set with old taxonomic data from ITIS} \title{Data set with old taxonomic data from ITIS}
\format{A \code{\link{data.frame}} with 1,682 observations and 5 variables: \format{A \code{\link{data.frame}} with 2,384 observations and 5 variables:
\describe{ \describe{
\item{\code{tsn}}{Old Taxonomic Serial Number (TSN), as defined by ITIS} \item{\code{tsn}}{Old Taxonomic Serial Number (TSN), as defined by ITIS}
\item{\code{name}}{Old taxonomic name of the microorganism as found in ITIS, see Source} \item{\code{name}}{Old taxonomic name of the microorganism as found in ITIS, see Source}
@ -19,12 +19,12 @@
microorganisms.old microorganisms.old
} }
\description{ \description{
A data set containing old, previously valid, taxonomic names. This data set is used internally by \code{\link{as.mo}}. A data set containing old (previously valid or accepted) taxonomic names according to ITIS. This data set is used internally by \code{\link{as.mo}}.
} }
\section{ITIS}{ \section{ITIS}{
\if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr} \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr}
This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, https://www.itis.gov). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS. This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, \url{https://www.itis.gov}). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS.
} }
\seealso{ \seealso{

View File

@ -7,7 +7,7 @@
\format{A \code{\link{tibble}} with 1,095 observations and 2 variables: \format{A \code{\link{tibble}} with 1,095 observations and 2 variables:
\describe{ \describe{
\item{\code{umcg}}{Code of microorganism according to UMCG MMB} \item{\code{umcg}}{Code of microorganism according to UMCG MMB}
\item{\code{mo}}{Code of microorganism in \code{\link{microorganisms}}} \item{\code{certe}}{Code of microorganism according to Certe MMB}
}} }}
\usage{ \usage{
microorganisms.umcg microorganisms.umcg
@ -16,6 +16,6 @@ microorganisms.umcg
A data set containing all bacteria codes of UMCG MMB. These codes can be joined to data with an ID from \code{\link{microorganisms}$mo} (using \code{\link{left_join_microorganisms}}). GLIMS codes can also be translated to valid \code{MO}s with \code{\link{guess_mo}}. A data set containing all bacteria codes of UMCG MMB. These codes can be joined to data with an ID from \code{\link{microorganisms}$mo} (using \code{\link{left_join_microorganisms}}). GLIMS codes can also be translated to valid \code{MO}s with \code{\link{guess_mo}}.
} }
\seealso{ \seealso{
\code{\link{as.mo}} \code{\link{microorganisms}} \code{\link{as.mo}} \code{\link{microorganisms.certe}} \code{\link{microorganisms}}
} }
\keyword{datasets} \keyword{datasets}

View File

@ -65,7 +65,7 @@ mo_taxonomy(x)
\item{property}{one of the column names of one of the \code{\link{microorganisms}} data set or \code{"shortname"}} \item{property}{one of the column names of one of the \code{\link{microorganisms}} data set or \code{"shortname"}}
} }
\value{ \value{
A logical (in case of \code{mo_aerobic}), a list (in case of \code{mo_taxonomy}), a character otherwise A \code{list} (in case of \code{mo_taxonomy}) or a \code{character} otherwise
} }
\description{ \description{
Use these functions to return a specific property of a microorganism from the \code{\link{microorganisms}} data set. All input values will be evaluated internally with \code{\link{as.mo}}. Use these functions to return a specific property of a microorganism from the \code{\link{microorganisms}} data set. All input values will be evaluated internally with \code{\link{as.mo}}.
@ -73,7 +73,7 @@ Use these functions to return a specific property of a microorganism from the \c
\section{ITIS}{ \section{ITIS}{
\if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr} \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr}
This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, https://www.itis.gov). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS. This \code{AMR} package contains the \strong{complete microbial taxonomic data} (with seven taxonomic ranks - from subkingdom to subspecies) from the publicly available Integrated Taxonomic Information System (ITIS, \url{https://www.itis.gov}). ITIS is a partnership of U.S., Canadian, and Mexican agencies and taxonomic specialists [3]. The complete taxonomic kingdoms Bacteria, Fungi and Protozoa are included in this package, as well as all previously accepted names known to ITIS.
} }
\section{Source}{ \section{Source}{
@ -94,7 +94,7 @@ mo_order("E. coli") # "Enterobacteriales"
mo_family("E. coli") # "Enterobacteriaceae" mo_family("E. coli") # "Enterobacteriaceae"
mo_genus("E. coli") # "Escherichia" mo_genus("E. coli") # "Escherichia"
mo_species("E. coli") # "coli" mo_species("E. coli") # "coli"
mo_subspecies("E. coli") # "" mo_subspecies("E. coli") # NA
mo_fullname("E. coli") # "Escherichia coli" mo_fullname("E. coli") # "Escherichia coli"
mo_shortname("E. coli") # "E. coli" mo_shortname("E. coli") # "E. coli"
mo_gramstain("E. coli") # "Gram negative" mo_gramstain("E. coli") # "Gram negative"

View File

@ -15,7 +15,7 @@ test_that("as.mo works", {
expect_equal(as.character(as.mo("Escherichia species")), "B_ESCHR") expect_equal(as.character(as.mo("Escherichia species")), "B_ESCHR")
expect_equal(as.character(as.mo("Escherichia")), "B_ESCHR") expect_equal(as.character(as.mo("Escherichia")), "B_ESCHR")
expect_equal(as.character(as.mo(" B_ESCHR_COL ")), "B_ESCHR_COL") expect_equal(as.character(as.mo(" B_ESCHR_COL ")), "B_ESCHR_COL")
#expect_equal(as.character(as.mo("coli")), "B_ESCHR_COL") # not Campylobacter expect_equal(as.character(as.mo("e coli")), "B_ESCHR_COL") # not Campylobacter
expect_equal(as.character(as.mo("klpn")), "B_KLBSL_PNE") 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("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("K. pneu rhino")), "B_KLBSL_PNE_RHI") # K. pneumoniae subspp. rhinoscleromatis
@ -34,9 +34,6 @@ test_that("as.mo works", {
expect_equal(as.character(as.mo("P. aer")), "B_PDMNS_AER") # not Pasteurella aerogenes expect_equal(as.character(as.mo("P. aer")), "B_PDMNS_AER") # not Pasteurella aerogenes
# expect_equal(as.character(as.mo("Negative rods")), "GNR")
# expect_equal(as.character(as.mo("Gram negative rods")), "GNR")
# GLIMS # GLIMS
expect_equal(as.character(as.mo("bctfgr")), "B_BCTRD_FRA") expect_equal(as.character(as.mo("bctfgr")), "B_BCTRD_FRA")

View File

@ -8,11 +8,13 @@ test_that("mo_property works", {
expect_equal(mo_family("E. coli"), "Enterobacteriaceae") expect_equal(mo_family("E. coli"), "Enterobacteriaceae")
expect_equal(mo_genus("E. coli"), "Escherichia") expect_equal(mo_genus("E. coli"), "Escherichia")
expect_equal(mo_species("E. coli"), "coli") expect_equal(mo_species("E. coli"), "coli")
expect_equal(mo_subspecies("E. coli"), "") expect_equal(mo_subspecies("E. coli"), NA_character_)
expect_equal(mo_fullname("E. coli"), "Escherichia coli") expect_equal(mo_fullname("E. coli"), "Escherichia coli")
expect_equal(mo_type("E. coli", language = "en"), "Bacteria") expect_equal(mo_type("E. coli", language = "en"), "Bacteria")
expect_equal(mo_gramstain("E. coli", language = "en"), "Gram negative") expect_equal(mo_gramstain("E. coli", language = "en"), "Gram negative")
expect_equal(class(mo_taxonomy("E. coli")), "list") expect_equal(class(mo_taxonomy("E. coli")), "list")
expect_equal(names(mo_taxonomy("E. coli")), c("subkingdom", "phylum", "class", "order",
"family", "genus", "species", "subspecies"))
expect_equal(mo_shortname("MRSA"), "S. aureus") expect_equal(mo_shortname("MRSA"), "S. aureus")
expect_equal(mo_shortname("MRSA", Becker = TRUE), "S. aureus") expect_equal(mo_shortname("MRSA", Becker = TRUE), "S. aureus")
@ -21,12 +23,8 @@ test_that("mo_property works", {
expect_equal(mo_shortname("S. aga", Lancefield = TRUE), "GBS") expect_equal(mo_shortname("S. aga", Lancefield = TRUE), "GBS")
# test integrity # test integrity
# library(dplyr) MOs <- AMR::microorganisms
# rnd <- sample(1:nrow(AMR::microorganisms), 500, replace = FALSE) # random 500 rows expect_identical(MOs$fullname, mo_fullname(MOs$fullname, language = "en"))
# MOs <- AMR::microorganisms %>% filter(!is.na(mo),
# species != "species",
# dplyr::row_number() %in% rnd)
# expect_identical(MOs$fullname, mo_fullname(MOs$fullname, language = "en"))
# check languages # check languages
expect_equal(mo_type("E. coli", language = "de"), "Bakterien") expect_equal(mo_type("E. coli", language = "de"), "Bakterien")