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")  11  12   17     13  15   51    10
#                          as.mo("stau") 150 160  170    170 190  200    10
#                          as.mo("STAU") 160 160  180    190 190  210    10
#                        as.mo("staaur")  12  13   23     15  20   68    10
#                        as.mo("STAAUR")  11  12   20     16  18   44    10
#                     as.mo("S. aureus")  11  13   29     17  44   84    10
#                      as.mo("S aureus")  11  15   21     16  18   46    10
#         as.mo("Staphylococcus aureus")  11  13   16     13  15   41    10
#  as.mo("Staphylococcus aureus (MRSA)") 870 890  920    900 950 1100    10
#       as.mo("Sthafilokkockus aaureuz") 400 410  430    440 450  490    10
#                          as.mo("MRSA")  13  13   17     14  16   40    10
#                          as.mo("VISA")  14  17   25     19  36   46    10
#                          as.mo("VRSA")  13  15   21     17  21   50    10
#                        as.mo(22242419) 130 140  150    150 150  180    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    max
#                   as.mo("metsem") 176.800 179.200 189.20 185.90 194.00 212.60
#                   as.mo("METSEM") 164.400 170.800 193.00 188.20 211.10 243.00
#              as.mo("M. semesiae")  10.950  11.310  19.92  15.41  18.79  50.84
#             as.mo("M.  semesiae")  11.560  11.860  17.66  14.15  16.96  50.76
#  as.mo("Methanosarcina semesiae")   9.408   9.669  18.03  14.12  15.24  42.57
#  neval
#     10
#     10
#     10
#     10
#     10

Looking up arbitrary codes of less prevalent microorganisms costs the most time. 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 some more time than common microorganisms. To 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.

# 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) 1720 1760 1820   1800 1830 1990    10

So transforming 500,000 values (!!) of 50 unique values only takes 1.8 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  8.16  8.35  9.06   8.97  9.75 10.20    10
#     B 10.50 10.60 15.50  12.20 12.80 49.90    10
#     C  1.04  1.15  1.21   1.19  1.27  1.53    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0012 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.948 0.971 1.14  1.020 1.39 1.52    10
#     B 0.968 1.040 1.22  1.190 1.41 1.56    10
#     C 0.979 1.020 1.31  1.260 1.58 1.66    10
#     D 0.964 1.010 1.24  1.190 1.45 1.83    10
#     E 0.977 0.995 1.15  1.030 1.40 1.45    10
#     F 0.878 0.982 1.11  1.010 1.37 1.43    10
#     G 0.929 0.961 1.18  1.000 1.43 1.58    10
#     H 0.901 0.967 1.09  0.998 1.35 1.40    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.09 12.46 15.90  13.86 14.55  57.62   100
#    de 12.92 13.26 19.73  14.63 16.01  61.55   100
#    nl 16.53 17.00 20.26  17.64 19.93  57.54   100
#    es 12.98 13.28 18.27  14.76 15.64 179.30   100
#    it 12.92 13.15 19.20  14.08 16.08  64.07   100
#    fr 12.99 13.21 17.81  13.59 15.71  67.97   100
#    pt 13.00 13.23 17.30  14.35 15.65  69.85   100

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