Skip to content

Commit

Permalink
Adds deprecation warning when trying to add_TopAttr bfactors or tempf…
Browse files Browse the repository at this point in the history
…actors (#3161)

Towards #1901

## Work done in this PR
- Adds a warning when trying to add_TopologyAttr for bfactors or tempfactors when the other already exists.
  • Loading branch information
lilyminium authored Mar 14, 2021
1 parent 7ed893d commit 1eede18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Changes
* Maximum pinned versions in setup.py removed for python 3.6+ (PR #3139)

Deprecations
* Deprecated tempfactors and bfactors being separate
TopologyAttrs, with a warning (PR #3161)
* hbonds.WaterBridgeAnalysis will be removed in 2.0.0 and
replaced with hydrogenbonds.WaterBridgeAnalysis (#3111)

Expand Down
18 changes: 18 additions & 0 deletions package/MDAnalysis/core/universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,14 @@ def add_TopologyAttr(self, topologyattr, values=None):
Can now also add TopologyAttrs with a string of the name of the
attribute to add (eg 'charges'), can also supply initial values
using values keyword.
.. versionchanged:: 1.1.0
Now warns when adding bfactors to a Universe with
existing tempfactors, or adding tempfactors to a
Universe with existing bfactors.
In version 2.0, MDAnalysis will stop treating
tempfactors and bfactors as separate attributes. Instead,
they will be aliases of the same attribute.
"""
if isinstance(topologyattr, six.string_types):
try:
Expand All @@ -850,6 +858,16 @@ def add_TopologyAttr(self, topologyattr, values=None):
n_residues=self._topology.n_residues,
n_segments=self._topology.n_segments,
values=values)
alias_pairs = [("bfactors", "tempfactors"),
("tempfactors", "bfactors")]
for a, b in alias_pairs:
if topologyattr.attrname == a and hasattr(self._topology, b):
err = ("You are adding {a} to a Universe that "
"has {b}. From MDAnalysis version 2.0, {a} "
"and {b} will no longer be separate "
"TopologyAttrs. Instead, they will be aliases "
"of the same attribute.").format(a=a, b=b)
warnings.warn(err, DeprecationWarning)
self._topology.add_TopologyAttr(topologyattr)
self._process_attr(topologyattr)

Expand Down
13 changes: 13 additions & 0 deletions testsuite/MDAnalysisTests/core/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
GRO, TRR,
two_water_gro, two_water_gro_nonames,
TRZ, TRZ_psf,
PDB, MMTF,
)

import MDAnalysis as mda
Expand Down Expand Up @@ -1136,3 +1137,15 @@ def test_empty_creation_raises_error(self):
def test_as_Universe_deprecation():
with pytest.deprecated_call():
_ = mda.core.universe.as_Universe(PSF, DCD)


def test_deprecate_b_tempfactors():
u = mda.Universe(PDB)
with pytest.warns(DeprecationWarning, match="alias"):
u.add_TopologyAttr("bfactors")


def test_deprecate_temp_bfactors():
u = mda.Universe(MMTF)
with pytest.warns(DeprecationWarning, match="alias"):
u.add_TopologyAttr("tempfactors")

0 comments on commit 1eede18

Please sign in to comment.