diff --git a/R/mo.R b/R/mo.R index 2256c09a..dd419d06 100755 --- a/R/mo.R +++ b/R/mo.R @@ -166,6 +166,21 @@ #' mutate(mo = as.mo(paste(genus, species))) #' } as.mo <- function(x, Becker = FALSE, Lancefield = FALSE, allow_uncertain = TRUE, reference_df = get_mo_source()) { + if (all(x %in% AMR::microorganisms$fullname) + & isFALSE(Becker) + & isFALSE(Lancefield) + & is.null(reference_df)) { + # we need special treatment for very prevalent full names, they are likely! + # e.g. as.mo("Staphylococcus aureus") + y <- microorganismsDT[prevalence == 1][data.table(fullname = x), on = "fullname", "mo"][[1]] + if (any(is.na(y))) { + y[is.na(y)] <- microorganismsDT[prevalence == 2][data.table(fullname = x[is.na(y)]), on = "fullname", "mo"][[1]] + } + if (any(is.na(y))) { + y[is.na(y)] <- microorganismsDT[prevalence == 3][data.table(fullname = x[is.na(y)]), on = "fullname", "mo"][[1]] + } + return(y) + } # will be checked for mo class in validation mo_validate(x = x, property = "mo", Becker = Becker, Lancefield = Lancefield, diff --git a/docs/articles/AMR.html b/docs/articles/AMR.html index 9975163c..178e6d5c 100644 --- a/docs/articles/AMR.html +++ b/docs/articles/AMR.html @@ -327,10 +327,32 @@ -2010-07-18 -Z5 +2011-05-27 +C5 Hospital B -Streptococcus pneumoniae +Escherichia coli +R +S +S +S +M + + +2010-08-22 +Q6 +Hospital D +Escherichia coli +S +S +S +S +F + + +2017-03-11 +V9 +Hospital A +Klebsiella pneumoniae S S S @@ -338,19 +360,8 @@ F -2017-01-13 -P8 -Hospital C -Escherichia coli -R -S -S -S -F - - -2014-07-31 -K5 +2012-12-25 +A1 Hospital B Escherichia coli S @@ -359,31 +370,20 @@ S M - -2017-07-12 -U8 -Hospital B -Klebsiella pneumoniae -S -S -S -S -F - -2013-09-10 -Y1 +2011-09-17 +I5 Hospital B -Klebsiella pneumoniae -R +Escherichia coli +S R S S -F +M -2017-07-30 -X10 +2017-05-19 +O7 Hospital C Streptococcus pneumoniae S @@ -411,8 +411,8 @@ #> #> Item Count Percent Cum. Count Cum. Percent #> --- ----- ------- -------- ----------- ------------- -#> 1 M 10,383 51.9% 10,383 51.9% -#> 2 F 9,617 48.1% 20,000 100.0% +#> 1 M 10,437 52.2% 10,437 52.2% +#> 2 F 9,563 47.8% 20,000 100.0%

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 (1340 changes)
+#> Table 1:  Intrinsic resistance in Enterobacteriaceae (1294 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 (2699 changes)
+#> Table 4:  Intrinsic resistance in Gram-positive bacteria (2675 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,406 out of 20,000 rows
+#> => EUCAST rules affected 7,390 out of 20,000 rows
 #>    -> added 0 test results
-#>    -> changed 4,039 test results (0 to S; 0 to I; 4,039 to R)
+#> -> changed 3,969 test results (0 to S; 0 to I; 3,969 to R)

@@ -489,7 +489,7 @@ #> NOTE: Using column `bacteria` as input for `col_mo`. #> NOTE: Using column `date` as input for `col_date`. #> NOTE: Using column `patient_id` as input for `col_patient_id`. -#> => Found 5,676 first isolates (28.4% of total)

+#> => Found 5,670 first isolates (28.4% of total)

So only 28.4% is suitable for resistance analysis! We can now filter on it with the filter() function, also from the dplyr package:

