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.

microbenchmark <- microbenchmark::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).

But the calculation time differs a lot:

S.aureus <- microbenchmark(
  as.mo("sau"), # WHONET code
  as.mo("stau"),
  as.mo("STAU"),
  as.mo("staaur"),
  as.mo("STAAUR"),
  as.mo("S. aureus"),
  as.mo("S aureus"),
  as.mo("Staphylococcus aureus"), # official taxonomic name
  as.mo("Staphylococcus aureus (MRSA)"), # additional text
  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
  as.mo(22242419), # Catalogue of Life ID
  times = 10)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
#                                   expr   min    lq mean median  uq max neval
#                           as.mo("sau")   8.7   9.3   13    9.8  12  40    10
#                          as.mo("stau") 160.0 180.0  200  200.0 210 220    10
#                          as.mo("STAU") 160.0 180.0  190  190.0 200 210    10
#                        as.mo("staaur")   9.8  12.0   15   12.0  12  42    10
#                        as.mo("STAAUR")   8.4   8.7   13   10.0  12  37    10
#                     as.mo("S. aureus")  13.0  16.0   38   18.0  45 150    10
#                      as.mo("S aureus")  12.0  17.0   21   17.0  18  48    10
#         as.mo("Staphylococcus aureus")   7.1   8.7   12    9.7  11  38    10
#  as.mo("Staphylococcus aureus (MRSA)") 880.0 920.0  930  930.0 960 980    10
#       as.mo("Sthafilokkockus aaureuz") 400.0 430.0  450  440.0 460 500    10
#                          as.mo("MRSA")   8.6  12.0   20   12.0  37  42    10
#                          as.mo("VISA")  15.0  17.0   20   18.0  19  40    10
#                          as.mo("VRSA")  13.0  14.0   19   17.0  19  46    10
#                        as.mo(22242419) 140.0 140.0  160  150.0 170 210    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.

To 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_name() for this test - 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 %>%
  # 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_name(x),
                         times = 10)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
#        expr  min   lq mean median   uq  max neval
#  mo_name(x) 1750 1790 1830   1810 1850 1950    10

So transforming 500,000 values (!!) of 50 unique values only takes 1.81 seconds. 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):

run_it <- microbenchmark(A = mo_name("B_STPHY_AURS"),
                         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  6.08  6.23 10.40   6.56  7.03 44.90    10
#     B 11.70 12.00 12.70  12.70 13.60 13.90    10
#     C  1.05  1.11  1.19   1.13  1.25  1.55    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0011 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_name("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.886 1.010 1.040  1.020 1.06 1.25    10
#     B 1.010 1.030 1.150  1.040 1.27 1.64    10
#     C 0.885 1.030 1.110  1.060 1.26 1.29    10
#     D 0.812 0.822 1.000  1.000 1.05 1.43    10
#     E 0.827 0.989 1.070  1.030 1.23 1.35    10
#     F 0.887 0.994 1.070  1.040 1.08 1.35    10
#     G 0.812 0.839 0.969  0.916 1.04 1.32    10
#     H 0.815 1.020 1.090  1.050 1.30 1.37    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.

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_name("CoNS", language = "en") # or just mo_name("CoNS") on an English system
# [1] "Coagulase-negative Staphylococcus (CoNS)"

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

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"),
                         de = mo_name("CoNS", language = "de"),
                         nl = mo_name("CoNS", language = "nl"),
                         es = mo_name("CoNS", language = "es"),
                         it = mo_name("CoNS", language = "it"),
                         fr = mo_name("CoNS", language = "fr"),
                         pt = mo_name("CoNS", language = "pt"),
                         times = 100)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
#  expr   min    lq  mean median    uq    max neval
#    en 12.88 13.52 16.72  14.63 16.00  55.03   100
#    de 13.79 14.51 18.36  15.11 16.67 136.90   100
#    nl 17.72 18.59 22.30  20.15 21.87  54.69   100
#    es 13.78 14.38 19.16  15.35 16.86  49.96   100
#    it 13.83 14.40 18.57  15.24 16.32  58.12   100
#    fr 13.72 14.47 19.67  15.21 17.46  52.22   100
#    pt 13.73 14.43 17.76  15.10 16.85  51.69   100

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