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")   9.1   9.5  12.0    9.8  10.0  34.0
#                          as.mo("stau")  38.0  41.0  55.0   41.0  42.0 160.0
#                          as.mo("STAU")  41.0  41.0  50.0   44.0  63.0  67.0
#                        as.mo("staaur")   8.6   8.8   9.4    9.4   9.6  10.0
#                        as.mo("STAAUR")   8.6   8.8  12.0    9.3   9.8  37.0
#                     as.mo("S. aureus")  14.0  15.0  51.0   27.0  43.0 250.0
#                      as.mo("S aureus")  14.0  15.0  16.0   15.0  16.0  18.0
#         as.mo("Staphylococcus aureus")   5.1   5.3   5.7    5.5   6.0   6.6
#  as.mo("Staphylococcus aureus (MRSA)") 640.0 670.0 700.0  690.0 730.0 760.0
#       as.mo("Sthafilokkockus aaureuz") 370.0 380.0 420.0  400.0 440.0 510.0
#                          as.mo("MRSA")   8.6   8.7  11.0    9.1   9.6  32.0
#                          as.mo("VISA")  24.0  25.0  38.0   38.0  50.0  53.0
#                          as.mo("VRSA")  24.0  25.0  49.0   37.0  52.0 150.0
#                        as.mo(22242419) 130.0 140.0 150.0  140.0 160.0 160.0
#  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") 1530.00 1561.000 1618.00 1620.000 1665.000
#                   as.mo("METSEM") 1482.00 1523.000 1544.00 1555.000 1567.000
#              as.mo("M. semesiae")   15.01   16.140   27.85   17.080   43.620
#             as.mo("M.  semesiae")   15.54   16.200   19.30   16.570   17.040
#  as.mo("Methanosarcina semesiae")    6.05    6.307    9.35    6.437    7.649
#      max neval
#  1710.00    10
#  1586.00    10
#    50.39    10
#    42.95    10
#    33.42    10

That takes 5.7 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) 572 626  648    645 666 792   100

So transforming 500,000 values (!!) of 50 unique values only takes 0.64 seconds (644 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.730  6.860  7.110  6.950  7.170  8.47    10
#     B 14.500 14.800 18.100 15.500 15.900 43.10    10
#     C  0.726  0.753  0.821  0.791  0.882  1.04    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0008 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.348 0.398 0.405  0.404 0.419 0.438    10
#     B 0.407 0.416 0.461  0.426 0.490 0.667    10
#     C 0.701 0.745 0.757  0.754 0.769 0.814    10
#     D 0.435 0.440 0.497  0.464 0.569 0.588    10
#     E 0.351 0.397 0.435  0.419 0.489 0.520    10
#     F 0.358 0.385 0.396  0.393 0.420 0.430    10
#     G 0.322 0.363 0.409  0.401 0.450 0.508    10
#     H 0.340 0.372 0.384  0.385 0.403 0.432    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 25.28 26.99 33.53  27.88 29.51  61.96   100
#    de 26.99 29.27 36.92  30.02 33.09 160.30   100
#    nl 32.37 34.58 40.59  35.88 37.10  67.43   100
#    es 27.54 29.14 34.40  29.88 31.45  61.89   100
#    it 26.77 28.79 34.28  29.73 31.30  69.41   100
#    fr 27.01 28.81 34.96  29.39 30.79 161.10   100
#    pt 26.76 28.85 34.13  29.69 31.74  63.08   100

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