# Benchmarks • AMR (for R)

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.

    library(microbenchmark)
    library(AMR)

In the next test, we try to ‘coerce’ different input values for Staphylococcus aureus. The actual result is the same every time: it returns its MO code `B_STPHY_AUR` (B stands for Bacteria, the taxonomic kingdom).

But the calculation time differs a lot:

    S.aureus <- microbenchmark(as.mo("sau"),
                               as.mo("stau"),
                               as.mo("staaur"),
                               as.mo("STAAUR"),
                               as.mo("S. aureus"),
                               as.mo("S.  aureus"),
                               as.mo("Staphylococcus aureus"),
                               times = 10)
    print(S.aureus, unit = "ms", signif = 2)
    # Unit: milliseconds
    #                            expr  min   lq mean median   uq max neval
    #                    as.mo("sau") 18.0 18.0   22   18.0 18.0  63    10
    #                   as.mo("stau") 66.0 66.0   71   66.0 66.0 110    10
    #                 as.mo("staaur") 18.0 18.0   18   18.0 18.0  18    10
    #                 as.mo("STAAUR") 18.0 18.0   18   18.0 18.0  20    10
    #              as.mo("S. aureus") 53.0 53.0   70   53.0 55.0 180    10
    #             as.mo("S.  aureus") 52.0 53.0   73   54.0 97.0 110    10
    #  as.mo("Staphylococcus aureus")  8.2  8.2   17    8.3  8.4  53    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. 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"),
                                   as.mo("THEISL"),
                                   as.mo("T. islandicus"),
                                   as.mo("T.  islandicus"),
                                   as.mo("Thermus islandicus"),
                                   times = 10)
    print(T.islandicus, unit = "ms", signif = 2)
    # Unit: milliseconds
    #                         expr min  lq mean median  uq max neval
    #              as.mo("theisl") 400 400  430    430 450 450    10
    #              as.mo("THEISL") 390 400  420    420 450 460    10
    #       as.mo("T. islandicus") 210 210  260    240 270 430    10
    #      as.mo("T.  islandicus") 210 210  250    260 260 270    10
    #  as.mo("Thermus islandicus")  74  75   94     76 120 120    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)

    boxplot(microbenchmark(as.mo("Thermus islandicus"),
                           as.mo("Prevotella brevis"),
                           as.mo("Escherichia coli"),
                           as.mo("T. islandicus"),
                           as.mo("P. brevis"),
                           as.mo("E. coli"),
                           times = 10),
            horizontal = TRUE, las = 1, unit = "s", log = FALSE,
            xlab = "", ylab = "Time in seconds",
            main = "Benchmarks per prevalence")

Uncommon microorganisms take a lot more time than common microorganisms. To relieve this pitfall and further improve performance, two important calculations take almost no time at all: 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_fullname()` for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses `as.mo()` internally.

    library(dplyr)
    # take all MO codes from the septic_patients data set
    x <- septic_patients$mo %>%
      # keep only the unique ones
      unique() %>%
      # pick 50 of them at random
      sample(50) %>%
      # paste that 10,000 times
      rep(10000) %>%
      # scramble it
      sample()

    # got indeed 50 times 10,000 = half a million?
    length(x)
    # [1] 500000

    # and how many unique values do we have?
    n_distinct(x)
    # [1] 50

    # 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) 1090 1130 1190   1170 1230 1320    10

So transforming 500,000 values (!!) of 50 unique values only takes 1.17 seconds (1167 ms). 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_fullname()`, it almost doesn’t take any time at all (see ‘C’ below):

    run_it <- microbenchmark(A = mo_fullname("B_STPHY_AUR"),
                             B = mo_fullname("S. aureus"),
                             C = mo_fullname("Staphylococcus aureus"),
                             times = 10)
    print(run_it, unit = "ms", signif = 3)
    # Unit: milliseconds
    #  expr   min    lq  mean median    uq    max neval
    #     A 13.00 13.20 13.70  13.60 14.10  14.70    10
    #     B 50.40 50.90 57.80  51.80 52.80 104.00    10
    #     C  1.72  1.77  1.86   1.83  1.98   2.02    10

So going from `mo_fullname("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"),
                             B = mo_genus("Staphylococcus"),
                             C = mo_fullname("Staphylococcus aureus"),
                             D = mo_family("Staphylococcaceae"),
                             E = mo_order("Bacillales"),
                             F = mo_class("Bacilli"),
                             G = mo_phylum("Firmicutes"),
                             H = mo_kingdom("Bacteria"),
                             times = 10)
    print(run_it, unit = "ms", signif = 3)
    # Unit: milliseconds
    #  expr   min    lq  mean median    uq   max neval
    #     A 0.591 0.635 0.719  0.681 0.808 0.968    10
    #     B 0.575 0.643 0.702  0.688 0.738 0.893    10
    #     C 1.550 1.660 1.780  1.730 1.920 2.170    10
    #     D 0.594 0.685 0.725  0.732 0.760 0.928    10
    #     E 0.584 0.614 0.667  0.650 0.730 0.782    10
    #     F 0.473 0.479 0.617  0.629 0.712 0.810    10
    #     G 0.495 0.526 0.576  0.559 0.602 0.756    10
    #     H 0.489 0.519 0.565  0.575 0.607 0.647    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.

### Results in other languages

When the system language is non-English and supported by this `AMR` package, some functions will have a translated result. This almost does’t take extra time:

    mo_fullname("CoNS", language = "en") # or just mo_fullname("CoNS") on an English system
    # [1] "Coagulase-negative Staphylococcus (CoNS)"

    mo_fullname("CoNS", language = "es") # or just mo_fullname("CoNS") on a Spanish system
    # [1] "Staphylococcus coagulasa negativo (SCN)"

    mo_fullname("CoNS", language = "nl") # or just mo_fullname("CoNS") on a Dutch system
    # [1] "Coagulase-negatieve Staphylococcus (CNS)"

    run_it <- microbenchmark(en = mo_fullname("CoNS", language = "en"),
                             de = mo_fullname("CoNS", language = "de"),
                             nl = mo_fullname("CoNS", language = "nl"),
                             es = mo_fullname("CoNS", language = "es"),
                             it = mo_fullname("CoNS", language = "it"),
                             fr = mo_fullname("CoNS", language = "fr"),
                             pt = mo_fullname("CoNS", language = "pt"),
                             times = 10)
    print(run_it, unit = "ms", signif = 4)
    # Unit: milliseconds
    #  expr   min    lq  mean median    uq    max neval
    #    en 43.75 43.82 44.07  43.93 44.28  44.90    10
    #    de 45.96 46.09 47.68  46.20 46.65  59.40    10
    #    nl 59.57 59.72 64.49  59.88 60.34 104.30    10
    #    es 45.82 45.89 50.72  46.30 46.60  90.94    10
    #    it 45.77 45.94 55.86  46.46 48.05  96.85    10
    #    fr 45.67 45.99 55.17  46.39 46.78  92.03    10
    #    pt 45.84 45.92 46.27  46.04 46.36  47.24    10

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