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

Deprecate nucleicacids pair distances results in favour of results.pair_distances [Issue 3744] #3958

Merged
merged 7 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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: 3 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ Changes
* adding element attribute to TXYZParser if all atom names are valid element symbols (PR #3826)

Deprecations
* Direct indexing of results in analysis.nucleicacids' NucPairDist
and WatsonCrickDist classes is deprecated and will be removed in 2.5.0.
Please use `results.pair_distances` instead (Issue #3744)
* Deprecated analysis.align.sequence_alignment() for removal in 3.0 (#3950)
* Add deprecation warning for `timestep` copying in DCDReader
(Issue #3889, PR #3888)
Expand Down
133 changes: 86 additions & 47 deletions package/MDAnalysis/analysis/nucleicacids.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
Denning2012

Distances
_________
---------

.. autoclass:: NucPairDist
:members:
Expand All @@ -60,6 +60,7 @@
"""

from typing import List, Dict
import warnings

import numpy as np

Expand All @@ -69,6 +70,26 @@
from MDAnalysis.core.groups import Residue


# Remove in 2.5.0
class DeprecatedResults(Results):
orbeckst marked this conversation as resolved.
Show resolved Hide resolved
def __getitem__(self, key):
if key in self.data:
return self.data[key]
if hasattr(self.__class__, "__missing__"):
return self.__class__.__missing__(self, key)
if isinstance(key, int):
try:
item = self['pair_distances'][:, key]
except KeyError:
raise KeyError(key)
else:
wmsg = ("Accessing results via selection indices is "
"deprecated and will be removed in MDAnalysis 2.5.0")
warnings.warn(wmsg)
return item
raise KeyError(key)


class NucPairDist(AnalysisBase):
r"""Atom Pair distance calculation base class.

Expand All @@ -79,30 +100,47 @@ class NucPairDist(AnalysisBase):
:class:`~MDAnalysis.core.groups.AtomGroup`.

Parameters
__________
----------
selection1: List[AtomGroup]
list of :class:`~MDAnalysis.core.groups.AtomGroup` containing an atom
List of :class:`~MDAnalysis.core.groups.AtomGroup` containing an atom
of each nucleic acid being analyzed.
selection1: List[AtomGroup]
list of :class:`~MDAnalysis.core.groups.AtomGroup` containing an atom
selection2: List[AtomGroup]
List of :class:`~MDAnalysis.core.groups.AtomGroup` containing an atom
of each nucleic acid being analyzed.
kwargs: dict
arguments for :class:`~MDAnalysis.analysis.base.AnalysisBase`
Arguments for :class:`~MDAnalysis.analysis.base.AnalysisBase`

Attributes
__________
results: numpy.ndarray
first index is selection second index is time
results.times: numpy.ndarray
times used in analysis
----------
results: numpy.ndarray
Array of pair distances. First index is selection, second index is time.

.. deprecated:: 2.4.0
Will be removed in MDAnalysis 2.5.0. Please use
:attr:`results.pair_distances` instead.

results.times: numpy.ndarray
Simulation times used in analysis
orbeckst marked this conversation as resolved.
Show resolved Hide resolved
results.pair_distances: numpy.ndarray
2D array of pair distances. First dimension is simulation time, second
dimension contains the pair distances for each each entry pair in
selection1 and selection2.

.. versionadded:: 2.4.0


Raises
______
------

ValueError
if the selections given are not the same length
If the selections given are not the same length


"""
.. versionchanged:: 2.4.0
Accessing results by passing selection indices to :attr:`results` is
now deprecated and will be removed in MDAnalysis version 2.5.0. Please
use :attr:`results.pair_distances` instead.
"""

_s1: mda.AtomGroup
_s2: mda.AtomGroup
Expand All @@ -127,23 +165,18 @@ def __init__(self, selection1: List[mda.AtomGroup],
self._s1 += selection1[i]
self._s2 += selection2[i]

self.results = Results()
self.results = DeprecatedResults()

def _prepare(self) -> None:
self._res_dict = {k: [] for k in range(self._n_sel)}
self._times = []
self._res_array: np.ndarray = np.zeros([self.n_frames, self._n_sel])

def _single_frame(self) -> None:
dist: np.ndarray = calc_bonds(self._s1.positions, self._s2.positions)

for i in range(self._n_sel):
self._res_dict[i].append(dist[i])
self._times.append(self._ts.time)
self._res_array[self._frame_index, :] = dist

def _conclude(self) -> None:
self.results['times'] = np.array(self._times)
for i in range(self._n_sel):
self.results[i] = np.array(self._res_dict[i])
self.results['times'] = np.array(self.times)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be removed in 2.5.0

self.results['pair_distances'] = self._res_array


class WatsonCrickDist(NucPairDist):
Expand All @@ -154,48 +187,54 @@ class WatsonCrickDist(NucPairDist):
their index in the lists given as arguments.

Parameters
__________
----------
strand1: List[Residue]
First list of bases
strand2: List[Residue]
Second list of bases
n1_name: str (optional)
Name of Nitrogen 1 of nucleic acids
by default assigned to N1
Name of Nitrogen 1 of nucleic acids, by default assigned to N1
n3_name: str (optional)
Name of Nitrogen 3 of nucleic acids
by default assigned to N3
Name of Nitrogen 3 of nucleic acids, by default assigned to N3
g_name: str (optional)
Name of Guanine in topology
by default assigned to G
Name of Guanine in topology, by default assigned to G
a_name: str (optional)
Name of Adenine in topology
by default assigned to G
Name of Adenine in topology, by default assigned to G
IAlibay marked this conversation as resolved.
Show resolved Hide resolved
u_name: str (optional)
Name of Uracil in topology
by default assigned to U
Name of Uracil in topology, by default assigned to U
t_name: str (optional)
Name of Thymine in topology
by default assigned to T
Name of Thymine in topology, by default assigned to T
c_name: str (optional)
Name of Cytosine in topology
by default assigned to C
Name of Cytosine in topology, by default assigned to C
**kwargs: dict
arguments for :class:`~MDAnalysis.analysis.base.AnalysisBase`

Attributes
__________
results: numpy.ndarray
first index is selection second index is time
results.times: numpy.ndarray
times used in analysis
----------
results: numpy.ndarray
Array of Watson-Crick basepair distances. First index is selection,
second index is time.

.. deprecated:: 2.4.0
Will be removed in MDAnalysis 2.5.0. Please use
:attr:`results.pair_distances` instead.

results.times: numpy.ndarray
Simulation times used in analysis
results.pair_distances: numpy.ndarray
2D array of Watson-Crick basepair distances. First dimension is
simulation time, second dimension contains the pair distances for
each each entry pair in strand1 and strand2.

.. versionadded:: 2.4.0


Raises
______
------
ValueError
if the residues given are not amino acids
If the residues given are not amino acids
ValueError
if the selections given are not the same length
If the selections given are not the same length

"""

Expand Down
11 changes: 9 additions & 2 deletions testsuite/MDAnalysisTests/analysis/test_nucleicacids.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,12 @@ def test_wc_dist(u):
WC = WatsonCrickDist(strand1, strand2)
WC.run()

assert_allclose(WC.results[0][0], 4.3874702, atol=1e-3)
assert_allclose(WC.results[1][0], 4.1716404, atol=1e-3)
assert_allclose(WC.results.pair_distances[0, 0], 4.3874702, atol=1e-3)
assert_allclose(WC.results.pair_distances[0, 1], 4.1716404, atol=1e-3)

wmsg = ("Accessing results via selection indices is "
"deprecated and will be removed in MDAnalysis 2.5.0")

with pytest.warns(match=wmsg):
IAlibay marked this conversation as resolved.
Show resolved Hide resolved
for i in range(len(strand1)):
assert_allclose(WC.results.pair_distances[:, i], WC.results[i][0])