Skip to content

Commit

Permalink
Merge pull request #195 from stephenslab/revamp-dscquery-2
Browse files Browse the repository at this point in the history
Revise dscquery to address Issue #182, implemented new function dscread
  • Loading branch information
gaow authored Jun 10, 2019
2 parents 5371fc3 + 63723c5 commit 3c95a9b
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 239 deletions.
4 changes: 2 additions & 2 deletions dscrutils/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: dscrutils
Encoding: UTF-8
Type: Package
Version: 0.3.8
Date: 2019-04-16
Version: 0.3.8.7
Date: 2019-05-15
Title: Dynamic Statistical Comparisons R Interface
Authors@R: c(person("Gao","Wang",role=c("aut","cre"),
email="[email protected]"),
Expand Down
2 changes: 1 addition & 1 deletion dscrutils/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by roxygen2: do not edit by hand

export(dscquery)
export(dscread)
export(dscreval)
export(load_inputs)
export(read_dsc)
export(run_cmd)
export(save_session)
export(shiny_plot)
Expand Down
262 changes: 137 additions & 125 deletions dscrutils/R/dscquery.R

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions dscrutils/R/dscread.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' @title Read DSC Module Outputs
#'
#' @description Reads in DSC module outputs generated from a single
#' run of a module instance.
#'
#' @details DSC module outputs are either stored in RDS files (see
#' \code{\link{readRDS}}) or a Python "pickle" file. For DSC module
#' outputs stored as Python pickle files, the reticulate package is
#' used to import the data into R.
#'
#' @param outdir Directory where the DSC output is stored.
#'
#' @param outfile File specifying the file path relative to the DSC
#' directory. You can use \code{\link{dscquery}} with the
#' \code{module.output.file} to obtain a correct file path. Note that
#' the file path should not contain the file extension (".rds" or
#' ".pkl").
#'
#' @return The return file is a list containing the DSC module
#' outputs. This list always includes a "DSC_DEBUG" list element
#' containing additional information recorded by DSC, such as the
#' replicate id.
#'
#' @seealso \code{\link{dscquery}}
#'
#' @examples
#'
#' dsc.dir <- system.file("datafiles","one_sample_location",
#' "dsc_result",package = "dscrutils")
#' dat <- dscquery(dsc.dir,targets = "simulate",
#' module.output.file = "simulate")
#' out <- dscread(dsc.dir,dat$simulate.output.file[1])
#'
#' @importFrom tools file_ext
#' @importFrom yaml yaml.load_file
#'
#' @export
#'
dscread <- function (outdir, outfile) {

# Check the input arguments.
if (!(is.character(outdir) & length(outdir) == 1))
stop("Argument \"outdir\" should be a character vector of length 1")
if (!(is.character(outfile) & length(outfile) == 1))
stop("Argument \"outfile\" should be a character vector of length 1")

# Look for files with extensions "rds" and "pkl".
outfile <- path.expand(file.path(outdir,outfile))
rds <- paste0(outfile,".rds")
pkl <- paste0(outfile,".pkl")
if (file.exists(rds) & file.exists(pkl))
stop(sprintf(paste("Both %s and %s DSC output files exist; files should",
"be cleaned up by running \"dsc --clean\""),rds,pkl))
else if (file.exists(rds))

# Read from the .rds file.
out <- tryCatch(readRDS(rds),
error = function (e) {
warning(sprintf("Unable to read from %s; file may be corrupted",rds))
return(NULL)
})
else if (file.exists(pkl)) {

# Read from the .pkl file.
if (!requireNamespace("reticulate",quietly = TRUE))
stop("Cannot read from .pkl file due to missing reticulate package")
out <- tryCatch(reticulate::py_load_object(pkl),
error = function (e) {
warning(sprintf("Unable to read from %s; file may be corrupted",pkl))
return(NULL)
})

# This additional processing step is needed to convert more
# complex Python data structures such as a pandas data frames.
out <- rapply(out,reticulate::py_to_r,classes = "python.builtin.object",
how = "replace")
} else {
warning(sprintf(paste("Unable to read from DSC output file %s as one or",
"more files may be missing; returning NULL"),
outfile))
out <- NULL
}

# We may use this code in the future to read from YAML files:
#
# yaml.load_file(outfile)
#
return(out)
}

34 changes: 1 addition & 33 deletions dscrutils/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,6 @@ merge_lists <- function(x, y, ...)
return(x)
}

#' @importFrom tools file_ext
#' @importFrom yaml yaml.load_file
#' @export
#'
read_dsc <- function(infile) {
inext = file_ext(infile)
if (inext == "") {
for (item in c("rds", "pkl", "yml")) {
if (file.exists(paste0(infile, ".", item))) {
inext = item
infile = paste0(infile, ".", item)
break
}
}
}
if (inext == "") stop(paste("Cannot determine extension for input file", infile))
if (inext == 'pkl') {
## Do not use `importFrom` for reticulate because
## this is not always required
## and when it is required the DSC parser code
## will check and install the package.
if (!requireNamespace("reticulate",quietly = TRUE))
stop("Cannot read Python's `pkl` files due to missing `reticulate` package.")
result = reticulate::py_load_object(infile)
return(rapply(result, reticulate::py_to_r, classes = "python.builtin.object", how = "replace"))
} else if (inext == 'yml') {
return(yaml.load_file(infile))
} else {
return(readRDS(infile))
}
}

#' @export
load_inputs <- function(files, loader) {
if (length(files) == 1) {
Expand Down Expand Up @@ -107,4 +75,4 @@ source_dir <- function(folder, recursive = TRUE, ...)

source_dirs <- function(folders, ...) {
for (folder in folders) source_dir(folder, ...)
}
}
2 changes: 1 addition & 1 deletion dscrutils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ command-line shell:

```bash
R CMD build dscrutils
R CMD check --as-cran dscrutils_0.3.6.4.tar.gz
R CMD check --as-cran dscrutils_0.3.8.7.tar.gz
```

To update the package documentation from the
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
90 changes: 48 additions & 42 deletions dscrutils/man/dscquery.Rd

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

45 changes: 45 additions & 0 deletions dscrutils/man/dscread.Rd

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

Loading

0 comments on commit 3c95a9b

Please sign in to comment.