diff --git a/DESCRIPTION b/DESCRIPTION index 5b13cd75..4c3603da 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR Version: 0.5.0.9018 -Date: 2019-02-22 +Date: 2019-02-23 Title: Antimicrobial Resistance Analysis Authors@R: c( person( diff --git a/R/mo.R b/R/mo.R index ecf598ae..2c23109f 100755 --- a/R/mo.R +++ b/R/mo.R @@ -84,7 +84,6 @@ #' \itemize{ #' \item{\code{"Streptococcus group B (known as S. agalactiae)"}. The text between brackets will be removed and a warning will be thrown that the result \emph{Streptococcus group B} (\code{B_STRPT_GRB}) needs review.} #' \item{\code{"S. aureus - please mind: MRSA"}. The last word will be stripped, after which the function will try to find a match. If it does not, the second last word will be stripped, etc. Again, a warning will be thrown that the result \emph{Staphylococcus aureus} (\code{B_STPHY_AUR}) needs review.} -#' \item{\code{"D. spartina"}. This is the abbreviation of an old taxonomic name: \emph{Didymosphaeria spartinae} (the last "e" was missing from the input). This fungus was renamed to \emph{Leptosphaeria obiones}, so a warning will be thrown that this result (\code{F_LPTSP_OBI}) needs review.} #' \item{\code{"Fluoroquinolone-resistant Neisseria gonorrhoeae"}. The first word will be stripped, after which the function will try to find a match. A warning will be thrown that the result \emph{Neisseria gonorrhoeae} (\code{B_NESSR_GON}) needs review.} #' } #' @@ -156,6 +155,7 @@ #' mutate(mo = as.mo(paste(genus, species))) #' } as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = TRUE, reference_df = get_mo_source()) { + # will be checked for mo class in validation mo <- mo_validate(x = x, property = "mo", Becker = Becker, Lancefield = Lancefield, allow_uncertain = allow_uncertain, reference_df = reference_df) @@ -170,7 +170,7 @@ is.mo <- function(x) { #' @importFrom dplyr %>% pull left_join n_distinct progress_estimated filter #' @importFrom data.table data.table as.data.table setkey -#' @importFrom crayon magenta red silver italic has_color +#' @importFrom crayon magenta red blue silver italic has_color exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = TRUE, reference_df = get_mo_source(), property = "mo", clear_options = TRUE) { @@ -210,12 +210,12 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, uncertainties <- character(0) failures <- character(0) x_input <- x + x <- trimws(x, which = "both") # only check the uniques, which is way faster x <- unique(x) # remove empty values (to later fill them in again with NAs) x <- x[!is.na(x) & !is.null(x) & !identical(x, "")] - # 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}")) { leftpart <- gsub("^([BFP]_[A-Z]{3,7}).*", "\\1", x) @@ -271,7 +271,7 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, } else if (!all(x %in% microorganismsDT[[property]])) { - x_backup <- trimws(x, which = "both") + x_backup <- x # trimws(x, which = "both") # remove spp and species x <- trimws(gsub(" +(spp.?|ssp.?|sp.? |ss ?.?|subsp.?|subspecies|biovar |serovar |species)", " ", x_backup, ignore.case = TRUE), which = "both") @@ -323,6 +323,13 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, progress$tick()$print() + found <- microorganismsDT[mo == toupper(x_backup[i]), ..property][[1]] + # is a valid MO code + if (length(found) > 0) { + x[i] <- found[1L] + next + } + if (tolower(x_trimmed[i]) %in% c("", "xxx", "other", "none", "unknown")) { # empty and nonsense values, ignore without warning ("xxx" is WHONET code for 'no growth') x[i] <- NA_character_ @@ -510,11 +517,11 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, return(found[1L]) } - found <- data_to_check[mo == toupper(a.x_backup), ..property][[1]] - # is a valid mo - if (length(found) > 0) { - return(found[1L]) - } + # found <- data_to_check[mo == toupper(a.x_backup), ..property][[1]] + # # is a valid mo + # if (length(found) > 0) { + # return(found[1L]) + # } found <- data_to_check[tolower(fullname) == tolower(c.x_trimmed_without_group), ..property][[1]] if (length(found) > 0) { return(found[1L]) @@ -872,7 +879,7 @@ TEMPORARY_TAXONOMY <- function(x) { x } -#' @importFrom crayon blue italic +#' @importFrom crayon italic was_renamed <- function(name_old, name_new, ref_old = "", ref_new = "", mo = "") { if (!is.na(ref_old)) { ref_old <- paste0(" (", ref_old, ")") diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index f9e3bbcb..d95f132e 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -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 22 February 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 23 February 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 (1284 changes)
+#> Table 1: Intrinsic resistance in Enterobacteriaceae (1334 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 (2790 changes)
+#> Table 4: Intrinsic resistance in Gram-positive bacteria (2731 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,321 out of 20,000 rows
+#> => EUCAST rules affected 7,419 out of 20,000 rows
#> -> added 0 test results
-#> -> changed 4,074 test results (0 to S; 0 to I; 4,074 to R)
So only 28.4% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
So only 28.3% 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.
isolate | @@ -654,10 +654,10 @@|||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | -2010-01-10 | -X9 | +2010-02-08 | +H1 | B_ESCHR_COL | -R | +S | S | S | S | @@ -666,47 +666,47 @@|||
2 | -2010-04-18 | -X9 | -B_ESCHR_COL | -R | -I | -S | -S | -FALSE | -FALSE | -||||
3 | -2010-07-02 | -X9 | +2010-04-06 | +H1 | B_ESCHR_COL | R | S | S | S | FALSE | -FALSE | -||
4 | -2010-09-21 | -X9 | -B_ESCHR_COL | -R | -S | -R | -S | -FALSE | TRUE | ||||
5 | -2010-09-22 | -X9 | +3 | +2010-04-25 | +H1 | B_ESCHR_COL | +S | R | S | +R | +FALSE | +TRUE | +|
4 | +2010-10-05 | +H1 | +B_ESCHR_COL | +I | +S | +S | +R | +FALSE | +TRUE | +||||
5 | +2010-11-09 | +H1 | +B_ESCHR_COL | +S | +S | S | S | FALSE | @@ -714,23 +714,23 @@|||||
6 | -2010-10-06 | -X9 | +2010-11-23 | +H1 | B_ESCHR_COL | +R | S | -S | -S | +R | S | FALSE | TRUE |
7 | -2010-10-14 | -X9 | +2010-12-26 | +H1 | B_ESCHR_COL | R | -S | +I | S | S | FALSE | @@ -738,47 +738,47 @@||
8 | -2011-01-09 | -X9 | +2011-01-01 | +H1 | B_ESCHR_COL | S | -I | S | -R | +S | +S | FALSE | TRUE |
9 | -2011-03-31 | -X9 | +2011-01-21 | +H1 | B_ESCHR_COL | R | +I | S | S | -S | -TRUE | -TRUE | -|
10 | -2011-03-31 | -X9 | -B_ESCHR_COL | -S | -S | -R | -S | FALSE | TRUE | ||||
10 | +2011-02-28 | +H1 | +B_ESCHR_COL | +S | +S | +S | +S | +TRUE | +TRUE | +
Instead of 2, now 8 isolates are flagged. In total, 79.3% of all isolates are marked ‘first weighted’ - 50.9% 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 2, now 10 isolates are flagged. In total, 79.3% of all isolates are marked ‘first weighted’ - 50.9% 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,854 isolates for analysis.
+So we end up with 15,851 isolates for analysis.
We can remove unneeded columns:
@@ -786,7 +786,6 @@date | patient_id | hospital | @@ -803,14 +802,13 @@|||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2 | -2016-11-18 | -I10 | +2011-09-14 | +N3 | Hospital B | B_ESCHR_COL | -R | S | -R | +S | +S | S | M | Gram negative | @@ -819,10 +817,9 @@TRUE | ||||||||
5 | -2017-01-25 | -H5 | -Hospital C | +2011-01-09 | +I3 | +Hospital A | B_ESCHR_COL | R | S | @@ -835,67 +832,63 @@TRUE | |||||||||||||
6 | -2017-03-12 | -B9 | -Hospital C | -B_ESCHR_COL | -S | -S | -S | +2015-06-02 | +E8 | +Hospital A | +B_STRPT_PNE | +R | S | +R | +R | M | -Gram negative | -Escherichia | -coli | +Gram positive | +Streptococcus | +pneumoniae | TRUE |
7 | -2015-08-12 | -Y4 | -Hospital B | -B_STPHY_AUR | -R | +2011-02-06 | +S1 | +Hospital D | +B_ESCHR_COL | +S | S | S | S | F | -Gram positive | -Staphylococcus | -aureus | +Gram negative | +Escherichia | +coli | TRUE | ||
9 | -2016-01-24 | -L10 | -Hospital A | +2010-01-27 | +N7 | +Hospital C | B_ESCHR_COL | +R | +I | +R | S | -S | -S | -S | -M | +F | Gram negative | Escherichia | coli | TRUE | |||
12 | -2013-09-11 | -H6 | +2017-08-11 | +U3 | Hospital B | -B_STPHY_AUR | +B_ESCHR_COL | S | S | -R | S | -M | -Gram positive | -Staphylococcus | -aureus | +S | +F | +Gram negative | +Escherichia | +coli | TRUE | ||
1 | Escherichia coli | -7,918 | -49.9% | -7,918 | -49.9% | +7,800 | +49.2% | +7,800 | +49.2% | ||||||||||||||
2 | Staphylococcus aureus | -3,930 | -24.8% | -11,848 | -74.7% | +4,008 | +25.3% | +11,808 | +74.5% | ||||||||||||||
3 | Streptococcus pneumoniae | -2,498 | -15.8% | -14,346 | -90.5% | +2,445 | +15.4% | +14,253 | +89.9% | ||||||||||||||
4 | Klebsiella pneumoniae | -1,508 | -9.5% | -15,854 | +1,598 | +10.1% | +15,851 | 100.0% | |||||||||||||||
Hospital A | -0.4737395 | +0.4877378 | |||||||||||||||||||||
Hospital B | -0.4763709 | +0.4750000 | |||||||||||||||||||||
Hospital C | -0.4739257 | +0.4869240 | |||||||||||||||||||||
Hospital D | -0.4636854 | +0.4860406 |
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 10 milliseconds means it can determine 100 input values per second. It case of 50 milliseconds, this is only 20 input values per second. The more an input value resembles a full name, the faster the result will be found.
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 Mycoplasma leonicaptivi (B_MYCPL_LEO
), a bug probably never found before in humans:
M.leonicaptivi <- microbenchmark(as.mo("myle"),
@@ -237,13 +237,13 @@
print(M.leonicaptivi, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
-#> as.mo("myle") 141 142 162 142 142 299 10
-#> as.mo("mycleo") 479 481 520 525 530 634 10
-#> as.mo("M. leonicaptivi") 241 242 273 263 281 382 10
-#> as.mo("M. leonicaptivi") 239 241 268 282 283 299 10
-#> as.mo("MYCLEO") 487 520 525 524 528 601 10
-#> as.mo("Mycoplasma leonicaptivi") 152 156 183 174 200 261 10
That takes 7.3 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.
+#> as.mo("myle") 131 132 132 132 133 133 10 +#> as.mo("mycleo") 439 445 471 481 488 505 10 +#> as.mo("M. leonicaptivi") 202 205 234 243 247 262 10 +#> as.mo("M. leonicaptivi") 202 202 221 212 242 249 10 +#> as.mo("MYCLEO") 441 449 469 480 486 493 10 +#> as.mo("Mycoplasma leonicaptivi") 143 143 165 165 185 190 10 +That takes 9.2 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.
In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Mycoplasma leonicaptivi (which is very uncommon):
par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)
@@ -283,8 +283,8 @@
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
-#> mo_fullname(x) 400 405 463 441 533 558 10
So transforming 500,000 values (!) of 95 unique values only takes 0.44 seconds (441 ms). You only lose time on your unique input values.
+#> mo_fullname(x) 618 653 729 695 813 846 10 +So transforming 500,000 values (!) of 95 unique values only takes 0.69 seconds (694 ms). You only lose time on your unique input values.
So going from mo_fullname("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0004 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"),
@@ -313,14 +313,14 @@
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
-#> A 0.298 0.327 0.398 0.400 0.452 0.535 10
-#> B 0.251 0.287 0.339 0.344 0.377 0.436 10
-#> C 0.293 0.403 0.451 0.487 0.500 0.537 10
-#> D 0.250 0.262 0.300 0.277 0.336 0.395 10
-#> E 0.249 0.261 0.306 0.313 0.344 0.384 10
-#> F 0.273 0.283 0.325 0.326 0.338 0.420 10
-#> G 0.238 0.293 0.312 0.325 0.342 0.356 10
-#> H 0.250 0.262 0.304 0.316 0.337 0.358 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 49660c8e..daad2819 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 0e5b2b51..3d5d055a 100644 --- a/docs/articles/freq.html +++ b/docs/articles/freq.html @@ -192,7 +192,7 @@freq.Rmd
mo_property.Rmd
resistance_predict.Rmd
allow_uncertain = TRUE
(which is the default setting), i
Examples:
"Streptococcus group B (known as S. agalactiae)"
. The text between brackets will be removed and a warning will be thrown that the result Streptococcus group B (B_STRPT_GRB
) needs review.
"S. aureus - please mind: MRSA"
. The last word will be stripped, after which the function will try to find a match. If it does not, the second last word will be stripped, etc. Again, a warning will be thrown that the result Staphylococcus aureus (B_STPHY_AUR
) needs review.
"D. spartina"
. This is the abbreviation of an old taxonomic name: Didymosphaeria spartinae (the last "e" was missing from the input). This fungus was renamed to Leptosphaeria obiones, so a warning will be thrown that this result (F_LPTSP_OBI
) needs review.
"Fluoroquinolone-resistant Neisseria gonorrhoeae"
. The first word will be stripped, after which the function will try to find a match. A warning will be thrown that the result Neisseria gonorrhoeae (B_NESSR_GON
) needs review.
Use mo_failures()
to get a vector with all values that could not be coerced to a valid value.