diff --git a/DESCRIPTION b/DESCRIPTION index 99727499..c63ac516 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: AMR -Version: 1.5.0.9023 -Date: 2021-02-21 +Version: 1.5.0.9024 +Date: 2021-02-22 Title: Antimicrobial Resistance Data Analysis Authors@R: c( person(role = c("aut", "cre"), diff --git a/NAMESPACE b/NAMESPACE index 504c8510..0037a592 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -62,6 +62,7 @@ S3method(print,mo_renamed) S3method(print,mo_uncertainties) S3method(print,pca) S3method(print,rsi) +S3method(rep,mo) S3method(skewness,data.frame) S3method(skewness,default) S3method(skewness,matrix) diff --git a/NEWS.md b/NEWS.md index 063832d2..48fe00dd 100755 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,5 @@ -# AMR 1.5.0.9023 -## Last updated: 21 February 2021 +# AMR 1.5.0.9024 +## Last updated: 22 February 2021 ### New * Support for EUCAST Clinical Breakpoints v11.0 (2021), effective in the `eucast_rules()` function and in `as.rsi()` to interpret MIC and disk diffusion values. This is now the default guideline in this package. diff --git a/R/mo.R b/R/mo.R index 2187c776..f1c1756b 100755 --- a/R/mo.R +++ b/R/mo.R @@ -489,10 +489,10 @@ exec_as.mo <- function(x, # now only continue where the right taxonomic output is not already known if (any(!already_known)) { x_known <- x[already_known] - + # remove spp and species - x <- gsub(" +(spp.?|ssp.?|sp.? |ss ?.?|subsp.?|subspecies|biovar |serovar |species)", " ", x, perl = TRUE) - x <- gsub("(spp.?|subsp.?|subspecies|biovar|serovar|species)", "", x, perl = TRUE) + x <- gsub(" +(spp.?|ssp.?|sp.? |ss ?.?|subsp.?|subspecies|biovar |serovar |species)", " ", x) + x <- gsub("(spp.?|subsp.?|subspecies|biovar|serovar|species)", "", x) x <- gsub("^([a-z]{2,4})(spe.?)$", "\\1", x, perl = TRUE) # when ending in SPE instead of SPP and preceded by 2-4 characters x <- strip_whitespace(x, dyslexia_mode) @@ -528,12 +528,12 @@ exec_as.mo <- function(x, # allow characters that resemble others = dyslexia_mode ---- if (dyslexia_mode == TRUE) { x <- tolower(x) - x <- gsub("[iy]+", "[iy]+", x, perl = TRUE) - x <- gsub("(c|k|q|qu|s|z|x|ks)+", "(c|k|q|qu|s|z|x|ks)+", x, perl = TRUE) - x <- gsub("(ph|hp|f|v)+", "(ph|hp|f|v)+", x, perl = TRUE) - x <- gsub("(th|ht|t)+", "(th|ht|t)+", x, perl = TRUE) - x <- gsub("a+", "a+", x, perl = TRUE) - x <- gsub("u+", "u+", x, perl = TRUE) + x <- gsub("[iy]+", "[iy]+", x) + x <- gsub("(c|k|q|qu|s|z|x|ks)+", "(c|k|q|qu|s|z|x|ks)+", x) + x <- gsub("(ph|hp|f|v)+", "(ph|hp|f|v)+", x) + x <- gsub("(th|ht|t)+", "(th|ht|t)+", x) + x <- gsub("a+", "a+", x) + x <- gsub("u+", "u+", x) # allow any ending of -um, -us, -ium, -icum, -ius, -icus, -ica, -ia and -a (needs perl for the negative backward lookup): x <- gsub("(u\\+\\(c\\|k\\|q\\|qu\\+\\|s\\|z\\|x\\|ks\\)\\+)(?![a-z])", "(u[s|m]|[iy][ck]?u[ms]|[iy]?[ck]?a)", x, perl = TRUE) @@ -543,9 +543,9 @@ exec_as.mo <- function(x, "(u[s|m]|[iy][ck]?u[ms]|[iy]?[ck]?a)", x, perl = TRUE) x <- gsub("(\\[iy\\]\\+a\\+)(?![a-z])", "([iy]*a+|[iy]+a*)", x, perl = TRUE) - x <- gsub("e+", "e+", x, perl = TRUE) - x <- gsub("o+", "o+", x, perl = TRUE) - x <- gsub("(.)\\1+", "\\1+", x, perl = TRUE) + x <- gsub("e+", "e+", x) + x <- gsub("o+", "o+", x) + x <- gsub("(.)\\1+", "\\1+", x) # allow multiplication of all other consonants x <- gsub("([bdgjlnrw]+)", "\\1+", x, perl = TRUE) # allow ending in -en or -us @@ -554,7 +554,7 @@ exec_as.mo <- function(x, # this will allow "Pasteurella damatis" to be correctly read as "Pasteurella dagmatis". consonants <- paste(letters[!letters %in% c("a", "e", "i", "o", "u")], collapse = "") x[nchar(x_backup_without_spp) > 10] <- gsub("[+]", paste0("+[", consonants, "]?"), x[nchar(x_backup_without_spp) > 10]) - # allow au and ou after all these regex implementations + # allow au and ou after all above regex implementations x <- gsub("a+[bcdfghjklmnpqrstvwxyz]?u+[bcdfghjklmnpqrstvwxyz]?", "(a+u+|o+u+)[bcdfghjklmnpqrstvwxyz]?", x, fixed = TRUE) x <- gsub("o+[bcdfghjklmnpqrstvwxyz]?u+[bcdfghjklmnpqrstvwxyz]?", "(a+u+|o+u+)[bcdfghjklmnpqrstvwxyz]?", x, fixed = TRUE) } @@ -1469,7 +1469,7 @@ exec_as.mo <- function(x, plural <- c("s", "them", "were") } msg <- paste0("Translation to ", nr2char(length(uncertainties$input)), " microorganism", plural[1], - " was guessed with uncertainty. Use mo_uncertainties() to review ", plural[2], ".") + " was guessed with uncertainty. Use `mo_uncertainties()` to review ", plural[2], ".") message_(msg) } x[already_known] <- x_known @@ -1807,6 +1807,15 @@ unique.mo <- function(x, incomparables = FALSE, ...) { y } +#' @method rep mo +#' @export +#' @noRd +rep.mo <- function(x, ...) { + y <- NextMethod() + attributes(y) <- attributes(x) + y +} + #' @rdname as.mo #' @export mo_failures <- function() { @@ -1831,7 +1840,7 @@ print.mo_uncertainties <- function(x, ...) { if (NROW(x) == 0) { return(NULL) } - message_("Matching scores are based on human pathogenic prevalence and the resemblance between the input and the full taxonomic name. See ?mo_matching_score.", as_note = FALSE) + message_("Matching scores are based on human pathogenic prevalence and the resemblance between the input and the full taxonomic name. See `?mo_matching_score`.", as_note = FALSE) msg <- "" for (i in seq_len(nrow(x))) { @@ -2009,7 +2018,7 @@ replace_old_mo_codes <- function(x, property) { x[which(!is.na(matched))] <- mo_new[which(!is.na(matched))] n_matched <- length(matched[!is.na(matched)]) if (property != "mo") { - message_(font_blue("The input contained old microbial codes (from previous package versions). Please update your MO codes with as.mo().")) + message_(font_blue("The input contained old microbial codes (from previous package versions). Please update your MO codes with `as.mo()`.")) } else { if (n_matched == 1) { message_(font_blue("1 old microbial code (from previous package versions) was updated to a current used MO code.")) diff --git a/data-raw/AMR_latest.tar.gz b/data-raw/AMR_latest.tar.gz index 58c5a1f4..42618ba5 100644 Binary files a/data-raw/AMR_latest.tar.gz and b/data-raw/AMR_latest.tar.gz differ diff --git a/docs/404.html b/docs/404.html index 0614e287..7549388a 100644 --- a/docs/404.html +++ b/docs/404.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index ea526df4..3751fc57 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 diff --git a/docs/articles/benchmarks.html b/docs/articles/benchmarks.html index fbfbfb39..1d9abe81 100644 --- a/docs/articles/benchmarks.html +++ b/docs/articles/benchmarks.html @@ -39,7 +39,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 @@ -200,17 +200,17 @@ -

One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life. We created a function as.mo() that transforms any user input value to a valid microbial ID by using intelligent rules combined with the taxonomic tree of Catalogue of Life.

-

Using the microbenchmark package, we can review the calculation performance of this function. Its function microbenchmark() runs different input expressions independently of each other and measures their time-to-result.

+

One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life (CoL) and the List of Prokaryotic names with Standing in Nomenclature (LPSN). We created a function as.mo() that transforms any user input value to a valid microbial ID by using intelligent rules combined with the microbial taxonomy.

+

Using the microbenchmark package, we can review the calculation performance of this function. Its function microbenchmark() runs different input expressions independently of each other and measures their time-to-result.

-microbenchmark <- microbenchmark::microbenchmark
+library(microbenchmark)
 library(AMR)
 library(dplyr)

In the next test, we try to ‘coerce’ different input values into the microbial code of Staphylococcus aureus. Coercion is a computational process of forcing output based on an input. For microorganism names, coercing user input to taxonomically valid microorganism names is crucial to ensure correct interpretation and to enable grouping based on taxonomic properties.

-

The actual result is the same every time: it returns its microorganism code B_STPHY_AURS (B stands for Bacteria, the taxonomic kingdom).

+

The actual result is the same every time: it returns its microorganism code B_STPHY_AURS (B stands for Bacteria, its taxonomic kingdom).

But the calculation time differs a lot:

-S.aureus <- microbenchmark(
+S.aureus <- microbenchmark(
   as.mo("sau"), # WHONET code
   as.mo("stau"),
   as.mo("STAU"),
@@ -223,40 +223,47 @@
   as.mo("Sthafilokkockus aaureuz"), # incorrect spelling
   as.mo("MRSA"), # Methicillin Resistant S. aureus
   as.mo("VISA"), # Vancomycin Intermediate S. aureus
-  as.mo("VRSA"), # Vancomycin Resistant S. aureus
-  times = 10)
+  times = 25)
 print(S.aureus, unit = "ms", signif = 2)
 # Unit: milliseconds
-#                                   expr   min    lq  mean median    uq max neval
-#                           as.mo("sau")  11.0  12.0  27.0   13.0  49.0  51    10
-#                          as.mo("stau")  53.0  57.0  76.0   74.0  93.0 100    10
-#                          as.mo("STAU")  53.0  54.0  69.0   56.0  58.0 190    10
-#                        as.mo("staaur")  11.0  12.0  21.0   13.0  42.0  44    10
-#                        as.mo("STAAUR")  11.0  12.0  16.0   13.0  14.0  48    10
-#                     as.mo("S. aureus")  27.0  27.0  38.0   32.0  35.0  75    10
-#                      as.mo("S aureus")  27.0  29.0  38.0   30.0  36.0  73    10
-#         as.mo("Staphylococcus aureus")   3.1   3.2   6.9    3.5   3.7  38    10
-#  as.mo("Staphylococcus aureus (MRSA)") 250.0 260.0 270.0  260.0 280.0 290    10
-#       as.mo("Sthafilokkockus aaureuz") 160.0 200.0 200.0  200.0 210.0 230    10
-#                          as.mo("MRSA")  10.0  11.0  12.0   11.0  13.0  14    10
-#                          as.mo("VISA")  19.0  20.0  26.0   22.0  24.0  61    10
-#                          as.mo("VRSA")  19.0  20.0  24.0   21.0  22.0  56    10
-

-

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. It is clear that accepted taxonomic names are extremely fast, but some variations can take up to 500-1000 times as much time.

-

To improve performance, two important calculations take almost no time at all: repetitive results and already precalculated results.

+# expr min lq mean median uq max neval +# as.mo("sau") 9.3 10 11.0 10 11.0 13.0 25 +# as.mo("stau") 52.0 55 73.0 58 92.0 100.0 25 +# as.mo("STAU") 50.0 54 73.0 58 96.0 110.0 25 +# as.mo("staaur") 9.7 10 14.0 11 12.0 57.0 25 +# as.mo("STAAUR") 8.9 10 14.0 10 11.0 52.0 25 +# as.mo("S. aureus") 26.0 28 41.0 29 67.0 76.0 25 +# as.mo("S aureus") 27.0 28 41.0 30 65.0 76.0 25 +# as.mo("Staphylococcus aureus") 2.6 3 3.2 3 3.3 4.6 25 +# as.mo("Staphylococcus aureus (MRSA)") 240.0 260 270.0 260 270.0 380.0 25 +# as.mo("Sthafilokkockus aaureuz") 160.0 190 200.0 200 200.0 300.0 25 +# as.mo("MRSA") 9.3 10 15.0 10 12.0 49.0 25 +# as.mo("VISA") 18.0 19 31.0 21 54.0 67.0 25
+

+

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 200 milliseconds, this is only 5 input values per second. It is clear that accepted taxonomic names are extremely fast, but some variations are up to 200 times slower to determine.

+

To improve performance, we implemented two important algorithms to save unnecessary calculations: repetitive results and already precalculated results.

Repetitive results

-

Repetitive results are unique values that are present more than once. Unique values will only be calculated once by as.mo(). We will use mo_name() for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses as.mo() internally.

+

Repetitive results are values that are present more than once in a vector. Unique values will only be calculated once by as.mo(). So running as.mo(c("E. coli", "E. coli")) will check the value "E. coli" only once.

+

To prove this, we will use mo_name() for testing - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses as.mo() internally.

-# take all MO codes from the example_isolates data set
-x <- example_isolates$mo %>%
+# start with the example_isolates data set
+x <- example_isolates %>% 
+  # take all MO codes from the 'mo' column
+  pull(mo) %>%
   # and copy them a thousand times
   rep(1000) %>%
   # then scramble them
   sample()
+
+# what do these values look like? They are of class <mo>:
+head(x)
+# Class <mo>
+# [1] B_STPHY_AURS B_STRPT_GRPC B_STPHY_CONS B_STPHY_EPDR B_STRPT_PNMN
+# [6] B_PROTS_VLGR
   
-# as the example_isolates has 2,000 rows, we should have 2 million items
+# as the example_isolates data set has 2,000 rows, we should have 2 million items
 length(x)
 # [1] 2000000
 
@@ -265,32 +272,32 @@
 # [1] 90
 
 # now let's see:
-run_it <- microbenchmark(mo_name(x),
+run_it <- microbenchmark(mo_name(x),
                          times = 10)
 print(run_it, unit = "ms", signif = 3)
 # Unit: milliseconds
 #        expr min  lq mean median  uq max neval
-#  mo_name(x) 137 146  178    172 193 282    10
-

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.172 seconds. You only lose time on your unique input values.

+# mo_name(x) 157 187 222 206 224 372 10
+

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.206 seconds. That is 2.471 milliseconds per unique item on average. You only lose time on your unique input values.

Precalculated results

-

What about precalculated results? If the input is an already precalculated result of a helper function like mo_name(), it almost doesn’t take any time at all (see ‘C’ below):

+

What about precalculated results? If the input is an already precalculated result of a helper function such as mo_name(), it almost doesn’t take any time at all. In other words, if you run mo_name() on a valid taxonomic name, it will return the results immediately (see ‘C’ below):

-run_it <- microbenchmark(A = mo_name("STAAUR"),
+run_it <- microbenchmark(A = mo_name("STAAUR"),
                          B = mo_name("S. aureus"),
                          C = mo_name("Staphylococcus aureus"),
                          times = 10)
 print(run_it, unit = "ms", signif = 3)
 # Unit: milliseconds
 #  expr   min    lq  mean median    uq   max neval
-#     A  7.12  7.56  7.89    7.7  8.39  8.69    10
-#     B 23.90 24.50 35.10   24.8 27.40 77.50    10
-#     C  1.73  1.84  1.95    1.9  2.09  2.36    10
-

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0019 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:

+# A 6.97 7.10 7.55 7.21 8.18 8.32 10 +# B 23.70 24.20 36.30 26.30 29.00 89.70 10 +# C 1.53 1.65 1.80 1.76 2.00 2.19 10
+

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0018 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"),
+run_it <- microbenchmark(A = mo_species("aureus"),
                          B = mo_genus("Staphylococcus"),
                          C = mo_name("Staphylococcus aureus"),
                          D = mo_family("Staphylococcaceae"),
@@ -302,15 +309,15 @@
 print(run_it, unit = "ms", signif = 3)
 # Unit: milliseconds
 #  expr  min   lq mean median   uq  max neval
-#     A 1.54 1.57 1.73   1.69 1.88 2.00    10
-#     B 1.51 1.52 1.75   1.70 1.86 2.41    10
-#     C 1.55 1.60 1.70   1.70 1.81 1.86    10
-#     D 1.55 1.59 1.71   1.66 1.83 1.94    10
-#     E 1.50 1.55 1.68   1.61 1.78 2.10    10
-#     F 1.51 1.62 1.75   1.79 1.82 2.02    10
-#     G 1.52 1.53 1.68   1.60 1.80 2.02    10
-#     H 1.53 1.54 1.63   1.60 1.67 1.84    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" anyway, 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.

+# A 1.44 1.72 1.69 1.74 1.75 1.78 10 +# B 1.47 1.55 1.67 1.73 1.74 1.89 10 +# C 1.52 1.59 1.73 1.74 1.89 1.91 10 +# D 1.46 1.51 1.64 1.64 1.75 1.89 10 +# E 1.42 1.47 1.60 1.52 1.71 1.97 10 +# F 1.44 1.71 1.69 1.72 1.75 1.88 10 +# G 1.42 1.50 1.68 1.61 1.73 2.36 10 +# H 1.52 1.54 1.66 1.71 1.73 1.87 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" anyway, there is no point in calculating the result. And because this package contains all phyla of all known bacteria, it can just return the initial value immediately.

@@ -326,7 +333,7 @@ mo_name("CoNS", language = "nl") # or just mo_name("CoNS") on a Dutch system # [1] "Coagulase-negatieve Staphylococcus (CNS)" -run_it <- microbenchmark(en = mo_name("CoNS", language = "en"), +run_it <- microbenchmark(en = mo_name("CoNS", language = "en"), de = mo_name("CoNS", language = "de"), nl = mo_name("CoNS", language = "nl"), es = mo_name("CoNS", language = "es"), @@ -336,15 +343,15 @@ times = 100) print(run_it, unit = "ms", signif = 4) # Unit: milliseconds -# expr min lq mean median uq max neval -# en 17.38 17.71 25.63 18.11 19.75 81.61 100 -# de 20.14 20.61 24.87 20.91 21.57 85.23 100 -# nl 25.02 25.46 28.40 25.83 26.58 78.47 100 -# es 19.90 20.41 24.86 20.77 21.78 81.38 100 -# it 20.01 20.44 24.40 20.80 21.57 76.08 100 -# fr 19.85 20.23 29.97 20.89 23.56 192.40 100 -# pt 19.90 20.26 26.53 20.75 22.56 85.69 100

-

Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.

+# expr min lq mean median uq max neval +# en 17.12 17.40 22.65 17.54 18.39 78.39 100 +# de 19.93 20.26 22.60 20.48 20.97 81.27 100 +# nl 24.87 25.24 30.93 25.50 26.90 87.17 100 +# es 20.00 20.26 24.39 20.58 21.13 82.01 100 +# it 19.92 20.26 26.54 20.66 21.38 79.15 100 +# fr 19.62 19.90 24.74 20.10 21.04 77.20 100 +# pt 19.74 20.02 26.76 20.41 22.68 82.61 100 +

Currently supported non-English languages are German, Dutch, Spanish, Italian, French and Portuguese.

diff --git a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png index c643f012..f8429aaf 100644 Binary files a/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png and b/docs/articles/benchmarks_files/figure-html/unnamed-chunk-4-1.png differ diff --git a/docs/articles/index.html b/docs/articles/index.html index ec8d95b2..28653326 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 diff --git a/docs/authors.html b/docs/authors.html index 29ad2cfc..245ceccf 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 diff --git a/docs/index.html b/docs/index.html index d631d734..3689a9d8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,7 +43,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 diff --git a/docs/news/index.html b/docs/news/index.html index 5388f298..5e1588e0 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024 @@ -236,13 +236,13 @@ Source: NEWS.md -
-

-AMR 1.5.0.9023 Unreleased +
+

+AMR 1.5.0.9024 Unreleased

-
+

-Last updated: 21 February 2021 +Last updated: 22 February 2021

diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index 929b6715..67708790 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -12,7 +12,7 @@ articles: datasets: datasets.html resistance_predict: resistance_predict.html welcome_to_AMR: welcome_to_AMR.html -last_built: 2021-02-21T22:18Z +last_built: 2021-02-22T19:20Z urls: reference: https://msberends.github.io/AMR//reference article: https://msberends.github.io/AMR//articles diff --git a/docs/reference/index.html b/docs/reference/index.html index 1bab2d20..df67210c 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024

diff --git a/docs/reference/like.html b/docs/reference/like.html index 885eac75..955d0355 100644 --- a/docs/reference/like.html +++ b/docs/reference/like.html @@ -82,7 +82,7 @@ AMR (for R) - 1.5.0.9021 + 1.5.0.9024
diff --git a/docs/survey.html b/docs/survey.html index fe19f59c..51aec706 100644 --- a/docs/survey.html +++ b/docs/survey.html @@ -81,7 +81,7 @@ AMR (for R) - 1.5.0.9023 + 1.5.0.9024
diff --git a/vignettes/benchmarks.Rmd b/vignettes/benchmarks.Rmd index 391b2ba1..b5b56810 100755 --- a/vignettes/benchmarks.Rmd +++ b/vignettes/benchmarks.Rmd @@ -17,47 +17,47 @@ knitr::opts_chunk$set( collapse = TRUE, comment = "#", fig.width = 7.5, - fig.height = 4.5, - dpi = 75 + fig.height = 5.5, + dpi = 100 ) ``` -One of the most important features of this package is the complete microbial taxonomic database, supplied by the [Catalogue of Life](http://catalogueoflife.org). We created a function `as.mo()` that transforms any user input value to a valid microbial ID by using intelligent rules combined with the taxonomic tree of Catalogue of Life. +One of the most important features of this package is the complete microbial taxonomic database, supplied by the [Catalogue of Life](http://www.catalogueoflife.org) (CoL) and the [List of Prokaryotic names with Standing in Nomenclature](https://lpsn.dsmz.de) (LPSN). We created a function `as.mo()` that transforms any user input value to a valid microbial ID by using intelligent rules combined with the microbial taxonomy. Using the `microbenchmark` package, we can review the calculation performance of this function. Its function `microbenchmark()` runs different input expressions independently of each other and measures their time-to-result. ```{r, message = FALSE, echo = FALSE} library(ggplot2) -ggplot.bm <- function(df, title = NULL) { - s <- summary(df)[order(summary(df)$median), ] - suppressWarnings( - print( - s %>% - ggplot(aes(x = expr, y = median)) + - geom_linerange(aes(ymin = 0, ymax = median), colour = "#555555") + - geom_text(aes(label = round(s$median, 0), hjust = -0.5), size = 3) + - geom_point(size = 3, colour = "#555555") + - coord_flip() + - scale_y_log10(breaks = c(1, 2, 5, - 10, 20, 50, - 100, 200, 500, - 1000, 2000, 5000), - limits = c(1, max(s$median) * 2)) + - labs(x = "Expression", y = "Median time in milliseconds (log scale)", title = title) - ) - ) +ggplot.bm <- function(df) { + reorder <- function(.f, .x, .fun, .desc = TRUE) { + summ <- tapply(.x, .f, .fun) + factor(.f, levels = names(summ)[order(summ, decreasing = .desc)], ordered = is.ordered(.f)) + } + ggplot(df, + aes(x = reorder(expr, time, median), y = time / 1000 / 1000)) + + stat_boxplot(geom = "errorbar", width = 0.5) + + geom_boxplot(outlier.alpha = 0) + + coord_flip() + + scale_y_continuous(trans = "log", breaks = c(1, 2, 5, + 10, 20, 50, + 100, 200, 500, + 1000, 2000, 5000)) + + labs(x = "Expression", + y = "Time in milliseconds (log scale)") + + theme_minimal() + + theme(axis.text.y = element_text(family = "mono")) } ``` ```{r, message = FALSE} -microbenchmark <- microbenchmark::microbenchmark +library(microbenchmark) library(AMR) library(dplyr) ``` In the next test, we try to 'coerce' different input values into the microbial code of *Staphylococcus aureus*. Coercion is a computational process of forcing output based on an input. For microorganism names, coercing user input to taxonomically valid microorganism names is crucial to ensure correct interpretation and to enable grouping based on taxonomic properties. -The actual result is the same every time: it returns its microorganism code ``r as.character(as.mo("Staphylococcus aureus"))`` (*B* stands for *Bacteria*, the taxonomic kingdom). +The actual result is the same every time: it returns its microorganism code ``r as.character(as.mo("Staphylococcus aureus"))`` (*B* stands for *Bacteria*, its taxonomic kingdom). But the calculation time differs a lot: @@ -75,31 +75,37 @@ S.aureus <- microbenchmark( as.mo("Sthafilokkockus aaureuz"), # incorrect spelling as.mo("MRSA"), # Methicillin Resistant S. aureus as.mo("VISA"), # Vancomycin Intermediate S. aureus - as.mo("VRSA"), # Vancomycin Resistant S. aureus - times = 10) + times = 25) print(S.aureus, unit = "ms", signif = 2) ``` ```{r, echo = FALSE} ggplot.bm(S.aureus) ``` -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. It is clear that accepted taxonomic names are extremely fast, but some variations can take up to 500-1000 times as much time. +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 200 milliseconds, this is only 5 input values per second. It is clear that accepted taxonomic names are extremely fast, but some variations are up to 200 times slower to determine. -To improve performance, two important calculations take almost no time at all: **repetitive results** and **already precalculated results**. +To improve performance, we implemented two important algorithms to save unnecessary calculations: **repetitive results** and **already precalculated results**. ### Repetitive results -Repetitive results are unique values that are present more than once. Unique values will only be calculated once by `as.mo()`. We will use `mo_name()` for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses `as.mo()` internally. +Repetitive results are values that are present more than once in a vector. Unique values will only be calculated once by `as.mo()`. So running `as.mo(c("E. coli", "E. coli"))` will check the value `"E. coli"` only once. + +To prove this, we will use `mo_name()` for testing - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses `as.mo()` internally. ```{r, message = FALSE} -# take all MO codes from the example_isolates data set -x <- example_isolates$mo %>% +# start with the example_isolates data set +x <- example_isolates %>% + # take all MO codes from the 'mo' column + pull(mo) %>% # and copy them a thousand times rep(1000) %>% # then scramble them sample() + +# what do these values look like? They are of class : +head(x) -# as the example_isolates has 2,000 rows, we should have 2 million items +# as the example_isolates data set has 2,000 rows, we should have 2 million items length(x) # and how many unique values do we have? @@ -111,11 +117,11 @@ run_it <- microbenchmark(mo_name(x), print(run_it, unit = "ms", signif = 3) ``` -So getting official taxonomic names of `r format(length(x), big.mark = ",")` (!!) items consisting of `r n_distinct(x)` unique values only takes `r round(median(run_it$time, na.rm = TRUE) / 1e9, 3)` seconds. You only lose time on your unique input values. +So getting official taxonomic names of `r format(length(x), big.mark = ",")` (!!) items consisting of `r n_distinct(x)` unique values only takes `r round(median(run_it$time, na.rm = TRUE) / 1e9, 3)` seconds. That is `r round(mean(run_it$time, na.rm = TRUE) / 1e6 / n_distinct(x), 3)` milliseconds per unique item on average. You only lose time on your unique input values. ### Precalculated results -What about precalculated results? If the input is an already precalculated result of a helper function like `mo_name()`, it almost doesn't take any time at all (see 'C' below): +What about precalculated results? If the input is an already precalculated result of a helper function such as `mo_name()`, it almost doesn't take any time at all. In other words, if you run `mo_name()` on a valid taxonomic name, it will return the results immediately (see 'C' below): ```{r, warning=FALSE, message=FALSE} run_it <- microbenchmark(A = mo_name("STAAUR"), @@ -140,7 +146,7 @@ run_it <- microbenchmark(A = mo_species("aureus"), print(run_it, unit = "ms", signif = 3) ``` -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"` anyway, 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. +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"` anyway, there is no point in calculating the result. And because this package contains all phyla of all known bacteria, it can just return the initial value immediately. ### Results in other languages @@ -164,4 +170,4 @@ run_it <- microbenchmark(en = mo_name("CoNS", language = "en"), print(run_it, unit = "ms", signif = 4) ``` -Currently supported are German, Dutch, Spanish, Italian, French and Portuguese. +Currently supported non-English languages are German, Dutch, Spanish, Italian, French and Portuguese.