One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life (CoL) and the List of Prokaryotic names with Standing in Nomenclature (LPSN). We created a function as.mo() that transforms any user input value to a valid microbial ID by using intelligent rules combined with the microbial taxonomy.

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.

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, its 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
  times = 25)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
#                                   expr   min    lq  mean median    uq max neval
#                           as.mo("sau")  10.0  12.0  20.0   12.0  15.0  53    25
#                          as.mo("stau")  49.0  54.0  72.0   58.0  91.0  97    25
#                          as.mo("STAU")  50.0  54.0  71.0   57.0  90.0 110    25
#                        as.mo("staaur")  10.0  12.0  17.0   12.0  14.0  51    25
#                        as.mo("STAAUR")  10.0  12.0  17.0   12.0  14.0  54    25
#                     as.mo("S. aureus")  26.0  27.0  40.0   31.0  56.0  74    25
#                      as.mo("S aureus")  26.0  27.0  39.0   29.0  58.0  68    25
#         as.mo("Staphylococcus aureus")   3.5   3.9   6.6    4.1   4.8  38    25
#  as.mo("Staphylococcus aureus (MRSA)") 230.0 240.0 250.0  240.0 250.0 280    25
#       as.mo("Sthafilokkockus aaureuz") 180.0 190.0 200.0  190.0 200.0 290    25
#                          as.mo("MRSA")  11.0  12.0  19.0   13.0  14.0  50    25
#                          as.mo("VISA")  21.0  22.0  32.0   25.0  50.0  60    25

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 200 milliseconds, this is only 5 input values per second. It is clear that accepted taxonomic names are extremely fast, but some variations are up to 47 times slower to determine.

To improve performance, we implemented two important algorithms to save unnecessary calculations: repetitive results and already precalculated results.

Repetitive results

Repetitive results are values that are present more than once in a vector. Unique values will only be calculated once by as.mo(). So running as.mo(c("E. coli", "E. coli")) will check the value "E. coli" only once.

To prove this, we will use mo_name() for testing - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses as.mo() internally.

# start with the example_isolates data set
x <- example_isolates %>% 
  # take all MO codes from the 'mo' column
  pull(mo) %>%
  # and copy them a thousand times
  rep(1000) %>%
  # then scramble them
  sample()

# what do these values look like? They are of class <mo>:
head(x)
# Class <mo>
# [1] B_STPHY_AURS B_STRPT_EQNS B_KLBSL_PNMN B_STPHY_EPDR B_STPHY_AURS
# [6] B_CRYNB_STRT
  
# as the example_isolates data set 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) 196 209  274    223 364 388    10

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.223 seconds. That is 112 nanoseconds on average. 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 such as mo_name(), it almost doesn’t take any time at all. In other words, if you run mo_name() on a valid taxonomic name, it will return the results immediately (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  8.00  9.16  9.19   9.28  9.46  9.73    10
#     B 23.40 27.20 32.60  27.90 28.10 80.20    10
#     C  1.85  2.25  2.40   2.47  2.62  2.90    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0025 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.76 1.80 2.07   1.95 2.26 2.90    10
#     B 1.69 1.73 1.90   1.81 2.03 2.48    10
#     C 1.71 1.77 1.92   1.91 2.05 2.17    10
#     D 1.68 1.71 1.76   1.76 1.82 1.88    10
#     E 1.68 1.70 1.89   1.89 2.04 2.26    10
#     F 1.67 1.75 1.93   1.89 2.11 2.35    10
#     G 1.70 1.76 1.97   1.88 2.12 2.43    10
#     H 1.67 1.71 1.83   1.75 1.98 2.14    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 contains all phyla of all known bacteria, 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 (compare “en” from the table below with the other languages):

CoNS <- as.mo("CoNS")
CoNS
# Class <mo>
# [1] B_STPHY_CONS

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(da = mo_name(CoNS, language = "da"),
                         de = mo_name(CoNS, language = "de"),
                         en = mo_name(CoNS, language = "en"),
                         es = mo_name(CoNS, language = "es"),
                         fr = mo_name(CoNS, language = "fr"),
                         it = mo_name(CoNS, language = "it"),
                         nl = mo_name(CoNS, language = "nl"),
                         pt = mo_name(CoNS, language = "pt"),
                         ru = mo_name(CoNS, language = "ru"),
                         sv = mo_name(CoNS, language = "sv"),
                         times = 100)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
#  expr    min     lq  mean median    uq    max neval
#    da 1.9470 2.0220 2.190 2.0720 2.358  3.234   100
#    de 1.9560 2.0330 3.649 2.1610 2.401 50.670   100
#    en 0.8937 0.9124 1.022 0.9776 1.120  1.748   100
#    es 1.9710 2.0290 2.216 2.1000 2.391  3.109   100
#    fr 1.8280 1.8960 3.214 1.9420 2.237 71.550   100
#    it 1.9370 1.9970 2.163 2.0610 2.339  3.210   100
#    nl 1.9710 2.0280 2.698 2.1110 2.421 49.340   100
#    pt 1.8920 1.9600 2.119 2.0200 2.261  3.265   100
#    ru 1.8630 1.9420 2.779 2.0270 2.335 66.660   100
#    sv 1.8680 1.9190 4.062 1.9890 2.263 78.870   100

Currently supported languages are Danish, Dutch, English, French, German, Italian, Portuguese, Russian, Spanish and Swedish.