tests/testthat/test-extra-misc.R

# these tests rely on files included in the chromConverterExtraTests package,
# which is available on GitHub (https://github.com/ethanbass/chromConverterExtraTests).

test_that("read_chroms can read 'Thermo' RAW files", {
  skip_on_cran()
  skip_on_ci()
  skip_if_not_installed("chromConverterExtraTests")
  skip_if_missing_thermorawfileparser()

  path <- system.file("CirA.raw",
                      package = "chromConverterExtraTests")
  skip_if_not(file.exists(path))

  tmp <- tempdir()
  on.exit(unlink(tmp))
  x <- read_chroms(path, progress_bar = FALSE, path_out = tmp)[[1]]
  expect_type(x, "list")
  expect_equal(names(x), c("MS1", "MS2", "DAD", "BPC",
                           "TIC", "chroms", "metadata"))
})

test_that("read_chroms can use 'OpenChrom' parsers", {
  skip_on_cran()
  skip_on_ci()
  skip_if_not_installed("chromConverterExtraTests")
  skip_if_missing_openchrom()

  path <- system.file("DCM1.SMS", package = "chromConverterExtraTests")
  skip_if_not(file.exists(path))
  tmp <- tempdir()
  on.exit(unlink(tmp))
  x <- read_chroms(path, format_in = "msd", progress_bar = FALSE,
                   verbose = FALSE, export_format = "csv",
                   path_out = tmp)[[1]]

  expect_equal(class(x)[1], "matrix")
  expect_equal(dim(x), c(3032, 297))

  x <- read_chroms(path, format_in = "msd", progress_bar = FALSE,
                   verbose = FALSE, path_out = tmp)[[1]]
  expect_type(x, "list")
  expect_equal(dim(x$MS1), c(469732,4))
})

test_that("read_cdf function can read peak tables", {
  skip_on_cran()
  skip_if_not_installed("ncdf4")
  skip_if_not_installed("chromConverterExtraTests")

  path <- system.file("VARIAN1.CDF", package = "chromConverterExtraTests")
  skip_if_not(file.exists(path))

  x <- read_cdf(path, what = "peak_table")
  # what about chromatograms
  expect_s3_class(x, "data.frame")
  expect_equal(dim(x), c(8,5))
})

test_that("read_chroms can read ANDI MS files", {
  skip_on_cran()
  skip_if_not_installed("ncdf4")
  skip_if_not_installed("chromConverterExtraTests")

  path <- system.file("HP_MS.CDF", package = "chromConverterExtraTests")
  skip_if_not(file.exists(path))

  x <- read_chroms(path, progress_bar = FALSE)[[1]]

  expect_equal(names(x), c("MS1", "TIC"))
  expect_equal(colnames(x$MS1), c("rt","mz","intensity"))
  expect_equal(colnames(x$TIC),"intensity")
  expect_s3_class(x$MS1, "data.frame")
  expect_equal(class(x$TIC)[1], "matrix")
  expect_true(all(dim(x$TIC) == c(621, 1)))
  expect_true(all(dim(x$MS1) == c(7638, 3)))

  x1 <- read_chroms(path, what=c("TIC"), data_format="long",
                    progress_bar = FALSE)[[1]]
  expect_equal(ncol(x1), 2)
  expect_equal(colnames(x1), c("rt", "intensity"))

  x2 <- read_chroms(path, what=c("MS1"), ms_format = "list",
                    progress_bar = FALSE)[[1]]
  expect_type(x2, "list")
  expect_equal(length(x2), length(unique(x$MS1$rt)))
})

test_that("read_chroms can read Varian SMS", {
  skip_on_cran()
  skip_if_not_installed("chromConverterExtraTests")

  path_sms <- system.file("STRD15.SMS", package = "chromConverterExtraTests")
  path_mzml <- system.file("STRD15.mzML", package = "chromConverterExtraTests")

  skip_if_not(file.exists(path_sms))
  skip_if_not(file.exists(path_mzml))

  tmp <- tempdir()

  x <- read_chroms(path_sms, progress_bar = FALSE, export_format = "mzml",
                   path_out = tmp)[[1]]

  path_mzml_cc <- fs::path(tmp, attr(x$MS1, "sample_name"), ext = "mzML")
  on.exit(unlink(path_mzml_cc))

  x1 <- read_chroms(path_mzml, format_in = "mzml", progress_bar = FALSE)[[1]]
  x2 <- read_chroms(path_mzml_cc, format_in = "mzml", progress_bar = FALSE,
                    format_out = "data.frame")[[1]]

  # check equality of mass spec scans
  ms1_mzml <- x1$MS1[,-4]
  ms1_mzml$rt <- ms1_mzml$rt / 1000
  expect_equal(as.data.frame(x$MS1), ms1_mzml, tolerance = .0000001,
               ignore_attr = TRUE)
  expect_equal(as.data.frame(x$MS1), x2$MS1[,c(1:3)], ignore_attr=TRUE)

  # check equality of TIC
  expect_equal(x$TIC[, "rt"], x1$TIC$rt/1000, tolerance = .000001)
  # there is a slight discrepancy with the TICs generated by OpenChrom but mine
  # seem to be correct...?
  expect_equal(x$TIC[, "intensity"], x1$TIC$int, tolerance = .000001)
  # expect_equal(x$TIC, x2$TIC[,-3], ignore_attr = FALSE)

  # check equality of BPC
  expect_equal(x$BPC[, "rt"], x1$BPC$rt/1000, tolerance = .000001)
  expect_equal(x$BPC[, "intensity"], x1$BPC$int)

  # check metadata
  expect_equal(attr(x$TIC, "software_name"), "MS Workstation (Upgrade)")
  expect_equal(attr(x$TIC, "no_scans"), 3432)
  expect_equal(attr(x$TIC, "sample_name"), "STRD15")
  expect_equal(attr(x$TIC, "ms_params")$max_ric_scan, 1445)
  expect_equal(attr(x$TIC, "ms_params")$max_ric_val, 39285)
  expect_equal(attr(x$TIC, "ms_params")$max_ionization_time, c(0, 25000))
  expect_equal(attr(x$TIC, "ms_params")$temp_trap, 150)
  expect_equal(attr(x$TIC, "ms_params")$temp_manifold, 50)
  expect_equal(attr(x$TIC, "ms_params")$temp_transferline, 250)
  expect_equal(attr(x$TIC, "ms_params")$axial_modulation, 4)
  # attr(x$MS1, "run_datetime") # should be 8/8/2014 8:50 PM - 9:20 PM
  expect_equal(x2$metadata$source_file, basename(attr(x$MS1,"source_file")))
  # expect_equal(x2$metadata$timestamp, attr(x$MS1,"run_datetime")[1])
})

