119
119
# ' @param BPPARAM Parallel setup configuration. See [BiocParallel::bpparam()]
120
120
# ' for more information.
121
121
# '
122
+ # ' @param by A character vector specifying the column names in both
123
+ # ' `peak_table` and the `chromData` of `object` that uniquely identify
124
+ # ' chromatograms. The combination of these columns must be unique in the
125
+ # ' `chromData`. Can also be of length 1
126
+ # '
122
127
# ' @param columns For `chromData()` accessor: optional `character` with column
123
128
# ' names (chromatogram variables) that should be included in the
124
129
# ' returned `data.frame`. By default, all columns are returned.
148
153
# '
149
154
# ' @param object Object extending `ChromBackend`.
150
155
# '
156
+ # ' @param peak_table A data frame containing the following minimum columns:
157
+ # ' - "rtMin": Minimum retention time for each peak. Cannot be NA
158
+ # ' - "rtMax": Maximum retention time for each peak. Cannot be NA
159
+ # '
160
+ # ' Additionally, the `peak_table` must include columns that uniquely identify
161
+ # ' chromatograms in the `object`. Common choices are "chromIndex" and/or
162
+ # ' "dataOrigin". These columns must also be present in the `chromData` of the
163
+ # ' `object`.
164
+ # '
151
165
# ' @param ranges For `filterChromData()` : a `numeric`
152
166
# ' vector of paired values (upper and lower boundary) that define the
153
167
# ' ranges to filter the `object`. These paired values need to be in the
273
287
# ' available in `object`. Variables listed by this function are expected to
274
288
# ' be returned (if requested) by the `chromData()` function.
275
289
# '
290
+ # ' - `chromExtract()`: return A new `Chrombackend` object containing separated
291
+ # ' peaks as individual chromatograms.
292
+ # '
276
293
# ' - `collisionEnergy()`, `collisionEnergy<-`: gets or sets the collision
277
294
# ' energy for the precursor (for SRM data). `collisionEnergy()` returns a
278
295
# ' `numeric` of length equal to the number of chromatograms in `object`.
473
490
NULL
474
491
475
492
setClass ("ChromBackend ",
476
- contains = " VIRTUAL" ,
477
- slots = c(
478
- version = " character"
479
- ),
480
- prototype = prototype(readonly = FALSE , version = " 0.1" )
493
+ contains = " VIRTUAL" ,
494
+ slots = c(
495
+ version = " character"
496
+ ),
497
+ prototype = prototype(readonly = FALSE , version = " 0.1" )
481
498
)
482
499
483
500
# ' @importMethodsFrom S4Vectors $ $<-
@@ -601,8 +618,8 @@ setReplaceMethod("[[", "ChromBackend", function(x, i, j, ..., value) {
601
618
# '
602
619
# ' @export
603
620
setMethod ("backendBpparam ",
604
- signature = " ChromBackend" ,
605
- function (object , BPPARAM = bpparam()) BPPARAM
621
+ signature = " ChromBackend" ,
622
+ function (object , BPPARAM = bpparam()) BPPARAM
606
623
)
607
624
608
625
# ' @exportMethod backendInitialize
@@ -613,11 +630,11 @@ setMethod("backendBpparam",
613
630
# '
614
631
# ' @rdname ChromBackend
615
632
setMethod ("backendInitialize ",
616
- signature = " ChromBackend" ,
617
- definition = function (object , ... ) {
618
- validObject(object )
619
- object
620
- }
633
+ signature = " ChromBackend" ,
634
+ definition = function (object , ... ) {
635
+ validObject(object )
636
+ object
637
+ }
621
638
)
622
639
623
640
# ' @rdname ChromBackend
@@ -1024,28 +1041,28 @@ setMethod("split", "ChromBackend", function(x, f, drop = FALSE, ...) {
1024
1041
setMethod(
1025
1042
" filterChromData" , " ChromBackend" ,
1026
1043
function (object , variables = character (),
1027
- ranges = numeric (), match = c(" any" , " all" ),
1028
- keep = TRUE ) {
1044
+ ranges = numeric (), match = c(" any" , " all" ),
1045
+ keep = TRUE ) {
1029
1046
if (! length(variables ) || ! length(ranges ))
1030
1047
return (object )
1031
1048
if (! is.numeric(ranges ))
1032
1049
stop(" filterChromData only support filtering for numerical " ,
1033
- " 'variables'" )
1050
+ " 'variables'" )
1034
1051
match <- match.arg(match )
1035
1052
if (is.character(variables )) {
1036
1053
if (! all(variables %in% chromVariables(object )))
1037
1054
stop(" One or more values passed with parameter " ,
1038
- " 'variables' are not available as chromatogram " ,
1039
- " variables in object. Use the 'chromVariables()' " ,
1040
- " function to list possible values." )
1055
+ " 'variables' are not available as chromatogram " ,
1056
+ " variables in object. Use the 'chromVariables()' " ,
1057
+ " function to list possible values." )
1041
1058
} else
1042
1059
stop(" The 'variables' parameter needs to be of type " ,
1043
- " 'character'." )
1060
+ " 'character'." )
1044
1061
if (length(variables ) != length(ranges ) / 2 )
1045
1062
stop(" Length of 'ranges' needs to be twice the length of " ,
1046
- " the parameter 'variables' and define the lower " ,
1047
- " and upper bound for values of each chromatogram " ,
1048
- " variable defined with parameter 'variables'." )
1063
+ " the parameter 'variables' and define the lower " ,
1064
+ " and upper bound for values of each chromatogram " ,
1065
+ " variable defined with parameter 'variables'." )
1049
1066
query <- chromData(object , columns = variables )
1050
1067
idx <- .filter_ranges(query , ranges , match )
1051
1068
if (keep ) return (object [idx ])
@@ -1063,41 +1080,37 @@ setMethod(
1063
1080
# ' @description
1064
1081
# ' Filter the peak data based on the provided ranges for the given variables.
1065
1082
# '
1066
- # ' @note This function replaces the peaksData() of the input object. Therefore
1067
- # ' backend with `readOnly == TRUE` (i.e. ChromBackendmzR) will need to have a
1068
- # ' carefully implemented `peaksData(object) <-` method.
1069
- # '
1070
1083
# ' @export
1071
1084
setMethod(
1072
1085
" filterPeaksData" , " ChromBackend" ,
1073
1086
function (object , variables = character (),
1074
- ranges = numeric (), match = c(" any" , " all" ),
1075
- keep = TRUE ) {
1087
+ ranges = numeric (), match = c(" any" , " all" ),
1088
+ keep = TRUE ) {
1076
1089
if (! length(ranges ) || ! length(variables ))
1077
1090
return (object )
1078
1091
if (! is.numeric(ranges ))
1079
1092
stop( " filterPeaksData only support filtering for " ,
1080
- " numerical peak variables" )
1093
+ " numerical peak variables" )
1081
1094
match <- match.arg(match )
1082
1095
if (is.character(variables )) {
1083
1096
if (! all(variables %in% peaksVariables(object )))
1084
1097
stop(" One or more values passed with parameter " ,
1085
- " 'variables' are not available as peaks " ,
1086
- " variables in object. Use the 'peaksVariables()' " ,
1087
- " function to list possible values." )
1098
+ " 'variables' are not available as peaks " ,
1099
+ " variables in object. Use the 'peaksVariables()' " ,
1100
+ " function to list possible values." )
1088
1101
} else
1089
1102
stop(" The 'variables' parameter needs to be of type " ,
1090
- " 'character'." )
1103
+ " 'character'." )
1091
1104
if (length(variables ) != length(ranges ) / 2 )
1092
1105
stop(" Length of 'ranges' needs to be twice the length of " ,
1093
- " the parameter 'variables' and define the lower and " ,
1094
- " upper bound for values of each peak variable " ,
1095
- " defined with parameter 'variables'." )
1106
+ " the parameter 'variables' and define the lower and " ,
1107
+ " upper bound for values of each peak variable " ,
1108
+ " defined with parameter 'variables'." )
1096
1109
if (keep ) sel_fun <- function (z , idx ) z [idx , , drop = FALSE ]
1097
1110
else sel_fun <- function (z , idx ) {
1098
- if (! length(idx )) return (z )
1099
- else return (z [- idx , , drop = FALSE ])
1100
- }
1111
+ if (! length(idx )) return (z )
1112
+ else return (z [- idx , , drop = FALSE ])
1113
+ }
1101
1114
peaksData(object ) <- lapply(peaksData(object ), function (pd ) {
1102
1115
sel_fun(pd , .filter_ranges(
1103
1116
pd [, variables , drop = FALSE ],
@@ -1113,5 +1126,73 @@ setMethod(
1113
1126
# ' @exportMethod supportsSetBackend
1114
1127
# '
1115
1128
# ' @rdname ChromBackend
1116
- # ' @export
1117
1129
setMethod ("supportsSetBackend ", "ChromBackend", function(object, ...) FALSE)
1130
+
1131
+ # ' @rdname ChromBackend
1132
+ # ' @exportMethod imputePeaksData
1133
+ # ' @param method character: Imputation method ("linear", "spline",
1134
+ # ' "gaussian", "loess")
1135
+ # ' @param span `numeric`, for the loess method: Smoothing parameter (only used
1136
+ # ' if method == "loess")
1137
+ # ' @param sd `numeric`, for the gaussian method: Standard deviation for
1138
+ # ' Gaussian kernel (only for method == "gaussian")
1139
+ # ' @param window `integer`, for the gaussian method: Half-width of Gaussian
1140
+ # ' kernel window (e.g., 2 gives window size 5)
1141
+ # ' @importFrom stats approx filter loess
1142
+ setMethod ("imputePeaksData ", signature(object = "ChromBackend"),
1143
+ function (object ,
1144
+ method = c(" linear" , " spline" , " gaussian" , " loess" ),
1145
+ span = 0.3 ,
1146
+ sd = 1 ,
1147
+ window = 2 ,
1148
+ ... ) {
1149
+ method <- match.arg(method )
1150
+ impute <- function (x ) {
1151
+ if (all(is.na(x ))) return (x )
1152
+
1153
+ na_idx <- which(is.na(x ))
1154
+ if (length(na_idx ) == 0 ) return (x )
1155
+
1156
+ not_na_idx <- which(! is.na(x ))
1157
+ x_out <- seq_along(x )
1158
+
1159
+ x [na_idx ] <- switch (method ,
1160
+ linear = approx(not_na_idx , x [not_na_idx ],
1161
+ xout = na_idx , rule = 2 )$ y ,
1162
+
1163
+ spline = spline(not_na_idx , x [not_na_idx ],
1164
+ xout = na_idx , method = " natural" )$ y ,
1165
+ gaussian = {
1166
+ # Create symmetric Gaussian kernel
1167
+ kernel_range <- - window : window
1168
+ w <- dnorm(kernel_range , mean = 0 , sd = sd )
1169
+ w <- w / sum(w )
1170
+
1171
+ # Fill missing with linear approx to allow smoothing
1172
+ x_filled <- x
1173
+ x_filled [is.na(x_filled )] <- approx(not_na_idx , x [not_na_idx ],
1174
+ xout = which(is.na(x_filled )),
1175
+ rule = 2 )$ y
1176
+ smoothed <- filter(x_filled , filter = w , sides = 2 , circular = FALSE )
1177
+ smoothed [na_idx ]
1178
+ },
1179
+
1180
+ loess = {
1181
+ fit <- loess(x [not_na_idx ] ~ not_na_idx , span = span )
1182
+ predict(fit , newdata = na_idx )
1183
+ }
1184
+ )
1185
+ x
1186
+ }
1187
+ object $ intensity <- lapply(object $ intensity , impute )
1188
+ object
1189
+ })
1190
+
1191
+ # ' @rdname ChromBackend
1192
+ # ' @exportMethod chromExtract
1193
+ # '
1194
+ setMethod ("chromExtract ", "ChromBackend",
1195
+ function (object , by = character (), drop = FALSE ) {
1196
+ stop(" Not implemented for " , class(object ), " ." )
1197
+ }
1198
+ )
0 commit comments