Skip to content

Commit

Permalink
previous commit introduced recursion bug in setattr - need to revisit…
Browse files Browse the repository at this point in the history
… the issue of properly wrapping mdtraj; fixed atom.name usage when atom.atomtype is not specified: No such file or directory; test_hoomdxml now loads into a compound instead of a trajectory; fixed some imports
  • Loading branch information
ctk3b committed Mar 2, 2015
1 parent 08b67af commit 8e0972d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
5 changes: 3 additions & 2 deletions mbuild/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def to_trajectory(self, show_ports=False, **kwargs):
return Trajectory.from_compound(self, show_ports=show_ports, **kwargs)

@classmethod
def load(cls, filename, relative_to_module=None, frame=0):
def load(cls, filename, relative_to_module=None, frame=0, **kwargs):
"""Load a file into a Compound.
Args:
Expand All @@ -216,7 +216,8 @@ def load(cls, filename, relative_to_module=None, frame=0):
"""
from mbuild.trajectory import Trajectory

traj = Trajectory.load(filename, relative_to_module=relative_to_module)
traj = Trajectory.load(filename, relative_to_module=relative_to_module,
**kwargs)
return traj.to_compound(frame=frame)

# Convenience functions
Expand Down
3 changes: 2 additions & 1 deletion mbuild/formats/lammps_data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mdtraj.utils import in_units_of
import operator

from mdtraj.utils import in_units_of


def save_lammps_data(traj, step=-1, filename='data.mbuild', unit_set='real'):
"""Output a Trajectory as a LAMMPS data file.
Expand Down
22 changes: 8 additions & 14 deletions mbuild/tests/test_hoomdxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"""Tests for `mbuild.hoomdxmlfile` module. """

import pytest
import numpy as np

from mbuild.compound import Compound
from mbuild.examples.ethane.ethane import Ethane
from mbuild.testing.tools import get_fn
import numpy as np
from mbuild.trajectory import Trajectory

from base_test import BaseTest


Expand All @@ -18,12 +20,9 @@ def molecule(self):
lj_units = {'mass': 72.0,
'distance': 0.6,
'energy': 0.4}
traj = Trajectory.load(get_fn('ecer2.hoomdxml'), lj_units=lj_units)
return traj
compound = Compound.load(get_fn('ecer2.hoomdxml'), lj_units=lj_units)
return compound

# def test_load_and_create(self):
# methyl = load_mol2('methyl.mol2')
#
def test_write(self, molecule):
molecule.save('ecer2-saved.hoomdxml')

Expand All @@ -46,10 +45,5 @@ def test_units(self, molecule):
[0.0277806278318, -3.6437664032, 0.310265809298],
[0.00889551546425, -3.54814314842, -0.252536147833],
[-0.121015898883, -3.50847649574, -0.732142329216]])
loaded = molecule[0].xyz / 0.6
assert loaded.all() == positions.all()

if __name__=="__main__":
TestHoomdXml().test_write()
TestHoomdXml().test_update_from_file()
TestHoomdXml().test_units()
loaded = molecule.to_trajectory().xyz / 0.6
assert loaded.all() == positions.all()
14 changes: 7 additions & 7 deletions mbuild/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ def __getattr__(self, attr_name):
return getattr(self._w_topology, attr_name)

def __setattr__(self, key, value):
try:
if key in ['chains', 'n_chains', 'residues', 'n_residues', 'atoms',
'n_atoms', 'bonds', 'n_bonds']:
self._w_topology.__setattr__(key, value)
except KeyError:
else:
self.__dict__[key] = value

@classmethod
Expand Down Expand Up @@ -250,11 +251,10 @@ def from_compound(cls, compound, bond_list=None, show_ports=False,
at = out.add_atom(atom.kind, ele, last_residue)
at.charge = atom.charge

if hasattr(atom, 'extras'):
if 'atomtype' in atom.extras:
at.atomtype = atom.atomtype
else:
at.atomtype = atom.kind
try:
at.atomtype = atom.atomtype
except AttributeError:
at.atomtype = atom.kind
#print("Added {} to residue {} in chain {}".format(atom, last_residue, last_chain))
atom_mapping[atom] = at

Expand Down
7 changes: 5 additions & 2 deletions mbuild/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,12 @@ def __getattr__(self, attr_name):
return getattr(self._w_trajectory, attr_name)

def __setattr__(self, key, value):
try:
if key in ['n_frames', 'n_atoms', 'n_residues', 'time', 'timestep',
'topology', 'top', 'xyz', 'unitcell_volumes',
'unitcell_vectors', 'unitcell_lengths', 'unitcell_angles',
'_have_unitcell']:
self._w_trajectory.__setattr__(key, value)
except KeyError:
else:
self.__dict__[key] = value

def __getitem__(self, key):
Expand Down

0 comments on commit 8e0972d

Please sign in to comment.