Skip to content

Commit

Permalink
Merge pull request #352 from summeraz/use_parmed
Browse files Browse the repository at this point in the history
Use Parmed loaders by default, remove Mdtraj dependency
  • Loading branch information
ctk3b authored Jun 9, 2017
2 parents 2fbf860 + 48534bb commit 21fa8f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
27 changes: 14 additions & 13 deletions mbuild/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import tempfile
from warnings import warn

import mdtraj as md
import numpy as np
from oset import oset as OrderedSet
import parmed as pmd
Expand All @@ -21,7 +20,6 @@

from mbuild.bond_graph import BondGraph
from mbuild.box import Box
from mbuild.coordinate_transform import translate
from mbuild.exceptions import MBuildError
from mbuild.formats.hoomdxml import write_hoomdxml
from mbuild.formats.lammpsdata import write_lammpsdata
Expand All @@ -32,7 +30,7 @@


def load(filename, relative_to_module=None, compound=None, coords_only=False,
rigid=False, use_parmed=False, **kwargs):
rigid=False, use_mdtraj=False, **kwargs):
"""Load a file into an mbuild compound.
Files are read using the MDTraj package unless the `use_parmed` argument is
Expand All @@ -55,8 +53,8 @@ def load(filename, relative_to_module=None, compound=None, coords_only=False,
Only load the coordinates into an existing compoint.
rigid : bool, optional, default=False
Treat the compound as a rigid body
use_parmed : bool, optional, default=False
Use readers from ParmEd instead of MDTraj.
use_mdtraj : bool, optional, default=False
Use readers from MDTraj instead of Parmed.
**kwargs : keyword arguments
Key word arguments passed to mdTraj for loading.
Expand All @@ -76,12 +74,14 @@ def load(filename, relative_to_module=None, compound=None, coords_only=False,
if compound is None:
compound = Compound()

if use_parmed:
structure = pmd.load_file(filename, structure=True)
compound.from_parmed(structure)
else:
traj = md.load(filename, **kwargs)
if use_mdtraj:
mdtraj = import_('mdtraj')

traj = mdtraj.load(filename, **kwargs)
compound.from_trajectory(traj, frame=-1, coords_only=coords_only)
else:
structure = pmd.load_file(filename, structure=True)
compound.from_parmed(structure, coords_only=coords_only)

if rigid:
compound.label_rigid_bodies()
Expand Down Expand Up @@ -1478,6 +1478,7 @@ def to_trajectory(self, show_ports=False, chains=None,
_to_topology
"""
mdtraj = import_('mdtraj')
atom_list = [particle for particle in self.particles(show_ports)]

top = self._to_topology(atom_list, chains, residues)
Expand All @@ -1496,7 +1497,7 @@ def to_trajectory(self, show_ports=False, chains=None,
else:
unitcell_lengths[dim] = box.lengths[dim]

return md.Trajectory(xyz, top, unitcell_lengths=unitcell_lengths,
return mdtraj.Trajectory(xyz, top, unitcell_lengths=unitcell_lengths,
unitcell_angles=np.array([90, 90, 90]))

def _to_topology(self, atom_list, chains=None, residues=None):
Expand Down Expand Up @@ -1640,7 +1641,7 @@ def from_parmed(self, structure, coords_only=False):
for parmed_atom, particle in atoms_particles:
particle.pos = np.array([parmed_atom.xx,
parmed_atom.xy,
parmed_atom.xz])
parmed_atom.xz]) / 10
return

atom_mapping = dict()
Expand All @@ -1657,7 +1658,7 @@ def from_parmed(self, structure, coords_only=False):
chain_compound = self
for residue in residues:
for atom in residue.atoms:
pos = np.array([atom.xx, atom.xy, atom.xz])
pos = np.array([atom.xx, atom.xy, atom.xz]) / 10
new_atom = Particle(name=str(atom.name), pos=pos)
chain_compound.add(new_atom, label='{0}[$]'.format(atom.name))
atom_mapping[atom] = new_atom
Expand Down
21 changes: 10 additions & 11 deletions mbuild/tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ def test_save_box(self, ch3):
box_attributes = ['mins', 'maxs', 'lengths']
custom_box = mb.Box([.8, .8, .8])
for ext in extensions:
use_mdtraj = False
if ext == '.hoomdxml':
use_mdtraj = True
outfile_padded = 'padded_methyl' + ext
outfile_custom = 'custom_methyl' + ext
ch3.save(filename=outfile_padded, box=None, overwrite=True)
ch3.save(filename=outfile_custom, box=custom_box, overwrite=True)
padded_ch3 = mb.load(outfile_padded)
custom_ch3 = mb.load(outfile_custom)
padded_ch3 = mb.load(outfile_padded, use_mdtraj=use_mdtraj)
custom_ch3 = mb.load(outfile_custom, use_mdtraj=use_mdtraj)
for attr in box_attributes:
pad_attr = getattr(padded_ch3.boundingbox, attr)
custom_attr = getattr(custom_ch3.boundingbox, attr)
Expand Down Expand Up @@ -559,14 +562,10 @@ def test_charge_neutrality_warn(self, benzene):
with pytest.warns(UserWarning):
benzene.save('charge-test.mol2')

with pytest.warns(ValidationWarning):
benzene.save('charge-test.mol2', forcefield_name='oplsaa',
overwrite=True)

with pytest.warns(None) as record_warnings:
with pytest.warns(None) as record_warnings, pytest.warns(ValidationWarning) as validation_warnings:
benzene.save('charge-test.mol2', forcefield_name='oplsaa',
overwrite=True)
assert len(record_warnings) - 1 == 0
assert len(record_warnings) - len(validation_warnings) == 0

@pytest.mark.skipif(not has_openbabel, reason="Open Babel package not installed")
def test_energy_minimization(self, octane):
Expand Down Expand Up @@ -623,7 +622,7 @@ def test_clone_outside_containment(self, ch2, ch3):
with pytest.raises(MBuildError):
ch3_clone = mb.clone(ch3)

def test_load_mol2_parmed(self):
def test_load_mol2_mdtraj(self):
with pytest.raises(KeyError):
mb.load(get_fn('benzene-nonelement.mol2'))
mb.load(get_fn('benzene-nonelement.mol2'), use_parmed=True)
mb.load(get_fn('benzene-nonelement.mol2'), use_mdtraj=True)
mb.load(get_fn('benzene-nonelement.mol2'))
6 changes: 6 additions & 0 deletions mbuild/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def import_(module):
except ImportError:
has_gsd = False

try:
import mdtraj
has_mdtraj = True
except ImportError:
has_mdtraj = False

try:
import openbabel
has_openbabel = True
Expand Down

0 comments on commit 21fa8f0

Please sign in to comment.