Skip to content

Feat: sym_redock_exlude_motifs feat added#49

Open
aimura09 wants to merge 2 commits intomainfrom
dev_am
Open

Feat: sym_redock_exlude_motifs feat added#49
aimura09 wants to merge 2 commits intomainfrom
dev_am

Conversation

@aimura09
Copy link
Copy Markdown

@aimura09 aimura09 commented May 2, 2025

User description

  • When this flag is on, sym_redock will be applied only on diffused residues and motifs will remain at the same xyz.

Description

  • Introduced a mask parameter to the sym_redock function to allow selective application of symmetry redocking.
  • Updated the clash error computation to incorporate masking, improving flexibility in handling residues.
  • Enhanced the logic for clash loss calculation, allowing for more precise control over interactions.

Changes walkthrough 📝

Relevant files
Enhancement
sym_util.py
Enhance sym_redock with masking functionality                       

ipd/sym/sym_util.py

  • Added a mask parameter to the sym_redock function.
  • Updated the clash_error_comp function to handle the new mask
    parameter.
  • Implemented masking for intra- and inter-contacts based on the
    provided mask.
  • Enhanced the clash computation logic to consider masked residues.
  • +15/-5   

    💡 Penify usage:
    Comment /help on the PR to get a list of all available Penify tools and their descriptions

    Copilot AI review requested due to automatic review settings May 2, 2025 17:53
    @sourcery-ai
    Copy link
    Copy Markdown
    Contributor

    sourcery-ai bot commented May 2, 2025

    Reviewer's Guide

    The sym_redock function was updated to optionally accept a mask parameter. This mask is used within the internal clash_error_comp function to exclude specified residues (motifs) from the clash calculation, thereby preventing them from being moved during the redocking optimization.

    Sequence diagram for sym_redock with mask parameter

    sequenceDiagram
        participant C as Caller
        participant SR as sym_redock
        participant CEC as clash_error_comp
        participant LBFGS as lbfgs
    
        C->>+SR: sym_redock(xyz, Lasu, frames, opt, mask)
        note right of SR: 'mask' is now an optional parameter
        SR->>SR: Initialize optimization parameters (Q0, T0)
        SR->>LBFGS: Run optimization using 'closure'
        loop Optimization Steps
            LBFGS->>+SR: closure()
            SR->>+CEC: clash_error_comp(..., mask)
            note right of CEC: If mask is provided...
            CEC->>CEC: Modify clash calculation (dsymm_2) to ignore masked residues
            CEC-->>-SR: loss
            SR-->>-LBFGS: loss
            LBFGS->>LBFGS: Update parameters (Q0, T0)
        end
        SR-->>-C: Optimized coordinates
    
    Loading

    File-Level Changes

    Change Details Files
    Added optional masking capability to sym_redock.
    • Added optional mask parameter to sym_redock function signature.
    • Added optional mask parameter to nested clash_error_comp function signature.
    • Passed mask parameter to clash_error_comp call within the LBFGS closure.
    ipd/sym/sym_util.py
    Applied mask within clash calculation to exclude specified residues.
    • Implemented logic in clash_error_comp to derive a pairwise interaction mask from the input residue mask.
    • Updated distance matrix modification logic to use the mask, setting distances involving masked residues to a high value to ignore them in clash calculations.
    ipd/sym/sym_util.py

    Tips and commands

    Interacting with Sourcery

    • Trigger a new review: Comment @sourcery-ai review on the pull request.
    • Continue discussions: Reply directly to Sourcery's review comments.
    • Generate a GitHub issue from a review comment: Ask Sourcery to create an
      issue from a review comment by replying to it. You can also reply to a
      review comment with @sourcery-ai issue to create an issue from it.
    • Generate a pull request title: Write @sourcery-ai anywhere in the pull
      request title to generate a title at any time. You can also comment
      @sourcery-ai title on the pull request to (re-)generate the title at any time.
    • Generate a pull request summary: Write @sourcery-ai summary anywhere in
      the pull request body to generate a PR summary at any time exactly where you
      want it. You can also comment @sourcery-ai summary on the pull request to
      (re-)generate the summary at any time.
    • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
      request to (re-)generate the reviewer's guide at any time.
    • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
      pull request to resolve all Sourcery comments. Useful if you've already
      addressed all the comments and don't want to see them anymore.
    • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
      request to dismiss all existing Sourcery reviews. Especially useful if you
      want to start fresh with a new review - don't forget to comment
      @sourcery-ai review to trigger a new review!

    Customizing Your Experience

    Access your dashboard to:

    • Enable or disable review features such as the Sourcery-generated pull request
      summary, the reviewer's guide, and others.
    • Change the review language.
    • Add, remove or edit custom review instructions.
    • Adjust other review settings.

    Getting Help

    @aimura09 aimura09 requested review from willsheffler and removed request for Copilot May 2, 2025 17:53
    @penify-dev penify-dev bot added the enhancement New feature or request label May 2, 2025
    @what-the-diff
    Copy link
    Copy Markdown

    what-the-diff bot commented May 2, 2025

    PR Summary

    • Functionality update to sym_redock: An optional feature, mask, has been introduced that enhances how clashes are managed.
    • Alteration within clash_error_comp function: The function can now take in a mask parameter, enabling users to selectively process residues.
    • Implementation of masking logic: Specific checks for mask have been added. This prevents conflicts between masked residues while also altering distance computations as needed.
    • Modification in loss calculation: The method of calculating loss is revised- integrating the mask so clash penalties are not levied on masked residues.

    Copy link
    Copy Markdown

    Copilot AI left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Pull Request Overview

    This PR introduces a new flag to control the behavior of sym_redock so that it is applied only on diffused residues while leaving motifs at the same xyz.

    • Added an optional mask parameter to the sym_redock function.
    • Modified the clash_error_comp function to accept and apply the mask during clash calculations.

    def clash_error_comp(R0, T0, xyz, fit_tscale):
    def clash_error_comp(R0, T0, xyz, fit_tscale, mask=None):
    if mask is not None:
    mask0 = mask[:Lasu.item()]
    Copy link

    Copilot AI May 2, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    If Lasu is already an integer type, calling .item() will raise an error. Consider using Lasu directly or ensuring that Lasu is a tensor before calling .item().

    Suggested change
    mask0 = mask[:Lasu.item()]
    mask0 = mask[:Lasu.item()] if isinstance(Lasu, th.Tensor) else mask[:Lasu]

    Copilot uses AI. Check for mistakes.
    Comment on lines +41 to +50
    mask = ~(mask0[None, :].expand(Lasu, Lasu))
    mask = mask | mask.T
    # dsymm_2 = dsymm.clone().fill_diagonal_(9999) # avoid in-place operation
    for i in range(0, len(Xsymmall), Lasu):
    dsymm_2[i:i + Lasu, i:i + Lasu] = 9999
    for i in range(0, len(Xsymmall), Lasu): # loop over rows
    dsymm_2[i:i + Lasu, i:i + Lasu] = 9999 # masking intra-contact
    if mask is not None:
    for j in range(0, len(Xsymmall), Lasu): # loop over cols
    if j == i: continue
    # masking inter-contacts involving masked residues
    dsymm_2[i:i + Lasu, j:j + Lasu][mask] = 9999
    Copy link

    Copilot AI May 2, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    [nitpick] The variable name 'mask' is reused to store a computed boolean mask, which can be confusing given that it is also a function parameter. Consider renaming the local variable (e.g., 'computed_mask') to improve clarity.

    Copilot uses AI. Check for mistakes.
    @penify-dev
    Copy link
    Copy Markdown
    Contributor

    penify-dev bot commented May 2, 2025

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    4, because the changes involve multiple function modifications and the introduction of new parameters, which require a thorough understanding of the existing logic and potential impacts on the overall functionality.

    🧪 Relevant tests

    No

    ⚡ Possible issues

    Possible Bug: The handling of the mask parameter in the clash_error_comp function may lead to unexpected behavior if the mask is not properly defined or if its dimensions do not match the expected input.

    🔒 Security concerns

    No

    Copy link
    Copy Markdown
    Contributor

    @sourcery-ai sourcery-ai bot left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Hey @aimura09 - I've reviewed your changes - here's some feedback:

    • The nested loops applying the mask within clash_error_comp could potentially be vectorized for improved clarity and performance.
    • Consider adding a type hint or docstring clarification for the expected format and shape of the mask parameter in sym_redock.
    Here's what I looked at during the review
    • 🟢 General issues: all looks good
    • 🟢 Security: all looks good
    • 🟢 Testing: all looks good
    • 🟢 Complexity: all looks good
    • 🟢 Documentation: all looks good

    Sourcery is free for open source - if you like our reviews please consider sharing them ✨
    Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

    @penify-dev
    Copy link
    Copy Markdown
    Contributor

    penify-dev bot commented May 2, 2025

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Maintainability
    Add validation for the dimensions of xyz against Lasu to prevent runtime errors

    Consider validating the dimensions of xyz and Lasu to ensure they are compatible before
    performing operations to prevent runtime errors.

    ipd/sym/sym_util.py [25]

    +if xyz.shape[0] < Lasu:
    +    raise ValueError("xyz has fewer elements than Lasu.")
     xyz0 = xyz[:Lasu]
     
    Suggestion importance[1-10]: 8

    Why: This suggestion addresses a potential runtime error by validating dimensions, which is crucial for ensuring the robustness of the function.

    8
    Possible bug
    Correctly apply the mask to avoid index errors when modifying dsymm_2

    Ensure that the mask is properly utilized in the loop to avoid potential index errors when
    accessing dsymm_2.

    ipd/sym/sym_util.py [50]

    -dsymm_2[i:i + Lasu, j:j + Lasu][mask] = 9999
    +dsymm_2[i:i + Lasu, j:j + Lasu][mask[i:i + Lasu, j:j + Lasu]] = 9999
     
    Suggestion importance[1-10]: 7

    Why: The suggestion correctly identifies a potential issue with index errors when applying the mask, which is important for maintaining the integrity of the data.

    7
    Performance
    Replace the mask creation logic with a more efficient method using torch.full

    Consider using torch.full to create the mask instead of using logical operations, which
    can improve readability and performance.

    ipd/sym/sym_util.py [40-42]

    -mask = ~(mask0[None, :].expand(Lasu, Lasu))
    -mask = mask | mask.T
    +mask = torch.full((Lasu, Lasu), True, dtype=torch.bool)
    +mask[mask0[None, :], mask0] = False
     
    Suggestion importance[1-10]: 6

    Why: The suggestion proposes a more efficient method for creating the mask, which could enhance performance and readability, but it may not be necessary given the existing logic.

    6
    Possible issue
    Add a check for Lasu to ensure it is not None when using it with mask

    Ensure that the mask variable is properly handled in the clash_error_comp function to
    avoid potential issues with uninitialized values when mask is None.

    ipd/sym/sym_util.py [23-24]

    -if mask is not None:
    +if mask is not None and Lasu is not None:
         mask0 = mask[:Lasu.item()]
     
    Suggestion importance[1-10]: 5

    Why: The suggestion adds a check for Lasu, but Lasu is not None in the context of this function as it is derived from the input. The check is unnecessary.

    5

     - When this flag is on, sym_redock will be applied only on diffused residues and motifs will remain at the same xyz.
    
    Fix: lines suggested by Copilot
    @aimura09 aimura09 requested a review from Copilot May 15, 2025 02:23
    Copy link
    Copy Markdown

    Copilot AI left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Pull Request Overview

    This PR introduces a masking feature to the symmetry redocking process so that when enabled, only diffused residues are redocked while motifs remain unchanged. Key changes include:

    • Adding a new optional mask parameter to the sym_redock function.
    • Updating the clash error computation to account for masking both intra- and inter-residue interactions.
    • Enhancing the looping logic to apply masking to specific contact regions.

    return allframes, frames

    def sym_redock(xyz, Lasu, frames, opt, **_):
    def sym_redock(xyz, Lasu, frames, opt, mask=None, **_):
    Copy link

    Copilot AI May 15, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Add a detailed docstring for the 'mask' parameter in sym_redock (and in clash_error_comp) that specifies the expected type (1D th.Tensor or list), shape, and intended behavior, to improve clarity and maintainability.

    Copilot uses AI. Check for mistakes.
    dsymm = th.cdist(Xsymmall, Xsymmall, p=2)
    dsymm_2 = dsymm.clone()
    if mask is not None:
    mask0 = mask[:Lasu.item()] if isinstance(Lasu, th.Tensor) else th.tensor(mask[:Lasu])
    Copy link

    Copilot AI May 15, 2025

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    [nitpick] Ensure that 'Lasu' is always provided as an integer value (or explicitly converted) since it is used both for slicing and for controlling loop iterations; consider renaming or converting it to clearly indicate its role as a count of residues.

    Copilot uses AI. Check for mistakes.
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    2 participants