Skip to content

Commit

Permalink
Merge pull request #26 from r-hyperspec/feature/move-remaining
Browse files Browse the repository at this point in the history
Update count_lines.R, read_asc_Andor, & Perkin from `hyperSpec`
  • Loading branch information
eoduniyi authored Aug 20, 2020
2 parents 476f3d4 + b283f9c commit 2d3a251
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 3 deletions.
26 changes: 23 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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 <eeoduniyi@gmail.com>
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
Expand All @@ -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'
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
41 changes: 41 additions & 0 deletions R/count_lines.R
Original file line number Diff line number Diff line change
@@ -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)
)
}
46 changes: 46 additions & 0 deletions R/read_asc_Andor.R
Original file line number Diff line number Diff line change
@@ -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")
})
}
43 changes: 43 additions & 0 deletions R/read_asc_PerkinElmer.R
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 2d3a251

Please sign in to comment.