Package: r Biocpkg("MsBackendMsp")
Authors: r packageDescription("MsBackendMsp")[["Author"]]
Compiled: r date()



The r Biocpkg("Spectra") package provides a central infrastructure for the handling of Mass Spectrometry (MS) data. The package supports interchangeable use of different backends to import MS data from a variety of sources (such as mzML files). The r Biocpkg("MsBackendMsp") package adds support for files in NIST MSP format which are frequently used to share spectra libraries and hence enhances small compound annotation workflows using the Spectra and r Biocpkg("MetaboAnnotation") packages [@rainer_modular_2022]. This vignette illustrates the usage of the MsBackendMsp package and how it can be used to import and export data in MSP file format.


To install this package, start R and enter:

if (!requireNamespace("BiocManager", quietly = TRUE))


This will install this package and all eventually missing dependencies.

MSP file format

NIST MSP file format is supported. Some (eventually more stringent) requirements of the format are:

An MSP file can define/provide data for any number of spectra, with no limit on the number of spectra, number of peaks per spectra or number of metadata lines.

Importing MS/MS data from MSP files

The MSP file format allows to store MS/MS spectra (m/z and intensity of mass peaks) along with additional annotations for each spectrum. A single MSP file can thus contain a single or multiple spectra. Below we load the package and define the file name of an MSP file which is distributed with this package.


nist <- system.file("extdata", "spectrum2.msp", package = "MsBackendMsp")

We next import the data into a Spectra object by specifying in the constructor function the backend class which can be used to read the data (in our case a MsBackendMsp).

sp <- Spectra(nist, source = MsBackendMsp())

With that we have now full access to all imported spectra variables that can be listed with the spectraVariables() function.


Besides default spectra variables, such as msLevel, rtime, precursorMz, we also have additional spectra variables such as the name or adduct that are additional data fields from the MSP file.


The NIST file format is however only loosely defined and variety of flavors (or dialects) exist which define their own data fields or use different names for the fields. The MsBackendMsp supports data import/export from all MSP format variations by defining and providing different mappings between MSP data fields and spectra variables. Also user-defined mappings can be used, which makes import from any MSP flavor possible. Pre-defined mappings between MSP data fields and spectra variables (i.e. variables within the Spectra object) are returned by the spectraVariableMapping() function.


The names of this character vector represent the spectra variable names and the values of the vector the MSP data fields. Note that by default, also all data fields for which no mapping is provided are imported (with the field name being used as spectra variable name).

This default mapping works well for MSP files from NIST or from other tools such as MS-DIAL. MassBank of North America MoNA however, uses a slightly different format. Below we read the first 6 lines of a MSP file from MoNA.

mona <- system.file("extdata", "minimona.msp", package = "MsBackendMsp")

The first 6 lines from a NIST MSP file:


MSP files with MoNA flavor use slightly different field names, that are also not all upper case, and also additional fields are defined. While it is possible to import MoNA flavored MSP files using the default variable mapping that was used above, most of the spectra variables would however not mapped correctly to the respective spectra variable in the resulting Spectra object (e.g. the precursor m/z would not be available with the expected spectra variable $precursorMz).

The spectraVariableMapping() provides however also the mapping for MSP files with MoNA flavor.

spectraVariableMapping(MsBackendMsp(), "mona")

Using this mapping in the data import will ensure that the fields get correctly mapped.

sp_mona <- Spectra(mona, source = MsBackendMsp(),
                   mapping = spectraVariableMapping(MsBackendMsp(), "mona"))

Note that in addition to the predefined variable mappings, it is also possible to provide any user-defined variable mapping with the mapping parameter thus enabling to import from MSP files with a highly customized format.

Multiple values for a certain spectrum are represented as duplicated fields in an MSP file. The MsBackendMsp supports also import of such data. MoNA MSP files use for example multiple "Synon" fields to list all synonyms of a compound. Below we extract such values for two spectra within our Spectra object from MoNA.


In addition to importing data from MSP files, MsBackendMsp allows also to export Spectra to files in MSP format. Below we export for example the Spectra with data from MoNA to a temporary file, using the default NIST MSP format.

tmpf <- tempfile()

export(sp_mona, backend = MsBackendMsp(), file = tmpf,
       mapping = spectraVariableMapping(MsBackendMsp()))

Or export the Spectra with data in NIST MSP format to a MSP file with MoNA flavor.

tmpf <- tempfile()

export(sp, backend = MsBackendMsp(), file = tmpf,
       mapping = spectraVariableMapping(MsBackendMsp(), "mona"))

Thus, this could also be used to convert between MSP files with different flavors.

Session information



rformassspectrometry/MsBackendMsp documentation built on April 11, 2024, 7:12 p.m.