def nextFrame(self, id): #Player.nextFrame(self, id) id = int(id) if id == self.currentFrameIndex: return if self.hasCounter and self.gui: self.form.ent2.delete(0,'end') self.form.ent2.insert(0, str(id)) if self.hasSlider: self.form.ifd.entryByName['slider']['widget'].set(id) self.currentFrameIndex = int(id) removeAtoms = AtomSet([]) addAtoms = AtomSet([]) id = int(id) flood = self.floods[id] centers = [] materials = [] radii = [] prev_coords = self.mol.allAtoms.coords lenAtoms = len(prev_coords) #self.residue.atoms = AtomSet([]) index = 0 #h = self.hp.heap() #print h for fl in flood: x = (fl[1] - self.xcent)*self.spacing + self.centerx y = (fl[2] - self.ycent)*self.spacing + self.centery z = (fl[3] - self.zcent)*self.spacing + self.centerz if fl[4] == 7: atomchr = 'P' # note, this will color the NA atom pink (the PDB color for Phosphorus) radius = AAradii[13][0] if fl[4] == 6: atomchr = 'S' radius = AAradii[13][0] if fl[4] == 5: atomchr = 'A' radius = AAradii[10][0] if fl[4] == 4: atomchr = 'O' radius = AAradii[1][0] if fl[4] == 3: atomchr = 'N' radius = AAradii[4][0] if fl[4] == 2: atomchr = 'C' radius = AAradii[10][0] if fl[4] == 1: atomchr = 'H' radius = AAradii[15][0] if not [x,y,z] in prev_coords: a = Atom(atomchr, self.residue, atomchr, top=self.mol) a._coords = [[x,y,z]] a._charges = {} a.hetatm = 1 a.radius = radius #a.number = lenAtoms + 1 addAtoms.append(a) lenAtoms += 1 for key in self.colorKeys: a.colors[key]=AtomElements[atomchr] a.opacities[key]=1.0 else: centers.append([x,y,z]) # a = Atom(atomchr, self.residue, atomchr, top=self.mol) # a._coords = [[x,y,z]] # a._charges = {} # a.hetatm = 1 # a.number = index # index += 1 #aterials.append(AtomElements[atomchr]) #enters.append([x,y,z]) #adii.append(radius) #self.mol.allAtoms = self.residue.atoms #self.mol.geomContainer.geoms['lines'].protected = False #for com in self.autoLigandCommand.vf.cmdsWithOnAddObj: # com.onAddObjectToViewer(self.mol) #self.autoLigandCommand.vf.displayCPK(self.mol, scaleFactor=0.1) halo_centers = [] for coord in prev_coords: if not coord in centers: index = prev_coords.index(coord) removeAtoms.append(self.mol.allAtoms[index]) self.residue.assignUniqIndex() #this is needed to avoid Traceback later on self.mol.allAtoms.stringRepr = None #stringRepr can be very large aousing memory errors event = AddAtomsEvent(objects=addAtoms) #self.autoLigandCommand.vf.dispatchEvent(event) self.autoLigandCommand.vf.displayCPK.updateGeom(event) event = DeleteAtomsEvent(objects=removeAtoms) #self.autoLigandCommand.vf.dispatchEvent(event) self.autoLigandCommand.vf.displayCPK.updateGeom(event) for atom in removeAtoms: self.residue.atoms.remove(atom) if id == self.maxFrame: self.autoLigandCommand.halo.Set(visible=0) else: self.autoLigandCommand.halo.Set(centers=addAtoms.coords, materials=((1,1,0,0.5),), radii=0.4)
def addHydrogens(self, mol): #check for bonds if len(mol.allAtoms.bonds[0]) == 0: mol.buildBondsByDistance() bonds = mol.allAtoms.bonds[0] #could have preset babel_types #so check if allAtoms are already typed try: t = mol.allAtoms.babel_type except: #if all are not pretyped, type them babel = AtomHybridization() babel.assignHybridization(mol.allAtoms) if self.method == 'withBondOrder': mol.rings = RingFinder() mol.rings.findRings2(mol.allAtoms, mol.allAtoms.bonds[0]) mol.rings.bondRings = {} for ind in xrange(len(mol.rings.rings)): r = mol.rings.rings[ind] for b in r['bonds']: if not mol.rings.bondRings.has_key(b): mol.rings.bondRings[b] = [ ind, ] else: mol.rings.bondRings[b].append(ind) bo = BondOrder() bo.assignBondOrder(mol.allAtoms, bonds, mol.rings) mol.allAtoms._bndtyped = 1 # do aromatic here arom = Aromatic(mol.rings) arom.find_aromatic_atoms(mol.allAtoms) hat = AddHydrogens().addHydrogens(mol.allAtoms, method=self.method) bondedAtomDict = {} # key is heavy atom for a in hat: if bondedAtomDict.has_key(a[1]): bondedAtomDict[a[1]].append(a) else: bondedAtomDict[a[1]] = [a] # now create Atom object for hydrogens # and add the to the residues's atom list molNewHs = AtomSet([]) # list of created H atoms for this molecule heavyAtoms = AtomSet([]) # list of atoms that need new radii for heavyAtom, HatmsDscr in bondedAtomDict.items(): #don't add hydrogens to carbons: polar Only!!! if self.htype != 'all' and heavyAtom.element == 'C': continue res = heavyAtom.parent # find where to insert H atom childIndex = res.children.index(heavyAtom) + 1 # loop over H atoms description to be added # start at the end to number correctly l = len(HatmsDscr) for i in range(l - 1, -1, -1): a = HatmsDscr[i] # build H atom's name if len(heavyAtom.name) == 1: name = 'H' + heavyAtom.name else: name = 'H' + heavyAtom.name[1:] # if more than 1 H atom, add H atom index # for instance HD11, HD12, Hd13 (index is 1,2,3) if l > 1: name = name + str(i + 1) # create the H atom object atom = Atom(name, res, top=heavyAtom.top, chemicalElement='H', childIndex=childIndex, assignUniqIndex=0) # set atoms attributes atom._coords = [a[0]] if hasattr(a[1], 'segID'): atom.segID = a[1].segID atom.hetatm = 0 atom.alternate = [] #atom.element = 'H' atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = a[2] atom.babel_type = a[3] atom.babel_organic = 1 atom.radius = 1.2 # create the Bond object bonding Hatom to heavyAtom bond = Bond(a[1], atom, bondOrder=1) # add the created atom the the list molNewHs.append(atom) # in case this new hydrogen atom ever ends up in pmv # HAVE TO CREATE THESE ENTRIES # create the color entries for all geoemtries # available for the heavyAtom for key, value in heavyAtom.colors.items(): atom.colors[key] = (0.0, 1.0, 1.0) atom.opacities[key] = 1.0 mol.allAtoms = mol.chains.residues.atoms if self.renumber: mol.allAtoms.number = range(1, len(mol.allAtoms) + 1) return len(molNewHs)
def nextFrame(self, id): #Player.nextFrame(self, id) id = int(id) if id == self.currentFrameIndex: return if self.hasCounter and self.gui: self.form.ent2.delete(0, 'end') self.form.ent2.insert(0, str(id)) if self.hasSlider: self.form.ifd.entryByName['slider']['widget'].set(id) self.currentFrameIndex = int(id) removeAtoms = AtomSet([]) addAtoms = AtomSet([]) id = int(id) flood = self.floods[id] centers = [] materials = [] radii = [] prev_coords = self.mol.allAtoms.coords lenAtoms = len(prev_coords) #self.residue.atoms = AtomSet([]) index = 0 #h = self.hp.heap() #print h for fl in flood: x = (fl[1] - self.xcent) * self.spacing + self.centerx y = (fl[2] - self.ycent) * self.spacing + self.centery z = (fl[3] - self.zcent) * self.spacing + self.centerz if fl[4] == 7: atomchr = 'P' # note, this will color the NA atom pink (the PDB color for Phosphorus) radius = AAradii[13][0] if fl[4] == 6: atomchr = 'S' radius = AAradii[13][0] if fl[4] == 5: atomchr = 'A' radius = AAradii[10][0] if fl[4] == 4: atomchr = 'O' radius = AAradii[1][0] if fl[4] == 3: atomchr = 'N' radius = AAradii[4][0] if fl[4] == 2: atomchr = 'C' radius = AAradii[10][0] if fl[4] == 1: atomchr = 'H' radius = AAradii[15][0] if not [x, y, z] in prev_coords: a = Atom(atomchr, self.residue, atomchr, top=self.mol) a._coords = [[x, y, z]] a._charges = {} a.hetatm = 1 a.radius = radius #a.number = lenAtoms + 1 addAtoms.append(a) lenAtoms += 1 for key in self.colorKeys: a.colors[key] = AtomElements[atomchr] a.opacities[key] = 1.0 else: centers.append([x, y, z]) # a = Atom(atomchr, self.residue, atomchr, top=self.mol) # a._coords = [[x,y,z]] # a._charges = {} # a.hetatm = 1 # a.number = index # index += 1 #aterials.append(AtomElements[atomchr]) #enters.append([x,y,z]) #adii.append(radius) #self.mol.allAtoms = self.residue.atoms #self.mol.geomContainer.geoms['lines'].protected = False #for com in self.autoLigandCommand.vf.cmdsWithOnAddObj: # com.onAddObjectToViewer(self.mol) #self.autoLigandCommand.vf.displayCPK(self.mol, scaleFactor=0.1) halo_centers = [] for coord in prev_coords: if not coord in centers: index = prev_coords.index(coord) removeAtoms.append(self.mol.allAtoms[index]) self.residue.assignUniqIndex( ) #this is needed to avoid Traceback later on self.mol.allAtoms.stringRepr = None #stringRepr can be very large aousing memory errors event = AddAtomsEvent(objects=addAtoms) #self.autoLigandCommand.vf.dispatchEvent(event) self.autoLigandCommand.vf.displayCPK.updateGeom(event) event = DeleteAtomsEvent(objects=removeAtoms) #self.autoLigandCommand.vf.dispatchEvent(event) self.autoLigandCommand.vf.displayCPK.updateGeom(event) for atom in removeAtoms: self.residue.atoms.remove(atom) if id == self.maxFrame: self.autoLigandCommand.halo.Set(visible=0) else: self.autoLigandCommand.halo.Set(centers=addAtoms.coords, materials=((1, 1, 0, 0.5), ), radii=0.4)
def addHydrogens(self, mol): #check for bonds if len(mol.allAtoms.bonds[0])==0: mol.buildBondsByDistance() bonds = mol.allAtoms.bonds[0] #could have preset babel_types #so check if allAtoms are already typed try: t = mol.allAtoms.babel_type except: #if all are not pretyped, type them babel = AtomHybridization() babel.assignHybridization(mol.allAtoms) if self.method=='withBondOrder': mol.rings = RingFinder() mol.rings.findRings2(mol.allAtoms, mol.allAtoms.bonds[0]) mol.rings.bondRings = {} for ind in xrange(len(mol.rings.rings)): r = mol.rings.rings[ind] for b in r['bonds']: if not mol.rings.bondRings.has_key(b): mol.rings.bondRings[b] = [ind,] else: mol.rings.bondRings[b].append(ind) bo = BondOrder() bo.assignBondOrder(mol.allAtoms, bonds, mol.rings) mol.allAtoms._bndtyped = 1 # do aromatic here arom = Aromatic(mol.rings) arom.find_aromatic_atoms(mol.allAtoms) hat = AddHydrogens().addHydrogens(mol.allAtoms, method=self.method) bondedAtomDict = {} # key is heavy atom for a in hat: if bondedAtomDict.has_key(a[1]): bondedAtomDict[a[1]].append(a) else: bondedAtomDict[a[1]] = [a] # now create Atom object for hydrogens # and add the to the residues's atom list molNewHs = AtomSet([]) # list of created H atoms for this molecule heavyAtoms = AtomSet([]) # list of atoms that need new radii for heavyAtom, HatmsDscr in bondedAtomDict.items(): #don't add hydrogens to carbons: polar Only!!! if self.htype!='all' and heavyAtom.element=='C': continue res = heavyAtom.parent # find where to insert H atom childIndex = res.children.index(heavyAtom)+1 # loop over H atoms description to be added # start at the end to number correctly l = len(HatmsDscr) for i in range(l-1,-1,-1): a = HatmsDscr[i] # build H atom's name if len(heavyAtom.name)==1: name = 'H' + heavyAtom.name else: name = 'H' + heavyAtom.name[1:] # if more than 1 H atom, add H atom index # for instance HD11, HD12, Hd13 (index is 1,2,3) if l > 1: name = name + str(i+1) # create the H atom object atom = Atom(name, res, top=heavyAtom.top, chemicalElement='H', childIndex=childIndex, assignUniqIndex=0) # set atoms attributes atom._coords = [ a[0] ] if hasattr(a[1], 'segID'): atom.segID = a[1].segID atom.hetatm = 0 atom.alternate = [] #atom.element = 'H' atom.occupancy = 1.0 atom.conformation = 0 atom.temperatureFactor = 0.0 atom.babel_atomic_number = a[2] atom.babel_type = a[3] atom.babel_organic = 1 atom.radius = 1.2 # create the Bond object bonding Hatom to heavyAtom bond = Bond( a[1], atom, bondOrder=1) # add the created atom the the list molNewHs.append(atom) # in case this new hydrogen atom ever ends up in pmv # HAVE TO CREATE THESE ENTRIES # create the color entries for all geoemtries # available for the heavyAtom for key, value in heavyAtom.colors.items(): atom.colors[key]=(0.0, 1.0, 1.0) atom.opacities[key]=1.0 mol.allAtoms = mol.chains.residues.atoms if self.renumber: mol.allAtoms.number = range(1, len(mol.allAtoms)+1) return len(molNewHs)
def __init__(self, command, file): master = command.vf.GUI.ROOT self.autoLigandCommand = command.vf.AutoLigandCommand self.autoLigandCommand.spheres.Set(visible=1) self.autoLigandCommand.halo.Set(visible=1) pkl_file = open(file, 'rb') self.floods = [] try: data = pickle.load(pkl_file) except Exception as inst: print("Error loading ", __file__, "\n", inst) self.xcent = data[0] self.ycent = data[1] self.zcent = data[2] self.centerx = data[3] self.centery = data[4] self.centerz = data[5] self.spacing = data[6] self.centers = [] data = pickle.load(pkl_file) self.floods.append(data[1]) try: while data: data = pickle.load(pkl_file) flood = copy.copy(self.floods[-1]) for item in data[0]: flood.remove(item) for item in data[1]: flood.append(item) self.floods.append(flood) except EOFError: pass pkl_file.close() fileName = os.path.splitext(os.path.split(file)[-1])[0] self.mol = Protein(fileName) self.mol.allAtoms = AtomSet([]) chain = Chain() self.residue = Residue(type="UNK") chain.adopt(self.residue, setChildrenTop=1) self.mol.adopt(chain, setChildrenTop=1) self.mol.parser = None self.filename = file fl = self.floods[0][0] x = (fl[1] - self.xcent) * self.spacing + self.centerx y = (fl[2] - self.ycent) * self.spacing + self.centery z = (fl[3] - self.zcent) * self.spacing + self.centerz if fl[4] == 7: atomchr = 'P' # note, this will color the NA atom pink (the PDB color for Phosphorus) radius = AAradii[13][0] if fl[4] == 6: atomchr = 'S' radius = AAradii[13][0] if fl[4] == 5: atomchr = 'A' radius = AAradii[10][0] if fl[4] == 4: atomchr = 'O' radius = AAradii[1][0] if fl[4] == 3: atomchr = 'N' radius = AAradii[4][0] if fl[4] == 2: atomchr = 'C' radius = AAradii[10][0] if fl[4] == 1: atomchr = 'H' radius = AAradii[15][0] a = Atom(atomchr, self.residue, atomchr, top=self.mol) a._coords = [[x, y, z]] a._charges = {} a.hetatm = 1 a.number = 0 a.radius = radius self.mol.allAtoms = self.residue.atoms self.mol = self.autoLigandCommand.vf.addMolecule(self.mol, False) self.mol.levels = [Protein, Chain, Residue, Atom] self.autoLigandCommand.vf.displayCPK(self.mol, scaleFactor=0.4) self.autoLigandCommand.vf.colorByAtomType(self.mol, ['cpk'], log=0) self.autoLigandCommand.vf.displayLines(self.mol, negate=True, displayBO=False, lineWidth=2, log=0, only=False) self.colorKeys = list(a.colors.keys()) maxLen = len(self.floods) - 1 Player.__init__(self, master=master, endFrame=maxLen, maxFrame=maxLen, titleStr="AutoLigand Flood Player", hasSlider=True) try: # withdrew SetAnim button self.form.ifd.entryByName['setanimB']['widget'].grid_forget() self.form.autoSize() except: pass self.nextFrame(0) self.form.root.protocol('WM_DELETE_WINDOW', self.hide_cb)