From 7ee26fc7cbb671735f7fda6cf44ea7ba18712c31 Mon Sep 17 00:00:00 2001 From: Gregory Jefferis Date: Mon, 1 Jul 2024 11:28:36 +0100 Subject: [PATCH] Add mirror_banc --- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/data.R | 12 ++++++++++ R/xform.R | 50 ++++++++++++++++++++++++++++++++++++++++ data-raw/mirror-banc.R | 17 ++++++++++++++ data/mirror_banc_lm.rda | Bin 0 -> 1699 bytes man/mirror_banc.Rd | 30 ++++++++++++++++++++++++ man/mirror_banc_lm.Rd | 21 +++++++++++++++++ 8 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 data-raw/mirror-banc.R create mode 100644 data/mirror_banc_lm.rda create mode 100644 man/mirror_banc.Rd create mode 100644 man/mirror_banc_lm.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 1134fb6..040ca43 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,8 @@ Imports: jsonlite, pbapply, dplyr, - checkmate + checkmate, + utils Suggests: testthat (>= 3.0.0), reticulate, diff --git a/NAMESPACE b/NAMESPACE index c99853b..a8409a8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(fanc_segid_from_cellid) export(fanc_set_token) export(fanc_voxdims) export(fanc_xyz2id) +export(mirror_banc) export(read_fanc_meshes) export(transform_fanc2manc) export(with_banc) diff --git a/R/data.R b/R/data.R index 8d47633..48d5530 100644 --- a/R/data.R +++ b/R/data.R @@ -45,3 +45,15 @@ #' boundingbox(BANC) "BANC" +#' Provisional mirroring registration for BANC (lanmarks+thin plate splines) +#' +#' @name mirror_banc_lm +#' @docType data +#' @description This is calibrated in nm. See \code{data-raw/mirror_banc.R} for +#' details of how it was generated. It is still provisional pending synpase +#' cloud-based registrations. +#' +#' @examples +#' utils::str(mirror_banc_lm) +"mirror_banc_lm" + diff --git a/R/xform.R b/R/xform.R index 3bf3e71..41be7ab 100644 --- a/R/xform.R +++ b/R/xform.R @@ -82,3 +82,53 @@ transform_fanc2manc <- function(x, inverse = F, ...) { reg = fanc_to_manc_reg() xform(x, reg=reg, swap=inverse,... ) } + + +#' Mirror points, neurons in BANC space +#' +#' @param x Points, neurons or other objects compatible with \code{xyzmatrix} +#' @param units Units for both input \emph{and} output data. +#' @param subset Optional argument to transform only a subset of a neuron list. +#' +#' @return Trge transformed object (calibrated according to the units argument) +#' @export +#' +#' @examples +#' BANC.surf.m <- mirror_banc(BANC.surf) +#' \dontrun{ +#' library(nat) +#' wire3d(BANC.surf) +#' # clearly not great in some places, especially optic lobe, but still useful +#' wire3d(BANC.surf.m, col='red') +#' } +mirror_banc <- function(x, units=c("nm", "microns", "raw"), subset=NULL) { + units=match.arg(units) + + if(!is.null(subset)) { + xs=x[subset] + xst=mirror_banc(xs, units = units, mirror_reg = fancr::mirror_banc_lm) + x[subset]=xst + return(x) + } + BANCmesh=templatebrain("BANC", + BoundingBox = structure(c(79392.9, 966179.9, 35524.5, 1131169.6, -62.8, 315466.7 + ), dim = 2:3, class = "boundingbox")) + # convert to nm if necessary + xyz=xyzmatrix(x) + if(units=='microns') + xyz=xyz*1e3 + else if(units=='raw') + xyz=banc_raw2nm(xyz) + + xyzf=mirror_brain(x, brain = BANCmesh, mirrorAxis = 'X', transform = 'flip') + xyzf2=xform(xyzf, reg = fancr::mirror_banc_lm) + + # convert from nm to original units if necessary + if(units=='microns') + xyzf2=xyzf2/1e3 + else if(units=='raw') + xyzf2=banc_nm2raw(xyzf2) + + xyzmatrix(x)=xyzf2 + x +} diff --git a/data-raw/mirror-banc.R b/data-raw/mirror-banc.R new file mode 100644 index 0000000..b78e594 --- /dev/null +++ b/data-raw/mirror-banc.R @@ -0,0 +1,17 @@ +# construct a mirroring registration from landmarks +mirror_banc_tps <- function(anns) { + if(is.character(anns)) { + anns=ngl_annotations(anns) + } + AB=rbind(banc_raw2nm(anns$pointA), banc_raw2nm(anns$pointB)) + BA=rbind(banc_raw2nm(anns$pointB), banc_raw2nm(anns$pointA)) + # have decided to use this rather than "official" space + BANCmesh=nat.templatebrains::templatebrain("BANC", + BoundingBox = structure(c(79392.9, 966179.9, 35524.5, 1131169.6, -62.8, 315466.7), dim = 2:3, class = "boundingbox")) + ABf=nat.templatebrains::mirror_brain(AB, brain = BANCmesh, mirrorAxis = 'X', transform = 'flip') + delta=BA-ABf + tpsreg(sample = ABf, reference = BA) +} +mirror_banc_lm=mirror_banc_tps('https://spelunker.cave-explorer.org/#!middleauth+https://global.daf-apis.com/nglstate/api/v1/5486159028289536') + +usethis::use_data(mirror_banc_lm, overwrite = TRUE) diff --git a/data/mirror_banc_lm.rda b/data/mirror_banc_lm.rda new file mode 100644 index 0000000000000000000000000000000000000000..ec09c03017987b0fa8e10f3298a2d4aabdd3fee3 GIT binary patch literal 1699 zcmV;U23+|p@-~Rvq|L@-K|Nrm5U;o4Z z?f(Dw|NqbhejI>lqD&|fNJ^w?Vm6fWgV8h|k*A7i^)zAwAYvLc^)hGyrh%p>sL(V6 zH5z(Brh`BRA&p0S)knXvkt90MG%T0000CKr{di0000P8esqc82|tP z000Jn0000020%0hnkdqs&@^ZO4FRA48UO=80MG*<007Vc&;S}5G#UT^G6A5_4FCWD z00;my0B8UJ00R&W00Te(00u^wKmZ0n000000iXZ?0004y4FIYnQzDq7%`lp15_wNe zCL!v2Jy88q(t4gm2jx#ujG0eU#WG{mm}#PVo+;>o=A_Z;Y3h1_VKe~Rk16TuJw(|x zGJ2kdrf5$@$&r&x(GOGFr#GLzXC8jxsQ0@}=c~Wd;j_<<*SBltO5Y|#T*hu$baHI$ zSDLW6RIz&~*W*)S#!XoFQr|qcF6deJyJ4?f7#Y3DQevpm;ycz-(pMX5jGn5ioInW1$0g?b8fH~xVI57pmLxbVvNm)^0 zQpsh2<=br=KWzw2Y8nZrXITW?eh-YZ4JlU!kC75g=VrE1@$QqdgyR;hzo z*)dhSr%^*aFZ!J-)swnjHdgjyFDt&C*0GhjU)H71q@l=Y)Q6b4rad{PmXA(-1|>f0 zexZDk>7{uQL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index 0000000..7fba4d9 --- /dev/null +++ b/man/mirror_banc.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/xform.R +\name{mirror_banc} +\alias{mirror_banc} +\title{Mirror points, neurons in BANC space} +\usage{ +mirror_banc(x, units = c("nm", "microns", "raw"), subset = NULL) +} +\arguments{ +\item{x}{Points, neurons or other objects compatible with \code{xyzmatrix}} + +\item{units}{Units for both input \emph{and} output data.} + +\item{subset}{Optional argument to transform only a subset of a neuron list.} +} +\value{ +Trge transformed object (calibrated according to the units argument) +} +\description{ +Mirror points, neurons in BANC space +} +\examples{ +BANC.surf.m <- mirror_banc(BANC.surf) +\dontrun{ +library(nat) +wire3d(BANC.surf) +# clearly not great in some places, especially optic lobe, but still useful +wire3d(BANC.surf.m, col='red') +} +} diff --git a/man/mirror_banc_lm.Rd b/man/mirror_banc_lm.Rd new file mode 100644 index 0000000..6d6a491 --- /dev/null +++ b/man/mirror_banc_lm.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/data.R +\docType{data} +\name{mirror_banc_lm} +\alias{mirror_banc_lm} +\title{Provisional mirroring registration for BANC (lanmarks+thin plate splines)} +\format{ +An object of class \code{tpsreg} of length 2. +} +\usage{ +mirror_banc_lm +} +\description{ +This is calibrated in nm. See \code{data-raw/mirror_banc.R} for +details of how it was generated. It is still provisional pending synpase +cloud-based registrations. +} +\examples{ +utils::str(mirror_banc_lm) +} +\keyword{datasets}