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) # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. print(S.aureus, unit = "ms", signif = 2) # Unit: milliseconds # expr min lq mean median uq max # as.mo("sau") 13.0 14.0 15.0 14.0 16.0 17.0 # as.mo("stau") 220.0 230.0 250.0 260.0 280.0 280.0 # as.mo("STAU") 220.0 240.0 270.0 250.0 290.0 330.0 # as.mo("staaur") 13.0 14.0 28.0 15.0 44.0 62.0 # as.mo("STAAUR") 11.0 14.0 17.0 14.0 15.0 48.0 # as.mo("S. aureus") 30.0 56.0 56.0 61.0 63.0 66.0 # as.mo("S aureus") 28.0 33.0 48.0 38.0 66.0 77.0 # as.mo("Staphylococcus aureus") 1.7 1.9 2.4 2.5 2.6 3.1 # as.mo("Staphylococcus aureus (MRSA)") 860.0 890.0 920.0 910.0 930.0 1000.0 # as.mo("Sthafilokkockus aaureuz") 350.0 370.0 380.0 390.0 390.0 410.0 # as.mo("MRSA") 13.0 15.0 28.0 18.0 42.0 61.0 # as.mo("VISA") 21.0 22.0 31.0 22.0 43.0 52.0 # as.mo("VRSA") 23.0 24.0 39.0 45.0 49.0 55.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 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) 99.2 127 145 143 145 243 10
So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.143 seconds. You only lose time on your unique input values.
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) # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. # Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it. print(run_it, unit = "ms", signif = 3) # Unit: milliseconds # expr min lq mean median uq max neval # A 7.53 8.50 8.77 8.83 9.14 9.89 10 # B 23.00 24.20 32.40 26.30 28.80 60.10 10 # C 1.91 1.98 2.33 2.30 2.66 2.83 10
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0023 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.55 1.74 1.91 1.98 2.06 2.16 10 # B 1.52 1.70 1.95 1.88 2.03 2.62 10 # C 1.61 1.68 6.25 2.04 2.18 44.80 10 # D 1.65 1.71 1.92 1.95 2.02 2.22 10 # E 1.56 1.79 1.92 1.99 2.01 2.14 10 # F 1.62 1.67 1.82 1.78 1.94 2.09 10 # G 1.61 1.70 1.92 1.97 2.01 2.31 10 # H 1.62 1.69 1.83 1.87 1.95 2.01 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.
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 13.92 14.62 19.73 15.07 17.06 71.77 100 # de 16.83 17.63 22.19 18.10 20.41 58.61 100 # nl 28.71 30.05 35.19 31.64 34.09 69.34 100 # es 16.78 17.61 22.57 18.56 20.21 57.98 100 # it 16.61 17.39 21.59 18.32 20.74 56.86 100 # fr 16.48 17.31 21.96 17.84 20.26 55.84 100 # pt 16.66 17.45 24.03 18.26 20.85 67.60 100
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.