Commit 213c77eb authored by peastman's avatar peastman
Browse files

Merge pull request #548 from peastman/master

Created ForceField.loadFile()
parents 8db70d34 039b53b8
...@@ -100,7 +100,7 @@ class ForceField(object): ...@@ -100,7 +100,7 @@ class ForceField(object):
"""Load one or more XML files and create a ForceField object based on them. """Load one or more XML files and create a ForceField object based on them.
Parameters: Parameters:
- files A list of XML files defining the force field. Each entry may - files (list) A list of XML files defining the force field. Each entry may
be an absolute file path, a path relative to the current working be an absolute file path, a path relative to the current working
directory, a path relative to this module's data subdirectory directory, a path relative to this module's data subdirectory
(for built in force fields), or an open file-like object with a (for built in force fields), or an open file-like object with a
...@@ -113,47 +113,59 @@ class ForceField(object): ...@@ -113,47 +113,59 @@ class ForceField(object):
self._forces = [] self._forces = []
self._scripts = [] self._scripts = []
for file in files: for file in files:
try: self.loadFile(file)
# this handles either filenames or open file-like objects
tree = etree.parse(file) def loadFile(self, file):
except IOError: """Load an XML file and add the definitions from it to this FieldField.
tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', file))
root = tree.getroot() Parameters:
- file (string or file) An XML file containing force field definitions. It may
# Load the atom types. be either an absolute file path, a path relative to the current working
directory, a path relative to this module's data subdirectory
if tree.getroot().find('AtomTypes') is not None: (for built in force fields), or an open file-like object with a
for type in tree.getroot().find('AtomTypes').findall('Type'): read() method from which the forcefield XML data can be loaded.
self.registerAtomType(type.attrib) """
try:
# Load the residue templates. # this handles either filenames or open file-like objects
tree = etree.parse(file)
if tree.getroot().find('Residues') is not None: except IOError:
for residue in root.find('Residues').findall('Residue'): tree = etree.parse(os.path.join(os.path.dirname(__file__), 'data', file))
resName = residue.attrib['name'] root = tree.getroot()
template = ForceField._TemplateData(resName)
for atom in residue.findall('Atom'): # Load the atom types.
template.atoms.append(ForceField._TemplateAtomData(atom.attrib['name'], atom.attrib['type'], self._atomTypes[atom.attrib['type']][2]))
for site in residue.findall('VirtualSite'): if tree.getroot().find('AtomTypes') is not None:
template.virtualSites.append(ForceField._VirtualSiteData(site)) for type in tree.getroot().find('AtomTypes').findall('Type'):
for bond in residue.findall('Bond'): self.registerAtomType(type.attrib)
template.addBond(int(bond.attrib['from']), int(bond.attrib['to']))
for bond in residue.findall('ExternalBond'): # Load the residue templates.
b = int(bond.attrib['from'])
template.externalBonds.append(b) if tree.getroot().find('Residues') is not None:
template.atoms[b].externalBonds += 1 for residue in root.find('Residues').findall('Residue'):
self.registerResidueTemplate(template) resName = residue.attrib['name']
template = ForceField._TemplateData(resName)
# Load force definitions for atom in residue.findall('Atom'):
template.atoms.append(ForceField._TemplateAtomData(atom.attrib['name'], atom.attrib['type'], self._atomTypes[atom.attrib['type']][2]))
for child in root: for site in residue.findall('VirtualSite'):
if child.tag in parsers: template.virtualSites.append(ForceField._VirtualSiteData(site))
parsers[child.tag](child, self) for bond in residue.findall('Bond'):
template.addBond(int(bond.attrib['from']), int(bond.attrib['to']))
# Load scripts for bond in residue.findall('ExternalBond'):
b = int(bond.attrib['from'])
for node in tree.getroot().findall('Script'): template.externalBonds.append(b)
self.registerScript(node.text) template.atoms[b].externalBonds += 1
self.registerResidueTemplate(template)
# Load force definitions
for child in root:
if child.tag in parsers:
parsers[child.tag](child, self)
# Load scripts
for node in tree.getroot().findall('Script'):
self.registerScript(node.text)
def getGenerators(self): def getGenerators(self):
"""Get the list of all registered generators.""" """Get the list of all registered generators."""
......
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