Unverified Commit e03ee784 authored by Evan Pretti's avatar Evan Pretti Committed by GitHub
Browse files

Merge pull request #4956 from epretti/ff-include

Handle relative include paths from force fields in data directories
parents 2b8ad703 58b2a321
...@@ -245,29 +245,24 @@ class ForceField(object): ...@@ -245,29 +245,24 @@ class ForceField(object):
i = 0 i = 0
while i < len(files): while i < len(files):
file = files[i] file = files[i]
tree = None # this handles either filenames or open file-like objects
try: if isinstance(file, str) and not os.path.isfile(file):
# this handles either filenames or open file-like objects
tree = etree.parse(file)
except IOError:
for dataDir in _getDataDirectories(): for dataDir in _getDataDirectories():
f = os.path.join(dataDir, file) f = os.path.join(dataDir, file)
if os.path.isfile(f): if os.path.isfile(f):
tree = etree.parse(f) file = f
break break
try:
tree = etree.parse(file)
except FileNotFoundError:
raise ValueError('Could not locate file "%s"' % file)
except Exception as e: except Exception as e:
# Fail with an error message about which file could not be read. # Fail with an error message about which file could not be read.
# TODO: Also handle case where fallback to 'data' directory encounters problems,
# but this is much less worrisome because we control those files.
msg = str(e) + '\n'
if hasattr(file, 'name'): if hasattr(file, 'name'):
filename = file.name filename = file.name
else: else:
filename = str(file) filename = str(file)
msg += "ForceField.loadFile() encountered an error reading file '%s'\n" % filename raise Exception('ForceField.loadFile() encountered an error reading file "%s": %s' % (filename, e))
raise Exception(msg)
if tree is None:
raise ValueError('Could not locate file "%s"' % file)
trees.append(tree) trees.append(tree)
i += 1 i += 1
......
...@@ -6,6 +6,8 @@ from openmm.unit import * ...@@ -6,6 +6,8 @@ from openmm.unit import *
import openmm.app.element as elem import openmm.app.element as elem
import openmm.app.forcefield as forcefield import openmm.app.forcefield as forcefield
import math import math
import shutil
import tempfile
import textwrap import textwrap
try: try:
from cStringIO import StringIO from cStringIO import StringIO
...@@ -1254,6 +1256,23 @@ class TestForceField(unittest.TestCase): ...@@ -1254,6 +1256,23 @@ class TestForceField(unittest.TestCase):
self.assertTrue('spce-O' in forcefield._atomTypes) self.assertTrue('spce-O' in forcefield._atomTypes)
self.assertTrue('HOH' in forcefield._templates) self.assertTrue('HOH' in forcefield._templates)
def test_IncludesFromDataDirectory(self):
"""Test relative include paths from subdirectories of the data directory."""
oldDataDirs = forcefield._dataDirectories
try:
with tempfile.TemporaryDirectory() as tempDataDir:
forcefield._dataDirectories = forcefield._getDataDirectories() + [tempDataDir]
os.mkdir(os.path.join(tempDataDir, 'subdir'))
for testFileName in ['ff_with_includes.xml', 'test_amber_ff.xml']:
shutil.copyfile(os.path.join('systems', testFileName), os.path.join(tempDataDir, 'subdir', testFileName))
ff = ForceField(os.path.join('subdir', 'ff_with_includes.xml'))
self.assertTrue(len(ff._atomTypes) > 10)
self.assertTrue('spce-O' in ff._atomTypes)
self.assertTrue('HOH' in ff._templates)
finally:
forcefield._dataDirectories = oldDataDirs
def test_ImpropersOrdering(self): def test_ImpropersOrdering(self):
"""Test correctness of the ordering of atom indexes in improper torsions """Test correctness of the ordering of atom indexes in improper torsions
and the torsion.ordering parameter. and the torsion.ordering parameter.
......
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