From ebc0e30741475c9ee4bd19237f5c9adc2f85c49d Mon Sep 17 00:00:00 2001
From: Luke Zappia <lazappi@users.noreply.github.com>
Date: Wed, 5 Oct 2016 16:53:24 +1100
Subject: [PATCH] Add getParams, change updateParams to setParams

---
 NAMESPACE                             |   3 +-
 R/params.R                            | 157 +++++++++++++++++---------
 man/getParams.Rd                      |  32 ++++++
 man/{updateParams.Rd => setParams.Rd} |  16 +--
 man/splatParams.Rd                    |   2 +-
 5 files changed, 146 insertions(+), 64 deletions(-)
 create mode 100644 man/getParams.Rd
 rename man/{updateParams.Rd => setParams.Rd} (71%)

diff --git a/NAMESPACE b/NAMESPACE
index a2b88a0..d796b88 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -3,6 +3,7 @@
 S3method(print,splatParams)
 export(checkParams)
 export(defaultParams)
+export(getParams)
 export(mergeParams)
+export(setParams)
 export(splatParams)
-export(updateParams)
diff --git a/R/params.R b/R/params.R
index a47d746..058c6a2 100644
--- a/R/params.R
+++ b/R/params.R
@@ -3,7 +3,7 @@
 #' S3 class for holding Splatter simulation parameters.
 #'
 #' @param ... parameters to set in the new params object, passed to
-#'            \code{\link{updateParams}}.
+#'            \code{\link{setParams}}.
 #'
 #' @details
 #' The splatParams object is a list based S3 object for holding simulation
@@ -108,7 +108,7 @@ splatParams <- function(...) {
 
     class(params) <- "splatParams"
 
-    params <- updateParams(params, ...)
+    params <- setParams(params, ...)
 
     return(params)
 }
@@ -157,6 +157,106 @@ print.splatParams <- function(x, ...) {
     }
 }
 
+#' Update a splatParams object
+#'
+#' Set any of the parameters in a splatParams object to have a new value.
+#'
+#' @param params the splatParams object to update.
+#' @param ... Any parameters to set.
+#'
+#' @details
+#' This function allows multiple parameters to be updated or set using a single
+#' simple function call. Parameters to update are specified by supplying
+#' additional arguments that follow the levels of the splatParams data structure
+#' separated by the "." character. For example
+#' \code{setParams(params, nGenes = 100)} is equivalent to
+#' \code{params$nGenes <- 100} and \code{update(params, mean.rate = 1)} is
+#' equivalent to \code{params$mean$rate <- 1}. For more details of the available
+#' parameters and the splatParams data structure see \code{\link{splatParams}}.
+#'
+#' @return splatParms object with updated parameters
+#' @examples
+#' params <- defaultParams()
+#' params
+#' # Set nGenes and nCells
+#' params <- setParams(params, nGenes = 1000, nCells = 200)
+#' params
+#' # Set mean rate paramater and library size location parameter
+#' params <- setParams(params, mean.rate = 1, lib.loc = 12)
+#' params
+#' @export
+setParams <- function(params, ...) {
+
+    update <- list(...)
+
+    if (length(update) == 0) {
+        return(params)
+    }
+
+    names <- strsplit(names(update), ".", fixed = TRUE)
+
+    for (idx in seq_along(names)) {
+        name <- names[[idx]]
+        value <- update[[idx]]
+        if (length(name) == 1) {
+            params[[name]] <- value
+        } else {
+            params[[name[1]]][[name[2]]] <- value
+        }
+    }
+
+    return(params)
+}
+
+#' Get parameters from splatParams object
+#'
+#' Get values for the parameters in a splatParams object. Uses the same pattern
+#' (category.parameter) as \code{\link{setParams}}.
+#'
+#' @param params splatParams object to get parameters from.
+#' @param names vector of parameter names to extract.
+#'
+#' @return Vector if all selected parameters are single values, otherwise a
+#'         list.
+#' @examples
+#' params <- defaultParams()
+#' # Get the number of genes
+#' getParams(params, "nGenes")
+#' # Get the number of genes and the mean rate parameter
+#' getParams(params, c("nGenes", "mean.rate"))
+#' # Returns a list if one of the selected parameters is a vector
+#' params <- setParams(params, groupCells = c(100, 200))
+#' getParams(parans, c("nGenes", "mean.rate", "groupCells"))
+#' @export
+getParams <- function(params, names) {
+
+    if (length(names) == 0) {
+        return(NULL)
+    }
+
+    output <- list()
+    keep.list <- FALSE
+    for (idx in seq_along(names)) {
+        name <- names[[idx]]
+        name.split <- strsplit(name, ".", fixed = TRUE)[[1]]
+        if (length(name) == 1) {
+            value <- params[[name.split]]
+        } else {
+            value <- params[[name.split[1]]][[name.split[2]]]
+        }
+        output[[name]] <- value
+        if (length(value) > 1) {
+            keep.list <- TRUE
+        }
+    }
+
+    if (!keep.list) {
+        output <- unlist(output)
+    }
+
+    return(output)
+}
+
 #' Check splatParams object
 #'
 #' Check that a splatParams object has valid parameter values.
@@ -260,57 +360,6 @@ checkParams <- function(params) {
     }
 }
 
