Skip to content

Commit

Permalink
add safeguard
Browse files Browse the repository at this point in the history
  • Loading branch information
RMeli committed Dec 6, 2024
1 parent 8c34466 commit 53fffa6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ The rules for this file:


-------------------------------------------------------------------------------
??/??/?? IAlibay
??/??/?? IAlibay, RMeli

* 2.9.0

Fixes

Enhancements
* Add check and warning for empty (all zero) coordinates in RDKit converter (PR #4824)

Changes

Expand Down
9 changes: 6 additions & 3 deletions package/MDAnalysis/converters/RDKit.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,12 @@ def convert(self, obj, cache=True, NoImplicit=True, max_iter=200,

# add a conformer for the current Timestep
if hasattr(ag, "positions"):
if np.isnan(ag.positions).any():
warnings.warn("NaN detected in coordinates, the output "
"molecule will not have 3D coordinates assigned")
if np.isnan(ag.positions).any() or np.allclose(
ag.positions, 0.0, rtol=0.0, atol=1e-12
):
warnings.warn("NaN or empty coordinates detected in coordinates, "

Check warning on line 369 in package/MDAnalysis/converters/RDKit.py

View check run for this annotation

Codecov / codecov/patch

package/MDAnalysis/converters/RDKit.py#L369

Added line #L369 was not covered by tests
"the output molecule will not have 3D coordinates "
"assigned")
else:
# assign coordinates
conf = Chem.Conformer(mol.GetNumAtoms())
Expand Down
16 changes: 13 additions & 3 deletions testsuite/MDAnalysisTests/converters/test_rdkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
_standardize_patterns)
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem import rdDistGeom
except ImportError:
pass

Expand Down Expand Up @@ -332,7 +331,7 @@ def test_nan_coords(self):
xyz = u.atoms.positions
xyz[0][2] = np.nan
u.atoms.positions = xyz
with pytest.warns(UserWarning, match="NaN detected"):
with pytest.warns(UserWarning, match="NaN .* detected"):
mol = u.atoms.convert_to("RDKIT")
with pytest.raises(ValueError, match="Bad Conformer Id"):
mol.GetConformer()
Expand Down Expand Up @@ -683,7 +682,6 @@ def test_ions(self, smi):
def test_reorder_atoms(self, smi):
mol = Chem.MolFromSmiles(smi)
mol = Chem.AddHs(mol)
rdDistGeom.EmbedMolecule(mol)
# remove bond order and charges info
pdb = Chem.MolToPDBBlock(mol)
u = mda.Universe(StringIO(pdb), format="PDB")
Expand All @@ -694,6 +692,18 @@ def test_reorder_atoms(self, smi):
expected = [a.GetSymbol() for a in mol.GetAtoms()]
assert values == expected

@pytest.mark.parametrize("smi", [
"O=S(C)(C)=NC",
])
def test_warn_empty_coords(self, smi):
mol = Chem.MolFromSmiles(smi)
mol = Chem.AddHs(mol)
# remove bond order and charges info
pdb = Chem.MolToPDBBlock(mol)
u = mda.Universe(StringIO(pdb), format="PDB")
with pytest.warns(match="NaN or empty coordinates detected"):
u.atoms.convert_to.rdkit()

def test_pdb_names(self):
u = mda.Universe(PDB_helix)
mol = u.atoms.convert_to.rdkit()
Expand Down

0 comments on commit 53fffa6

Please sign in to comment.