/
DataClasses.R
526 lines (504 loc) · 21.5 KB
/
DataClasses.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
## Returns of class version as documented in .MSnBaseEnd$ClassVersions
## as and instance of class Versions.
getClassVersion <- function(x) {
if (!is.character(x))
x <- class(x)[1]
## This get class versions from parent classes (if any)
ver <- classVersion(x)
## Adds (or overwrites) x's class version to the list of class
## versions
ver[x] <- getClassVersionString(x)
ver
}
## Utility to just extract the version string from the environment.
getClassVersionString <- function(x) {
if (!is.character(x))
x <- class(x)[1]
return(.MSnbaseEnv$ClassVersions[x])
}
######################################################################
## MSnProcess: Container for MSnExp and MSnSet processing information
## See online documentation for more information.
setClass("MSnProcess",
representation = representation(
files = "character",
processing = "character",
merged = "logical",
cleaned = "logical",
removedPeaks = "character",
smoothed = "logical",
trimmed = "numeric",
normalised = "logical",
MSnbaseVersion = "character"),
contains = c("Versioned"),
prototype = prototype(
new("Versioned", versions = c(MSnProcess = "0.1.3")),
processing = character(),
files = character(),
trimmed = numeric(),
removedPeaks = character(),
MSnbaseVersion = character())) ## set in initialize()
#################################################################
## The 'Minimum Information About a Proteomics Experiment' Class
## See online documentation for more information.
setClass("MIAPE",
representation = representation(
title = "character",
url = "character",
## Publication details
abstract = "character",
pubMedIds = "character",
## Other useful slots, from MIAME
samples = "list",
preprocessing = "list",
other = "list",
## ########################
## Based on MIAPE-MS 2.24
## will be updated with MIAPE-MSI and MIAPE-Quant
## 1. General features - (a) Global descriptors
dateStamp = "character",
## Responsible person
name = "character",
lab = "character",
contact = "character",
email = "character",
## Instrument details
instrumentModel = "character",
instrumentManufacturer = "character",
instrumentCustomisations = "character",
## 1. General features - (b) Control and analysis software
softwareName = "character",
softwareVersion = "character",
switchingCriteria = "character",
isolationWidth = "numeric",
parameterFile = "character",
## 2. Ion sources -- will be updated to
## provided details specific to
## different sources
ionSource = "character", ## ESI, MALDI, ...
ionSourceDetails = "character",
## 3. Post-source component
analyser = "character", ## Quad, TOF, Trap, ...
analyserDetails = "character",
## 3. Post-source component - (d) Collision cell
collisionGas = "character",
collisionPressure = "numeric",
collisionEnergy = "character",
## 3. Post-source component - (f) Detectors
detectorType = "character",
detectorSensitivity = "character"
## 4. Spectrum and peak list generation and annotation
## (a) Spectrum description
## (b) Peak list generation
## (c) Quantitation for selected ions
## -- see Spectrum and MSnProcess objects
),
contains = c("MIAxE"),
prototype = prototype(
new("Versioned",
versions = c(classVersion("MIAxE"), MIAPE = "0.2.2")),
name = "",
lab = "",
contact = "",
title = "",
abstract = "",
url = "",
pubMedIds = "",
email = "",
samples = list(),
preprocessing = list(),
other = list()))
#############################################################################
## pSet: similarly to eSet but with a focus toward proteomics experiments,
## pSet is a VIRTUAL class containing assay data (typically, one or many
## different sets of spectra), phenotypic data (describing the samples involved
## in the experiment), experimental data (describing the methods and
## protocols used) and feature data (describing the features in the assay).
##
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setClass("pSet",
representation(assayData = "environment", ## locked environment
phenoData = "AnnotatedDataFrame",
featureData = "AnnotatedDataFrame",
# How to link individual spectra in assayData env to featureData?
# The spectra and the rowNames of featureData will be named X1..(n1+n2+...)
experimentData = "MIAxE",
protocolData = "AnnotatedDataFrame",
processingData = "MSnProcess",
##onDisk = "logical",
.cache = "environment", ## locked
"VIRTUAL"),
contains = "Versioned",
prototype = prototype(
new("Versioned", versions = c(pSet = "0.1.1")),
assayData = new.env(parent=emptyenv()),
experimentData = new("MIAPE"),
phenoData = new("AnnotatedDataFrame",
dimLabels=c("sampleNames", "fileNumbers")),
featureData = new("AnnotatedDataFrame",
dimLabels=c("featureNames", "featureColumns")),
protocolData = new("AnnotatedDataFrame",
dimLabels=c("sampleNames", "sampleColumns"))
##,onDisk = FALSE
)
)
##################################################################
## Container for MSn Experiments Data and Meta-Data
## See online documentation for more information.
setClass("MSnExp",
contains=c("pSet"),
prototype = prototype(
new("VersionedBiobase",
versions = c(classVersion("pSet"), MSnExp="0.3.1")),
experimentData = new("MIAPE"))
)
###################################################################
## The Spectrum class and it's sub-classes Spectrum1 and Spectrum2
## See online documentation for more information.
setClass("Spectrum",
representation = representation(
msLevel="integer",
peaksCount="integer",
rt="numeric",
acquisitionNum="integer",
scanIndex = "integer",
tic = "numeric",
mz = "numeric",
intensity = "numeric",
fromFile = "integer",
centroided = "logical",
smoothed = "logical",
polarity="integer",
"VIRTUAL"),
contains=c("Versioned"),
prototype = prototype(
rt = numeric(),
polarity = NA_integer_,
acquisitionNum = NA_integer_,
msLevel = NA_integer_,
centroided = NA,
smoothed = NA,
peaksCount = 0L,
tic = 0,
scanIndex = integer(),
mz = numeric(),
intensity = numeric()),
validity = function(object)
validSpectrum(object)
)
setClass("Spectrum2",
representation = representation(
merged="numeric",
precScanNum="integer",
precursorMz="numeric",
precursorIntensity = "numeric",
precursorCharge = "integer",
collisionEnergy = "numeric"),
contains=c("Spectrum"),
prototype = prototype(
merged = 1,
acquisitionNum = integer(),
precScanNum = integer(),
precursorMz = numeric(),
precursorIntensity = numeric(),
msLevel = as.integer(2),
precursorCharge = integer(),
collisionEnergy = numeric()),
validity = function(object) {
msg <- validMsg(NULL, NULL)
msl <- object@msLevel
if (msl < as.integer(2))
msg <- validMsg(msg,
paste0("Object of class ",
class(object),
" but msLevel is ", msl,
" (should be > 1)"))
if (is.null(msg)) TRUE
else msg
})
setClass("Spectrum1",
contains=c("Spectrum"),
prototype = prototype(
polarity=integer(),
msLevel = as.integer(1)),
validity = function(object) {
msg <- validMsg(NULL, NULL)
msl <- object@msLevel
if (msl!=as.integer(1))
msg <- validMsg(msg,paste("Object of class",class(object),
"but msLevel is",msl,sep=" "))
if (is.null(msg)) TRUE
else msg
})
##################################################################
## Data Structure for Reporter Ions for labelled MS Quantification
## See online documentation for more information.
setClass("ReporterIons",
representation = representation(
name = "character",
reporterNames = "character",
description = "character",
mz = "numeric",
col = "character",
width = "numeric"),
contains = c("Versioned"),
prototype = prototype(
new("Versioned", versions = c(ReporterIons = "0.1.0")),
name = character(),
reporterNames = character(),
description = character(),
mz = numeric(),
col = character(),
width = numeric()),
validity = function(object) {
msg <- validMsg(NULL, NULL)
if (length(object@mz) == 0) {
msg <- validMsg(msg, "No reporter ions defined.")
} else {
if (length(object@col) != length(object@mz))
warning("Missing color(s) for the reporter ions.")
}
if (is.null(msg)) TRUE
else msg
})
#####################################################################
## The "MSnSet" Class for MS Proteomics Expression Data and Meta-Data
## See online documentation for more information.
setClass("MSnSet",
representation = representation(
experimentData="MIAPE",
processingData = "MSnProcess",
qual = "data.frame"),
contains = "eSet",
prototype = prototype(
new("VersionedBiobase",
versions=c(classVersion("eSet"),
classVersion("pSet"),
MSnSet="0.4.0")),
experimentData=new("MIAPE"),
annotation="No feature annotation."))
.MSnSetList <-
setClass("MSnSetList",
slots = c(x = "list",
log = "list",
featureData = "DataFrame"),
contains = "Versioned",
prototype = prototype(
new("Versioned",
versions = c(MSnSetList = "0.2.0"))),
validity = function(object) {
msg <- validMsg(NULL, NULL)
if (!listOf(object@x, "MSnSet", valid = FALSE))
msg <- validMsg(msg, "Not all items are MSnSets.")
nvals <- sapply(object@x, validObject)
if (!all(nvals))
msg <- validMsg(msg,
paste(sum(!nvals),
"MSnSets are not valid."))
if (length(object@x) != nrow(object@featureData))
msg <- validMsg(msg,
"Data and meta-data dimensions don't match.")
if (length(object@x) &&
!identical(names(object@x), rownames(object@featureData)))
msg <- validMsg(msg,
"Data and meta-data names don't match.")
if (is.null(msg)) TRUE
else msg
})
#####################################################################
## Features of interest infrastructure
.FeaturesOfInterest <-
setClass("FeaturesOfInterest",
slots = c(
description = "character",
fnames = "character",
date = "character",
objpar = "list"),
contains = "Versioned",
prototype = prototype(
new("Versioned",
versions = c(FeaturesOfInterest = "0.1.0"))))
.FoICollection <-
setClass("FoICollection",
slots = c(foic = "list"),
contains = "Versioned",
prototype = prototype(
new("Versioned",
versions = c(FeaturesOfInterest = "0.1.0"))))
#####################################################################
## Support for the PSI mzTab format
.MzTab <- setClass("MzTab",
slots = c(
Metadata = "list",
Filename = "character",
Proteins = "data.frame",
Peptides = "data.frame",
PSMs = "data.frame",
SmallMolecules = "data.frame",
MoleculeFeatures = "data.frame",
MoleculeEvidence = "data.frame",
Comments = "character"))
##################################################################
## Container for MSn Experiments Data and Meta-Data enabling/allowing
## to process raw MS files on-the-fly (without the need to keep all
## data in memory).
setClass("OnDiskMSnExp",
representation=representation(
spectraProcessingQueue="list", ## List collecting ProcessingSteps for lazy processing.
backend="character" ## That's to eventually add a SQLite backend later...
),
contains=c("MSnExp"),
prototype = prototype(
new("VersionedBiobase",
versions = c(classVersion("MSnExp"), OnDiskMSnExp="0.0.1")),
spectraProcessingQueue=list(),
backend=character()),
validity=function(object){
## Return true if the object is empty.
if (length(object) == 0)
return(TRUE)
## Ensure that the files (returned by fileNames) are available
## and check also that the featureData contains all the required
## information.
msg <- validMsg(NULL, NULL)
## Elements in spectraProcessingQueue have to be ProcessingStep objects.
if (length(object@spectraProcessingQueue) > 0){
isOK <- unlist(lapply(object@spectraProcessingQueue,
function(z)
return(is(z, "ProcessingStep"))))
if (any(!isOK))
msg <- validMsg(msg,
paste0("Only objects of type 'ProcessingStep'",
" allowed in slot 'spectraProcessingQueue'"))
}
## Check that required columns are present in the featureData:
msg <- validMsg(msg, validateFeatureDataForOnDiskMSnExp(featureData(object)))
## Check if the files do exist.
theFiles <- fileNames(object)
for (theF in theFiles){
if (!file.exists(theF))
msg <- validMsg(msg,
paste0("Required data file '", basename(theF),
"' not found!"))
}
## Some last checks I had to take from the pSet as validObject on OnDiskMSnExp was first
## calling the pSet validate method on the MSnExp and that caused a problem since we don't
## have an assayData with spectra here (fromFile was trying to get that form there).
aFileIds <- fromFile(object)
fFileIds <- fData(object)$fileIdx
if (length(fFileIds) && any(aFileIds != fFileIds))
msg <- validMsg(msg, "Mismatch of files in assayData and processingData.")
## Check if the fromFile values match to @files in processingData
filesProcData <- 1:length(processingData(object)@files)
if ( !all(unique(sort(aFileIds)) == unique(sort(filesProcData))) )
msg <- validMsg(msg, "Spectra file indices in assayData does not match files in processinData.")
nfilesprocData <- length(processingData(object)@files)
nfilesSpectra <- length(unique(aFileIds))
if (nfilesprocData < nfilesSpectra)
msg <- validMsg(msg, "More spectra files in assayData than in processingData.")
if (length(sampleNames(object)) != nrow(pData(object)))
msg <- validMsg(msg, "Different number of samples accoring to sampleNames and pData.")
## Check also experimentData:
if (length(
unique(c(length(fileNames(object)),
length(experimentData(object)@instrumentManufacturer),
length(experimentData(object)@instrumentModel),
length(experimentData(object)@ionSource),
length(experimentData(object)@analyser),
length(experimentData(object)@detectorType)))) != 1)
msg <- validMsg(msg, "The number of files does not match the information in experimentData.")
if (!isOnDisk(object))
msg <- validMsg(msg, "Object is not 'onDisk'.")
if (!isEmpty(object@assayData))
msg <- validMsg(msg, "Assaydata is not empty.")
if (is.null(msg)) {
return(TRUE)
} else {
return(msg)
}
})
setClass("Chromatogram",
slots = c(
rtime = "numeric",
intensity = "numeric",
mz = "numeric",
filterMz = "numeric",
precursorMz = "numeric", ## Or call that Q1mz?
productMz = "numeric", ## Or call that Q3mz?
fromFile = "integer",
aggregationFun = "character",
msLevel = "integer"
),
contains = "Versioned",
prototype = prototype(
rtime = numeric(),
intensity = numeric(),
mz = c(NA_real_, NA_real_),
filterMz = c(NA_real_, NA_real_),
precursorMz = c(NA_real_, NA_real_),
productMz = c(NA_real_, NA_real_),
fromFile = integer(),
aggregationFun = character(),
msLevel = 1L
),
validity = function(object)
.validChromatogram(object)
)
setClass("MChromatograms",
contains = "matrix",
slots = c(phenoData = "AnnotatedDataFrame",
featureData = "AnnotatedDataFrame"),
prototype = prototype(
matrix(ncol = 0, nrow = 0),
phenoData = new("AnnotatedDataFrame",
dimLabels = c("sampleNames", "sampleColumns")),
featureData = new("AnnotatedDataFrame",
dimLabels = c("featureNames", "featureColumns"))
),
validity = function(object)
.validMChromatograms(object)
)
#' @name MSpectra
#'
#' @aliases MSpectra-class show,MSpectra-method coerce,MSpectra,list-method coerce,MSpectra,MSnExp-method
#'
#' @title List of Spectrum objects along with annotations
#'
#' @description
#'
#' `MSpectra` (Mass Spectra) objects allow to collect one or more
#' [Spectrum-class] object(s) ([Spectrum1-class] or [Spectrum2-class]) in
#' a `list`-like structure with the possibility to add arbitrary annotations
#' to each individual `Spectrum` object. These can be accessed/set with
#' the [mcols()] method.
#'
#' `MSpectra` objects can be created with the `MSpectra` function.
#'
#' Functions to access the individual spectra's attributes are available
#' (listed below).
#'
#' @details
#'
#' `MSpectra` inherits all methods from the [SimpleList] class of the
#' `S4Vectors` package. This includes `lapply` and other data manipulation
#' and subsetting operations.
#'
#' @param object For all functions: a `MSpectra` object.
#'
#' @param x For all functions: a `MSpectra` object.
#'
#' @md
#'
#' @rdname MSpectra
NULL
.MSpectra <- setClass("MSpectra",
contains = "SimpleList",
prototype = prototype(elementType = "Spectrum")
)
setValidity("MSpectra", function(object) {
## All elements in the list have to be Spectrum objects.
msg <- character()
if (any(vapply(object, function(z) !is(z, "Spectrum"), logical(1))))
msg <- c(msg, "All elements have to be Spectrum objects")
if (length(msg)) msg else TRUE
})