data_1st <- data %>% 
   filter(first == TRUE)
@@ -516,41 +516,41 @@ 1 -2010-03-12 -I7 +2010-03-10 +F7 B_ESCHR_COL S S -S +R S TRUE 2 -2010-04-30 -I7 +2010-03-12 +F7 B_ESCHR_COL +I +I S S -R -S FALSE 3 -2010-06-28 -I7 +2010-06-18 +F7 B_ESCHR_COL -R S +I S S FALSE 4 -2010-07-08 -I7 +2010-10-16 +F7 B_ESCHR_COL S S @@ -560,21 +560,21 @@ 5 -2010-08-22 -I7 +2010-11-12 +F7 B_ESCHR_COL -R -I +S +S S S FALSE 6 -2010-09-04 -I7 +2010-11-24 +F7 B_ESCHR_COL -R +S I S S @@ -582,10 +582,10 @@ 7 -2010-11-16 -I7 +2011-02-24 +F7 B_ESCHR_COL -R +S S S S @@ -593,34 +593,34 @@ 8 -2011-03-31 -I7 +2011-03-30 +F7 B_ESCHR_COL -S -S R S +S +S TRUE 9 -2011-05-25 -I7 +2011-08-09 +F7 B_ESCHR_COL S -S R S +S FALSE 10 -2011-08-25 -I7 +2011-08-14 +F7 B_ESCHR_COL -R S -R +S +S S FALSE @@ -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,935 first weighted isolates (79.7% of total) +#> => Found 15,887 first weighted isolates (79.4% of total) @@ -654,70 +654,70 @@ - - + + - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + @@ -726,10 +726,10 @@ - - + + - + @@ -738,47 +738,47 @@ - - + + - - + + - - + + - + - + - - + + - - + +
isolate
12010-03-12I72010-03-10F7 B_ESCHR_COL S SSR S TRUE TRUE
22010-04-30I72010-03-12F7 B_ESCHR_COLSSRSFALSETRUE
32010-06-28I7B_ESCHR_COLRSSSFALSETRUE
42010-07-08I7B_ESCHR_COLSSSSFALSETRUE
52010-08-22I7B_ESCHR_COLRI I S S FALSE TRUE
32010-06-18F7B_ESCHR_COLSISSFALSEFALSE
42010-10-16F7B_ESCHR_COLSSSSFALSEFALSE
52010-11-12F7B_ESCHR_COLSSSSFALSEFALSE
62010-09-04I72010-11-24F7 B_ESCHR_COLRS I S S
72010-11-16I72011-02-24F7 B_ESCHR_COLRS S S S
82011-03-31I72011-03-30F7 B_ESCHR_COLSS R SSS TRUE TRUE
92011-05-25I72011-08-09F7 B_ESCHR_COL SS R SS FALSEFALSETRUE
102011-08-25I72011-08-14F7 B_ESCHR_COLR SRSS S FALSE TRUE
-

Instead of 2, now 7 isolates are flagged. In total, 79.7% of all isolates are marked ‘first weighted’ - 51.3% 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 5 isolates are flagged. In total, 79.4% of all isolates are marked ‘first weighted’ - 51.1% 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:

data_1st <- data %>% 
   filter_first_weighted_isolate()
-

So we end up with 15,935 isolates for analysis.

+

So we end up with 15,887 isolates for analysis.

We can remove unneeded columns:

data_1st <- data_1st %>% 
   select(-c(first, keyab))
