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
<<<<<<< HEAD
#                                   expr   min  lq  mean median    uq max neval
#                           as.mo("sau")  12.0  13  18.0   14.0  15.0  48    25
#                          as.mo("stau")  54.0  59  80.0   91.0  96.0  99    25
#                          as.mo("STAU")  53.0  61  77.0   66.0  94.0 100    25
#                        as.mo("staaur")  12.0  13  19.0   14.0  16.0  62    25
#                        as.mo("STAAUR")  12.0  13  16.0   14.0  15.0  48    25
#                     as.mo("S. aureus")  28.0  30  38.0   33.0  35.0  69    25
#                      as.mo("S aureus")  27.0  31  46.0   34.0  65.0  73    25
#         as.mo("Staphylococcus aureus")   3.7   4   6.7    4.3   4.5  36    25
#  as.mo("Staphylococcus aureus (MRSA)") 260.0 270 290.0  280.0 290.0 360    25
#       as.mo("Sthafilokkockus aaureuz") 190.0 210 220.0  210.0 220.0 330    25
#                          as.mo("MRSA")  12.0  13  20.0   14.0  16.0  68    25
#                          as.mo("VISA")  22.0  23  32.0   25.0  27.0  63    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 67 times slower to determine.

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

======= # expr min lq mean median uq max neval # as.mo("sau") 20.0 20.0 25.0 20.0 21 57 25 # as.mo("stau") 94.0 94.0 110.0 95.0 130 130 25 # as.mo("STAU") 94.0 95.0 110.0 130.0 130 130 25 # as.mo("staaur") 20.0 20.0 28.0 20.0 21 58 25 # as.mo("STAAUR") 20.0 20.0 25.0 20.0 21 57 25 # as.mo("S. aureus") 56.0 56.0 72.0 57.0 92 97 25 # as.mo("S aureus") 55.0 56.0 66.0 56.0 75 93 25 # as.mo("Staphylococcus aureus") 5.7 5.8 7.3 5.9 6 41 25 # as.mo("Staphylococcus aureus (MRSA)") 370.0 370.0 390.0 380.0 410 410 25 # as.mo("Sthafilokkockus aaureuz") 260.0 290.0 300.0 290.0 300 330 25 # as.mo("MRSA") 20.0 20.0 23.0 20.0 20 56 25 # as.mo("VISA") 34.0 35.0 48.0 35.0 35 220 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 65 times slower to determine.

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

>>>>>>> 8c9feea087f568fd4abbdb325140d1d628e6856f

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>
<<<<<<< HEAD
# [1] B_ESCHR_COLI B_STRPT_MITS B_STRPT_ANGN B_STPHY_CONS B_ESCHR_COLI
# [6] B_ESCHR_COLI
=======
# [1] B_PROTS_MRBL B_STPHY_CONS B_ESCHR_COLI B_PSDMN_AERG B_ENTRC_FCLS
# [6] B_STPHY_AURS
>>>>>>> 8c9feea087f568fd4abbdb325140d1d628e6856f
  
# 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
<<<<<<< HEAD
#  mo_name(x) 207 225  288    233 370 414    10

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.233 seconds. That is 117 nanoseconds on average. You only lose time on your unique input values.

======= # mo_name(x) 265 269 357 298 471 516 10

So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.298 seconds. That is 149 nanoseconds on average. You only lose time on your unique input values.

>>>>>>> 8c9feea087f568fd4abbdb325140d1d628e6856f

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
<<<<<<< HEAD
#  expr  min    lq  mean median    uq   max neval
#     A 10.3 10.90 11.20  11.10 11.20 12.40    10
#     B 31.3 32.70 38.20  33.90 35.20 79.80    10
#     C  2.5  2.64  2.79   2.78  2.85  3.13    10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0028 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:

======= # expr min lq mean median uq max neval # A 12.30 12.30 12.60 12.50 12.60 14.2 10 # B 61.70 61.90 62.70 62.40 62.50 66.2 10 # C 3.04 3.05 6.92 3.12 3.21 40.9 10

So going from mo_name("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0031 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:

>>>>>>> 8c9feea087f568fd4abbdb325140d1d628e6856f
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
<<<<<<< HEAD
#     A 2.30 2.55 2.68   2.66 2.85 2.94    10
#     B 2.27 2.38 2.59   2.56 2.83 2.88    10
#     C 2.22 2.25 2.51   2.47 2.74 2.87    10
#     D 2.21 2.40 2.68   2.73 2.94 3.08    10
#     E 2.22 2.28 2.46   2.45 2.56 2.81    10
#     F 2.19 2.34 2.52   2.48 2.71 3.04    10
#     G 2.23 2.40 2.52   2.46 2.62 2.88    10
#     H 2.13 2.25 2.42   2.47 2.50 2.77    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.

======= # A 3.02 3.09 3.22 3.10 3.46 3.55 10 # B 3.03 3.04 3.21 3.12 3.42 3.48 10 # C 3.03 3.05 3.08 3.06 3.12 3.16 10 # D 2.98 3.01 3.17 3.09 3.40 3.47 10 # E 3.01 3.06 3.19 3.12 3.43 3.43 10 # F 2.95 2.99 3.11 3.05 3.16 3.46 10 # G 2.94 3.06 3.19 3.13 3.41 3.54 10 # H 2.93 3.01 3.12 3.08 3.19 3.42 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.

>>>>>>> 8c9feea087f568fd4abbdb325140d1d628e6856f

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
<<<<<<< HEAD
#  expr    min    lq  mean median    uq    max neval
#    da 2.1730 2.476 3.956  2.609 2.873 45.180   100
#    de 2.2170 2.497 3.161  2.646 2.855 48.260   100
#    en 0.9509 1.122 1.640  1.183 1.321 38.430   100
#    es 2.1870 2.546 2.763  2.659 2.872  5.676   100
#    fr 1.9880 2.339 2.609  2.456 2.636  5.197   100
#    it 2.2580 2.475 4.081  2.619 2.867 47.080   100
#    nl 2.3120 2.535 2.792  2.664 2.822  8.113   100
#    pt 2.1930 2.417 3.329  2.528 2.783 48.600   100
#    ru 2.0470 2.360 2.596  2.481 2.683  6.030   100
#    sv 2.2030 2.443 3.077  2.545 2.703 43.350   100

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

======= # expr min lq mean median uq max neval # da 3.579 3.689 3.811 3.737 3.894 4.626 100 # de 3.600 3.694 4.205 3.750 3.869 43.820 100 # en 1.686 1.720 1.828 1.766 1.822 2.244 100 # es 3.618 3.688 4.633 3.772 4.083 43.890 100 # fr 3.493 3.602 4.104 3.658 3.785 41.860 100 # it 3.543 3.628 4.152 3.702 3.810 44.040 100 # nl 3.625 3.716 5.024 3.763 3.924 44.220 100 # pt 3.510 3.610 3.742 3.684 3.861 4.096 100 # ru 3.568 3.680 4.534 3.742 3.871 41.170 100 # sv 3.585 3.664 3.833 3.748 4.046 4.987 100

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

>>>>>>> 8c9feea087f568fd4abbdb325140d1d628e6856f