AMR/R/mo_matching_score.R

133 lines
7.6 KiB
R
Raw Normal View History

# ==================================================================== #
# TITLE: #
2022-10-05 09:12:22 +02:00
# AMR: An R Package for Working with Antimicrobial Resistance Data #
# #
# SOURCE CODE: #
# https://github.com/msberends/AMR #
# #
# 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. #
# #
# 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. #
2020-10-08 11:16:03 +02:00
# #
# Visit our website for the full manual and a complete tutorial about #
# how to conduct AMR data analysis: https://msberends.github.io/AMR/ #
# ==================================================================== #
#' Calculate the Matching Score for Microorganisms
2022-08-28 10:31:50 +02:00
#'
#' This algorithm is used by [as.mo()] and all the [`mo_*`][mo_property()] functions to determine the most probable match of taxonomic records based on user input.
2023-01-06 19:21:04 +01:00
#' @author Dr. Matthijs Berends, 2018
#' @param x Any user input value(s)
2020-09-26 16:26:01 +02:00
#' @param n A full taxonomic name, that exists in [`microorganisms$fullname`][microorganisms]
2022-12-19 15:32:41 +01:00
#' @note This algorithm was originally described in: Berends MS *et al.* (2022). **AMR: An R Package for Working with Antimicrobial Resistance Data**. *Journal of Statistical Software*, 104(3), 1-31; \doi{10.18637/jss.v104.i03}.
2023-01-23 15:01:21 +01:00
#'
2022-12-19 15:32:41 +01:00
#' Later, the work of Bartlett A *et al.* about bacterial pathogens infecting humans (2022, \doi{10.1099/mic.0.001269}) was incorporated.
#' @section Matching Score for Microorganisms:
2020-09-28 11:00:59 +02:00
#' With ambiguous user input in [as.mo()] and all the [`mo_*`][mo_property()] functions, the returned results are chosen based on their matching score using [mo_matching_score()]. This matching score \eqn{m}, is calculated as:
2022-08-28 10:31:50 +02:00
#'
2023-01-14 17:10:10 +01:00
#' \ifelse{latex}{\deqn{m_{(x, n)} = \frac{l_{n} - 0.5 \cdot \min \begin{cases}l_{n} \\ \textrm{lev}(x, n)\end{cases}}{l_{n} \cdot p_{n} \cdot k_{n}}}}{
2023-01-23 15:01:21 +01:00
#'
2023-01-14 17:10:10 +01:00
#' \ifelse{html}{\figure{mo_matching_score.png}{options: width="300" alt="mo matching score"}}{m(x, n) = ( l_n * min(l_n, lev(x, n) ) ) / ( l_n * p_n * k_n )}}
2022-08-28 10:31:50 +02:00
#'
2020-09-26 16:26:01 +02:00
#' where:
2022-08-28 10:31:50 +02:00
#'
2023-01-06 19:21:04 +01:00
#' * \eqn{x} is the user input;
2023-01-07 01:51:19 +01:00
#' * \eqn{n} is a taxonomic name (genus, species, and subspecies);
#' * \eqn{l_n} is the length of \eqn{n};
#' * \eqn{lev} is the [Levenshtein distance function](https://en.wikipedia.org/wiki/Levenshtein_distance) (counting any insertion as 1, and any deletion or substitution as 2) that is needed to change \eqn{x} into \eqn{n};
2023-01-14 17:10:10 +01:00
#' * \eqn{p_n} is the human pathogenic prevalence group of \eqn{n}, as described below;
#' * \eqn{k_n} is the taxonomic kingdom of \eqn{n}, set as Bacteria = 1, Fungi = 1.25, Protozoa = 1.5, Archaea = 2, others = 3.
2022-08-28 10:31:50 +02:00
#'
2023-01-07 01:51:19 +01:00
#' The grouping into human pathogenic prevalence \eqn{p} is based on recent work from Bartlett *et al.* (2022, \doi{10.1099/mic.0.001269}) who extensively studied medical-scientific literature to categorise all bacterial species into these groups:
2023-01-23 15:01:21 +01:00
#'
2022-12-20 16:14:04 +01:00
#' - **Established**, if a taxonomic species has infected at least three persons in three or more references. These records have `prevalence = 1.0` in the [microorganisms] data set;
#' - **Putative**, if a taxonomic species has fewer than three known cases. These records have `prevalence = 1.25` in the [microorganisms] data set.
2023-01-23 15:01:21 +01:00
#'
2022-12-19 15:32:41 +01:00
#' Furthermore,
2023-01-23 15:01:21 +01:00
#'
2022-12-20 16:14:04 +01:00
#' - Any genus present in the **established** list also has `prevalence = 1.0` in the [microorganisms] data set;
#' - Any other genus present in the **putative** list has `prevalence = 1.25` in the [microorganisms] data set;
#' - Any other species or subspecies of which the genus is present in the two aforementioned groups, has `prevalence = 1.5` in the [microorganisms] data set;
#' - Any *non-bacterial* genus, species or subspecies of which the genus is present in the following list, has `prevalence = 1.25` in the [microorganisms] data set: `r vector_or(MO_PREVALENT_GENERA, quotes = "*")`;
2022-12-20 16:14:04 +01:00
#' - All other records have `prevalence = 2.0` in the [microorganisms] data set.
2022-10-05 09:12:22 +02:00
#'
2022-12-19 15:32:41 +01:00
#' When calculating the matching score, all characters in \eqn{x} and \eqn{n} are ignored that are other than A-Z, a-z, 0-9, spaces and parentheses.
2022-08-28 10:31:50 +02:00
#'
#' All matches are sorted descending on their matching score and for all user input values, the top match will be returned. This will lead to the effect that e.g., `"E. coli"` will return the microbial ID of *Escherichia coli* (\eqn{m = `r round(mo_matching_score("E. coli", "Escherichia coli"), 3)`}, a highly prevalent microorganism found in humans) and not *Entamoeba coli* (\eqn{m = `r round(mo_matching_score("E. coli", "Entamoeba coli"), 3)`}, a less prevalent microorganism in humans), although the latter would alphabetically come first.
#' @export
#' @inheritSection AMR Reference Data Publicly Available
2022-08-28 10:31:50 +02:00
#' @examples
2023-01-07 01:51:19 +01:00
#' mo_reset_session()
2023-01-23 15:01:21 +01:00
#'
#' as.mo("E. coli")
#' mo_uncertainties()
2022-08-28 10:31:50 +02:00
#'
#' mo_matching_score(
#' x = "E. coli",
#' n = c("Escherichia coli", "Entamoeba coli")
#' )
2020-09-26 16:26:01 +02:00
mo_matching_score <- function(x, n) {
meet_criteria(x, allow_class = c("character", "data.frame", "list"))
meet_criteria(n, allow_class = "character")
2022-08-28 10:31:50 +02:00
2023-01-21 23:47:20 +01:00
add_MO_lookup_to_AMR_env()
2020-10-08 11:16:03 +02:00
x <- parse_and_convert(x)
# no dots and other non-whitespace characters
x <- gsub("[^a-zA-Z0-9 \\(\\)]+", "", x)
2022-08-28 10:31:50 +02:00
2020-10-08 11:16:03 +02:00
# only keep one space
x <- gsub(" +", " ", x)
2022-08-28 10:31:50 +02:00
2022-10-05 09:12:22 +02:00
# force a capital letter, so this conversion will not count as a substitution
substr(x, 1, 1) <- toupper(substr(x, 1, 1))
2023-01-23 15:01:21 +01:00
2020-09-26 16:26:01 +02:00
# n is always a taxonomically valid full name
if (length(n) == 1) {
n <- rep(n, length(x))
}
if (length(x) == 1) {
2020-09-26 16:26:01 +02:00
x <- rep(x, length(n))
}
2023-01-23 15:01:21 +01:00
2020-10-08 11:16:03 +02:00
# length of fullname
l_n <- nchar(n)
lev <- double(length = length(x))
l_n.lev <- double(length = length(x))
2022-12-19 15:32:41 +01:00
# get Levenshtein distance
2022-10-05 09:12:22 +02:00
lev <- unlist(Map(f = function(a, b) {
as.double(utils::adist(a, b,
ignore.case = FALSE,
fixed = TRUE,
costs = c(insertions = 1, deletions = 2, substitutions = 2),
counts = FALSE
))
}, x, n, USE.NAMES = FALSE))
l_n.lev[l_n < lev] <- l_n[l_n < lev]
l_n.lev[lev < l_n] <- lev[lev < l_n]
l_n.lev[lev == l_n] <- lev[lev == l_n]
2020-10-08 11:16:03 +02:00
# human pathogenic prevalence (1 to 3), see ?as.mo
2022-10-14 13:02:50 +02:00
p_n <- AMR_env$MO_lookup[match(n, AMR_env$MO_lookup$fullname), "prevalence", drop = TRUE]
2020-10-08 11:16:03 +02:00
# kingdom index (Bacteria = 1, Fungi = 2, Protozoa = 3, Archaea = 4, others = 5)
2022-10-14 13:02:50 +02:00
k_n <- AMR_env$MO_lookup[match(n, AMR_env$MO_lookup$fullname), "kingdom_index", drop = TRUE]
2023-01-23 15:01:21 +01:00
2020-09-19 11:54:01 +02:00
# matching score:
2020-10-08 11:16:03 +02:00
(l_n - 0.5 * l_n.lev) / (l_n * p_n * k_n)
}