diff --git a/DESCRIPTION b/DESCRIPTION index 4ea0df0..81037dc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -4,10 +4,12 @@ Title: File Import Functions for Spectra in Various ASCII or Text File Formats Version: 0.0.0.9001 Maintainer: Erick Oduniyi Authors@R: c( - person("Claudia", "Beleites", role = c("aut"), email = "Claudia.Beleites@chemometrix.gmbh"), - person("Erick", "Oduniyi", role = c("aut","cre"), email = "eeoduniyi@gmail.com"), + person("Claudia", "Beleites", role = c("aut"), email = "Claudia.Beleites@chemometrix.gmbh"), + person("Erick", "Oduniyi", role = c("aut","cre"), email = "eeoduniyi@gmail.com"), person("Vilmantas", "Gegzna", role = c("aut")), - person("Bryan", "Hanson", role = c("aut"))) + person("Bryan", "Hanson", role = c("aut")), + person("Marcel", "Dahms", role = c("aut")), + person("Bjoern", "Egert", role = c("aut"))) Description: File import filters for a number of file formats produced by Witec instruments. The imported data are returned as hyperSpec objects. License: MIT + file LICENSE @@ -32,3 +34,21 @@ Suggests: VignetteBuilder: knitr RoxygenNote: 7.1.1 Roxygen: list(markdown = TRUE) +Collate: + '0temp_fileio_optional.R' + 'count_lines.R' + 'helpers.R' + 'microbenchmark.R' + 'read_txt_Witec.R' + 'read_asc_Andor.R' + 'read_asc_PerkinElmer.R' + 'read_dat_Witec.R' + 'read_ini.R' + 'read_mat_Witec.R' + 'read_txt_Graph_Witec.R' + 'read_txt_Horiba.R' + 'read_txt_Renishaw.R' + 'read_txt_Shimadzu.R' + 'read_txt_Witec_TrueMatch.R' + 'read_txt_long.R' + 'read_txt_wide.R' diff --git a/NAMESPACE b/NAMESPACE index d42d1f9..bd12e80 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,8 @@ # Generated by roxygen2: do not edit by hand +export(count_lines) +export(read_asc_Andor) +export(read_asc_PerkinElmer) export(read_dat_Witec) export(read_ini) export(read_mat_Witec) diff --git a/R/count_lines.R b/R/count_lines.R new file mode 100644 index 0000000..901c8da --- /dev/null +++ b/R/count_lines.R @@ -0,0 +1,41 @@ +#' Count Lines (of an ASCII File) +#' +#' @param file the file name or connection +#' @param chunksize `file` is read in chunks of `chunksize` lines. +#' @return number of lines in file +#' @export +#' +#' @concept io +#' +#' @author C. Beleites +count_lines <- function(file, chunksize = 1e4) { + nlines <- 0 + + con <- file(file, open = "r") + on.exit(close(con)) + + while ((n <- length(readLines(con, n = chunksize))) > 0L) { + nlines <- nlines + n + } + + nlines +} + +hySpc.testthat::test(count_lines) <- function() { + context("count_lines") + + tmpfile <- tempfile() + on.exit(unlink(tmpfile)) + + writeLines("blabla\nblubb", con = tmpfile) + + test_that( + "file read in one chunk", + expect_equal(count_lines(tmpfile), 2) + ) + + test_that( + "file read in more chunks", + expect_equal(count_lines(tmpfile, chunksize = 1L), 2) + ) +} diff --git a/R/read_asc_Andor.R b/R/read_asc_Andor.R new file mode 100644 index 0000000..e96649c --- /dev/null +++ b/R/read_asc_Andor.R @@ -0,0 +1,46 @@ +#' Import Raman Spectra/Maps from Andor Cameras/Solis ASCII files. +#' +#' `read_asc_Andor()` reads Andor Solis ASCII (`.asc`) files where the first column gives the wavelength +#' axes and the other columns the spectra. +#' +#' @param file filename or connection to ASCII file +#' @param ...,quiet,dec,sep handed to [base::scan()] +#' @return a hyperSpec object +#' @author Claudia Beleites +#' @seealso `vignette ("fileio")` for more information on file import and +#' +#' @concept io +#' +#' @export +read_asc_Andor <- function(file = stop("filename or connection needed"), + ..., quiet = TRUE, dec = ".", sep = ",") { + + ## check for valid data connection + check_con(file = file) + + ## read spectra + tmp <- readLines(file) + nwl <- length(tmp) + txt <- scan(text = tmp, dec = dec, sep = sep, quiet = quiet, ...) + + dim(txt) <- c(length(txt) / nwl, nwl) + + ## fix: Andor Solis may have final comma without values + if (all(is.na(txt [nrow(txt), ]))) { + txt <- txt [-nrow(txt), ] + } + + spc <- new("hyperSpec", wavelength = txt [1, ], spc = txt [-1, ]) + + ## consistent file import behaviour across import functions + .fileio.optional(spc, file) +} + +hySpc.testthat::test(read_asc_Andor) <- function() { + skip("Need to adapt to new package") + context("read_asc_Andor") + test_that("Andor Solis .asc text files", { + skip_if_not_fileio_available() + expect_known_hash(read_asc_Andor("fileio/asc.Andor/ASCII-Andor-Solis.asc"), "9ead937f51") + }) +} diff --git a/R/read_asc_PerkinElmer.R b/R/read_asc_PerkinElmer.R new file mode 100644 index 0000000..48c0159 --- /dev/null +++ b/R/read_asc_PerkinElmer.R @@ -0,0 +1,43 @@ +#' File import filter PerkinElmer ASCII spectra. +#' +#' Imports a single spectrum in PerkinElmer's ASCII format. This function is experimental. +#' +#' @param file filename (or connection) +#' @param ... further parameters are handed to [hySpc.read.txt::read_txt_long()] +#' +#' @return hyperSpec object +#' @importFrom utils packageDescription +#' @export +#' +#' @concept io +#' +read_asc_PerkinElmer <- function(file = stop("filename or connection needed"), ...) { + content <- readLines(con = file) + + message( + "read_asc_PerkinElmer is experimental, hyperSpec so far has no test data for PE .asc files.", + " Please consider submitting your spectrum in an enhancement request to ", packageDescription("hyperSpec")$BugReports, + " in order to help the development of hyperSpec." + ) + + ## find beginning of DATA section + startDATA <- grep("DATA", content) + + if (length(startDATA) != 1L) { + stop( + "read_asc_PerkinElmer so far can deal with single spectra files only.", + " Please file an enhancement request at", packageDescription("hyperSpec")$BugReports, + " with your file as an example or contact the maintainer (", + maintainer("hyperSpec"), ")." + ) + } + + ## Spectra values are stored + content <- content[-seq_len(startDATA)] + + spc <- read_txt_long(textConnection(content), header = FALSE, sep = "\t", ...) + spc$filename <- NULL # not meaningful due to textConnection use + + ## consistent file import behaviour across import functions + .fileio.optional(spc, file) +}