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

Add pbc kwarg to Contacts #2646

Merged
merged 8 commits into from Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 26 additions & 7 deletions package/MDAnalysis/analysis/contacts.py
Expand Up @@ -376,7 +376,7 @@ class Contacts(AnalysisBase):

"""
def __init__(self, u, select, refgroup, method="hard_cut", radius=4.5,
kwargs=None, **basekwargs):
pbc=False, kwargs=None, **basekwargs):
"""
Parameters
----------
Expand All @@ -392,6 +392,9 @@ def __init__(self, u, select, refgroup, method="hard_cut", radius=4.5,
method : string | callable (optional)
Can either be one of ``['hard_cut' , 'soft_cut', 'radius_cut']`` or a callable
with call signature ``func(r, r0, **kwargs)`` (the "Contacts API").
pbc : bool (optional)
Uses periodic boundary conditions to calculate distances if set to ``True``; the
default is ``False``.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would set the default to True instead, since this is probably what most users would expect the behaviour to be

kwargs : dict, optional
dictionary of additional kwargs passed to `method`. Check
respective functions for reasonable values.
Expand Down Expand Up @@ -424,18 +427,29 @@ def __init__(self, u, select, refgroup, method="hard_cut", radius=4.5,
self.select = select
self.grA = u.select_atoms(select[0])
self.grB = u.select_atoms(select[1])

self.pbc = pbc

# contacts formed in reference
self.r0 = []
self.initial_contacts = []

if isinstance(refgroup[0], AtomGroup):
refA, refB = refgroup
self.r0.append(distance_array(refA.positions, refB.positions))
if(self.pbc):
self.r0.append(distance_array(refA.positions, refB.positions,
box=refA.universe.dimensions))
else:
self.r0.append(distance_array(refA.positions, refB.positions))

self.initial_contacts.append(contact_matrix(self.r0[-1], radius))
else:
for refA, refB in refgroup:
self.r0.append(distance_array(refA.positions, refB.positions))
if(self.pbc):
self.r0.append(distance_array(refA.positions, refB.positions,
box=refA.universe.dimensions))
else:
self.r0.append(distance_array(refA.positions, refB.positions))

self.initial_contacts.append(contact_matrix(self.r0[-1],
radius))

Expand All @@ -444,9 +458,13 @@ def _prepare(self):

def _single_frame(self):
self.timeseries[self._frame_index][0] = self._ts.frame

# compute distance array for a frame
d = distance_array(self.grA.positions, self.grB.positions)
if(self.pbc):
d = distance_array(self.grA.positions, self.grB.positions,
box=self._ts.dimensions)
else:
d = distance_array(self.grA.positions, self.grB.positions)

for i, (initial_contacts, r0) in enumerate(zip(self.initial_contacts,
self.r0), 1):
Expand All @@ -461,7 +479,8 @@ def _new_selections(u_orig, selections, frame):
"""create stand alone AGs from selections at frame"""
u = MDAnalysis.Universe(u_orig.filename, u_orig.trajectory.filename)
u.trajectory[frame]
return [u.select_atoms(s) for s in selections]
temp_u = u.copy()
Copy link
Contributor

Choose a reason for hiding this comment

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

The idea of using copy() here would be to avoid having to reload a universe from the filenames - you can copy u_orig directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can I fix this in another pull request?

return [temp_u.select_atoms(s) for s in selections]


def q1q2(u, select='all', radius=4.5):
Expand Down
23 changes: 22 additions & 1 deletion testsuite/MDAnalysisTests/analysis/test_contacts.py
Expand Up @@ -37,6 +37,8 @@
from MDAnalysisTests.datafiles import (
PSF,
DCD,
TPR,
XTC,
contacts_villin_folded,
contacts_villin_unfolded,
contacts_file
Expand Down Expand Up @@ -217,7 +219,6 @@ def test_villin_folded(self):
results = soft_cut(f, u, sel, sel)
assert_almost_equal(q.timeseries[:, 1], results[:, 1])


def test_villin_unfolded(self):
# both folded
f = mda.Universe(contacts_villin_folded)
Expand Down Expand Up @@ -302,6 +303,26 @@ def test_non_callable_method(self, universe):
with pytest.raises(ValueError):
self._run_Contacts(universe, method=2, stop=2)

def test_distance_box(self):
u = mda.Universe(TPR, XTC)
sel = "(resname ASP GLU)"
acidic = u.select_atoms(sel)

r = contacts.Contacts(u,
select=(sel, sel),
refgroup=(acidic, acidic), pbc=False)

q = contacts.Contacts(u,
select=(sel, sel),
refgroup=(acidic, acidic),
pbc=True)

r.run()
q.run()
average_contacts_r = np.mean(r.timeseries[:, 1])
average_contacts_q = np.mean(q.timeseries[:, 1])

assert not average_contacts_q == average_contacts_r
Copy link
Contributor

Choose a reason for hiding this comment

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

A test like this isn't ideal as inequality could be due to any number of things going wrong. I'd find a specific value(s) you expect to be returned and check against that - say by measuring the expected distances with and without PBC in another program or directly using distance_array


def test_q1q2():
u = mda.Universe(PSF, DCD)
Expand Down