Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Fixes

Enhancements
* Expanded selection wildcards to the start and middle of strings (Issue #2370)
* Added type checking and conversion to Connection TopologyAttrs (Issue #2373)


09/05/19 IAlibay, richardjgowers
Expand Down
13 changes: 12 additions & 1 deletion package/MDAnalysis/core/topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,14 @@ def _get_named_segment(group, segid):
class _Connection(AtomAttr):
"""Base class for connectivity between atoms"""
def __init__(self, values, types=None, guessed=False, order=None):
self.values = list(values)
values = [tuple(x) for x in values]
if not all(len(x) == self._n_atoms
and all(isinstance(y, (int, np.integer)) for y in x)
for x in values):
raise ValueError(("{} must be an iterable of tuples with {}"
" atom indices").format(self.attrname,
self._n_atoms))
self.values = values
if types is None:
types = [None] * len(values)
self.types = types
Expand Down Expand Up @@ -1743,6 +1750,7 @@ class Bonds(_Connection):
# many bonds, so still asks for "bonds" in the plural
singular = 'bonds'
transplants = defaultdict(list)
_n_atoms = 2

def bonded_atoms(self):
"""An :class:`~MDAnalysis.core.groups.AtomGroup` of all
Expand Down Expand Up @@ -1891,17 +1899,20 @@ class Angles(_Connection):
attrname = 'angles'
singular = 'angles'
transplants = defaultdict(list)
_n_atoms = 3


class Dihedrals(_Connection):
"""A connection between four sequential atoms"""
attrname = 'dihedrals'
singular = 'dihedrals'
transplants = defaultdict(list)
_n_atoms = 4


class Impropers(_Connection):
"""An imaginary dihedral between four atoms"""
attrname = 'impropers'
singular = 'impropers'
transplants = defaultdict(list)
_n_atoms = 4
36 changes: 36 additions & 0 deletions testsuite/MDAnalysisTests/core/test_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,42 @@ def test_add_charges(self, universe, toadd, attrname, default):

assert hasattr(universe.atoms, attrname)
assert getattr(universe.atoms, attrname)[0] == default

@pytest.mark.parametrize(
'attr,values', (
('bonds', [(1, 0), (1, 2)]),
('bonds', [[1, 0], [1, 2]]),
('bonds', set([(1, 0), (1, 2)])),
('angles', [(1, 0, 2), (1, 2, 3), (2, 1, 4)]),
('dihedrals', [[1, 2, 3, 1], (3, 1, 5, 2)]),
('impropers', [[1, 2, 3, 1], (3, 1, 5, 2)]),
)
)
def test_add_connection(self, universe, attr, values):
universe.add_TopologyAttr(attr, values)
assert hasattr(universe, attr)
attrgroup = getattr(universe, attr)
assert len(attrgroup) == len(values)
for x in attrgroup:
ix = x.indices
assert ix[0] <= ix[-1]

@pytest.mark.parametrize(
'attr,values', (
('bonds', [(1, 0, 0), (1, 2)]),
('bonds', [['x', 'y'], [1, 2]]),
('bonds', 'rubbish'),
('bonds', [[1.01, 2.0]]),
('angles', [(1, 0), (1, 2)]),
('angles', 'rubbish'),
('dihedrals', [[1, 1, 1, 0.1]]),
('impropers', [(1, 2, 3)]),
)
)
def add_connection_error(self, universe, attr, values):
with pytest.raises(ValueError):
universe.add_TopologyAttr(attr, values)



class TestAllCoordinatesKwarg(object):
Expand Down