--- title: "Computing Dynamic SWATH Windows" author: "Witold E. Wolski, Bernd Roschitzki" date: "`r Sys.Date()`" package: "`r packageVersion('prozor')`" vignette: > %\VignetteIndexEntry{Computing Dynamic SWATH Windows} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} output: rmarkdown::html_vignette editor_options: chunk_output_type: console --- # Introduction This vignette computes precursor isolation windows for DIA (SWATH) that each hold about the same number of precursors. The input is a vector of precursor m/z values (MS1 masses), e.g. from a spectral library or a non-redundant BiblioSpec `.blib` file. `Cdsw$new()` computes the window boundaries (breaks) once, with one of three methods: `"constant"`, `"quantile"` or `"mixture"` (the default). The object cannot be changed afterwards, so we create one object per method and compare them at the end. # Prerequisites ```{r} library(prozor) library(ggplot2) ``` # Constant width method ```{r} data("masses") cdsw_const <- Cdsw$new(masses, n_bins = 25, method = "constant") cdsw_const cdsw_const$plot() ``` ```{r, results='markup'} knitr::kable(cdsw_const$as_table()) ``` ```{r} const_error <- cdsw_const$error() ``` # Classical Method based on `quantile` Same number of MS1 precursors in each window ```{r} cdsw_quantile <- Cdsw$new(masses, n_bins = 25, method = "quantile") cdsw_quantile ``` ```{r} cdsw_quantile$plot() ``` ```{r, results='markup'} knitr::kable(cdsw_quantile$as_table()) ``` ```{r} quantile_error <- cdsw_quantile$error() ``` # Adjust windows Using this method the window start and end is shifted to a mass range with as few MS1 peaks as possible. ```{r fig.width=7, fig.height=4} optimized <- cdsw_quantile$optimize_windows(min_count = 10) knitr::kable(optimized) plot_windows(optimized, masses) ``` # Dynamic Swath Windows with Constraints. - Maximal window width (`max_window`). Windows should not be too large because of optimal collision energy (personal communication by Bernd R.). - Minimal window width (`min_window`). - Number of windows (`n_bins`). - Window boundaries can then be moved to regions with few precursors (`optimize_windows()`). `max_window`, `min_window` and `n_bins` are arguments of `Cdsw$new()`. `method = "mixture"` mixes the masses with uniformly spread data and takes the quantile breaks of the mixture, with the smallest share of uniform data for which all windows lie within `min_window` and `max_window`. `plot_breaks()` compares the resulting breaks with the constant and quantile breaks. ```{r} cdsw_mixed <- Cdsw$new(masses, n_bins = 25, method = "mixture", max_window = 100) cdsw_mixed cdsw_mixed$plot_breaks() ``` ```{r} cdsw_mixed$plot() ``` ```{r, results='markup'} knitr::kable(cdsw_mixed$as_table()) ``` ```{r fig.width=7, fig.height=4} optimized <- cdsw_mixed$optimize_windows(min_count = 10) knitr::kable(optimized) plot_windows(optimized, masses) ``` ```{r} mixed_error <- cdsw_mixed$error() ``` # Benchmarking of the methods. We compare the optimal number of MS1 peaks per SWATH window (same in each window) with the numbers obtained by using all of the 3 methods implemented. ```{r fig.width=6, fig.height=3} errors <- data.frame( method = factor(rep(c("constant", "quantile", "mixture"), 2), levels = c("constant", "quantile", "mixture")), metric = rep(c("Manhattan distance", "Euclidean distance"), each = 3), value = c( const_error$manhattan, quantile_error$manhattan, mixed_error$manhattan, const_error$euclidean, quantile_error$euclidean, mixed_error$euclidean ) ) ggplot(errors, aes(x = method, y = value)) + geom_col(fill = "#0072B2") + geom_text(aes(label = signif(value, 2)), vjust = -0.3, size = 3) + facet_wrap(~metric, scales = "free_y") + labs(x = NULL, y = "deviation from equal counts") ``` The error of the `"mixture"` windows lies between the two others: it is smaller than that of the `"constant"` (equal-width) windows, and unlike the `"quantile"` windows the `"mixture"` windows meet the width limits. # Session info ```{r sessionInfo, echo=FALSE} sessionInfo() ```