Skip to content

Commit a9eea01

Browse files
committed
tests: test data_format attributes
- also more tests for Shimadzu timezone conversions - split long lines
1 parent a5a9b2c commit a9eea01

File tree

6 files changed

+171
-81
lines changed

6 files changed

+171
-81
lines changed

tests/testthat/test-extra-agilent.R

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ test_that("read_chroms can read 'Agilent' MS files", {
1515
expect_equal(class(x)[1], "matrix")
1616
expect_equal(dim(x), c(95471, 3))
1717
expect_equal(attr(x, "parser"), "entab")
18+
expect_equal(attr(x, "data_format"), "long")
1819
expect_equal(colnames(x), c("rt", "mz", "intensity"))
1920

2021
# export as mzML
@@ -23,40 +24,55 @@ test_that("read_chroms can read 'Agilent' MS files", {
2324
ext = "mzML")
2425
on.exit(unlink(path_mzml))
2526

26-
x1 <- read_chroms(path, parser = "chromconverter", what = "MS1",
27-
format_out = "data.table",
27+
# chromConverter can read Agilent MS data and write to mzml
28+
x1 <- read_chroms(path, parser = "chromconverter",
29+
format_out = "data.frame", data_format = "wide",
2830
progress_bar = FALSE,
29-
export_format = "mzML", path_out = tmp)[[1]]
30-
expect_s3_class(x1, "data.table")
31-
expect_equal(attr(x1, "format_out"), "data.table")
31+
export_format = "mzML", path_out = tmp, force = TRUE)[[1]]
32+
expect_s3_class(x1$MS1, "data.frame")
33+
expect_equal(attr(x1$MS1, "format_out"), "data.frame")
34+
expect_equal(attr(x1$MS1, "data_format"), "long")
35+
expect_equal(attr(x1$TIC, "data_format"), "wide")
36+
expect_equal(attr(x1$BPC, "data_format"), "long")
3237

3338
xx <- read_mzml(path_mzml)
34-
expect_equal(x1, xx$MS1[,-4], ignore_attr = TRUE)
35-
expect_equal(x1, as.data.frame(x), ignore_attr = TRUE)
36-
expect_equal(attr(x1,"sample_name"), attr(x1,"sample_name"))
37-
expect_equal(attr(x1,"source_sha1"), attr(x1,"source_sha1"))
38-
expect_equal(attr(x1,"time_unit"), attr(x1,"time_unit"))
39-
expect_equal(attr(x1,"run_datetime"), attr(x1,"run_datetime"))
40-
expect_equal(attr(x1,"operator"), attr(x1,"operator"))
41-
expect_equal(attr(x1,"method"), attr(x1,"method"))
42-
expect_equal(attr(x1,"detector"), attr(x1,"detector"))
39+
expect_equal(xx$MS1[,-4], x1$MS1, ignore_attr = TRUE)
40+
expect_equal(as.data.frame(xx$TIC), x1$TIC, ignore_attr = TRUE)
41+
expect_equal(rownames(xx$TIC), rownames(x1$TIC))
42+
expect_equal(xx$BPC[,1], x1$BPC[,3], ignore_attr=TRUE)
43+
expect_equal(as.numeric(rownames(xx$BPC)), x1$BPC$rt, tolerance = 0.00001)
44+
45+
expect_equal(x1$MS1, as.data.frame(x), ignore_attr = TRUE)
46+
expect_equal(attr(x1$MS1,"sample_name"), attr(x,"sample_name"))
47+
expect_equal(attr(x1$MS1,"source_sha1"), attr(x,"source_sha1"))
48+
expect_equal(attr(x1$MS1, "time_unit"), attr(x, "time_unit"))
49+
expect_equal(attr(x1$MS1,"run_datetime"), attr(x,"run_datetime"))
50+
expect_equal(attr(x1$MS1,"operator"), attr(x,"operator"))
51+
expect_equal(attr(x1$MS1,"method"), attr(x,"method"))
52+
expect_equal(attr(x1$MS1,"detector"), attr(x,"detector"))
53+
expect_equal(attr(x1$MS1, "data_format"), "long")
4354

4455
# rainbow
45-
x1 <- read_chroms(path, parser = "rainbow",
46-
progress_bar = FALSE, precision = 0)[[1]]
47-
expect_equal(class(x1)[1], "matrix")
48-
expect_equal(dim(x1), c(2534, 841))
49-
expect_equal(attr(x1,"run_datetime"), attr(x1,"run_datetime"))
50-
expect_equal(attr(x1,"method"), attr(x1,"method"))
51-
expect_equal(attr(x1,"detector"), attr(x1,"detector"))
52-
5356
x2 <- read_chroms(path, parser = "rainbow",
57+
progress_bar = FALSE, precision = 0)[[1]]
58+
expect_equal(class(x2)[1], "matrix")
59+
expect_equal(dim(x2), c(2534, 841))
60+
# date time stamp appears to be wrong in rainbow
61+
# expect_equal(attr(x2,"run_datetime"), attr(x,"run_datetime"))
62+
expect_equal(attr(x2, "method"), attr(x1$MS1, "method"))
63+
expect_equal(attr(x2, "detector"), attr(x1$MS1, "detector"))
64+
expect_equal(attr(x2, "data_format"), "wide")
65+
66+
x3 <- read_chroms(path, parser = "rainbow",
5467
progress_bar = FALSE, data_format = "long",
5568
format_out = "data.table",
5669
precision = 0)[[1]]
57-
expect_s3_class(x2, "data.table")
58-
expect_equal(dim(x2), c(2131094, 3))
59-
expect_equal(colnames(x2), c("rt", "mz", "intensity"))
70+
expect_s3_class(x3, "data.table")
71+
expect_equal(dim(x3), c(2131094, 3))
72+
expect_equal(colnames(x3), c("rt", "mz", "intensity"))
73+
expect_equal(attr(x3, "method"), attr(x2, "method"))
74+
expect_equal(attr(x3, "detector"), attr(x2, "detector"))
75+
expect_equal(attr(x3, "data_format"), "long")
6076
})
6177

6278
test_that("read_chroms can read 'Agilent ChemStation' version 30 files", {
@@ -80,6 +96,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 30 files", {
8096
expect_equal(attr(x, "detector_y_unit"), "mAU")
8197
expect_equal(attr(x, "method"), "JCMONO1.M")
8298
expect_equal(attr(x, "time_unit"), "Minutes")
99+
expect_equal(attr(x, "data_format"), "wide")
83100

84101
x1 <- read_chroms(path, parser = "chromconverter", format_out = "data.frame",
85102
data_format = "long", progress_bar = FALSE)[[1]]
@@ -89,6 +106,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 30 files", {
89106
expect_equal(x[,1], x1[,2], ignore_attr = TRUE)
90107
expect_equal(head(x1$rt, 1), -.00133333333333333, tolerance = .00001)
91108
expect_equal(tail(x1$rt, 1), 32.002, tolerance = .00001)
109+
expect_equal(attr(x1, "data_format"), "long")
92110
})
93111

94112
test_that("read_chroms can read 'Agilent ChemStation' 31 files", {
@@ -133,6 +151,9 @@ test_that("read_chroms can read 'Agilent ChemStation' 31 files", {
133151

134152
expect_equal(attr(x, "time_unit"), "Minutes")
135153
expect_equal(attr(x, "time_unit"), attr(x1, "time_unit"))
154+
155+
expect_equal(attr(x, "data_format"), "wide")
156+
expect_equal(attr(x, "data_format"), attr(x1, "data_format"))
136157
})
137158

138159
test_that("read_chroms can read 'Agilent ChemStation' version 81 files", {
@@ -157,6 +178,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 81 files", {
157178
expect_equal(attr(x, "detector_id"), "HP G1530A")
158179
expect_equal(attr(x, "sample_name"), "5970 mix 10nG")
159180
expect_equal(attr(x, "time_unit"), "Minutes")
181+
expect_equal(attr(x, "data_format"), "wide")
160182

161183
# long format
162184
x1 <- read_chroms(path, progress_bar = FALSE,
@@ -168,6 +190,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 81 files", {
168190
expect_equal(x[,1], x1[[2]], ignore_attr = TRUE)
169191
expect_equal(head(x1$rt, 1), 3.00044479166667, tolerance = .00001)
170192
expect_equal(tail(x1$rt, 1), 11.9971114583333, tolerance = .00001)
193+
expect_equal(attr(x1, "data_format"), "long")
171194
})
172195

173196
test_that("read_chroms can read 'Agilent ChemStation' version 130 files", {
@@ -190,6 +213,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 130 files", {
190213
expect_equal(attr(x, "detector_y_unit"), "mAU")
191214
expect_equal(attr(x, "method"), "Phenolics_new2.M")
192215
expect_equal(attr(x, "time_unit"), "Minutes")
216+
expect_equal(attr(x, "data_format"), "wide")
193217

194218
# long format
195219
x1 <- read_chroms(path, data_format = "long", format_out = "data.table",
@@ -206,7 +230,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 130 files", {
206230
expect_equal(attr(x1, "detector_y_unit"), "mAU")
207231
expect_equal(attr(x1, "method"), "Phenolics_new2.M")
208232
expect_equal(attr(x1, "time_unit"), "Minutes")
209-
233+
expect_equal(attr(x1, "data_format"), "long")
210234
})
211235

212236

@@ -229,6 +253,7 @@ test_that("read_chroms can read 'Agilent OpenLab' 179 files", {
229253
expect_equal(attr(x, "sample_name"), "STD_1_1mM-1MKHCO3")
230254
expect_equal(attr(x, "detector_y_unit"), "nRIU")
231255
expect_equal(attr(x, "time_unit"), "Minutes")
256+
expect_equal(attr(x, "data_format"), "wide")
232257

233258
# long format
234259
x1 <- read_chroms(path, progress_bar = FALSE,
@@ -241,6 +266,7 @@ test_that("read_chroms can read 'Agilent OpenLab' 179 files", {
241266
expect_equal(x[,1], x1[,2], ignore_attr = TRUE)
242267
expect_equal(head(x1$rt,1), 0.001125, tolerance = .00001)
243268
expect_equal(tail(x1$rt,1), 36, tolerance = .00001)
269+
expect_equal(attr(x1, "data_format"), "long")
244270
})
245271

246272
test_that("read_chroms can read 'Agilent ChemStation' 179 files (8-byte format)", {
@@ -265,6 +291,7 @@ test_that("read_chroms can read 'Agilent ChemStation' 179 files (8-byte format)"
265291
expect_equal(attr(x, "software"), "Mustang ChemStation")
266292
expect_equal(attr(x, "method"), "NGS Default Edit.M")
267293
expect_equal(attr(x, "time_unit"), "Minutes")
294+
expect_equal(attr(x, "data_format"), "wide")
268295

269296
# test scale argument
270297
x1 <- read_chroms(path, progress_bar = FALSE, scale=FALSE)[[1]]
@@ -284,6 +311,7 @@ test_that("read_chroms can read 'Agilent ChemStation' 179 (4-byte format)", {
284311
expect_equal(dim(x), c(22800, 1))
285312
expect_equal(head(get_times(x), 1), 0.00083331667582194, tolerance = .00001)
286313
expect_equal(tail(get_times(x), 1), 19, tolerance = .00001)
314+
expect_equal(attr(x, "data_format"), "wide")
287315

288316
# check metadata
289317
expect_equal(attr(x, "parser"), "chromconverter")
@@ -304,15 +332,17 @@ test_that("read_chroms can read 'Agilent MassHunter' dad files", {
304332
skip_if_not(file.exists(path))
305333

306334
x <- read_chroms(path, format_in = "masshunter_dad", parser = "entab",
307-
progress_bar = FALSE)
335+
progress_bar = FALSE)[[1]]
308336
x1 <- read_chroms(path, format_in = "masshunter_dad", parser = "aston",
309-
progress_bar = FALSE)
337+
progress_bar = FALSE)[[1]]
310338

311-
expect_equal(dim(x[[1]]), c(240, 276))
312-
expect_equal(class(x[[1]])[1], "matrix")
313-
expect_equal(x[[1]], x1[[1]], ignore_attr = TRUE)
314-
expect_equal(attr(x[[1]], "parser"), "entab")
315-
expect_equal(attr(x1[[1]], "parser"), "aston")
339+
expect_equal(dim(x), c(240, 276))
340+
expect_equal(class(x)[1], "matrix")
341+
expect_equal(x, x1, ignore_attr = TRUE)
342+
expect_equal(attr(x, "parser"), "entab")
343+
expect_equal(attr(x1, "parser"), "aston")
344+
expect_equal(attr(x, "data_format"), "wide")
345+
expect_equal(attr(x1, "data_format"), "wide")
316346

317347
x <- read_chroms(path, format_in = "masshunter_dad", parser = "entab",
318348
data_format = "long", format_out = "data.frame",
@@ -327,6 +357,8 @@ test_that("read_chroms can read 'Agilent MassHunter' dad files", {
327357

328358
expect_equal(attr(x1, "parser"), "aston")
329359
expect_equal(colnames(x1), c("rt", "lambda", "intensity"))
360+
expect_equal(attr(x, "data_format"), "long")
361+
expect_equal(attr(x1, "data_format"), "long")
330362
})
331363

332364

@@ -356,6 +388,7 @@ test_that("read_chroms can read 'Agilent ChemStation' version 181 files", {
356388
expect_equal(attr(x[[1]], "run_datetime"), as.POSIXct("2022-8-23 12:16:25",
357389
tz = "UTC"))
358390
expect_equal(attr(x[[1]], "time_unit"), "Minutes")
391+
expect_equal(attr(x[[1]], "data_format"), "wide")
359392

360393
expect_equal(attr(x[[2]], "sample_name"), "140+H")
361394
expect_equal(attr(x[[2]], "file_version"), "181")
@@ -379,9 +412,10 @@ test_that("read_chroms can read 'Agilent ChemStation' version 181 files", {
379412
expect_equal(tail(x1$FID1A$rt, 1), 19.7048479166667, tolerance = .00001)
380413

381414
expect_equal(extract_metadata(x)[,c(1:8)], extract_metadata(x1)[,c(1:8)])
415+
expect_equal(attr(x1[[1]], "data_format"), "long")
382416

383417
expect_warning(read_chroms(path, format_in = "agilent_d", what = "dad",
384-
progress_bar = FALSE))
418+
progress_bar = FALSE))
385419
expect_error(read_agilent_d(path, what = "dad"))
386420

387421
})
@@ -419,10 +453,10 @@ test_that("read_chroms can read 'Agilent' .dx files", {
419453
expect_true(all(round(x$instrument$`THM1B,Right Temperature`) == 45))
420454

421455
expect_equal(head(names(x$instrument),5), c("RID1G,Board Temperature",
422-
"RID1F,Diode 2",
423-
"RID1E,Diode 1",
424-
"RID1D,Polarity",
425-
"RID1C,Diode Balance"))
456+
"RID1F,Diode 2",
457+
"RID1E,Diode 1",
458+
"RID1D,Polarity",
459+
"RID1C,Diode Balance"))
426460

427461
expect_equal(sapply(x$instrument, function(xx) attr(xx, "detector_y_unit")),
428462
c("\u00b0C","counts","counts","","","\u00b0C", "\u00b0C",
@@ -437,14 +471,15 @@ test_that("read_chroms can read 'Agilent' .dx files", {
437471
expect_true(all(
438472
sapply(x$instrument, function(xx){
439473
attr(xx, "run_datetime")
440-
}) == 1636717143))
474+
}) == 1636717143))
441475

442476
x1 <- read_chroms(path, format_in="agilent_dx", what = c("chroms","instrument"),
443477
progress_bar = FALSE, data_format = "long",
444478
format_out = "data.frame")[[1]]
445479
expect_s3_class(x1$chroms[1], "data.frame")
446480
expect_equal(dim(x1$chroms), c(10000, 2))
447-
expect_equal(dim(x1$instrument$`PMP1C,Solvent Ratio A`), c(43253, 2))
481+
expect_equal(dim(x1$instrument[["PMP1C,Solvent Ratio A"]]), c(43253, 2))
482+
expect_equal(attr(x1$chroms,"data_format"),"long")
448483

449484
expect_warning(read_chroms(path, format_in = "agilent_dx", what = "dad",
450485
progress_bar = FALSE))

tests/testthat/test-extra-asm.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test_that("read_chroms can read ASM LC format", {
2121
expect_equal(attr(x[[1]], "run_datetime"), as.POSIXct("2016-10-20 06:33:54",
2222
tz = "UTC"))
2323
expect_equal(as.character(attr(x[[1]], "time_unit")), "s")
24-
expect_equal(as.character(attr(x[[1]], "data_format")), "wide")
24+
expect_equal(as.character(attr(x[[1]], "data_format")), "long")
2525
})
2626

2727
test_that("read_chroms can read ASM GC format", {
@@ -45,3 +45,4 @@ test_that("read_chroms can read ASM GC format", {
4545
expect_equal(as.character(attr(x, "time_unit")), "s")
4646
expect_equal(as.character(attr(x, "data_format")), "long")
4747
})
48+

tests/testthat/test-extra-export.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ test_that("read_chroms can write mzML files", {
4141
format_out = "data.table",
4242
export_format = "mzml", path_out = tmp, force = TRUE)[[1]]
4343

44-
path_mzml <- fs::path(tmp, gsub(" ", "_", attr(x,"sample_name")), ext = "mzML")
44+
path_mzml <- fs::path(tmp, gsub(" ", "_",
45+
attr(x, "sample_name")), ext = "mzML")
4546
on.exit(unlink(path_mzml))
4647

47-
x1 <- read_mzml(path_mzml, what = c("MS1","metadata"))
48-
expect_equal(x1$MS1[,c(1:3)], x, ignore_attr = TRUE)
48+
x1 <- read_mzml(path_mzml, what = c("MS1", "metadata"))
49+
50+
expect_equal(x1$MS1[,c(1:3)], x[,c(2:4)], ignore_attr = TRUE)
4951
# expect_equal(x1$metadata$timestamp, attr(x,"run_datetime"))
5052
expect_equal(x1$metadata$source_file, basename(attr(x,"source_file")))
5153

tests/testthat/test-extra-misc.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test_that("read_chroms can read ANDI MS files", {
7474
expect_true(all(dim(x$TIC) == c(621, 1)))
7575
expect_true(all(dim(x$MS1) == c(7638, 3)))
7676

77-
x1 <- read_chroms(path, what=c("TIC"), data_format="long",
77+
x1 <- read_chroms(path, what = c("TIC"), data_format="long",
7878
progress_bar = FALSE)[[1]]
7979
expect_equal(ncol(x1), 2)
8080
expect_equal(colnames(x1), c("rt", "intensity"))
@@ -98,12 +98,13 @@ test_that("read_chroms can read Varian SMS", {
9898
tmp <- tempdir()
9999

100100
x <- read_chroms(path_sms, progress_bar = FALSE, export_format = "mzml",
101-
path_out = tmp)[[1]]
101+
data_format = "long", path_out = tmp)[[1]] # must be in long format to export successfully
102102

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

106-
x1 <- read_chroms(path_mzml, format_in = "mzml", progress_bar = FALSE)[[1]]
106+
x1 <- read_chroms(path_mzml, format_in = "mzml", data_format = "long",
107+
progress_bar = FALSE)[[1]]
107108
x2 <- read_chroms(path_mzml_cc, format_in = "mzml", progress_bar = FALSE,
108109
format_out = "data.frame")[[1]]
109110

@@ -136,6 +137,10 @@ test_that("read_chroms can read Varian SMS", {
136137
expect_equal(attr(x$TIC, "ms_params")$temp_manifold, 50)
137138
expect_equal(attr(x$TIC, "ms_params")$temp_transferline, 250)
138139
expect_equal(attr(x$TIC, "ms_params")$axial_modulation, 4)
140+
expect_equal(attr(x$TIC, "data_format"), "long")
141+
expect_equal(attr(x$BPC, "data_format"), "long")
142+
expect_equal(attr(x$MS1, "data_format"), "long")
143+
139144
# attr(x$MS1, "run_datetime") # should be 8/8/2014 8:50 PM - 9:20 PM
140145
expect_equal(x2$metadata$source_file, basename(attr(x$MS1,"source_file")))
141146
# expect_equal(x2$metadata$timestamp, attr(x$MS1,"run_datetime")[1])
@@ -155,7 +160,6 @@ test_that("read_varian_peaklist function works", {
155160
})
156161

157162
test_that("read_chroms can write Varian SMS to CDF", {
158-
159163
# write CDF
160164
skip_if_not_installed("ncdf4")
161165
skip_on_cran()
@@ -171,9 +175,10 @@ test_that("read_chroms can write Varian SMS to CDF", {
171175
on.exit(unlink(path_cdf))
172176

173177
x <- read_chroms(path_sms, path_out = tmp, export_format = "cdf",
174-
progress_bar = FALSE)[[1]]
178+
data_format = "long", progress_bar = FALSE)[[1]]
175179

176180
x3 <- read_cdf(path_cdf, data_format = "long", format_out = "data.frame")
181+
177182
x3$MS1$rt <- x3$MS1$rt/60
178183
x3$TIC$rt <- x3$TIC$rt/60
179184

0 commit comments

Comments
 (0)