diff --git a/NAMESPACE b/NAMESPACE index c851382..0130250 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -7,7 +7,6 @@ importFrom(RCy3,addAnnotationShape) importFrom(RCy3,addAnnotationText) importFrom(RCy3,createNetworkFromDataFrames) importFrom(RCy3,createVisualStyle) -importFrom(RCy3,getAnnotationList) importFrom(RCy3,getNetworkCenter) importFrom(RCy3,getNodePosition) importFrom(RCy3,groupAnnotation) diff --git a/R/utils_visualizeNetworks.R b/R/utils_visualizeNetworks.R index dc295f2..6bb14d5 100644 --- a/R/utils_visualizeNetworks.R +++ b/R/utils_visualizeNetworks.R @@ -1,25 +1,40 @@ #' Add a legend using annotations #' @param legend_items list of items and properties #' @importFrom RCy3 addAnnotationShape addAnnotationText getNodePosition -#' getNetworkCenter groupAnnotation getAnnotationList +#' getNetworkCenter groupAnnotation #' @keywords internal #' @noRd .addLegendInCytoscape <- function(legend_items) { # Starting position for the legend - node_coordinates = getNodePosition() - node_coordinates$x_location = as.numeric(node_coordinates$x_location) + node_coordinates <- getNodePosition() + node_coordinates$x_location <- as.numeric(node_coordinates$x_location) x_start <- max(node_coordinates$x_location) + 50 # Place the legend 50 units to the right of the rightmost node y_start <- getNetworkCenter()$y box_size <- 20 # Size of the color boxes spacing <- 10 # Spacing between legend items + legend_title <- "Legend" + title_spacing <- 20 # Additional spacing below the title + annotation_names <- c() + + title_name <- addAnnotationText( + text = legend_title, + x.pos = x_start, # Center the title with respect to the legend + y.pos = y_start, # Position above the items + fontSize = 14, + color = "black" + ) + annotation_names <- c(annotation_names, title_name) + + # Adjust the starting y-coordinate for the legend items + y_start <- y_start + title_spacing # Loop to add shapes and text for each legend item for (i in seq_along(legend_items)) { item <- legend_items[[i]] - y_pos <- y_start - (i - 1) * (box_size + spacing) # Adjust position for each item + y_pos <- y_start + (i - 1) * (box_size + spacing) # Adjust position for each item # Add a colored rectangle for the legend - addAnnotationShape( + shape_name <- addAnnotationShape( type = "rectangle", x.pos = x_start, y.pos = y_pos, @@ -31,14 +46,18 @@ ) # Add corresponding text label - addAnnotationText( + text_name <- addAnnotationText( text = item$label, x.pos = x_start + box_size + spacing, - y.pos = y_pos + box_size / 2, # Center text vertically with the rectangle + y.pos = y_pos + box_size / 4, # Center text vertically with the rectangle fontSize = 12, color = "black" ) + + annotation_names <- c(annotation_names, shape_name, text_name) } # Group all annotations for now - groupAnnotation(sapply(getAnnotationList(), '[[', 'uuid')) + groupAnnotation( + names = annotation_names + ) } \ No newline at end of file diff --git a/tests/testthat/test-utils_visualizeNetworks.R b/tests/testthat/test-utils_visualizeNetworks.R index 42671dc..7af60f7 100644 --- a/tests/testthat/test-utils_visualizeNetworks.R +++ b/tests/testthat/test-utils_visualizeNetworks.R @@ -5,7 +5,7 @@ test_that(".addLegendInCytoscape works correctly", { list(color = "#FFA590", label = "label3") ) - mock_getNodePosition <- mock(data.frame(x_position = c("50", "51"))) + mock_getNodePosition <- mock(data.frame(x_location = c("50", "51"))) stub( .addLegendInCytoscape, "getNodePosition", mock_getNodePosition @@ -17,13 +17,13 @@ test_that(".addLegendInCytoscape works correctly", { mock_getNetworkCenter ) - mock_addAnnotationShape <- mock() + mock_addAnnotationShape <- mock("id-12345", cycle = TRUE) stub( .addLegendInCytoscape, "addAnnotationShape", mock_addAnnotationShape ) - mock_addAnnotationText <- mock() + mock_addAnnotationText <- mock("id-12345", cycle = TRUE) stub( .addLegendInCytoscape, "addAnnotationText", mock_addAnnotationText @@ -35,17 +35,10 @@ test_that(".addLegendInCytoscape works correctly", { mock_groupAnnotation ) - mock_getAnnotationList <- mock(c("id1", "id2")) - stub( - .addLegendInCytoscape, "getAnnotationList", - mock_getAnnotationList - ) - expect_silent(.addLegendInCytoscape(legend_items)) expect_called(mock_getNodePosition, 1) expect_called(mock_getNetworkCenter, 1) expect_called(mock_addAnnotationShape, 3) - expect_called(mock_addAnnotationText, 3) + expect_called(mock_addAnnotationText, 4) expect_called(mock_groupAnnotation, 1) - expect_called(mock_getAnnotationList, 1) }) \ No newline at end of file