Package Pmv :: Module deleteCommands
[hide private]
[frames] | no frames]

Source Code for Module Pmv.deleteCommands

  1  ############################################################################# 
  2  # 
  3  # Author: Ruth Huey, Michel F. SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  # 
 10  # $Header: /opt/cvs/python/packages/share1.5/Pmv/deleteCommands.py,v 1.42 2006/09/11 18:57:58 sargis Exp $ 
 11  # 
 12  # $Id: deleteCommands.py,v 1.42 2006/09/11 18:57:58 sargis Exp $ 
 13  # 
 14   
 15  """ 
 16  This Module implements commands to delete items from the MoleculeViewer: 
 17  for examples: 
 18      Delete Molecule 
 19  """ 
 20   
 21  import types, Numeric, string 
 22   
 23  from ViewerFramework.VFCommand import CommandGUI 
 24  from ViewerFramework.VF import DeleteAtomsEvent 
 25   
 26  from Pmv.mvCommand import MVCommand 
 27  from Pmv.guiTools import MoleculeChooser 
 28  ##  from ViewerFramework.gui import InputFormDescr 
 29  from mglutil.gui.InputForm.Tk.gui import InputFormDescr 
 30  from mglutil.util.callback import CallBackFunction 
 31   
 32  from MolKit.protein import Coil, Helix, Strand, Turn 
 33  from MolKit.tree import TreeNode, TreeNodeSet 
 34  from MolKit.molecule import MoleculeSet, AtomSet, Atom 
 35  from MolKit.protein import SecondaryStructure 
 36   
 37  import Tkinter 
 38   
 39   
