# RMassBank: Non-standard usage

Michael Stravs

#### 19 May 2021

# 1 Introduction

    ##
    ## Attaching package: 'gplots'

    ## The following object is masked from 'package:stats':
    ##
    ##     lowess

This vignette assumes you are familiar with the standard usage of RMassBank, which is documented in

    vignette("RMassBank")

# 2 Skipping recalibration

For instances where recalibration is not wanted, e.g. there is not enough data, or the user wants to use non-recalibrated data, recalibration can be deactivated. To do this, the `recalibrator` entry in the settings must be set to `recalibrate.identity`. This can be done in the settings file directly (preferred):

    recalibrator:
        MS1: recalibrate.identity
        MS2: recalibrate.identity

Or, alternatively, the settings can be adapted directly via R code.

    RmbDefaultSettings()
    rmbo <- getOption("RMassBank")
    rmbo$recalibrator <- list(
            "MS1" = "recalibrate.identity",
            "MS2" = "recalibrate.identity"
        )
    options("RMassBank" = rmbo)

To show the results of using a non-recalibrated workflow, we load a workspace with pre-processed data:

    w <- loadMsmsWorkspace(system.file("results/pH_narcotics_RF.RData",
                    package="RMassBankData"))

    ## Warning in .updateObject.RmbWorkspace.1to2(w, ..., verbose): You are
    ## loading an archive from an old RMassBank version. The aggregate tables are
    ## not loaded from the original object, but recomputed.

    ## Warning in .updateObject.RmbWorkspace.1to2(w, ..., verbose): If you hand-
    ## edited any aggregate table, the information might not be retained in the
    ## new object.

    ## Warning in .updateObject.RmbWorkspace.1to2(w, ..., verbose): You are
    ## loading an archive from an old RMassBank version. The aggregate tables are
    ## not loaded from the original object, but recomputed.

    ## Warning in .updateObject.RmbWorkspace.1to2(w, ..., verbose): If you hand-
    ## edited any aggregate table, the information might not be retained in the
    ## new object.

    ## Warning in .updateObject.RmbWorkspace.1to2(w, ..., verbose): You are
    ## loading an archive from an old RMassBank version. The multiplicity
    ## filtering results are not loaded from the original object, but recomputed.

    ## Warning in .updateObject.RmbWorkspace.1to2(w, ..., verbose): If you hand-
    ## edited any multiplicity filtering results, the information might not be
    ## retained in the new object.

The recalibration curve:

    recal <- makeRecalibration(w@parent, "pH",
                    recalibrateBy = rmbo$recalibrateBy,
                    recalibrateMS1 = rmbo$recalibrateMS1,
                    recalibrator = list(MS1="recalibrate.loess",MS2="recalibrate.loess"),
                    recalibrateMS1Window = 15)

    w@rc <- recal$rc
    w@rc.ms1 <- recal$rc.ms1
    w@parent <- w
    plotRecalibration(w)

Some example peaks to show the effect of recalibration:

    w@spectra[[1]]@parent@mz[30:32]

    ## [1] 133.0346 133.0381 133.9772

    w@spectra[[1]]@children[[1]]@mz[15:17]

    ## [1] 87.00286 88.13592 97.10767

Now reprocess the recalibration step with the above set `recalibration.identity`:

    w <- msmsWorkflow(w, steps=4)

    ## msmsWorkflow: Step 4. Recalibrate m/z values in raw spectra

    ## msmsWorkflow: Done.

The recalibration graph shows that the recalibration “curve” will do no recalibration. To verify, we can show the same peaks as before:

    w@spectra[[1]]@parent@mz[30:32]

    ## [1] 133.0346 133.0381 133.9772

    w@spectra[[1]]@children[[1]]@mz[15:17]

    ## [1] 87.00286 88.13592 97.10767

# 3 Combining multiplicities

