Skip to content

Commit

Permalink
Created ggneuron
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbates committed Jul 12, 2024
1 parent d6f20f6 commit 80f4372
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 39 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ Imports:
RSQLite,
readr,
checkmate,
utils
utils,
ggplot2 (>= 3.5.1.9000)

This comment has been minimized.

Copy link
@jefferis

jefferis Jul 13, 2024

Member

@alexanderbates I recommend against having ggplot2 as a required dependency (i.e. putting it in Imports rather than suggests) but I definitely recommend against having a development version of ggplot2 as a required dependency.

Suggests:
testthat (>= 3.0.0),
reticulate,
Expand All @@ -53,10 +54,10 @@ Suggests:
rmarkdown,
spelling,
arrow,
ggplot2,
ggnewscale,
ggnewscale (>= 0.4.10.9000),

This comment has been minimized.

Copy link
@jefferis

jefferis Jul 13, 2024

Member

Why do you need these development versions? If you need them, then you need to add remotes to allow them to be installed.

This comment has been minimized.

Copy link
@alexanderbates

alexanderbates Jul 13, 2024

Author Collaborator

Some versions of ggplot2 and ggnewscale do not work together as well; specifically I had an issue where only two new scales could be implemented and on the third it bugged out. I added the versions I had on my machine, only because I knew those would work.

ggpubr,
catmaid
catmaid,
grDevices
Remotes:
natverse/nat,
natverse/fafbseg,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/cloudvolume.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
112 changes: 96 additions & 16 deletions R/ggplot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -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") +
Expand Down Expand Up @@ -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)
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -372,14 +378,88 @@ 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(
ggplot2::geom_polygon(...)
)
}

#' 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)
}



Expand Down
6 changes: 2 additions & 4 deletions R/synapses.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'
Expand All @@ -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,
Expand Down Expand Up @@ -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"){
Expand Down
3 changes: 1 addition & 2 deletions man/banc_all_synapses.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions man/geom_neuron.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions man/ggneuron.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 80f4372

Please sign in to comment.