MS chromatograms are returned by default in long
format
with three columns: retention time, m/z, and intensity.
As an example, we can load the ‘Varian’ SMS chromatogram included in
the chromConverterExtraTests
package.
# download example file from the web
path_sms <- tempfile(fileext = ".sms")
download.file("https://raw.github.com/ethanbass/chromConverterExtraTests/master/inst/STRD15.SMS", destfile = path_sms)
dat <- read_chroms(path_sms, format_in = "varian_sms", format_out = "data.frame")
Plot TIC and mass spectra use base R syntax
x <- dat[[1]]$MS1
# derive TIC using aggregate
tic <- aggregate(intensity ~ rt, data = x, FUN = sum)
# plot TIC
matplot(tic$rt, tic$intensity, type = 'l',
ylab = "Total intensity", xlab = "Time (min)")
Here is a simple plot function you could use to plot mass spectra in base R:
plot_spec <- function(spec, lab_int=0.2, digits=1){
plot(spec, type = "h", xlab = "m/z", ylab = "Intensity")
lab.idx <- which(spec$intensity > lab_int * max(spec$intensity))
text(spec$mz[lab.idx], spec$intensity[lab.idx], round(spec$mz[lab.idx],
digits), offset = 0.25, pos = 3, cex = 0.5)
}
You can extract mass spectra by filtering on time, e.g., to get the mass spectrum of the first scan, you could do:
times <- unique(x$rt)
spec <- x[x$rt == times[100], -1]
plot_spec(spec)
Plot TIC and mass spectra using dplyr syntax
Plot TIC with dplyr:
tic <- x |> dplyr::group_by(rt) |> dplyr::summarize_at("intensity", sum)
plot(intensity ~ rt, data=tic, type = 'l',
ylab = "Total intensity", xlab = "Time (min)")
Plot spectrum with dplyr:
Plot TIC and mass spectra using data.table syntax
Convert to data.table
:
x <- data.table::as.data.table(x)
chromConverter can also return chromatograms in data.table format directly:
dat <- read_chroms(path_sms, format_in = "varian_sms", format_out = "data.table")
Extract the total ion chromatogram:
tic <- x[, .(intensity = sum(intensity)), by = rt]
matplot(tic$rt, tic$intensity, type = 'l',
ylab = "Total intensity", xlab = "Time (min)")
Extract the base ion chromatogram:
bpc <- x[, .(intensity = max(intensity)), by = rt]
matplot(bpc$rt, bpc$intensity, type = 'l',
ylab = "Base ion chromatogram", xlab = "Time (min)")
To obtain a mass spectrum we just filter by retention time as before:
plot_spec(x[rt == 7.26355, c('mz','intensity')])
Plot TIC and mass spectra using ggplot
library(ggplot2)
ggplot(data = tic, aes(x=rt, y=intensity)) +
geom_line() +
xlab("Retention time (min)") +
ylab("Intensity")
Plot mass spectrum with ggplot:
lab_int <- 0.2
digits <- 1
dplyr::filter(x, rt == 7.26355) |>
dplyr::select(mz, intensity) |>
ggplot(aes(x = mz, y = intensity)) +
geom_segment(aes(xend = mz, yend = 0), linewidth = 0.5) +
geom_text(data = subset(spec, intensity > lab_int * max(intensity)),
aes(label = round(mz, digits)),
vjust = -0.5, size = 2) +
labs(x = "m/z", y = "Intensity") +
theme_minimal()
Session Information
sessionInfo()
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 22.04.5 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.20.so; LAPACK version 3.10.0
#>
#> locale:
#> [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
#> [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
#> [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
#> [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
#>
#> time zone: UTC
#> tzcode source: system (glibc)
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] data.table_1.16.2 ggplot2_3.5.1 chromConverter_0.7.1
#>
#> loaded via a namespace (and not attached):
#> [1] rappdirs_0.3.3 generics_0.1.3 sass_0.4.9 utf8_1.2.4
#> [5] bitops_1.0-9 xml2_1.3.6 stringi_1.8.4 lattice_0.22-6
#> [9] digest_0.6.37 magrittr_2.0.3 evaluate_1.0.1 grid_4.4.2
#> [13] fastmap_1.2.0 rprojroot_2.0.4 cellranger_1.1.0 jsonlite_1.8.9
#> [17] Matrix_1.7-1 purrr_1.0.2 fansi_1.0.6 scales_1.3.0
#> [21] RaMS_1.4.3 pbapply_1.7-2 textshaping_0.4.0 jquerylib_0.1.4
#> [25] cli_3.6.3 rlang_1.1.4 bit64_4.5.2 munsell_0.5.1
#> [29] withr_3.0.2 base64enc_0.1-3 cachem_1.1.0 yaml_2.3.10
#> [33] parallel_4.4.2 tools_4.4.2 dplyr_1.1.4 colorspace_2.1-1
#> [37] here_1.0.1 reticulate_1.39.0 vctrs_0.6.5 R6_2.5.1
#> [41] png_0.1-8 lifecycle_1.0.4 stringr_1.5.1 fs_1.6.5
#> [45] bit_4.5.0 ragg_1.3.3 pkgconfig_2.0.3 desc_1.4.3
#> [49] pkgdown_2.1.1 bslib_0.8.0 pillar_1.9.0 gtable_0.3.6
#> [53] glue_1.8.0 Rcpp_1.0.13-1 systemfonts_1.1.0 tidyselect_1.2.1
#> [57] xfun_0.49 tibble_3.2.1 knitr_1.49 farver_2.1.2
#> [61] htmltools_0.5.8.1 labeling_0.4.3 rmarkdown_2.29 compiler_4.4.2
#> [65] entab_0.3.1 readxl_1.4.3