diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f57d3bae..a4051166 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -97,7 +97,7 @@ pages: script: #- Rscript -e "install.packages('pkgdown', repos = 'https://cran.rstudio.com')" - Rscript -e "devtools::install(build = TRUE, upgrade = FALSE)" - - R -e "pkgdown::build_site(examples = FALSE, override = list(destination = 'public'))" + - R -e "pkgdown::build_site(examples = FALSE, lazy = TRUE, override = list(destination = 'public'))" artifacts: paths: - public diff --git a/DESCRIPTION b/DESCRIPTION index dd487ba3..f00fe5e6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 0.5.0.9020 -Date: 2019-03-02 +Version: 0.5.0.9021 +Date: 2019-03-05 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/NAMESPACE b/NAMESPACE index 44c0422e..f55cd80c 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -81,8 +81,20 @@ export(count_df) export(eucast_exceptional_phenotypes) export(eucast_rules) export(facet_rsi) +export(filter_1st_cephalosporins) +export(filter_2nd_cephalosporins) +export(filter_3rd_cephalosporins) +export(filter_4th_cephalosporins) +export(filter_ab_class) +export(filter_aminoglycosides) +export(filter_carbapenems) +export(filter_cephalosporins) export(filter_first_isolate) export(filter_first_weighted_isolate) +export(filter_fluoroquinolones) +export(filter_glycopeptides) +export(filter_macrolides) +export(filter_tetracyclines) export(first_isolate) export(freq) export(frequency_tbl) diff --git a/R/filter_ab_class.R b/R/filter_ab_class.R new file mode 100644 index 00000000..fed9838a --- /dev/null +++ b/R/filter_ab_class.R @@ -0,0 +1,268 @@ +# ==================================================================== # +# TITLE # +# Antimicrobial Resistance (AMR) Analysis # +# # +# SOURCE # +# https://gitlab.com/msberends/AMR # +# # +# LICENCE # +# (c) 2019 Berends MS (m.s.berends@umcg.nl), Luz CF (c.f.luz@umcg.nl) # +# # +# This R package is free software; you can freely use and distribute # +# it for both personal and commercial purposes under the terms of the # +# GNU General Public License version 2.0 (GNU GPL-2), as published by # +# the Free Software Foundation. # +# # +# This R package was created for academic research and was publicly # +# released in the hope that it will be useful, but it comes WITHOUT # +# ANY WARRANTY OR LIABILITY. # +# Visit our website for more info: https://msberends.gitab.io/AMR. # +# ==================================================================== # + +#' Filter on antibiotic class +#' +#' Filter on specific antibiotic variables based on their class (ATC groups). +#' @param tbl a data set +#' @param ab_class an antimicrobial class, like \code{"carbapenems"} +#' @param result an antibiotic result: S, I or R (or a combination of more of them) +#' @param scope the scope to check which variables to check, can be \code{"any"} (default) or \code{"all"} +#' @param ... parameters passed on to \code{\link[dplyr]{filter_at}} +#' @details The \code{\code{antibiotics}} data set will be searched for \code{ab_class} in the columns \code{atc_group1} and \code{atc_group2} (case-insensitive). Next, \code{tbl} will be checked for column names with a value in any abbreviations, codes or official names found in the \code{antibiotics} data set. +#' @rdname filter_ab_class +#' @importFrom dplyr filter_at %>% select vars any_vars all_vars +#' @importFrom crayon bold blue +#' @export +#' @examples +#' library(dplyr) +#' +#' # filter on isolates that have any result for any aminoglycoside +#' septic_patients %>% filter_aminoglycosides() +#' +#' # this is essentially the same as: +#' septic_patients %>% +#' filter_at(.vars = vars(c("gent", "tobr", "amik", "kana")), +#' .vars_predicate = any_vars(. %in% c("S", "I", "R"))) +#' +#' +#' # filter on isolates that show resistance to ANY aminoglycoside +#' septic_patients %>% filter_aminoglycosides("R") +#' +#' # filter on isolates that show resistance to ALL aminoglycosides +#' septic_patients %>% filter_aminoglycosides("R", "all") +#' +#' # filter on isolates that show resistance to +#' # any aminoglycoside and any fluoroquinolone +#' septic_patients %>% +#' filter_aminoglycosides("R", "any") %>% +#' filter_fluoroquinolones("R", "any") +filter_ab_class <- function(tbl, + ab_class, + result = NULL, + scope = "any", + ...) { + scope <- scope[1L] + if (is.null(result)) { + result <- c("S", "I", "R") + } + + if (!all(result %in% c("S", "I", "R"))) { + stop("`result` must be one or more of: S, I, R", call. = FALSE) + } + if (!all(scope %in% c("any", "all"))) { + stop("`scope` must be one of: any, all", call. = FALSE) + } + + vars_df <- colnames(tbl)[tolower(colnames(tbl)) %in% tolower(ab_class_vars(ab_class))] + atc_groups <- ab_class_atcgroups(ab_class) + + if (length(vars_df) > 0) { + if (length(result) == 1) { + operator <- " is " + } else { + operator <- " is one of " + } + if (scope == "any") { + scope_txt <- " or " + scope_fn <- any_vars + } else { + scope_txt <- " and " + scope_fn <- all_vars + } + message(blue(paste0("Filtering on ", atc_groups, ": ", scope, " of ", + paste(bold(vars_df), collapse = scope_txt), operator, toString(result)))) + tbl %>% + filter_at(.vars = vars(vars_df), + .vars_predicate = scope_fn(. %in% result), + ...) + } else { + warning(paste0("no antibiotics of class ", atc_groups, " found, leaving data unchanged"), call. = FALSE) + tbl + } +} + +#' @rdname filter_ab_class +#' @export +filter_aminoglycosides <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "aminoglycoside", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_carbapenems <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "carbapenem", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_cephalosporins <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "cephalosporin", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_1st_cephalosporins <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "first-generation cephalosporin", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_2nd_cephalosporins <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "second-generation cephalosporin", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_3rd_cephalosporins <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "third-generation cephalosporin", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_4th_cephalosporins <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "fourth-generation cephalosporin", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_fluoroquinolones <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "fluoroquinolone", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_glycopeptides <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "glycopeptide", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_macrolides <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "macrolide", + result = result, + scope = scope, + ...) +} + +#' @rdname filter_ab_class +#' @export +filter_tetracyclines <- function(tbl, + result = NULL, + scope = "any", + ...) { + filter_ab_class(tbl = tbl, + ab_class = "tetracycline", + result = result, + scope = scope, + ...) +} + +#' @importFrom dplyr %>% filter_at any_vars select +ab_class_vars <- function(ab_class) { + ab_vars <- AMR::antibiotics %>% + filter_at(vars(c("atc_group1", "atc_group2")), any_vars(. %like% ab_class)) %>% + select(atc:trade_name) %>% + as.matrix() %>% + as.character() %>% + paste(collapse = "|") %>% + strsplit("|", fixed = TRUE) %>% + unlist() %>% + unique() + ab_vars[!is.na(ab_vars)] +} + +#' @importFrom dplyr %>% filter pull +ab_class_atcgroups <- function(ab_class) { + AMR::antibiotics %>% + filter(atc %in% ab_class_vars(ab_class)) %>% + pull("atc_group2") %>% + unique() %>% + tolower() %>% + paste(collapse = "/") +} diff --git a/R/mo.R b/R/mo.R index 856f68ba..b15f0268 100755 --- a/R/mo.R +++ b/R/mo.R @@ -174,14 +174,26 @@ as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = TRUE, # check onLoad() in R/zzz.R: data tables are created there. } - if (deparse(substitute(reference_df)) == "get_mo_source()" + if (mo_source_isvalid(reference_df) & isFALSE(Becker) & isFALSE(Lancefield) & !is.null(reference_df) - & all(x %in% reference_df[,1])) { + & all(x %in% reference_df[,1][[1]])) { + # has valid own reference_df # (data.table not faster here) + reference_df <- reference_df %>% filter(!is.na(mo)) + # keep only first two columns, second must be mo + if (colnames(reference_df)[1] == "mo") { + reference_df <- reference_df[, c(2, 1)] + } else { + reference_df <- reference_df[, c(1, 2)] + } colnames(reference_df)[1] <- "x" + # remove factors, just keep characters + suppressWarnings( + reference_df[] <- lapply(reference_df, as.character) + ) suppressWarnings( y <- data.frame(x = x, stringsAsFactors = FALSE) %>% left_join(reference_df, by = "x") %>% @@ -277,8 +289,12 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, # only check the uniques, which is way faster x <- unique(x) # remove empty values (to later fill them in again with NAs) - # ("xxx" is WHONET code for 'no growth') - x <- x[!is.na(x) & !is.null(x) & !identical(x, "") & !identical(x, "xxx")] + # ("xxx" is WHONET code for 'no growth' and "con" is WHONET code for 'contamination') + x <- x[!is.na(x) + & !is.null(x) + & !identical(x, "") + & !identical(x, "xxx") + & !identical(x, "con")] # conversion of old MO codes from v0.5.0 (ITIS) to later versions (Catalogue of Life) if (any(x %like% "^[BFP]_[A-Z]{3,7}") & !all(x %in% microorganisms$mo)) { @@ -292,14 +308,18 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, # defined df to check for if (!is.null(reference_df)) { - if (!is.data.frame(reference_df) | NCOL(reference_df) < 2) { - stop('`reference_df` must be a data.frame with at least two columns.', call. = FALSE) - } - if (!"mo" %in% colnames(reference_df)) { + if (!mo_source_isvalid(reference_df)) { stop("`reference_df` must contain a column `mo` with values from the 'microorganisms' data set.", call. = FALSE) } reference_df <- reference_df %>% filter(!is.na(mo)) - # # remove factors, just keep characters + # keep only first two columns, second must be mo + if (colnames(reference_df)[1] == "mo") { + reference_df <- reference_df[, c(2, 1)] + } else { + reference_df <- reference_df[, c(1, 2)] + } + colnames(reference_df)[1] <- "x" + # remove factors, just keep characters suppressWarnings( reference_df[] <- lapply(reference_df, as.character) ) @@ -314,8 +334,7 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, return(rep(NA_character_, length(x_input))) } - } else if (all(x %in% reference_df[, 1]) - & all(reference_df[, "mo"] %in% AMR::microorganisms$mo)) { + } else if (all(x %in% reference_df[, 1][[1]])) { # all in reference df colnames(reference_df)[1] <- "x" suppressWarnings( @@ -420,12 +439,12 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, next } - if (any(x_trimmed[i] %in% c(NA, ""))) { + if (any(x_trimmed[i] %in% c(NA, "", "xxx", "con"))) { x[i] <- NA_character_ next } - if (tolower(x_trimmed[i]) %in% c("xxx", "other", "none", "unknown")) { + if (tolower(x_trimmed[i]) %in% c("other", "none", "unknown")) { # empty and nonsense values, ignore without warning x[i] <- microorganismsDT[mo == "UNKNOWN", ..property][[1]] next @@ -959,7 +978,11 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, # Wrap up ---------------------------------------------------------------- # comply to x, which is also unique and without empty values - x_input_unique_nonempty <- unique(x_input[!is.na(x_input) & !is.null(x_input) & !identical(x_input, "") & !identical(x_input, "xxx")]) + x_input_unique_nonempty <- unique(x_input[!is.na(x_input) + & !is.null(x_input) + & !identical(x_input, "") + & !identical(x_input, "xxx") + & !identical(x_input, "con")]) # left join the found results to the original input values (x_input) df_found <- data.frame(input = as.character(x_input_unique_nonempty), diff --git a/R/mo_source.R b/R/mo_source.R index 4a3ae099..34f5f940 100644 --- a/R/mo_source.R +++ b/R/mo_source.R @@ -117,22 +117,6 @@ set_mo_source <- function(path) { stop("File not found: ", path) } - is_valid <- function(df) { - valid <- TRUE - if (!is.data.frame(df)) { - valid <- FALSE - } else if (!"mo" %in% colnames(df)) { - valid <- FALSE - } else if (all(as.data.frame(df)[, 1] == "")) { - valid <- FALSE - } else if (!all(df$mo %in% c("", AMR::microorganisms$mo))) { - valid <- FALSE - } else if (NCOL(df) < 2) { - valid <- FALSE - } - valid - } - if (path %like% '[.]rds$') { df <- readRDS(path) @@ -151,13 +135,13 @@ set_mo_source <- function(path) { try( df <- utils::read.table(header = TRUE, sep = ",", stringsAsFactors = FALSE), silent = TRUE) - if (!is_valid(df)) { + if (!mo_source_isvalid(df)) { # try tab try( df <- utils::read.table(header = TRUE, sep = "\t", stringsAsFactors = FALSE), silent = TRUE) } - if (!is_valid(df)) { + if (!mo_source_isvalid(df)) { # try pipe try( df <- utils::read.table(header = TRUE, sep = "|", stringsAsFactors = FALSE), @@ -165,10 +149,12 @@ set_mo_source <- function(path) { } } - if (!is_valid(df)) { + if (!mo_source_isvalid(df)) { stop("File must contain a column with self-defined values and a reference column `mo` with valid values from the `microorganisms` data set.") } + df <- df %>% filter(!is.na(mo)) + # keep only first two columns, second must be mo if (colnames(df)[1] == "mo") { df <- df[, c(2, 1)] @@ -213,3 +199,22 @@ get_mo_source <- function() { readRDS("~/.mo_source.rds") } + +mo_source_isvalid <- function(x) { + if (deparse(substitute(x)) == "get_mo_source()") { + return(TRUE) + } + if (identical(x, get_mo_source())) { + return(TRUE) + } + if (is.null(x)) { + return(TRUE) + } + if (!is.data.frame(x)) { + return(FALSE) + } + if (!"mo" %in% colnames(x)) { + return(FALSE) + } + all(x$mo %in% c("", AMR::microorganisms$mo)) +} diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index 17b48a67..f4aca6a7 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -78,7 +78,7 @@
diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index ca96704e..e0076c6e 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -40,7 +40,7 @@ @@ -192,7 +192,7 @@AMR.Rmd
Note: values on this page will change with every website update since they are based on randomly created values and the page was written in RMarkdown. However, the methodology remains unchanged. This page was generated on 02 March 2019.
+Note: values on this page will change with every website update since they are based on randomly created values and the page was written in RMarkdown. However, the methodology remains unchanged. This page was generated on 05 March 2019.
So, we can draw at least two conclusions immediately. From a data scientist perspective, the data looks clean: only values M
and F
. From a researcher perspective: there are slightly more men. Nothing we didn’t already know.
The data is already quite clean, but we still need to transform some variables. The bacteria
column now consists of text, and we want to add more variables based on microbial IDs later on. So, we will transform this column to valid IDs. The mutate()
function of the dplyr
package makes this really easy:
data <- data %>%
@@ -443,10 +443,10 @@
#> Kingella kingae (no changes)
#>
#> EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016)
-#> Table 1: Intrinsic resistance in Enterobacteriaceae (1323 changes)
+#> Table 1: Intrinsic resistance in Enterobacteriaceae (1344 changes)
#> Table 2: Intrinsic resistance in non-fermentative Gram-negative bacteria (no changes)
#> Table 3: Intrinsic resistance in other Gram-negative bacteria (no changes)
-#> Table 4: Intrinsic resistance in Gram-positive bacteria (2834 changes)
+#> Table 4: Intrinsic resistance in Gram-positive bacteria (2767 changes)
#> Table 8: Interpretive rules for B-lactam agents and Gram-positive cocci (no changes)
#> Table 9: Interpretive rules for B-lactam agents and Gram-negative rods (no changes)
#> Table 10: Interpretive rules for B-lactam agents and other Gram-negative bacteria (no changes)
@@ -462,9 +462,9 @@
#> Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no changes)
#> Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no changes)
#>
-#> => EUCAST rules affected 7,524 out of 20,000 rows
+#> => EUCAST rules affected 7,383 out of 20,000 rows
#> -> added 0 test results
-#> -> changed 4,157 test results (0 to S; 0 to I; 4,157 to R)
So only 28.5% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
So only 28.4% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
For future use, the above two syntaxes can be shortened with the filter_first_isolate()
function:
Only 2 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and should be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics()
function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.
Only 1 isolates are marked as ‘first’ according to CLSI guideline. But when reviewing the antibiogram, it is obvious that some isolates are absolutely different strains and should be included too. This is why we weigh isolates, based on their antibiogram. The key_antibiotics()
function adds a vector with 18 key antibiotics: 6 broad spectrum ones, 6 small spectrum for Gram negatives and 6 small spectrum for Gram positives. These can be defined by the user.
If a column exists with a name like ‘key(…)ab’ the first_isolate()
function will automatically use it and determine the first weighted isolates. Mind the NOTEs in below output:
data <- data %>%
mutate(keyab = key_antibiotics(.)) %>%
@@ -637,7 +637,7 @@
#> NOTE: Using column `patient_id` as input for `col_patient_id`.
#> NOTE: Using column `keyab` as input for `col_keyantibiotics`. Use col_keyantibiotics = FALSE to prevent this.
#> [Criterion] Inclusion based on key antibiotics, ignoring I.
-#> => Found 15,826 first weighted isolates (79.1% of total)
isolate | @@ -654,11 +654,11 @@||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | -2010-01-18 | -C7 | +2010-01-20 | +L10 | B_ESCHR_COL | -S | -S | +I | +R | S | S | TRUE | @@ -666,32 +666,32 @@||
2 | -2010-02-27 | -C7 | +2010-03-26 | +L10 | B_ESCHR_COL | -S | +R | S | S | S | FALSE | -FALSE | +TRUE | |
3 | -2010-04-22 | -C7 | +2010-05-05 | +L10 | B_ESCHR_COL | S | S | S | S | FALSE | -FALSE | +TRUE | ||
4 | -2010-06-09 | -C7 | +2010-06-20 | +L10 | B_ESCHR_COL | S | S | @@ -702,83 +702,83 @@|||||||
5 | -2011-04-13 | -C7 | -B_ESCHR_COL | -R | -S | -S | -S | -TRUE | -TRUE | -|||||
6 | -2011-04-25 | -C7 | +2010-07-10 | +L10 | B_ESCHR_COL | S | S | S | S | FALSE | -TRUE | +FALSE | +||
6 | +2010-08-01 | +L10 | +B_ESCHR_COL | +S | +S | +S | +S | +FALSE | +FALSE | |||||
7 | -2011-08-02 | -C7 | +2010-08-27 | +L10 | B_ESCHR_COL | R | S | -R | +S | S | FALSE | TRUE | ||
8 | -2011-10-19 | -C7 | +2010-09-09 | +L10 | B_ESCHR_COL | R | -I | +S | S | S | FALSE | -TRUE | +FALSE | |
9 | -2011-10-23 | -C7 | +2010-09-26 | +L10 | B_ESCHR_COL | -S | +R | S | S | S | FALSE | -TRUE | +FALSE | |
10 | -2011-11-10 | -C7 | +2010-10-11 | +L10 | B_ESCHR_COL | -S | -S | R | S | +S | +S | +FALSE | FALSE | -TRUE |
Instead of 2, now 7 isolates are flagged. In total, 79.1% of all isolates are marked ‘first weighted’ - 50.6% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
+Instead of 1, now 4 isolates are flagged. In total, 78.8% of all isolates are marked ‘first weighted’ - 50.5% more than when using the CLSI guideline. In real life, this novel algorithm will yield 5-10% more isolates than the classic CLSI guideline.
As with filter_first_isolate()
, there’s a shortcut for this new algorithm too:
So we end up with 15,826 isolates for analysis.
+So we end up with 15,767 isolates for analysis.
We can remove unneeded columns:
@@ -786,7 +786,6 @@date | patient_id | hospital | @@ -803,79 +802,44 @@|||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2 | -2012-07-07 | -T4 | +2010-01-23 | +E2 | Hospital B | -B_ESCHR_COL | -R | -R | -R | +B_STPHY_AUR | +I | S | -F | -Gram negative | -Escherichia | -coli | +S | +S | +M | +Gram positive | +Staphylococcus | +aureus | TRUE |
3 | -2011-02-19 | -H3 | +2017-12-07 | +L8 | Hospital B | -B_ESCHR_COL | -S | -S | -S | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -||||||||
4 | -2012-12-15 | -G10 | -Hospital C | -B_KLBSL_PNE | -R | -S | -R | -S | -M | -Gram negative | -Klebsiella | -pneumoniae | -TRUE | -||||||||||
6 | -2011-03-27 | -H5 | -Hospital A | -B_ESCHR_COL | -S | -S | -S | -R | -M | -Gram negative | -Escherichia | -coli | -TRUE | -||||||||||
7 | -2012-06-22 | -Q8 | -Hospital A | B_STPHY_AUR | S | S | +S | +S | +M | +Gram positive | +Staphylococcus | +aureus | +TRUE | +||||||||||
2012-07-19 | +W5 | +Hospital A | +B_STPHY_AUR | +S | R | -R | +S | +S | F | Gram positive | Staphylococcus | @@ -883,12 +847,41 @@TRUE | |||||||||||
8 | -2015-06-27 | -Q2 | +2013-11-26 | +L7 | +Hospital A | +B_ESCHR_COL | +S | +S | +R | +S | +M | +Gram negative | +Escherichia | +coli | +TRUE | +||||||||
2016-01-24 | +M7 | Hospital B | B_ESCHR_COL | -R | +S | +S | +S | +S | +M | +Gram negative | +Escherichia | +coli | +TRUE | +||||||||||
2016-11-13 | +V10 | +Hospital A | +B_ESCHR_COL | +S | S | S | S | @@ -915,9 +908,9 @@||||||||||||||||
1 | Escherichia coli | -7,714 | -48.7% | -7,714 | -48.7% | +7,762 | +49.2% | +7,762 | +49.2% | ||||||||||||||
2 | Staphylococcus aureus | -3,977 | -25.1% | -11,691 | -73.9% | +4,014 | +25.5% | +11,776 | +74.7% | ||||||||||||||
3 | Streptococcus pneumoniae | -2,514 | -15.9% | -14,205 | -89.8% | +2,450 | +15.5% | +14,226 | +90.2% | ||||||||||||||
4 | Klebsiella pneumoniae | -1,621 | -10.2% | -15,826 | +1,541 | +9.8% | +15,767 | 100.0% | |||||||||||||||
Hospital A | -0.4798820 | +0.4717496 | |||||||||||||||||||||
Hospital B | -0.4792835 | +0.4754662 | |||||||||||||||||||||
Hospital C | -0.4863714 | +0.4748170 | |||||||||||||||||||||
Hospital D | -0.4915730 | +0.4854430 |
EUCAST.Rmd
G_test.Rmd
WHONET.Rmd
atc_property.Rmd
benchmarks.Rmd
In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 5 milliseconds means it can determine 200 input values per second. It case of 100 milliseconds, this is only 10 input values per second. The second input is the only one that has to be looked up thoroughly. All the others are known codes (the first one is a WHONET code) or common laboratory codes, or common full organism names like the last one. Full organism names are always preferred.
To achieve this speed, the as.mo
function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of Thermus islandicus (B_THERMS_ISL
), a bug probably never found before in humans:
T.islandicus <- microbenchmark(as.mo("theisl"),
@@ -235,13 +235,13 @@
times = 10)
print(T.islandicus, unit = "ms", signif = 3)
#> Unit: milliseconds
-#> expr min lq mean median uq max neval
-#> as.mo("theisl") 265.0 268.0 294.0 307.0 312 321 10
-#> as.mo("THEISL") 264.0 264.0 312.0 307.0 316 464 10
-#> as.mo("T. islandicus") 142.0 142.0 159.0 143.0 187 216 10
-#> as.mo("T. islandicus") 142.0 143.0 173.0 185.0 187 190 10
-#> as.mo("Thermus islandicus") 68.1 68.4 81.9 68.6 111 115 10
That takes 8 times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like Thermus islandicus) are almost fast - these are the most probable input from most data sets.
+#> expr min lq mean median uq max neval +#> as.mo("theisl") 275.0 304.0 310 309.0 316 337 10 +#> as.mo("THEISL") 263.0 304.0 311 306.0 312 391 10 +#> as.mo("T. islandicus") 142.0 143.0 168 148.0 188 220 10 +#> as.mo("T. islandicus") 142.0 142.0 169 143.0 185 312 10 +#> as.mo("Thermus islandicus") 68.1 68.6 101 89.8 122 179 10 +That takes 7 times as much time on average. A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance. Full names (like Thermus islandicus) are almost fast - these are the most probable input from most data sets.
In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Thermus islandicus (which is very uncommon):
par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)
@@ -286,9 +286,9 @@
times = 10)
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
-#> expr min lq mean median uq max neval
-#> mo_fullname(x) 732 772 823 819 858 1020 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.82 seconds (819 ms). You only lose time on your unique input values.
+#> expr min lq mean median uq max neval +#> mo_fullname(x) 687 738 767 770 774 887 10 +So transforming 500,000 values (!!) of 50 unique values only takes 0.77 seconds (770 ms). You only lose time on your unique input values.
So going from mo_fullname("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0006 seconds - it doesn’t even start calculating if the result would be the same as the expected resulting value. That goes for all helper functions:
run_it <- microbenchmark(A = mo_species("aureus"),
B = mo_genus("Staphylococcus"),
@@ -317,14 +317,14 @@
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
-#> A 0.322 0.338 0.397 0.384 0.415 0.569 10
-#> B 0.316 0.370 0.442 0.442 0.508 0.601 10
-#> C 0.335 0.385 0.502 0.504 0.566 0.724 10
-#> D 0.283 0.324 0.362 0.366 0.389 0.437 10
-#> E 0.252 0.274 0.317 0.323 0.355 0.383 10
-#> F 0.255 0.275 0.325 0.332 0.348 0.411 10
-#> G 0.259 0.272 0.307 0.299 0.318 0.412 10
-#> H 0.271 0.319 0.338 0.334 0.362 0.418 10
Of course, when running mo_phylum("Firmicutes")
the function has zero knowledge about the actual microorganism, namely S. aureus. But since the result would be "Firmicutes"
too, there is no point in calculating the result. And because this package ‘knows’ all phyla of all known bacteria (according to the Catalogue of Life), it can just return the initial value immediately.
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.
diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png index 558d400a..12367582 100644 Binary files a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-5-1.png differ diff --git a/docs/articles/freq.html b/docs/articles/freq.html index 3fe5d762..9aa22ce2 100644 --- a/docs/articles/freq.html +++ b/docs/articles/freq.html @@ -40,7 +40,7 @@ @@ -192,7 +192,7 @@freq.Rmd
mo_property.Rmd
resistance_predict.Rmd
filter_ab_class.Rd
Filter on specific antibiotic variables based on their class (ATC groups).
+ +filter_ab_class(tbl, ab_class, result = NULL, scope = "any", ...) + +filter_aminoglycosides(tbl, result = NULL, scope = "any", ...) + +filter_carbapenems(tbl, result = NULL, scope = "any", ...) + +filter_cephalosporins(tbl, result = NULL, scope = "any", ...) + +filter_1st_cephalosporins(tbl, result = NULL, scope = "any", ...) + +filter_2nd_cephalosporins(tbl, result = NULL, scope = "any", ...) + +filter_3rd_cephalosporins(tbl, result = NULL, scope = "any", ...) + +filter_4th_cephalosporins(tbl, result = NULL, scope = "any", ...) + +filter_fluoroquinolones(tbl, result = NULL, scope = "any", ...) + +filter_glycopeptides(tbl, result = NULL, scope = "any", ...) + +filter_macrolides(tbl, result = NULL, scope = "any", ...) + +filter_tetracyclines(tbl, result = NULL, scope = "any", ...)+ +
tbl | +a data set |
+
---|---|
ab_class | +an antimicrobial class, like |
+
result | +an antibiotic result: S, I or R (or a combination of more of them) |
+
scope | +the scope to check which variables to check, can be |
+
... | +parameters passed on to |
+
The
data set will be searched for antibiotics
ab_class
in the columns atc_group1
and atc_group2
(case-insensitive). Next, tbl
will be checked for column names with a value in any abbreviations, codes or official names found in the antibiotics
data set.
# NOT RUN { +library(dplyr) + +# filter on isolates that have any result for any aminoglycoside +septic_patients %>% filter_aminoglycosides() + +# this is essentially the same as: +septic_patients %>% + filter_at(.vars = vars(c("gent", "tobr", "amik", "kana")), + .vars_predicate = any_vars(. %in% c("S", "I", "R"))) + + +# filter on isolates that show resistance to ANY aminoglycoside +septic_patients %>% filter_aminoglycosides("R") + +# filter on isolates that show resistance to ALL aminoglycosides +septic_patients %>% filter_aminoglycosides("R", "all") + +# filter on isolates that show resistance to +# any aminoglycoside and any fluoroquinolone +septic_patients %>% + filter_aminoglycosides("R", "any") %>% + filter_fluoroquinolones("R", "any") +# }+