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
  times = 10)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
#                                   expr   min  lq  mean median     uq    max
#                           as.mo("sau")  11.0  12  16.0   14.0   15.0   40.0
#                          as.mo("stau") 110.0 120 140.0  140.0  150.0  160.0
#                          as.mo("STAU") 110.0 120 140.0  140.0  140.0  150.0
#                        as.mo("staaur")  12.0  13  20.0   15.0   16.0   45.0
#                        as.mo("STAAUR")  15.0  15  26.0   16.0   41.0   58.0
#                     as.mo("S. aureus")  31.0  35  50.0   59.0   62.0   65.0
#                      as.mo("S aureus")  27.0  33  59.0   59.0   62.0  160.0
#         as.mo("Staphylococcus aureus")   1.9   2   2.4    2.4    2.7    2.9
#  as.mo("Staphylococcus aureus (MRSA)") 910.0 960 980.0  970.0 1000.0 1100.0
#       as.mo("Sthafilokkockus aaureuz") 400.0 410 430.0  420.0  440.0  560.0
#                          as.mo("MRSA")  12.0  13  19.0   15.0   15.0   42.0
#                          as.mo("VISA")  20.0  22  31.0   24.0   47.0   54.0
#                          as.mo("VRSA")  22.0  23  35.0   24.0   52.0   56.0
#  neval
#     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. It is clear that accepted taxonomic names are extremely fast, but some variations can take up to 500-1000 times as much time.

To 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 %>%
  # and copy them a thousand times
  rep(1000) %>%
  # then scramble them
  sample()
  
# as the example_isolates has 2,000 rows, we should have 2 million items
length(x)
# [1] 2000000

# and how many unique values do we have?
n_distinct(x)
# [1] 90

# 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) 165 176  223    202 274 318    10

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.202 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("STAAUR"),
                         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  7.83  8.05  9.56   9.43 10.90 12.90    10
#     B 23.50 24.40 30.20  25.50 26.80 74.50    10
#     C  2.00  2.19  2.42   2.41  2.56  3.06    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0024 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 1.69 1.75 1.90   1.86 2.00 2.25    10
#     B 1.69 1.72 1.86   1.79 1.87 2.43    10
#     C 1.67 1.75 1.89   1.81 1.93 2.67    10
#     D 1.64 1.73 1.87   1.87 1.94 2.23    10
#     E 1.65 1.75 1.90   1.83 1.92 2.35    10
#     F 1.70 1.75 1.87   1.85 1.93 2.20    10
#     G 1.73 1.83 1.93   1.93 2.01 2.25    10
#     H 1.65 1.73 1.85   1.77 2.06 2.11    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 16.23 16.62 21.00  17.00 18.51 59.06   100
#    de 19.27 19.96 22.73  20.36 21.08 69.70   100
#    nl 31.40 32.54 38.76  33.16 35.34 82.43   100
#    es 19.36 19.75 23.61  20.26 20.88 67.39   100
#    it 19.24 19.64 21.23  20.00 20.72 62.92   100
#    fr 19.19 19.82 25.12  20.16 21.49 70.00   100
#    pt 19.18 19.64 22.44  20.23 21.47 60.56   100

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