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")  12.0  13  18.0   14.0  15.0  45.0    25
#                          as.mo("stau")  55.0  61  79.0   87.0  91.0  99.0    25
#                          as.mo("STAU")  55.0  58  71.0   61.0  91.0 100.0    25
#                        as.mo("staaur")  11.0  12  20.0   14.0  25.0  46.0    25
#                        as.mo("STAAUR")  12.0  12  13.0   13.0  14.0  15.0    25
#                     as.mo("S. aureus")  27.0  30  43.0   34.0  58.0  62.0    25
#                      as.mo("S aureus")  26.0  28  43.0   32.0  59.0  66.0    25
#         as.mo("Staphylococcus aureus")   3.7   4   4.3    4.1   4.6   5.3    25
#  as.mo("Staphylococcus aureus (MRSA)") 250.0 250 280.0  280.0 290.0 390.0    25
#       as.mo("Sthafilokkockus aaureuz") 190.0 200 210.0  210.0 220.0 230.0    25
#                          as.mo("MRSA")  10.0  12  18.0   14.0  14.0  43.0    25
#                          as.mo("VISA")  19.0  22  29.0   24.0  26.0  55.0    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 200 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_CONS B_STPHY_EPDR B_STPHY_CONS B_STRPT_PNMN B_STRPT_EQNS
# [6] B_STPHY_AURS
  
# 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) 190 204  271    259 346 372    10

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.259 seconds. That is 130 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.16  8.77  9.77  10.10 10.60 10.90    10
#     B 23.20 26.60 32.00  27.50 28.90 76.30    10
#     C  2.01  2.24  2.44   2.44  2.67  2.91    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.57 1.61 1.69   1.65 1.77 1.91    10
#     B 1.54 1.59 1.73   1.64 1.84 2.19    10
#     C 1.56 1.63 1.76   1.75 1.77 2.11    10
#     D 1.58 1.59 1.73   1.65 1.66 2.62    10
#     E 1.55 1.62 1.70   1.66 1.72 2.13    10
#     F 1.58 1.62 1.81   1.72 1.81 2.61    10
#     G 1.54 1.65 1.73   1.69 1.71 2.32    10
#     H 1.58 1.65 1.80   1.74 1.84 2.24    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:

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 19.38 19.92 23.08  20.50 21.40  67.58   100
#    de 30.74 31.50 39.35  32.08 33.43 201.20   100
#    nl 34.89 35.54 43.82  36.15 37.79  86.27   100
#    es 34.62 35.30 42.36  35.99 37.61  90.39   100
#    it 33.42 34.36 41.50  35.13 36.40  84.45   100
#    fr 32.86 33.35 38.69  34.20 35.26  82.96   100
#    pt 30.76 31.42 37.89  32.17 33.33  84.66   100

Currently supported non-English languages are German, Dutch, Spanish, Italian, French and Portuguese.