Skip to content

Commit c164cc3

Browse files
committed
test: fix failing retention time tests
1 parent 2ed6d60 commit c164cc3

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

R/utils.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,24 @@ simple_cap <- function(x) {
416416
paste(toupper(substring(s, 1, 1)), substring(s, 2),
417417
sep = "", collapse = " ")
418418
}
419+
420+
#' Get retention times
421+
#'
422+
#' Get retention times from a list of chromatograms or a \code{peak_table} object.
423+
#'
424+
#' @param x A list of chromatograms or \code{peak_table} object.
425+
#' @param idx Index of chromatogram from which to extract times.
426+
#' @return Numeric vector of retention times from the chromatogram specified by
427+
#' \code{idx}.
428+
#' @family utility functions
429+
#' @noRd
430+
get_times <- function(x, idx = 1){
431+
if (inherits(x, "peak_table")){
432+
x <- get_chrom_list(x)
433+
}
434+
if (inherits(x, "chrom_list") | inherits(x, "list")){
435+
as.numeric(rownames(x[[idx]]))
436+
} else if (inherits(x, "matrix")){
437+
as.numeric(rownames(x))
438+
}
439+
}

tests/testthat/test-extra-agilent.R

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 30 files", {
7171

7272
expect_equal(class(x)[1], "matrix")
7373
expect_equal(dim(x), c(38405, 1))
74-
expect_equal(head(rownames(x), 1), "-.00133333333333333")
75-
expect_equal(tail(rownames(x), 1), "32.002")
74+
expect_equal(head(get_times(x),1), -0.001333333, tolerance = .00001)
75+
expect_equal(tail(get_times(x),1), 32.002, tolerance = .00001)
7676

7777
expect_equal(attr(x, "parser"), "chromconverter")
7878
expect_equal(attr(x, "sample_name"), "NVAC-6B1-S3R1")
@@ -87,8 +87,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 30 files", {
8787
expect_s3_class(x1[1], "data.frame")
8888
expect_equal(as.numeric(rownames(x)), x1[,1])
8989
expect_equal(x[,1], x1[,2], ignore_attr = TRUE)
90-
expect_equal(head(x1$rt, 1), -.00133333333333333)
91-
expect_equal(tail(x1$rt, 1), 32.002)
90+
expect_equal(head(x1$rt, 1), -.00133333333333333, tolerance = .00001)
91+
expect_equal(tail(x1$rt, 1), 32.002, tolerance = .00001)
9292
})
9393

