Skip to content

Commit

Permalink
Align pow as method
Browse files Browse the repository at this point in the history
  • Loading branch information
lalo-caballero committed Jan 25, 2024
1 parent 66d41a6 commit 8f2aff3
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 46 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export(alignDt)
export(alignPlots)
export(alignRt)
export(align_ip)
export(align_pow)
export(baseline)
export(clusterPeaks)
export(create_annotations_table)
Expand Down Expand Up @@ -72,6 +73,7 @@ exportMethods(align)
exportMethods(alignDt)
exportMethods(alignRt)
exportMethods(align_ip)
exportMethods(align_pow)
exportMethods(baseline)
exportMethods(decimate)
exportMethods(description)
Expand Down
10 changes: 9 additions & 1 deletion R/aaa-AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,22 @@ setGeneric("decimate", function(object, ...) standardGeneric("decimate"))
#' @export
setGeneric("align", function(object, ...) standardGeneric("align"))

#' @describeIn GCIMS-generics Align to injection point an object
#' @describeIn GCIMS-generics Align to injection point in retention time
#'
#' @param object An object to align
#' @param ... Further arguments, possibly used by downstream methods.
#' @return The object, modified
#' @export
setGeneric("align_ip", function(object, ...) standardGeneric("align_ip"))

#' @describeIn GCIMS-generics Align in retention time with POW algorithm
#'
#' @param object An object to align
#' @param ... Further arguments, possibly used by downstream methods.
#' @return The object, modified
#' @export
setGeneric("align_pow", function(object, ...) standardGeneric("align_pow"))

#' @describeIn GCIMS-generics Align an object in drift time
#'
#' @param x An object to align
Expand Down
4 changes: 2 additions & 2 deletions R/align_ip_GCIMSSample.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#'
#' Aligns a sample to the reference injection point
#'
#' @param object A [GCIMSDataset] object, modified in-place
#' @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 [GCIMSDataset]
#' @return The modified [GCIMSSample]
#' @export
methods::setMethod(
"align_ip",
Expand Down
75 changes: 45 additions & 30 deletions R/align_pow-GCIMSDataset.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
align_pow <- function(x,
lambdas = pracma::logspace(-2, 4, 31),
p = 10,
max_it = 1000){
rics <- x$getRIC()
idx_y <- Align::select_reference(rics)
X <- as.matrix(rics[-idx_y,])
y <- as.matrix(rics[idx_y,])
n_samples <- nrow(X)
m <- length(y)
v <- rep(1,m)
iv <- seq(2, m - 1, by = p)
v[iv] <- 0
W <- Matrix::Diagonal(x = v)
val<-Align::compute_val_error(X,y,W,iv,lambdas)
opar <- Align::optimize_params(n_samples, lambdas, val$ti_ix, val$e_ix)
best_lambdas2 <- opar$best_params
#' Align a [GCIMSDataset] using the POW algorithm
#'
#' Uses the POW algorithm to generate waprs and align the samples
#'
#' @param object A [GCIMSDataset] object, modified in-place
#' @param lambdas vector of lambdas to be tested for the POW algorithm
#' @param p points to be taken for validation for the POW algorithm
#' @param max_it maximum iterations for applying the POW algorithm
#' @return The modified [GCIMSDataset]
#' @export

w<-mapply(Align::pow,x=as.list(data.frame(t(X))), lambda2=as.list(best_lambdas2),MoreArgs = list(y=y,max_it = max_it))
w<-as.list((as.data.frame(w)))
w<-append(w,list(1:m),idx_y-1)
names(w)<-x$sampleNames
methods::setMethod(
"align_pow",
"GCIMSDataset",
function(object,
lambdas = pracma::logspace(-2, 4, 31),
p = 10,
max_it = 1000){
rics <- object$getRIC()
idx_y <- Align::select_reference(rics)
X <- as.matrix(rics[-idx_y,])
y <- as.matrix(rics[idx_y,])
n_samples <- nrow(X)
m <- length(y)
v <- rep(1,m)
iv <- seq(2, m - 1, by = p)
v[iv] <- 0
W <- Matrix::Diagonal(x = v)
val<-Align::compute_val_error(X,y,W,iv,lambdas)
opar <- Align::optimize_params(n_samples, lambdas, val$ti_ix, val$e_ix)
best_lambdas2 <- opar$best_params

delayed_op <- DelayedOperation(
name = "align_pow",
fun = align_pow_sample,
params_iter = list(w=w)
)
x$appendDelayedOp(delayed_op)
x <- GCIMS:::extract_RIC_and_TIS(x)
invisible(x)
}
w<-mapply(Align::pow,x=as.list(data.frame(t(X))), lambda2=as.list(best_lambdas2),MoreArgs = list(y=y,max_it = max_it))
w<-as.list((as.data.frame(w)))
w<-append(w,list(1:m),idx_y-1)
names(w)<-object$sampleNames

delayed_op <- DelayedOperation(
name = "align_pow",
fun = align_pow,
params_iter = list(w=w)
)
object$appendDelayedOp(delayed_op)
object <- GCIMS:::extract_RIC_and_TIS(object)
invisible(object)
}
)
29 changes: 21 additions & 8 deletions R/align_pow-GCIMSSample.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
align_pow_sample <- function(x,w){
int <- GCIMS::intensity(x)
inter <- t(apply(int, 1, Align::interpolation, w = w, return = FALSE))
sel <- !is.na(inter[1,])
x@retention_time <- x@retention_time[sel]
x@data <- inter[,sel]
return(x)
}
#' Warp a GCIMSSample
#'
#' Warps a GCIMSSample with the provided warp
#'
#' @param object A [GCIMSSample] object, modified in-place
#' @param w warp to be applied to the GCIMSSample
#' @return The modified [GCIMSSample]
#' @export

methods::setMethod(
"align_pow",
"GCIMSSample",
function(object,w){
int <- GCIMS::intensity(object)
inter <- t(apply(int, 1, Align::interpolation, w = w, return = FALSE))
sel <- !is.na(inter[1,])
object@retention_time <- object@retention_time[sel]
object@data <- inter[,sel]
return(object)
}
)
9 changes: 8 additions & 1 deletion man/GCIMS-generics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/align_ip-GCIMSSample-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/align_pow-GCIMSDataset-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/align_pow-GCIMSSample-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vignettes/introduction-to-gcims.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,14 @@ peak_list <- peaks(dataset)

```{r}
peak_table <- peakTable(peak_list, aggregate_conflicting_peaks = max)
peak_table$peak_table_matrix[,22:27]
tail(peak_table$peak_table_matrix)
```
We showed the last 6 clusters where we have "NA" Values

# Imputation
```{r}
peak_table_imputed <- imputePeakTable(peak_table$peak_table_matrix, dataset, peak_clustering$cluster_stats)
peak_table_imputed[,22:27]
tail(peak_table_imputed)
```
As we can see the "NA" values were imputed

Expand Down

0 comments on commit 8f2aff3

Please sign in to comment.