Skip to contents

Introduction

In the previous tutorial Introduction to MS2extract package, we described in a detailed manner the core functions of the package. If you are starting to use the MS2extract package with this tutorial, we encourage you to take a look at the aforementioned tutorial.

Now that you are familiar with the core workflow and functions of this package, we can dive into an automated pipeline with the proposed batch_*() functions.

We can remember the workflow summary where we specify the alternative of core functions for batch_*() alternatives (Figure 1). Three main steps has its own batch alternatives functions, importing mzXML files, extracting MS/MS spectra, and detecting masses. However, exporting the database in a msp file is able to detect if the provided spectra comes from a single mzXML file or it is a batch set of spectra.

Figure 1. Overview of general data processing pipeline to extract MS/MS spectra using the MS2extract package

Figure 1. Overview of general data processing pipeline to extract MS/MS spectra using the MS2extract package

Batch functions

We are familiar with the arguments that the core functions accepts, in this section we describe extra arguments that specific batch_*() functions requeire in order to work.

batch_import_mzxml

knitr::opts_chunk$set(warning = FALSE)
library(MS2extract)

Similarly to import_mzxml() function, we need a compound metadata, with at lest the Compound name, Formula, Ionization mode, and optionally but suggested, the region of interest (min_rt and max_rt).


# Select the csv file name and path
batch_file <- system.file("extdata", "batch_read.csv",
                          package = "MS2extract")
# Read the data frame
batch_data <- read.csv(batch_file)

# File paths for Procyanidin A2 and Rutin
ProcA2_file <- system.file("extdata",
                       "ProcyanidinA2_neg_20eV.mzXML",
                        package = "MS2extract")
Rutin_file <- system.file("extdata",
                       "Rutin_neg_20eV.mzXML",
                        package = "MS2extract")

# Add file path - User should specified the file path -
batch_data$File <- c(ProcA2_file, Rutin_file)

# Checking batch_data data frame
dplyr::glimpse(batch_data)
#> Rows: 2
#> Columns: 6
#> $ Name            <chr> "Procyanidin A2", "Rutin"
#> $ Formula         <chr> "C30H24O12", "C27H30O16"
#> $ Ionization_mode <chr> "Negative", "Negative"
#> $ min_rt          <int> 163, 162
#> $ max_rt          <int> 180, 171
#> $ File            <chr> "/home/runner/work/_temp/Library/MS2extract/extdata/Pr…

The only difference with the core import_mzxml() met_metadata is that you can provide more compounds by adding rows. You can see we are working with two compounds, Procyanidin A2 and Rutin.

Tip: you can specify multiple compounds to the same mzXML if they have different precursor ion m/z.

batch_compounds <- batch_import_mzxml(batch_data)
#> Reading MS2 data from ProcyanidinA2_neg_20eV.mzXML
#> Processing...
#> Reading MS2 data from Rutin_neg_20eV.mzXML
#> Processing...

The raw mzXML data contains:

  • Procyanidin A2: 24249 ions
  • Rutin: 22096 ions
# Checking dimension by compound
purrr::map(batch_compounds, dim)
#> $`Procyanidin A2`
#> [1] 24249     4
#> 
#> $Rutin
#> [1] 22096     4

batch_extract_MS2

Now that we have our data in memory, we can proceed to extract the most intense MS/MS scan. In this case, the batch_extract_MS2() functions do not have extra arguments, although most of the arguments remains fairly similar.

# Use extract batch extract_MS2
batch_extracted <- batch_extract_MS2(batch_compounds,
                                     verbose = TRUE,
                                     out_list = FALSE)

By using verbose = TRUE, we can display the MS/MS TIC plot as well the raw MS/MS spectra.

batch_detect_mass

Now that we have the raw MS/MS spectra, we are going to remove background noise based on intensity. Again, batch_detect_mass() has the same arguments than its core analogue.

batch_mass_detected <- batch_detect_mass(batch_extracted, # Compound list
                                         normalize = TRUE, # Normalize
                                         min_int = 1) # Minimum intensity

purrr::map(batch_mass_detected, dim)
#> $`Procyanidin A2`
#> [1] 38  4
#> 
#> $Rutin
#> [1] 4 4

We see a decrease of number of ions, 38 and 4 ions for Procyanidin A2 and Rutin, respectively.

write_msp

In contrast with the previous batch functions, write_msp() is able to detect if the user is providing a single spectra, or multiple spectra. However, the user needs to provide information about each compound.

# Reading batch metadata
metadata_msp_file <- system.file("extdata",
                                 "batch_msp_metadata.csv",
                                 package = "MS2extract")

