-
Notifications
You must be signed in to change notification settings - Fork 12
add 3d viz to ligandatommapping #646
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f5fc178
first pass at adding 3d viz to ligandatommapping
atravitz 6c721e9
Merge branch 'main' into add/3d_mapping_viz
atravitz 24d273e
fix imports
atravitz 9fd6335
add py3dmol smoke test
atravitz 85ed3d1
add skipif string
atravitz cf62c6d
move helper functions to visualization/mapping_visualization
atravitz 55c8fe4
fix imports
atravitz b8728a2
Merge branch 'main' into add/3d_mapping_viz
atravitz 73efd57
add news item
atravitz 85a30c0
add ci testing for with and without py3dmol
atravitz f73bc50
improve naming
atravitz 70a1874
back to requires_package
atravitz 7b18b4f
move import to top
atravitz a93c56b
add PR #
atravitz c0052af
🎨
atravitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,17 +3,23 @@ | |
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from typing import Any | ||
| from typing import TYPE_CHECKING, Any, Optional, Tuple, Union | ||
|
|
||
| import numpy as np | ||
| from numpy.typing import NDArray | ||
| from rdkit import Chem | ||
|
|
||
| from gufe.components import SmallMoleculeComponent | ||
| from gufe.visualization.mapping_visualization import draw_mapping | ||
|
|
||
| from ..tokenization import JSON_HANDLER | ||
| from ..utils import requires_package | ||
atravitz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from ..visualization import mapping_visualization as viz | ||
| from . import AtomMapping | ||
|
|
||
| if TYPE_CHECKING: | ||
| import py3Dmol | ||
|
|
||
|
|
||
| class LigandAtomMapping(AtomMapping): | ||
| """ | ||
|
|
@@ -194,6 +200,84 @@ def draw_to_file(self, fname: str, d2d=None): | |
| ) | ||
| ) | ||
|
|
||
| @requires_package("py3Dmol") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that using this decorator will actually only raise an issue with |
||
| def view_3d( | ||
atravitz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self, | ||
| spheres: Optional[bool] = True, | ||
| show_atomIDs: Optional[bool] = False, | ||
| style: Optional[str] = "stick", | ||
| shift: Optional[Union[Tuple[float, float, float], NDArray[np.float64]]] = None, | ||
| ) -> py3Dmol.view: | ||
| """ | ||
| Render relative transformation edge in 3D using py3Dmol. | ||
|
|
||
| By default matching atoms will be annotated using colored spheres. | ||
|
|
||
| py3Dmol is an optional dependency, it can be installed with: | ||
| pip install py3Dmol | ||
|
|
||
| Parameters | ||
| ---------- | ||
| spheres : bool, optional | ||
| Whether or not to show matching atoms as spheres. | ||
| show_atomIDs: bool, optional | ||
| Whether or not to show atom ids in the mapping visualization | ||
| style : str, optional | ||
| Style in which to represent the molecules in py3Dmol. | ||
| shift : Tuple of floats, optional | ||
| Amount to shift molB by in order to visualize the two ligands. | ||
| If None, the default shift will be estimated as the largest | ||
| intraMol distance of both mols. | ||
|
|
||
| Returns | ||
| ------- | ||
| view : py3Dmol.view | ||
| View of the system containing both molecules in the edge. | ||
| """ | ||
| import py3Dmol | ||
|
|
||
| if shift is None: | ||
| shift = np.array([viz._get_max_dist_in_x(self) * 1.5, 0, 0]) | ||
| else: | ||
| shift = np.array(shift) | ||
|
|
||
| molA = self.componentA.to_rdkit() | ||
| molB = self.componentB.to_rdkit() | ||
|
|
||
| # 0 * shift is the centrepoint | ||
| # shift either side of the mapping +- a shift to clear the centre view | ||
| lmol = viz._translate(molA, -1 * shift) | ||
| rmol = viz._translate(molB, +1 * shift) | ||
|
|
||
| view = py3Dmol.view(width=600, height=600) | ||
| view.addModel(Chem.MolToMolBlock(lmol), "molA") | ||
| view.addModel(Chem.MolToMolBlock(rmol), "molB") | ||
|
|
||
| if spheres: | ||
| viz._add_spheres(view, lmol, rmol, self.componentA_to_componentB) | ||
|
|
||
| if show_atomIDs: | ||
| view.addPropertyLabels( | ||
| "index", | ||
| {"not": {"resn": ["molA_overlay", "molA_overlay"]}}, | ||
| { | ||
| "fontColor": "black", | ||
| "font": "sans-serif", | ||
| "fontSize": "10", | ||
| "showBackground": "false", | ||
| "alignment": "center", | ||
| }, | ||
| ) | ||
|
|
||
| # middle fig | ||
| view.addModel(Chem.MolToMolBlock(molA), "molA_overlay") | ||
| view.addModel(Chem.MolToMolBlock(molB), "molB_overlay") | ||
|
|
||
| view.setStyle({style: {}}) | ||
|
|
||
| view.zoomTo() | ||
| return view | ||
|
|
||
| def with_annotations(self, annotations: dict[str, Any]) -> LigandAtomMapping: | ||
| """Create a new mapping based on this one with extra annotations. | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Added:** | ||
|
|
||
| * Added ``LigandAtomMapping.view_3d()`` method (previously implemented as ``openfe.utils.visualization_3D.view_mapping()`` (`PR #646 <https://github.com/OpenFreeEnergy/gufe/pull/646>`_). | ||
|
|
||
| **Changed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Deprecated:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Removed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Fixed:** | ||
|
|
||
| * <news item> | ||
|
|
||
| **Security:** | ||
|
|
||
| * <news item> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea! we will need to update the branch rules after we merge this PR in. I am happy with it for now, but it might be better to call it something like "min deps" so we can keep adding things to the install step. We can wait until the next optional dep we add to do this.