/
ChromBackend.R
867 lines (806 loc) · 29.1 KB
/
ChromBackend.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
#' @title Chromatographic MS Data Backends
#'
#' @name ChromBackend
#'
#' @aliases ChromBackend-class
#'
#' @description
#'
#' `ChromBackend` is a virtual class that defines what different backends need
#' to provide to be used by the `Chromatograms` package and classes.
#'
#' The backend should provide access to the chromatographic data which mainly
#' consists of (paired) intensity and retention time values. Additional
#' chromatographic metadata such as MS level and precursor and product m/z
#' should also be provided.
#'
#' Through their implementation different backends can be either optimized for
#' minimal memory requirements or performance. Each backend needs to implemet
#' data access methods listed in section *Backend functions:* below.
#'
#' And example implementation and more details and descriptions are provided
#' in the *Creating new `ChromBackend` classes for Chromatograms* vignette.
#'
#' @section Core chromatogram variables:
#'
#' The *core* chromatogram variables are variables (metadata) that can/should
#' be provided by a backend. For each of these variables a value needs to be
#' returned, if none is defined, a missing value (of the correct data type)
#' should be returned. The names of the core variables are returned with the
#' `chromVariables` function.
#'
#' For each core chromatogram variable a dedicated access method exists.
#'
#' The `coreChromVariables()` function returns the core chromatogram variables
#' along with their expected (defined) data type.
#'
#' The core chromatogram variables (in alphabetical order) are:
#'
#' - `chromIndex`: an `integer` with the index of the chromatogram in the
#' original source file (e.g. *mzML* file).
#' - `collisionEnergy`: for SRM data, `numeric` with the collision energy of
#' the precursor.
#' - `dataOrigin`: optional `character` with the origin of a chromatogram.
#' - `dataStorage`: `character` defining where the data is (currently) stored.
#' - `intensity`: `NumericList` with the intensity values of each chromatogram.
#' - `msLevel`: `integer` defining the MS level of the data.
#' - `mz`: optional `numeric` with the (target) m/z value for the
#' chromatographic data.
#' - `mzMin`: optional `numeric` with the lower m/z value of the m/z range in
#' case the data (e.g. an extracted ion chromatogram EIC) was extracted from
#' a `Spectra` object.
#' - `mzMax`: optional `numeric` with the upper m/z value of the m/z range.
#' - `precursorMz`: for SRM data, `numeric` with the target m/z of the
#' precursor (parent).
#' - `precursorMzMin`: for SRM data, optional `numeric` with the lower m/z of
#' the precursor's isolation window.
#' - `precursorMzMax`: for SRM data, optional `numeric` with the upper m/z of
#' the precursor's isolation window.
#' - `productMz` for SRM data, `numeric` with the target m/z of the
#' product ion.
#' - `productMzMin`: for SRM data, optional `numeric` with the lower m/z of
#' the product's isolation window.
#' - `productMzMax`: for SRM data, optional `numeric` with the upper m/z of
#' the product's isolation window.
#' - `rtime`: `NumericList` with the retention times of each chromatogram.
#' retention times are expected to be increasingly sorted.
#'
#' @param chromVariables For `selectChromVariables()`: `character` with the
#' names of the chromatogram variables to which the backend should be
#' subsetted.
#'
#' @param columns For `chromData()` accessor: optional `character` with column
#' names (chromatogram variables) that should be included in the
#' returned `DataFrame`. By default, all columns are returned.
#'
#' @param dataOrigin For `filterDataOrigin()`: `character` to define which
#' chromatograms to keep.
#'
#' @param dataStorage For `filterDataStorage()`: `character` to define which
#' chromatograms to keep.
#'
#' @param drop For `[`: not considered.
#'
#' @param f `factor` defining the grouping to split `x`. See [split()].
#'
#' @param i For `[`: `integer`, `logical` or `character` to subset the object.
#'
#' @param j For `[`: ignored.
#'
#' @param msLevel `integer` defining the MS level of the chromatograms to which
#' the function should be applied. For `filterMsLevel()`: the MS level to
#' which `object` should be subsetted.
#'
#' @param mz For `filterMzValues()`: `numeric` with the m/z values of
#' chromatograms to keep. All chromatograms with their `mz` chromatogram
#' variable matching any of the values provided with this parameter are
#' retained. Parameters `ppm` and `tolerance` allow relaxed matching.
#' For `filterMzRange()`: `numeric(2)` defining the lower and upper
#' boundary of the m/z range. Chromatograms with their `mz` chromatogram
#' variable within this range are retained.
#'
#' @param name For `$` and `$<-`: the name of the chromatogram variable to
#' return or set.
#'
#' @param object Object extending `ChromBackend`.
#'
#' @param ppm For `filterMzValues()`: m/z-relative acceptable difference (in
#' parts-per-million) for m/z values to be considered *matching*.
#'
#' @param tolerance For `filterMzValues()`: largest acceptable absolute
#' difference in m/z values to consider them *matching*.
#'
#' @param value replacement value for `<-` methods. See individual
#' method description or expected data type.
#'
#' @param x Object extending `ChromBackend`.
#'
#' @param ... Additional arguments.
#'
#' @section Backend functions:
#'
#' New backend classes **must** extend the base `ChromBackend` class and
#' implement the following mandatory methods:
#'
#' - `backendInitialize()`: initialises the backend. This method is
#' supposed to be called right after creating an instance of the
#' backend class and should prepare the backend.
#' Parameters can be defined freely for each backend, depending on what is
#' needed to initialize the backend. It is however suggested to also support
#' a parameter `data` that can be used to submit the full data as a
#' `DataFrame` to the backend. This would allow the backend to be also
#' usable for the [setBackend()] function from `Chromatograms`.
#' This method has to ensure to set the spectra variable `dataStorage`
#' correctly.
#'
#' - `chromData()`, `chromData<-`: gets or sets general chromatogram metadata
#' (annotation). `chromData()` returns a `DataFrame`, `chromData<-` expects
#' a `DataFrame` with the same number of rows as there are chromatograms in
#' `object`. Note that `chromData()` has to return the **full** data, i.e.
#' all core chromatogram variables and also the intensity and retention
#' time values (as a `list` or `SimpleList` in columns `"intensity"` and
#' `"rtime"`. Read-only backends might not need to implement the
#' replacement method `chromData<-` (unless some internal caching mechanism
#' could be used).
#'
#' - `peaksData()`: returns a `list` of two-dimensional arrays (`matrix` or
#' `data.frame`) with the data (e.g. retention time - intensity pairs) from
#' each chromatogram. The length of the `list` is equal to the number of
#' chromatograms in `object`. For an empty chromatogram a `matrix`
#' with 0 rows and two columns (named `"rtime"` and `"intensity"`) has to be
#' returned. The optional parameter `columns`, if supported by the backend`
#' allows to define which peak variables should be returned in each array.
#' As default (minimum) columns `"rtime"` and `"intensity"` have to be
#' provided.
#'
#' - `peaksData<-` replaces the peak data (retention time and intensity values)
#' of the backend. This method expects a `list` of two-dimensional arrays
#' (`matrix` or `data.frame`) with columns representing the peak variables.
#' All existing peaks data are expected to be replaced with these new values.
#' The length of the `list` has to match the number of spectra of `object`.
#' Note that only writeable backends need to support this method.
#'
#' - `[`: subset the backend. Only subsetting by element (*row*/`i`) is
#' allowed.
#'
#' - `$`, `$<-`: access or set/add a single chromatogram variable (column) in
#' the backend.
#'
#' - `selectChromVariables()`: reduce `object` retaining only specified
#' chromatogram variables.
#'
#' - `backendMerge()`: merges (combines) `ChromBackend` objects into a single
#' instance. All objects to be merged have to be of the same type.
#'
#' Additional methods that might be implemented, but for which default
#' implementations are already present are:
#'
#' - `backendParallelFactor()`: returns a `factor` defining an optimal
#' (preferred) way how the backend can be split for parallel processing
#' used for all *peak* data accessor or data manipulation functions.
#' The default implementation returns a factor of length 0 (`factor()`)
#' providing thus no default splitting.
#'
#' - `chromVariables()`: returns a `character` vector with the
#' available chromatogram variables (columns, fields or attributes)
#' available in `object`. This should return **all** variables which
#' are present in `object`, also `"intensity"` and `"rtime"` (which are by
#' default not returned by the `chromVariables,Chromatograms` method).
#'
#' - `chromIndex()`: returns an `integer` vector with the index of the
#' chromatograms in the original source file.
#'
#' - `collisionEnergy()`, `collisionEnergy<-`: gets or sets the collision energy
#' for the precursor (for SRM data). `collisionEnergy()` returns a `numeric`
#' of length equal to the number of chromatograms in `object`.
#'
#' - `dataOrigin()`, `dataOrigin<-`: gets or sets the *data origin* variable.
#' `dataOrigin()` returns a `character` of length equal to the number of
#' chromatograms, `dataOrigin<-` expects a `character` of length equal
#' `length(object)`.
#'
#' - `dataStorage()`, `dataStorage<-`: gets or sets the *data storage* variable.
#' `dataStorage()` returns a `character` of length equal to the number of
#' chromatograms in `object`, `dataStorage<- ` expects a `character` of
#' length equal `length(object)`. Note that missing values (`NA_character_`)
#' are not supported for `dataStorage()`.
#'
#' - `intensity()`: gets the intensity values from the chromatograms. Returns
#' a [NumericList()] of `numeric` vectors (intensity values for each
#' chromatogram). The length of the list is equal to the number of
#' chromatograms in `object`.
#'
#' - `intensity<-`: replaces the intensity values. `value` has to be a `list`
#' (or [NumericList()]) of length equal to the number of chromatograms and
#' the number of values within each list element identical to the number of
#' data pairs in each chromatogram. Note that just writeable backends
#' need to support this method.
#'
#' - `isReadOnly()`: returns a `logical(1)` whether the backend is *read
#' only* or does allow also to write/update data.
#'
#' - `isEmpty()`: returns a `logical` of length equal to the number of
#' chromatograms with `TRUE` for chromatograms without any data pairs.
#'
#' - `length()`: returns the number of chromatograms in the object.
#'
#' - `lengths()`: returns the number of data pairs (retention time and intensity
#' values) per chromatogram.
#'
#' - `msLevel()`: gets the chromatogram's MS level. Returns an `integer`
#' vector (of length equal to the number of chromatograms) with the MS
#' level for each chromatogram (or `NA_integer_` if not available).
#'
#' - `mz()`,`mz<-`: gets or sets the m/z value of the chromatograms. `mz()`
#' returns a `numeric` of length equal to the number of chromatograms in `
#' object`, `mz<-` expects a `numeric` of length `length(object)`.
#'
#' - `mzMax()`,`mzMax<-`: gets or sets the upper m/z of the mass-to-charge
#' range from which a chromatogram contains signal (e.g. if the chromatogram
#' was extracted from MS data in spectra format and a m/z range was provided).
#' `mzMax()` returns a `numeric` of length equal to the number of
#' chromatograms in `object`, `mzMax<-` expects a `numeric` of length equal
#' to the number of chromatograms in `object`.
#'
#' - `mzMin()`,`mzMin<-`: gets or sets the lower m/z of the mass-to-charge range
#' from which a chromatogram contains signal (e.g. if the chromatogram
#' was extracted from MS data in spectra format and a m/z range was provided).
#' `mzMin()` returns a `numeric` of length equal to the number of
#' chromatograms in `object`, `mzMin<-` expects a `numeric` of length equal
#' to the number of chromatograms in `object`.
#'
#' - `peaksVariables()`: lists the available data variables for the
#' chromatograms. Default peak variables are `"rtime"` and `"intensity"`
#' (which all backends need to support and provide), but some backends
#' might provide additional variables.
#' Variables listed by this function are expected to be returned (if
#' requested) by the `peaksData()` function.
#'
#' - `precursorMz()`,`precursorMz<-`: gets or sets the (target) m/z of the
#' precursor (for SRM data). `precursorMz()` returns a `numeric` of length
#' equal to the number of chromatograms in `object`. `precursorMz<-` expects
#' a `numeric` of length equal to the number of chromatograms.
#'
#' - `precursorMzMin()`,`precursorMzMax()`,`productMzMin()`, `productMzMax()`:
#' gets the lower and upper margin for the precursor or product isolation
#' windows. These functions might return the value of `productMz()` if the
#' respective minimal or maximal m/z values are not defined in `object`.
#'
#' - `productMz()`,`productMz<-`: gets or sets the (target) m/z of the
#' product (for SRM data). `productMz()` returns a `numeric` of length
#' equal to the number of chromatograms in `object`. `productMz<-` expects
#' a `numeric` of length equal to the number of chromatograms.
#'
#' - `rtime()`: gets the retention times from the chromatograms. returns a
#' [NumericList()] of `numeric` vectors (retention times for each
#' chromatogram). The length of the returned list is equal to the number of
#' chromatograms in `object`.
#'
#' - `rtime<-`: replaces the retention times. `value` has to be a `list` (or
#' [NumericList()]) of length equal to the number of chromatograms and the
#' number of values within each list element identical to the number of
#' data pairs in each chromatogram. Note that just writeable backends support
#' this method.
#'
#' - `split()`: splits the backend into a `list` of backends (depending on
#' parameter `f`). The default method for `ChromBackend` uses
#' [split.default()], thus backends extending `ChromBackend` don't
#' necessarily need to implement this method.
#'
#'
#' Filter methods:
#'
#' - `filterDataOrigin()`: filters the object retaining chromatograms matching
#' any of the provided `dataOrigin`. Parameter `dataOrigin` has to be of
#' type `character` and needs to match exactly the data origin value of the
#' chromatograms to subset.
#' `filterDataOrigin()` should return the data ordered by the provided
#' `dataOrigin` parameter, i.e. if `dataOrigin = c("2", "1")` was provided,
#' the chromatograms in the resulting object should be ordered accordingly
#' (first chromatogram from data origin `"2"` and then from `"1"`).
#'
#' - `filterDataStorage()`: filters the object retaining chromatograms matching
#' any of the provided `dataStorage`. Parameter `dataStorage` has to be
#' of type `character` and needs to match exactly the data storage value of
#' the chromatograms to subset.
#' `filterDataStorage()` should return the data ordered by the provided
#' `dataStorage` parameter, i.e. if `dataStorage = c("2", "1")` was
#' provided, the chromatograms in the resulting object should be ordered
#' accordingly (first chromatogram from data storage `"2"` and then from
#' `"1"`).
#'
#' - `filterMsLevel()`: retains chromatograms of MS level `msLevel()`.
#'
#' - `filterMzRange()`: retains chromatograms with their m/z within the
#' provided m/z range.
#'
#' - `filterMzValues()`: retains chromatograms with their m/z matching any of
#' the provided m/z values (given the provided acceptable differences defined
#' by parameters `tolerance` and `ppm`.
#'
#'
#' @section Implementation notes:
#'
#' Backends extending `ChromBackend` **must** implement all of its methods
#' (listed above). A guide to create new backend classes is provided as a
#' dedicated vignette. Additional information and an example for a backend
#' implementation is provided in the respective vignette.
#'
#' @author Johannes Rainer
#'
#' @md
#'
#' @importFrom IRanges NumericList
#'
#' @exportClass ChromBackend
#'
#' @examples
#'
#' ## Create a simple backend implementation
#' ChromBackendDummy <- setClass("ChromBackendDummy",
#' contains = "ChromBackend")
NULL
setClass("ChromBackend",
contains = "VIRTUAL",
slots = c(
version = "character"),
prototype = prototype(readonly = FALSE, version = "0.1"))
#' @importFrom methods .valueClassTest is new validObject
#'
#' @noRd
setValidity("ChromBackend", function(object) {
msg <- .valid_chrom_backend_data_storage(dataStorage(object))
if (is.null(msg)) TRUE
else msg
})
#' @exportMethod [
#'
#' @rdname ChromBackend
setMethod("[", "ChromBackend", function(x, i, j, ..., drop = FALSE) {
stop("Not implemented for ", class(x), ".")
})
#' @exportMethod $
#'
#' @rdname ChromBackend
setMethod("$", "ChromBackend", function(x, name) {
stop("Not implemented for ", class(x), ".")
})
#' @exportMethod $<-
#'
#' @rdname ChromBackend
setReplaceMethod("$", "ChromBackend", function(x, name, value) {
stop("Not implemented for ", class(x), ".")
})
#' @exportMethod backendInitialize
#'
#' @importFrom methods .valueClassTest validObject
#'
#' @importMethodsFrom ProtGenerics backendInitialize
#'
#' @rdname ChromBackend
setMethod("backendInitialize", signature = "ChromBackend",
definition = function(object, ...) {
validObject(object)
object
})
#' @exportMethod chromData
#'
#' @rdname ChromBackend
setMethod("chromData", "ChromBackend",
function(object, columns = chromVariables(object)) {
stop("Not implemented for ", class(object), ".")
})
#' @exportMethod chromData<-
#'
#' @rdname ChromBackend
setReplaceMethod("chromData", "ChromBackend", function(object, value) {
stop("Not implemented for ", class(object), ".")
})
#' @exportMethod selectChromVariables
#'
#' @rdname ChromBackend
setMethod("selectChromVariables", "ChromBackend",
function(object, chromVariables = chromVariables(object)) {
stop("Not implemented for ", class(object), ".")
})
#' @exportMethod peaksData
#'
#' @importMethodsFrom ProtGenerics peaksData
#'
#' @rdname ChromBackend
setMethod(
"peaksData", "ChromBackend",
function(object, columns = c("rtime", "intensity")) {
stop("Not implemented for ", class(object), ".")
})
#' @exportMethod peaksData<-
#'
#' @importMethodsFrom ProtGenerics peaksData<-
#'
#' @rdname ChromBackend
setReplaceMethod("peaksData", "ChromBackend", function(object, value) {
stop("Not implemented for ", class(object), ".")
})
#' @exportMethod backendMerge
#'
#' @importMethodsFrom ProtGenerics backendMerge
#'
#' @rdname ChromBackend
setMethod("backendMerge", "ChromBackend", function(object, ...) {
stop("Not implemented for ", class(object), ".")
})
################################################################################
## Methods with default implementations below.
#' @rdname ChromBackend
#'
#' @importMethodsFrom ProtGenerics backendParallelFactor
#'
#' @exportMethod backendParallelFactor
setMethod("backendParallelFactor", "ChromBackend", function(object, ...) {
factor()
})
#' @rdname ChromBackend
#'
#' @importMethodsFrom ProtGenerics backendMerge
setMethod("backendMerge", "list", function(object, ...) {
backendMerge(object[[1]], object[-1])
})
#' @exportMethod chromVariables
#'
#' @rdname ChromBackend
setMethod("chromVariables", "ChromBackend", function(object) {
colnames(chromData(object))
})
#' @exportMethod chromIndex
#'
#' @rdname ChromBackend
setMethod("chromIndex", "ChromBackend",
function(object, columns = chromVariables(object)) {
chromData(object, columns = "chromIndex")[, 1L]
})
#' @exportMethod collisionEnergy
#'
#' @importMethodsFrom ProtGenerics collisionEnergy
#'
#' @rdname ChromBackend
setMethod("collisionEnergy", "ChromBackend", function(object) {
chromData(object, columns = "collisionEnergy")[, 1L]
})
#' @exportMethod collisionEnergy<-
#'
#' @importMethodsFrom ProtGenerics collisionEnergy<-
#'
#' @rdname ChromBackend
setReplaceMethod("collisionEnergy", "ChromBackend", function(object, value) {
object$collisionEnergy <- value
object
})
#' @exportMethod dataOrigin
#'
#' @importMethodsFrom ProtGenerics dataOrigin
#'
#' @rdname ChromBackend
setMethod("dataOrigin", "ChromBackend", function(object) {
chromData(object, columns = "dataOrigin")[, 1L]
})
#' @exportMethod dataOrigin<-
#'
#' @importMethodsFrom ProtGenerics dataOrigin<-
#'
#' @rdname ChromBackend
setReplaceMethod("dataOrigin", "ChromBackend", function(object, value) {
object$dataOrigin <- value
object
})
#' @exportMethod dataStorage
#'
#' @importMethodsFrom ProtGenerics dataStorage
#'
#' @rdname ChromBackend
setMethod("dataStorage", "ChromBackend", function(object) {
chromData(object, columns = "dataStorage")[, 1L]
})
#' @exportMethod dataStorage<-
#'
#' @importMethodsFrom ProtGenerics dataStorage<-
#'
#' @rdname ChromBackend
setReplaceMethod("dataStorage", "ChromBackend", function(object, value) {
object$dataStorage <- value
object
})
#' @exportMethod intensity
#'
#' @importMethodsFrom ProtGenerics intensity
#'
#' @rdname ChromBackend
setMethod("intensity", "ChromBackend", function(object) {
chromData(object, columns = "intensity")[, 1L]
})
#' @exportMethod intensity<-
#'
#' @importMethodsFrom ProtGenerics intensity<-
#'
#' @rdname ChromBackend
setReplaceMethod("intensity", "ChromBackend", function(object, value) {
object$intensity <- value
object
})
#' @exportMethod isEmpty
#'
#' @rdname ChromBackend
#'
#' @importMethodsFrom S4Vectors isEmpty
setMethod("isEmpty", "ChromBackend", function(x) {
lengths(x) == 0L
})
#' @exportMethod isReadOnly
#'
#' @importMethodsFrom ProtGenerics isReadOnly
#'
#' @rdname ChromBackend
setMethod("isReadOnly", "ChromBackend", function(object) {
TRUE
})
#' @exportMethod length
#'
#' @rdname ChromBackend
setMethod("length", "ChromBackend", function(x) {
nrow(chromData(x, columns = "dataStorage"))
})
#' @exportMethod lengths
#'
#' @rdname ChromBackend
setMethod("lengths", "ChromBackend", function(x) {
lengths(intensity(x))
})
#' @exportMethod msLevel
#'
#' @importMethodsFrom ProtGenerics msLevel
#'
#' @rdname ChromBackend
setMethod("msLevel", "ChromBackend", function(object) {
chromData(object, columns = "msLevel")[, 1L]
})
#' @exportMethod msLevel<-
#'
#' @importMethodsFrom ProtGenerics msLevel<-
#'
#' @rdname ChromBackend
setReplaceMethod("msLevel", "ChromBackend", function(object, value) {
object$msLevel <- value
object
})
#' @exportMethod mz
#'
#' @importMethodsFrom ProtGenerics mz
#'
#' @rdname ChromBackend
setMethod("mz", "ChromBackend", function(object) {
chromData(object, columns = "mz")[, 1L]
})
#' @exportMethod mz<-
#'
#' @importMethodsFrom ProtGenerics mz<-
#'
#' @rdname ChromBackend
setReplaceMethod("mz", "ChromBackend", function(object, value) {
object$mz <- value
object
})
#' @exportMethod mzMax
#'
#' @rdname ChromBackend
setMethod("mzMax", "ChromBackend", function(object) {
chromData(object, columns = "mzMax")[, 1L]
})
#' @exportMethod mzMax<-
#'
#' @rdname ChromBackend
setReplaceMethod("mzMax", "ChromBackend", function(object, value) {
object$mzMax <- value
object
})
#' @exportMethod mzMin
#'
#' @rdname ChromBackend
setMethod("mzMin", "ChromBackend", function(object) {
chromData(object, columns = "mzMin")[, 1L]
})
#' @exportMethod mzMin<-
#'
#' @rdname ChromBackend
setReplaceMethod("mzMin", "ChromBackend", function(object, value) {
object$mzMin <- value
object
})
#' @exportMethod peaksData
#'
#' @importMethodsFrom ProtGenerics peaksVariables
#'
#' @rdname ChromBackend
setMethod(
"peaksVariables", "ChromBackend", function(object) {
c("rtime", "intensity")
})
#' @exportMethod precursorMz
#'
#' @importMethodsFrom ProtGenerics precursorMz
#'
#' @rdname ChromBackend
setMethod("precursorMz", "ChromBackend", function(object) {
chromData(object, columns = "precursorMz")[, 1L]
})
#' @exportMethod precursorMz<-
#'
#' @importMethodsFrom ProtGenerics precursorMz<-
#'
#' @rdname ChromBackend
setReplaceMethod("precursorMz", "ChromBackend", function(object, value) {
object$precursorMz <- value
object
})
#' @exportMethod precursorMzMax
#'
#' @rdname ChromBackend
setMethod("precursorMzMax", "ChromBackend", function(object) {
chromData(object, columns = "precursorMzMax")[, 1L]
})
#' @exportMethod precursorMzMax<-
#'
#' @rdname ChromBackend
setReplaceMethod("precursorMzMax", "ChromBackend", function(object, value) {
object$precursorMzMax <- value
object
})
#' @exportMethod precursorMzMin
#'
#' @rdname ChromBackend
setMethod("precursorMzMin", "ChromBackend", function(object) {
chromData(object, columns = "precursorMzMin")[, 1L]
})
#' @exportMethod precursorMzMin<-
#'
#' @rdname ChromBackend
setReplaceMethod("precursorMzMin", "ChromBackend", function(object, value) {
object$precursorMzMin <- value
object
})
#' @exportMethod productMz
#'
#' @importMethodsFrom ProtGenerics productMz
#'
#' @rdname ChromBackend
setMethod("productMz", "ChromBackend", function(object) {
chromData(object, columns = "productMz")[, 1L]
})
#' @exportMethod productMz<-
#'
#' @importMethodsFrom ProtGenerics productMz<-
#'
#' @rdname ChromBackend
setReplaceMethod("productMz", "ChromBackend", function(object, value) {
object$productMz <- value
object
})
#' @exportMethod productMzMax
#'
#' @rdname ChromBackend
setMethod("productMzMax", "ChromBackend", function(object) {
chromData(object, columns = "productMzMax")[, 1L]
})
#' @exportMethod productMzMax<-
#'
#' @rdname ChromBackend
setReplaceMethod("productMzMax", "ChromBackend", function(object, value) {
object$productMzMax <- value
object
})
#' @exportMethod productMzMin
#'
#' @rdname ChromBackend
setMethod("productMzMin", "ChromBackend", function(object) {
chromData(object, columns = "productMz")[, 1L]
})
#' @exportMethod productMzMin<-
#'
#' @rdname ChromBackend
setReplaceMethod("productMzMin", "ChromBackend", function(object, value) {
object$productMzMin <- value
object
})
#' @exportMethod rtime
#'
#' @importMethodsFrom ProtGenerics rtime
#'
#' @rdname ChromBackend
setMethod("rtime", "ChromBackend", function(object) {
chromData(object, columns = "rtime")[, 1L]
})
#' @exportMethod rtime<-
#'
#' @importMethodsFrom ProtGenerics rtime<-
#'
#' @rdname ChromBackend
setReplaceMethod("rtime", "ChromBackend", function(object, value) {
object$rtime <- value
object
})
#' @exportMethod split
#'
#' @importMethodsFrom S4Vectors split
#'
#' @rdname ChromBackend
setMethod("split", "ChromBackend", function(x, f, drop = FALSE, ...) {
split.default(x, f, drop = drop, ...)
})
################################################################################
## Filter functions TODO ADD MORE!
#' @exportMethod filterDataOrigin
#'
#' @importMethodsFrom ProtGenerics filterDataOrigin
#'
#' @rdname ChromBackend
setMethod("filterDataOrigin", "ChromBackend",
function(object, dataOrigin = character(), ...) {
if (length(dataOrigin)) {
object <- object[dataOrigin(object) %in% dataOrigin]
if (is.unsorted(dataOrigin))
object[order(match(dataOrigin(object), dataOrigin))]
else object
} else object
})
#' @exportMethod filterDataStorage
#'
#' @importMethodsFrom ProtGenerics filterDataStorage
#'
#' @rdname ChromBackend
setMethod("filterDataStorage", "ChromBackend",
function(object, dataStorage = character()) {
if (length(dataStorage)) {
object <- object[dataStorage(object) %in% dataStorage]
if (is.unsorted(dataStorage))
object[order(match(dataStorage(object), dataStorage))]
else object
} else object
})
#' @exportMethod filterMsLevel
#'
#' @importMethodsFrom ProtGenerics filterMsLevel
#'
#' @rdname ChromBackend
setMethod("filterMsLevel", "ChromBackend",
function(object, msLevel = integer()) {
if (length(msLevel)) {
object[msLevel(object) %in% msLevel]
} else object
})
#' @exportMethod filterMzRange
#'
#' @importMethodsFrom ProtGenerics filterMzRange
#'
#' @importFrom MsCoreUtils between
#'
#' @rdname ChromBackend
setMethod("filterMzRange", "ChromBackend", function(object, mz = numeric(),
...) {
if (length(mz)) {
mz <- range(mz)
keep <- which(between(mz(object), mz))
object[keep]
} else object
})
#' @exportMethod filterMzValues
#'
#' @importMethodsFrom ProtGenerics filterMzValues
#'
#' @rdname ChromBackend
setMethod("filterMzValues", "ChromBackend",
function(object, mz = numeric(), ppm = 20, tolerance = 0, ...) {
if (length(mz)) {
object[.values_match_mz(precursorMz(object), mz = mz,
ppm = ppm, tolerance = tolerance)]
} else object
})
## TODO:
## - generic filterRanges, filterValues methods as in Spectra.