Skip to content

Commit

Permalink
Add align_dt to align pow and add ggplotly to vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
lalo-caballero committed Feb 7, 2024
1 parent 1674684 commit 1ff18e7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 51 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Suggests:
knitr,
labeling,
plotly (>= 4.10.2),
pow,
rmarkdown,
scales,
testthat (>= 3.0.0),
Expand All @@ -117,4 +118,3 @@ RoxygenNote: 7.2.3
VignetteBuilder: knitr
Config/testthat/edition: 3
Roxygen: list(markdown = TRUE)

13 changes: 12 additions & 1 deletion R/align_pow-GCIMSDataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#'
#' Uses the POW algorithm to generate waprs and align the samples
#'
#' To use this function you need to install the "pow" package hosted on github, to do this run he next line:
#' remotes::install_github("sipss/pow"),
#' also you will need the "pracma" package hosted in CRAN
#'
#' @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
Expand All @@ -26,7 +30,7 @@ methods::setMethod(
iv <- seq(2, m - 1, by = p)
v[iv] <- 0
W <- Matrix::Diagonal(x = v)
val<-pow::compute_val_error(X,y,W,iv,lambdas)
val<-pow::compute_val_error(X, y, W, iv, lambdas)
opar <- pow::optimize_params(n_samples, lambdas, val$ti_ix, val$e_ix)
best_lambdas2 <- opar$best_params

Expand All @@ -35,9 +39,16 @@ methods::setMethod(
w<-append(w,list(1:m),idx_y-1)
names(w)<-object$sampleNames

tis_matrix <- getTIS(object)
dt <- dtime(object)
rip_position <- apply(tis_matrix, 1L, which.max)
rip_ref_idx <- round(stats::median(rip_position, na.rm = TRUE))
rip_ref_ms <- dt[rip_ref_idx]

delayed_op <- DelayedOperation(
name = "align_pow",
fun = align_pow,
params = list(rip_ref_ms = rip_ref_ms),
params_iter = list(w=w)
)
object$appendDelayedOp(delayed_op)
Expand Down
8 changes: 7 additions & 1 deletion R/align_pow-GCIMSSample.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
methods::setMethod(
"align_pow",
"GCIMSSample",
function(object,w){
function(object, rip_ref_ms, w){
object <- alignDt(object, rip_ref_ms = rip_ref_ms)
dt_range <- c(
object@proc_params$align$dt_min_ms,
object@proc_params$align$dt_max_ms
)
object <- subset(object, dt_range = dt_range)
int <- GCIMS::intensity(object)
inter <- t(apply(int, 1, pow::interpolation, w = w, return = FALSE))
sel <- !is.na(inter[1,])
Expand Down
4 changes: 4 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.

2 changes: 0 additions & 2 deletions vignettes/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
*.html
*.R
Rplots.pdf
*.log
GCIMSDataset_demo1_
sample1.csv
sample2.csv
69 changes: 23 additions & 46 deletions vignettes/introduction-to-gcims.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,9 @@ to_plot <- dplyr::bind_rows(
Smoothed = as.data.frame(one_ims_smoothed),
.id = "Status"
)
ggplot(to_plot) +
geom_line(aes(x = drift_time_ms, y = intensity, colour = Status)) +
coord_cartesian(xlim = c(7, 10)) +
labs(x = "Drift time (ms)", y = "Intensity (a.u.)")
ggplot(to_plot) +
plotly::ggplotly(ggplot(to_plot) +
geom_line(aes(x = drift_time_ms, y = intensity, colour = Status)) +
coord_cartesian(xlim = c(7, 10), ylim = c(0, 300)) +
labs(x = "Drift time (ms)", y = "Intensity (a.u.)")
labs(x = "Drift time (ms)", y = "Intensity (a.u.)"))
```

```{r}
Expand All @@ -213,13 +208,9 @@ to_plot <- dplyr::bind_rows(
Smoothed = as.data.frame(one_chrom_smoothed),
.id = "Status"
)
ggplot(to_plot) +
geom_line(aes(x = retention_time_s, y = intensity, colour = Status)) +
labs(x = "Retention time (s)", y = "Intensity (a.u.)")
ggplot(to_plot) +
plotly::ggplotly(ggplot(to_plot) +
geom_line(aes(x = retention_time_s, y = intensity, colour = Status)) +
coord_cartesian(xlim = c(200, 250)) +
labs(x = "Retention time (s)", y = "Intensity (a.u.)")
labs(x = "Retention time (s)", y = "Intensity (a.u.)"))
```

You can also apply it to a single sample:
Expand Down Expand Up @@ -392,15 +383,15 @@ ket1 <- findPeaks(
rt_peakDetectionCWTParams = list(exclude0scaleAmpThresh = TRUE),
dt_extension_factor = 0,
rt_extension_factor = 0,
exclude_rip = FALSE,
exclude_rip = TRUE,
iou_overlap_threshold = 0.2
)
```

```{r}
peak_list_ket1 <- peaks(ket1)
plot(ket1) +
overlay_peaklist(peak_list_ket1, color_by = "PeakID")
plotly::ggplotly(plot(ket1) +
overlay_peaklist(peak_list_ket1, color_by = "PeakID"))
```

Then do it on the whole dataset:
Expand Down Expand Up @@ -429,20 +420,16 @@ ket2 <- dataset$getSample("Ketones2")
```

```{r}
plot(ket2) +
overlay_peaklist(peaks(ket2)) +
lims(x = c(7.5,15), y = c(0, 500))
plotly::ggplotly(plot(ket2) +
overlay_peaklist(peaks(ket2)) )
```

Or plot all the peaks from all the dataset together, overlayed on a single sample:

```{r}
plt <- plot(ket2) +
overlay_peaklist(peaks(dataset), color_by = "SampleID")
# We could explore the plot interactively:
# plot_interactive(plt)
# Or use a static version to save memory and render it faster:
plt
plotly::ggplotly(plt)
```

# Clustering
Expand All @@ -464,18 +451,16 @@ peak_list_clustered <- peak_clustering$peak_list_clustered
```


```{r fig.height=10, fig.width=10}
```{r }
plt <- plot(ket2) +
overlay_peaklist(peak_list_clustered, color_by = "SampleID")
print(plt)
print(plt + lims(x = c(6, 15), y = c(0, 400)))
plotly::ggplotly(plt)
```

```{r}
plt <- plot(ket2) +
overlay_peaklist(dplyr::filter(peak_list_clustered, !is.na(cluster)), color_by = "cluster")
print(plt)
print(plt + lims(x = c(6, 15), y = c(0, 400)))
plotly::ggplotly(plt)
```

The resulting cluster sizes (median position of individual clusters) is not a good reference
Expand All @@ -484,8 +469,7 @@ for integration. We are working on this.
```{r}
plt <- plot(ket2) +
overlay_peaklist(peak_clustering$cluster_stats, color_by = "cluster")
#plot_interactive(plt)
plt + lims(x = c(6, 15), y = c(0, 400))
plotly::ggplotly(plt)
```

# Baseline correction
Expand All @@ -510,21 +494,12 @@ to_plot <- data.frame(
Intensity = intensity(one_ims_spec),
Baseline = baseline(one_ims_spec)
)
ggplot(to_plot) +
plotly::ggplotly(ggplot(to_plot) +
geom_line(aes(x = drift_time_ms, y = Intensity), color = "red") +
geom_line(aes(x = drift_time_ms, y = Baseline), color = "blue") +
labs(x = "Drift time (ms)", y = "Intensity (a.u.)")
labs(x = "Drift time (ms)", y = "Intensity (a.u.)"))
```

```{r }
ggplot(to_plot) +
geom_line(aes(x = drift_time_ms, y = Intensity), color = "red") +
geom_line(aes(x = drift_time_ms, y = Baseline), color = "blue") +
coord_cartesian(xlim = c(7, 10), ylim = c(0, 500)) +
labs(x = "Drift time (ms)", y = "Intensity (a.u.)")
```


```{r}
one_chrom <- getChromatogram(ket1, dt_range = 10.4)
```
Expand Down Expand Up @@ -564,7 +539,7 @@ ket1_basline <- estimateBaseline(

```{r}
cowplot::plot_grid(
plot(ket1_no_basline, rt_range = c(0, 500), dt_range = c(6, 16)),
plot(x = ket1_no_basline, rt_range = c(0, 500), dt_range = c(6, 16)),
plot(x = ket1_basline, rt_range = c(0, 500), dt_range = c(6, 16)),
ncol = 2
)
Expand All @@ -586,7 +561,7 @@ dataset$realize()

Some clusters:

```{r fig.height=10, fig.width=10}
```{r}
plot(ket2) +
overlay_peaklist(
peak_clustering$peak_list_clustered,
Expand Down Expand Up @@ -626,14 +601,16 @@ peak_list <- peaks(dataset)

```{r}
peak_table <- peakTable(peak_list, aggregate_conflicting_peaks = max)
tail(peak_table$peak_table_matrix)
t(tail(t(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)
tail(peak_table_imputed)
peak_table_imputed <- imputePeakTable(peak_table$peak_table_matrix,
dataset,
peak_clustering$cluster_stats)
t(tail(t(peak_table_imputed)))
```
As we can see the "NA" values were imputed

Expand Down

0 comments on commit 1ff18e7

Please sign in to comment.