Skip to content

Commit

Permalink
Ensure SD tags are parsed from OEGraphMol (#1778)
Browse files Browse the repository at this point in the history
* Add test loading `OEGraphMol` with SD tags

* Save SD tags before `OEMol` cast

* Typo

* Update release history
  • Loading branch information
mattwthompson authored Dec 1, 2023
1 parent b17f0fc commit 9b23790
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w

### Bugfixes

- [PR #1778](https://github.com/openforcefield/openff-toolkit/pull/1778): Ensures SD data tags are preserved in `Molecule.from_openeye` if the input is of type `oechem.OEGraphMol`.

### New features
- [PR #1775](https://github.com/openforcefield/openff-toolkit/pull/1775): Re-exports `openff.units.unit` and `Quantity` at `openff.toolkit.unit` and `Quantity`.

Expand Down
11 changes: 11 additions & 0 deletions openff/toolkit/_tests/test_toolkits.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ def test_to_from_openeye_core_props_unset(self):
== expected_output_smiles
)

def test_sd_tags_in_graph_mol_preserved(self):
from openeye import oechem

graph = oechem.OEGraphMol()

oechem.OEParseSmiles(graph, "CCO")

oechem.OEAddSDData(graph, "cat", "meow")

assert Molecule.from_openeye(graph).properties["cat"] == "meow"

def test_to_from_openeye_none_partial_charges(self):
"""Test to ensure that to_openeye and from_openeye correctly handle None partial charges"""
import math
Expand Down
2 changes: 2 additions & 0 deletions openff/toolkit/topology/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,8 @@ def assign_fractional_bond_orders(
def _invalidate_cached_properties(self) -> None:
"""
Indicate that the chemical entity has been altered.
Note that this does not clear the `.properties` dictionary attribute.
"""
self._conformers = None
self._partial_charges = None
Expand Down
12 changes: 9 additions & 3 deletions openff/toolkit/utils/openeye_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,12 @@ def from_openeye(

from openeye import oechem

# Save the existing SD tags, if any, since they're lost in the cast to
# OEMol of the input is OEGraphMol. See issue #1711
existing_sd_tags = {
pair.GetTag(): pair.GetValue() for pair in oechem.OEGetSDDataIter(oemol)
}

oemol = oechem.OEMol(oemol)

# Add explicit hydrogens if they're implicit
Expand Down Expand Up @@ -1208,9 +1214,9 @@ def describe_oeatom(oeatom) -> str:
if oemol.GetTitle() != "":
molecule.name = oemol.GetTitle()

# Copy any attached SD tag information
for dp in oechem.OEGetSDDataPairs(oemol):
molecule._properties[dp.GetTag()] = dp.GetValue()
# Attach any SD tag information we saved before casting input to new OEMol
for key, value in existing_sd_tags.items():
molecule._properties[key] = value

off_to_oe_idx = dict() # {oemol_idx: molecule_idx}
atom_mapping = {}
Expand Down

0 comments on commit 9b23790

Please sign in to comment.