Computing Dynamic SWATH Windows

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

library(prozor)
library(ggplot2)

Constant width method

data("masses")
cdsw_const <- Cdsw$new(masses, n_bins = 25, method = "constant")
cdsw_const
## <Cdsw> 146,009 masses in 25 windows, method "constant"
## window widths 33.99 to 34.00 m/z
cdsw_const$plot()

knitr::kable(cdsw_const$as_table())
from to mid width n_precursors
349.63 384.62 367.125 34.99 6688
383.62 418.62 401.120 35.00 8357
417.62 452.61 435.115 34.99 9661
451.61 486.61 469.110 35.00 10452
485.61 520.60 503.105 34.99 10725
519.60 554.59 537.095 34.99 10837
553.59 588.59 571.090 35.00 10433
587.59 622.58 605.085 34.99 9750
621.58 656.58 639.080 35.00 9276
655.58 690.57 673.075 34.99 8406
689.57 724.56 707.065 34.99 7848
723.56 758.56 741.060 35.00 7116
757.56 792.55 775.055 34.99 6355
791.55 826.55 809.050 35.00 5666
825.55 860.54 843.045 34.99 4923
859.54 894.53 877.035 34.99 4359
893.53 928.53 911.030 35.00 3807
927.53 962.52 945.025 34.99 3344
961.52 996.52 979.020 35.00 2724
995.52 1030.51 1013.015 34.99 2357
1029.51 1064.50 1047.005 34.99 2042
1063.50 1098.50 1081.000 35.00 1807
1097.50 1132.49 1114.995 34.99 1313
1131.49 1166.49 1148.990 35.00 1088
1165.49 1200.48 1182.985 34.99 881
const_error <- cdsw_const$error()

Classical Method based on quantile

Same number of MS1 precursors in each window

cdsw_quantile <- Cdsw$new(masses, n_bins = 25, method = "quantile")
cdsw_quantile
## <Cdsw> 146,009 masses in 25 windows, method "quantile"
## window widths 18.69 to 151.00 m/z
cdsw_quantile$plot()

knitr::kable(cdsw_quantile$as_table())
from to mid width n_precursors
349.63 381.03 365.330 31.40 5956
380.03 406.71 393.370 26.68 6131
405.71 429.24 417.475 23.53 6070
428.24 450.05 439.145 21.81 6086
449.05 470.06 459.555 21.01 6095
469.06 488.80 478.930 19.74 6107
487.80 508.12 497.960 20.32 6173
507.12 526.81 516.965 19.69 6150
525.81 545.79 535.800 19.98 6166
544.79 565.29 555.040 20.50 6123
564.29 584.80 574.545 20.51 6139
583.80 605.12 594.460 21.32 6121
604.12 626.34 615.230 22.22 6113
625.34 648.36 636.850 23.02 6108
647.36 672.34 659.850 24.98 6074
671.34 696.53 683.935 25.19 6082
695.53 722.89 709.210 27.36 6054
721.89 751.40 736.645 29.51 6053
750.40 782.43 766.415 32.03 6023
781.43 817.40 799.415 35.97 5982
816.40 857.96 837.180 41.56 6026
856.96 905.62 881.290 48.66 5971
904.62 964.93 934.775 60.31 5943
963.93 1049.48 1006.705 85.55 5903
1048.48 1200.48 1124.480 152.00 5863
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.

optimized <- cdsw_quantile$optimize_windows(min_count = 10)
knitr::kable(optimized)
from to mid width n_precursors
349.63 381.05 365.340 31.42 5956
379.95 407.05 393.500 27.10 6211
405.45 429.35 417.400 23.90 6174
428.15 450.05 439.100 21.90 6143
449.05 470.15 459.600 21.10 6095
469.05 488.85 478.950 19.80 6110
487.45 508.15 497.800 20.70 6329
507.05 527.05 517.050 20.00 6184
525.45 546.05 535.750 20.60 6391
544.45 565.45 554.950 21.00 6322
564.15 585.05 574.600 20.90 6267
583.55 605.15 594.350 21.60 6199
604.05 626.45 615.250 22.40 6144
625.15 648.45 636.800 23.30 6217
647.15 672.45 659.800 25.30 6215
671.25 696.55 683.900 25.30 6121
695.45 723.15 709.300 27.70 6105
721.55 751.55 736.550 30.00 6175
750.25 782.55 766.400 32.30 6076
781.25 817.55 799.400 36.30 6086
816.25 858.05 837.150 41.80 6062
856.85 905.65 881.250 48.80 6007
904.55 965.05 934.800 60.50 5967
963.85 1049.65 1006.750 85.80 5931
1048.45 1200.48 1124.465 152.03 5867
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.

