diff --git a/R/align-GCIMSDataset.R b/R/align-GCIMSDataset.R index a8a30c7..0f89793 100644 --- a/R/align-GCIMSDataset.R +++ b/R/align-GCIMSDataset.R @@ -9,7 +9,7 @@ setMethod( "align", "GCIMSDataset", - function(object) { + function(object, method = "pow", shift_ip = TRUE) { tis_matrix <- getTIS(object) ric_matrix <- getRIC(object) dt <- dtime(object) @@ -19,7 +19,9 @@ setMethod( dt = dt, rt = rt, tis_matrix = tis_matrix, - ric_matrix = ric_matrix + ric_matrix = ric_matrix, + method = method, + shift_ip = shift_ip ) delayed_op <- DelayedOperation( @@ -70,30 +72,14 @@ setMethod( ds } -#' Find the best retention time reference chromatogram. -#' @noRd -#' -#' @description This function provides the index corresponding to the reference Reactant Ion Chromatogram (RIC) to correct -#' misalignments in retention time. -#' @param rics A matrix. Each row correspond to a different RIC. There are as many RICs as samples. -#' @return An Integer number that indicates the reference sample. -#' @examples -#' rics <- rbind( -#' dnorm(1:100, mean=50, sd =1), -#' dnorm(1:100, mean=51, sd =1), -#' dnorm(1:100, mean=52, sd =1) -#' ) -#' find_reference_ric(rics) == 2L -find_reference_ric <- function(rics){ - ref_ric_sample_idx <- ptw::bestref(rics)$best.ref - return(ref_ric_sample_idx) -} - - -alignParams <- function(dt, rt, tis_matrix, ric_matrix) { +alignParams <- function(dt, rt, tis_matrix, ric_matrix, method, shift_ip) { # Optimize ret time alignment parameters: - ref_ric_sample_idx <- find_reference_ric(ric_matrix) + if (method == "ptw"){ + ref_ric_sample_idx <- ptw::bestref(ric_matrix)$best.ref + } else { + ref_ric_sample_idx <- pow::select_reference(ric_matrix) + } # Select reference RIC ric_ref <- as.numeric(ric_matrix[ref_ric_sample_idx, ]) @@ -102,7 +88,18 @@ alignParams <- function(dt, rt, tis_matrix, ric_matrix) { rip_ref_idx <- round(stats::median(rip_position, na.rm = TRUE)) rip_ref_ms <- dt[rip_ref_idx] - list(rip_ref_ms = rip_ref_ms, ric_ref = ric_ref, ric_ref_rt = rt) + # align ip params + + mins <- apply(ric_matrix, 1,which.min) + rt_ref <- rt[1 : (length(rt) - (max(mins) - min(mins)))] + min_start <- min(mins) - 1 + + list(rip_ref_ms = rip_ref_ms, + ric_ref = ric_ref, + ric_ref_rt = rt, + min_start = min_start, + rt_ref = rt_ref, + shift_ip = shift_ip) } #' Plots to interpret alignment results diff --git a/R/align-GCIMSSample.R b/R/align-GCIMSSample.R index 4d2bd3f..d743da3 100644 --- a/R/align-GCIMSSample.R +++ b/R/align-GCIMSSample.R @@ -7,7 +7,7 @@ #' @export methods::setMethod( "align", "GCIMSSample", - function(object, rip_ref_ms, ric_ref, ric_ref_rt){ + function(object, rip_ref_ms, ric_ref, ric_ref_rt, min_start, rt_ref, shift_ip){ if (all(is.na(object@data))) { cli_abort("All the data matrix of {description(object)} are missing values. Align is impossible") } @@ -15,7 +15,18 @@ methods::setMethod( if (all(is.na(object@data))) { cli_abort("After aligning drift times, all the data matrix of {description(object)} are missing values. This should not happen") } - object <- alignRt(object, ric_ref = ric_ref, ric_ref_rt = ric_ref_rt) + if (shift_ip){ + ric <- getRIC(object) + injection_point <- which.min(ric) + object@retention_time <- rt_ref + object@data <- object@data[, (injection_point - min_start):((injection_point - min_start)+length(rt_ref)-1)] + } + if (method == "ptw") { + object <- alignRt_ptw(object, ric_ref = ric_ref, ric_ref_rt = ric_ref_rt) + } else { + ####object <- alignRt_pow + } + if (all(is.na(object@data))) { cli_abort("After aligning drift and retention times, all the data matrix of {description(object)} are missing values. This should not happen") } @@ -100,7 +111,7 @@ methods::setMethod( #' @importMethodsFrom ProtGenerics alignRt #' @export methods::setMethod( - "alignRt", + "alignRt_ptw", signature = c(x = "GCIMSSample", y = "ANY"), function(x, y, ric_ref, ric_ref_rt) { optimize_polynomial_order <- function(ric_sample, ric_ref) { diff --git a/R/align_ip-GCIMSDataset.R b/R/align_ip-GCIMSDataset.R deleted file mode 100644 index 8f292c1..0000000 --- a/R/align_ip-GCIMSDataset.R +++ /dev/null @@ -1,31 +0,0 @@ -#' Align a GCIMS dataset to the injection point -#' -#' Aligns all the samples to their injection point in retention time -#' -#' @param object A [GCIMSDataset] object, modified in-place -#' @return The modified [GCIMSDataset] -#' @export -methods::setMethod( - "align_ip", - "GCIMSDataset", - function(object){ - rics <- GCIMS::getRIC(object) - mins <- apply(rics, 1,which.min) - rt_ref <- GCIMS::rtime(object, which.min(mins)) - rt_ref <- rt_ref[1 : (length(rt_ref) - (max(mins) - min(mins)))] - min_start <- min(mins) - 1 - delayed_op <- DelayedOperation( - name = "align_injection_point", - fun = align_ip, - params = list(min_start = min_start, rt_ref = rt_ref) - ) - object$appendDelayedOp(delayed_op) - object$extract_dtime_rtime() - object <- GCIMS:::extract_RIC_and_TIS(object) - invisible(object) - } -) - - - - diff --git a/R/align_ip-GCIMSSample.R b/R/align_ip-GCIMSSample.R deleted file mode 100644 index ab6467b..0000000 --- a/R/align_ip-GCIMSSample.R +++ /dev/null @@ -1,20 +0,0 @@ -#' Align a GCIMS Sample to the injection point -#' -#' Aligns a sample to the reference injection point -#' -#' @param object A [GCIMSSample] object, modified in-place -#' @param min_start The points that will be used before the injection point -#' @param rt_ref Retention time vector to be used -#' @return The modified [GCIMSSample] -#' @export -methods::setMethod( - "align_ip", - "GCIMSSample", - function(object, min_start, rt_ref){ - ric <- GCIMS::getRIC(object) - injection_point <- which.min(ric) - object@retention_time <- rt_ref - object@data <- object@data[, (injection_point - min_start):((injection_point - min_start)+length(rt_ref)-1)] - return(object) - } -)