Standard multiplicity filtering, which is configurable in the settings, eliminates peaks which are observed only once for a compound. This eliminates spurious formula matches for random noise efficiently. It works well if either many spectra are recorded per compound, or if the same collision energy is present twice (e.g. with different resolutions). It sometimes fails for spectra on the “outer end” of the recorded collision energies when that spectrum is only present once – peaks which appear only in the highest or only in the lowest recorded energy can be erroneously deleted. To prevent this, one can re-run the workflow, read a second set of spectra for every compound (the second most intense) and combine the peak multiplicities of the two analyzed runs. (Mutiplicity filtering can also be switched off completely.)

Example:

    RmbDefaultSettings()
    getOption("RMassBank")$multiplicityFilter

    ## [1] 2

    # to make processing faster, we only use 3 spectra per compound
    rmbo <- getOption("RMassBank")
    rmbo$spectraList <- list(
        list(mode="CID", ces = "35%", ce = "35 % (nominal)", res = 7500),
        list(mode="HCD", ces = "15%", ce = "15 % (nominal)", res = 7500),
        list(mode="HCD", ces = "30%", ce = "30 % (nominal)", res = 7500)
    )
    options(RMassBank = rmbo)

    loadList(system.file("list/NarcoticsDataset.csv",
            package="RMassBankData"))

    ## Loaded compoundlist successfully

    w <- newMsmsWorkspace()
    files <- list.files(system.file("spectra", package="RMassBankData"),
            ".mzML", full.names = TRUE)
    w@files <- files[1:2]

First, the spectra are read and processed until reanalysis (step 7) normally:

    w1 <- msmsWorkflow(w, mode="pH", steps=c(1))

    ## msmsWorkflow: Step 1. Acquire all MSMS spectra from files

    ## msmsWorkflow: Done.

    # Here we artificially cut spectra out to make the workflow run faster for the vignette:
    w1@spectra <- as(lapply(w1@spectra, function(s)
        {
            s@children <- s@children[1:3]
            s
        }),"SimpleList")
    w1 <- msmsWorkflow(w1, mode="pH", steps=c(2:7))

    ## msmsWorkflow: Step 2. First analysis pre recalibration

    ## msmsWorkflow: Step 3. Aggregate all spectra

    ## msmsWorkflow: Step 4. Recalibrate m/z values in raw spectra

    ## msmsWorkflow: Step 5. Reanalyze recalibrated spectra

    ## msmsWorkflow: Step 6. Aggregate recalibrated results

    ## msmsWorkflow: Step 7. Reanalyze fail peaks for N2 + O

    ## msmsWorkflow: Done.

Subsequently, we re-read and process the “confirmation spectra”, i.e. the second-best spectra from the files. Therefore, we will have two sets of spectra for each compound and every real peak should in theory occur twice.

    w2 <- msmsWorkflow(w, mode="pH", steps=c(1), confirmMode = 1)

    ## msmsWorkflow: Step 1. Acquire all MSMS spectra from files

    ## msmsWorkflow: Done.

    # Here we artificially cut spectra out to make the workflow run faster for the vignette:

    w2@spectra <- as(lapply(w2@spectra, function(s)
        {
            s@children <- s@children[1:3]
            s
        }),"SimpleList")
    w2 <- msmsWorkflow(w2, mode="pH", steps=c(2:7))

    ## msmsWorkflow: Step 2. First analysis pre recalibration

    ## msmsWorkflow: Step 3. Aggregate all spectra

    ## msmsWorkflow: Step 4. Recalibrate m/z values in raw spectra

    ## msmsWorkflow: Step 5. Reanalyze recalibrated spectra

    ## msmsWorkflow: Step 6. Aggregate recalibrated results

    ## msmsWorkflow: Step 7. Reanalyze fail peaks for N2 + O

    ## msmsWorkflow: Done.

