def makeChemicalObjects(self, template, top_level): self.groups[template.name].locked = True if top_level: if template.attributes.has_key('sequence'): object = ChemicalObjects.ChainMolecule(None) else: object = ChemicalObjects.Molecule(None) else: object = ChemicalObjects.Group(None) object.atoms = [] object.bonds = Bonds.BondList([]) object.groups = [] object.type = self.groups[template.name] object.parent = None child_objects = [] for child in template.children: if isinstance(child, GroupTemplate): group = self.makeChemicalObjects(child, False) object.groups.append(group) object.atoms.extend(group.atoms) object.bonds.extend(group.bonds) group.parent = object child_objects.append(group) else: atom = ChemicalObjects.Atom(child.element) object.atoms.append(atom) atom.parent = object child_objects.append(atom) for name, index in template.names.items(): setattr(object, name, child_objects[index]) child_objects[index].name = name for name, value in template.attributes.items(): path = name.split('.') setattr(self.namePath(object, path[:-1]), path[-1], value) for atom1, atom2 in template.bonds: atom1 = self.namePath(object, atom1) atom2 = self.namePath(object, atom2) object.bonds.append(Bonds.Bond((atom1, atom2))) for name, vector in template.positions.items(): path = name.split('.') self.namePath(object, path).setPosition(vector) return object
def createMolecules(self, names = None, permit_undefined=True): """ :param names: If a list of molecule names (as defined in the chemical database) and/or PDB residue names, only molecules mentioned in this list will be constructed. If a dictionary, it is used to map PDB residue names to molecule names. With the default (None), only water molecules are built. :type names: list :param permit_undefined: If False, an exception is raised when a PDB residue is encountered for which no molecule name is supplied in names. If True, an AtomCluster object is constructed for each unknown molecule. :returns: a collection of :class:~MMTK.ChemicalObjects.Molecule objects, one for each molecule in the PDB file. Each PDB residue not describing an amino acid or nucleotide residue is considered a molecule. :rtype: :class:~MMTK.Collections.Collection """ collection = Collections.Collection() mol_dicts = [molecule_names] if type(names) == type({}): mol_dicts.append(names) names = None for name in self.molecules.keys(): full_name = None for dict in mol_dicts: full_name = dict.get(name, None) if names is None or name in names or full_name in names: if full_name is None and not permit_undefined: raise ValueError("no definition for molecule " + name) for molecule in self.molecules[name]: if full_name: m = ChemicalObjects.Molecule(full_name) setConfiguration(m, [molecule]) else: pdbdict = {} atoms = [] i = 0 for atom in molecule: aname = atom.name while aname[0] in string.digits: aname = aname[1:] + aname[0] try: element = atom['element'].strip() a = ChemicalObjects.Atom(element, name = aname) except KeyError: try: a = ChemicalObjects.Atom(aname[:2].strip(), name = aname) except IOError: a = ChemicalObjects.Atom(aname[:1], name = aname) a.setPosition(atom.position) atoms.append(a) pdbdict[atom.name] = Database.AtomReference(i) i += 1 m = ChemicalObjects.AtomCluster(atoms, name = name) if len(pdbdict) == len(molecule): # pdbmap is correct only if the AtomCluster has # unique atom names m.pdbmap = [(name, pdbdict)] setConfiguration(m, [molecule]) collection.addObject(m) return collection