cdsw_mixed <- Cdsw$new(masses, n_bins = 25, method = "mixture", max_window = 100)
cdsw_mixed
## <Cdsw> 146,009 masses in 25 windows, method "mixture"
## window widths 20.69 to 100.00 m/z
cdsw_mixed$plot_breaks()

cdsw_mixed$plot()

knitr::kable(cdsw_mixed$as_table())
from to mid width n_precursors
349.63 381.69 365.660 32.06 6098
380.69 408.26 394.475 27.57 6377
407.26 432.22 419.740 24.96 6531
431.22 454.62 442.920 23.40 6588
453.62 476.03 464.825 22.41 6627
475.03 496.74 485.885 21.71 6715
495.74 517.43 506.585 21.69 6717
516.43 538.27 527.350 21.84 6709
537.27 559.27 548.270 22.00 6689
558.27 580.35 569.310 22.08 6720
579.35 602.32 590.835 22.97 6682
601.32 625.28 613.300 23.96 6542
624.28 648.83 636.555 24.55 6562
647.83 674.15 660.990 26.32 6467
673.15 699.89 686.520 26.74 6406
698.89 727.78 713.335 28.89 6313
726.78 757.52 742.150 30.74 6238
756.52 789.86 773.190 33.34 6123
788.86 825.37 807.115 36.51 5932
824.37 865.29 844.830 40.92 5730
864.29 910.49 887.390 46.20 5533
909.49 962.37 935.930 52.88 5194
961.37 1025.02 993.195 63.65 4737
1024.02 1100.48 1062.250 76.46 4192
1099.48 1200.48 1149.980 101.00 3140
optimized <- cdsw_mixed$optimize_windows(min_count = 10)
knitr::kable(optimized)
from to mid width n_precursors
349.63 381.95 365.790 32.32 6177
380.45 408.35 394.400 27.90 6431
407.05 432.35 419.700 25.30 6695
431.15 454.65 442.900 23.50 6613
453.45 476.05 464.750 22.60 6658
474.85 496.85 485.850 22.00 6842
495.45 517.45 506.450 22.00 6774
516.15 538.45 527.300 22.30 6981
537.15 559.45 548.300 22.30 6837
558.15 580.45 569.300 22.30 6741
579.15 602.45 590.800 23.30 6861
601.15 625.45 613.300 24.30 6731
624.15 649.15 636.650 25.00 6639
647.55 674.15 660.850 26.60 6535
672.55 700.15 686.350 27.60 6611
698.75 728.15 713.450 29.40 6485
726.75 757.55 742.150 30.80 6240
756.25 790.15 773.200 33.90 6277
788.65 825.55 807.100 36.90 6019
824.25 865.35 844.800 41.10 5732
864.25 910.55 887.400 46.30 5542
909.35 962.65 936.000 53.30 5284
961.35 1025.15 993.250 63.80 4745
1023.85 1100.65 1062.250 76.80 4229
1099.45 1200.48 1149.965 101.03 3142
plot_windows(optimized, masses)

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.

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 version 4.6.2 (2026-10-29)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 26.04.1 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.32.so;  LAPACK version 3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Etc/UTC
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_4.0.4  prozor_0.5.0   rmarkdown_2.32
## 
## loaded via a namespace (and not attached):
##  [1] vctrs_0.7.3        cli_3.6.6          knitr_1.52         rlang_1.3.0       
##  [5] xfun_0.62          otel_0.3.0         generics_0.1.4     S7_1.0.0          
##  [9] jsonlite_2.0.0     labeling_0.4.3     glue_1.8.1         buildtools_1.0.0  
## [13] htmltools_0.5.9    maketools_1.3.2    sys_3.4.3          sass_0.5.0        
## [17] scales_1.4.0       grid_4.6.2         tibble_3.3.1       evaluate_1.0.5    
## [21] jquerylib_0.1.4    fastmap_1.2.0      yaml_2.3.12        lifecycle_1.0.5   
## [25] compiler_4.6.2     dplyr_1.2.1        RColorBrewer_1.1-3 pkgconfig_2.0.3   
## [29] farver_2.1.2       digest_0.6.39      R6_2.6.1           tidyselect_1.2.1  
## [33] pillar_1.11.1      magrittr_2.0.5     bslib_0.12.0       withr_3.0.3       
## [37] tools_4.6.2        gtable_0.3.6       cachem_1.1.0