5555# ' If for example data was transformed or metadata added or removed in the
5656# ' Python object, it immediately affects the `Spectra`/backend.
5757# '
58+ # ' Any replacement operation uses internally the `spectraData()<-` method,
59+ # ' thus replacing/updating values for individual spectra variables or peaks
60+ # ' variables will first load the current data from Python to R, update or
61+ # ' replace the values and then store the full MS data again to the
62+ # ' referenced Python attribute.
63+ # '
5864# ' @section `MsBackendPy` methods:
5965# '
6066# ' The `MsBackendPy` supports all methods defined by the [Spectra::MsBackend()]
9197# ' instance of `MsBackendPy`. See examples below for different settings
9298# ' and conversion of spectra variables.
9399# '
100+ # ' - `intensity()`, `intensity()<-`: get or replace the intensity values.
101+ # ' `intensity()` returns a `NumericList` of length equal to the number of
102+ # ' spectra with each element being the intensity values of the individual
103+ # ' mass peaks per spectrum. `intensity()<-` takes the same list-like
104+ # ' structure as input parameter. Both the number of spectra and the number of
105+ # ' peaks must match the length of the spectra and the number of existing mass
106+ # ' peaks. To change the number of peaks use the `peaksData()<-` method
107+ # ' instead that replaces the *m/z* and intensity values at the same time.
108+ # ' Calling `intensity()<-` will replace the full MS data (spectra variables
109+ # ' as well as peaks variables) of the associated Python variable.
110+ # '
111+ # ' - `mz()`, `mz()<-`: get or replace the *m/z* values. `mz()` returns a
112+ # ' `NumericList` of length equal to the number of spectra with each element
113+ # ' being the *m/z* values of the individual mass peaks per spectrum.
114+ # ' `mz()<-` takes the same list-like structure as input parameter. Both the
115+ # ' number of spectra and the number of peaks must match the length of the
116+ # ' spectra and the number of existing mass peaks. To change the number of
117+ # ' peaks use the `peaksData()<-` method instead that replaces the *m/z* and
118+ # ' intensity values at the same time.
119+ # ' Calling `mz()<-` will replace the full MS data (spectra variables
120+ # ' as well as peaks variables) of the associated Python variable.
121+ # '
94122# ' - `peaksData()`: extracts the peaks data matrices from the backend. Python
95123# ' code is applied to the data structure in Python to
96124# ' extract the *m/z* and intensity values as a list of (numpy) arrays. These
@@ -628,6 +656,18 @@ setMethod("intensity", "MsBackendPy", function(object) {
628656 NumericList(peaksData(object , " intensity" , drop = TRUE ), compress = FALSE )
629657})
630658
659+ # ' @importMethodsFrom ProtGenerics intensity<-
660+ # '
661+ # ' @rdname MsBackendPy
662+ setReplaceMethod ("intensity ", "MsBackendPy", function(object, value) {
663+ .check_mz_intensity(value , length(object ), lengths(object ))
664+ spd <- spectraData(object , union(names(spectraVariableMapping(object )),
665+ peaksVariables(object )))
666+ spd [[" intensity" ]] <- value
667+ spectraData(object ) <- spd
668+ object
669+ })
670+
631671# ' @importMethodsFrom ProtGenerics isolationWindowLowerMz
632672setMethod ("isolationWindowLowerMz ", "MsBackendPy", function(object) {
633673 spectraData(object , " isolationWindowLowerMz" , drop = TRUE )
@@ -653,6 +693,18 @@ setMethod("mz", "MsBackendPy", function(object) {
653693 NumericList(peaksData(object , " mz" , drop = TRUE ), compress = FALSE )
654694})
655695
696+ # ' @importMethodsFrom ProtGenerics mz<-
697+ # '
698+ # ' @rdname MsBackendPy
699+ setReplaceMethod ("mz ", "MsBackendPy", function(object, value) {
700+ .check_mz_intensity(value , length(object ), lengths(object ))
701+ spd <- spectraData(object , union(names(spectraVariableMapping(object )),
702+ peaksVariables(object )))
703+ spd [[" mz" ]] <- value
704+ spectraData(object ) <- spd
705+ object
706+ })
707+
656708# ' @importMethodsFrom ProtGenerics polarity
657709setMethod ("polarity ", "MsBackendPy", function(object) {
658710 spectraData(object , " polarity" , drop = TRUE )
@@ -877,3 +929,25 @@ reindex <- function(object) {
877929 keep <- c(setdiff(colnames(x ), svs ), svs [keep ])
878930 x [, colnames(x ) %in% keep , drop = FALSE ]
879931}
932+
933+ # ' helper to check input/validity of intensity or mz: has to be a list-like
934+ # ' structure with numeric vectors.
935+ # '
936+ # ' @param x `list` or `NumericList`.
937+ # '
938+ # ' @param l `integer(1)` with the number of spectra/expected elements in `x`.
939+ # '
940+ # ' @param ls `integer` with the number of peaks, i.e., the lengths of the
941+ # ' numeric vectors.
942+ # '
943+ # ' @noRd
944+ .check_mz_intensity <- function (x , l = length(x ), ls = lengths(x )) {
945+ if (! (is.list(x ) | inherits(x , " SimpleList" )))
946+ stop(" 'value' has to be a list-like data structure." )
947+ if (length(x ) != l )
948+ stop(" length of 'value' has to match the number of spectra" )
949+ if (! all(lengths(x ) == ls ))
950+ stop(" lengths(value) has to match the number of peaks per spectrum" )
951+ if (! all(vapply1l(x , is.numeric )))
952+ stop(" elements of 'value' are expected to be numeric vectors." )
953+ }
0 commit comments