Skip to content

Commit

Permalink
Merge pull request #388 from OpenBioSim/fix_sire_287
Browse files Browse the repository at this point in the history
Allow user to force stereo inference
  • Loading branch information
lohedges authored Feb 12, 2025
2 parents c649536 + a97cc19 commit b12baa4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
26 changes: 24 additions & 2 deletions python/BioSimSpace/Convert/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from sire import convert as _sire_convert
from sire import smiles as _sire_smiles

import sire.legacy.Base as _SireBase
import sire.legacy.Mol as _SireMol
import sire.legacy.System as _SireSystem
import sire.legacy.Vol as _SireVol
Expand Down Expand Up @@ -146,7 +147,7 @@ def supportedFormats():
return _sire_convert.supported_formats()


def to(obj, format="biosimspace", property_map={}):
def to(obj, format="biosimspace", property_map={}, **kwargs):
"""
Convert an object to a specified format.
Expand Down Expand Up @@ -187,6 +188,13 @@ def to(obj, format="biosimspace", property_map={}):
if not isinstance(property_map, dict):
raise TypeError("'property_map' must be of type 'dict'.")

# Check for force_stereo_inference in kwargs.
if "force_stereo_inference" in kwargs:
force_stereo_inference = kwargs["force_stereo_inference"]
if not isinstance(force_stereo_inference, bool):
raise TypeError("'force_stereo_inference' must be of type 'bool'.")
property_map["force_stereo_inference"] = _SireBase.wrap(force_stereo_inference)

# Special handling for OpenMM conversion. Currently this is a one-way (toOpenMM)
# conversion only and is only supported for specific Sire and BioSimSpace types.
if format == "openmm":
Expand Down Expand Up @@ -508,7 +516,7 @@ def toOpenMM(obj, property_map={}):
)


def toRDKit(obj, property_map={}):
def toRDKit(obj, force_stereo_inference=False, property_map={}):
"""
Convert an object to RDKit format.
Expand All @@ -518,6 +526,11 @@ def toRDKit(obj, property_map={}):
obj :
The input object to convert.
bool : force_stereo_inference
Whether to force inference of stereochemistry, overriding any
stereochemistry present in the input object. This is useful when
the object has been loaded from a file with invalid stereochemistry.
property_map : dict
A dictionary that maps system "properties" to their user defined
values. This allows the user to refer to properties with their
Expand All @@ -529,6 +542,15 @@ def toRDKit(obj, property_map={}):
converted_obj :
The object in OpenMM format.
"""

if not isinstance(force_stereo_inference, bool):
raise TypeError("'force_stereo_inference' must be of type 'bool'.")

if not isinstance(property_map, dict):
raise TypeError("'property_map' must be of type 'dict'.")

property_map["force_stereo_inference"] = _SireBase.wrap(force_stereo_inference)

return to(obj, format="rdkit", property_map=property_map)


Expand Down
26 changes: 24 additions & 2 deletions python/BioSimSpace/Sandpit/Exscientia/Convert/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from sire import convert as _sire_convert
from sire import smiles as _sire_smiles

import sire.legacy.Base as _SireBase
import sire.legacy.Mol as _SireMol
import sire.legacy.System as _SireSystem
import sire.legacy.Vol as _SireVol
Expand Down Expand Up @@ -146,7 +147,7 @@ def supportedFormats():
return _sire_convert.supported_formats()


def to(obj, format="biosimspace", property_map={}):
def to(obj, format="biosimspace", property_map={}, **kwargs):
"""
Convert an object to a specified format.
Expand Down Expand Up @@ -187,6 +188,13 @@ def to(obj, format="biosimspace", property_map={}):
if not isinstance(property_map, dict):
raise TypeError("'property_map' must be of type 'dict'.")

# Check for force_stereo_inference in kwargs.
if "force_stereo_inference" in kwargs:
force_stereo_inference = kwargs["force_stereo_inference"]
if not isinstance(force_stereo_inference, bool):
raise TypeError("'force_stereo_inference' must be of type 'bool'.")
property_map["force_stereo_inference"] = _SireBase.wrap(force_stereo_inference)

# Special handling for OpenMM conversion. Currently this is a one-way (toOpenMM)
# conversion only and is only supported for specific Sire and BioSimSpace types.
if format == "openmm":
Expand Down Expand Up @@ -508,7 +516,7 @@ def toOpenMM(obj, property_map={}):
)


def toRDKit(obj, property_map={}):
def toRDKit(obj, force_stereo_inference=False, property_map={}):
"""
Convert an object to RDKit format.
Expand All @@ -518,6 +526,11 @@ def toRDKit(obj, property_map={}):
obj :
The input object to convert.
bool : force_stereo_inference
Whether to force inference of stereochemistry, overriding any
stereochemistry present in the input object. This is useful when
the object has been loaded from a file with invalid stereochemistry.
property_map : dict
A dictionary that maps system "properties" to their user defined
values. This allows the user to refer to properties with their
Expand All @@ -529,6 +542,15 @@ def toRDKit(obj, property_map={}):
converted_obj :
The object in OpenMM format.
"""

if not isinstance(force_stereo_inference, bool):
raise TypeError("'force_stereo_inference' must be of type 'bool'.")

if not isinstance(property_map, dict):
raise TypeError("'property_map' must be of type 'dict'.")

property_map["force_stereo_inference"] = _SireBase.wrap(force_stereo_inference)

return to(obj, format="rdkit", property_map=property_map)


Expand Down

0 comments on commit b12baa4

Please sign in to comment.