
Base MsBackend class providing data caching mechanism
Source:R/MsBackendCached.R
MsBackendCached.RdThe MsBackendCached class is a rudimentary implementation of the
MsBackend providing a simple mechanism to cache spectra data locally. This
class is thought to be used as a base class for other MsBackend
implementations to reuse its caching mechanism and avoid having to
re-implement commonly used methods. This class is thus not thought to be
used directly by a user.
The MsBackendCached caching mechanism allows MsBackend instances
to add or replace spectra variables even if the backend used by them does
not allow to alter values (e.g. if a SQL database is used as a backend). Any
replacement operation with $<- will add the specified values to a local
data.frame within the MsBackendCached class that allows to cache these
values (increasing obviously the memory demand of the object).
Any data accessor functions of the extending MsBackend class (such as $
or msLevel() or spectraData()) should first use callNextMethod() to
call the respective accessor of MsBackendCached that will evaluate if the
requested spectra variable(s) are in the local cache and return these. If
the requested spectra variables are neither in the local cache, nor
listed in the @spectraVariables slot (which defines all spectra variables
that can be requested from the extending MsBackend class) but are core
spectra variables then missing values of the correct data type are returned.
Usage
MsBackendCached()
# S4 method for class 'MsBackendCached'
backendInitialize(
object,
data = data.frame(),
nspectra = 0L,
spectraVariables = character(),
...
)
# S4 method for class 'MsBackendCached'
dataStorage(object)
# S4 method for class 'MsBackendCached,ANY'
extractByIndex(object, i)
# S4 method for class 'MsBackendCached'
length(x)
# S4 method for class 'MsBackendCached'
spectraVariables(object)
# S4 method for class 'MsBackendCached'
spectraData(object, columns = spectraVariables(object))
# S4 method for class 'MsBackendCached'
spectraData(object) <- value
# S4 method for class 'MsBackendCached'
x[i, j, ..., drop = FALSE]
# S4 method for class 'MsBackendCached'
x$name
# S4 method for class 'MsBackendCached'
x$name <- value
# S4 method for class 'MsBackendCached'
selectSpectraVariables(object, spectraVariables = spectraVariables(object))
# S4 method for class 'MsBackendCached'
show(object)
# S4 method for class 'MsBackendCached'
intensity(object)
# S4 method for class 'MsBackendCached'
ionCount(object)
# S4 method for class 'MsBackendCached'
mz(object)Arguments
- object
A
MsBackendCachedobject.- data
For
backendInitialize(): (optional)data.framewith cached values. The number of rows (and their order) has to match the number of spectra.- nspectra
For
backendInitialize():integerwith the number of spectra.- spectraVariables
For
backendInitialize():characterwith the names of the spectra variables that are provided by the extending backend. ForselectSpectraVariables():characterspecifying the spectra variables to keep.- ...
ignored
- i
For
[:integerwith the indices to subset the object.- x
A
MsBackendCachedobject.- columns
For
spectraData():characterwith the names of the spectra variables to retrieve.- value
replacement value for
<-methods. See individual method description or expected data type.- j
For
[: ignored.- drop
For
[: not considered.- name
For
$<-: the name of the spectra variable to set.
Implementation notes
Classes extending the MsBackendCached need to
call the
backendInitialize()method of this class in their ownbackendInitialize()method and set at least the number of spectra with thenspectraparameter and thespectraVariablesthat are available to the (extending) backend class.implement the
spectraData()method that also calls thespectraData()method fromMsBackendCachedto also retrieve cached values (e.g. usingres <- callNextMethod()at the beginning of thespectraDatafunction). ThespectraData,MsBackendCachedmethod will returnNULLif the selected spectra variables were not cached and are not core spectra variables not being provided by the extending backend. Thus, the extending backend can then proceed to retrieve the respective values from its own backend/data storage.implement eventually the
[method that calls in addition the[from theMsBackendCached.
All other methods accessing or setting spectra variables don't need to be
implemented by the extending backend class (the default implementations of
the MsBackendCached will then be used instead; these ensure that cached
values are returned first).
Spectra variables can be modified or added using the $<- method of the
MsBackendCached. Replacing or adding multiple variables using the
spectraData<- is not supported by MsBackendCached. The extending backend
might however implement such a method that internally uses $<- to
add/replace single variables.
The MsBackendCached has the following slots:
nspectra:integer(1)defining the number of spectra of the backend. This variable needs to be set and must match the number of rows oflocalDataand the actual number of spectra in the (extending) backend.localData:data.framewith the cached local data. Any replacement operation with$<-will set/add a column with the respective values.spectraVariables:characterdefining the spectra variables that are provided by the extendingMsBackendclass (e.g. all spectra variables that can be retrieved from the data base or original data files).
Available methods
acquisitionNum(): returns the acquisition number of each spectrum. Returns anintegerof length equal to the number of spectra (withNA_integer_if not available).backendInitialize(): initializes the backend. The method takes parametersdata(data.framewith cached data),nspectra(integerdefining the number of spectra) andspectraVariables(characterwith the spectra variables that are provided by the extending backend.centroided(),centroided<-: gets or sets the centroiding information of the spectra.centroidedreturns alogicalvector of length equal to the number of spectra withTRUEif a spectrum is centroided,FALSEif it is in profile mode andNAif it is undefined. See alsoisCentroidedfor estimating from the spectrum data whether the spectrum is centroided.valueforcentroided<-is either a singlelogicalor alogicalof length equal to the number of spectra inobject.collisionEnergy(),collisionEnergy<-: gets or sets the collision energy for all spectra inobject.collisionEnergy()returns anumericwith length equal to the number of spectra (NA_real_if not present/defined),collisionEnergy<-takes anumericof length equal to the number of spectra inobject.dataOrigin(): gets acharacterof length equal to the number of spectra inobjectwith the data origin of each spectrum. This could e.g. be the mzML file from which the data was read.intensity(): gets the intensity values from the spectra. Returns aIRanges::NumericList()ofnumericvectors (intensity values for each spectrum). The length of thelistis equal to the number ofspectrainobject.ionCount(): returns anumericwith the sum of intensities for each spectrum. If the spectrum is empty (seeisEmpty()),NA_real_is returned.isEmpty(): checks whether a spectrum inobjectis empty (i.e. does not contain any peaks). Returns alogicalvector of length equal number of spectra.isolationWindowLowerMz(),isolationWindowLowerMz<-: gets or sets the lower m/z boundary of the isolation window.isolationWindowTargetMz(),isolationWindowTargetMz<-: gets or sets the target m/z of the isolation window.isolationWindowUpperMz(),isolationWindowUpperMz<-: gets or sets the upper m/z boundary of the isolation window.length(): returns the number of spectra (i.e. the@nspectra).lengths(): gets the number of peaks (m/z-intensity values) per spectrum. Returns anintegervector (length equal to the number of spectra). For empty spectra,0is returned.msLevel(): gets the spectra's MS level. Returns anintegervector (of length equal to the number of spectra) with the MS level for each spectrum (orNA_integer_if not available).mz(): gets the mass-to-charge ratios (m/z) from the spectra. Returns aIRanges::NumericList()or length equal to the number of spectra, each element anumericvector with the m/z values of one spectrum.polarity(),polarity<-: gets or sets the polarity for each spectrum.polarityreturns anintegervector (length equal to the number of spectra), with0and1representing negative and positive polarities, respectively.polarity<-expects an integer vector of length 1 or equal to the number of spectra.precursorCharge(),precursorIntensity(),precursorMz(),precScanNum(),precAcquisitionNum(): get the charge (integer), intensity (numeric), m/z (numeric), scan index (integer) and acquisition number (interger) of the precursor for MS level 2 and above spectra from the object. Returns a vector of length equal to the number of spectra inobject.NAare reported for MS1 spectra of if no precursor information is available.rtime(),rtime<-: gets or sets the retention times for each spectrum (in seconds).rtime()returns anumericvector (length equal to the number of spectra) with the retention time for each spectrum.rtime<-expects a numeric vector with length equal to the number of spectra.scanIndex(): returns anintegervector with the scan index for each spectrum. This represents the relative index of the spectrum within each file. Note that this can be different to theacquisitionNum()of the spectrum which is the index of the spectrum as reported in the mzML file.selectSpectraVariables(): subset the object to specified spectra variables. This will eventually remove spectra variables listed in@spectraVariablesand will also drop columns from the local cache if not amongspectraVariables.smoothed(),smoothed<-: gets or sets whether a spectrum is smoothed.smoothed()returns alogicalvector of length equal to the number of spectra.smoothed<-takes alogicalvector of length 1 or equal to the number of spectra inobject.spectraVariables(): returns the available spectra variables, i.e. the unique set of core spectra variables, cached spectra variables and spectra variables defined in the@spectraVariablesslot (i.e. spectra variables thought to be provided by the extendingMsBackendinstance).spectraData(): returns aDataFramewith cached spectra variablers or initialized core spectra variables. ParameterspectraVariablesallows to specify the variables to retrieve. The function returnsNULLif the requested variables are not cached and are not provided by the extending backend. Note that this method only returns cached spectra variables or core spectra variables not provided by the extending backend. It is the responsibility of the extending backend to add/provide these.[: subsets the cached data. Parameterineeds to be anintegervector.$,$<-: access or set/add a single spectrum variable (column) in the backend.
See also
MsBackend for the documentation of MS backends.