Skip to content

Commit

Permalink
Convert insertion code or chain id empty string to single character s…
Browse files Browse the repository at this point in the history
…pace in to_openeye (#1971)

* Fix issue #1967 and add test

* update releasehistory

* remove unused stringio import
  • Loading branch information
j-wags authored Dec 4, 2024
1 parent 3d84c70 commit a1d2bee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w
### Behavior changes

### Bugfixes
- [PR #1971](https://github.com/openforcefield/openff-toolkit/pull/1971): Fixes bug where OpenEyeToolkitWrapper would write coordinate-less PDB atoms if insertion_code or chain_id was an empty string ([Issue #1967](https://github.com/openforcefield/openff-toolkit/issues/1967))


### New features

Expand Down
28 changes: 28 additions & 0 deletions openff/toolkit/_tests/test_toolkits.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,34 @@ def test_write_pdb_preserving_atom_order(self):
assert water_from_pdb_split[1].split()[2].rstrip() == "O"
assert water_from_pdb_split[2].split()[2].rstrip() == "H"

def test_write_pdb_blank_chain_id_insertion_code(self):
"""
Ensure PDB files are written with coords by OE when chain ID or insertion code is blank string.
(reference: https://github.com/openforcefield/openff-toolkit/issues/1967).
"""
toolkit = OpenEyeToolkitWrapper()
water = Molecule()
water.add_atom(1, 0, False)
water.add_atom(8, 0, False)
water.add_atom(1, 0, False)
water.add_bond(0, 1, 1, False)
water.add_bond(1, 2, 1, False)
water.add_conformer(
Quantity(
np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]),
unit.angstrom,
)
)
water.atoms[0].metadata["insertion_code"] = ""
water.atoms[1].metadata["chain_id"] = ""
with NamedTemporaryFile(suffix='.pdb') as of:
water.to_file(of.name, "pdb", toolkit_registry=toolkit)
roundtripped = toolkit.from_file(of.name, file_format='pdb')
np.testing.assert_allclose(
water.conformers[0].m_as(unit.angstrom),
roundtripped[0].conformers[0].m_as(unit.angstrom)
)

def test_get_sdf_coordinates(self):
"""Test OpenEyeToolkitWrapper for importing a single set of coordinates from a sdf file"""

Expand Down
14 changes: 12 additions & 2 deletions openff/toolkit/utils/openeye_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,12 +1664,22 @@ def to_openeye(
res.SetResidueNumber(1)

if "insertion_code" in off_atom.metadata:
res.SetInsertCode(off_atom.metadata["insertion_code"])
# Replace blank string with single character space to avoid OE PDB writing issue
# https://github.com/openforcefield/openff-toolkit/issues/1967
if off_atom.metadata["insertion_code"] == "":
res.SetInsertCode(" ")
else:
res.SetInsertCode(off_atom.metadata["insertion_code"])
else:
res.SetInsertCode(" ")

if "chain_id" in off_atom.metadata:
res.SetChainID(off_atom.metadata["chain_id"])
# Replace blank string with single character space to avoid OE PDB writing issue
# https://github.com/openforcefield/openff-toolkit/issues/1967
if off_atom.metadata["chain_id"] == "":
res.SetChainID(" ")
else:
res.SetChainID(off_atom.metadata["chain_id"])
else:
res.SetChainID(" ")

Expand Down

0 comments on commit a1d2bee

Please sign in to comment.