40 -class DeleteMolecule(MVCommand):
41 """Command to delete a molecule from the MoleculeViewer 42 \nPackage : Pmv 43 \nModule : deleteCommands 44 \nClass : DeleteMolecule 45 \nCommand : deleteMolecule 46 \nSynopsis:\n 47 None<---deleteMol(nodes, **kw) 48 \nRequired Arguments:\n 49 nodes --- TreeNodeSet holding the current selection 50 It resets the undo stack automatically.\n 51 """ 52
53 - def onAddCmdToViewer(self):
54 self.vf._deletedLevels = [] 55 if not self.vf.commands.has_key('select'): 56 self.vf.loadCommand("selectionCommands", "select", "Pmv", 57 topCommand=0) 58 if not self.vf.commands.has_key('clearSelection'): 59 self.vf.loadCommand("selectionCommands", "clearSelection", "Pmv", 60 topCommand=0)
61
62 - def onAddObjectToViewer(self, obj):
63 if hasattr(self, 'chooser')\ 64 and self.chooser is not None \ 65 and self.form.root.winfo_exists(): 66 w = self.chooser.form.descr.entryByName['Molecule']['widget'] 67 molParser = obj.parser 68 molStr = molParser.getMoleculeInformation() 69 w.add((obj.name, molStr))
70 71
72 - def deleteMolecule_cb(self):
73 """called each time the 'Delete Molecule' button is pressed""" 74 mols = self.chooser.getMolSet() 75 if mols is not None and len(mols): 76 if self.vf.undoCmdStack == []: 77 self.doitWrapper(mols, redraw=0) 78 79 else: 80 81 text = """WARNING: This command cannot be undone. 82 if you choose to continue the undo list will be reset. 83 The hide Button will make the molecule disappear without 84 resetting the Undo list. Do you want to continue?""" 85 if not hasattr(self, 'idf'): 86 self.idf = InputFormDescr(title="WARNING") 87 self.idf.append({'widgetType':Tkinter.Label, 88 'wcfg':{'text':text}, 89 'gridcfg':{'columnspan':3,'sticky':'w'}}) 90 91 self.idf.append({'name': 'Continue Button', 92 'widgetType':Tkinter.Button, 93 'wcfg':{'text':'CONTINUE', 94 'command':self.continue_cb}, 95 'gridcfg':{'sticky':'we'}}) 96 97 self.idf.append({'name': 'Cancel Button', 98 'widgetType':Tkinter.Button, 99 'wcfg':{'text':'CANCEL', 100 'command':self.cancel_cb}, 101 'gridcfg':{'row':-1,'sticky':'we'}}) 102 103 self.idf.append({'name': 'Hide Button', 104 'widgetType':Tkinter.Button, 105 'wcfg':{'text':'HIDE', 106 'command':self.hide_cb}, 107 'gridcfg':{'row':-1,'sticky':'we'}}) 108 form = self.vf.getUserInput(self.idf, okcancel=0) 109 self.form.root.protocol('WM_DELETE_WINDOW',self.cancel_cb)
110 111
112 - def hide_cb(self):
113 if hasattr(self, 'chooser'): 114 if self.chooser.ipf.form is not None: 115 mols = self.chooser.getMolSet() 116 self.vf.showMolecules(mols.name, negate=1, topCommand=0) 117 self.chooser.done_cb() 118 self.idf.form.destroy()
119 120 121
122 - def continue_cb(self):
123 if hasattr(self, 'chooser'): 124 if self.chooser.ipf.form is not None: 125 mols = self.chooser.getMolSet() 126 self.vf.resetUndo(topCommand=0) 127 self.doitWrapper(mols, redraw=0) 128 self.idf.form.destroy()
129 130
131 - def cancel_cb(self):
132 self.idf.form.destroy()
133
134 - def guiCallback(self):
135 if len(self.vf.Mols) == 0: return 136 self.chooser = MoleculeChooser(self.vf,mode = 'extended', 137 title='Choose Molecules to delete' ) 138 self.chooser.ipf.append({'name':'Delete Button', 139 'widgetType':Tkinter.Button, 140 'text':'Delete Molecule', 141 'wcfg':{'bd':6}, 142 'gridcfg':{'sticky':Tkinter.E+Tkinter.W}, 143 'command': self.deleteMolecule_cb}) 144 self.form = self.chooser.go(modal=0, blocking=0)
145 146
147 - def deleteMol(self, mol):
148 """ Function to delete all the references to each elements of a 149 molecule and then these elements and the molecule to free the 150 memory space.""" 151 152 # Call the removeObject function for all the command having an 153 # onRemoveMol function 154 self.vf.removeObject(mol) 155 156 # Maybe do that in moleculeViewer ??? 157 # also need to clean up selector.selection: 158 nodes = self.vf.getSelection() 159 mol.__class__._numberOfDeletedNodes = 0 160 node = mol 161 while len(node.children): 162 node = node.children 163 164 # Initialize the variable _numberOfDeletedNodes at 0 165 node[0].__class__._numberOfDeletedNodes = 0 166 sslevels = [Coil, Helix, Strand, Turn] 167 # Initialize the variable _numberOfDeletedNodes for each secondary 168 # structure to 0. 169 for sl in sslevels: 170 # Initialize the variable _numberOfDeletedNodes at 0 171 sl._numberOfDeletedNodes = 0 172 173 # but only change selection if there is any 174 if nodes is not None and len(nodes)>0: 175 setClass = nodes.__class__ 176 thisMolNodes = setClass(nodes.get(lambda x, mol=mol: x.top==mol)) 177 #only change selection if this molecule has any nodes in it 178 if len(thisMolNodes)>0: 179 nodes = nodes-thisMolNodes 180 self.vf.clearSelection(topCommand=0) 181 if nodes is not None: 182 self.vf.select(nodes) 183 #self.vf.selector.select(nodes) 184 185 if hasattr(self, 'chooser') and self.form.root.winfo_exists(): 186 # update the listChooser 187 lc = self.chooser.ipf.entryByName['Molecule']['widget'] 188 lc.remove(mol.name) 189 lc.clearComments() 190 191 #check for any possible reference in self.vf.GUI.VIEWER.lastPick 192 if self.vf.hasGui and self.vf.GUI.VIEWER.lastPick: 193 for key in self.vf.GUI.VIEWER.lastPick.hits.keys(): 194 if hasattr(key,'mol'): 195 if mol==key.mol: 196 del self.vf.GUI.VIEWER.lastPick.hits[key] 197 198 # Remove the atoms of the molecule you are deleting from the 199 # the AtomSet self.vf.allAtoms 200 self.vf.allAtoms = self.vf.allAtoms - mol.allAtoms 201 # Delete all the reference to the atoms you want to delete 202 if hasattr(mol.allAtoms, 'bonds'): 203 bnds = mol.allAtoms.bonds[0] 204 for b in bnds: 205 b.__dict__.clear() 206 del(b) 207 mol.allAtoms.__dict__.clear() 208 del mol.allAtoms 209 210 if self.vf.hasGui and hasattr(mol, 'geomContainer'): 211 for g in mol.geomContainer.geoms.values(): 212 if hasattr(g, 'mol'): 213 delattr(g, 'mol') 214 215 mol.geomContainer.geoms.clear() 216 mol.geomContainer.atoms.clear() 217 delattr(mol.geomContainer, 'mol') 218 del mol.geomContainer 219 220 if hasattr(mol, 'atmNum'): 221 mol.atmNum.clear() 222 del mol.atmNum 223 224 if hasattr(mol, 'childByName'): 225 mol.childByName.clear() 226 del mol.childByName 227 228 if hasattr(mol, 'parser') and hasattr(mol.parser, 'mol'): 229 delattr(mol.parser,'mol') 230 del mol.parser 231 232 # delete molecule from Vision, if Vision is running 233 if self.vf.visionAPI: 234 self.vf.visionAPI.remove(mol) 235 236 if len(mol.children): 237 deletedLevels = mol.deleteSubTree() 238 else: 239 deletedLevels = [] 240 # then delete all the refences to the molecule 241 del mol.top 242 # Then delete the molecule 243 deletedLevels.insert(0, mol.__class__) 244 245 mol.__dict__.clear() 246 del mol 247 self.vf._deletedLevels = deletedLevels 248 249 if len(self.vf.Mols) == 0 and hasattr(self, 'chooser') \ 250 and self.form.root.winfo_exists(): 251 self.chooser.done_cb()
252 253
254 - def getFreeMemoryInformation(self):
255 """Store how many TreeNodes have been actually free'ed during the 256 last delete operation in a dictionary""" 257 258 memoryInformation = {} 259 #print 'self.vf._deletedLevels=', self.vf._deletedLevels 260 for d in self.vf._deletedLevels: 261 #print 'checking ', d, ' for deletedNodes' 262 memoryInformation[d.__name__] = d._numberOfDeletedNodes 263 sslevels = [Coil, Helix, Strand, Turn] 264 ## geomslevels = [IndexedPolylines, IndexedPolygons] 265 # Have to loop on the known secondarystructure because our 266 # Data structure doesn't support multiple children and parents. 267 for sl in sslevels: 268 if sl._numberOfDeletedNodes!=0: 269 memoryInformation[sl.__name__] = sl._numberOfDeletedNodes 270 271 ## for sg in geomslevels: 272 ## if sl._numberOfDeletedNodes!=0: 273 ## memoryInformation[sl.__name__] = sl._numberOfDeletedNodes 274 275 return memoryInformation
276 277
278 - def doit(self, nodes):
279 #if called with no selection, just return 280 molecules, nodeSets = self.vf.getNodesByMolecule(nodes) 281 for mol in molecules: self.deleteMol(mol)
282 283
284 - def __call__(self, nodes, **kw):
285 """None <- deleteMol(nodes, **kw) 286 \nnodes: TreeNodeSet holding the current selection. 287 \nIt resets the undo stack automatically. 288 """ 289 if type(nodes) is types.StringType: 290 self.nodeLogString = "'"+nodes+"'" 291 292 self.vf.resetUndo(topCommand=0) 293 apply ( self.doitWrapper, (nodes,), kw )
294 295 296 deleteMoleculeGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 297 'menuButtonName':'Edit', 298 'menuCascadeName':'Delete', 299 'menuEntryLabel':'Molecule', 300 'index':0} 301 302 303 DeleteMoleculeGUI = CommandGUI() 304 DeleteMoleculeGUI.addMenuCommand('menuRoot', 'Edit', 'Delete Molecule', 305 cascadeName = 'Delete') 306 307 308
309 -class DeleteAllMolecules(MVCommand):
310 """Command to delete all molecules from the MoleculeViewer 311 \nPackage : Pmv 312 \nModule : deleteCommands 313 \nClass : DeleteAllMolecules 314 \nCommand : deleteAllMolecules 315 \nSynopsis:\n 316 None<---deleteAllMolecules( **kw) 317 \nRequired Arguments:\n 318 It resets the undo stack automatically.\n 319 """ 320
321 - def onAddCmdToViewer(self):
322 self.vf._deletedLevels = []
323
324 - def guiCallback(self):
325 """called each time the 'Delete All Molecules' button is pressed""" 326 if len(self.vf.Mols) == 0: 327 self.warningMsg("No molecules present in the Viewer") 328 return 329 mols = self.vf.Mols 330 if mols is not None and len(mols): 331 text = """WARNING: This command cannot be undone. 332 if you choose to continue the undo list will be reset. 333 Do you want to continue?""" 334 if not hasattr(self, 'idf'): 335 self.idf = InputFormDescr(title="WARNING") 336 self.idf.append({'widgetType':Tkinter.Label, 337 'wcfg':{'text':text}, 338 'gridcfg':{'columnspan':3,'sticky':'w'}}) 339 340 self.idf.append({'name': 'Continue Button', 341 'widgetType':Tkinter.Button, 342 'wcfg':{'text':'CONTINUE', 343 'command':self.continue_cb}, 344 'gridcfg':{'sticky':'we'}}) 345 346 self.idf.append({'name': 'Cancel Button', 347 'widgetType':Tkinter.Button, 348 'wcfg':{'text':'CANCEL', 349 'command':self.cancel_cb}, 350 'gridcfg':{'row':-1,'sticky':'we'}}) 351 self.vf.getUserInput(self.idf, okcancel=0)
352 353 354 355
356 - def continue_cb(self):
357 self.vf.resetUndo(topCommand=0) 358 self.doitWrapper() 359 if hasattr(self, 'idf') and hasattr(self.idf, 'form'): 360 self.idf.form.destroy()
361 362
363 - def cancel_cb(self):
364 if hasattr(self, 'idf') and hasattr(self.idf, 'form'): 365 self.idf.form.destroy()
366 367
368 - def deleteMol(self, mol):
369 """ Function to delete all the references to each elements of a 370 molecule and then these elements and the molecule to free the 371 memory space.""" 372 373 # Call the removeObject function for all the command having an 374 # onRemoveMol function 375 self.vf.removeObject(mol) 376 377 # Maybe do that in moleculeViewer ??? 378 # also need to clean up selector.selection: 379 nodes = self.vf.getSelection() 380 mol.__class__._numberOfDeletedNodes = 0 381 node = mol 382 while len(node.children): 383 node = node.children 384 385 # Initialize the variable _numberOfDeletedNodes at 0 386 node[0].__class__._numberOfDeletedNodes = 0 387 sslevels = [Coil, Helix, Strand, Turn] 388 # Initialize the variable _numberOfDeletedNodes for each secondary 389 # structure to 0. 390 for sl in sslevels: 391 # Initialize the variable _numberOfDeletedNodes at 0 392 sl._numberOfDeletedNodes = 0 393 394 # but only change selection if there is any 395 if nodes is not None and len(nodes)>0: 396 setClass = nodes.__class__ 397 thisMolNodes = setClass(nodes.get(lambda x, mol=mol: x.top==mol)) 398 #only change selection if this molecule has any nodes in it 399 if len(thisMolNodes)>0: 400 nodes = nodes-thisMolNodes 401 self.vf.clearSelection(topCommand=0) 402 if nodes is not None: 403 self.vf.select(nodes) 404 #self.vf.selector.select(nodes) 405 406 if hasattr(self, 'chooser') and self.form.root.winfo_exists(): 407 # update the listChooser 408 lc = self.chooser.ipf.entryByName['Molecule']['widget'] 409 lc.remove(mol.name) 410 lc.clearComments() 411 412 #check for any possible reference in self.vf.GUI.VIEWER.lastPick 413 if self.vf.hasGui and self.vf.GUI.VIEWER.lastPick: 414 for key in self.vf.GUI.VIEWER.lastPick.hits.keys(): 415 if hasattr(key,'mol'): 416 if mol==key.mol: 417 del self.vf.GUI.VIEWER.lastPick.hits[key] 418 419 # Remove the atoms of the molecule you are deleting from the 420 # the AtomSet self.vf.allAtoms 421 self.vf.allAtoms = self.vf.allAtoms - mol.allAtoms 422 # Delete all the reference to the atoms you want to delete 423 if hasattr(mol.allAtoms, 'bonds'): 424 bnds = mol.allAtoms.bonds[0] 425 for b in bnds: 426 b.__dict__.clear() 427 del(b) 428 mol.allAtoms.__dict__.clear() 429 del mol.allAtoms 430 431 if self.vf.hasGui and hasattr(mol, 'geomContainer'): 432 for g in mol.geomContainer.geoms.values(): 433 if hasattr(g, 'mol'): 434 delattr(g, 'mol') 435 436 mol.geomContainer.geoms.clear() 437 mol.geomContainer.atoms.clear() 438 delattr(mol.geomContainer, 'mol') 439 del mol.geomContainer 440 441 if hasattr(mol, 'atmNum'): 442 mol.atmNum.clear() 443 del mol.atmNum 444 445 if hasattr(mol, 'childByName'): 446 mol.childByName.clear() 447 del mol.childByName 448 449 if hasattr(mol, 'parser') and hasattr(mol.parser, 'mol'): 450 delattr(mol.parser,'mol') 451 del mol.parser 452 453 # delete molecule from Vision, if Vision is running 454 if self.vf.visionAPI: 455 self.vf.visionAPI.remove(mol) 456 457 if len(mol.children): 458 deletedLevels = mol.deleteSubTree() 459 else: 460 deletedLevels = [] 461 # then delete all the refences to the molecule 462 del mol.top 463 # Then delete the molecule 464 deletedLevels.insert(0, mol.__class__) 465 466 mol.__dict__.clear() 467 del mol 468 self.vf._deletedLevels = deletedLevels 469 470 if len(self.vf.Mols) == 0 and hasattr(self, 'chooser') \ 471 and self.form.root.winfo_exists(): 472 self.chooser.done_cb()
473 474
475 - def getFreeMemoryInformation(self):
476 """Store how many TreeNodes have been actually free'ed during the 477 last delete operation in a dictionary""" 478 479 memoryInformation = {} 480 #print 'self.vf._deletedLevels=', self.vf._deletedLevels 481 for d in self.vf._deletedLevels: 482 #print 'checking ', d, ' for deletedNodes' 483 memoryInformation[d.__name__] = d._numberOfDeletedNodes 484 sslevels = [Coil, Helix, Strand, Turn] 485 ## geomslevels = [IndexedPolylines, IndexedPolygons] 486 # Have to loop on the known secondarystructure because our 487 # Data structure doesn't support multiple children and parents. 488 for sl in sslevels: 489 if sl._numberOfDeletedNodes!=0: 490 memoryInformation[sl.__name__] = sl._numberOfDeletedNodes 491 492 ## for sg in geomslevels: 493 ## if sl._numberOfDeletedNodes!=0: 494 ## memoryInformation[sl.__name__] = sl._numberOfDeletedNodes 495 496 return memoryInformation
497 498
499 - def doit(self):
500 for i in range(len(self.vf.Mols)): 501 self.deleteMol(self.vf.Mols[-1]) 502 self.vf.GUI.VIEWER.Redraw()
503 504
505 - def __call__(self, **kw):
506 """ None<---deleteAllMolecules( **kw) 507 \nRemove all molecules in the viewer 508 \nIt resets the undo stack automatically. 509 """ 510 self.vf.resetUndo(topCommand=0) 511 apply ( self.doitWrapper, (), kw )
512 513 514 deleteAllMoleculesGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 515 'menuButtonName':'Edit', 516 'menuCascadeName':'Delete', 517 'menuEntryLabel':'AllMolecules', 518 'index':0} 519 520 521 DeleteAllMoleculesGUI = CommandGUI() 522 DeleteAllMoleculesGUI.addMenuCommand('menuRoot', 'Edit', 'Delete All Molecules', 523 cascadeName = 'Delete') 524 525 526
527 -class DeleteAtomSet(MVCommand):
528 """ Command to remove an AtomSet from the MoleculeViewer 529 \nPackage : Pmv 530 \nModule : deleteCommands 531 \nClass : DeleteAtomSet 532 \nCommand : deleteAtomSet 533 \nSynopsis:\n 534 None<---deleteAtomSet(atoms, **kw)\n 535 \nRequired Arguments:\n 536 atoms --- AtomSet to be deleted.\n 537 """ 538
539 - def __init__(self, func=None):
540 MVCommand.__init__(self, func) 541 self.flag = self.flag | self.objArgOnly
542
543 - def onAddCmdToViewer(self):
544 self.vf._deletedLevels = [] 545 if self.vf.hasGui and not self.vf.commands.has_key('deleteMol'): 546 self.vf.loadCommand("deleteCommands", "deleteMol", "Pmv", 547 topCommand=0) 548 if not self.vf.commands.has_key('select'): 549 self.vf.loadCommand("selectionCommands", "select", "Pmv", 550 topCommand=0) 551 if not self.vf.commands.has_key('clearSelection'): 552 self.vf.loadCommand("selectionCommands", "clearSelection", "Pmv", 553 topCommand=0)
554 555
556 - def guiCallback(self, event=None):
557 """called each time the 'Delete AtomSet' button is pressed""" 558 z = self.vf.getSelection() 559 if z: 560 ats = z.findType(Atom) 561 if self.vf.userpref['expandNodeLogString']['value'] == 0: 562 self.vf.deleteAtomSet.nodeLogString = "self.getSelection()" 563 else: 564 self.vf.warningMsg('no atoms selected') 565 return 'ERROR' 566 if ats: 567 if self.vf.undoCmdStack == []: 568 self.doitWrapper(ats, redraw=0) 569 else: 570 text = """WARNING: This command cannot be undone. 571 if you choose to continue the undo list will be reset. 572 Do you want to continue?""" 573 574 idf = self.idf = InputFormDescr(title="WARNING") 575 idf.append({'widgetType':Tkinter.Label, 576 'wcfg':{'text':text}, 577 'gridcfg':{'columnspan':2,'sticky':'w'}}) 578 579 idf.append({'name': 'continueBut', 580 'widgetType':Tkinter.Button, 581 'wcfg':{'text':'CONTINUE', 582 'command':CallBackFunction(self.continue_cb, 583 ats)}, 584 'gridcfg':{'sticky':'we'}}) 585 586 idf.append({'name': 'cancelBut', 587 'widgetType':Tkinter.Button, 588 'wcfg':{'text':'CANCEL', 'command':self.cancel_cb}, 589 'gridcfg':{'row':-1,'sticky':'we'}}) 590 591 self.form = self.vf.getUserInput(idf, modal = 0, blocking = 0) 592 self.form.root.protocol('WM_DELETE_WINDOW',self.cancel_cb)
593 594 595
596 - def continue_cb(self, ats):
597 self.vf.resetUndo(topCommand=0) 598 self.doitWrapper(ats) 599 #self.doitWrapper(ats, log=0) 600 self.form.destroy()
601 602
603 - def cancel_cb(self):
604 self.idf.form.destroy()
605 606
607 - def __call__(self, atoms, **kw):
608 """None <- deleteAtomSet(atoms, **kw) 609 \natoms: AtomSet to be deleted.""" 610 if type(atoms) is types.StringType: 611 self.nodeLogString = "'"+atoms+"'" 612 ats = self.vf.expandNodes(atoms) 613 if not len(ats): 614 return 'ERROR' 615 ats = ats.findType(Atom) 616 kw['redraw'] = True 617 apply(self.doitWrapper, (ats,), kw)
618 619
620 - def doit(self, ats):
621 """ Function to delete all the references to each atom of a 622 AtomSet.""" 623 624 # Remove the atoms of the molecule you are deleting from the 625 # the AtomSet self.vf.allAtoms 626 self.vf.allAtoms = self.vf.allAtoms - ats 627 628 # If the current selection 629 atmsInSel = self.vf.selection.findType(Atom)[:] 630 atmsInSel.sort() 631 ats.sort() 632 # Call the updateGeoms function for all the command having an 633 # updateGeom function 634 molecules, atomSets = self.vf.getNodesByMolecule(ats) 635 done = 0 636 637 event = DeleteAtomsEvent(objects=ats) 638 self.vf.dispatchEvent(event) 639 640 allAtoms = AtomSet([]) 641 for mol, atSet in map(None, molecules, atomSets): 642 if len(atSet)==len(mol.allAtoms): 643 #have to add atoms back to allAtoms for deleteMol to work 644 self.vf.allAtoms = self.vf.allAtoms + atSet 645 self.vf.deleteMol.deleteMol(mol) 646 #if this is the last atom, quit the loop 647 if mol==molecules[-1]: 648 done=1 649 break 650 continue 651 652 mol.allAtoms = mol.allAtoms - atSet 653 allAtoms = allAtoms + atSet 654 #FIRST remove any possible hbonds 655 hbondAts = atSet.get(lambda x: hasattr(x, 'hbonds')) 656 if hbondAts is not None: 657 #for each atom with hbonds 658 for at in hbondAts: 659 if not hasattr(at, 'hbonds'): 660 continue 661 #remove each of its hbonds 662 for b in at.hbonds: 663 self.removeHBond(b) 664 for at in atSet: 665 for b in at.bonds: 666 at2 = b.atom1 667 if at2 == at: at2 = b.atom2 668 at2.bonds.remove(b) 669 at.parent.remove(at, cleanup=1) 670 671 if len(atmsInSel): 672 if atmsInSel == ats: 673 # the current selection was deleted 674 self.vf.clearSelection(topCommand=0) 675 else: 676 nodes = self.vf.selection 677 lenSel = len(nodes) 678 setClass = nodes.__class__ 679 elementClass = nodes.elementType 680 if lenSel>0: 681 # this breaks if selectionlevel is Molecule, for instance 682 # setClass = nodes.__class__ 683 # newSel = setClass(nodes.findType(Atom) - ats) 684 # newSel2 = setClass([]) 685 newSel = atmsInSel-ats 686 newSel2 = AtomSet([]) 687 # may have ats which have been deleted everywhere else 688 for at in newSel: 689 if at in at.top.allAtoms: 690 newSel2.append(at) 691 if len(newSel2)!=lenSel: 692 self.vf.clearSelection(topCommand=0) 693 if len(newSel2): 694 newSel2 = newSel2.findType(elementClass).uniq() 695 self.vf.select(newSel2, topCommand=0) 696 697 698 #this fixed a bug which occurred when only 1 molecule present 699 #and cmd invoked with mv.deleteAtomSet(mv.Mols[0].allAtoms) 700 if not done: 701 for at in ats: del at 702 self.vf.resetUndo(topCommand=0)
703
704 - def removeHBond(self, b):
705 atList = [b.donAt, b.accAt] 706 if b.hAt is not None: 707 atList.append(b.hAt) 708 for at in atList: 709 #hbonds might already be gone 710 if not hasattr(at, 'hbonds'): 711 continue 712 okhbnds = [] 713 for hb in at.hbonds: 714 if hb!=b: 715 okhbnds.append(hb) 716 if len(okhbnds): 717 at.hbonds = okhbnds 718 else: 719 delattr(at, 'hbonds') 720 return
721 722 723 deleteAtomSetGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 724 'menuButtonName':'Edit', 725 'menuCascadeName':'Delete', 726 'menuEntryLabel':'Delete AtomSet'} 727 728 DeleteAtomSetGUI = CommandGUI() 729 DeleteAtomSetGUI.addMenuCommand('menuRoot', 'Edit', 'Delete AtomSet', 730 cascadeName='Delete') 731 732 733
734 -class DeleteHydrogens(DeleteAtomSet):
735 """ Command to remove hydrogen atoms from the MoleculeViewer 736 \nPackage : Pmv 737 \nModule : deleteCommands 738 \nClass : DeleteHydrogens 739 \nCommand : deleteHydrogens 740 \nSynopsis:\n 741 None<---deleteAtomSet(atoms, **kw)\n 742 \nRequired Arguments:\n 743 atoms --- AtomSet to be deleted.\n 744 """
745 - def guiCallback(self):
746 """called each time the 'Delete AtomSet' button is pressed""" 747 z = self.vf.getSelection() 748 hats = None 749 if z: 750 ats = z.findType(Atom) 751 if ats: 752 hats = ats.get(lambda x: x.element=='H') 753 else: 754 self.vf.warningMsg('no atoms selected') 755 return 'ERROR' 756 if hats is None: 757 self.vf.warningMsg('no hydrogens selected') 758 return 'ERROR' 759 760 #only get to this point if hats 761 if self.vf.undoCmdStack == []: 762 self.doitWrapper(hats, redraw=0) 763 else: 764 text = """WARNING: This command cannot be undone. 765 if you choose to continue the undo list will be reset. 766 Do you want to continue?""" 767 768 idf = self.idf = InputFormDescr(title="WARNING") 769 idf.append({'widgetType':Tkinter.Label, 770 'wcfg':{'text':text}, 771 'gridcfg':{'columnspan':2,'sticky':'w'}}) 772 773 idf.append({'name': 'continueBut', 774 'widgetType':Tkinter.Button, 775 'wcfg':{'text':'CONTINUE', 776 'command':CallBackFunction(self.continue_cb, 777 hats)}, 778 'gridcfg':{'sticky':'we'}}) 779 780 idf.append({'name': 'cancelBut', 781 'widgetType':Tkinter.Button, 782 'wcfg':{'text':'CANCEL', 'command':self.cancel_cb}, 783 'gridcfg':{'row':-1,'sticky':'we'}}) 784 785 self.form = self.vf.getUserInput(idf, modal = 0, blocking = 0) 786 self.form.root.protocol('WM_DELETE_WINDOW',self.cancel_cb)
787 788
789 - def __call__(self, atoms, **kw):
790 """None <- deleteAtomSet(atoms, **kw) 791 \natoms: AtomSet to be deleted.""" 792 if type(atoms) is types.StringType: 793 self.nodeLogString = "'"+atoms+"'" 794 795 ats = self.vf.expandNodes(atoms) 796 if not len(ats): 797 return 'ERROR' 798 hats = ats.get(lambda x: x.element=='H') 799 if not len(hats): 800 return 'ERROR' 801 ats = hats 802 #WHY??? 803 #kw['log'] = 0 804 apply(self.doitWrapper, (ats,), kw)
805 806 807 deleteHydrogensGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 808 'menuButtonName':'Edit', 809 'menuCascadeName':'Delete', 810 'menuEntryLabel':'Delete Hydrogens'} 811 812 DeleteHydrogensGUI = CommandGUI() 813 DeleteHydrogensGUI.addMenuCommand('menuRoot', 'Edit', 'Delete Hydrogens', 814 cascadeName='Delete') 815 816 817 818 commandList = [ 819 {'name':'deleteMol','cmd': DeleteMolecule(),'gui': DeleteMoleculeGUI}, 820 {'name':'deleteAllMolecules','cmd': DeleteAllMolecules(),'gui': DeleteAllMoleculesGUI}, 821 {'name':'deleteAtomSet','cmd': DeleteAtomSet(),'gui': DeleteAtomSetGUI}, 822 {'name':'deleteHydrogens','cmd': DeleteHydrogens(),'gui': DeleteHydrogensGUI} 823 ] 824 825
826 -def initModule(viewer):
827 for dict in commandList: 828 viewer.addCommand(dict['cmd'], dict['name'], dict['gui'])
829