From 2c5a93bfdf02aab909daf97bfe71748184f65e83 Mon Sep 17 00:00:00 2001 From: Ethan Bass Date: Mon, 6 Jan 2025 12:04:45 -0500 Subject: [PATCH] fix: bug in read_chromeleon (faulty inference of decimal separator) --- NEWS.md | 2 ++ R/read_chromeleon.R | 20 ++++++++++++++------ man/read_chromeleon.Rd | 7 ++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 5d6a88e..b195416 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/read_chromeleon.R b/R/read_chromeleon.R index 8543648..7c841b8 100644 --- a/R/read_chromeleon.R +++ b/R/read_chromeleon.R @@ -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 @@ -20,12 +23,16 @@ 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) @@ -33,12 +40,14 @@ read_chromeleon <- function(path, format_out = c("matrix", "data.frame", 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){ @@ -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")){ diff --git a/man/read_chromeleon.Rd b/man/read_chromeleon.Rd index 2b01803..b0ac04b 100644 --- a/man/read_chromeleon.Rd +++ b/man/read_chromeleon.Rd @@ -9,7 +9,8 @@ read_chromeleon( 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 ) } \arguments{ @@ -24,6 +25,10 @@ or \code{data.table}.} \item{metadata_format}{Format to output metadata. Either \code{chromconverter} or \code{raw}.} + +\item{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{","}.} } \value{ A chromatogram in the format specified by \code{format_out}.