Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Seurat and ggplot compatibility #95

Merged
merged 7 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: clustree
Type: Package
Title: Visualise Clusterings at Different Resolutions
Version: 0.5.0
Date: 2022-06-24
Version: 0.5.0.9001
Date: 2023-10-24
Authors@R:
c(person("Luke", "Zappia", role = c("aut", "cre"),
email = "[email protected]",
Expand Down Expand Up @@ -33,7 +33,7 @@ Imports: checkmate,
igraph,
dplyr,
grid,
ggplot2,
ggplot2 (>= 3.4.0),
viridis,
methods,
rlang,
Expand All @@ -50,5 +50,5 @@ Suggests:
pkgdown,
spelling
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.0
RoxygenNote: 7.2.3
Language: en-GB
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# clustree (development version)

### _clustree_ 0.5.0.9001 (2023-10-24)

* Avoid warnings due to deprecated functionality in _ggplot2_ and
_tidyselect_. Includes increasing the _ggplot2_ requirement to >= 3.4.0.
(Fixes #35)

### _clustree_ 0.5.0.9000 (2023-10-24)

* Minor updates for compatibility with upcoming _Seurat_ v5 release (Fixes #93)

# _clustree_ 0.5.0 (2022-06-24)

## Minor changes
Expand Down
8 changes: 8 additions & 0 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#' @param node_aes value of the node aesthetic to check
#' @param node_aes_aggr aggregation function associated with the node aesthetic
#'
#' @keywords internal
#'
#' @importFrom utils head
assert_node_aes <- function(node_aes_name, prefix, metadata, node_aes,
node_aes_aggr) {
Expand Down Expand Up @@ -58,6 +60,8 @@ assert_node_aes <- function(node_aes_name, prefix, metadata, node_aes,
#' @param node_aes_aggr aggregation function associated with the node aesthetic
#' @param min minimum numeric value allowed
#' @param max maximum numeric value allowed
#'
#' @keywords internal
assert_numeric_node_aes <- function(node_aes_name, prefix, metadata, node_aes,
node_aes_aggr, min, max) {

Expand Down Expand Up @@ -87,6 +91,8 @@ assert_numeric_node_aes <- function(node_aes_name, prefix, metadata, node_aes,
#' @param min minimum numeric value allowed
#' @param max maximum numeric value allowed
#'
#' @keywords internal
#'
#' @importFrom grDevices col2rgb
assert_colour_node_aes <- function(node_aes_name, prefix, metadata, node_aes,
node_aes_aggr, min, max) {
Expand Down Expand Up @@ -120,6 +126,8 @@ assert_colour_node_aes <- function(node_aes_name, prefix, metadata, node_aes,
#'
#' @param node_aes_list List of node aesthetics
#'
#' @keywords internal
#'
#' @return Corrected node aesthetics list
check_node_aes_list <- function(node_aes_list) {
for (aes in names(node_aes_list)) {
Expand Down
62 changes: 36 additions & 26 deletions R/clustree.R
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,14 @@ clustree.matrix <- function(x, prefix,
ends = edge_arrow_ends),
end_cap = circle(circle_size_end, "points"),
start_cap = circle(circle_size_start, "points"),
aes_(colour = ~count,
alpha = ~in_prop,
edge_width = ~is_core))
aes(colour = .data$count,
alpha = .data$in_prop,
edge_width = .data$is_core))

} else {
gg <- gg + geom_edge_link(aes_(colour = ~count, alpha = ~in_prop,
edge_width = ~is_core))
gg <- gg + geom_edge_link(aes(colour = .data$count,
alpha = .data$in_prop,
edge_width = .data$is_core))
}

if (highlight_core) {
Expand All @@ -311,17 +312,17 @@ clustree.matrix <- function(x, prefix,

# Plot node text
if (scale_node_text && !is.numeric(node_size)) {
gg <- gg + geom_node_text(aes_(label = ~cluster,
size = as.name(graph_attr$node_size)),
gg <- gg + geom_node_text(aes(label = .data$cluster,
size = .data[[graph_attr$node_size]]),
colour = node_text_colour,
angle = node_text_angle
)
} else {
gg <- gg + geom_node_text(aes_(label = ~cluster),
gg <- gg + geom_node_text(aes(label = .data$cluster),
size = node_text_size,
colour = node_text_colour,
angle = node_text_angle
)
)
}

if (!(is.null(node_label))) {
Expand Down Expand Up @@ -516,8 +517,13 @@ clustree.Seurat <- function(x, prefix = paste0(assay, "_snn_res."),
)
}
aes_name <- paste0(exprs, "_", node_aes_name)
x[[aes_name]] <- Seurat::FetchData(x, vars = node_aes_value,
slot = exprs)
if (packageVersion("SeuratObject") >= package_version("5.0.0")) {
x[[aes_name]] <- Seurat::FetchData(x, vars = node_aes_value,
layer = exprs)
} else {
x[[aes_name]] <- Seurat::FetchData(x, vars = node_aes_value,
slot = exprs)
}
args[[node_aes]] <- aes_name
}
}
Expand All @@ -541,6 +547,8 @@ clustree.Seurat <- function(x, prefix = paste0(assay, "_snn_res."),
#' name of a metadata column to use for node transparency
#' @param allowed vector of allowed node attributes to use as aesthetics
#'
#' @keywords internal
#'
#' @importFrom ggraph geom_node_point
#' @importFrom ggplot2 aes_
add_node_points <- function(node_colour, node_size, node_alpha, allowed) {
Expand All @@ -555,25 +563,25 @@ add_node_points <- function(node_colour, node_size, node_alpha, allowed) {
}

switch(aes_allowed,
col_size_alpha = geom_node_point(aes_(colour = as.name(node_colour),
size = as.name(node_size),
alpha = as.name(node_alpha))),
col_alpha = geom_node_point(aes_(colour = as.name(node_colour),
alpha = as.name(node_alpha)),
col_size_alpha = geom_node_point(aes(colour = .data[[node_colour]],
size = .data[[node_size]],
alpha = .data[[node_alpha]])),
col_alpha = geom_node_point(aes(colour = .data[[node_colour]],
alpha = .data[[node_alpha]]),
size = node_size),
col_size = geom_node_point(aes_(colour = as.name(node_colour),
size = as.name(node_size)),
col_size = geom_node_point(aes(colour = .data[[node_colour]],
size = .data[[node_size]]),
alpha = node_alpha),
col = geom_node_point(aes_(colour = as.name(node_colour)),
col = geom_node_point(aes(colour = .data[[node_colour]]),
size = node_size,
alpha = node_alpha),
size_alpha = geom_node_point(aes_(size = as.name(node_size),
alpha = as.name(node_alpha)),
size_alpha = geom_node_point(aes(size = .data[[node_size]],
alpha = .data[[node_alpha]]),
colour = node_colour),
size = geom_node_point(aes_(size = as.name(node_size)),
size = geom_node_point(aes(size = .data[[node_size]]),
colour = node_colour,
alpha = node_alpha),
alpha = geom_node_point(aes_(alpha = as.name(node_alpha)),
alpha = geom_node_point(aes(alpha = .data[[node_alpha]]),
colour = node_colour,
size = node_size),
none = geom_node_point(colour = node_colour,
Expand All @@ -597,6 +605,8 @@ add_node_points <- function(node_colour, node_size, node_alpha, allowed) {
#' labels
#' @param allowed vector of allowed node attributes to use as aesthetics
#'
#' @keywords internal
#'
#' @importFrom ggraph geom_node_label
#' @importFrom ggplot2 aes_
add_node_labels <- function(node_label, node_colour, node_label_size,
Expand All @@ -605,13 +615,13 @@ add_node_labels <- function(node_label, node_colour, node_label_size,
is_allowed <- c(node_colour) %in% allowed

if (is_allowed) {
geom_node_label(aes_(label = as.name(node_label),
fill = as.name(node_colour)),
geom_node_label(aes(label = .data[[node_label]],
fill = .data[[node_colour]]),
size = node_label_size,
colour = node_label_colour,
nudge_y = node_label_nudge)
} else {
geom_node_label(aes_(label = as.name(node_label)),
geom_node_label(aes(label = .data[[node_label]]),
fill = node_colour,
size = node_label_size,
colour = node_label_colour,
Expand Down
Loading