diff --git a/DESCRIPTION b/DESCRIPTION index 5d9b6ad6..36b1583a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -38,6 +38,7 @@ Imports: checkmate, stringr, pbapply, + units, zip Suggests: spelling, diff --git a/R/neuron.R b/R/neuron.R index d54782cf..448a34d1 100644 --- a/R/neuron.R +++ b/R/neuron.R @@ -33,6 +33,7 @@ #' @param NeuronName Character vector containing name of neuron or a function #' with one argument (the full path) which returns the name. The default #' (\code{NULL}) sets NeuronName to the file name without the file extension. +#' @param unit a unit #' @param MD5 Logical indicating whether to calculate MD5 hash of input #' @importFrom tools md5sum #' @examples @@ -82,8 +83,8 @@ #' plot(gstem2) #' plot(as.neuron(gstem2)) neuron<-function(d, NumPoints=nrow(d), StartPoint, BranchPoints=integer(), EndPoints, - SegList, SubTrees=NULL, InputFileName=NULL, NeuronName=NULL, ..., - MD5=TRUE){get + SegList, SubTrees=NULL, InputFileName=NULL, NeuronName=NULL, + unit=NULL, ..., MD5=TRUE){get coreFieldOrder=c("NumPoints", "StartPoint", "BranchPoints", "EndPoints", "nTrees", "NumSegs", "SegList", "SubTrees","d" ) @@ -202,6 +203,7 @@ normalise_swc<-function(x, requiredColumns=c('PointNo','Label','X','Y','Z','W',' #' @param origin Root vertex, matched against names (aka PointNo) when #' available (see details) #' @param Verbose Whether to be verbose (default: FALSE) +#' @param unit a unit #' @return A list with elements: #' (NumPoints,StartPoint,BranchPoints,EndPoints,nTrees,NumSegs,SegList, #' [SubTrees]) NB SubTrees will only be present when nTrees>1. @@ -209,7 +211,7 @@ normalise_swc<-function(x, requiredColumns=c('PointNo','Label','X','Y','Z','W',' #' @importFrom igraph V V<- vcount decompose.graph #' @rdname neuron #' @seealso \code{\link{graph.dfs}, \link{as.seglist}} -as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){ +as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, unit=NULL, ...){ # translate origin into raw vertex id if necessary if(length(origin)){ vertex_names=igraph::V(x)$name @@ -263,6 +265,7 @@ as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){ # TODO refactor this into a separate function e.g. normalise.swc since # we need to do something similar in as.neuron.dataframe and seglist2swc etc d=data.frame(PointNo=get.vertex.attribute(x,'name')) + if(is.null(vertexData)){ # get vertex information from graph object xyz=xyzmatrix(x) @@ -290,6 +293,8 @@ as.neuron.ngraph<-function(x, vertexData=NULL, origin=NULL, Verbose=FALSE, ...){ d=seglist2swc(x=subtrees,d=d) d=normalise_swc(d) + if (!is.null(unit)) + d=assign_unit(d, unit) n=list(d=d,NumPoints=igraph::vcount(masterg), StartPoint=StartPoint, BranchPoints=branchpoints(masterg, original.ids='vid'), @@ -1615,3 +1620,23 @@ reroot.neuronlist<-function(x, idx=NULL, pointno=NULL, point=NULL, ...){ } res } + +#' Assigns a unit to the distance matrix +#' +#' @param d distance matrix +#' @param unit a unit +#' +#' @importFrom units set_units +assign_unit <- function(d, unit = NULL) { + for (k in c("X", "Y", "Z", "W")) { + d[[k]] <- set_units(d[[k]], unit, mode = "standard") + } + d +} + +# Set unit for a neuron +#' @export +set_units.neuron <- function(n, unit = NULL, ...) { + n$d <- assign_unit(n$d, unit) + n +} diff --git a/R/neuronlist.R b/R/neuronlist.R index f655898f..f40d477f 100644 --- a/R/neuronlist.R +++ b/R/neuronlist.R @@ -1132,3 +1132,9 @@ prune_twigs.neuronlist <- function(x, twig_length, OmitFailures=NA, ...) { if(missing(twig_length)) stop("Missing twig_length argument") nlapply(x, prune_twigs, twig_length=twig_length, OmitFailures=OmitFailures, ...) } + +# Set unit for a neuronlist +#' @export +set_units.neuronlist <- function(nl, unit = NULL, ...) { + nlapply(nl, function(x) set_units.neuron(x, unit = unit)) +} diff --git a/tests/testthat/test-neuron.R b/tests/testthat/test-neuron.R index 519173ee..b60597d6 100644 --- a/tests/testthat/test-neuron.R +++ b/tests/testthat/test-neuron.R @@ -396,3 +396,11 @@ test_that("rerooting neuronlist", { rpns=reroot.neuronlist(pns, point = points) expect_equal(rpns[[1]]$StartPoint, 12) }) + +test_that("set_units neuron", { + pn<-Cell07PNs[[1]] + pnu<-set_units.neuron(pn, 'um') + # units setting works correctly + expect_equal(class(pnu$d$X), "units") + expect_equal(class(pnu$d$W), "units") +})