diff --git a/devtools/conda-envs/test_env.yaml b/devtools/conda-envs/test_env.yaml index f1495c3..7049e0d 100644 --- a/devtools/conda-envs/test_env.yaml +++ b/devtools/conda-envs/test_env.yaml @@ -1,4 +1,4 @@ -name: test +name: openff-fragmenter-test channels: - conda-forge @@ -10,6 +10,7 @@ dependencies: - python - openff-toolkit-base >=0.11.0 + - openff-utilities >=0.1.9 - ambertools - rdkit - pydantic diff --git a/openff/fragmenter/chemi.py b/openff/fragmenter/chemi.py index 060f5f2..929202b 100755 --- a/openff/fragmenter/chemi.py +++ b/openff/fragmenter/chemi.py @@ -8,6 +8,7 @@ from openff.fragmenter.utils import get_atom_index, get_map_index from openff.toolkit.topology import Molecule from openff.toolkit.utils import LicenseError, ToolkitUnavailableException +from openff.utilities import MissingOptionalDependencyError, requires_oe_module logger = logging.getLogger(__name__) @@ -164,6 +165,7 @@ def find_ring_systems(molecule: Molecule) -> Dict[int, int]: return ring_systems +@requires_oe_module("oechem") def _find_oe_stereocenters( molecule: Molecule, ) -> Tuple[List[int], List[Tuple[int, int]]]: @@ -277,7 +279,12 @@ def find_stereocenters(molecule: Molecule) -> Tuple[List[int], List[Tuple[int, i try: stereogenic_atoms, stereogenic_bonds = _find_oe_stereocenters(molecule) - except (ModuleNotFoundError, ToolkitUnavailableException, LicenseError): + except ( + ModuleNotFoundError, + ToolkitUnavailableException, + LicenseError, + MissingOptionalDependencyError, + ): stereogenic_atoms, stereogenic_bonds = _find_rd_stereocenters(molecule) return stereogenic_atoms, stereogenic_bonds @@ -379,6 +386,7 @@ def _extract_rd_fragment( return fragment +@requires_oe_module("oechem") def _extract_oe_fragment( molecule: Molecule, atom_indices: Set[int], bond_indices: Set[Tuple[int, int]] ) -> Molecule: @@ -471,7 +479,12 @@ def extract_fragment( try: fragment = _extract_oe_fragment(molecule, atom_indices, bond_indices) - except (ModuleNotFoundError, ToolkitUnavailableException, LicenseError): + except ( + ModuleNotFoundError, + ToolkitUnavailableException, + LicenseError, + MissingOptionalDependencyError, + ): fragment = _extract_rd_fragment(molecule, atom_indices, bond_indices) # Sanity check that all atoms are still bonded diff --git a/openff/fragmenter/depiction.py b/openff/fragmenter/depiction.py index eb1d471..6427873 100644 --- a/openff/fragmenter/depiction.py +++ b/openff/fragmenter/depiction.py @@ -6,9 +6,11 @@ from openff.fragmenter.fragment import BondTuple, FragmentationResult from openff.fragmenter.utils import get_map_index from openff.toolkit.topology import Molecule +from openff.utilities import MissingOptionalDependencyError, requires_oe_module from pkg_resources import resource_filename +@requires_oe_module("oechem") def _oe_fragment_predicates(map_indices: Collection[int]): """Returns an atom and bond predicate which matches atoms whose map index appears in the specified ``map_indices`` collection""" @@ -43,6 +45,7 @@ def CreateCopy(self): return PredicateAtoms(map_indices), PredicateBonds(map_indices) +@requires_oe_module("oedepict") def _oe_wbo_label_display(bond_tuples: Collection[BondTuple]): """Returns a ``OEDisplayBondPropBase`` subclass which will label bonds with the specified map indices with their WBO values if present. @@ -75,6 +78,7 @@ def CreateCopy(self): return LabelWibergBondOrder(bond_tuples) +@requires_oe_module("oedepict") def _oe_render_parent( parent: Molecule, rotor_bonds: Optional[Collection[BondTuple]] = None, @@ -113,6 +117,8 @@ def _oe_render_parent( return svg_contents.decode() +@requires_oe_module("oedepict") +@requires_oe_module("oechem") def _oe_render_fragment( parent: Molecule, fragment: Molecule, @@ -328,7 +334,7 @@ def depict_fragments( for bond_tuple, fragment in fragments.items() ] - except ModuleNotFoundError: + except (ModuleNotFoundError, MissingOptionalDependencyError): header_svg = _rd_render_parent(parent) fragment_svg = [ _rd_render_fragment(parent, fragment, bond_tuple) diff --git a/openff/fragmenter/tests/test_chemi.py b/openff/fragmenter/tests/test_chemi.py index 59b4043..5a657ae 100755 --- a/openff/fragmenter/tests/test_chemi.py +++ b/openff/fragmenter/tests/test_chemi.py @@ -22,6 +22,7 @@ ToolkitRegistry, ) from openff.units import unit +from openff.utilities import MissingOptionalDependencyError def test_assign_elf10_am1_bond_orders(): @@ -151,7 +152,7 @@ def test_find_stereocenters(smiles, expected_atoms, expected_bonds, find_method) try: stereogenic_atoms, stereogenic_bonds = find_method(molecule) - except ModuleNotFoundError as e: + except (ModuleNotFoundError, MissingOptionalDependencyError) as e: pytest.skip(str(e)) assert stereogenic_atoms == expected_atoms @@ -202,7 +203,7 @@ def test_extract_fragment(smiles, atoms, bonds, expected, extract_method): try: fragment = extract_method(molecule, atoms, bonds) - except ModuleNotFoundError as e: + except (ModuleNotFoundError, MissingOptionalDependencyError) as e: pytest.skip(str(e)) expected_fragment = Molecule.from_smiles(expected) diff --git a/openff/fragmenter/tests/test_depiction.py b/openff/fragmenter/tests/test_depiction.py index 536773e..2f9f389 100644 --- a/openff/fragmenter/tests/test_depiction.py +++ b/openff/fragmenter/tests/test_depiction.py @@ -11,6 +11,7 @@ ) from openff.fragmenter.fragment import Fragment, FragmentationResult from openff.toolkit.topology import Molecule +from openff.utilities import MissingOptionalDependencyError @pytest.mark.parametrize("draw_function", [_oe_render_parent, _rd_render_parent]) @@ -21,7 +22,7 @@ def test_xx_render_parent(draw_function): "[C:1]([H:5])([H:6])([H:7])[C:2]([H:8])([H:9])[C:3]([H:10])([H:11])[C:4]([H:12])([H:13])([H:14])" ) ) - except ModuleNotFoundError as e: + except (ModuleNotFoundError, MissingOptionalDependencyError) as e: pytest.skip(str(e)) assert isinstance(svg_contents, str) @@ -41,7 +42,7 @@ def test_xx_render_fragment(draw_function): (1, 2), ) - except ModuleNotFoundError as e: + except (ModuleNotFoundError, MissingOptionalDependencyError) as e: pytest.skip(str(e)) assert isinstance(svg_contents, str)