Skip to content

Commit 6f444a2

Browse files
committed
fix: waters_raw, lowercase filenames, metadata, directory detection, v0.7.1
1 parent fc0e9da commit 6f444a2

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.Rproj.user
22
docs
3-
chromConverter.Rcheck
3+
chromConverter.Rcheck
4+
.Rhistory

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ importFrom(data.table,setorder)
4848
importFrom(purrr,partial)
4949
importFrom(readxl,read_xls)
5050
importFrom(stats,reshape)
51+
importFrom(stats,setNames)
5152
importFrom(stringr,str_split_fixed)
5253
importFrom(utils,file_test)
5354
importFrom(utils,head)

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
## chromConverter 0.7.1
22

3-
* Fix automatic file detection for directories (e.g., Waters `.raw` and Agilent `.D`)
3+
* Fixed automatic file detection for directories (e.g., Waters `.raw` and Agilent `.D`)
4+
* Fixed bug preventing extraction of `Waters` chromatograms with lowercase filenames.
5+
* Added support for extracting metadata from 'Waters' `.raw` header files.
6+
* Added support for extraction of detector units from 'Waters' chromatograms.
47

58
## chromConverter 0.7.0
69

R/attach_metadata.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ attach_metadata <- function(x, meta, format_in, format_out, data_format,
174174
data_format = data_format,
175175
parser = "chromconverter",
176176
format_out = format_out)
177+
}, "waters_raw" = {
178+
structure(x, instrument = get_metadata_field(meta, "Instrument"),
179+
detector = NA,
180+
software = NA,
181+
method = NA,
182+
batch = NA,
183+
operator = get_metadata_field(meta, "User_Name"),
184+
run_datetime = as.POSIXct(paste(meta$Acquired_Date, meta$Acquired_Time,
185+
collapse = " "),
186+
format = "%d-%b-%Y %I:%M:%S",
187+
tz = "UTC"),
188+
sample_name = ifelse(is.null(meta$`Acquired Name`),
189+
fs::path_ext_remove(basename(source_file)),
190+
meta$`Acquired Name`),
191+
sample_id = NA,
192+
sample_injection_volume = NA,
193+
sample_amount = NA,
194+
time_range = NA,
195+
time_interval = NA,
196+
time_unit = NA,
197+
detector_range = NA,
198+
detector_y_unit = get_metadata_field(meta, "Detector_Unit"),
199+
source_file = source_file,
200+
source_file_format = source_file_format,
201+
source_sha1 = NA,
202+
data_format = data_format,
203+
parser = "chromconverter",
204+
format_out = format_out)
177205
}, "shimadzu_dad" = {
178206
structure(x,
179207
instrument = get_metadata_field(meta, "Instrument Name"),

R/read_waters_raw.R

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#'
33
#' Parser for reading 'Waters MassLynx (.raw) files into R.
44
#'
5+
#' @importFrom stats setNames
56
#' @param path Path to \code{.raw} file.
67
#' @param format_out Class of output. Either \code{matrix}, \code{data.frame},
78
#' or \code{data.table}.
@@ -23,23 +24,49 @@ read_waters_raw <- function(path, format_out = c("matrix", "data.frame", "data.t
2324

2425
format_out <- check_format_out(format_out)
2526
data_format <- match.arg(data_format, c("wide", "long"))
26-
metadata_format <- match.arg(metadata_format, c("chromconverter", "raw"))
27+
metadata_format <- match.arg(tolower(metadata_format),
28+
c("chromconverter", "raw"))
29+
metadata_format <- switch(metadata_format,
30+
chromconverter = "waters_raw", raw = "raw")
31+
uv_paths <- list.files(path, pattern="_CHRO", full.names = TRUE, ignore.case = TRUE)
32+
meta_path <- grep("\\.INF$", uv_paths, value = TRUE, ignore.case = TRUE)
33+
uv_paths <- grep("\\.INF$", uv_paths, invert = TRUE, value = TRUE, ignore.case = TRUE)
2734

28-
uv_paths <- list.files(path, pattern="_CHRO", full.names = TRUE)
29-
meta_path <- grep("\\.INF", uv_paths, value = TRUE)
30-
uv_paths <- grep("\\.INF", uv_paths, invert = TRUE, value = TRUE)
35+
if (read_metadata){
36+
hdr_path <- list.files(path, pattern="_HEADER.TXT",
37+
full.names = TRUE, ignore.case = TRUE)
38+
hdr <- readLines(hdr_path)
39+
hdr <- gsub("\\$\\$ ", "", hdr)
40+
hdr <- stringr::str_split_fixed(hdr, ":", n = 2)
41+
hdr[,2] <- gsub("^ ", "", hdr[,2])
42+
hdr[hdr[,2] == "", 2] <- NA
43+
hdr[,1] <- gsub(" ", "_", hdr[,1])
44+
hdr <- as.list(setNames(hdr[,2], hdr[,1]))
45+
}
3146

3247
dat <- lapply(uv_paths, read_waters_chro, format_out = format_out,
33-
data_format = data_format, read_metadata = read_metadata,
34-
metadata_format = metadata_format)
48+
data_format = data_format)
3549

3650
meta <- readLines(meta_path, skipNul = TRUE, warn = FALSE,
3751
encoding = "Latin-1")
3852
meta <- iconv(meta, sub = "")
39-
meta <- strsplit(meta,"\\([0-9]\\)")[[1]][-1]
40-
meta <- gsub("^ |\\$CC\\$", "", sapply(strsplit(meta, ","), function(x) x[1]))
53+
meta <- strsplit(meta, "\001")[[1]][-c(1:3)]
54+
nms <- gsub("^ |\\$CC\\$", "", sapply(strsplit(meta, ","), `[`, 1))
4155

42-
names(dat) <- meta
56+
if (read_metadata){
57+
detector_unit <- sapply(strsplit(meta, ","), `[`, 6)
58+
dat <- lapply(seq_along(dat), function(i){
59+
attach_metadata(x = dat[[i]], meta = c(hdr, Detector_Unit = detector_unit[i]),
60+
format_in = metadata_format,
61+
format_out = format_out,
62+
data_format = data_format,
63+
parser = "chromconverter",
64+
source_file = path,
65+
source_file_format = "waters_raw",
66+
scale = FALSE)
67+
})
68+
}
69+
names(dat) <- gsub("^\\([0-9]+\\)\\s*", "", nms)
4370
dat
4471
}
4572

@@ -51,9 +78,6 @@ read_waters_raw <- function(path, format_out = c("matrix", "data.frame", "data.t
5178
#' @param path Path to \code{.dat} file.
5279
#' @param format_out Matrix or data.frame.
5380
#' @param data_format Either \code{wide} (default) or \code{long}.
54-
#' @param read_metadata Logical. Whether to attach metadata.
55-
#' @param metadata_format Format to output metadata. Either \code{chromconverter}
56-
#' or \code{raw}.
5781
#' @return A chromatogram in the format specified by \code{format_out}
5882
#' (retention time x wavelength).
5983
#' @author Ethan Bass
@@ -62,14 +86,9 @@ read_waters_raw <- function(path, format_out = c("matrix", "data.frame", "data.t
6286
#magic 80000100 08000200
6387

6488
read_waters_chro <- function(path, format_out = "data.frame",
65-
data_format = c("wide", "long"),
66-
read_metadata = TRUE,
67-
metadata_format = c("chromconverter", "raw")){
89+
data_format = c("wide", "long")){
6890

6991
data_format <- match.arg(data_format, c("wide", "long"))
70-
metadata_format <- match.arg(metadata_format, c("chromconverter", "raw"))
71-
# metadata_format <- switch(metadata_format,
72-
# chromconverter = "waters_uv", raw = "raw")
7392

7493
f <- file(path, "rb")
7594
on.exit(close(f))

tests/testthat/test-read_chroms.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ test_that("chromConverter can read `Agilent Chemstation` .csv file", {
1111
tolerance = .0001, ignore_attr = TRUE)
1212
expect_equal(head(rownames(x), n = 3), c("0.002", "0.0086666666667",
1313
"0.0153333333333"))
14-
x1 <- read_chroms(path_csv, format_in="chemstation_csv",
15-
format_out="data.table", progress_bar = FALSE)[[1]]
14+
x1 <- read_chroms(path_csv, format_in = "chemstation_csv",
15+
format_out = "data.table", progress_bar = FALSE)[[1]]
1616
expect_s3_class(x1, "data.table")
1717

1818
x2 <- read_chroms(path_csv, format_in="chemstation_csv",

0 commit comments

Comments
 (0)