-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathmethods-pSet.R
597 lines (524 loc) · 23.2 KB
/
methods-pSet.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
## setMethod("initialize",
## signature(.Object="pSet"),
## function(.Object, ..., .cache) {
## cat("initialize pSet")
## if (missing(.cache)) {
## .cache <- new.env()
## assign("level", 0 ,.cache)
## lockEnvironment(.cache)
## }
## callNextMethod(.Object, ..., .cache = .cache)
## })
## That (according to the R-help) should be the "correct" way to set specific
## slot values if not provided. With the "old" version above some slot values (like
## 'onDisk') were not propagated.
setMethod("initialize",
"pSet",
function(.Object, ...) {
if (!any(names(list(...)) == ".cache")) {
.cache <- new.env()
assign("level", 0, .cache)
lockEnvironment(.cache)
.Object@.cache <- .cache
}
callNextMethod()
})
setValidity("pSet", function(object) {
msg <- validMsg(NULL, NULL)
## Skip some (most) of the tests for a OnDiskMSnExp, since we don't have
## spectrum data available, i.e. assayData is empty.
if (length(object)) {
if (!all(sapply(assayData(object), function(x) inherits(x, "Spectrum"))))
msg <- validMsg(msg,
"assayData must contain 'Spectrum' objects.")
msl <- msLevel(object)
if (length(unique(msl)) > 1)
warning(paste0("Different MS levels in ", class(object),
" object: ", unique(msl)))
## checking number of spectra in assayData and
## number of features in featureData
nspectra <- length(assayData(object))
nfeatures <- nrow(featureData(object))
if (nspectra != nfeatures)
msg <- validMsg(msg, "Unequal number of spectra in assayData and features in featureData.")
if (length(spectra(object)) != length(ls(assayData(object))))
msg <- validMsg(msg, "Object size inconsistence using assayData() and spectra() methods.")
if (!identical(featureNames(object),
ls(assayData(object))))
msg <- validMsg(msg, "featureNames differ between assayData and featureData.")
## checking number of files in phenoData and
## number of files in assayData
## removing the following check because we add identification files
## (a modified version using "<" instead of "!=" is below
## nfilesprocData <- length(processingData(object)@files)
## nfilesSpectra <- length(unique(unlist(eapply(assayData(object),fromFile))))
## if (nfilesprocData != nfilesSpectra)
## msg <- validMsg(msg, "unequal number of files in assayData and processingData.")
aFileIds <- fromFile(object)
fFileIds <- fData(object)$file
if (length(fFileIds) && any(aFileIds != fFileIds))
msg <- validMsg(msg, "Mismatch of files in assayData and processingData.")
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.")
## phenoData: at this stage, we don't know how many sample there will be
## expecting one for label-free, but we could have 4, 6, 8 for isobaric tagging
## or 2 for metabolic labelling
## if (nrow(pData(object)) > 1)
## message("Detected ", nrow(pData(object)), " samples in pData().")
##
## protocolData not checked yet - depends very much
## on type of assay (MS1, MS2 quant, reporter ions, ...)
if (!cacheEnvIsLocked(object))
msg <- validMsg(msg, "'.cache' environment is not locked.")
if (!exists("level", envir = object@.cache))
msg <- validMsg(msg, "'.cache' level not defined.")
hd <- header(object)
if (nrow(hd) != length(object))
msg("(Cached) header nrow and object length differ.")
sapply(spectra(object), validObject)
}
if (is.null(msg)) TRUE else msg
})
setMethod("[", "pSet",
function(x, i, j = "missing", drop = "missing") {
if (!(is.logical(i) | is.numeric(i)))
stop("subsetting works only with numeric or logical")
if (is.numeric(i)) {
if (max(i) > length(x) | min(i) < 1)
stop("subscript out of bounds")
i <- base::sort(i) ## crash if unsorted (because of
## (alphanumerical) order in ls(assayData(.)) and
## featureNames(featureData) that have to be
## indentical - see issues #70 and #71)
}
whichElements <- ls(assayData(x))[i]
sel <- featureNames(x) %in% whichElements
## Sub-setting also processingData, phenoData and experimentData.
file <- base::sort(unique(fromFile(x)[sel]))
## o Sub-set the phenoData.
pd <- phenoData(x)[file, , drop = FALSE]
pData(pd) <- droplevels(pData(pd))
x@phenoData <- pd
## Sub-set the files.
## This one should never be empty
x@processingData@files <- x@processingData@files[file]
## Sub-set the experimentData: these slots can be empty
## character(), producing NA if subest with character()[file]
expD <- experimentData(x)
if (length(expD@instrumentManufacturer))
expD@instrumentManufacturer <- expD@instrumentManufacturer[file]
else expD@instrumentManufacturer <- expD@instrumentManufacturer
if (length(expD@instrumentModel))
expD@instrumentModel <- expD@instrumentModel[file]
else expD@instrumentModel <- expD@instrumentModel
if (length(expD@ionSource))
expD@ionSource <- expD@ionSource[file]
else expD@ionSource <- expD@ionSource
if (length(expD@analyser))
expD@analyser <- expD@analyser[file]
else expD@analyser <- expD@ionSource
if (length(expD@detectorType))
expD@detectorType <- expD@detectorType[file]
else expD@detectorType <- expD@ionSource
x@experimentData <- expD
## Fix the fromFile property
newFromFile <- base::match(fromFile(x), file)
names(newFromFile) <- names(fromFile(x))
## Proceed.
orghd <- header(x)
olde <- assayData(x)
newe <- new.env(parent = emptyenv())
if (length(whichElements) > 0) {
for (el in whichElements) {
sp <- olde[[el]]
sp@fromFile <- unname(newFromFile[el])
newe[[el]] <- sp
}
if (environmentIsLocked(olde))
lockEnvironment(newe,
bindings = bindingIsLocked(el, olde))
} else {
lockEnvironment(newe, bindings = TRUE)
}
x@assayData <- newe
x@featureData <- featureData(x)[i, ]
if (is.logical(i)) {
x@processingData@processing <-
c(processingData(x)@processing,
paste("Data [logically] subsetted ", sum(i),
" spectra: ", date(), sep = ""))
} else if (is.numeric(i)) {
x@processingData@processing <-
c(processingData(x)@processing,
paste("Data [numerically] subsetted ",
length(i), " spectra: ", date(),
sep = ""))
} else {
x@processingData@processing <-
c(processingData(x)@processing,
paste("Data subsetted ", i ,": ", date(),
sep = ""))
}
if (x@.cache$level > 0) {
## no caching for empty objects
.cache <- ifelse(length(x) > 1, x@.cache$level, 0)
x@.cache <- setCacheEnv(list(assaydata = assayData(x),
hd = orghd[sel, ]), ## faster than .header for big instances
.cache)
}
if (validObject(x))
return(x)
})
setMethod("[[", "pSet",
function(x, i, j = "missing", drop = "missing") {
if (length(i) != 1)
stop("subscript out of bounds")
if (!is.character(i))
i <- featureNames(x)[i]
return(get(i, envir = assayData(x)))
})
setMethod("precursorMz", "pSet",
function(object) {
## this assumes that if first spectrum
## has msLevel > 1, all have
if (.firstMsLevel(object) > 1)
return(sapply(spectra(object), precursorMz))
stop("No precursor MZ value for MS1 spectra.")
})
## a general version
## setMethod("precursorMz","pSet",
## function(object) {
## msl <- msLevel(object)
## ans <- rep(NA_integer_, length(object))
## obj2 <- object[msl > 1]
## ans2 <- sapply(spectra(obj2), slot, "precursorMz")
## ans[msl > 1] <- ans2
## names(ans) <- featureNames(object)
## ans
## })
setMethod("precScanNum", "pSet",
function(object) {
if (.firstMsLevel(object) > 1)
return(unlist(sapply(spectra(object), precScanNum)))
stop("This experiment contains MS1 spectra.")
})
setMethod("tic", "pSet",
function(object) sapply(spectra(object), tic))
setMethod("ionCount", "pSet",
function(object) sapply(spectra(object), ionCount))
setMethod("precursorCharge", "pSet",
function(object) {
if (.firstMsLevel(object) > 1)
return(sapply(spectra(object), precursorCharge))
stop("No precursor MZ value for MS1 spectra.")
})
setMethod("precursorIntensity", "pSet",
function(object) {
if (.firstMsLevel(object) > 1)
return(sapply(spectra(object), precursorIntensity))
stop("No precursor data for MS1 spectra.")
})
setMethod("acquisitionNum", "pSet",
function(object) sapply(spectra(object), acquisitionNum))
setMethod("scanIndex", "pSet",
function(object) sapply(spectra(object), scanIndex))
setMethod("rtime", "pSet",
function(object) sapply(spectra(object), rtime))
setMethod("centroided", "pSet",
function(object, na.fail = FALSE)
sapply(spectra(object), centroided, na.fail))
setReplaceMethod("centroided",
signature(object = "pSet",
value = "logical"),
function(object, value) {
if (length(value) == 1)
value <- rep(value, length(object))
if (length(object) != length(value))
stop("Length of replacement value is different than number of spectra.")
sl <- spectra(object)
for (i in 1:length(sl))
centroided(sl[[i]]) <- value[i]
object@assayData <- as.environment(sl)
if (validObject(object))
return(object)
})
setMethod("smoothed", "pSet",
function(object) sapply(spectra(object), smoothed))
setReplaceMethod("smoothed",
signature(object = "pSet",
value = "logical"),
function(object, value) {
if (length(value) == 1)
value <- rep(value, length(object))
if (length(object) != length(value))
stop("Length of replacement value is different than number of spectra.")
sl <- spectra(object)
for (i in 1:length(sl))
smoothed(sl[[i]]) <- value[i]
object@assayData <- as.environment(sl)
if (validObject(object))
return(object)
})
setMethod("peaksCount",
signature("pSet", "missing"),
function(object) sapply(spectra(object), peaksCount))
setMethod("peaksCount",
signature("pSet", "numeric"),
function(object, scans) {
if (length(scans) == 1)
return(peaksCount(object[[scans]]))
sapply(spectra(object)[scans], peaksCount)
})
setMethod("msLevel", "pSet",
function(object) sapply(spectra(object), msLevel))
setMethod("collisionEnergy", "pSet",
function(object) {
if (.firstMsLevel(object) > 1)
return(sapply(spectra(object), collisionEnergy))
stop("No collision energy for MS1 spectra.")
})
setMethod("intensity", "pSet",
function(object) lapply(spectra(object),intensity))
setMethod("mz", "pSet",
function(object) lapply(spectra(object),mz))
setMethod("polarity", "pSet",
function(object) sapply(spectra(object), polarity))
setMethod("fromFile", "pSet",
function(object) return(sapply(spectra(object), fromFile)))
setReplaceMethod("fromFile",
signature(object = "pSet",
value = "integer"),
function(object, value) {
if (length(object) != length(value))
stop("Length of replacement value is different than number of spectra.")
sl <- spectra(object)
for (i in 1:length(sl))
sl[[i]]@fromFile <- value[i]
object@assayData <- as.environment(sl)
if (validObject(object))
return(object)
})
setMethod("header",
signature("pSet", "missing"),
function(object) {
ifelse(object@.cache$level > 0,
hd <- object@.cache$hd,
hd <- .header(object))
return(hd)
})
setMethod("header",
signature = c("pSet", "numeric"),
function(object, scans) {
hd <- header(object)
return(hd[scans, ])
})
##################################################################
## NOTE: pSet is based in the eSet class defined in the Biobase
## package, and accessor and replace methods for the common
## slots are expected to have identical behaviours.
## In these cases, code is heavily inspired and sometimes
## directly copies from the respective eSet method.
## returns the dimensions of PhenoData
setMethod("dim", "pSet", function(x) dim(pData(x)))
## returns the number of spectra in the AssayData env
setMethod("length", "pSet", function(x) length(assayData(x)))
setMethod("assayData", "pSet", function(object) object@assayData)
setMethod("spectra", "MSnExp", function(object, ...) {
sl <- as.list(assayData(object))
fnames <- featureNames(object)
## reordering the spectra in the spectra list to match
## their order in featureData
return(sl[fnames])
})
## setReplaceMethod("assayData",
## signature=signature(
## object="pSet",
## value="AssayData"),
## function(object, value) {
## object@assayData <- value ## may be lock env?
## return(object)
## })
setMethod("sampleNames",
signature(object = "pSet"),
function(object) sampleNames(phenoData(object)))
setMethod("fileNames",
signature(object = "pSet"),
function(object) processingData(object)@files)
## setReplaceMethod("fileNames",
## signature(object="pSet", value="character"),
## function(object, value) {
## fileNames(object@processingData) <- value
## return(object)
## })
setReplaceMethod("sampleNames",
signature = signature(object = "pSet", value = "character"),
function(object, value) {
pd <- phenoData(object)
sampleNames(pd) <- value
prd <- protocolData(object)
if (nrow(prd) == 0) {
prd <- pd[, integer(0)]
} else {
sampleNames(prd) <- value
}
object@phenoData <- pd
object@protocolData <- prd
if (validObject(object))
return(object)
})
setMethod("featureNames",
signature = signature(object = "pSet"),
function(object) featureNames(featureData(object)))
setMethod("phenoData", "pSet", function(object) object@phenoData)
setMethod("pData", "pSet", function(object) pData(phenoData(object)))
setMethod("varMetadata",
signature = signature(object = "pSet"),
function(object) varMetadata(phenoData(object)))
setMethod("varLabels",
signature = signature(object = "pSet"),
function(object) varLabels(phenoData(object)))
setMethod("featureData",
signature(object = "pSet"),
function(object) object@featureData)
setReplaceMethod("featureData",
signature = signature(
object = "pSet",
value = "AnnotatedDataFrame"),
function(object, value) {
object@featureData <- value
if (validObject(object))
return(object)
})
setMethod("fData",
signature = signature(object = "pSet"),
function(object) pData(featureData(object)))
setReplaceMethod("fData",
signature = signature(
object = "pSet",
value = "data.frame"),
function(object, value) {
if (!identical(featureNames(object), rownames(value)))
stop("Feature names and rownames of the new fData are not identical.")
fd <- featureData(object)
pData(fd) <- value
object@featureData <- fd
if (validObject(object))
return(object)
})
setMethod("fvarMetadata",
signature = signature(object = "pSet"),
function(object) varMetadata(featureData(object)))
setMethod("fvarLabels",
signature = signature(object = "pSet"),
function(object) varLabels(featureData(object)))
setMethod("experimentData", signature(object = "pSet"),
function(object) object@experimentData)
setMethod("msInfo", "pSet",
function(object) msInfo(experimentData(object)))
setMethod("expinfo", "pSet",
function(object) expinfo(experimentData(object)))
setMethod("exptitle", "pSet",
function(object) exptitle(experimentData(object)))
setMethod("expemail", "pSet",
function(object) expemail(experimentData(object)))
setMethod("ionSource", "pSet",
function(object) ionSource(experimentData(object)))
setMethod("ionSourceDetails", "pSet",
function(object) ionSourceDetails(experimentData(object)))
setMethod("analyser", "pSet",
function(object) analyser(experimentData(object)))
setMethod("analyzer", "pSet",
function(object) analyzer(experimentData(object)))
setMethod("analyzerDetails", "pSet",
function(object) analyzerDetails(experimentData(object)))
setMethod("analyserDetails", "pSet",
function(object) analyzerDetails(experimentData(object)))
setMethod("instrumentModel", "pSet",
function(object) instrumentModel(experimentData(object)))
setMethod("instrumentManufacturer", "pSet",
function(object) instrumentManufacturer(experimentData(object)))
setMethod("instrumentCustomisations", "pSet",
function(object) instrumentCustomisations(experimentData(object)))
setMethod("detectorType", "pSet",
function(object) detectorType(experimentData(object)))
setMethod("description", signature(object = "pSet"),
function(object, ...) {
experimentData(object)
})
setMethod("notes", signature(object = "pSet"),
function(object) otherInfo(experimentData(object)))
setMethod("pubMedIds", signature(object = "pSet"),
function(object) pubMedIds(experimentData(object)))
setReplaceMethod("pubMedIds",
signature = signature(
object = "pSet",
value = "character"),
function(object, value) {
ed <- experimentData(object)
pubMedIds(ed) <- value
object@experimentData <- ed
return(object)
})
setMethod("abstract", "pSet",
function(object) abstract(experimentData(object)))
setMethod("protocolData", "pSet",
function(object) {
tryCatch(object@protocolData,
error = function(x) {
phenoData(object)[, integer(0)]
})
})
setMethod("processingData",
signature(object = "pSet"),
function(object) object@processingData)
setMethod("spectrapply", "pSet", function(object, FUN = NULL,
BPPARAM = bpparam(), ...) {
BPPARAM <- getBpParam(object, BPPARAM = BPPARAM)
if (is.null(FUN))
return(spectra(object))
bplapply(spectra(object), FUN = FUN, BPPARAM = BPPARAM, ...)
})
setMethod("$", "pSet", function(x, name) {
eval(substitute(pData(x)$NAME_ARG, list(NAME_ARG = name)))
})
setReplaceMethod("$", "pSet", function(x, name, value) {
pData(x)[[name]] <- value
x
})
setReplaceMethod("pData", "pSet", function(object, value) {
if (!is.data.frame(value))
stop("'value' has to be a 'data.frame'")
pData(object@phenoData) <- value
object
})
setReplaceMethod("phenoData", "pSet", function(object, value) {
object@phenoData <- value
if (validObject(object))
object
})
setMethod("isolationWindowLowerMz", "pSet", function(object)
stop("isolationWindowLowerMz not available for ", class(object)))
setMethod("isolationWindowUpperMz", "pSet", function(object)
stop("isolationWindowUpperMz not available for ", class(object)))
################################
## TODO: setReplaceMethods for
## phenoData
## pData
## processingData - or may be individual elements for MSnProcess class
## varMetadata
## varLabels
## fvarMetadata
## fvarLabels
## experimentData
## description
## notes
## pubMedIds
## abstract
## protocolData
## - - - - - - - - - - - - - - - - - - - - - -
## Other TODO (based on eSet):
## setMethod("combine","pSet", ## rather a combine(MSnExp, )