Skip to content

Commit

Permalink
fix: bug in read_chromeleon (faulty inference of decimal separator)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanbass committed Jan 6, 2025
1 parent fefefb0 commit 2c5a93b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Updated `read_shimadzu_lcd` to infer retention times in Shimadzu 3D Data from `Max Plot` stream since it is always (?) present.
* Updated `read_shimadzu_lcd` to skip parsing of metadata from 3D Data Item when it is not present.
* Updated `read_shimadzu_lcd` to include `Max Plot` stream when parsing 2D chromatograms.
* Fixed bug in `read_chromeleon` related to inference of decimal separators.
* Added `decimal_mark` argument to `read_chromeleon` to manually set decimal separator.

## chromConverter 0.7.2

Expand Down
20 changes: 14 additions & 6 deletions R/read_chromeleon.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#' @param read_metadata Whether to read metadata from file.
#' @param metadata_format Format to output metadata. Either \code{chromconverter} or
#' \code{raw}.
#' @param decimal_mark Which character is used as the decimal separator in the
#' file. By default, decimal mark will be detected automatically, but it can
#' also be manually set as \code{"."} or \code{","}.
#' @return A chromatogram in the format specified by \code{format_out}.
#' (retention time x wavelength).
#' @author Ethan Bass
Expand All @@ -20,25 +23,31 @@ read_chromeleon <- function(path, format_out = c("matrix", "data.frame",
"data.table"),
data_format = c("wide", "long"),
read_metadata = TRUE,
metadata_format = c("chromconverter", "raw")){
metadata_format = c("chromconverter", "raw"),
decimal_mark = NULL){
format_out <- check_format_out(format_out)
data_format <- match.arg(data_format, c("wide", "long"))
metadata_format <- match.arg(metadata_format, c("chromconverter", "raw"))
metadata_format <- switch(metadata_format, chromconverter = "chromeleon",
raw = raw)
if (!is.null(decimal_mark)){
decimal_mark <- match.arg(decimal_mark, c(".", ","))
}
xx <- readLines(path)
xx <- remove_unicode_chars(xx)
start <- tail(grep("Data:", xx), 1)
x <- read.csv(path, skip = start, sep = "\t", row.names = NULL,
check.names = FALSE)
x <- x[, -2, drop = FALSE]
x <- x[, colSums(is.na(x)) < nrow(x)]
if (any(grepl(",", as.data.frame(x)[-1, 2]))){
decimal_separator <- ","
meta <- try(read_chromeleon_metadata(xx))
if (is.null(decimal_mark) && grepl(",", meta$`Dilution Factor`)){
decimal_mark <- ","
x <- apply(x, 2, function(x) gsub("\\.", "", x))
x <- apply(x, 2, function(x) gsub(",", ".", x))
} else {
decimal_separator <- "."
decimal_mark <- "."
x <- apply(x, 2, function(x) gsub(",", "", x))
}
x <- apply(x, 2, as.numeric)
if (ncol(x) == 2){
Expand All @@ -55,8 +64,7 @@ read_chromeleon <- function(path, format_out = c("matrix", "data.frame",
}
x <- convert_chrom_format(x, format_out = format_out)
if (read_metadata){
meta <- try(read_chromeleon_metadata(xx))
if (decimal_separator == ","){
if (decimal_mark == ","){
meta <- lapply(meta, function(x) gsub(",", ".", x))
}
if (!inherits(meta, "try-error")){
Expand Down
7 changes: 6 additions & 1 deletion man/read_chromeleon.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c5a93b

Please sign in to comment.