Finally, we combine the two workspaces for multiplicity filtering, and apply the last step in the workflow (multiplicity filtering).

    wTotal <- combineMultiplicities(c(w1, w2))
    wTotal <- msmsWorkflow(wTotal, steps=8, mode="pH", archivename = "output")

    ## msmsWorkflow: Step 8. Peak multiplicity filtering

    ## msmsWorkflow: Done.

Subsequently, we can proceed as usual with `mbWorkflow`:

    mb <- newMbWorkspace(wTotal)
    # [...] load lists, execute workflow etc.

# 4 Session information

    sessionInfo()

    ## R version 4.1.0 RC (2021-05-10 r80283)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ## Running under: Windows Server x64 (build 17763)
    ##
    ## Matrix products: default
    ##
    ## locale:
    ## [1] LC_COLLATE=C
    ## [2] LC_CTYPE=English_United States.1252
    ## [3] LC_MONETARY=English_United States.1252
    ## [4] LC_NUMERIC=C
    ## [5] LC_TIME=English_United States.1252
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ## [1] gplots_3.1.1         RMassBankData_1.29.0 RMassBank_3.2.0
    ## [4] Rcpp_1.0.6           BiocStyle_2.20.0
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] Biobase_2.52.0        httr_1.4.2            sass_0.4.0
    ##  [4] vsn_3.60.0            jsonlite_1.7.2        foreach_1.5.1
    ##  [7] gtools_3.8.2          bslib_0.2.5.1         assertthat_0.2.1
    ## [10] highr_0.9             BiocManager_1.30.15   affy_1.70.0
    ## [13] stats4_4.1.0          rcdklibs_2.3          yaml_2.2.1
    ## [16] impute_1.66.0         pillar_1.6.1          lattice_0.20-44
    ## [19] glue_1.4.2            limma_3.48.0          fingerprint_3.5.7
    ## [22] digest_0.6.27         colorspace_2.0-1      htmltools_0.5.1.1
    ## [25] preprocessCore_1.54.0 plyr_1.8.6            MALDIquant_1.19.3
    ## [28] XML_3.99-0.6          pkgconfig_2.0.3       magick_2.7.2
    ## [31] bookdown_0.22         zlibbioc_1.38.0       purrr_0.3.4
    ## [34] scales_1.1.1          itertools_0.1-3       affyio_1.62.0
    ## [37] BiocParallel_1.26.0   tibble_3.1.2          generics_0.1.0
    ## [40] IRanges_2.26.0        ggplot2_3.3.3         ellipsis_0.3.2
    ## [43] BiocGenerics_0.38.0   magrittr_2.0.1        crayon_1.4.1
    ## [46] evaluate_0.14         ncdf4_1.17            fansi_0.4.2
    ## [49] doParallel_1.0.16     MASS_7.3-54           mzR_2.26.0
    ## [52] tools_4.1.0           lifecycle_1.0.0       stringr_1.4.0
    ## [55] MSnbase_2.18.0        S4Vectors_0.30.0      munsell_0.5.0
    ## [58] cluster_2.1.2         rcdk_3.5.0            pcaMethods_1.84.0
    ## [61] compiler_4.1.0        jquerylib_0.1.4       mzID_1.30.0
    ## [64] caTools_1.18.2        rlang_0.4.11          grid_4.1.0
    ## [67] iterators_1.0.13      rjson_0.2.20          MsCoreUtils_1.4.0
    ## [70] bitops_1.0-7          rmarkdown_2.8         gtable_0.3.0
    ## [73] codetools_0.2-18      curl_4.3.1            DBI_1.1.1
    ## [76] R6_2.5.0              knitr_1.33            dplyr_1.0.6
    ## [79] utf8_1.2.1            clue_0.3-59           ProtGenerics_1.24.0
    ## [82] KernSmooth_2.23-20    rJava_1.0-4           stringi_1.6.2
    ## [85] parallel_4.1.0        vctrs_0.3.8           png_0.1-7
    ## [88] tidyselect_1.1.1      xfun_0.23