conversion | R Documentation |
The rspec_to_pyspec()
and pyspec_to_rspec()
functions allow to convert
(translate) MS data structures between R and Python. At present the
R Spectra::Spectra()
objects are translated into a list of
matchms Python matchms.Spectrum
objects. For better integration with the reticulate R package also a
r_to_py.Spectra()
method is available.
The mapping of spectra variables (in R) to (Python) spectra metadata can
be configured and defined with the setSpectraVariableMapping()
and
spectraVariableMapping()
. These get and set the global (system wide)
setting and are thus also used by the r_to_py()
method.
See the indivudual function's documentation for more details.
Function to convert R Spectra objects into a Python list of matchms Spectrum
objects using the reticulate
package.
## S4 method for signature 'missing'
spectraVariableMapping(object, ...)
setSpectraVariableMapping(x)
defaultSpectraVariableMapping()
## S3 method for class 'Spectra'
r_to_py(x, convert = FALSE)
rspec_to_pyspec(x, mapping = spectraVariableMapping())
pyspec_to_rspec(x, mapping = spectraVariableMapping())
object |
For |
... |
For |
x |
|
convert |
Boolean; should Python objects be automatically converted to
their R equivalent? Defaults to |
mapping |
named |
For r_to_py.Spectra()
and rspec_to_pyspec()
: Python list of
matchms.Spectrum
objects. For pyspec_to_rspec()
:
Spectra::Spectra()
with the MS data of all matchms.Spectrum
objects
in the submitted list
.
MS data structures can be translated between R and Python using the
rspec_to_pyspec()
and pyspec_to_rspec()
functions, or with the
r_to_py()
method.
rspec_to_pyspec()
translates an R Spectra::Spectra()
object into a
list of matchms.Spectrum
objects. Parameter mapping
allows to specify
which spectra variables from the Spectra
object x
should be converted
in addition to the peaks data (m/z and intensity values). It defaults to
mapping = spectraVariableMapping()
(See the respective help below for
more information on the variable mapping). While being fast, this function
first loads all peaks and spectra data into memory before translating to
Python data structures. A less memory intense operation could be to call
this function in a loop to only load parts of the data at a time into
memory.
pyspec_to_rspec()
translates a single, or a list of matchms.Spectrum
objects to a Spectra::Spectra()
object. Parameter mapping
allows to
speficy the metadata variables that should be translated and mapped in
addition to the peaks data.
r_to_py.Spectra()
is equivalent to rspec_to_pyspec()
. The spectra
variables that should be converted can be configures with
setSpectraVariableMapping()
(see documentation below).
Metadata for MS spectra are represented and stored as spectra variables
in the R Spectra::Spectra()
objects. Also Python MS data structures
store such metadata along with the mass peak data. While spectra metadata
is thus supported by data structures in both programming languages,
different names and naming conventions are used. The
spectraVariableMapping()
and setSpectraVariableMapping()
functions allow
to define how the names of spectra metadata (spectra variables) should be
translated between R and Python. The r_to_py()
and py_to_r()
functions
will used these to name the spectra variables accordingly. Also, only
spectra metadata/variables in spectraVariableMapping()
will be translated.
The initial mapping is based on this
definition in matchms.
defaultSpectraVariableMapping()
: returns the default mapping between
spectra variables and matchms metadata names.
spectraVariableMapping()
: returns the currently defined spectra
variable mapping as a named character vector, with names representing the
names of the spectra variables in R and elements the respective names
of the spectra metadata in Python. Use Spectra::spectraVariables()
on
the Spectra
object that should be converted with r_to_py()
to list
all available spectra variables. r_to_py()
and py_to_r()
for MS data
structures will use this mapping.
setSpectraVariableMapping()
: sets/replaces the currently defined mapping
of spectra variable names to Python metadata names. Setting
setSpectraVariableMapping(character())
will only convert the mass peaks
data (m/z and intensity values) but no spectra metadata.
Michael Witting, Johannes Rainer, Wout Bittremieux
## Import a MGF file as a `Spectra` object
library(MsBackendMgf)
library(SpectriPy)
s <- Spectra(
system.file("extdata", "mgf", "spectra2.mgf", package = "SpectriPy"),
source = MsBackendMgf())
s
#########################
## Conversion R to Python
## A `Spectra` can be translated to a `list` of `matchms.Spectrum` objects
## using either the `r_to_py()` method or the `rspec_to_pyspec()` function:
s_py <- r_to_py(s)
s_py
## The `s_py` can now be used like any other Python variable within the R
## *reticulate* framework. Below we extract the m/z values of the first
## spectrum
s_py[0]$mz
## Extracting that information from the `Spectra` object in R
s[1]$mz
## The `spectraVariableMapping()` defines which spectra variables (metadata)
## should be translated between R and Python:
spectraVariableMapping()
## The names of that character vector represent the names of the spectra
## variables in R, the elements the name of the metadata variable in Python.
## Below we list the available metadata information from the first
## Spectrum in Python
s_py[0]$metadata
## `setSpectraVariableMapping()` allows to replace the default mapping
## of variables. Below we e.g. add a new spectra variable to the `Spectra`
## object.
s$new_col <- 1:4
## To translate that variable to Python we need to include it to the
## `spectraVariableMapping()`. Below we define to translate only the
## precursor m/z and the new spectra variable to Python. Be aware that
## `setSpectraVariableMapping()` **globally** sets the default for any
## spectra variable mapping between R and Python. Thus, any subsequent
## calls mapping calls will use the same mapping. It is suggested to
## eventually *restore* the default mapping again after the call or
## use the `rspec_to_pyspec()` function instead, that allows to configure
## the mapping using a parameter `mapping`.
setSpectraVariableMapping(
c(precursorMz = "precursor_mz", new_col = "new_col"))
s_py <- r_to_py(s)
s_py[0]$metadata
## Restoring the global spectra variable mapping configuration to
## the default mapping:
setSpectraVariableMapping(defaultSpectraVariableMapping())
## As an alternative to the `r_to_py()` we can use the `rspec_to_pyspec()`
## function and provide a custom mapping using the `mapping` parameter:
s_py <- rspec_to_pyspec(
s, mapping = c(precursorMz = "precursor_mz", new_col = "new_col"))
#########################
## Conversion Python to R
## A `list` of `matchms.Spectrum` objects in Python can be translated into
## the corresponding MS data structure in R (i.e. a `Spectra`) object using
## the `pyspec_to_rspec()` function:
res <- pyspec_to_rspec(s_py)
res
## All spectra from Python are thus converted into a single `Spectra` object.
## Or providing a custom variable mapping:
res <- pyspec_to_rspec(
s_py, mapping = c(precursorMz = "precursor_mz", new_col = "new_col"))
res$new_col
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.