AMR/R/mo.R

659 lines
26 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE #
# Antimicrobial Resistance (AMR) Analysis #
# #
# AUTHORS #
# Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) #
# #
# LICENCE #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License version 2.0, #
# as published by the Free Software Foundation. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# ==================================================================== #
2018-08-31 13:36:19 +02:00
#' Transform to microorganism ID
#'
2018-09-24 23:33:29 +02:00
#' Use this function to determine a valid microorganism ID (\code{mo}). Determination is done using Artificial Intelligence (AI) and the complete taxonomic kingdoms \emph{Bacteria}, \emph{Fungi} and \emph{Protozoa} (see Source), so the input can be almost anything: a full name (like \code{"Staphylococcus aureus"}), an abbreviated name (like \code{"S. aureus"}), an abbreviation known in the field (like \code{"MRSA"}), or just a genus. You could also \code{\link{select}} a genus and species column, zie Examples.
#' @param x a character vector or a \code{data.frame} with one or two columns
#' @param Becker a logical to indicate whether \emph{Staphylococci} should be categorised into Coagulase Negative \emph{Staphylococci} ("CoNS") and Coagulase Positive \emph{Staphylococci} ("CoPS") instead of their own species, according to Karsten Becker \emph{et al.} [1].
#'
#' This excludes \emph{Staphylococcus aureus} at default, use \code{Becker = "all"} to also categorise \emph{S. aureus} as "CoPS".
2018-09-24 23:33:29 +02:00
#' @param Lancefield a logical to indicate whether beta-haemolytic \emph{Streptococci} should be categorised into Lancefield groups instead of their own species, according to Rebecca C. Lancefield [2]. These \emph{Streptococci} will be categorised in their first group, e.g. \emph{Streptococcus dysgalactiae} will be group C, although officially it was also categorised into groups G and L.
#'
#' This excludes \emph{Enterococci} at default (who are in group D), use \code{Lancefield = "all"} to also categorise all \emph{Enterococci} as group D.
2018-09-24 23:33:29 +02:00
#' @param allow_uncertain a logical to indicate whether empty results should be checked for only a part of the input string. When results are found, a warning will be given about the uncertainty and the result.
2018-08-31 13:36:19 +02:00
#' @rdname as.mo
#' @aliases mo
#' @keywords mo Becker becker Lancefield lancefield guess
2018-09-24 23:33:29 +02:00
#' @details
#' A microbial ID (class: \code{mo}) typically looks like these examples:\cr
#' \preformatted{
#' Code Full name
#' --------------- --------------------------------------
#' B_KLBSL Klebsiella
#' B_KLBSL_PNE Klebsiella pneumoniae
#' B_KLBSL_PNE_RHI Klebsiella pneumoniae rhinoscleromatis
#' | | | |
#' | | | |
#' | | | ----> subspecies, a 3-4 letter acronym
#' | | ----> species, a 3-4 letter acronym
#' | ----> genus, a 5-7 letter acronym, mostly without vowels
#' ----> taxonomic kingdom, either Bacteria (B), Fungi (F) or Protozoa (P)
#' }
2018-08-01 08:03:31 +02:00
#'
#' Use the \code{\link{mo_property}} functions to get properties based on the returned code, see Examples.
#'
2018-09-24 23:33:29 +02:00
#' This function uses Artificial Intelligence (AI) to help getting more logical results, based on type of input and known prevalence of human pathogens. For example:
2018-07-23 14:14:03 +02:00
#' \itemize{
#' \item{\code{"E. coli"} will return the ID of \emph{Escherichia coli} and not \emph{Entamoeba coli}, although the latter would alphabetically come first}
#' \item{\code{"H. influenzae"} will return the ID of \emph{Haemophilus influenzae} and not \emph{Haematobacter influenzae} for the same reason}
2018-07-23 14:14:03 +02:00
#' \item{Something like \code{"p aer"} will return the ID of \emph{Pseudomonas aeruginosa} and not \emph{Pasteurella aerogenes}}
#' \item{Something like \code{"stau"} or \code{"S aur"} will return the ID of \emph{Staphylococcus aureus} and not \emph{Staphylococcus auricularis}}
2018-07-23 14:14:03 +02:00
#' }
2018-09-24 23:33:29 +02:00
#' This means that looking up human non-pathogenic microorganisms takes a longer time compares to human pathogenic microorganisms.
#'
#' \code{guess_mo} is an alias of \code{as.mo}.
#' @section ITIS:
#' \if{html}{\figure{itis_logo.jpg}{options: height=60px style=margin-bottom:5px} \cr}
2018-09-25 16:44:40 +02:00
#' 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.
2018-09-24 23:33:29 +02:00
# (source as section, so it can be inherited by mo_property:)
#' @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}
#'
#' [2] Lancefield RC \strong{A serological differentiation of human and other groups of hemolytic streptococci}. 1933. J Exp Med. 57(4): 57195. \url{https://dx.doi.org/10.1084/jem.57.4.571}
2018-09-24 23:33:29 +02:00
#'
#' [3] Integrated Taxonomic Information System (ITIS). Retrieved September 2018. \url{http://www.itis.gov}
#' @export
2018-09-24 23:33:29 +02:00
#' @importFrom dplyr %>% pull left_join
#' @importFrom data.table as.data.table setkey
2018-08-31 13:36:19 +02:00
#' @return Character (vector) with class \code{"mo"}. Unknown values will return \code{NA}.
2018-09-24 23:33:29 +02:00
#' @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.
#' @examples
#' # These examples all return "STAAUR", the ID of S. aureus:
2018-08-31 13:36:19 +02:00
#' as.mo("stau")
#' as.mo("STAU")
#' as.mo("staaur")
#' as.mo("S. aureus")
#' as.mo("S aureus")
#' as.mo("Staphylococcus aureus")
#' as.mo("MRSA") # Methicillin Resistant S. aureus
#' as.mo("VISA") # Vancomycin Intermediate S. aureus
#' as.mo("VRSA") # Vancomycin Resistant S. aureus
2018-09-24 23:33:29 +02:00
#' as.mo(369) # Search on TSN (Taxonomic Serial Number), a unique identifier
#' # for the Integrated Taxonomic Information System (ITIS)
#'
2018-09-05 10:51:46 +02:00
#' as.mo("Streptococcus group A")
#' as.mo("GAS") # Group A Streptococci
#' as.mo("GBS") # Group B Streptococci
#'
2018-08-31 13:36:19 +02:00
#' # guess_mo is an alias of as.mo and works the same
2018-09-24 23:33:29 +02:00
#' guess_mo("S. epidermidis") # will remain species: B_STPHY_EPI
#' guess_mo("S. epidermidis", Becker = TRUE) # will not remain species: B_STPHY_CNS
#'
2018-09-24 23:33:29 +02:00
#' guess_mo("S. pyogenes") # will remain species: B_STRPTC_PYO
#' guess_mo("S. pyogenes", Lancefield = TRUE) # will not remain species: B_STRPTC_GRA
#'
2018-08-31 13:36:19 +02:00
#' # Use mo_* functions to get a specific property based on `mo`
2018-09-24 23:33:29 +02:00
#' Ecoli <- as.mo("E. coli") # returns `B_ESCHR_COL`
#' mo_genus(Ecoli) # returns "Escherichia"
2018-09-24 23:33:29 +02:00
#' mo_gramstain(Ecoli) # returns "Gram negative"
#' # but it uses as.mo internally too, so you could also just use:
#' mo_genus("E. coli") # returns "Escherichia"
#'
#'
#' \dontrun{
2018-08-31 13:36:19 +02:00
#' df$mo <- as.mo(df$microorganism_name)
#'
#' # the select function of tidyverse is also supported:
2018-07-23 14:14:03 +02:00
#' library(dplyr)
2018-08-31 13:36:19 +02:00
#' df$mo <- df %>%
2018-07-23 14:14:03 +02:00
#' select(microorganism_name) %>%
2018-08-31 13:36:19 +02:00
#' guess_mo()
#'
#' # and can even contain 2 columns, which is convenient for genus/species combinations:
2018-08-31 13:36:19 +02:00
#' df$mo <- df %>%
2018-07-23 14:14:03 +02:00
#' select(genus, species) %>%
2018-08-31 13:36:19 +02:00
#' guess_mo()
2018-07-23 14:14:03 +02:00
#'
#' # same result:
2018-07-23 14:14:03 +02:00
#' df <- df %>%
2018-08-31 13:36:19 +02:00
#' mutate(mo = guess_mo(paste(genus, species)))
#' }
2018-09-24 23:33:29 +02:00
as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = FALSE) {
2018-07-23 14:14:03 +02:00
if (NCOL(x) == 2) {
# support tidyverse selection like: df %>% select(colA, colB)
# paste these columns together
x_vector <- vector("character", NROW(x))
for (i in 1:NROW(x)) {
x_vector[i] <- paste(pull(x[i,], 1), pull(x[i,], 2), sep = " ")
}
x <- x_vector
} else {
if (NCOL(x) > 2) {
stop('`x` can be 2 columns at most', call. = FALSE)
}
2018-09-09 12:11:44 +02:00
x[is.null(x)] <- NA
2018-07-23 14:14:03 +02:00
# support tidyverse selection like: df %>% select(colA)
if (!is.vector(x)) {
x <- pull(x, 1)
}
}
2018-09-24 23:33:29 +02:00
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)
x_input <- x
# only check the uniques, which is way faster
x <- unique(x)
2018-09-24 23:33:29 +02:00
x_backup <- trimws(x, which = "both")
x_species <- paste(x_backup, "species")
# translate to English for supported languages of mo_property
x <- gsub("(Gruppe|gruppe|groep|grupo|gruppo|groupe)", "group", x)
2018-09-09 12:11:44 +02:00
# 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
2018-09-24 23:33:29 +02:00
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, '$')
2018-09-01 21:19:46 +02:00
x_withspaces_all <- x_withspaces
2018-09-06 16:50:03 +02:00
x_withspaces_start <- paste0('^', x_withspaces)
x_withspaces <- paste0('^', x_withspaces, '$')
# cat(paste0('x "', x, '"\n'))
2018-09-24 23:33:29 +02:00
# cat(paste0('x_species "', x_species, '"\n'))
# cat(paste0('x_withspaces_all "', x_withspaces_all, '"\n'))
# cat(paste0('x_withspaces_start "', x_withspaces_start, '"\n'))
# cat(paste0('x_withspaces "', x_withspaces, '"\n'))
# cat(paste0('x_backup "', x_backup, '"\n'))
2018-09-24 23:33:29 +02:00
# cat(paste0('x_trimmed "', x_trimmed, '"\n'))
# cat(paste0('x_trimmed_species "', x_trimmed_species, '"\n'))
for (i in 1:length(x)) {
2018-09-24 23:33:29 +02:00
if (identical(x_trimmed[i], "") | is.na(x_trimmed[i])) {
2018-07-30 00:14:06 +02:00
# empty values
x[i] <- NA
next
}
2018-09-14 10:31:21 +02:00
# translate known trivial abbreviations to genus + species ----
if (!is.na(x_trimmed[i])) {
if (toupper(x_trimmed[i]) == 'MRSA'
| toupper(x_trimmed[i]) == 'VISA'
| toupper(x_trimmed[i]) == 'VRSA') {
2018-09-24 23:33:29 +02:00
x[i] <- 'B_STPHY_AUR'
2018-07-30 01:18:40 +02:00
next
}
if (toupper(x_trimmed[i]) == 'MRSE') {
2018-09-24 23:33:29 +02:00
x[i] <- 'B_STPHY_EPI'
2018-08-01 08:03:31 +02:00
next
}
if (toupper(x_trimmed[i]) == 'VRE') {
2018-09-24 23:33:29 +02:00
x[i] <- 'B_ENTRC'
2018-07-30 01:18:40 +02:00
next
}
if (toupper(x_trimmed[i]) == 'MRPA') {
# multi resistant P. aeruginosa
2018-09-24 23:33:29 +02:00
x[i] <- 'B_PDMNS_AER'
2018-07-30 01:18:40 +02:00
next
}
if (toupper(x_trimmed[i]) %in% c('PISP', 'PRSP', 'VISP', 'VRSP')) {
# peni I, peni R, vanco I, vanco R: S. pneumoniae
2018-09-24 23:33:29 +02:00
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])
2018-08-01 09:45:11 +02:00
next
}
2018-09-24 23:33:29 +02:00
# 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'
2018-09-05 10:51:46 +02:00
next
}
}
2018-09-24 23:33:29 +02:00
# 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])), mo]
if (length(found) > 0) {
x[i] <- found[1L]
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), mo]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
}
}
# search for GLIMS code ----
found <- AMR::microorganisms.umcg[which(toupper(AMR::microorganisms.umcg$umcg) == toupper(x_trimmed[i])),]$mo
if (length(found) > 0) {
2018-09-24 23:33:29 +02:00
x[i] <- MOs[mo.old == found, mo][1L]
next
}
2018-09-24 23:33:29 +02:00
# TRY FIRST THOUSAND MOST PREVALENT IN HUMAN INFECTIONS ----
found <- MOs_mostprevalent[tolower(fullname) %in% tolower(c(x_backup[i], x_trimmed[i])), mo]
# most probable: is exact match in fullname
if (length(found) > 0) {
x[i] <- found[1L]
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])
2018-09-25 16:44:40 +02:00
| (substr(x_backup[i], 4, 6) == "SPP" & mo.old == substr(x_backup[i], 1, 3))
| mo.old == substr(x_backup[i], 1, 3), mo]
2018-09-24 23:33:29 +02:00
# is a valid old mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-09-24 23:33:29 +02:00
# try any match keeping spaces ----
found <- MOs_mostprevalent[fullname %like% x_withspaces[i], mo]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-09-14 10:31:21 +02:00
# try any match keeping spaces, not ending with $ ----
2018-09-24 23:33:29 +02:00
found <- MOs_mostprevalent[fullname %like% x_withspaces_start[i], mo]
2018-09-06 16:50:03 +02:00
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-09-14 10:31:21 +02:00
# try any match diregarding spaces ----
2018-09-24 23:33:29 +02:00
found <- MOs_mostprevalent[fullname %like% x[i], mo]
2018-07-30 00:14:06 +02:00
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-09-14 10:31:21 +02:00
# try fullname without start and stop regex, to also find subspecies ----
# like "K. pneu rhino" -> "Klebsiella pneumoniae (rhinoscleromatis)" = KLEPNERH
2018-09-24 23:33:29 +02:00
found <- MOs_mostprevalent[fullname %like% x_withspaces_start[i], mo]
2018-09-01 21:19:46 +02:00
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-09-24 23:33:29 +02:00
# try splitting of characters and then find ID ----
# like esco = E. coli, klpn = K. pneumoniae, stau = S. aureus
x_split <- x
x_length <- nchar(x_trimmed[i])
x_split[i] <- paste0(x_trimmed[i] %>% substr(1, x_length / 2) %>% trimws(),
'.* ',
x_trimmed[i] %>% substr((x_length / 2) + 1, x_length) %>% trimws())
found <- MOs_mostprevalent[fullname %like% paste0('^', x_split[i]), mo]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try any match with text before and after original search string ----
# 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_mostprevalent[fullname %like% x_trimmed[i], mo]
# if (length(found) > 0) {
# x[i] <- found[1L]
# next
# }
# }
# THEN TRY ALL OTHERS ----
if (is.null(MOs_allothers)) {
MOs_allothers <- MOs[prevalence == 9999,]
}
found <- MOs_allothers[tolower(fullname) == tolower(x_backup[i]), mo]
# most probable: is exact match in fullname
if (length(found) > 0) {
x[i] <- found[1L]
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
}
2018-09-25 16:44:40 +02:00
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]
2018-09-24 23:33:29 +02:00
# is a valid old mo
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try any match keeping spaces ----
found <- MOs_allothers[fullname %like% x_withspaces[i], mo]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try any match keeping spaces, not ending with $ ----
found <- MOs_allothers[fullname %like% x_withspaces_start[i], mo]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try any match diregarding spaces ----
found <- MOs_allothers[fullname %like% x[i], mo]
if (length(found) > 0) {
x[i] <- found[1L]
next
}
# try fullname without start and stop regex, to also find subspecies ----
# like "K. pneu rhino" -> "Klebsiella pneumoniae (rhinoscleromatis)" = KLEPNERH
found <- MOs_allothers[fullname %like% x_withspaces_start[i], mo]
2018-07-30 00:14:06 +02:00
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-07-30 00:14:06 +02:00
2018-09-14 10:31:21 +02:00
# try splitting of characters and then find ID ----
2018-07-30 00:14:06 +02:00
# like esco = E. coli, klpn = K. pneumoniae, stau = S. aureus
x_split <- x
x_length <- nchar(x_trimmed[i])
x_split[i] <- paste0(x_trimmed[i] %>% substr(1, x_length / 2) %>% trimws(),
2018-07-30 00:14:06 +02:00
'.* ',
x_trimmed[i] %>% substr((x_length / 2) + 1, x_length) %>% trimws())
2018-09-24 23:33:29 +02:00
found <- MOs_allothers[fullname %like% paste0('^', x_split[i]), mo]
2018-07-30 00:14:06 +02:00
if (length(found) > 0) {
x[i] <- found[1L]
next
}
2018-07-30 00:14:06 +02:00
2018-09-24 23:33:29 +02:00
# # try any match with text before and after original search string ----
# # 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)) {
2018-09-25 16:44:40 +02:00
MOs_old <- as.data.table(AMR::microorganisms.old)
2018-09-24 23:33:29 +02:00
setkey(MOs_old, name, tsn_new)
}
2018-09-24 23:33:29 +02:00
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]
2018-09-25 16:44:40 +02:00
renamed_note(name_old = found[1, name],
name_new = MOs[tsn == found[1, tsn_new], fullname],
authors = found[1, authors],
year = found[1, year])
2018-09-24 23:33:29 +02:00
next
}
# check for uncertain results ----
if (allow_uncertain == TRUE) {
2018-09-25 16:44:40 +02:00
# (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) {
x[i] <- MOs[tsn == found[1, tsn_new], mo]
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
2018-09-24 23:33:29 +02:00
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])) {
2018-09-25 16:44:40 +02:00
warning("Uncertain interpretation: '",
x_backup[i], "' -> '", MOs[mo == x[i], fullname], "' (", x[i], ")",
call. = FALSE, immediate. = TRUE)
2018-07-30 00:14:06 +02:00
next
}
}
2018-09-14 10:31:21 +02:00
# not found ----
2018-07-30 00:14:06 +02:00
x[i] <- NA_character_
failures <- c(failures, x_backup[i])
2018-07-30 00:14:06 +02:00
}
2018-07-23 14:14:03 +02:00
failures <- failures[!failures %in% c(NA, NULL, NaN)]
if (length(failures) > 0) {
2018-09-24 23:33:29 +02:00
warning("These ", length(failures) , " values could not be coerced (try again with allow_uncertain = TRUE):\n",
2018-07-23 14:14:03 +02:00
paste('"', unique(failures), '"', sep = "", collapse = ', '),
".",
call. = FALSE)
}
2018-09-14 10:31:21 +02:00
# Becker ----
2018-09-01 21:19:46 +02:00
if (Becker == TRUE | Becker == "all") {
# See Source. It's this figure:
# https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4187637/figure/F3/
2018-09-25 16:44:40 +02:00
MOs_staph <- MOs[genus == "Staphylococcus"]
setkey(MOs_staph, species)
CoNS <- MOs_staph[species %in% c("arlettae", "auricularis", "capitis",
"caprae", "carnosus", "cohnii", "condimenti",
"devriesei", "epidermidis", "equorum",
"fleurettii", "gallinarum", "haemolyticus",
"hominis", "jettensis", "kloosii", "lentus",
"lugdunensis", "massiliensis", "microti",
"muscae", "nepalensis", "pasteuri", "petrasii",
"pettenkoferi", "piscifermentans", "rostri",
"saccharolyticus", "saprophyticus", "sciuri",
"stepanovicii", "simulans", "succinus",
"vitulinus", "warneri", "xylosus"), mo]
CoPS <- MOs_staph[species %in% c("simiae", "agnetis", "chromogenes",
"delphini", "felis", "lutrae",
"hyicus", "intermedius",
"pseudintermedius", "pseudointermedius",
"schleiferi"), mo]
2018-09-24 23:33:29 +02:00
x[x %in% CoNS] <- "B_STPHY_CNS"
x[x %in% CoPS] <- "B_STPHY_CPS"
2018-09-01 21:19:46 +02:00
if (Becker == "all") {
2018-09-24 23:33:29 +02:00
x[x == "B_STPHY_AUR"] <- "B_STPHY_CPS"
2018-09-01 21:19:46 +02:00
}
}
2018-09-14 10:31:21 +02:00
# Lancefield ----
if (Lancefield == TRUE | Lancefield == "all") {
2018-09-01 21:19:46 +02:00
# group A
2018-09-24 23:33:29 +02:00
x[x == "B_STRPTC_PYO"] <- "B_STRPTC_GRA" # S. pyogenes
2018-09-01 21:19:46 +02:00
# group B
2018-09-24 23:33:29 +02:00
x[x == "B_STRPTC_AGA"] <- "B_STRPTC_GRB" # S. agalactiae
2018-09-01 21:19:46 +02:00
# group C
S_groupC <- MOs %>% filter(genus == "Streptococcus",
species %in% c("equisimilis", "equi",
"zooepidemicus", "dysgalactiae")) %>%
pull(mo)
2018-09-24 23:33:29 +02:00
x[x %in% S_groupC] <- "B_STRPTC_GRC" # S. agalactiae
if (Lancefield == "all") {
2018-09-24 23:33:29 +02:00
x[substr(x, 1, 7) == "B_ENTRC"] <- "B_STRPTC_GRD" # all Enterococci
}
2018-09-01 21:19:46 +02:00
# group F
2018-09-24 23:33:29 +02:00
x[x == "B_STRPTC_ANG"] <- "B_STRPTC_GRF" # S. anginosus
2018-09-01 21:19:46 +02:00
# group H
2018-09-24 23:33:29 +02:00
x[x == "B_STRPTC_SAN"] <- "B_STRPTC_GRH" # S. sanguinis
2018-09-01 21:19:46 +02:00
# group K
2018-09-24 23:33:29 +02:00
x[x == "B_STRPTC_SAL"] <- "B_STRPTC_GRK" # S. salivarius
2018-09-01 21:19:46 +02:00
}
# left join the found results to the original input values (x_input)
2018-09-25 16:44:40 +02:00
DT_found <- data.table(input = as.character(unique(x_input)),
found = x,
2018-09-25 16:44:40 +02:00
key = "input",
stringsAsFactors = FALSE)
2018-09-25 16:44:40 +02:00
DT_input <- data.table(input = as.character(x_input),
key = "input",
stringsAsFactors = FALSE)
2018-09-25 16:44:40 +02:00
x <- DT_found[DT_input, on = "input", found]
# df_found <- data.frame(input = as.character(unique(x_input)),
# found = x,
# stringsAsFactors = FALSE)
# df_input <- data.frame(input = as.character(x_input),
# stringsAsFactors = FALSE)
# x <- df_input %>%
# left_join(df_found,
# by = "input") %>%
# pull(found)
2018-08-31 13:36:19 +02:00
class(x) <- "mo"
2018-07-23 14:14:03 +02:00
attr(x, 'package') <- 'AMR'
2018-09-24 23:33:29 +02:00
attr(x, 'ITIS') <- TRUE
x
}
2018-07-23 14:14:03 +02:00
2018-09-25 16:44:40 +02:00
renamed_note <- function(name_old, name_new, authors, year) {
msg <- paste0("Note: '", name_old, "' was renamed to '", name_new, "'")
if (!authors %in% c("", NA)) {
msg <- paste0(msg, " by ", authors)
}
if (!year %in% c("", NA)) {
msg <- paste0(msg, " in ", year)
}
base::message(msg)
}
2018-09-24 23:33:29 +02:00
2018-08-31 13:36:19 +02:00
#' @rdname as.mo
2018-07-23 14:14:03 +02:00
#' @export
2018-08-31 13:36:19 +02:00
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
#' @export
#' @noRd
print.mo <- function(x, ...) {
cat("Class 'mo'\n")
print.default(as.character(x), quote = FALSE)
}
2018-07-23 14:14:03 +02:00
2018-08-31 13:36:19 +02:00
#' @exportMethod as.data.frame.mo
2018-07-23 14:14:03 +02:00
#' @export
2018-08-31 13:36:19 +02:00
#' @noRd
as.data.frame.mo <- function (x, ...) {
# same as as.data.frame.character but with removed stringsAsFactors
nm <- paste(deparse(substitute(x), width.cutoff = 500L),
collapse = " ")
if (!"nm" %in% names(list(...))) {
as.data.frame.vector(x, ..., nm = nm)
} else {
as.data.frame.vector(x, ...)
}
}
#' @exportMethod pull.mo
#' @export
#' @importFrom dplyr pull
#' @noRd
pull.mo <- function(.data, ...) {
pull(as.data.frame(.data), ...)
2018-07-23 14:14:03 +02:00
}