Skip to content

Commit

Permalink
density_from_PDB: deprecated, test, and bugfix
Browse files Browse the repository at this point in the history
- density_from_PDB is terrible code (I wrote it) and not used: deprecated and to be
  removed for 2.0.0
- added test so that it is covered
- fixed bug that was uncovered by test (us ag.tempfactors)
- added test for Bfactors2RMSF
  • Loading branch information
orbeckst committed Feb 19, 2020
1 parent d792cc9 commit 4892532
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ Deprecations
* analysis.hbonds.HydrogenBondAnalysis is deprecated in 1.0 (remove in 2.0)
* analysis.density.density_from_Universe() (remove in 2.0)
* analysis.density.notwithin_coordinates_factory() (remove in 2.0)
* analysis.density.density_from_PDB and BfactorDensityCreator (remove in 2.0)



09/05/19 IAlibay, richardjgowers

* 0.20.1
Expand Down
18 changes: 14 additions & 4 deletions package/MDAnalysis/analysis/density.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,7 @@ def Bfactor2RMSF(B):
return np.sqrt(3. * B / 8.) / np.pi


@deprecate(release="1.0.0", remove="2.0.0")
def density_from_PDB(pdb, **kwargs):
"""Create a density from a single frame PDB.
Expand Down Expand Up @@ -1313,10 +1314,15 @@ def density_from_PDB(pdb, **kwargs):
--------
:func:`Bfactor2RMSF` and :class:`BfactorDensityCreator`
.. deprecated: 1.0.0
This function is not well tested or optimized and will be removed
in 2.0.0.
"""
return BfactorDensityCreator(pdb, **kwargs).Density()


@deprecate(release="1.0.0", remove="2.0.0")
class BfactorDensityCreator(object):
"""Create a density grid from a pdb file using MDAnalysis.
Expand All @@ -1337,6 +1343,10 @@ class BfactorDensityCreator(object):
.. * Using a temporary Creator class with the
.. :meth:`BfactorDensityCreator.Density` helper method is clumsy.
.. deprecated: 1.0.0
This function is not well tested or optimized and will be removed
in 2.0.0.
"""

@deprecate(release="1.0.0", remove="2.0.0")
Expand Down Expand Up @@ -1405,11 +1415,11 @@ def __init__(self, pdb, delta=1.0, select='resname HOH and name O',
if sigma is None:
# histogram individually, and smear out at the same time
# with the appropriate B-factor
if np.any(group.bfactors == 0.0):
if np.any(group.tempfactors == 0.0):
wmsg = "Some B-factors are Zero (will be skipped)."
logger.warning(wmsg)
warnings.warn(wmsg, category=MissingDataWarning)
rmsf = Bfactor2RMSF(group.bfactors)
rmsf = Bfactor2RMSF(group.tempfactors)
grid *= 0.0 # reset grid
self.g = self._smear_rmsf(coord, grid, self.edges, rmsf)
else:
Expand Down Expand Up @@ -1450,7 +1460,7 @@ def _smear_sigma(self, grid, sigma):
for iwat in range(len(pos[0])): # super-ugly loop
p = tuple([wp[iwat] for wp in pos])
g += grid[p] * np.fromfunction(self._gaussian, grid.shape, dtype=np.int, p=p, sigma=sigma)
print("Smearing out atom position {0:4d}/{1:5d} with RMSF {2:4.2f} A\r".format(iwat + 1, len(pos[0]), sigma),)
# print("Smearing out atom position {0:4d}/{1:5d} with RMSF {2:4.2f} A\r".format(iwat + 1, len(pos[0]), sigma),)
return g

def _smear_rmsf(self, coordinates, grid, edges, rmsf):
Expand All @@ -1463,7 +1473,7 @@ def _smear_rmsf(self, coordinates, grid, edges, rmsf):
continue
g += np.fromfunction(self._gaussian_cartesian, grid.shape, dtype=np.int,
c=coord, sigma=rmsf[iwat])
print("Smearing out atom position {0:4d}/{1:5d} with RMSF {2:4.2f} A\r".format(iwat + 1, N, rmsf[iwat]),)
# print("Smearing out atom position {0:4d}/{1:5d} with RMSF {2:4.2f} A\r".format(iwat + 1, N, rmsf[iwat]),)
return g

def _gaussian(self, i, j, k, p, sigma):
Expand Down
14 changes: 13 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import MDAnalysis as mda
from MDAnalysis.analysis import density

from MDAnalysisTests.datafiles import TPR, XTC, GRO
from MDAnalysisTests.datafiles import TPR, XTC, GRO, PDB_full


class TestDensity(object):
Expand Down Expand Up @@ -425,3 +425,15 @@ def test_Bfactor2RMSF(B):
values = density.Bfactor2RMSF(B)
ref = np.sqrt(3. * B / 8.) / np.pi # original equation
assert_almost_equal(values, ref)

@pytest.mark.parametrize('sigma,ref_shape,ref_gridsum',
[(None, (21, 26, 29), 108.710149),
(1.5, (21, 26, 29), 160.050167)])
def test_density_from_PDB(sigma, ref_shape, ref_gridsum):
# simple test that the code runs: use B-factors from PDB or fixed sigma
D = density.density_from_PDB(PDB_full, delta=2.0, sigma=sigma)

assert isinstance(D, density.Density)
assert_equal(D.grid.shape, ref_shape)
assert_almost_equal(D.grid.sum(), ref_gridsum, decimal=6)

0 comments on commit 4892532

Please sign in to comment.