Skip to content
Snippets Groups Projects
Commit ebc0e307 authored by Luke Zappia's avatar Luke Zappia
Browse files

Add getParams, change updateParams to setParams

parent c695d501
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
S3method(print,splatParams) S3method(print,splatParams)
export(checkParams) export(checkParams)
export(defaultParams) export(defaultParams)
export(getParams)
export(mergeParams) export(mergeParams)
export(setParams)
export(splatParams) export(splatParams)
export(updateParams)
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#' S3 class for holding Splatter simulation parameters. #' S3 class for holding Splatter simulation parameters.
#' #'
#' @param ... parameters to set in the new params object, passed to #' @param ... parameters to set in the new params object, passed to
#' \code{\link{updateParams}}. #' \code{\link{setParams}}.
#' #'
#' @details #' @details
#' The splatParams object is a list based S3 object for holding simulation #' The splatParams object is a list based S3 object for holding simulation
...@@ -108,7 +108,7 @@ splatParams <- function(...) { ...@@ -108,7 +108,7 @@ splatParams <- function(...) {
class(params) <- "splatParams" class(params) <- "splatParams"
params <- updateParams(params, ...) params <- setParams(params, ...)
return(params) return(params)
} }
...@@ -157,6 +157,106 @@ print.splatParams <- function(x, ...) { ...@@ -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 splatParams object
#' #'
#' Check that a splatParams object has valid parameter values. #' Check that a splatParams object has valid parameter values.
...@@ -260,57 +360,6 @@ checkParams <- function(params) { ...@@ -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
#' #'
#' Merge two splatParams objects. Any parameters that are NA in the first #' Merge two splatParams objects. Any parameters that are NA in the first
...@@ -355,7 +404,7 @@ defaultParams <- function() { ...@@ -355,7 +404,7 @@ defaultParams <- function() {
params <- splatParams() 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, groupCells = 100, mean.rate = 0.3, mean.shape = 0.4,
lib.loc = 10, lib.scale = 0.5, out.prob = 0.1, lib.loc = 10, lib.scale = 0.5, out.prob = 0.1,
out.loProb = 0.5, out.facLoc = 4, out.facScale = 1, out.loProb = 0.5, out.facLoc = 4, out.facScale = 1,
......
% 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"))
}
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/params.R % Please edit documentation in R/params.R
\name{updateParams} \name{setParams}
\alias{updateParams} \alias{setParams}
\title{Update a splatParams object} \title{Update a splatParams object}
\usage{ \usage{
updateParams(params, ...) setParams(params, ...)
} }
\arguments{ \arguments{
\item{params}{the splatParams object to update.} \item{params}{the splatParams object to update.}
\item{...}{Any parameters to update.} \item{...}{Any parameters to set.}
} }
\value{ \value{
splatParms object with updated parameters splatParms object with updated parameters
} }
\description{ \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{ \details{
This function allows multiple parameters to be updated or set using a single This function allows multiple parameters to be updated or set using a single
simple function call. Parameters to update are specified by supplying simple function call. Parameters to update are specified by supplying
additional arguments that follow the levels of the splatParams data structure additional arguments that follow the levels of the splatParams data structure
separated by the "." character. For example 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 \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 equivalent to \code{params$mean$rate <- 1}. For more details of the available
parameters and the splatParams data structure see \code{\link{splatParams}}. parameters and the splatParams data structure see \code{\link{splatParams}}.
...@@ -31,10 +31,10 @@ 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 <- defaultParams()
params params
# Set nGenes and nCells # Set nGenes and nCells
params <- updateParams(params, nGenes = 1000, nCells = 200) params <- setParams(params, nGenes = 1000, nCells = 200)
params params
# Set mean rate paramater and library size location parameter # 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 params
} }
...@@ -8,7 +8,7 @@ splatParams(...) ...@@ -8,7 +8,7 @@ splatParams(...)
} }
\arguments{ \arguments{
\item{...}{parameters to set in the new params object, passed to \item{...}{parameters to set in the new params object, passed to
\code{\link{updateParams}}.} \code{\link{setParams}}.}
} }
\value{ \value{
List based S3 splatParams object List based S3 splatParams object
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment