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):
self.loadFile(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
----------
......@@ -140,6 +140,18 @@ class ForceField(object):
tree = etree.parse(file)
except IOError:
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()
# Load the atom types.
......
......@@ -293,20 +293,20 @@ class TestForceField(unittest.TestCase):
# Define forcefield parameters used by simpleTemplateGenerator.
# 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.
simple_ffxml_contents = """
simple_ffxml_contents = """\
<ForceField>
<AtomTypes>
<Type name="XXX" class="XXX" element="C" mass="12"/>
</AtomTypes>
<HarmonicBondForce>
<Bond type1="XXX" type2="XXX" length="0.1409" k="392459.2"/>
</HarmonicBondForce>
<HarmonicAngleForce>
<Angle type1="XXX" type2="XXX" type3="XXX" angle="2.09439510239" k="527.184"/>
</HarmonicAngleForce>
<NonbondedForce coulomb14scale="0.833333" lj14scale="0.5">
<Atom type="XXX" charge="0.000" sigma="0.315" epsilon="0.635"/>
</NonbondedForce>
<AtomTypes>
<Type name="XXX" class="XXX" element="C" mass="12"/>
</AtomTypes>
<HarmonicBondForce>
<Bond type1="XXX" type2="XXX" length="0.1409" k="392459.2"/>
</HarmonicBondForce>
<HarmonicAngleForce>
<Angle type1="XXX" type2="XXX" type3="XXX" angle="2.09439510239" k="527.184"/>
</HarmonicAngleForce>
<NonbondedForce coulomb14scale="0.833333" lj14scale="0.5">
<Atom type="XXX" charge="0.000" sigma="0.315" epsilon="0.635"/>
</NonbondedForce>
</ForceField>"""
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