metadata_msp <- read.csv(metadata_msp_file)

dplyr::glimpse(metadata_msp)
#> Rows: 2
#> Columns: 8
#> $ NAME            <chr> "Procyanidin A2", "Rutin"
#> $ PRECURSORTYPE   <chr> "[M-H]-", "[M-H]-"
#> $ FORMULA         <chr> "C30H24O12", "C27H30O16"
#> $ INCHIKEY        <chr> "NSEWTSAADLNHNH-LSBOWGMISA-N", "IKGXIBQEEMLURG-NVPNHPE…
#> $ SMILES          <chr> "C1C(C(OC2=C1C(=CC3=C2C4C(C(O3)(OC5=CC(=CC(=C45)O)O)C6…
#> $ IONMODE         <chr> "Negative", "Negative"
#> $ INSTRUMENTTYPE  <chr> "Q-TOF 6545", "Q-TOF 6546"
#> $ COLLISIONENERGY <chr> "20 eV", "20 eV"

After having the clean MS/MS spectra and the compound metadata, we can proceed to export them into a msp files.

write_msp(spec = batch_mass_detected,
            spec_metadata = metadata_msp,
            msp_name = "ProcA2_Rutin_batch.msp")

Session info

sessionInfo()
#> R version 4.2.2 (2022-10-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 22.04.2 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] MS2extract_0.1.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] ProtGenerics_1.30.0   fs_1.6.1              doParallel_1.0.17    
#>  [4] rprojroot_2.0.3       MSnbase_2.24.2        tools_4.2.2          
#>  [7] backports_1.4.1       bslib_0.4.2           utf8_1.2.3           
#> [10] R6_2.5.1              affyio_1.68.0         BiocGenerics_0.44.0  
#> [13] colorspace_2.1-0      withr_2.5.0           tidyselect_1.2.0     
#> [16] compiler_4.2.2        preprocessCore_1.60.2 textshaping_0.3.6    
#> [19] cli_3.6.0             Biobase_2.58.0        desc_1.4.2           
#> [22] labeling_0.4.2        sass_0.4.5            scales_1.2.1         
#> [25] affy_1.76.0           pkgdown_2.0.7         systemfonts_1.0.4    
#> [28] stringr_1.5.0         digest_0.6.31         rmarkdown_2.20       
#> [31] pkgconfig_2.0.3       htmltools_0.5.4       OrgMassSpecR_0.5-3   
#> [34] fastmap_1.1.1         limma_3.54.1          highr_0.10           
#> [37] rlang_1.0.6           impute_1.72.3         farver_2.1.1         
#> [40] jquerylib_0.1.4       generics_0.1.3        jsonlite_1.8.4       
#> [43] mzID_1.36.0           BiocParallel_1.32.5   dplyr_1.1.0          
#> [46] car_3.1-1             magrittr_2.0.3        MALDIquant_1.22      
#> [49] Rcpp_1.0.10           munsell_0.5.0         S4Vectors_0.36.2     
#> [52] fansi_1.0.4           abind_1.4-5           MsCoreUtils_1.10.0   
#> [55] lifecycle_1.0.3       vsn_3.66.0            stringi_1.7.12       
#> [58] yaml_2.3.7            carData_3.0-5         MASS_7.3-58.1        
#> [61] zlibbioc_1.44.0       plyr_1.8.8            grid_4.2.2           
#> [64] parallel_4.2.2        ggrepel_0.9.3         crayon_1.5.2         
#> [67] lattice_0.20-45       cowplot_1.1.1         mzR_2.32.0           
#> [70] knitr_1.42            pillar_1.8.1          ggpubr_0.6.0         
#> [73] ggsignif_0.6.4        codetools_0.2-18      stats4_4.2.2         
#> [76] XML_3.99-0.13         glue_1.6.2            evaluate_0.20        
#> [79] Rdisop_1.58.0         pcaMethods_1.90.0     BiocManager_1.30.20  
#> [82] vctrs_0.5.2           foreach_1.5.2         gtable_0.3.1         
#> [85] purrr_1.0.1           tidyr_1.3.0           clue_0.3-64          
#> [88] cachem_1.0.7          ggplot2_3.4.1         xfun_0.37            
#> [91] broom_1.0.3           rstatix_0.7.2         ragg_1.2.5           
#> [94] ncdf4_1.21            tibble_3.1.8          iterators_1.0.14     
#> [97] memoise_2.0.1         IRanges_2.32.0        cluster_2.1.4