-
Notifications
You must be signed in to change notification settings - Fork 0
fix vignettes, refactor chromBackendSpectra #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
74743d6
c10498e
876abea
c2759e9
3ace76e
11cfd82
5a5537d
a07a739
d26ead0
5ee9cc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,20 @@ NULL | |
| #' replacement is unsupported — modifications are temporary to optimize memory. | ||
| #' The `inMemory` slot indicates this with `TRUE`. | ||
| #' | ||
| #' **Spectra Sort Index**: The `ChromBackendSpectra` backend maintains a | ||
| #' `spectraSortIndex` slot that stores a sort order for the internal `Spectra` | ||
| #' object based on `dataOrigin` and `rtime`. This avoids the need to physically | ||
| #' reorder disk-backed `Spectra` objects, which would require loading all data | ||
| #' into memory. The sort index is automatically recalculated whenever the | ||
| #' `factorize()` method is called, ensuring it remains valid and consistent. | ||
| #' | ||
| #' **Factorize and Subsetting**: The `factorize()` method updates the | ||
| #' `chromSpectraIndex` in both `chromData` and the `spectra` object to reflect | ||
|
||
| #' the current grouping, and recalculates `spectraSortIndex` to maintain the | ||
| #' correct sort order. The `[` subsetting operator properly handles subsetting | ||
| #' of both `chromData`, `peaksData`, and `spectra`, while updating the | ||
| #' `spectraSortIndex` to reference valid positions in the subsetted data. | ||
| #' | ||
| #' `ChromBackendSpectra` should reuse `ChromBackendMemory` methods whenever | ||
| #' possible to keep implementations simple. | ||
| #' | ||
|
|
@@ -131,7 +145,8 @@ ChromBackendSpectra <- setClass( | |
| slots = c( | ||
| inMemory = "logical", | ||
| spectra = "Spectra", | ||
| summaryFun = "function" | ||
| summaryFun = "function", | ||
| spectraSortIndex = "integer" | ||
| ), | ||
| prototype = prototype( | ||
| chromData = fillCoreChromVariables(data.frame()), | ||
|
|
@@ -140,7 +155,8 @@ ChromBackendSpectra <- setClass( | |
| spectra = Spectra(), | ||
| version = "0.1", | ||
| inMemory = FALSE, | ||
| summaryFun = sumi | ||
| summaryFun = sumi, | ||
| spectraSortIndex = integer() | ||
| ) | ||
| ) | ||
|
|
||
|
|
@@ -181,12 +197,13 @@ setMethod("backendInitialize", "ChromBackendSpectra", | |
| "it needs to be part of the `coreChromVariables()` ", | ||
| "available.") | ||
| ## Spectra object are not expected to be ordered by rtime, | ||
| ## so we fix that below. | ||
| spectra <- lapply(split(spectra, spectra$dataOrigin), | ||
| function(x) { | ||
| x[order(x$rtime)] | ||
| }) | ||
| spectra <- concatenateSpectra(spectra) | ||
| ## so we store a sort index instead of concatenating. | ||
| ## This allows us to keep disk-backed backends intact. | ||
| sort_idx <- order( | ||
| spectra$dataOrigin, | ||
| spectra$rtime | ||
| ) | ||
| object@spectraSortIndex <- sort_idx | ||
| object@chromData <- chromData | ||
| object@spectra <- spectra | ||
|
|
||
|
|
@@ -210,7 +227,7 @@ setMethod("show", "ChromBackendSpectra", function(object) { | |
| }) | ||
|
|
||
| #' @rdname ChromBackendSpectra | ||
| #' @note ensure that it returns a factor | ||
| #' @export | ||
| chromSpectraIndex <- function(object) { | ||
| if (!is(object, "ChromBackendSpectra")) | ||
| stop("The object must be a 'ChromBackendSpectra' object.") | ||
|
|
@@ -228,32 +245,48 @@ setMethod("factorize", "ChromBackendSpectra", | |
| spectraVariables(.spectra(object)))) | ||
| stop("All 'factorize.by' variables must be in the ", | ||
| "Spectra object.") | ||
| spectra_f <- interaction(as.list( | ||
|
|
||
| spectra_f <- interaction(as.list( | ||
| spectraData(.spectra(object))[, | ||
| factorize.by, drop = FALSE]), | ||
| factorize.by, drop = FALSE]), | ||
| drop = TRUE, sep = "_") | ||
|
|
||
| cd <- .chromData(object) | ||
| if (nrow(cd)) { | ||
| if (!all(factorize.by %in% chromVariables(object))) | ||
| stop("All 'factorize.by' variables must be in chromData.") | ||
| cd$chromSpectraIndex <- interaction(cd[, factorize.by, | ||
| drop = FALSE], | ||
| drop = TRUE, sep = "_") | ||
| levels(spectra_f) <- levels(cd$chromSpectraIndex) | ||
| object@spectra$chromSpectraIndex <- droplevels(spectra_f) | ||
| object@chromData <- .ensure_rt_mz_columns(cd, | ||
| .spectra(object), | ||
| spectra_f) | ||
| } else { | ||
| object@spectra$chromSpectraIndex <- spectra_f | ||
| full_sp <- do.call(rbindFill, | ||
| lapply(split(.spectra(object), spectra_f), | ||
| .spectra_format_chromData)) | ||
| rownames(full_sp) <- NULL | ||
| object@chromData <- full_sp | ||
| } | ||
| object | ||
| cd <- .chromData(object) | ||
|
|
||
philouail marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (nrow(cd)) { | ||
| ## chromData exists: validate and align spectra to it | ||
| if (!all(factorize.by %in% chromVariables(object))) | ||
| stop("All 'factorize.by' variables must be in chromData.") | ||
|
|
||
| cd$chromSpectraIndex <- interaction(cd[, factorize.by, | ||
| drop = FALSE], | ||
| drop = TRUE, sep = "_") | ||
|
|
||
| object@spectra$chromSpectraIndex <- factor(as.character(spectra_f), | ||
| levels = levels(cd$chromSpectraIndex)) | ||
|
|
||
| sorted_spectra <- .spectra(object)[object@spectraSortIndex] | ||
| sorted_spectra_f <- spectra_f[object@spectraSortIndex] | ||
|
|
||
| object@chromData <- .ensure_rt_mz_columns(cd, | ||
| sorted_spectra, | ||
| sorted_spectra_f) | ||
| } else { | ||
| ## chromData is empty: create it from spectra | ||
| object@spectra$chromSpectraIndex <- spectra_f | ||
| full_sp <- do.call(rbindFill, | ||
| lapply(split(.spectra(object), spectra_f), | ||
| .spectra_format_chromData)) | ||
| rownames(full_sp) <- NULL | ||
| object@chromData <- full_sp | ||
| } | ||
|
|
||
| object@spectraSortIndex <- order( | ||
| object@spectra$dataOrigin, | ||
| object@spectra$rtime | ||
| ) | ||
|
|
||
| object | ||
| }) | ||
|
|
||
| #' @rdname hidden_aliases | ||
|
|
@@ -276,9 +309,11 @@ setMethod( | |
| } | ||
| ## Ensure chromSpectraIndex only contains relevant levels needed | ||
| valid_f <- chromSpectraIndex(object) | ||
| current_vals <- as.character(.spectra(object)$chromSpectraIndex) | ||
| ## Apply the sort index to spectra for processing | ||
| sorted_spectra <- .spectra(object)[object@spectraSortIndex] | ||
philouail marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| current_vals <- as.character(sorted_spectra$chromSpectraIndex) | ||
| if (!setequal(unique(current_vals), levels(valid_f))) { | ||
| object@spectra$chromSpectraIndex <- factor( | ||
| sorted_spectra$chromSpectraIndex <- factor( | ||
| current_vals, | ||
| levels = levels(valid_f) | ||
| ) | ||
|
|
@@ -287,8 +322,8 @@ setMethod( | |
| pd <- mapply(.process_peaks_data, | ||
| cd = split(chromData(object), valid_f), | ||
| s = split( | ||
| .spectra(object), | ||
| .spectra(object)$chromSpectraIndex | ||
| sorted_spectra, | ||
| sorted_spectra$chromSpectraIndex | ||
| ), | ||
| MoreArgs = list( | ||
| columns = columns, | ||
|
|
@@ -323,11 +358,38 @@ setMethod( | |
|
|
||
| #' @rdname hidden_aliases | ||
| #' @importMethodsFrom S4Vectors [ [[ | ||
| #' @importFrom MsCoreUtils i2index | ||
| #' @export | ||
| setMethod("[", "ChromBackendSpectra", function(x, i, j, ...) { | ||
| if (!length(i)) | ||
| return(ChromBackendSpectra()) | ||
| callNextMethod() | ||
|
|
||
| i <- i2index(i, length = length(x)) | ||
|
|
||
| ## Subset chromData and peaksData via parent method | ||
| x@chromData <- .chromData(x)[i, , drop = FALSE] | ||
| x@peaksData <- .peaksData(x)[i] | ||
|
|
||
| ## Determine which spectra to keep based on chromSpectraIndex | ||
| kept_indices <- chromSpectraIndex(x)[i] | ||
| spectra_keep <- x@spectra$chromSpectraIndex %in% kept_indices | ||
|
|
||
| ## Subset the spectra object | ||
| x@spectra <- x@spectra[spectra_keep] | ||
|
|
||
| ## Update spectraSortIndex to reflect the new ordering after subsetting | ||
| old_positions_kept <- which(spectra_keep) | ||
| mapping <- match(old_positions_kept, seq_along(spectra_keep)[spectra_keep]) | ||
philouail marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| kept_sort_positions <- x@spectraSortIndex %in% old_positions_kept | ||
| x@spectraSortIndex <- mapping[match(x@spectraSortIndex[kept_sort_positions], | ||
| old_positions_kept)] | ||
|
|
||
| ## Ensure chromSpectraIndex levels are still consistent | ||
| x@chromData$chromSpectraIndex <- droplevels(x@chromData$chromSpectraIndex) | ||
| x@spectra$chromSpectraIndex <- droplevels(x@spectra$chromSpectraIndex) | ||
|
|
||
| x | ||
| }) | ||
|
|
||
| #' @rdname hidden_aliases | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.