From 80f43729fa91b5fa69a16a182d65fc6272dbd6af Mon Sep 17 00:00:00 2001 From: alexanderbates Date: Fri, 12 Jul 2024 19:03:37 -0400 Subject: [PATCH] Created ggneuron --- DESCRIPTION | 9 ++-- NAMESPACE | 1 + R/cloudvolume.R | 2 +- R/ggplot2.R | 112 +++++++++++++++++++++++++++++++++------ R/synapses.R | 6 +-- man/banc_all_synapses.Rd | 3 +- man/geom_neuron.Rd | 24 ++++----- man/ggneuron.Rd | 65 +++++++++++++++++++++++ 8 files changed, 183 insertions(+), 39 deletions(-) create mode 100644 man/ggneuron.Rd diff --git a/DESCRIPTION b/DESCRIPTION index c6f66c2..390631b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -42,7 +42,8 @@ Imports: RSQLite, readr, checkmate, - utils + utils, + ggplot2 (>= 3.5.1.9000) Suggests: testthat (>= 3.0.0), reticulate, @@ -53,10 +54,10 @@ Suggests: rmarkdown, spelling, arrow, - ggplot2, - ggnewscale, + ggnewscale (>= 0.4.10.9000), ggpubr, - catmaid + catmaid, + grDevices Remotes: natverse/nat, natverse/fafbseg, diff --git a/NAMESPACE b/NAMESPACE index 83623a7..63d6db9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -57,6 +57,7 @@ export(choose_banc) export(dr_banc) export(elastix_xform) export(geom_neuron) +export(ggneuron) export(ggplot2_neuron_path) export(navis_elastix_xform) export(with_banc) diff --git a/R/cloudvolume.R b/R/cloudvolume.R index bb5c759..66bb651 100644 --- a/R/cloudvolume.R +++ b/R/cloudvolume.R @@ -56,7 +56,7 @@ dr_banc <- function() { cat("\n\n") res = fafbseg:::py_report() cat("\n") - try(fafbseg:::check_cloudvolume_reticulate(min_version = "3.12")) + try(fafbseg:::check_cloudvolume_reticulate(min_version = "8.32.1")) invisible(res) } diff --git a/R/ggplot2.R b/R/ggplot2.R index 030eb3b..c0b9505 100644 --- a/R/ggplot2.R +++ b/R/ggplot2.R @@ -97,10 +97,10 @@ banc_neuron_comparison_plot <- function(neuron1, # Create the plot p <- ggplot2::ggplot() + - geom_neuron.mesh3d(x = mesh, rotation_matrix = rotation_matrix, alpha = 0.05, low = "grey95", high = "grey50") + + geom_neuron.mesh3d(x = mesh, rotation_matrix = rotation_matrix, alpha = 0.05, low = "grey90", high = "grey50") + geom_neuron(x=neuron_pruned1, rotation_matrix = rotation_matrix, low = "turquoise", high = "navy", alpha = 0.5, linewidth = 0.3) + - geom_neuron(x=neuron_pruned2, rotation_matrix = rotation_matrix, low = "red", high = "darkred", alpha = 0.5, linewidth = 0.3) + - geom_neuron(x=neuron_pruned3, rotation_matrix = rotation_matrix, low = "chartreuse", high = "darkgreen", alpha = 0.5, linewidth = 0.3) + + geom_neuron(x=neuron_pruned2, rotation_matrix = rotation_matrix, low = "darkred", high = "coral", alpha = 0.5, linewidth = 0.3) + + geom_neuron(x=neuron_pruned3, rotation_matrix = rotation_matrix, low = "darkgreen", high = "chartreuse", alpha = 0.5, linewidth = 0.3) + ggplot2::coord_fixed() + ggplot2::theme_void() + ggplot2::guides(fill="none",color="none") + @@ -293,17 +293,16 @@ ggplot2_neuron_path.mesh3d <- function(x, rotation_matrix = NULL, ...) { #' #' @importFrom rlang .data #' @export -geom_neuron <-function(x, rotation_matrix = NULL, low = "turquoise", high = "navy", +geom_neuron <-function(x, rotation_matrix = NULL, low = "navy", high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = FALSE, ...) UseMethod('geom_neuron') #' @rdname geom_neuron #' @method geom_neuron neuron #' @export -geom_neuron.neuron <- function(x = NULL, rotation_matrix = NULL, low = "turquoise", high = "navy", +geom_neuron.neuron <- function(x = NULL, rotation_matrix = NULL, low = "navy", high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = FALSE, ...) { - check_package_available('ggnewscale') check_package_available('catmaid') soma <- catmaid::soma(x) @@ -328,21 +327,28 @@ geom_neuron.neuron <- function(x = NULL, rotation_matrix = NULL, low = "turquois #' @rdname geom_neuron #' @method geom_neuron neuronlist #' @export -geom_neuron.neuronlist <- function(x = NULL, rotation_matrix = NULL, low = "turquoise", high = "navy", +geom_neuron.neuronlist <- function(x = NULL, rotation_matrix = NULL, low = "navy", high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = FALSE, ...) { - x <- ggplot2_neuron_path.neuronlist(x, rotation_matrix = rotation_matrix) - list( - ggplot2::geom_path(mapping = ggplot2::aes(x = .data$X, y = .data$Y, color = .data$id, group = .data$group), data = x, - stat = stat, position = position, na.rm = na.rm, - show.legend = show.legend, inherit.aes = inherit.aes, ...) - ) + if(length(x)>1){ + color_palette <- grDevices::colorRampPalette(c(low, high))(length(x)) + } + glist <- list() + for(i in 1:length(x)){ + if(length(x)>1){ + low<-high<-color_palette[i] + } + glist[[i]] <- geom_neuron(x = x[[i]], rotation_matrix = rotation_matrix, low = low, high = high, + stat = stat, position = position, na.rm = na.rm, show.legend = show.legend, + inherit.aes = FALSE, ...) + } + glist } #' @rdname geom_neuron #' @method geom_neuron mesh3d #' @export -geom_neuron.mesh3d <- function(x = NULL, rotation_matrix = NULL, low = "turquoise", high = "navy", +geom_neuron.mesh3d <- function(x = NULL, rotation_matrix = NULL, low = "navy", high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = FALSE, ...) { check_package_available('ggnewscale') @@ -360,7 +366,7 @@ geom_neuron.mesh3d <- function(x = NULL, rotation_matrix = NULL, low = "turquois #' @rdname geom_neuron #' @method geom_neuron hxsurf #' @export -geom_neuron.hxsurf <- function(x = NULL, rotation_matrix = NULL, low = "turquoise", high = "navy", +geom_neuron.hxsurf <- function(x = NULL, rotation_matrix = NULL, low = "navy", high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = FALSE, ...) { x <- rgl::as.mesh3d(x) @@ -372,7 +378,7 @@ geom_neuron.hxsurf <- function(x = NULL, rotation_matrix = NULL, low = "turquois #' @rdname geom_neuron #' @method geom_neuron NULL #' @export -geom_neuron.NULL <- function(x = NULL, rotation_matrix = NULL, low = "turquoise", high = "navy", +geom_neuron.NULL <- function(x = NULL, rotation_matrix = NULL, low = "navy", high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = FALSE, ...) { list( @@ -380,6 +386,80 @@ geom_neuron.NULL <- function(x = NULL, rotation_matrix = NULL, low = "turquoise" ) } +#' Create a ggplot2 Visualisation of Neuron Objects +#' +#' @description +#' This function creates a complete ggplot2 visualization for neuron objects, +#' including 'neuron', 'neuronlist', 'mesh3d', and 'hxsurf' objects. It sets up +#' a minimal theme and applies consistent styling to the plot. +#' +#' @param x A 'neuron', 'neuronlist', 'mesh3d', or 'hxsurf' object to be visualized. +#' @param volume a brain/neuropil volume to be plotted in grey, for context. +#' Defaults to NULL, no volume plotted. +#' @param info Optional. A string to be used as the plot title. +#' @param rotation_matrix An optional 4x4 rotation matrix to apply to the neuron coordinates. +#' @param low Color for the lowest Z values. Default is "turquoise". +#' @param high Color for the highest Z values. Default is "navy". +#' @param alpha Transparency of the neuron visualization. Default is 0.5. +#' @param title.col Color of the plot title. Default is "darkgrey". +#' @param ... Additional arguments passed to geom_neuron(). +#' +#' @return A ggplot2 object representing the neuron visualization. +#' +#' @details +#' This function wraps around geom_neuron() to create a complete plot with a +#' consistent, minimal theme. It removes axes, legends, and other extraneous +#' elements to focus on the neuron visualization itself. +#' +#' @examples +#' \dontrun{ +#' # Visualize the banc volume +#' ggneuron(banc_neuropil.surf, banc.surf) +#' +#' # Visualize the banc brain neuropil +#' ggneuron(banc_neuropil.surf, +#' rotation_matrix = bancr:::banc_rotation_matrices[["front"]]) +#' } +#' +#' @seealso +#' \code{\link{geom_neuron}} for the underlying geom used in this function. +#' +#' @export +ggneuron <- function(x, + volume = NULL, + info = NULL, + rotation_matrix = NULL, + low = "turquoise", + high = "navy", + alpha = 0.5, + title.col = "darkgrey", + ...){ + ggplot2::ggplot() + + {if(!is.null(volume)){ + geom_neuron(x = volume, rotation_matrix = rotation_matrix, alpha = min(alpha-0.25,0.01), low = "grey75", high = "grey50") + }} + + geom_neuron(x = x, rotation_matrix = rotation_matrix, low = low, high = high, alpha = alpha, ...) + + ggplot2::coord_fixed() + + ggplot2::theme_void() + + ggplot2::guides(fill="none",color="none") + + ggplot2::theme(legend.position = "none", + plot.title = ggplot2::element_text(hjust = 0, size = 8, face = "bold", colour = title.col), + axis.title.x=ggplot2::element_blank(), + axis.text.x=ggplot2::element_blank(), + axis.ticks.x=ggplot2::element_blank(), + axis.title.y=ggplot2::element_blank(), + axis.text.y=ggplot2::element_blank(), + axis.ticks.y=ggplot2::element_blank(), + axis.line = ggplot2::element_blank(), + panel.grid.major = ggplot2::element_blank(), + panel.grid.minor = ggplot2::element_blank(), + plot.margin = ggplot2::margin(0, 0, 0, 0), + panel.spacing = ggplot2::unit(0, "cm"), + panel.border = ggplot2::element_blank(), + panel.background = ggplot2::element_blank(), #gplot2::element_rect(fill = "grey95", color = NA), + plot.background = ggplot2::element_blank()) + #gplot2::element_rect(fill = "grey95", color = NA)) + ggplot2::labs(title = info) +} diff --git a/R/synapses.R b/R/synapses.R index ccb39de..8d862c2 100644 --- a/R/synapses.R +++ b/R/synapses.R @@ -11,7 +11,7 @@ #' @param details Logical Whether or not to read all data columns in the target synapse \code{.csv}. Defaults to #' \code{FALSE} in order to read only the essential presynapse position data. #' @param min_size Numeric, filter parameter, the minimum size (in nm) of the detected synapse. -#' @param rawcoords Logical, whether or not yto convert from raw coordinates into nanometers. Default is `FALSE`. +#' @param rawcoords Logical, whether or not to convert from raw coordinates into nanometers. Default is `FALSE`. #' #' @return a data.frame #' @@ -22,7 +22,6 @@ #' \dontrun{ #' syns <- banc_all_synapses() #' } -#' # Helpful scene: https://spelunker.cave-explorer.org/#!middleauth+https://global.daf-apis.com/nglstate/api/v1/4753860997414912 banc_all_synapses <- function(path = "gs://zetta_lee_fly_cns_001_synapse/240623_run/assignment/final_edgelist.df", overwrite = FALSE, n_max = 2000, @@ -112,8 +111,7 @@ banc_all_synapses <- function(path = "gs://zetta_lee_fly_cns_001_synapse/240623_ dplyr::tbl(src = con, from = "synapses") } -# Helpful scene: https://spelunker.cave-explorer.org/#!middleauth+https://global.daf-apis.com/ -# nglstate/api/v1/4753860997414912 +# Helpful scene: https://spelunker.cave-explorer.org/#!middleauth+https://global.daf-apis.com/nglstate/api/v1/4753860997414912 # # googleCloudStorageR # banc_gcs_read <- function(path = "gs://zetta_lee_fly_cns_001_synapse/240623_run/assignment/final_edgelist.df"){ diff --git a/man/banc_all_synapses.Rd b/man/banc_all_synapses.Rd index 940282e..c7b34f6 100644 --- a/man/banc_all_synapses.Rd +++ b/man/banc_all_synapses.Rd @@ -26,7 +26,7 @@ a taster of the file.} \item{min_size}{Numeric, filter parameter, the minimum size (in nm) of the detected synapse.} -\item{rawcoords}{Logical, whether or not yto convert from raw coordinates into nanometers. Default is \code{FALSE}.} +\item{rawcoords}{Logical, whether or not to convert from raw coordinates into nanometers. Default is \code{FALSE}.} } \value{ a data.frame @@ -43,7 +43,6 @@ lazily so as not to throw the whole thing into system memory. \dontrun{ syns <- banc_all_synapses() } -# Helpful scene: https://spelunker.cave-explorer.org/#!middleauth+https://global.daf-apis.com/nglstate/api/v1/4753860997414912 } \seealso{ \code{\link{banc_partner_summary}}, \code{\link{banc_partners}} diff --git a/man/geom_neuron.Rd b/man/geom_neuron.Rd index 3108c8f..24b6ff0 100644 --- a/man/geom_neuron.Rd +++ b/man/geom_neuron.Rd @@ -12,8 +12,8 @@ geom_neuron( x, rotation_matrix = NULL, - low = "turquoise", - high = "navy", + low = "navy", + high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, @@ -25,8 +25,8 @@ geom_neuron( \method{geom_neuron}{neuron}( x = NULL, rotation_matrix = NULL, - low = "turquoise", - high = "navy", + low = "navy", + high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, @@ -38,8 +38,8 @@ geom_neuron( \method{geom_neuron}{neuronlist}( x = NULL, rotation_matrix = NULL, - low = "turquoise", - high = "navy", + low = "navy", + high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, @@ -51,8 +51,8 @@ geom_neuron( \method{geom_neuron}{mesh3d}( x = NULL, rotation_matrix = NULL, - low = "turquoise", - high = "navy", + low = "navy", + high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, @@ -64,8 +64,8 @@ geom_neuron( \method{geom_neuron}{hxsurf}( x = NULL, rotation_matrix = NULL, - low = "turquoise", - high = "navy", + low = "navy", + high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, @@ -77,8 +77,8 @@ geom_neuron( \method{geom_neuron}{`NULL`}( x = NULL, rotation_matrix = NULL, - low = "turquoise", - high = "navy", + low = "navy", + high = "turquoise", stat = "identity", position = "identity", na.rm = FALSE, diff --git a/man/ggneuron.Rd b/man/ggneuron.Rd new file mode 100644 index 0000000..e616742 --- /dev/null +++ b/man/ggneuron.Rd @@ -0,0 +1,65 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ggplot2.R +\name{ggneuron} +\alias{ggneuron} +\title{Create a ggplot2 Visualisation of Neuron Objects} +\usage{ +ggneuron( + x, + volume = NULL, + info = NULL, + rotation_matrix = NULL, + low = "turquoise", + high = "navy", + alpha = 0.5, + title.col = "darkgrey", + ... +) +} +\arguments{ +\item{x}{A 'neuron', 'neuronlist', 'mesh3d', or 'hxsurf' object to be visualized.} + +\item{volume}{a brain/neuropil volume to be plotted in grey, for context. +Defaults to NULL, no volume plotted.} + +\item{info}{Optional. A string to be used as the plot title.} + +\item{rotation_matrix}{An optional 4x4 rotation matrix to apply to the neuron coordinates.} + +\item{low}{Color for the lowest Z values. Default is "turquoise".} + +\item{high}{Color for the highest Z values. Default is "navy".} + +\item{alpha}{Transparency of the neuron visualization. Default is 0.5.} + +\item{title.col}{Color of the plot title. Default is "darkgrey".} + +\item{...}{Additional arguments passed to geom_neuron().} +} +\value{ +A ggplot2 object representing the neuron visualization. +} +\description{ +This function creates a complete ggplot2 visualization for neuron objects, +including 'neuron', 'neuronlist', 'mesh3d', and 'hxsurf' objects. It sets up +a minimal theme and applies consistent styling to the plot. +} +\details{ +This function wraps around geom_neuron() to create a complete plot with a +consistent, minimal theme. It removes axes, legends, and other extraneous +elements to focus on the neuron visualization itself. +} +\examples{ +\dontrun{ +# Visualize the banc volume +ggneuron(banc_neuropil.surf, banc.surf) + +# Visualize the banc brain neuropil +ggneuron(banc_neuropil.surf, +rotation_matrix = bancr:::banc_rotation_matrices[["front"]]) +} + +} +\seealso{ +\code{\link{geom_neuron}} for the underlying geom used in this function. +}