@@ -786,6 +786,7 @@
head(data_1st)
+ @@ -802,41 +803,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + - + @@ -847,38 +819,25 @@ - - - - - + + + + + + - - + + - - - - - - - - - - - - - - - - - + + + @@ -891,6 +850,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
date patient_id hospital
2010-07-18Z5Hospital BB_STRPT_PNESSSRFGram positiveStreptococcuspneumoniaeTRUE
2017-01-13P8Hospital CB_ESCHR_COLRSSSFGram negativeEscherichiacoliTRUE
2014-07-31K512011-05-27C5 Hospital B B_ESCHR_COLSR S S STRUE
2017-07-12U8Hospital BB_KLBSL_PNER22010-08-22Q6Hospital DB_ESCHR_COLS S S S F Gram negativeKlebsiellapneumoniaeEscherichiacoli TRUE
2013-09-10Y1Hospital BB_KLBSL_PNERRSSFGram negativeKlebsiellapneumoniaeTRUE
2017-07-30X1062017-05-19O7 Hospital C B_STRPT_PNE Spneumoniae TRUE
82011-01-19Q7Hospital BB_ESCHR_COLSSSSFGram negativeEscherichiacoliTRUE
92014-06-24S9Hospital BB_STRPT_PNERRSRFGram positiveStreptococcuspneumoniaeTRUE
102011-10-01O10Hospital BB_STPHY_AURRSRSFGram positiveStaphylococcusaureusTRUE

Time for the analysis!

@@ -908,9 +915,9 @@
freq(paste(data_1st$genus, data_1st$species))

Or can be used like the dplyr way, which is easier readable:

data_1st %>% freq(genus, species)
-

Frequency table of genus and species from a data.frame (15,935 x 13)

+

Frequency table of genus and species from a data.frame (15,887 x 13)

Columns: 2
-Length: 15,935 (of which NA: 0 = 0.00%)
+Length: 15,887 (of which NA: 0 = 0.00%)
Unique: 4

Shortest: 16
Longest: 24

@@ -927,33 +934,33 @@ Longest: 24

1 Escherichia coli -7,898 -49.6% -7,898 -49.6% +7,900 +49.7% +7,900 +49.7% 2 Staphylococcus aureus -4,017 -25.2% -11,915 -74.8% +3,951 +24.9% +11,851 +74.6% 3 Streptococcus pneumoniae -2,449 -15.4% -14,364 -90.1% +2,413 +15.2% +14,264 +89.8% 4 Klebsiella pneumoniae -1,571 -9.9% -15,935 +1,623 +10.2% +15,887 100.0% @@ -964,7 +971,7 @@ Longest: 24

Resistance percentages

The functions portion_R, portion_RI, portion_I, portion_IS and portion_S can be used to determine the portion of a specific antimicrobial outcome. They can be used on their own:

data_1st %>% portion_IR(amox)
-#> [1] 0.4776279
+#> [1] 0.4763014

Or can be used in conjuction with group_by() and summarise(), both from the dplyr package:

data_1st %>% 
   group_by(hospital) %>% 
@@ -977,19 +984,19 @@ Longest: 24

Hospital A -0.4740616 +0.4774125 Hospital B -0.4748265 +0.4762754 Hospital C -0.4910305 +0.4630957 Hospital D -0.4777953 +0.4845070 @@ -1007,23 +1014,23 @@ Longest: 24

Hospital A -0.4740616 -4742 +0.4774125 +4715 Hospital B -0.4748265 -5621 +0.4762754 +5606 Hospital C -0.4910305 -2397 +0.4630957 +2371 Hospital D -0.4777953 -3175 +0.4845070 +3195 @@ -1043,27 +1050,27 @@ Longest: 24

