Commit 7b767ce4 authored by John Chodera (MSKCC)'s avatar John Chodera (MSKCC)
Browse files

Add more informative error message when loading ffxml file fails.

parent 3244fecf
...@@ -124,7 +124,7 @@ class ForceField(object): ...@@ -124,7 +124,7 @@ class ForceField(object):
self.loadFile(file) self.loadFile(file)
def loadFile(self, file): def loadFile(self, file):
"""Load an XML file and add the definitions from it to this FieldField. """Load an XML file and add the definitions from it to this ForceField.
Parameters Parameters
---------- ----------
...@@ -140,6 +140,18 @@ class ForceField(object): ...@@ -140,6 +140,18 @@ class ForceField(object):
tree = etree.parse(file) tree = etree.parse(file)
except IOError: except IOError:
tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', file)) tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', file))
except Exception as e:
# Fail with a more useful 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, 'close'):
filename = file.name
else:
filename = file
msg += 'ForceField.loadFile() encountered an error reading file: %s' % filename
raise Exception(msg)
root = tree.getroot() root = tree.getroot()
# Load the atom types. # Load the atom types.
......
...@@ -293,20 +293,20 @@ class TestForceField(unittest.TestCase): ...@@ -293,20 +293,20 @@ class TestForceField(unittest.TestCase):
# Define forcefield parameters used by simpleTemplateGenerator. # Define forcefield parameters used by simpleTemplateGenerator.
# NOTE: This parameter definition file will currently only work for residues that either have # NOTE: This parameter definition file will currently only work for residues that either have
# no external bonds or external bonds to other residues parameterized by the simpleTemplateGenerator. # no external bonds or external bonds to other residues parameterized by the simpleTemplateGenerator.
simple_ffxml_contents = """ simple_ffxml_contents = """\
<ForceField> <ForceField>
<AtomTypes> <AtomTypes>
<Type name="XXX" class="XXX" element="C" mass="12"/> <Type name="XXX" class="XXX" element="C" mass="12"/>
</AtomTypes> </AtomTypes>
<HarmonicBondForce> <HarmonicBondForce>
<Bond type1="XXX" type2="XXX" length="0.1409" k="392459.2"/> <Bond type1="XXX" type2="XXX" length="0.1409" k="392459.2"/>
</HarmonicBondForce> </HarmonicBondForce>
<HarmonicAngleForce> <HarmonicAngleForce>
<Angle type1="XXX" type2="XXX" type3="XXX" angle="2.09439510239" k="527.184"/> <Angle type1="XXX" type2="XXX" type3="XXX" angle="2.09439510239" k="527.184"/>
</HarmonicAngleForce> </HarmonicAngleForce>
<NonbondedForce coulomb14scale="0.833333" lj14scale="0.5"> <NonbondedForce coulomb14scale="0.833333" lj14scale="0.5">
<Atom type="XXX" charge="0.000" sigma="0.315" epsilon="0.635"/> <Atom type="XXX" charge="0.000" sigma="0.315" epsilon="0.635"/>
</NonbondedForce> </NonbondedForce>
</ForceField>""" </ForceField>"""
simple_ffxml = StringIO(simple_ffxml_contents) simple_ffxml = StringIO(simple_ffxml_contents)
......
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