-#' Update a splatParams object
-#'
-#' Update any of the parameters in a splatParams object to have a new value.
-#'
-#' @param params the splatParams object to update.
-#' @param ... Any parameters to update.
-#'
-#' @details
-#' This function allows multiple parameters to be updated or set using a single
-#' simple function call. Parameters to update are specified by supplying
-#' additional arguments that follow the levels of the splatParams data structure
-#' separated by the "." character. For example
-#' \code{updateParams(params, nGenes = 100)} is equivalent to
-#' \code{params$nGenes <- 100} and \code{update(params, mean.rate = 1)} is
-#' equivalent to \code{params$mean$rate <- 1}. For more details of the available
-#' parameters and the splatParams data structure see \code{\link{splatParams}}.
-#'
-#' @return splatParms object with updated parameters
-#' @examples
-#' params <- defaultParams()
-#' params
-#' # Set nGenes and nCells
-#' params <- updateParams(params, nGenes = 1000, nCells = 200)
-#' params
-#' # Set mean rate paramater and library size location parameter
-#' params <- updateParams(params, mean.rate = 1, lib.loc = 12)
-#' params
-#' @export
-updateParams <- function(params, ...) {
-
-    update <- list(...)
-
-    if (length(update) == 0) {
-        return(params)
-    }
-
-    update.names <- strsplit(names(update), ".", fixed = TRUE)
-
-    for (idx in 1:length(update)) {
-        update.name <- update.names[[idx]]
-        value <- update[[idx]]
-        if (length(update.name) == 1) {
-            params[[update.name]] <- value
-        } else {
-            params[[update.name[1]]][[update.name[2]]] <- value
-        }
-    }
-
-    return(params)
-}
-
 #' Merge two splatParams objects
 #'
 #' Merge two splatParams objects. Any parameters that are NA in the first
@@ -355,7 +404,7 @@ defaultParams <- function() {
 
     params <- splatParams()
 
-    params <- updateParams(params, nGenes = 10000, nCells = 100,
+    params <- setParams(params, nGenes = 10000, nCells = 100,
                            groupCells = 100, mean.rate = 0.3, mean.shape = 0.4,
                            lib.loc = 10, lib.scale = 0.5, out.prob = 0.1,
                            out.loProb = 0.5, out.facLoc = 4, out.facScale = 1,
diff --git a/man/getParams.Rd b/man/getParams.Rd
new file mode 100644
index 0000000..6623ec4
--- /dev/null
+++ b/man/getParams.Rd
@@ -0,0 +1,32 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/params.R
+\name{getParams}
+\alias{getParams}
+\title{Get parameters from splatParams object}
+\usage{
+getParams(params, names)
+}
+\arguments{
+\item{params}{splatParams object to get parameters from.}
+
+\item{names}{vector of parameter names to extract.}
+}
+\value{
+Vector if all selected parameters are single values, otherwise a
+        list.
+}
+\description{
+Get values for the parameters in a splatParams object. Uses the same pattern
+(category.parameter) as \code{\link{setParams}}.
+}
+\examples{
+params <- defaultParams()
+# Get the number of genes
+getParams(params, "nGenes")
+# Get the number of genes and the mean rate parameter
+getParams(params, c("nGenes", "mean.rate"))
+# Returns a list if one of the selected parameters is a vector
+params <- setParams(params, groupCells = c(100, 200))
+getParams(parans, c("nGenes", "mean.rate", "groupCells"))
+}
+
diff --git a/man/updateParams.Rd b/man/setParams.Rd
similarity index 71%
rename from man/updateParams.Rd
rename to man/setParams.Rd
index 965ac85..6b8f571 100644
--- a/man/updateParams.Rd
+++ b/man/setParams.Rd
@@ -1,28 +1,28 @@
 % Generated by roxygen2: do not edit by hand
 % Please edit documentation in R/params.R
-\name{updateParams}
-\alias{updateParams}
+\name{setParams}
+\alias{setParams}
 \title{Update a splatParams object}
 \usage{
-updateParams(params, ...)
+setParams(params, ...)
 }
 \arguments{
 \item{params}{the splatParams object to update.}
 
-\item{...}{Any parameters to update.}
+\item{...}{Any parameters to set.}
 }
 \value{
 splatParms object with updated parameters
 }
 \description{
-Update any of the parameters in a splatParams object to have a new value.
+Set any of the parameters in a splatParams object to have a new value.
 }
 \details{
 This function allows multiple parameters to be updated or set using a single
 simple function call. Parameters to update are specified by supplying
 additional arguments that follow the levels of the splatParams data structure
 separated by the "." character. For example
-\code{updateParams(params, nGenes = 100)} is equivalent to
+\code{setParams(params, nGenes = 100)} is equivalent to
 \code{params$nGenes <- 100} and \code{update(params, mean.rate = 1)} is
 equivalent to \code{params$mean$rate <- 1}. For more details of the available
 parameters and the splatParams data structure see \code{\link{splatParams}}.
@@ -31,10 +31,10 @@ parameters and the splatParams data structure see \code{\link{splatParams}}.
 params <- defaultParams()
 params
 # Set nGenes and nCells
-params <- updateParams(params, nGenes = 1000, nCells = 200)
+params <- setParams(params, nGenes = 1000, nCells = 200)
 params
 # Set mean rate paramater and library size location parameter
-params <- updateParams(params, mean.rate = 1, lib.loc = 12)
+params <- setParams(params, mean.rate = 1, lib.loc = 12)
 params
 }
 
diff --git a/man/splatParams.Rd b/man/splatParams.Rd
index e1bf1b4..c8963c5 100644
--- a/man/splatParams.Rd
+++ b/man/splatParams.Rd
@@ -8,7 +8,7 @@ splatParams(...)
 }
 \arguments{
 \item{...}{parameters to set in the new params object, passed to
-\code{\link{updateParams}}.}
+\code{\link{setParams}}.}
 }
 \value{
 List based S3 splatParams object
-- 
GitLab