Escherichia -0.7295518 -0.9013674 -0.9772094 +0.7267089 +0.9002532 +0.9720253 Klebsiella -0.7517505 -0.9000637 -0.9789943 +0.7233518 +0.8983364 +0.9741220 Staphylococcus -0.7353747 -0.9168534 -0.9788399 +0.7296887 +0.9248292 +0.9787396 Streptococcus -0.7390772 +0.7318690 0.0000000 -0.7390772 +0.7318690 diff --git a/docs/articles/AMR_files/figure-html/plot 1-1.png b/docs/articles/AMR_files/figure-html/plot 1-1.png index 6f5f6c3b..82f75604 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 1-1.png and b/docs/articles/AMR_files/figure-html/plot 1-1.png differ diff --git a/docs/articles/AMR_files/figure-html/plot 3-1.png b/docs/articles/AMR_files/figure-html/plot 3-1.png index 6f22a8ba..fb5561c1 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 3-1.png and b/docs/articles/AMR_files/figure-html/plot 3-1.png differ diff --git a/docs/articles/AMR_files/figure-html/plot 4-1.png b/docs/articles/AMR_files/figure-html/plot 4-1.png index c954d5c6..d8a26ca3 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 4-1.png and b/docs/articles/AMR_files/figure-html/plot 4-1.png differ diff --git a/docs/articles/AMR_files/figure-html/plot 5-1.png b/docs/articles/AMR_files/figure-html/plot 5-1.png index 08fe5b7b..b1b19637 100644 Binary files a/docs/articles/AMR_files/figure-html/plot 5-1.png and b/docs/articles/AMR_files/figure-html/plot 5-1.png differ diff --git a/docs/articles/benchmarks.html b/docs/articles/benchmarks.html index 87f36dc1..117feb83 100644 --- a/docs/articles/benchmarks.html +++ b/docs/articles/benchmarks.html @@ -217,14 +217,14 @@ times = 10) print(S.aureus, unit = "ms", signif = 3) #> Unit: milliseconds -#> expr min lq mean median uq max neval -#> as.mo("sau") 12.1 12.2 12.3 12.2 12.3 12.6 10 -#> as.mo("stau") 81.3 81.9 97.3 82.7 120.0 155.0 10 -#> as.mo("staaur") 12.2 12.3 12.7 12.6 13.2 13.5 10 -#> as.mo("STAAUR") 12.2 12.3 16.5 12.6 13.4 50.7 10 -#> as.mo("S. aureus") 20.1 20.1 25.2 20.1 20.2 69.9 10 -#> as.mo("S. aureus") 20.1 20.2 20.7 20.3 21.6 22.0 10 -#> as.mo("Staphylococcus aureus") 11.2 11.3 11.5 11.3 11.5 12.4 10
+#> expr min lq mean median uq max neval +#> as.mo("sau") 13.40 13.60 17.8 13.60 13.80 51.6 10 +#> as.mo("stau") 83.00 83.30 96.5 85.30 88.40 163.0 10 +#> as.mo("staaur") 13.50 13.50 19.1 13.70 14.90 51.5 10 +#> as.mo("STAAUR") 13.50 13.50 14.1 13.60 13.70 18.2 10 +#> as.mo("S. aureus") 21.40 21.40 22.1 21.50 21.70 25.4 10 +#> as.mo("S. aureus") 21.40 21.40 25.7 21.60 23.30 60.1 10 +#> as.mo("Staphylococcus aureus") 5.63 5.87 15.2 5.94 8.32 57.8 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 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") 446.0 452.0 478.0  486.0 488.0 506    10
-#>              as.mo("THEISL") 446.0 446.0 471.0  467.0 489.0 528    10
-#>       as.mo("T. islandicus")  76.5  77.1  87.4   77.2  85.4 127    10
-#>      as.mo("T.  islandicus")  76.9  76.9  81.4   77.1  79.1 116    10
-#>  as.mo("Thermus islandicus")  67.6  67.7  80.0   67.9 106.0 112    10
-

That takes 8.5 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") 448.0 486.0 483.0 489.0 490.0 510.0 10 +#> as.mo("THEISL") 447.0 489.0 487.0 491.0 493.0 499.0 10 +#> as.mo("T. islandicus") 78.0 78.2 78.9 78.7 78.9 82.3 10 +#> as.mo("T. islandicus") 78.1 78.3 84.4 78.8 81.3 129.0 10 +#> as.mo("Thermus islandicus") 61.8 62.1 75.4 62.8 104.0 109.0 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.

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)
 
@@ -287,8 +287,8 @@
 print(run_it, unit = "ms", signif = 3)
 #> Unit: milliseconds
 #>            expr min  lq mean median  uq max neval
