Skip to content

Commit

Permalink
make Atom target class of ResidueAttr by default (MDAnalysis#2805)
Browse files Browse the repository at this point in the history
* make Atom target class by default

* make molnums apply to segmentgroup
  • Loading branch information
lilyminium authored and PicoCentauri committed Mar 30, 2021
1 parent 59a76e4 commit 435c888
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ The rules for this file:
* 2.0.0

Fixes
* ResidueAttrs now have Atom as a target class by default; ICodes and
Molnums now have default target_classes (#2803, PR #2805)
* Selections on emtpy AtomGroups now return an empty AtomGroup instead of an
error (Issue #2765)
* TOPParser no longer guesses elements when missing atomic number records
Expand Down
20 changes: 5 additions & 15 deletions package/MDAnalysis/core/topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ def phi_selections(residues, c_name='C', n_name='N', ca_name='CA'):

transplants[ResidueGroup].append(('phi_selections', phi_selections))


def psi_selection(residue, c_name='C', n_name='N', ca_name='CA'):
"""Select AtomGroup corresponding to the psi protein backbone dihedral
N-CA-C-N'.
Expand Down Expand Up @@ -891,7 +890,6 @@ def _get_prev_residues_by_resid(residues):
pvres[i] = None
return pvres


transplants[ResidueGroup].append(('_get_prev_residues_by_resid',
_get_prev_residues_by_resid))

Expand Down Expand Up @@ -1081,7 +1079,7 @@ def chi1_selection(residue, n_name='N', ca_name='CA', cb_name='CB',
"""
names = [n_name, ca_name, cb_name, cg_name]
ags = [residue.atoms[residue.atoms.names == n] for n in names]
if any(len(ag)!= 1 for ag in ags):
if any(len(ag) != 1 for ag in ags):
return None
return sum(ags)

Expand Down Expand Up @@ -1827,7 +1825,7 @@ def _gen_initial_values(na, nr, ns):
class ResidueAttr(TopologyAttr):
attrname = 'residueattrs'
singular = 'residueattr'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Residue]
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Atom, Residue]
per_object = 'residue'

def get_atoms(self, ag):
Expand Down Expand Up @@ -1862,7 +1860,6 @@ class Resids(ResidueAttr):
"""Residue ID"""
attrname = 'resids'
singular = 'resid'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Atom, Residue]
dtype = int

@staticmethod
Expand Down Expand Up @@ -1931,7 +1928,6 @@ def set_residues(self, rg, values):
class Resnames(_ResidueStringAttr):
attrname = 'resnames'
singular = 'resname'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Atom, Residue]
transplants = defaultdict(list)
dtype = object

Expand Down Expand Up @@ -2041,7 +2037,6 @@ def sequence(self, **kwargs):
class Resnums(ResidueAttr):
attrname = 'resnums'
singular = 'resnum'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Atom, Residue]
dtype = int

@staticmethod
Expand All @@ -2063,18 +2058,14 @@ class Moltypes(_ResidueStringAttr):
"""
attrname = 'moltypes'
singular = 'moltype'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Atom, Residue]
dtype = object


class Molnums(ResidueAttr):
"""Name of the molecule type
Two molecules that share a molecule type share a common template topology.
"""Index of molecule from 0
"""
attrname = 'molnums'
singular = 'molnum'
target_classes = [AtomGroup, ResidueGroup, Atom, Residue]
dtype = np.intp

# segment attributes
Expand All @@ -2086,7 +2077,8 @@ class SegmentAttr(TopologyAttr):
"""
attrname = 'segmentattrs'
singular = 'segmentattr'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup, Segment]
target_classes = [AtomGroup, ResidueGroup,
SegmentGroup, Atom, Residue, Segment]
per_object = 'segment'

def get_atoms(self, ag):
Expand Down Expand Up @@ -2172,8 +2164,6 @@ def set_segments(self, sg, values):
class Segids(_SegmentStringAttr):
attrname = 'segids'
singular = 'segid'
target_classes = [AtomGroup, ResidueGroup, SegmentGroup,
Atom, Residue, Segment]
transplants = defaultdict(list)
dtype = object

Expand Down
7 changes: 7 additions & 0 deletions testsuite/MDAnalysisTests/core/test_topologyattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ def test_get_atoms(self, attr):
assert_equal(attr.get_atoms(DummyGroup([7, 3, 9])),
self.values[[3, 2, 2]])

def test_get_atom(self, universe):
attr = getattr(universe.atoms[0], self.attrclass.singular)
assert_equal(attr, self.values[0])

def test_get_residues(self, attr):
assert_equal(attr.get_residues(DummyGroup([1, 2, 1, 3])),
self.values[[1, 2, 1, 3]])
Expand Down Expand Up @@ -254,6 +258,9 @@ def test_get_segments(self, attr):
assert_equal(attr.get_segments(DummyGroup([0, 1, 1])),
[self.values[[0, 3]], self.values[[1, 2]], self.values[[1, 2]]])

class TestICodes(TestResidueAttr):
values = np.array(['a', 'b', '', 'd'])
attrclass = tpattrs.ICodes

class TestResnames(TestResidueAttr):
attrclass = tpattrs.Resnames
Expand Down

0 comments on commit 435c888

Please sign in to comment.