Working with TopologyAttributes

Richard Gowers edited this page Dec 29, 2015 · 2 revisions

TopologyAttributes (TA) are used to define static information in the Universe.

Creating a new TopologyAttribute

Should a Topology require a TopologyAttribute that doesn't currently exist, these can be easily made....

The transplant system

All attributes added to the Topology object are automatically added to the namespace of all Groups (AtomGroup, ResidueGroup and SegmentGroup) and components (Atom, Residue & Segment). It is also possible to make a TA supply arbitrary methods and properties to these classes, done via the transplant system.

To do this, the desired method is defined inside the TopologyAttribute. A defaultdict called transplants is defined, and a tuple containing the method's name and the method is appended to the list of methods. Transplants may be defined at either the atom, residue, segment, atomgroup, residuegroup or segmentgroup levels.

class MyAttribute(TopologyAttribute):
    # <normal TA code>
    transplants = defaultdict(list)

    def mass_multiplier(ag, value):
        """Multiply the mass of each item by *value*"""
        return ag.masses * value

    transplants['atomgroup'].append(('multimass', mass_multiplier))


# Creates the following method:
u.atoms.multimass(5)

It is also possible to add in properties as well as methods:

class MyAttribute(TopologyAttribute):
    # <normal TA code>
    transplants = defaultdict(list)

    def mass_doubler(ag):
        """Returns double the mass of each item"""
        return ag.masses * 2

    transplants['atomgroup'].append(('doublemass', property(mass_doubler, None, None, mass_multiplier.__doc__)))


# Creates the following method:
u.atoms.doublemass

Project Information

About
GNU GPL v2 code license
Labels: python, molecular dynamics, analysis, DCD, CHARMM, LAMMPS, NAMD, Gromacs, computer simulation, atoms, coordinates, trajectory, XTC, Library, object-oriented
Core Developers

Essentials

Applications
Downloads
Install
Release Notes
Guide for Developers
Google Summer of Code

Code of Conduct

Links

@mdanalysis on Twitter
Documentation
Tutorials
Downloads (PyPi)
Mailing Lists:
User discussion group
Developer mailing list

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.