Skip to content

Commit

Permalink
simplified wrapping of mdtraj top/traj with added proper exceptions; …
Browse files Browse the repository at this point in the history
…data. .lmp and .lammps all recognized as lammps data files
  • Loading branch information
ctk3b committed Mar 2, 2015
1 parent 90b0632 commit 08b67af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
12 changes: 9 additions & 3 deletions mbuild/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ def __init__(self, topology=None):

def __getattr__(self, attr_name):
"""Redirect attribute access to the wrapped topology. """
return getattr(self.__dict__['_w_topology'], attr_name)
return getattr(self._w_topology, attr_name)

def __setattr__(self, key, value):
try:
self._w_topology.__setattr__(key, value)
except KeyError:
self.__dict__[key] = value

@classmethod
def from_dataframe(cls, atoms, bonds=None):
Expand Down Expand Up @@ -239,8 +245,8 @@ def from_compound(cls, compound, bond_list=None, show_ports=False,
# Add the actual atoms
try:
ele = elem.get_by_symbol(atom.kind)
except:
ele = Element(1000, atom.kind, atom.kind, 1.0)
except KeyError:
ele = Element(1000, atom.kind, atom.kind, 1.0)
at = out.add_atom(atom.kind, ele, last_residue)
at.charge = atom.charge

Expand Down
16 changes: 4 additions & 12 deletions mbuild/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def save(self, filename, **kwargs):
save_xyz(traj=self, filename=filename, **kwargs)
elif filename.endswith(".mol2"):
save_mol2(traj=self, filename=filename, **kwargs)
elif filename.startswith("data.") or filename.startswith(".lmp"):
elif filename.startswith("data.") or filename.endswith((".lmp", ".lammps")):
save_lammps_data(traj=self, filename=filename, **kwargs)
else:
self._w_trajectory.save(filename, **kwargs)
Expand All @@ -257,9 +257,10 @@ def __getattr__(self, attr_name):
return getattr(self._w_trajectory, attr_name)

def __setattr__(self, key, value):
if key in ['unitcell_vectors', 'unitcell_lengths', 'unitcell_angles', 'xyz', 'time']:
try:
self._w_trajectory.__setattr__(key, value)
self.__dict__[key] = value
except KeyError:
self.__dict__[key] = value

def __getitem__(self, key):
"""Get a slice of this trajectory"""
Expand All @@ -277,12 +278,3 @@ def _string_summary_basic(self):
value = "mbuild.Trajectory with %d frames, %d atoms, %d residues, %s" % (
self.n_frames, self.n_atoms, self.n_residues, unitcell_str)
return value


if __name__ == "__main__":
import pdb
from testing.tools import get_fn
traj = Trajectory.load(get_fn('ecer2.hoomdxml'))
comp = traj.to_compound()

pdb.set_trace()

0 comments on commit 08b67af

Please sign in to comment.