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
#                           as.mo("sau")   8.5  11.0  17.0   12.0  12.0   43.0
#                          as.mo("stau") 120.0 130.0 150.0  140.0 160.0  180.0
#                          as.mo("STAU") 130.0 140.0 150.0  150.0 160.0  170.0
#                        as.mo("staaur")   7.7   9.1  13.0   11.0  12.0   38.0
#                        as.mo("STAAUR")   8.3   9.3  15.0   10.0  11.0   37.0
#                     as.mo("S. aureus")  11.0  12.0  18.0   13.0  14.0   41.0
#                      as.mo("S aureus")   8.8  11.0  17.0   12.0  13.0   41.0
#         as.mo("Staphylococcus aureus")   6.4   6.6   7.4    7.6   7.8    9.1
#  as.mo("Staphylococcus aureus (MRSA)") 810.0 870.0 890.0  890.0 900.0 1000.0
#       as.mo("Sthafilokkockus aaureuz") 320.0 340.0 370.0  350.0 400.0  490.0
#                          as.mo("MRSA")   9.2  10.0  13.0   11.0  12.0   37.0
#                          as.mo("VISA")  12.0  12.0  22.0   13.0  43.0   44.0
#                          as.mo("VRSA")  11.0  13.0  21.0   14.0  38.0   41.0
#                        as.mo(22242419) 130.0 140.0 150.0  140.0 170.0  200.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    max
#                   as.mo("metsem") 143.400 146.300 156.10 155.400 164.900 176.40
#                   as.mo("METSEM") 141.600 146.900 167.00 170.700 185.000 188.00
#              as.mo("M. semesiae")   9.665   9.879  16.50  10.090  11.960  44.29
#             as.mo("M.  semesiae")  10.000  10.080  14.46  11.660  13.140  42.01
#  as.mo("Methanosarcina semesiae")   7.161   7.389  10.40   7.542   9.294  33.00
#  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) 1650 1730 1790   1790 1840 1900    10

So transforming 500,000 values (!!) of 50 unique values only takes 1.79 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 5.680  5.820  9.61   6.36  6.850 39.500    10
#     B 9.790 10.000 10.60  10.40 10.900 11.900    10
#     C 0.229  0.259  0.27   0.27  0.286  0.311    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0003 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.209 0.221 0.236  0.225 0.244 0.311    10
#     B 0.197 0.201 0.215  0.212 0.222 0.266    10
#     C 0.205 0.224 0.243  0.229 0.242 0.383    10
#     D 0.199 0.207 0.216  0.211 0.214 0.270    10
#     E 0.196 0.206 0.218  0.215 0.221 0.270    10
#     F 0.188 0.197 0.212  0.210 0.216 0.269    10
#     G 0.195 0.198 0.213  0.203 0.215 0.299    10
#     H 0.184 0.193 0.205  0.201 0.207 0.252    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  9.303 11.59 14.90  12.40 13.63 45.92   100
#    de 10.080 12.39 15.77  13.11 14.45 46.27   100
#    nl 13.200 16.26 20.88  17.80 19.52 49.93   100
#    es  9.957 12.23 15.57  13.12 14.59 51.99   100
#    it 10.210 12.44 19.02  13.34 14.74 52.96   100
#    fr 10.040 12.40 18.90  13.26 15.07 54.40   100
#    pt 10.450 12.67 16.91  13.46 14.68 51.47   100

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