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")   9.9  14   22     15  36   48    10
#                          as.mo("stau") 160.0 160  180    170 210  210    10
#                          as.mo("STAU") 160.0 160  180    170 200  210    10
#                        as.mo("staaur")   9.5  13   14     14  16   17    10
#                        as.mo("STAAUR")   9.7  11   13     13  15   19    10
#                     as.mo("S. aureus")  12.0  13   17     15  18   35    10
#                      as.mo("S aureus")  11.0  16   26     19  42   43    10
#         as.mo("Staphylococcus aureus")   8.7  11   19     13  14   55    10
#  as.mo("Staphylococcus aureus (MRSA)") 890.0 910  930    920 950 1000    10
#       as.mo("Sthafilokkockus aaureuz") 420.0 430  450    440 470  510    10
#                          as.mo("MRSA")   9.8  10   13     12  14   19    10
#                          as.mo("VISA")  12.0  14   33     18  43  120    10
#                          as.mo("VRSA")  12.0  15   24     17  37   48    10
#                        as.mo(22242419) 140.0 150  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") 169.600 189.900 199.30 199.70 211.00 223.90
#                   as.mo("METSEM") 170.000 186.900 197.40 191.80 210.70 232.30
#              as.mo("M. semesiae")  11.330  11.620  12.37  12.21  13.20  13.78
#             as.mo("M.  semesiae")  11.410  12.090  15.74  12.48  13.55  43.19
#  as.mo("Methanosarcina semesiae")   8.592   8.906  12.89   9.31  11.54  41.08
#  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) 1610 1650 1680   1670 1710 1780    10

So transforming 500,000 values (!!) of 50 unique values only takes 1.67 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.15  8.69  9.63   9.05 10.40 12.40    10
#     B 11.30 12.70 17.50  13.40 14.80 54.30    10
#     C  1.04  1.17  1.28   1.28  1.38  1.59    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0013 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.852 0.977 1.040  0.990 1.040 1.29    10
#     B 0.802 0.933 1.070  0.994 1.160 1.65    10
#     C 0.804 0.983 1.060  1.000 1.210 1.32    10
#     D 0.783 0.855 0.996  0.989 1.030 1.36    10
#     E 0.820 0.962 1.020  0.982 1.070 1.27    10
#     F 0.770 0.785 0.937  0.953 0.996 1.30    10
#     G 0.963 0.981 1.140  1.160 1.250 1.38    10
#     H 0.778 0.842 0.985  0.977 0.995 1.30    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.84 13.10 18.11  14.63 15.54 58.10   100
#    de 13.58 14.02 18.05  14.61 16.58 69.21   100
#    nl 17.07 17.89 22.36  18.97 20.85 68.70   100
#    es 13.63 14.20 20.75  15.82 16.79 73.94   100
#    it 13.64 13.95 16.95  15.40 16.57 50.96   100
#    fr 13.74 14.12 19.84  15.81 16.62 61.13   100
#    pt 13.58 14.02 19.00  14.90 16.49 53.41   100

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