test_that("read_varian_peaklist function works", {
  skip_on_cran()
  skip_if_not_installed("chromConverterExtraTests")

  path <- system.file("varian_peaklist.csv",
                      package = "chromConverterExtraTests")
  skip_if_not(file.exists(path))

  x <- read_varian_peaklist(path)
  expect_s3_class(x, "data.frame")
  expect_equal(dim(x), c(46476, 15))
})

test_that("read_chroms can write Varian SMS to CDF", {

  # write CDF
  skip_if_not_installed("ncdf4")
  skip_on_cran()
  skip_if_not_installed("chromConverterExtraTests")

  path_sms <- system.file("STRD15.SMS", package = "chromConverterExtraTests")
  skip_if_not(fs::file_exists(path_sms))

  tmp <- tempdir()

  path_cdf <- fs::path(tmp, fs::path_ext_remove(basename(path_sms)),
                       ext = "cdf")
  on.exit(unlink(path_cdf))

  x <- read_chroms(path_sms, path_out = tmp, export_format = "cdf",
               progress_bar = FALSE)[[1]]

  x3 <- read_cdf(path_cdf, data_format = "long", format_out = "data.frame")
  x3$MS1$rt <- x3$MS1$rt/60
  x3$TIC$rt <- x3$TIC$rt/60

  expect_equal(as.data.frame(x$MS1), x3$MS1, ignore_attr = TRUE,
               tolerance = .0000001)
  expect_equal(as.data.frame(x$TIC), x3$TIC, ignore_attr = TRUE,
               tolerance = .0000001)
  expect_equal(attr(x3$MS1, "run_datetime"), attr(x$MS1, "run_datetime")[1])
  expect_equal(attr(x3$MS1, "time_unit"), "Seconds")
  expect_equal(attr(x3$MS1, "detector"), "MS")
  expect_equal(attr(x3$MS1, "sample_name"), "STRD15")
})

test_that("get_filetype works as expected", {
  skip_on_cran()
  skip_if_not_installed("chromConverterExtraTests")

  expect_equal(get_filetype(system.file("chemstation_MSD.MS",
                                        package = "chromConverterExtraTests")),
               "chemstation_ms"
  )
  expect_equal(get_filetype(system.file("B4NF.7_C23.qgd",
                                        package="chromConverterExtraTests")),
               "shimadzu_qgd")
  expect_equal(get_filetype(system.file("chemstation_181.D/FID1A.ch",
                                        package="chromConverterExtraTests")),
               "chemstation_181")
  expect_equal(get_filetype(system.file("chemstation_179_mustang.ch",
                                        package="chromConverterExtraTests")),
               "chemstation_179")
  expect_equal(get_filetype(system.file("openlab_131.uv",
                                        package="chromConverterExtraTests")),
               "openlab_131")
  expect_equal(get_filetype(system.file("chemstation_81.ch",
                                        package="chromConverterExtraTests")),
               "chemstation_81")
  expect_equal(get_filetype(system.file("chemstation_30.ch",
                                        package="chromConverterExtraTests")),
               "chemstation_30")
  expect_equal(get_filetype(system.file("chemstation_31.uv",
                                        package="chromConverterExtraTests")),
               "chemstation_31")
  expect_equal(get_filetype(system.file("small.RAW",
                                        package="chromConverterExtraTests")),
               "thermoraw")
  expect_equal(get_filetype(system.file("FS19_214.gcd",
                                        package="chromConverterExtraTests")),
               "shimadzu_gcd")
  expect_equal(get_filetype(system.file("DCM1.SMS",
                                        package="chromConverterExtraTests")),
               "varian_sms")
  expect_equal(get_filetype(system.file("VARIAN1.CDF",
                                        package="chromConverterExtraTests")),
               "cdf")
  expect_equal(get_filetype(system.file("agilent.dx",
                                        package="chromConverterExtraTests")),
               "agilent_dx")
})
ethanbass/chromConverter documentation built on July 4, 2025, 12:08 p.m.