Skip to content

Commit

Permalink
add warnings for multiple molecules and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
j-wags committed Sep 27, 2024
1 parent 747c2f1 commit 44d9597
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
15 changes: 15 additions & 0 deletions openff/toolkit/_tests/test_toolkits.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,14 @@ def test_from_openeye_transition_metal_radical(self):

OpenEyeToolkitWrapper().from_openeye(oemol)

def test_from_openeye_multiple_molecule(self):
"""Test that parsing a OEMol that is actually multiple disconnected molecules raises a warning"""
from openeye import oechem
oemol = oechem.OEMol()
oechem.OESmilesToMol(oemol, "C.N")
with pytest.warns(match="more than one molecule"):
OpenEyeToolkitWrapper().from_openeye(oemol)

def test_from_openeye_implicit_hydrogen(self):
"""
Test OpenEyeToolkitWrapper for loading a molecule with implicit
Expand Down Expand Up @@ -2654,6 +2662,13 @@ def test_from_rdkit_transition_metal_radical(self):

RDKitToolkitWrapper().from_rdkit(rdmol)

def test_from_rdkit_multiple_molecule(self):
"""Test that parsing a rdmol that is actually multiple disconnected molecules raises a warning"""
from rdkit import Chem
rdmol = Chem.MolFromSmiles("C.N")
with pytest.warns(match="more than one molecule"):
RDKitToolkitWrapper().from_rdkit(rdmol)

@pytest.mark.parametrize(
"smiles, expected_map", [("[Cl:1][Cl]", {0: 1}), ("[Cl:1][Cl:2]", {0: 1, 1: 2})]
)
Expand Down
15 changes: 13 additions & 2 deletions openff/toolkit/utils/openeye_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pathlib
import re
import tempfile
import warnings
from collections import defaultdict
from functools import wraps
from typing import TYPE_CHECKING, Any, Optional, Union
Expand Down Expand Up @@ -1061,8 +1062,6 @@ def _check_mol2_gaff_atom_type(molecule, file_path=None):
except KeyError:
pass
else:
import warnings

warn_msg = (
f'OpenEye interpreted the type "{atom_type}" in {file_path}{molecule.name}'
f" as {element_name}. Does your mol2 file uses Tripos SYBYL atom types?"
Expand Down Expand Up @@ -1203,6 +1202,18 @@ def from_openeye(

oemol = oechem.OEMol(oemol)

components = oechem.OEDetermineComponents(oemol)
if components[0] > 1:
warnings.warn("OpenEye OEMol passed to from_openeye consists of more than one molecule, consider running "
"something like https://docs.eyesopen.com/toolkits/python/oechemtk/oechem_examples/"
"oechem_example_parts2mols.html or splitting input SMILES at '.'s to get separate molecules and pass them "
"to from_openeye one at a time. While this is supported for legacy reasons, OpenFF Molecule "
"objects are not supposed to contain disconnected chemical graphs and this may result in "
"undefined behavior. The OpenFF ecosystem is perfectly capable of handling multiple "
"molecules, you just have to add them together in an OpenFF Topology object.",
stacklevel=2
)

# Add explicit hydrogens if they're implicit
if oechem.OEHasImplicitHydrogens(oemol):
oechem.OEAddExplicitHydrogens(oemol)
Expand Down
12 changes: 12 additions & 0 deletions openff/toolkit/utils/rdkit_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,7 @@ def from_rdkit(
"""
from rdkit import Chem
from rdkit.Chem import AllChem

if _cls is None:
from openff.toolkit.topology.molecule import Molecule
Expand All @@ -2241,6 +2242,17 @@ def from_rdkit(
# Make a copy of the RDKit Mol as we'll need to change it (e.g. assign stereo).
rdmol = Chem.Mol(rdmol)

frags = AllChem.GetMolFrags(rdmol)
if len(frags) > 1:
warnings.warn("RDKit Molecule passed to from_rdkit consists of more than one molecule, consider running "
"rdkit.Chem.AllChem.GetMolfrags(rdmol, asMols=True) or splitting input SMILES at '.' to get separate molecules and pass them "
"to from_rdkit one at a time. While this is supported for legacy reasons, OpenFF Molecule "
"objects are not supposed to contain disconnected chemical graphs and this may result in "
"undefined behavior. The OpenFF ecosystem is perfectly capable of handling multiple "
"molecules, you just have to add them together in an OpenFF Topology object.",
stacklevel=2
)

if not hydrogens_are_explicit:
rdmol = Chem.AddHs(rdmol, addCoords=True)

Expand Down

0 comments on commit 44d9597

Please sign in to comment.