Skip to content

Commit

Permalink
fix ensemble merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
corinwagen committed Apr 14, 2020
2 parents ac16c3c + 415a7dd commit eab201b
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 207 deletions.
231 changes: 179 additions & 52 deletions cctk/ensemble.py

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions cctk/gaussian_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
if not all(isinstance(job, JobType) for job in job_types):
raise TypeError(f"invalid job type {job}")

self.molecules = ConformationalEnsemble()
self.ensemble = ConformationalEnsemble()
self.route_card = route_card
self.link0 = link0
self.footer = footer
Expand All @@ -94,7 +94,7 @@ def __init__(
self.success = success

def __str__(self):
return f"GaussianFile (title=\"{str(self.title)}\", {len(self.molecules)} entries in Ensemble)"
return f"GaussianFile (title=\"{str(self.title)}\", {len(self.ensemble)} entries in Ensemble)"

@classmethod
def write_molecule_to_file(cls, filename, molecule, route_card, link0={"mem": "32GB", "nprocshared": 16}, footer=None, title="title", append=False, print_symbol=False):
Expand Down Expand Up @@ -156,7 +156,7 @@ def write_file(self, filename, molecule=None, route_card=None, link0=None, foote
Args:
filename (str): path to the new file
molecule (int): which molecule to use -- passed to ``self.get_molecule()``.
Default is -1 (e.g. the last molecule), but positive integers will select from self.molecules (1-indexed).
Default is -1 (e.g. the last molecule), but positive integers will select from self.ensemble(1-indexed).
A ``Molecule`` object can also be passed, in which case that molecule will be written to the file.
route_card (str): route card for new file
link0 (dict): dictionary of Link 0 commands (e.g. {"mem": "32GB", "nprocshared": 16}
Expand Down Expand Up @@ -318,7 +318,7 @@ def read_file(cls, filename, return_lines=False):
pass

for mol, prop in zip(molecules, properties):
f.molecules.add_molecule(mol, properties=prop)
f.ensemble.add_molecule(mol, properties=prop)

f.check_has_properties()
files.append(f)
Expand Down Expand Up @@ -414,7 +414,7 @@ def _read_gjf_file(cls, filename, return_lines=False):
job_types = cls._assign_job_types(header)

f = GaussianFile(job_types=job_types, route_card=header, link0=link0, footer=footer, title=title)
f.molecules.add_molecule(Molecule(atomic_numbers, geometry, charge=charge, multiplicity=multip))
f.ensemble.add_molecule(Molecule(atomic_numbers, geometry, charge=charge, multiplicity=multip))
if return_lines:
return f, lines
else:
Expand All @@ -424,7 +424,7 @@ def get_molecule(self, num=None):
"""
Returns the last molecule (from an optimization job) or the only molecule (from other jobs).
If ``num`` is specified, returns ``self.molecules[num]``
If ``num`` is specified, returns ``self.ensemble.molecule_list()[num]``
"""
# some methods pass num=None, which overrides setting the default above
if num is None:
Expand All @@ -433,7 +433,7 @@ def get_molecule(self, num=None):
if not isinstance(num, int):
raise TypeError("num must be int")

return self.molecules[num]
return self.ensemble.molecule_list()[num]

@classmethod
def _assign_job_types(cls, header):
Expand All @@ -460,11 +460,11 @@ def check_has_properties(self):
"""
Checks that the file has all the appropriate properties for its job types, and raises ValueError if not.
This only checks the last molecule in ``self.molecules``, for now.
This only checks the last molecule in ``self.ensemble``, for now.
"""
for job_type in self.job_types:
for prop in EXPECTED_PROPERTIES[job_type.value]:
if not self.molecules.has_property(-1, prop):
if not self.ensemble.has_property(-1, prop):
raise ValueError(f"expected property {prop} for job type {job_type}, but it's not there!")

@classmethod
Expand Down
12 changes: 6 additions & 6 deletions cctk/mae_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MAEFile(File):
Attributes:
name (str): name of file
molecules (Ensemble): ``Ensemble`` or ``ConformationalEnsemble`` object
ensemble (Ensemble): ``Ensemble`` or ``ConformationalEnsemble`` object
"""

def __init__(self, name=None):
Expand Down Expand Up @@ -42,12 +42,12 @@ def read_file(cls, filename, name=None, **kwargs):
atomic_numbers = np.array([get_number(z) for z in symbols], dtype=np.int8)

if conformers == True:
file.molecules = ConformationalEnsemble()
file.ensemble = ConformationalEnsemble()
else:
file.molecules = Ensemble()
file.ensemble = Ensemble()

for geom in geometries:
file.molecules.add_molecule(Molecule(atomic_numbers, geom, bonds=bonds.edges))
file.ensemble.add_molecule(Molecule(atomic_numbers, geom, bonds=bonds.edges))

return file, p_names, p_vals

Expand Down Expand Up @@ -268,7 +268,7 @@ def get_molecule(self, num=None):
"""
Returns the last molecule from the ensemble.
If ``num`` is specified, returns ``self.molecules[num]``
If ``num`` is specified, returns ``self.ensemble.molecules[num]``
"""
# some methods pass num=None, which overrides setting the default above
if num is None:
Expand All @@ -277,4 +277,4 @@ def get_molecule(self, num=None):
if not isinstance(num, int):
raise TypeError("num must be int")

return self.molecules[num]
return self.ensemble.molecules[num]
21 changes: 10 additions & 11 deletions cctk/mol2_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MOL2File(File):
Attributes:
name (str): name of file
molecules (Ensemble): ``Ensemble`` or ``ConformationalEnsemble`` object
ensemble (Ensemble): ``Ensemble`` or ``ConformationalEnsemble`` object
"""

def __init__(self, name=None):
Expand All @@ -40,12 +40,12 @@ def read_file(cls, filename, name=None, **kwargs):
atomic_numbers = np.array([get_number(z) for z in symbols], dtype=np.int8)

if conformers == True:
file.molecules = ConformationalEnsemble()
file.ensemble = ConformationalEnsemble()
else:
file.molecules = Ensemble()
file.ensemble = Ensemble()

for geom in geometries:
file.molecules.add_molecule(Molecule(atomic_numbers, geom, bonds=bonds.edges))
file.ensemble.add_molecule(Molecule(atomic_numbers, geom, bonds=bonds.edges))

return file

Expand Down Expand Up @@ -261,7 +261,7 @@ def get_molecule(self, num=None):
"""
Returns the last molecule from the ensemble.
If ``num`` is specified, returns ``self.molecules[num]``
If ``num`` is specified, returns ``self.ensemble.molecules[num]``
"""
# some methods pass num=None, which overrides setting the default above
if num is None:
Expand All @@ -270,7 +270,7 @@ def get_molecule(self, num=None):
if not isinstance(num, int):
raise TypeError("num must be int")

return self.molecules[num]
return self.ensemble.molecules[num]

@classmethod
def write_molecule_to_file(cls, filename, molecule, title=None):
Expand Down Expand Up @@ -298,17 +298,16 @@ def write_molecule_to_file(cls, filename, molecule, title=None):

super().write_file(filename, text)

def write_file(self, filename, molecule=None, **kwargs):
def write_file(self, filename, molecule=-1, **kwargs):
"""
Write a ``.mol2`` file, using object attributes.
Args:
filename (str): path to the new file
molecule (int): which molecule to use -- passed to ``self.get_molecule()``.
Default is -1 (e.g. the last molecule), but positive integers will select from self.molecules (1-indexed).
Default is -1 (e.g. the last molecule), but positive integers will select from self.ensemble.molecules (0-indexed).
A ``Molecule`` object can also be passed, in which case that molecule will be written to the file.
"""
if not isinstance(molecule, Molecule):
molecule = self.get_molecule(molecule)

if molecule is None or isinstance(molecule, (np.integer, int)):
molecule = self.ensemble.molecules[molecule]
self.write_molecule_to_file(filename, molecule, **kwargs)
18 changes: 11 additions & 7 deletions cctk/orca_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class OrcaFile(File):
Attributes:
title (str): the title from the file
molecules (ConformationalEnsemble): `ConformationalEnsemble` instance
ensemble (ConformationalEnsemble): `ConformationalEnsemble` instance
header (str): file header
"""

def __init__(self, molecules, title=None, header=None):
if molecules and isinstance(molecules, ConformationalEnsemble):
self.molecules = molecules
def __init__(self, ensemble, title=None, header=None):
if ensemble and isinstance(ensemble, ConformationalEnsemble):
self.ensemble = ensemble
if title and (isinstance(title, str)):
self.title = title
if header and (isinstance(header, str)):
Expand All @@ -36,12 +36,14 @@ def write_file(self, filename, molecule=None, header=None):
Args:
filename (str): path to the new file
molecule (int): which molecule to use -- passed to ``self.get_molecule()``.
Default is -1 (e.g. the last molecule), but positive integers will select from self.molecules (1-indexed).
Default is -1 (e.g. the last molecule), but positive integers will select from self.ensemble.molecules (0-indexed).
A ``Molecule`` object can also be passed, in which case that molecule will be written to the file.
header (str): header for new file
"""
if molecule is None:
molecule = -1
if not isinstance(molecule, Molecule):
molecule = self.get_molecule(molecule)
molecule = self.ensemble.molecules[molecule]

if header is None:
header = self.header
Expand Down Expand Up @@ -79,6 +81,8 @@ def write_molecule_to_file(cls, filename, molecule, header):

def get_molecule(self, num=None):
"""
TODO: check indexing here
Returns the last molecule (from an optimization job or other multi-molecule jobs) or the only molecule (from other jobs).
If ``num`` is specified, returns that job (1-indexed for positive numbers). So ``job.get_molecule(3)`` will return the 3rd element of ``job.molecules``, not the 4th.
Expand All @@ -94,4 +98,4 @@ def get_molecule(self, num=None):
if num > 0:
num += -1

return self.molecules[num]
return self.ensemble.molecules[num]
2 changes: 1 addition & 1 deletion cctk/pdb_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def write_ensemble_to_trajectory(cls, filename, ensemble):
filename (str): where to write the file
ensemble (Ensemble): ``Ensemble`` object to write
"""
for idx, molecule in enumerate(ensemble.molecules()):
for idx, molecule in enumerate(ensemble.molecules):
if idx == 0:
cls.write_molecule_to_file(filename, molecule, num=idx+1, append=False)
else:
Expand Down
21 changes: 7 additions & 14 deletions test/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class TestAlign(unittest.TestCase):
def test_RMSD(self):
path = "test/static/gaussian_file.out"
gaussian_file = cctk.GaussianFile.read_file(path)
conformational_ensemble = gaussian_file.molecules
m1 = conformational_ensemble[0]
m2 = conformational_ensemble[-1]
ensemble = gaussian_file.ensemble
m1 = ensemble.molecules[0]
m2 = ensemble.molecules[-1]
RMSD = cctk.helper_functions.compute_RMSD(m1,m2)
delta = abs(0.0006419131435567976 - RMSD)
self.assertLess(delta, 0.0001)
Expand All @@ -20,10 +20,10 @@ def test_align(self):
conformational_ensemble = cctk.ConformationalEnsemble()
for filename in sorted(glob.glob(path)):
gaussian_file = cctk.GaussianFile.read_file(filename)
e = gaussian_file.molecules
m = e[-1]
p = e[m]
conformational_ensemble.add_molecule(m,p)
ensemble = gaussian_file.ensemble
molecule = ensemble.molecules[-1]
property_dict = ensemble.get_property_dict(molecule)
conformational_ensemble.add_molecule(molecule,property_dict)

comparison_atoms = [1,2,3,4,5,6]
# added np.int64 here to check that the to_geometry parameter will take any int
Expand All @@ -42,12 +42,5 @@ def test_align(self):
self.assertEqual(len(ensemble3), 1)
cctk.GaussianFile.write_ensemble_to_file("test/static/phenylpropane_aligned3.gjf", ensemble3, "#p")

#for i,molecule in enumerate(aligned_ensemble):
# filename = f"test/static/phenylpropane_{i+1}.gjf"
# route_card = "#"
# cctk.GaussianFile.write_molecule_to_file(filename, molecule, route_card)
# rmsd = cctk.helper_functions.compute_RMSD(m0,molecule,comparison_atoms)
# #print("after:",rmsd)

if __name__ == '__main__':
unittest.main()
16 changes: 8 additions & 8 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def test_read(self):
file = cctk.MOL2File.read_file(path)
self.assertTrue(isinstance(file, cctk.MOL2File))

ensemble = file.molecules
ensemble = file.ensemble
self.assertTrue(isinstance(ensemble, cctk.ConformationalEnsemble))
self.assertEqual(len(ensemble), 1)

mol = ensemble[0]
mol = ensemble.molecules[0]
self.assertTrue(isinstance(mol, cctk.Molecule))
self.assertEqual(len(mol.atomic_numbers), 38)
self.assertEqual(len(mol.geometry), 38)
Expand All @@ -50,10 +50,10 @@ def test_bulk_read(self):
file = cctk.MOL2File.read_file(path)
self.assertTrue(isinstance(file, cctk.MOL2File))

ensemble = file.molecules
ensemble = file.ensemble
self.assertTrue(isinstance(ensemble, cctk.ConformationalEnsemble))
self.assertEqual(len(ensemble), 597)
for mol in ensemble.molecules():
for mol in ensemble.molecules:
self.assertEqual(len(mol.atomic_numbers), 38)
self.assertEqual(len(mol.geometry), 38)
self.assertEqual(mol.get_bond_order(1,2), 1)
Expand Down Expand Up @@ -82,10 +82,10 @@ def test_read(self):
self.assertEqual(len(pnames), 597)
self.assertEqual(len(pvals), 597)

ensemble = file.molecules
ensemble = file.ensemble
self.assertTrue(isinstance(ensemble, cctk.ConformationalEnsemble))
self.assertEqual(len(ensemble), 597)
for mol in ensemble.molecules():
for mol in ensemble.molecules:
self.assertEqual(len(mol.atomic_numbers), 38)
self.assertEqual(len(mol.geometry), 38)
self.assertEqual(mol.get_bond_order(1,2), 1)
Expand All @@ -112,7 +112,7 @@ def test_writefile(self):
ensemble = cctk.ConformationalEnsemble()
ensemble.add_molecule(file.molecule)

orca_file = cctk.OrcaFile(molecules=ensemble, header=header)
orca_file = cctk.OrcaFile(ensemble=ensemble, header=header)
orca_file.write_file(new_path)

with open(path) as old:
Expand All @@ -128,7 +128,7 @@ class TestPDB(unittest.TestCase):
def test_write_traj(self):
path = "test/static/gaussian_file.out"
file = cctk.GaussianFile.read_file(path)
mols = file.molecules
mols = file.ensemble
self.assertTrue(isinstance(mols, cctk.ConformationalEnsemble))

old_path = "test/static/traj.pdb"
Expand Down

0 comments on commit eab201b

Please sign in to comment.