Commit 8914ab9c authored by Jason Swails's avatar Jason Swails
Browse files

Minor enhancements to Topology

- Add an informative __repr__ to Topology
- Add __len__ to Chain and Residue to quickly tell how many residues or atoms
  are in them, respectively
parent 50442b0a
...@@ -59,6 +59,14 @@ class Topology(object): ...@@ -59,6 +59,14 @@ class Topology(object):
self._bonds = [] self._bonds = []
self._periodicBoxVectors = None self._periodicBoxVectors = None
def __repr__(self):
nchains = len(self._chains)
nres = sum(1 for r in self.residues)
natom = sum(1 for a in self.atoms)
nbond = len(self._bonds)
return '<%s; %d chains, %d residues, %d atoms, %d bonds>' % (
type(self).__name__, nchains, nres, natom, nbond)
def addChain(self, id=None): def addChain(self, id=None):
"""Create a new Chain and add it to the Topology. """Create a new Chain and add it to the Topology.
...@@ -291,6 +299,9 @@ class Chain(object): ...@@ -291,6 +299,9 @@ class Chain(object):
for atom in residue._atoms: for atom in residue._atoms:
yield atom yield atom
def __len__(self):
return len(self._residues)
class Residue(object): class Residue(object):
"""A Residue object represents a residue within a Topology.""" """A Residue object represents a residue within a Topology."""
def __init__(self, name, index, chain, id): def __init__(self, name, index, chain, id):
...@@ -309,6 +320,9 @@ class Residue(object): ...@@ -309,6 +320,9 @@ class Residue(object):
"""Iterate over all Atoms in the Residue.""" """Iterate over all Atoms in the Residue."""
return iter(self._atoms) return iter(self._atoms)
def __len__(self):
return len(self._atoms)
class Atom(object): class Atom(object):
"""An Atom object represents a residue within a Topology.""" """An Atom object represents a residue within a Topology."""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment