Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for enpty coordinates in RDKit converter #4824

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

# 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
14 changes: 13 additions & 1 deletion testsuite/MDAnalysisTests/converters/test_rdkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,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 @@ -692,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
Loading