-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChromBackendMemory.R
More file actions
345 lines (323 loc) · 10.7 KB
/
ChromBackendMemory.R
File metadata and controls
345 lines (323 loc) · 10.7 KB
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
#' @include helpers.R
#' @include hidden_aliases.R
#' @include ChromBackend.R
NULL
#' @title Improved in-memory Chromatographic data backend
#'
#' @name ChromBackendMemory
#'
#'
#' @description
#' `ChromBackendMemory`: This backend stores chromatographic data directly
#' in memory, making it ideal for small datasets or testing. It can be
#' initialized with a `data.frame` of chromatographic data via the `chromData`
#' parameter and a `list` of `data.frame` entries for peaks data using the
#' `peaksData` parameter. These data can be accessed with the `chromData()` and
#' `peaksData()` functions.
#'
#' @param chromData For `backendInitialize()` of a `ChromBackendMemory`
#' backend, a `data.frame` with the chromatographic data. If not
#' provided (or if empty), a default `data.frame` with the core
#' chromatographicvariables will be created.
#'
#' @param object A `ChromBackendMemory` object.
#'
#' @param peaksData For `backendInitialize()` of a `ChromBackendMemory`
#' backend, a `list` of `data.frame` with the peaks data. If not
#' provided (or if empty), a default `list` of empty `data.frame` with
#' the core peaks variables will be created. The length of the list
#' should match the number of chromatograms in the `chromData`
#' parameter.
#'
#' @param ... Additional parameters to be passed.
#'
#' @author Philippine Louail
#'
#' @exportClass ChromBackendMemory
#'
#' @return Refer to the individual function description for information on the
#' return value.
#'
#' @examples
#'
#' ## Method 1: Initialize backend directly
#' cdata <- data.frame(
#' msLevel = c(1L, 1L, 1L),
#' mz = c(112.2, 123.3, 134.4),
#' dataOrigin = c("mem1", "mem2", "mem3")
#' )
#'
#' pdata <- list(
#' data.frame(
#' rtime = c(12.4, 12.8, 13.2, 14.6),
#' intensity = c(123.3, 153.6, 2354.3, 243.4)
#' ),
#' data.frame(
#' rtime = c(45.1, 46.2),
#' intensity = c(100, 80.1)
#' ),
#' data.frame(
#' rtime = c(12.4, 12.8, 13.2, 14.6),
#' intensity = c(123.3, 153.6, 2354.3, 243.4)
#' )
#' )
#'
#' cbm <- ChromBackendMemory()
#' cbm <- backendInitialize(cbm, chromData = cdata, peaksData = pdata)
#' cbm
#'
#' ## Method 2: Use Chromatograms constructor (recommended)
#' chr <- Chromatograms(ChromBackendMemory(), chromData = cdata, peaksData = pdata)
#' chr
#'
NULL
#' @noRd
setClass("ChromBackendMemory",
contains = "ChromBackend",
slots = c(
chromData = "data.frame",
peaksData = "list"
),
prototype = prototype(
chromData = fillCoreChromVariables(data.frame()),
peaksData = list(.EMPTY_PEAKS_DATA),
readonly = FALSE,
version = "0.1"
)
)
#' @rdname ChromBackendMemory
#'
#' @importFrom methods new
#' @export ChromBackendMemory
ChromBackendMemory <- function() {
new("ChromBackendMemory")
}
#' @rdname ChromBackendMemory
setMethod(
"backendInitialize", "ChromBackendMemory",
function(object,
chromData = fillCoreChromVariables(data.frame()),
peaksData = list(.EMPTY_PEAKS_DATA), ...) {
if (!is(chromData, "data.frame")) {
stop(
"'chromData' needs to be a 'data.frame' with the general",
" chromatogram variables"
)
}
n_cd <- nrow(chromData)
if (n_cd) {
if (is.null(chromData$dataOrigin)) {
chromData$dataOrigin <- NA_character_
}
validChromData(chromData)
chromData <- chromData[, !vapply(chromData, function(x) {
all(is.na(x))
}, logical(1)), drop = FALSE]
} else {
chromData <- fillCoreChromVariables(data.frame())
}
object@chromData <- chromData
if (length(peaksData) > 1 || length(peaksData) == n_cd) {
validPeaksData(peaksData)
} else {
peaksData <- replicate(n_cd, .EMPTY_PEAKS_DATA,
simplify = FALSE
)
}
object@peaksData <- peaksData
object
}
)
#' @rdname hidden_aliases
setMethod("backendMerge", "ChromBackendMemory", function(object, ...) {
object <- unname(c(object, ...))
not_empty <- lengths(object) > 0
if (any(not_empty)) {
res <- .df_combine(object[not_empty])
} else {
res <- object[[1L]]
}
validChromData(.chromData(res))
validPeaksData(.peaksData(res))
res
})
#' @rdname hidden_aliases
#' @description This method returns the chromatographic data stored in the
#' backend. If not specified otherwise it will return all defined columns in
#' the chromData slot as well as adding the `coreChromVariables` missing with
#' NA values.
setMethod(
"chromData", "ChromBackendMemory",
function(object, columns = chromVariables(object), drop = FALSE) {
if (!any(chromVariables(object) %in% columns)) {
stop(
"Some of the requested Chromatogram variables are not ",
"available"
)
}
res <- fillCoreChromVariables(.chromData(object))
res <- res[, columns, drop = drop]
res
}
)
#' @rdname hidden_aliases
setReplaceMethod("chromData", "ChromBackendMemory", function(object, value) {
if (!inherits(value, "data.frame")) {
stop("'value' is expected to be a 'data.frame'")
}
if (length(object) && length(object) != nrow(value)) {
stop("'value' has to be a 'data.frame' with ", length(object), " rows")
}
validChromData(value)
object@chromData <- value
object
})
#' @rdname hidden_aliases
setMethod("chromVariables", "ChromBackendMemory", function(object) {
union(names(.chromData(object)), names(coreChromVariables()))
})
#' @rdname hidden_aliases
setMethod(
"peaksData", "ChromBackendMemory",
function(object, columns = peaksVariables(object),
drop = FALSE, ...) {
if (!any(peaksVariables(object) %in% columns)) {
stop(
"Some of the requested peaks variables are not",
" available"
)
}
if (identical(
as.vector(peaksVariables(object)),
as.vector(columns)
)) {
return(.peaksData(object))
}
lapply(.peaksData(object), function(x) x[, columns, drop = drop])
}
)
#' @rdname hidden_aliases
setReplaceMethod("peaksData", "ChromBackendMemory", function(object, value) {
if (!is.list(value)) {
stop("'value' is expected to be a list")
}
if (length(object) && length(object) != length(value)) {
stop("'value' has to be a list with ", length(object), " elements")
}
validPeaksData(value)
object@peaksData <- value
object
})
#' @rdname hidden_aliases
setMethod("peaksVariables", "ChromBackendMemory", function(object) {
union(names(.peaksData(object)[[1]]), names(corePeaksVariables()))
})
#' @rdname hidden_aliases
#' @export
setMethod("isReadOnly", "ChromBackendMemory", function(object) FALSE)
#' @importFrom utils capture.output head
#' @rdname hidden_aliases
setMethod("show", "ChromBackendMemory", function(object) {
cpd <- .chromData(object)
cat(class(object), "with", nrow(cpd), "chromatograms\n")
if (nrow(cpd)) {
cpd <- fillCoreChromVariables(cpd)
cpd <- cpd[, c("chromIndex", "msLevel", "mz"), drop = FALSE]
txt <- capture.output(print(head(cpd)))
cat(txt, sep = "\n")
cp_cols <- .chromData(object)[
,
!(colnames(.chromData(object)) %in%
colnames(cpd))
]
cat("...", length(cp_cols), "more chromatogram variables/columns\n")
cat("...", ncol(.peaksData(object)[[1]]), "peaksData variables\n")
}
})
#' @rdname hidden_aliases
#' @export
setMethod(
"supportsSetBackend", "ChromBackendMemory",
function(object, ...) TRUE
)
#' @importFrom MsCoreUtils i2index
#' @importMethodsFrom S4Vectors [ [<-
#' @rdname hidden_aliases
setMethod("[", "ChromBackendMemory", function(x, i, j, ..., drop = FALSE) {
if (!length(i)) {
return(ChromBackendMemory())
}
i <- i2index(i, length = length(x))
x@chromData <- .chromData(x)[i, ]
x@peaksData <- .peaksData(x)[i]
x
})
#' @rdname hidden_aliases
setMethod("$", "ChromBackendMemory", function(x, name) {
if (name %in% union(chromVariables(x), names(coreChromVariables()))) {
res <- chromData(x, columns = name, drop = TRUE)
} else if (name %in% peaksVariables(x)) {
res <- peaksData(x, columns = name, drop = TRUE)
} else {
stop("The requested variable '", name, "' is not available")
}
res
})
#' @rdname hidden_aliases
setReplaceMethod("$", "ChromBackendMemory", function(x, name, value) {
if (length(x) && length(value) != length(x)) {
stop(
"length of 'value' needs to match the number of chromatograms ",
"in object."
)
}
if (name %in% peaksVariables(x)) {
if (!is.list(value)) {
stop("The value for peaksData should be a list")
}
for (i in seq_along(value)) peaksData(x)[[i]][[name]] <- value[[i]]
} else {
chromData(x)[, name] <- value
}
x
})
#' @rdname hidden_aliases
setMethod("chromExtract", "ChromBackendMemory", function(object, peak.table, by) {
required_cols <- c("rtMin", "rtMax", by)
.validate_chromExtract_input(
object = object,
peak.table = peak.table,
by = by, required_cols = required_cols
)
matched <- .match_chromdata_peaktable(
object = object,
peak.table = peak.table,
by = by
)
object <- matched$object
chrom_keys <- matched$chrom_keys
peak_keys <- matched$peak_keys
obj_sp <- split(object, chrom_keys) ## UT need to check that
pk_split <- split(peak.table, peak_keys)
overl_cols <- .check_overl_columns(peak.table = peak.table,
object = object,
required_cols = required_cols)
new_data <- mapply(function(obj, pks) { ## could switch to bpmapply ?
d <- .chromData(obj)
d <- suppressWarnings(cbind(d, pks[!overl_cols]))
d[, names(peak.table)[overl_cols]] <- pks[, overl_cols]
p <- vector("list", nrow(d))
for (z in seq(nrow(d))) {
rt <- rtime(obj)[[1]]
inrt <- rt >= d$rtMin[z] & rt <= d$rtMax[z]
p[[z]] <- peaksData(obj)[[1]][inrt, , drop = FALSE]
}
list(cd = d, pd = p)
}, obj = obj_sp,
pks = pk_split, SIMPLIFY = FALSE)
new_cdata <- do.call(rbind, lapply(new_data, `[[`, "cd"))
rownames(new_cdata) <- NULL
new_pdata <- unlist(lapply(new_data, `[[`, "pd"), recursive = FALSE)
backendInitialize(new("ChromBackendMemory"),
chromData = new_cdata, peaksData = new_pdata)
})