Skip to content

Commit a27591c

Browse files
authored
Check for enpty coordinates in RDKit converter (#4824)
* prevents assignment of coordinates if all positions are zero
1 parent 83f702f commit a27591c

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

package/CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ The rules for this file:
1414

1515

1616
-------------------------------------------------------------------------------
17-
??/??/?? IAlibay, ChiahsinChu
17+
??/??/?? IAlibay, ChiahsinChu, RMeli
1818

1919
* 2.9.0
2020

2121
Fixes
2222

2323
Enhancements
24-
* Added `precision` for XYZWriter (Issue #4775, PR #4771)
24+
* Add check and warning for empty (all zero) coordinates in RDKit converter (PR #4824)
25+
* Added `precision` for XYZWriter (Issue #4775, PR #4771)
2526

2627
Changes
2728

package/MDAnalysis/converters/RDKit.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,12 @@ def convert(self, obj, cache=True, NoImplicit=True, max_iter=200,
363363

364364
# add a conformer for the current Timestep
365365
if hasattr(ag, "positions"):
366-
if np.isnan(ag.positions).any():
367-
warnings.warn("NaN detected in coordinates, the output "
368-
"molecule will not have 3D coordinates assigned")
366+
if np.isnan(ag.positions).any() or np.allclose(
367+
ag.positions, 0.0, rtol=0.0, atol=1e-12
368+
):
369+
warnings.warn("NaN or empty coordinates detected in coordinates, "
370+
"the output molecule will not have 3D coordinates "
371+
"assigned")
369372
else:
370373
# assign coordinates
371374
conf = Chem.Conformer(mol.GetNumAtoms())

testsuite/MDAnalysisTests/converters/test_rdkit.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_nan_coords(self):
331331
xyz = u.atoms.positions
332332
xyz[0][2] = np.nan
333333
u.atoms.positions = xyz
334-
with pytest.warns(UserWarning, match="NaN detected"):
334+
with pytest.warns(UserWarning, match="NaN .* detected"):
335335
mol = u.atoms.convert_to("RDKIT")
336336
with pytest.raises(ValueError, match="Bad Conformer Id"):
337337
mol.GetConformer()
@@ -692,6 +692,18 @@ def test_reorder_atoms(self, smi):
692692
expected = [a.GetSymbol() for a in mol.GetAtoms()]
693693
assert values == expected
694694

695+
@pytest.mark.parametrize("smi", [
696+
"O=S(C)(C)=NC",
697+
])
698+
def test_warn_empty_coords(self, smi):
699+
mol = Chem.MolFromSmiles(smi)
700+
mol = Chem.AddHs(mol)
701+
# remove bond order and charges info
702+
pdb = Chem.MolToPDBBlock(mol)
703+
u = mda.Universe(StringIO(pdb), format="PDB")
704+
with pytest.warns(match="NaN or empty coordinates detected"):
705+
u.atoms.convert_to.rdkit()
706+
695707
def test_pdb_names(self):
696708
u = mda.Universe(PDB_helix)
697709
mol = u.atoms.convert_to.rdkit()

0 commit comments

Comments
 (0)