Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonywu1999 committed Jan 10, 2025
1 parent 06308c5 commit 8dd0029
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 27 additions & 8 deletions R/utils_visualizeNetworks.R
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
)
}
15 changes: 4 additions & 11 deletions tests/testthat/test-utils_visualizeNetworks.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
})

0 comments on commit 8dd0029

Please sign in to comment.