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 24.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.26.so; LAPACK version 3.12.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.4 ggplot2_3.5.1 chromConverter_0.7.3
#>
#> loaded via a namespace (and not attached):
#> [1] rappdirs_0.3.3 sass_0.4.9 generics_0.1.3 bitops_1.0-9
#> [5] xml2_1.3.6 stringi_1.8.4 lattice_0.22-6 digest_0.6.37
#> [9] magrittr_2.0.3 evaluate_1.0.3 grid_4.4.2 fastmap_1.2.0
#> [13] rprojroot_2.0.4 cellranger_1.1.0 jsonlite_1.8.9 Matrix_1.7-1
#> [17] purrr_1.0.2 scales_1.3.0 RaMS_1.4.3 pbapply_1.7-2
#> [21] textshaping_0.4.1 jquerylib_0.1.4 cli_3.6.3 rlang_1.1.4
#> [25] bit64_4.5.2 munsell_0.5.1 withr_3.0.2 base64enc_0.1-3
#> [29] cachem_1.1.0 yaml_2.3.10 parallel_4.4.2 tools_4.4.2
#> [33] dplyr_1.1.4 colorspace_2.1-1 here_1.0.1 reticulate_1.40.0
#> [37] vctrs_0.6.5 R6_2.5.1 png_0.1-8 lifecycle_1.0.4
#> [41] stringr_1.5.1 fs_1.6.5 bit_4.5.0.1 ragg_1.3.3
#> [45] pkgconfig_2.0.3 desc_1.4.3 pkgdown_2.1.1 bslib_0.8.0
#> [49] pillar_1.10.1 gtable_0.3.6 glue_1.8.0 Rcpp_1.0.13-1
#> [53] systemfonts_1.1.0 tidyselect_1.2.1 xfun_0.50 tibble_3.2.1
#> [57] knitr_1.49 farver_2.1.2 htmltools_0.5.8.1 labeling_0.4.3
#> [61] rmarkdown_2.29 compiler_4.4.2 entab_0.3.1 readxl_1.4.3