Skip to content

Commit

Permalink
address comments and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
cbouy committed Apr 1, 2024
1 parent 12b2436 commit 11f68ac
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
9 changes: 8 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The rules for this file:
-------------------------------------------------------------------------------
??/??/?? IAlibay, HeetVekariya, marinegor, lilyminium, RMeli,
ljwoods2, aditya292002, pstaerk, PicoCentauri, BFedder,
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs
tyler.je.reddy, SampurnaM, leonwehrhan, kainszs, cbouy

* 2.8.0

Expand All @@ -36,6 +36,8 @@ Fixes
* Fix groups.py doctests using sphinx directives (Issue #3925, PR #4374)

Enhancements
* Added `TemplateInferer` and `RDKitInferer` dataclasses to the
`RDKitInferring` module to be used by the RDKit converter. (PR #4305)
* Added a tqdm progress bar for `MDAnalysis.analysis.pca.PCA.transform()`
(PR #4531)
* Improved performance of PDBWriter (Issue #2785, PR #4472)
Expand All @@ -46,6 +48,9 @@ Enhancements
DOI 10.1021/acs.jpcb.7b11988. (Issue #2039, PR #4524)

Changes
* Refactored the RDKit converter code to move the inferring code in a separate
`RDKitInferring` module. The bond order and charges inferer has been move to
an `MDAnalysisInferer` dataclass in there. (PR #4305)
* As per SPEC0 the minimum supported Python version has been raised
to 3.10 (PR #4502)
* MDAnalysis.analysis.hole2 is now directly imported from the mdakit
Expand All @@ -63,6 +68,8 @@ Changes
numpy.testing.assert_allclose #4438)

Deprecations
* The RDKit converter parameter `NoImplicit` has been deprecated in favour of
`implicit_hydrogens` and `inferer` parameters. (PR #4305)
* The MDAnalysis.analysis.waterdynamics module has been deprecated in favour
of the waterdynamics MDAKit and will be removed in version 3.0.0 (PR #4404)
* The MDAnalysis.analysis.psa module has been deprecated in favour of
Expand Down
32 changes: 20 additions & 12 deletions package/MDAnalysis/converters/RDKit.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ class RDKitConverter(base.ConverterBase):
>>> mol = u.atoms.convert_to.rdkit(inferer=infer_from_template)
Note that all builtin inferer functions can be found in the
:mod:`RDKitInferring` module.
Since one of the main use case of the converter is converting trajectories
and not just a topology, creating a new molecule from scratch for every
frame would be too slow so the converter uses a caching system. The cache
Expand Down Expand Up @@ -292,7 +295,7 @@ class RDKitConverter(base.ConverterBase):
files while the original name is still available through
``atom.GetProp("_MDAnalysis_name")``
.. versionchanged:: 2.7.0
.. versionchanged:: 2.8.0
Deprecated ``max_iter`` (moved to the inferer class
:class:`~MDAnalysis.converters.RDKitInferring.MDAnalysisInferer`) and
deprecated ``NoImplicit`` in favor of ``implicit_hydrogens``. Added
Expand Down Expand Up @@ -341,20 +344,20 @@ def convert(
raise ImportError(
"RDKit is required for the RDKitConverter but "
"it's not installed. Try installing it with \n"
"conda install -c conda-forge rdkit"
)
"`conda install -c conda-forge rdkit` or `pip install rdkit`"
) from None
try:
# make sure to use atoms (Issue 46)
ag = obj.atoms
except AttributeError:
raise TypeError(
"No `atoms` attribute in object of type {}, "
"please use a valid AtomGroup or Universe".format(type(obj))
f"No `atoms` attribute in object of type {type(obj)!r}, "
"please use a valid AtomGroup or Universe"
) from None

if (max_iter := kwargs.get("max_iter")) is not None:
warnings.warn(
"Using `max_iter` is deprecated, use `MDAnalysisInferer "
"Using `max_iter` is deprecated, use `MDAnalysisInferer"
"(max_iter=...)` instead",
DeprecationWarning,
)
Expand Down Expand Up @@ -427,6 +430,12 @@ def atomgroup_to_mol(
inferer : Optional[Callable[[rdkit.Chem.rdchem.Mol], rdkit.Chem.rdchem.Mol]]
A callable to infer bond orders and charges for the RDKit molecule
created by the converter. If ``None``, inferring is skipped.
.. versionchanged:: 2.8.0
Deprecated ``NoImplicit`` in favor of ``implicit_hydrogens``. Added
``inferer`` to specify a callable that can transform the molecule (this
operation is cached).
"""
try:
elements = ag.elements
Expand Down Expand Up @@ -469,7 +478,7 @@ def atomgroup_to_mol(

# attributes accepted in PDBResidueInfo object
pdb_attrs = {}
for attr in RDATTRIBUTES.keys():
for attr in RDATTRIBUTES:
if hasattr(ag, attr):
pdb_attrs[attr] = getattr(ag, attr)
resnames = pdb_attrs.get("resnames", None)
Expand Down Expand Up @@ -504,10 +513,9 @@ def get_resname(idx):
_add_mda_attr_to_rdkit(attr, values[i], mi, get_resname(i))
rdatom.SetMonomerInfo(mi)
# other properties
for attr in other_attrs.keys():
value = other_attrs[attr][i]
attr = "_MDAnalysis_%s" % _TOPOLOGY_ATTRS[attr].singular
_set_atom_property(rdatom, attr, value)
for attr, values in other_attrs.items():
attr = f"_MDAnalysis_{_TOPOLOGY_ATTRS[attr].singular}"
_set_atom_property(rdatom, attr, values[i])
_set_atom_property(rdatom, "_MDAnalysis_index", i)
# add atom
index = mol.AddAtom(rdatom)
Expand Down Expand Up @@ -577,7 +585,7 @@ def _add_mda_attr_to_rdkit(attr, value, mi, resname=""):

# set attribute value in RDKit MonomerInfo
rdattr = RDATTRIBUTES[attr]
getattr(mi, "Set%s" % rdattr)(value)
getattr(mi, f"Set{rdattr}")(value)


def _set_str_prop(atom, attr, value):
Expand Down
12 changes: 8 additions & 4 deletions package/MDAnalysis/converters/RDKitInferring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the GNU Public Licence, v2 or any higher version
# Released under the GNU Lesser General Public License, v2 or higher
#
# Please cite your use of MDAnalysis in published work:
#
Expand Down Expand Up @@ -69,8 +69,8 @@

with suppress(ImportError):
from rdkit.Chem.rdDetermineBonds import (
DetermineBondOrders, # available since 2022.09.1
)
DetermineBondOrders,
) # available since 2022.09.1


def reorder_atoms(
Expand Down Expand Up @@ -124,7 +124,7 @@ class MDAnalysisInferer:
fix challenging cases must have single reactant and product, and
cannot add any atom.
.. versionadded:: 2.7.0
.. versionadded:: 2.8.0
"""

MONATOMIC_CATION_CHARGES: ClassVar[Dict[int, int]] = {
Expand Down Expand Up @@ -624,6 +624,8 @@ class TemplateInferer:
back. Useful to avoid adding explicit hydrogens on the template which
can prevent RDKit from finding a match between the template and the
molecule.
.. versionadded:: 2.8.0
"""

template: "Chem.Mol"
Expand Down Expand Up @@ -689,6 +691,8 @@ class RDKitInferer:
"""Uses RDKit's :func:`~rdkit.Chem.rdDetermineBonds.DetermineBondOrders`
to infer bond orders and formal charges. This is the same algorithm used
by the :ref:`xyz2mol <https://github.com/jensengroup/xyz2mol>` package.
.. versionadded:: 2.8.0
"""

charge: int = 0
Expand Down
22 changes: 13 additions & 9 deletions package/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ build-backend = "setuptools.build_meta"
[project]
name = "MDAnalysis"
dynamic = ['version', 'readme']
license = {file = "LICENSE"}
license = { file = "LICENSE" }
description = "An object-oriented toolkit to analyze molecular dynamics trajectories."
authors = [
{name = 'MDAnalysis Development Team', email = 'mdanalysis@numfocus.org'}
{ name = 'MDAnalysis Development Team', email = 'mdanalysis@numfocus.org' },
]
maintainers = [
{name = 'MDAnalysis Core Developers', email = 'mdanalysis@numfocus.org'}
{ name = 'MDAnalysis Core Developers', email = 'mdanalysis@numfocus.org' },
]
requires-python = ">=3.10"
dependencies = [
Expand All @@ -47,8 +47,14 @@ dependencies = [
'mdahole2',
]
keywords = [
"python", "science", "chemistry", "biophysics", "molecular-dynamics",
"computational-chemistry", "molecular-simulation", "analysis",
"python",
"science",
"chemistry",
"biophysics",
"molecular-dynamics",
"computational-chemistry",
"molecular-simulation",
"analysis",
"trajectory-analysis",
]
classifiers = [
Expand Down Expand Up @@ -79,7 +85,7 @@ extra_formats = [
"pyedr>=0.7.0",
"pytng>=0.2.3",
"gsd>3.0.0",
"rdkit>=2021.09.1",
"rdkit>=2022.09.1",
]
analysis = [
"biopython>=1.80",
Expand Down Expand Up @@ -117,6 +123,4 @@ zip-safe = false
find = {}

[tool.setuptools.package-data]
MDAnalysis = [
'analysis/data/*.npy',
]
MDAnalysis = ['analysis/data/*.npy']

0 comments on commit 11f68ac

Please sign in to comment.