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

feat(visualizeNetworks): Add legend to cytoscape network #27

Merged
merged 10 commits into from
Jan 10, 2025

Conversation

tonywu1999
Copy link
Contributor

@tonywu1999 tonywu1999 commented Jan 10, 2025

User description

Motivation and Context

Please include relevant motivation and context of the problem along with a short summary of the solution.

Changes

Please provide a detailed bullet point list of your changes.

Testing

Please describe any unit tests you added or modified to verify your changes.

Checklist Before Requesting a Review

  • I have read the MSstats contributing guidelines
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules
  • Ran styler::style_pkg(transformers = styler::tidyverse_style(indent_by = 4))
  • Ran devtools::document()

PR Type

Enhancement, Tests


Description

  • Added a legend to Cytoscape network visualizations.

  • Adjusted node size and font size based on logFC.

  • Enhanced visual style mappings for edge labels and node properties.

  • Added unit tests for legend functionality and updated existing tests.


Changes walkthrough 📝

Relevant files
Dependencies
NAMESPACE
Update namespace with new RCy3 imports                                     

NAMESPACE

  • Added imports for new RCy3 functions used in legend creation.
  • Updated namespace to include additional dependencies.
  • +5/-0     
    Enhancement
    utils_visualizeNetworks.R
    Add `.addLegendInCytoscape` function for legend creation 

    R/utils_visualizeNetworks.R

  • Introduced .addLegendInCytoscape function to add a legend.
  • Utilized RCy3 functions for annotations and grouping.
  • Configured legend layout and appearance dynamically.
  • +63/-0   
    visualizeNetworks.R
    Enhance `visualizeNetworks` with legend and visual updates

    R/visualizeNetworks.R

  • Integrated legend creation into visualizeNetworks.
  • Adjusted node size and font size based on logFC.
  • Enhanced visual style mappings for edge labels and node properties.
  • +21/-5   
    Tests
    test-utils_visualizeNetworks.R
    Add unit tests for `.addLegendInCytoscape`                             

    tests/testthat/test-utils_visualizeNetworks.R

  • Added unit tests for .addLegendInCytoscape.
  • Mocked RCy3 functions for testing legend creation.
  • +44/-0   
    test-visualizeNetworks.R
    Update tests for `visualizeNetworks` with legend validation

    tests/testthat/test-visualizeNetworks.R

  • Updated tests to validate legend integration in visualizeNetworks.
  • Added mocks for .addLegendInCytoscape and related functions.
  • +13/-1   

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Possible Issue

    The x_start calculation in .addLegendInCytoscape assumes that node_coordinates$x_location is always numeric after conversion. This could fail if non-numeric values exist in the input data. Consider adding validation or error handling for this scenario.

    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
    Visual Style Mapping

    The mapVisualProperty for Node Fill Color uses a hardcoded color scale. Ensure this scale is appropriate for all potential datasets or consider making it configurable.

    "Node Fill Color", "logFC_color", "c",
    c(-logfcCutoff, 0.0, logfcCutoff),
    c("#ADD8E6", "#ADD8E6", "#D3D3D3", "#FFA590", "#FFA590")

    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add validation to ensure legend_items contains the required fields before processing

    Validate the structure and content of legend_items to ensure it contains the
    required fields (color and label) before processing, to prevent runtime errors.

    R/utils_visualizeNetworks.R [32-34]

    +if (!all(sapply(legend_items, function(item) all(c("color", "label") %in% names(item))))) {
    +    stop("Each legend item must contain 'color' and 'label' fields.")
    +}
     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
    Suggestion importance[1-10]: 9

    Why: This suggestion adds a crucial validation step to ensure that legend_items contains the required fields (color and label), preventing potential runtime errors. It is highly relevant and improves the robustness of the function.

    9
    Add error handling for addAnnotationShape and addAnnotationText to ensure robustness

    Check the return values of addAnnotationShape and addAnnotationText to ensure they
    are valid and handle any errors or unexpected results.

    R/utils_visualizeNetworks.R [37-54]

    -shape_name <- addAnnotationShape(
    -    type = "rectangle",
    -    x.pos = x_start,
    -    y.pos = y_pos,
    -    width = box_size,
    -    height = box_size,
    -    fillColor = item$color,
    -    borderColor = "black",
    -    borderThickness = 1
    -)
    -text_name <- addAnnotationText(
    -    text = item$label,
    -    x.pos = x_start + box_size + spacing,
    -    y.pos = y_pos + box_size / 4,  # Center text vertically with the rectangle
    -    fontSize = 12,
    -    color = "black"
    -)
    +shape_name <- tryCatch({
    +    addAnnotationShape(
    +        type = "rectangle",
    +        x.pos = x_start,
    +        y.pos = y_pos,
    +        width = box_size,
    +        height = box_size,
    +        fillColor = item$color,
    +        borderColor = "black",
    +        borderThickness = 1
    +    )
    +}, error = function(e) stop("Error adding annotation shape: ", e$message))
    +text_name <- tryCatch({
    +    addAnnotationText(
    +        text = item$label,
    +        x.pos = x_start + box_size + spacing,
    +        y.pos = y_pos + box_size / 4,  # Center text vertically with the rectangle
    +        fontSize = 12,
    +        color = "black"
    +    )
    +}, error = function(e) stop("Error adding annotation text: ", e$message))
    Suggestion importance[1-10]: 8

    Why: Adding error handling for addAnnotationShape and addAnnotationText ensures that unexpected errors during annotation creation are caught and handled gracefully, improving the function's reliability.

    8
    Add a check to ensure the logFC column exists before computing logFC_abs

    Ensure that the logFC_abs column is computed only after confirming the presence of
    the logFC column in the nodes dataframe to avoid potential errors.

    R/visualizeNetworks.R [40]

    +if (!"logFC" %in% colnames(nodes)) {
    +    stop("The 'logFC' column is missing from the nodes dataframe.")
    +}
     nodes$logFC_abs <- abs(nodes$logFC)
    Suggestion importance[1-10]: 7

    Why: This suggestion adds a validation step to ensure the presence of the logFC column before computation, which prevents potential errors and enhances code safety. However, the likelihood of the column being absent in this context is low, slightly reducing its impact.

    7
    General
    Ensure visualization functions are executed only in interactive environments

    Validate that the interactive() function returns TRUE before proceeding with
    visualization steps to avoid unexpected behavior in non-interactive environments.

    R/visualizeNetworks.R [43-44]

    -if (interactive()) {
    -    createNetworkFromDataFrames(nodes, edges)
    +if (!interactive()) {
    +    stop("Visualization is only supported in interactive mode.")
    +}
    +createNetworkFromDataFrames(nodes, edges)
    Suggestion importance[1-10]: 6

    Why: While the PR already includes a warning for non-interactive environments, this suggestion enforces a stricter validation by stopping execution, which could be useful in some cases. However, it may limit flexibility unnecessarily, slightly reducing its impact.

    6

    @codecov-commenter
    Copy link

    codecov-commenter commented Jan 10, 2025

    Codecov Report

    All modified and coverable lines are covered by tests ✅

    Please upload report for BASE (devel@fa70cfe). Learn more about missing BASE report.
    Report is 1 commits behind head on devel.

    Additional details and impacted files
    @@           Coverage Diff            @@
    ##             devel      #27   +/-   ##
    ========================================
      Coverage         ?   88.28%           
    ========================================
      Files            ?        7           
      Lines            ?      384           
      Branches         ?        0           
    ========================================
      Hits             ?      339           
      Misses           ?       45           
      Partials         ?        0           

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    @tonywu1999 tonywu1999 merged commit 4c5c730 into devel Jan 10, 2025
    3 checks passed
    @tonywu1999 tonywu1999 deleted the hupo-changes branch January 10, 2025 22:48
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants