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 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
#                           as.mo("sau")   8.0   8.2   9.1    8.4   8.5   16
#                          as.mo("stau")  37.0  40.0  51.0   52.0  60.0   76
#                          as.mo("STAU")  36.0  38.0  58.0   60.0  68.0  100
#                        as.mo("staaur")   8.2   8.4   9.5    8.6   8.9   14
#                        as.mo("STAAUR")   8.2   8.3  15.0    9.2  14.0   53
#                     as.mo("S. aureus")  13.0  21.0  64.0   21.0  45.0  260
#                      as.mo("S aureus")  13.0  14.0  33.0   24.0  44.0   76
#         as.mo("Staphylococcus aureus")   4.7   4.8   9.9    6.8   7.9   42
#  as.mo("Staphylococcus aureus (MRSA)") 620.0 640.0 770.0  700.0 860.0 1100
#       as.mo("Sthafilokkockus aaureuz") 330.0 350.0 460.0  490.0 560.0  570
#                          as.mo("MRSA")   8.1   8.3  14.0   12.0  13.0   48
#                          as.mo("VISA")  24.0  25.0  34.0   26.0  38.0   59
#                          as.mo("VRSA")  23.0  24.0  37.0   27.0  39.0   78
#                        as.mo(22242419) 120.0 130.0 150.0  140.0 160.0  240
#  neval
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     10
#     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 achieve this speed, the as.mo function also takes into account the prevalence of human pathogenic microorganisms. The downside of this is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of Methanosarcina semesiae (B_MTHNSR_SEMS), a bug probably never found before in humans:

M.semesiae <- microbenchmark(as.mo("metsem"),
                             as.mo("METSEM"),
                             as.mo("M. semesiae"),
                             as.mo("M.  semesiae"),
                             as.mo("Methanosarcina semesiae"),
                             times = 10)
print(M.semesiae, unit = "ms", signif = 4)
# Unit: milliseconds
#                              expr      min       lq     mean   median       uq
#                   as.mo("metsem") 1349.000 1352.000 1597.000 1411.000 1983.000
#                   as.mo("METSEM") 1316.000 2146.000 2069.000 2226.000 2245.000
#              as.mo("M. semesiae")   13.330   14.110   32.960   21.840   53.090
#             as.mo("M.  semesiae")   13.730   20.960   29.720   21.430   40.000
#  as.mo("Methanosarcina semesiae")    4.802    5.171    6.667    6.551    8.036
#       max neval
#  2184.000    10
#  2337.000    10
#    62.780    10
#    64.510    10
#     8.735    10

That takes 6.1 times as much time on average. 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 Methanosarcina semesiae) are always very fast and only take some thousands of seconds to coerce - they 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 Methanosarcina semesiae (which is uncommon):

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_name() 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 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 = 100)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
#        expr min  lq mean median  uq  max neval
#  mo_name(x) 564 605  673    630 657 1100   100

So transforming 500,000 values (!!) of 50 unique values only takes 0.63 seconds (630 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_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.58  6.590  7.340  6.630  6.780 13.00    10
#     B 13.50 13.700 18.700 13.900 14.600 60.80    10
#     C  0.72  0.863  0.917  0.898  0.935  1.26    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0009 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.499 0.511 0.516  0.517 0.522 0.544    10
#     B 0.532 0.539 0.550  0.542 0.563 0.592    10
#     C 0.718 0.787 0.832  0.843 0.889 0.904    10
#     D 0.538 0.548 0.566  0.567 0.571 0.607    10
#     E 0.503 0.509 0.515  0.513 0.516 0.549    10
#     F 0.502 0.504 0.514  0.511 0.519 0.539    10
#     G 0.493 0.513 0.538  0.514 0.536 0.684    10
#     H 0.499 0.501 0.509  0.505 0.516 0.531    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 23.72 25.30 30.59  25.77 26.99  76.03   100
#    de 24.88 26.81 31.11  27.47 28.93  69.86   100
#    nl 30.65 32.77 38.07  33.70 35.23  74.79   100
#    es 24.89 26.33 32.10  27.13 28.87  68.79   100
#    it 24.78 26.72 33.51  27.53 28.91 166.60   100
#    fr 24.84 26.58 31.50  27.13 28.29  67.38   100
#    pt 24.88 26.58 32.38  27.50 29.20  79.30   100

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