9494
test_that("read_chroms can read 'Agilent ChemStation' 31 files", {
@@ -108,11 +108,11 @@ test_that("read_chroms can read 'Agilent ChemStation' 31 files", {
108108
expect_equal(dim(x), c(27659, 176))
109109
expect_equal(dim(x1), c(27659, 177))
110110

111-
expect_equal(head(rownames(x), 1), "0.000333333333333333")
112-
expect_equal(head(rownames(x1), 1), "0.000333333333333333")
111+
expect_equal(head(get_times(x), 1), 0.000333333333333333, tolerance = .00001)
112+
expect_equal(head(get_times(x1), 1), 0.000333333333333333, tolerance = .00001)
113113

114-
expect_equal(tail(rownames(x), 1), "31.9911666666667")
115-
expect_equal(tail(rownames(x1), 1), "31.9911666666667")
114+
expect_equal(tail(get_times(x), 1), 31.9911666666667, tolerance = .00001)
115+
expect_equal(tail(get_times(x1), 1), 31.9911666666667, tolerance = .00001)
116116

117117
# check metadata
118118
expect_equal(attr(x1, "parser"), "entab")
@@ -146,8 +146,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 81 files", {
146146
x <- read_chroms(path, progress_bar = FALSE)[[1]]
147147

148148
expect_equal(dim(x), c(2699, 1))
149-
expect_equal(head(rownames(x), 1), "3.00044479166667")
150-
expect_equal(tail(rownames(x), 1), "11.9971114583333")
149+
expect_equal(head(get_times(x), 1), 3.00044479166667, tolerance = .00001)
150+
expect_equal(tail(get_times(x), 1), 11.9971114583333, tolerance = .00001)
151151

152152
# check metadata
153153
expect_equal(class(x)[1], "matrix")
@@ -166,8 +166,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 81 files", {
166166
expect_equal(dim(x1), c(2699, 2))
167167
expect_equal(as.numeric(rownames(x)), x1[[1]])
168168
expect_equal(x[,1], x1[[2]], ignore_attr = TRUE)
169-
expect_equal(head(x1$rt, 1), 3.00044479166667)
170-
expect_equal(tail(x1$rt, 1), 11.9971114583333)
169+
expect_equal(head(x1$rt, 1), 3.00044479166667, tolerance = .00001)
170+
expect_equal(tail(x1$rt, 1), 11.9971114583333, tolerance = .00001)
171171
})
172172

173173
test_that("read_chroms can read 'Agilent ChemStation' version 130 files", {
@@ -182,8 +182,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 130 files", {
182182

183183
expect_equal(class(x)[1], "matrix")
184184
expect_equal(dim(x), c(12750, 1))
185-
expect_equal(head(rownames(x),1), "0.00583333333333333")
186-
expect_equal(tail(rownames(x),1), "84.9991666666667")
185+
expect_equal(head(get_times(x),1), 0.00583333333333333, tolerance = .00001)
186+
expect_equal(tail(get_times(x),1), 84.9991666666667, tolerance = .00001)
187187

188188
# check metadata
189189
expect_equal(attr(x, "sample_name"), "0-CN-6-6-PU")
@@ -199,8 +199,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 130 files", {
199199
expect_s3_class(x1[1], c("data.table","data.frame"))
200200
expect_equal(colnames(x1), c("rt", "intensity"))
201201
expect_equal(dim(x1), c(12750, 2))
202-
expect_equal(head(x1$rt,1), 0.00583333333333333)
203-
expect_equal(tail(x1$rt,1), 84.9991666666667)
202+
expect_equal(head(x1$rt,1), 0.00583333333333333, tolerance = .00001)
203+
expect_equal(tail(x1$rt,1), 84.9991666666667, tolerance = .00001)
204204

205205
expect_equal(attr(x1, "sample_name"), "0-CN-6-6-PU")
206206
expect_equal(attr(x1, "detector_y_unit"), "mAU")
@@ -221,8 +221,8 @@ test_that("read_chroms can read 'Agilent OpenLab' 179 files", {
221221
x <- read_chroms(path, progress_bar = FALSE)[[1]]
222222
expect_equal(class(x)[1], "matrix")
223223
expect_equal(dim(x), c(10000, 1))
224-
expect_equal(head(rownames(x),1),"0.001125")
225-
expect_equal(tail(rownames(x),1),"36")
224+
expect_equal(head(get_times(x),1), 0.001125, tolerance = .00001)
225+
expect_equal(tail(get_times(x),1), 36, tolerance = .00001)
226226

227227
# check metadata
228228
expect_equal(attr(x, "parser"), "chromconverter")
@@ -239,8 +239,8 @@ test_that("read_chroms can read 'Agilent OpenLab' 179 files", {
239239
expect_equal(colnames(x1), c("rt", "intensity"))
240240
expect_equal(as.numeric(rownames(x)), x1[,1])
241241
expect_equal(x[,1], x1[,2], ignore_attr = TRUE)
242-
expect_equal(head(x1$rt,1), 0.001125)
243-
expect_equal(tail(x1$rt,1), 36)
242+
expect_equal(head(x1$rt,1), 0.001125, tolerance = .00001)
243+
expect_equal(tail(x1$rt,1), 36, tolerance = .00001)
244244
})
245245

246246
test_that("read_chroms can read 'Agilent ChemStation' 179 files (8-byte format)", {
@@ -255,8 +255,8 @@ test_that("read_chroms can read 'Agilent ChemStation' 179 files (8-byte format)"
255255

256256
expect_equal(class(x)[1], "matrix")
257257
expect_equal(dim(x), c(54704, 1))
258-
expect_equal(head(rownames(x),1),"0.000326049995422363")
259-
expect_equal(tail(rownames(x),1),"18.2346604166667")
258+
expect_equal(head(get_times(x),1), 0.000326049995422363, tolerance = .00001)
259+
expect_equal(tail(get_times(x),1), 18.2346604166667, tolerance = .00001)
260260

261261
# check metadata
262262
expect_equal(attr(x, "parser"), "chromconverter")
@@ -282,8 +282,8 @@ test_that("read_chroms can read 'Agilent ChemStation' 179 (4-byte format)", {
282282
x <- read_chroms(path, progress_bar = FALSE)[[1]]
283283
expect_equal(class(x)[1], "matrix")
284284
expect_equal(dim(x), c(22800, 1))
285-
expect_equal(head(rownames(x),1), "0.00083331667582194")
286-
expect_equal(tail(rownames(x),1),"19")
285+
expect_equal(head(get_times(x), 1), 0.00083331667582194, tolerance = .00001)
286+
expect_equal(tail(get_times(x), 1), 19, tolerance = .00001)
287287

288288
# check metadata
289289
expect_equal(attr(x, "parser"), "chromconverter")
@@ -343,10 +343,10 @@ test_that("read_chroms can read 'Agilent ChemStation' version 181 files", {
343343
expect_type(x, "list")
344344
expect_equal(class(x[[1]]), c("matrix","array"))
345345
expect_equal(dim(x[[2]]), c(5914, 1))
346-
expect_equal(head(rownames(x$FID1A), 1), "-.00181875") # is this right?
347-
expect_equal(tail(rownames(x$FID1A), 1), "19.7048479166667")
348-
expect_equal(head(rownames(x$V181), 1), "-.00126875") # is this right?
349-
expect_equal(tail(rownames(x$V181), 1), "19.7053979166667")
346+
expect_equal(head(get_times(x$FID1A), 1), -.00181875, tolerance = .00001)
347+
expect_equal(tail(get_times(x$FID1A), 1), 19.7048479166667, tolerance = .00001)
348+
expect_equal(head(get_times(x$V181), 1), -.00126875, tolerance = .00001)
349+
expect_equal(tail(get_times(x$V181), 1), 19.7053979166667, tolerance = .00001)
350350

351351
# check metadata
352352
expect_equal(attr(x[[1]], "sample_name"), "blanc421")
@@ -375,8 +375,8 @@ test_that("read_chroms can read 'Agilent ChemStation' version 181 files", {
375375
expect_equal(as.numeric(rownames(x[[1]])), x1[[1]][[1]])
376376
expect_equal(x[[2]][,1], x1[[2]][[2]], ignore_attr=TRUE)
377377
expect_equal(colnames(x1[[1]]), c("rt","intensity"))
378-
expect_equal(head(x1$FID1A$rt, 1), -.00181875) # is this right?
379-
expect_equal(tail(x1$FID1A$rt, 1), 19.7048479166667)
378+
expect_equal(head(x1$FID1A$rt, 1), -.00181875, tolerance = .00001)
379+
expect_equal(tail(x1$FID1A$rt, 1), 19.7048479166667, tolerance = .00001)
380380

381381
expect_equal(extract_metadata(x)[,c(1:8)], extract_metadata(x1)[,c(1:8)])
382382

@@ -400,18 +400,18 @@ test_that("read_chroms can read 'Agilent' .dx files", {
400400
expect_equal(dim(x$chroms), c(10000, 1))
401401
expect_equal(attr(x$chroms, "parser"), "chromconverter")
402402
expect_equal(attr(x$chroms, "data_format"), "wide")
403-
expect_equal(head(rownames(x$chroms),1), "0.001125")
404-
expect_equal(tail(rownames(x$chroms),1), "36")
403+
expect_equal(head(get_times(x$chroms),1), 0.001125, tolerance = .00001)
404+
expect_equal(tail(get_times(x$chroms),1), 36, tolerance = .00001)
405405

406406
# auxiliary instrumental data
407407
expect_equal(dim(x$instrument$`PMP1C,Solvent Ratio A`), c(43253, 1))
408408

409409
expect_true(all(
410-
sapply(x$instrument, function(x) round(as.numeric(tail(rownames(x),1)))) == 36)
410+
sapply(x$instrument, function(x) round(tail(get_times(x),1))) == 36)
411411
)
412412

413413
expect_true(all(
414-
sapply(x$instrument, function(x) round(as.numeric(head(rownames(x),1)))) == 0)
414+
sapply(x$instrument, function(x) round(head(get_times(x),1))) == 0)
415415
)
416416

417417
expect_true(all(x$instrument$`PMP1C,Solvent Ratio A` == 100))

0 commit comments

Comments
 (0)