7.7. AtomGroup accessors —
This module provides classes for accessing and converting
objects. It is used for the
method to make it usable in two different ways:
>>> class SpeechWrapper: ... def __init__(self, person): ... self.person = person ... def __call__(self, *args): ... print(self.person.name, "says", *args) ... def whoami(self): ... print("I am %s" % self.person.name) ... >>> class Person: ... def __init__(self, name): ... self.name = name ... say = Accessor("say", SpeechWrapper) ... >>> bob = Person("Bob") >>> bob.say("hello") Bob says hello >>> bob.say.whoami() I am Bob
- class MDAnalysis.core.accessors.Accessor(name, accessor)
Used to pass data between two classes
name (str) – Name of the property in the parent class
accessor (class) – A class that needs access to its parent’s instance
If you want the property to be named “convert_to” in the AtomGroup class, use:
>>> class AtomGroup: >>> # ... >>> convert_to = Accessor("convert_to", ConverterWrapper)
And when calling
ag.convert_to.rdkit(), the “rdkit” method of the ConverterWrapper will be able to have access to “ag”
New in version 2.0.0.
- class MDAnalysis.core.accessors.ConverterWrapper(ag)
AtomGroupto a structure from another Python package.
The converters are accessible to any AtomGroup through the
convert_toproperty. ag.convert_to will return this ConverterWrapper, which can be called directly with the name of the destination package as a string (similarly to the old API), or through custom methods named after the package (in lowercase) that are automatically constructed thanks to metaclass magic.
The code below converts a Universe to a
>>> import MDAnalysis as mda >>> from MDAnalysis.tests.datafiles import GRO >>> u = mda.Universe(GRO) >>> parmed_structure = u.atoms.convert_to('PARMED') >>> parmed_structure <Structure 47681 atoms; 11302 residues; 0 bonds; PBC (triclinic); NOT parametrized>
You can also directly use
package (str) – The name of the package to convert to, e.g.
*args – Positional arguments passed to the converter
**kwargs – Keyword arguments passed to the converter
An instance of the structure type from another package.
- Return type
ValueError: – No converter was found for the required package
New in version 1.0.0.
Changed in version 2.0.0: Moved the
convert_tomethod to its own class. The old API is still available and is now case-insensitive to package names, it also accepts positional and keyword arguments. Each converter function can also be accessed as a method with the name of the package in lowercase, i.e. convert_to.parmed()
ag (AtomGroup) – The AtomGroup to convert