-#>  mo_fullname(x) 679 731  768    762 779 886    10
-

So transforming 500,000 values (!!) of 50 unique values only takes 0.76 seconds (762 ms). You only lose time on your unique input values.

+#> mo_fullname(x) 741 746 806 778 827 968 10 +

So transforming 500,000 values (!!) of 50 unique values only takes 0.78 seconds (778 ms). You only lose time on your unique input values.

@@ -300,11 +300,11 @@ times = 10) print(run_it, unit = "ms", signif = 3) #> Unit: milliseconds -#> expr min lq mean median uq max neval -#> A 10.200 10.400 10.800 10.700 10.900 12.00 10 -#> B 20.700 20.800 21.000 20.900 21.100 22.30 10 -#> C 0.305 0.313 0.454 0.441 0.568 0.72 10

-

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:

+#> expr min lq mean median uq max neval +#> A 10.200 10.300 10.600 10.400 11.00 11.300 10 +#> B 20.500 20.700 21.300 21.400 22.00 22.100 10 +#> C 0.308 0.504 0.589 0.591 0.73 0.863 10 +

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"),
                          C = mo_fullname("Staphylococcus aureus"),
@@ -317,14 +317,14 @@
 print(run_it, unit = "ms", signif = 3)
 #> Unit: milliseconds
 #>  expr   min    lq  mean median    uq   max neval
-#>     A 0.366 0.446 0.563  0.523 0.681 0.785    10
-#>     B 0.365 0.453 0.598  0.661 0.682 0.859    10
-#>     C 0.369 0.477 0.633  0.646 0.674 1.150    10
-#>     D 0.283 0.337 0.455  0.464 0.609 0.611    10
-#>     E 0.309 0.333 0.422  0.393 0.527 0.606    10
-#>     F 0.280 0.316 0.457  0.466 0.557 0.676    10
-#>     G 0.283 0.328 0.422  0.359 0.535 0.638    10
-#>     H 0.295 0.339 0.402  0.368 0.460 0.562    10
+#> A 0.318 0.340 0.388 0.382 0.434 0.474 10 +#> B 0.339 0.362 0.424 0.428 0.449 0.555 10 +#> C 0.331 0.369 0.522 0.526 0.637 0.673 10 +#> D 0.269 0.278 0.313 0.300 0.353 0.384 10 +#> E 0.252 0.266 0.322 0.302 0.349 0.448 10 +#> F 0.241 0.264 0.310 0.313 0.347 0.379 10 +#> G 0.241 0.258 0.310 0.317 0.355 0.386 10 +#> H 0.278 0.289 0.316 0.313 0.334 0.375 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.

@@ -351,13 +351,13 @@ print(run_it, unit = "ms", signif = 4) #> Unit: milliseconds #> expr min lq mean median uq max neval -#> en 13.28 13.65 27.39 31.90 36.32 47.03 10 -#> de 21.46 22.08 32.62 22.15 45.37 80.16 10 -#> nl 21.54 22.09 29.93 22.23 44.86 55.15 10 -#> es 21.53 22.09 28.80 22.10 22.16 57.05 10 -#> it 22.00 22.16 23.19 22.28 23.35 27.89 10 -#> fr 22.15 22.17 26.42 22.34 23.48 45.54 10 -#> pt 22.05 22.09 28.13 22.31 23.76 55.38 10
+#> en 13.23 13.57 16.92 13.69 13.73 46.78 10 +#> de 22.09 22.20 25.72 22.32 23.16 55.31 10 +#> nl 21.66 22.03 22.12 22.15 22.20 22.52 10 +#> es 21.67 22.07 22.32 22.16 22.45 23.26 10 +#> it 21.64 21.86 22.35 22.21 22.48 23.90 10 +#> fr 21.70 22.10 28.72 22.21 22.33 55.28 10 +#> pt 21.78 22.12 28.83 22.19 22.21 55.99 10

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 d61c4155..fabe6abc 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