Skip to content

Commit 50baf60

Browse files
committed
fix: read_agilent_amx, detect flow gradients, added t0
1 parent dd71863 commit 50baf60

5 files changed

Lines changed: 92 additions & 18 deletions

File tree

R/call_openchrom.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ write_openchrom_batchfile <- function(files, path_out,
184184
#' indicating whether 'OpenChrom' is configured correctly. Otherwise, returns
185185
#' the path to OpenChrom command-line application.
186186
#' @author Ethan Bass
187-
#' @seealso [call_openchrom]
187+
#' @seealso [`call_openchrom`]
188188
#' @export
189189

190190
configure_openchrom <- function(cli = c("null", "true", "false", "status"), path = NULL){

R/read_agilent_amx.R

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ parse_pump_method <- function(file_path, format_out = "data.frame",
165165

166166
timetable_nodes <- xml2::xml_find_all(doc, ".//TimetableEntry")
167167

168-
gradient <- purrr::map_dfr(timetable_nodes, function(node) {
168+
composition_nodes <- timetable_nodes[
169+
xml2::xml_attr(timetable_nodes, "type") == "ChangeSolventCompositionType"
170+
]
171+
flow_nodes <- timetable_nodes[
172+
xml2::xml_attr(timetable_nodes, "type") == "ChangeFlowType"
173+
]
174+
175+
gradient <- purrr::map_dfr(composition_nodes, function(node) {
169176
data.frame(
170177
time_min = get_amx_num(node, ".//Time"),
171178
channel = c("A", "B", "C", "D"),
@@ -177,22 +184,59 @@ parse_pump_method <- function(file_path, format_out = "data.frame",
177184
)
178185
)
179186
})
187+
188+
flow_gradient <- purrr::map_dfr(flow_nodes, function(node) {
189+
data.frame(
190+
time_min = get_amx_num(node, ".//Time"),
191+
channel = "flow",
192+
percent = get_amx_num(node, ".//Flow")
193+
)
194+
})
195+
180196
if (nrow(gradient) != 0){
181197
gradient <- gradient[gradient$channel %in% active_channels, ]
182198

183-
if (gradient_format == "wide") {
184-
gradient <- tidyr::pivot_wider(gradient, names_from = "channel",
185-
values_from = "percent",
186-
names_prefix = "pct_")
199+
zero_row <- data.frame(
200+
time_min = 0,
201+
channel = active_channels,
202+
percent = solvents$percentage
203+
)
204+
205+
if (!any(gradient$time_min == 0)) {
206+
gradient <- rbind(zero_row, gradient)
207+
}
208+
}
209+
if (nrow(flow_gradient) != 0) {
210+
flow_zero <- data.frame(
211+
time_min = 0,
212+
channel = "flow",
213+
percent = get_amx_num(doc, "/PumpMethod/Flow")
214+
)
215+
if (!any(flow_gradient$time_min == 0)) {
216+
flow_gradient <- rbind(flow_zero, flow_gradient)
187217
}
188218
}
219+
if (nrow(gradient) != 0 || nrow(flow_gradient) != 0){
220+
gradient <- rbind(gradient, flow_gradient)
221+
gradient <- gradient[order(gradient$time_min), ]
222+
}
223+
if (nrow(gradient) != 0 && gradient_format == "wide") {
224+
gradient <- tidyr::pivot_wider(gradient, names_from = "channel",
225+
values_from = "percent",
226+
names_prefix = "pct_")
227+
if ("pct_flow" %in% names(gradient)) {
228+
names(gradient)[names(gradient) == "pct_flow"] <- "flow_mL_min"
229+
gradient <- tidyr::fill(gradient, "flow_mL_min", .direction = "down")
230+
}
231+
}
232+
189233
gradient <- convert_format_out(gradient, format_out = format_out)
190234
list(
191-
flow_mL_min = get_amx_num(doc, ".//Flow"),
192-
stop_time_min = get_amx_num(doc, ".//StopTimeValue"),
193-
post_time_min = get_amx_num(doc, ".//PostTimeValue"),
194-
pressure_low_bar = get_amx_num(doc, ".//LowPressureLimit"),
195-
pressure_high_bar = get_amx_num(doc, ".//HighPressureLimit"),
235+
flow_mL_min = get_amx_num(doc, "/PumpMethod/Flow"),
236+
stop_time_min = get_amx_num(doc, "/PumpMethod/StopTime/StopTimeValue"),
237+
post_time_min = get_amx_num(doc, "/PumpMethod/PostTime/PostTimeValue"),
238+
pressure_low_bar = get_amx_num(doc, "/PumpMethod/LowPressureLimit"),
239+
pressure_high_bar = get_amx_num(doc, "/PumpMethod/HighPressureLimit"),
196240
solvents = solvents,
197241
gradient = gradient
198242
)

man/configure_openchrom.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-extra-agilent.R

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ test_that("read_chroms can write 'Agilent ChemStation' version 81 files to CDF",
218218

219219
xx <- read_chroms(fs::path(path_out, "5970_mix_10nG.cdf"),
220220
progress_bar = FALSE)[[1]]
221-
expect_equal(x, xx, ignore_attr=TRUE, tolerance=1e-7)
221+
expect_equal(x, xx, ignore_attr = TRUE, tolerance = 1e-7)
222222
expect_equal(get_times(x), get_times(xx))
223223
fields <-c("sample_name", "detector", "detector_id", "detector_y_unit",
224224
"method", "operator", "time_interval", "time_unit", "run_datetime")
@@ -628,7 +628,7 @@ test_that("read_chroms can read 'Agilent ACAML' files", {
628628
expect_s3_class(x2, "tbl")
629629
})
630630

631-
test_that("read_agilent_amx works correctly", {
631+
test_that("read_agilent_amx works correctly, part 1", {
632632
skip_on_cran()
633633
skip_if_not_installed("chromConverterExtraTests")
634634
path <- system.file("column_storage_ACN100.amx",
@@ -644,7 +644,11 @@ test_that("read_agilent_amx works correctly", {
644644
expect_equal(method1$dad$peakwidth_nm, 4)
645645
expect_equal(nrow(method1$pump$gradient), 0)
646646
expect_equal(as.numeric(method1$metadata$created), 1767977263.0)
647+
})
647648

649+
test_that("read_agilent_amx works correctly, part 2", {
650+
skip_on_cran()
651+
skip_if_not_installed("chromConverterExtraTests")
648652
path <- system.file("Glucosinolates-XDB5.amx",
649653
package = "chromConverterExtraTests")
650654
skip_if_not(file.exists(path))
@@ -658,7 +662,7 @@ test_that("read_agilent_amx works correctly", {
658662
expect_equal(method2$dad$peakwidth_nm, 4)
659663
expect_equal(c(method2$dad$spectra_from_nm, method2$dad$spectra_to_nm),
660664
c(190,400))
661-
expect_shape(method2$pump$gradient, dim = c(8,3))
665+
expect_shape(method2$pump$gradient, dim = c(9,3))
662666
expect_equal(method2$column$post_time_min, 6)
663667
expect_equal(method2$column$temp_controls$temperature_C, c(40, 40))
664668
expect_equal(method2$autosampler$injection_volume_uL, 5)
@@ -668,10 +672,34 @@ test_that("read_agilent_amx works correctly", {
668672
gradient_format = "long")
669673
expect_s3_class(method_dt$dad$signals, "data.table")
670674
expect_s3_class(method_dt$pump$gradient, "data.table")
671-
expect_shape(method_dt$pump$gradient, dim = c(16,3))
675+
expect_shape(method_dt$pump$gradient, dim = c(18, 3))
672676

673677
method_tibble <- read_agilent_amx(path, format_out = "tibble")
674678
expect_s3_class(method_tibble$dad$signals, "tbl")
675679
expect_s3_class(method_tibble$pump$gradient, "tbl")
676-
expect_shape(method_tibble$pump$gradient, dim = c(8,3))
680+
expect_shape(method_tibble$pump$gradient, dim = c(9, 3))
681+
})
682+
683+
684+
test_that("read_agilent_amx works correctly, part 3", {
685+
skip_on_cran()
686+
skip_if_not_installed("chromConverterExtraTests")
687+
path <- system.file("flow_rate_example.amx",
688+
package = "chromConverterExtraTests")
689+
skip_if_not(file.exists(path))
690+
method3 <- read_agilent_amx(path)
691+
expect_equal(names(method3$metadata),
692+
c("method_name", "version", "status", "created", "created_by",
693+
"modified", "modified_by")
694+
)
695+
expect_equal(method3$dad$peakwidth_nm, 4)
696+
expect_equal(c(method3$dad$spectra_from_nm, method3$dad$spectra_to_nm),
697+
c(190,400))
698+
expect_shape(method3$pump$gradient, dim = c(10,4))
699+
expect_equal(method3$column$post_time_min, 2.5)
700+
expect_equal(method3$column$temp_controls$temperature_C, c(25, 25))
701+
expect_equal(method3$autosampler$injection_volume_uL, 1)
702+
expect_equal(as.numeric(method3$metadata$created), 1779656802.0)
703+
expect_equal(method3$pump$gradient$flow_mL_min,
704+
c(0.4,0.5,0.5,0.5,0.55,0.55,0.6,0.6,0.7,0.7))
677705
})

tests/testthat/test-read_chroms.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ test_that("extract_metadata function works", {
8989
meta <- extract_metadata(x1, what = c("sample_name"))
9090
expect_named(meta, c("name", "sample_name"))
9191

92-
x2 <- read_chroms(rep(path_uv, 2), parser = "chromConverter",
92+
expect_warning({
93+
x2 <- read_chroms(rep(path_uv, 2), parser = "chromConverter",
9394
progress_bar = FALSE)
95+
})
9496
attr(x2[[1]],"detector") <- NULL
9597
meta2 <- extract_metadata(x2)
9698
expect_equal(nrow(meta2), length(x2))

0 commit comments

Comments
 (0)