diff --git a/R/mo.R b/R/mo.R index 2c23109f..3d44c66c 100755 --- a/R/mo.R +++ b/R/mo.R @@ -210,6 +210,7 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, uncertainties <- character(0) failures <- character(0) x_input <- x + # already strip leading and trailing spaces x <- trimws(x, which = "both") # only check the uniques, which is way faster x <- unique(x) @@ -218,6 +219,7 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, # 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}")) { + print("is any") leftpart <- gsub("^([BFP]_[A-Z]{3,7}).*", "\\1", x) if (any(leftpart %in% names(mo_codes_v0.5.0))) { rightpart <- gsub("^[BFP]_[A-Z]{3,7}(.*)", "\\1", x) @@ -241,8 +243,8 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, ) } + # all empty if (all(identical(trimws(x_input), "") | is.na(x_input))) { - # all empty if (property == "mo") { return(structure(rep(NA_character_, length(x_input)), class = "mo")) } else { @@ -264,6 +266,11 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, # existing mo codes when not looking for property "mo", like mo_genus("B_ESCHR_COL") x <- microorganismsDT[data.table(mo = x), on = "mo", ..property][[1]] + } else if (all(x %in% microorganismsDT[prevalence == 1, "fullname"][[1]])) { + # we need special treatment for prevalent full names, they are likely! + # e.g. as.mo("Staphylococcus aureus") + x <- microorganismsDT[prevalence == 1][data.table(fullname = x), on = "fullname", ..property][[1]] + } else if (all(toupper(x) %in% microorganisms.codes[, "code"])) { # commonly used MO codes y <- as.data.table(microorganisms.codes)[data.table(code = toupper(x)), on = "code", ] @@ -271,7 +278,7 @@ exec_as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, } else if (!all(x %in% microorganismsDT[[property]])) { - x_backup <- x # trimws(x, which = "both") + x_backup <- x # remove spp and species x <- trimws(gsub(" +(spp.?|ssp.?|sp.? |ss ?.?|subsp.?|subspecies|biovar |serovar |species)", " ", x_backup, ignore.case = TRUE), which = "both") @@ -517,11 +524,6 @@ 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[tolower(fullname) == tolower(c.x_trimmed_without_group), ..property][[1]] if (length(found) > 0) { return(found[1L]) diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index d95f132e..b0b78828 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -327,70 +327,70 @@
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 %>%
@@ -423,48 +423,50 @@
Finally, we will apply EUCAST rules on our antimicrobial results. In Europe, most medical microbiological laboratories already apply these rules. Our package features their latest insights on intrinsic resistance and exceptional phenotypes. Moreover, the eucast_rules()
function can also apply additional rules, like forcing ampicillin = R when amoxicillin/clavulanic acid = R.
Because the amoxicillin (column amox
) and amoxicillin/clavulanic acid (column amcl
) in our data were generated randomly, some rows will undoubtedly contain amox = S and amcl = R, which is technically impossible. The eucast_rules()
fixes this:
data <- eucast_rules(data, col_mo = "bacteria")
-#>
-#> Rules by the European Committee on Antimicrobial Susceptibility Testing (EUCAST)
+#> [1] "is any"
+#> [1] "is any"
#>
-#> EUCAST Clinical Breakpoints (v9.0, 2019)
-#> Enterobacteriales (Order) (no changes)
-#> Staphylococcus (no changes)
-#> Enterococcus (no changes)
-#> Streptococcus groups A, B, C, G (no changes)
-#> Streptococcus pneumoniae (no changes)
-#> Viridans group streptococci (no changes)
-#> Haemophilus influenzae (no changes)
-#> Moraxella catarrhalis (no changes)
-#> Anaerobic Gram positives (no changes)
-#> Anaerobic Gram negatives (no changes)
-#> Pasteurella multocida (no changes)
-#> Campylobacter jejuni and C. coli (no changes)
-#> Aerococcus sanguinicola and A. urinae (no changes)
-#> Kingella kingae (no changes)
-#>
-#> EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016)
-#> 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 (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)
-#> Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no changes)
-#> Table 12: Interpretive rules for aminoglycosides (no changes)
-#> Table 13: Interpretive rules for quinolones (no changes)
-#>
-#> Other rules
-#> Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (no changes)
-#> Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no changes)
-#> Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no changes)
-#> Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (no changes)
-#> Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no changes)
-#> Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no changes)
-#>
-#> => EUCAST rules affected 7,419 out of 20,000 rows
-#> -> added 0 test results
-#> -> changed 4,065 test results (0 to S; 0 to I; 4,065 to R)
+#> Rules by the European Committee on Antimicrobial Susceptibility Testing (EUCAST)
+#>
+#> EUCAST Clinical Breakpoints (v9.0, 2019)
+#> Enterobacteriales (Order) (no changes)
+#> Staphylococcus (no changes)
+#> Enterococcus (no changes)
+#> Streptococcus groups A, B, C, G (no changes)
+#> Streptococcus pneumoniae (no changes)
+#> Viridans group streptococci (no changes)
+#> Haemophilus influenzae (no changes)
+#> Moraxella catarrhalis (no changes)
+#> Anaerobic Gram positives (no changes)
+#> Anaerobic Gram negatives (no changes)
+#> Pasteurella multocida (no changes)
+#> Campylobacter jejuni and C. coli (no changes)
+#> Aerococcus sanguinicola and A. urinae (no changes)
+#> Kingella kingae (no changes)
+#>
+#> EUCAST Expert Rules, Intrinsic Resistance and Exceptional Phenotypes (v3.1, 2016)
+#> Table 1: Intrinsic resistance in Enterobacteriaceae (1230 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 (2700 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)
+#> Table 11: Interpretive rules for macrolides, lincosamides, and streptogramins (no changes)
+#> Table 12: Interpretive rules for aminoglycosides (no changes)
+#> Table 13: Interpretive rules for quinolones (no changes)
+#>
+#> Other rules
+#> Non-EUCAST: ampicillin = R where amoxicillin/clav acid = R (no changes)
+#> Non-EUCAST: piperacillin = R where piperacillin/tazobactam = R (no changes)
+#> Non-EUCAST: trimethoprim = R where trimethoprim/sulfa = R (no changes)
+#> Non-EUCAST: amoxicillin/clav acid = S where ampicillin = S (no changes)
+#> Non-EUCAST: piperacillin/tazobactam = S where piperacillin = S (no changes)
+#> Non-EUCAST: trimethoprim/sulfa = S where trimethoprim = S (no changes)
+#>
+#> => EUCAST rules affected 7,267 out of 20,000 rows
+#> -> added 0 test results
+#> -> changed 3,930 test results (0 to S; 0 to I; 3,930 to R)
data <- data %>%
mutate(gramstain = mo_gramstain(bacteria),
genus = mo_genus(bacteria),
- species = mo_species(bacteria))
So only 28.3% is suitable for resistance analysis! We can now filter on it with the filter()
function, also from the dplyr
package:
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,8 +662,8 @@||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | -2010-02-08 | -H1 | +2010-04-19 | +S8 | B_ESCHR_COL | S | S | @@ -666,44 +674,44 @@|||||||
2 | -2010-04-06 | -H1 | +2010-08-08 | +S8 | B_ESCHR_COL | -R | S | +I | S | S | FALSE | -TRUE | +FALSE | |
3 | -2010-04-25 | -H1 | +2010-10-31 | +S8 | B_ESCHR_COL | -S | +R | +I | R | S | -R | FALSE | TRUE | |
4 | -2010-10-05 | -H1 | +2010-11-11 | +S8 | B_ESCHR_COL | +S | I | -S | -S | +R | R | FALSE | TRUE | |
5 | -2010-11-09 | -H1 | +2011-04-04 | +S8 | B_ESCHR_COL | S | S | @@ -714,23 +722,23 @@|||||||
6 | -2010-11-23 | -H1 | +2011-05-22 | +S8 | B_ESCHR_COL | -R | S | -R | S | -FALSE | +S | +S | +TRUE | TRUE |
7 | -2010-12-26 | -H1 | +2011-08-15 | +S8 | B_ESCHR_COL | -R | I | +R | S | S | FALSE | @@ -738,23 +746,23 @@|||
8 | -2011-01-01 | -H1 | +2011-08-20 | +S8 | B_ESCHR_COL | S | S | S | -S | +R | FALSE | TRUE | ||
9 | -2011-01-21 | -H1 | +2011-08-25 | +S8 | B_ESCHR_COL | -R | -I | +S | +S | S | S | FALSE | @@ -762,23 +770,23 @@||
10 | -2011-02-28 | -H1 | +2011-12-16 | +S8 | B_ESCHR_COL | S | S | +R | S | -S | -TRUE | +FALSE | TRUE |
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.
+Instead of 2, now 9 isolates are flagged. In total, 79.3% of all isolates are marked ‘first weighted’ - 51% 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,851 isolates for analysis.
+So we end up with 15,865 isolates for analysis.
We can remove unneeded columns:
@@ -786,6 +794,7 @@date | patient_id | hospital | @@ -802,43 +811,14 @@|||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2011-09-14 | -N3 | -Hospital B | -B_ESCHR_COL | -S | -S | -S | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -|||||||||
2011-01-09 | -I3 | -Hospital A | -B_ESCHR_COL | -R | -S | -S | -S | -M | -Gram negative | -Escherichia | -coli | -TRUE | -|||||||||
2015-06-02 | -E8 | -Hospital A | +2 | +2011-10-29 | +H5 | +Hospital D | B_STRPT_PNE | -R | S | -R | +S | +S | R | M | Gram positive | @@ -847,29 +827,31 @@TRUE | |||||
2011-02-06 | -S1 | +3 | +2013-02-03 | +D7 | Hospital D | -B_ESCHR_COL | +B_STRPT_PNE | S | S | S | -S | -F | -Gram negative | -Escherichia | -coli | +R | +M | +Gram positive | +Streptococcus | +pneumoniae | TRUE |
2010-01-27 | -N7 | +4 | +2013-01-14 | +U1 | Hospital C | B_ESCHR_COL | -R | -I | -R | S | +S | +S | +R | F | Gram negative | Escherichia | @@ -877,18 +859,51 @@TRUE | ||||
2017-08-11 | -U3 | -Hospital B | -B_ESCHR_COL | -S | +5 | +2010-02-23 | +O7 | +Hospital A | +B_STPHY_AUR | +R | S | S | S | F | +Gram positive | +Staphylococcus | +aureus | +TRUE | +|||
6 | +2010-09-26 | +E6 | +Hospital D | +B_KLBSL_PNE | +R | +I | +R | +S | +M | Gram negative | -Escherichia | -coli | +Klebsiella | +pneumoniae | +TRUE | +||||||
8 | +2017-06-19 | +J3 | +Hospital B | +B_STPHY_AUR | +S | +S | +S | +S | +M | +Gram positive | +Staphylococcus | +aureus | TRUE | ||||||||
1 | Escherichia coli | -7,800 | -49.2% | -7,800 | -49.2% | +7,999 | +50.4% | +7,999 | +50.4% | ||||||||||||
2 | Staphylococcus aureus | -4,008 | -25.3% | -11,808 | -74.5% | +3,900 | +24.6% | +11,899 | +75.0% | ||||||||||||
3 | Streptococcus pneumoniae | -2,445 | -15.4% | -14,253 | -89.9% | +2,453 | +15.5% | +14,352 | +90.5% | ||||||||||||
4 | Klebsiella pneumoniae | -1,598 | -10.1% | -15,851 | +1,513 | +9.5% | +15,865 | 100.0% | |||||||||||||
Hospital A | -0.4877378 | +0.4644901 | |||||||||||||||||||
Hospital B | -0.4750000 | +0.4754156 | |||||||||||||||||||
Hospital C | -0.4869240 | +0.4927721 | |||||||||||||||||||
Hospital D | -0.4860406 | +0.4793125 |
No errors or warnings, so all values are transformed succesfully. Let’s check it though, with a couple of frequency tables:
+[1] “is any” [1] “is any” [1] “is any”
Frequency table of mo
from a data.frame
(500 x 54)
Class: mo
(character
)
Length: 500 (of which NA: 0 = 0.00%)
diff --git a/docs/articles/benchmarks.html b/docs/articles/benchmarks.html
index 19c332af..8f718c6f 100644
--- a/docs/articles/benchmarks.html
+++ b/docs/articles/benchmarks.html
@@ -217,15 +217,15 @@
times = 10)
print(S.aureus, unit = "ms", signif = 3)
#> Unit: milliseconds
-#> expr min lq mean median uq max neval
-#> as.mo("sau") 10.4 10.5 10.7 10.6 10.7 11.2 10
-#> as.mo("stau") 84.4 84.7 95.6 85.2 101.0 136.0 10
-#> as.mo("staaur") 10.5 10.6 10.8 10.6 11.1 11.2 10
-#> as.mo("S. aureus") 21.3 21.4 31.4 21.9 41.6 60.3 10
-#> as.mo("S. aureus") 21.3 21.4 21.8 21.4 21.5 24.9 10
-#> as.mo("STAAUR") 10.5 10.6 23.5 10.6 43.8 65.0 10
-#> as.mo("Staphylococcus aureus") 16.1 16.2 20.7 16.4 17.5 57.7 10
-
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.
+#> expr min lq mean median uq max neval +#> as.mo("sau") 12.7 12.7 22.7 13.2 13.5 71.1 10 +#> as.mo("stau") 87.6 87.8 90.2 87.9 90.6 104.0 10 +#> as.mo("staaur") 12.6 12.6 12.8 12.7 12.8 13.6 10 +#> as.mo("S. aureus") 24.0 24.2 32.0 24.5 25.4 63.2 10 +#> as.mo("S. aureus") 24.1 24.1 29.3 24.2 24.6 74.0 10 +#> as.mo("STAAUR") 12.6 12.7 16.6 12.7 12.8 51.5 10 +#> as.mo("Staphylococcus aureus") 13.5 13.5 17.9 13.6 14.5 54.5 10 +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 second input is the only one that has to be looked up thoroughly. All the others are known codes (the first is a WHONET code) or common laboratory codes, or common full organism names like the last one.
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"),
as.mo("mycleo"),
@@ -237,13 +237,13 @@
print(M.leonicaptivi, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
-#> 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.
+#> as.mo("myle") 134 135 144 135 138 184 10 +#> as.mo("mycleo") 445 458 479 487 493 500 10 +#> as.mo("M. leonicaptivi") 204 207 224 214 245 248 10 +#> as.mo("M. leonicaptivi") 205 206 223 208 246 250 10 +#> as.mo("MYCLEO") 446 448 480 486 492 529 10 +#> as.mo("Mycoplasma leonicaptivi") 146 149 170 169 191 193 10 +That takes 9.1 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)
@@ -280,11 +280,21 @@
# now let's see:
run_it <- microbenchmark(mo_fullname(x),
times = 10)
-print(run_it, unit = "ms", signif = 3)
-#> Unit: milliseconds
-#> expr min lq mean median uq max neval
-#> 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.
+#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +#> [1] "is any" +print(run_it, unit = "ms", signif = 3) +#> Unit: milliseconds +#> expr min lq mean median uq max neval +#> mo_fullname(x) 623 641 708 648 822 864 10 +So transforming 500,000 values (!) of 95 unique values only takes 0.65 seconds (647 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 +333,14 @@
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
-#> A 0.303 0.338 0.414 0.431 0.453 0.550 10
-#> B 0.244 0.282 0.339 0.363 0.372 0.395 10
-#> C 0.302 0.404 0.437 0.430 0.490 0.527 10
-#> D 0.257 0.279 0.315 0.310 0.344 0.378 10
-#> E 0.219 0.270 0.306 0.298 0.355 0.377 10
-#> F 0.248 0.296 0.312 0.317 0.334 0.349 10
-#> G 0.228 0.248 0.287 0.278 0.336 0.367 10
-#> H 0.250 0.255 0.312 0.312 0.352 0.398 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 daad2819..cd0b6c3f 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/resistance_predict.html b/docs/articles/resistance_predict.html index 2ef2ffe9..15e5cc33 100644 --- a/docs/articles/resistance_predict.html +++ b/docs/articles/resistance_predict.html @@ -305,7 +305,9 @@ filter(mo_gramstain(mo) == "Gram positive") %>% resistance_predict(col_ab = "vanc", year_min = 2010, info = FALSE) %>% ggplot_rsi_predict() -#> NOTE: Using column `date` as input for `col_date`. +#> [1] "is any" +#> [1] "is any" +#> NOTE: Using column `date` as input for `col_date`.Vancomycin resistance could be 100% in ten years, but might also stay around 0%.
You can define the model with the model
parameter. The default model is a generalised linear regression model using a binomial distribution, assuming that a period of zero resistance was followed by a period of increasing resistance leading slowly to more and more resistance.
This seems more likely, doesn’t it?
The model itself is also available from the object, as an attribute
: