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

Source Code for Module Pmv.colorCommands

   1   
   2  ############################################################################# 
   3  # 
   4  # Author: Michel F. SANNER, Sophie COON 
   5  # 
   6  # Copyright: M. Sanner TSRI 2000 
   7  # 
   8  ############################################################################# 
   9   
  10  # 
  11  # $Header: /opt/cvs/python/packages/share1.5/Pmv/colorCommands.py,v 1.124 2007/07/12 22:47:42 vareille Exp $ 
  12  # 
  13  # $Id: colorCommands.py,v 1.124 2007/07/12 22:47:42 vareille Exp $ 
  14  # 
  15  """ 
  16  This Module implements commands to color the current selection different ways. 
  17  for example: 
  18      by atoms. 
  19      by residues. 
  20      by chains. 
  21      etc ... 
  22       
  23  """ 
  24  from mglutil.util.colorUtil import ToHEX 
  25  from Pmv.colorPalette import ColorPalette, ColorPaletteFunction 
  26   
  27  import types, string, Tkinter, Numeric, Pmw, os, traceback 
  28  from ViewerFramework.VFCommand import CommandGUI 
  29  from mglutil.gui.InputForm.Tk.gui import InputFormDescr, evalString 
  30  from DejaVu.colorTool import Map, RGBARamp, RedWhiteARamp, WhiteBlueARamp,\ 
  31       RedWhiteBlueARamp 
  32  from mglutil.gui.BasicWidgets.Tk.colorWidgets import ColorChooser 
  33  from mglutil.util.callback import CallBackFunction 
  34  from Pmv.mvCommand import MVCommand, MVAtomICOM 
  35  from MolKit.tree import TreeNode, TreeNodeSet 
  36  from MolKit.molecule import Molecule, Atom, AtomSet 
  37  from MolKit.protein import Protein, Residue, Chain, ProteinSet, ChainSet, ResidueSet 
  38  from mglutil.gui.BasicWidgets.Tk.customizedWidgets import LoadOrSaveText, \ 
  39       FunctionButton, ListChooser 
  40  from mglutil.gui.BasicWidgets.Tk.colorWidgets import ColorChooser 
  41  from DejaVu.colorMap import ColorMap 
  42   
43 -class ColorCommand(MVCommand):
44 """The ColorCommand class is the base class from which all the color commands implemented for PMV will derive. 45 \nPackage : Pmv 46 \nModule : colorCommands 47 \nClass : ColorCommand 48 \nCommand : color 49 \nDescription:\n 50 It implements the general functionalities to color the specified geometries 51 representing the specified nodes with the given list of colors.\n 52 \nSynopsis:\n 53 None <--- color(nodes, colors[(1.,1.,1.),], geomsToColor='all', **kw)\n 54 \nRequired Arguments:\n 55 nodes --- any set of MolKit nodes describing molecular components\n 56 \nOptional Arguments:\n 57 colors --- list of rgb tuple\n 58 geomsToColor --- list of the name of geometries to color default is 'all'\n 59 Keywords --- color\n 60 """ 61
62 - def __init__(self, func=None):
63 MVCommand.__init__(self, func) 64 self.flag = self.flag | self.objArgOnly
65
66 - def onAddCmdToViewer(self):
67 # this is done for sub classes to be able to change the undoCmdsString 68 self.undoCmdsString = self.name
69
70 - def doit(self, nodes, colors, geomsToColor):
71 molecules, atms, nodes = self.getNodes(nodes, returnNodes=True) 72 if atms is None: 73 return 'ERROR' 74 if len(colors)==len(nodes) and not isinstance(nodes[0], Atom): 75 #expand colors from nodes to atoms 76 newcolors = [] 77 for n,c in map(None,nodes,colors): 78 newcolors.extend( [c]*len(n.findType(Atom)) ) 79 colors = newcolors 80 81 for g in geomsToColor: 82 if len(colors)==1 or len(colors)!=len(atms): 83 for a in atms: 84 if not a.colors.has_key(g): continue 85 a.colors[g] = tuple( colors[0] ) 86 else: 87 for a, c in map(None, atms, colors): 88 if not a.colors.has_key(g): continue 89 #a.colors[g] = tuple(c[:3]) 90 a.colors[g] = tuple(c) 91 92 updatedGeomsToColor = [] 93 for mol in molecules: 94 for gName in geomsToColor: 95 if not mol.geomContainer.geoms.has_key(gName): continue 96 geom = mol.geomContainer.geoms[gName] 97 # turn off texturemapping: 98 if geom.texture is not None: 99 geom.texture.Set(enable=0, tagModified=False) 100 updatedGeomsToColor.append(gName) 101 geom.Set(inheritMaterial=0, redo=0, tagModified=False) 102 103 updatedGeomsToColor.append(gName) 104 geom.Set(inheritMaterial=0, redo=0, tagModified=False) 105 106 if geom.children != []: 107 # get geom Name: 108 childrenNames = map(lambda x: x.name, geom.children) 109 updatedGeomsToColor = updatedGeomsToColor + childrenNames 110 for childGeom in geom.children: 111 childGeom.Set(inheritMaterial=0, redo=0, 112 tagModified=False) 113 114 115 mol.geomContainer.updateColors(updatedGeomsToColor)
116 117
118 - def __call__(self, nodes, colors=[(1.,1.,1.),], 119 geomsToColor=('all',), **kw):
120 """None <--- color(nodes, colors=[(1.,1.,1.),], geomsToColor=('all',), **kw) 121 \nnodes---TreeNodeSet holding the current selection 122 \ncolors---list of rgb tuple. 123 \ngeomsToColor---list of the name of geometries to color,default is 'all' 124 """ 125 if not nodes: return 'ERROR' 126 127 if type(nodes) is types.StringType: 128 self.nodeLogString = "'"+nodes+"'" 129 nodes = self.vf.expandNodes(nodes) 130 if not nodes: return 'ERROR' 131 kw['redraw'] = 1 132 if geomsToColor in ['all', '*'] or 'all' in geomsToColor\ 133 or '*' in geomsToColor: 134 geomsToColor = self.getAvailableGeoms(nodes) 135 if not type(geomsToColor) in [types.ListType, types.TupleType]: 136 return 'ERROR' 137 geomsToColor = filter(lambda x: x not in [' ', ''], geomsToColor) 138 if not len(geomsToColor): 139 return 'ERROR' 140 status = apply( self.doitWrapper, (nodes, colors, geomsToColor), kw) 141 return status
142 143
144 - def showForm(self, *args, **kw):
145 # sub class to remove showUndis value, and force rebuilding form 146 # so that list of geoms is up to date 147 if args and args[0] == 'default': 148 kw['force']=1 149 val = MVCommand.showForm( *((self,)+args), **kw) 150 151 try: 152 del val['showUndis'] 153 except: 154 pass 155 return val
156 157
158 - def buildFormDescr(self, formName):
159 if formName == 'default': 160 idf = self.idf = InputFormDescr(title = self.name) 161 geomsAvailable = self.getAvailableGeoms(self.vf.getSelection(), 162 showUndisplay=0) 163 if geomsAvailable is None: return None 164 165 idf.append({'widgetType':Tkinter.Button, 166 'wcfg':{'text':'All Geometries','height':2, 167 'command':self.selectall_cb}, 168 'gridcfg':{'sticky':'we'}}) 169 170 idf.append({'widgetType':Tkinter.Button, 171 'wcfg':{'text':'No Geometries','height':2, 172 'command':self.deselectall_cb}, 173 'gridcfg':{'row':-1,'sticky':'we'}}) 174 175 idf.append({'widgetType':Tkinter.Checkbutton, 176 'name':'showUndis', 177 'defaultValue':0, 178 'wcfg':{'text':'show undisplayed', 179 'command':self.showUndisplayed_cb, 180 'indicatoron':0,'height':2,'pady':5,'padx':5, 181 'variable':Tkinter.IntVar()}, 182 'gridcfg':{'row':-1,'sticky':'we'}}) 183 184 idf.append({'widgetType':Pmw.RadioSelect, 185 'name':'geomsToColor', 186 'listtext':geomsAvailable, 187 'wcfg':{'labelpos':'n', 188 'label_text':'Select the geometry you would like to color:', 189 'orient':'vertical', 190 'buttontype':'checkbutton'}, 191 'gridcfg':{'sticky':'w','columnspan':3}}) 192 return idf 193 194 elif formName == 'chooseColor': 195 idf = InputFormDescr(title = 'Choose Color') 196 197 idf.append({'widgetType':ColorChooser, 198 'name':'colors', 199 'wcfg':{'title':'ColorChooser', 200 'commands':self.color_cb, 201 'immediate':0, 'exitFunction':self.dismiss_cb}, 202 'gridcfg':{'sticky':'wens', 'columnspan':3} 203 }) 204 idf.append({'widgetType':Tkinter.Button, 205 'name':'dismiss', 206 'wcfg':{'text':'DISMISS', 'command':self.dismiss_cb}, 207 'gridcfg':{'sticky':'we', 'columnspan':3}}) 208 209 return idf
210 211
212 - def guiCallback(self):
213 nodes = self.vf.getSelection() 214 if not nodes: 215 self.warningMsg("no nodes selected") 216 return 'ERROR' # to prevent logging 217 218 val = self.showForm('default', scrolledFrame = 1, 219 width= 500, height = 200, force=1) 220 if val: 221 self.geomsToColor = val['geomsToColor'] 222 else: 223 self.geomsToColor = None 224 return 'ERROR' 225 226 form = self.showForm('chooseColor', modal=0, blocking=0)
227 228
229 - def getNodes(self, nodes, returnNodes=False):
230 """Expand nodes argument into a list of atoms and a list of 231 molecules.This function is used to prevent the expansion operation to be done 232 in both doit and setupUndoBefore.The nodes.findType( Atom ) is the operation 233 that is potentially expensive 234 """ 235 if not hasattr(self, 'expandedNodes____Atoms'): 236 nodes = self.vf.expandNodes(nodes) 237 if nodes==self.vf.Mols: 238 self.expandedNodes____Atoms = nodes.allAtoms 239 self.expandedNodes____Molecules = nodes 240 else: 241 self.expandedNodes____Atoms = nodes.findType( Atom ) 242 if len(nodes) == 0: 243 self.expandedNodes____Molecules = ProteinSet() 244 else: 245 self.expandedNodes____Molecules = nodes.top.uniq() 246 247 if returnNodes: 248 if not hasattr(self, 'expandedNodes____Nodes'): 249 nodes = self.vf.expandNodes(nodes) 250 self.expandedNodes____Nodes = nodes 251 return self.expandedNodes____Molecules, \ 252 self.expandedNodes____Atoms, self.expandedNodes____Nodes 253 else: 254 return self.expandedNodes____Molecules,\ 255 self.expandedNodes____Atoms
256 257
258 - def cleanup(self):
259 """ This method is called by the afterDoit method and will be called 260 eventhough the doit failed. 261 """ 262 if hasattr(self, 'expandedNodes____Molecules'): 263 del self.expandedNodes____Molecules 264 if hasattr(self, 'expandedNodes____Atoms'): 265 del self.expandedNodes____Atoms 266 if hasattr(self, 'expandedNodes____Nodes'): 267 del self.expandedNodes____Nodes
268 269
270 - def setupUndoBefore(self, nodes, colors, geomsToColor):
271 if not nodes: return 'ERROR' 272 molecules, atms, nodes = self.getNodes(nodes, returnNodes=True) 273 if atms is None: return 'ERROR' 274 self.undoCmds = '' 275 sameColor = 1 276 277 ## atmsWithGeom = [] 278 ## for g in geomsToColor: 279 ## oldColors = [] 280 ## firstCol = atms[0].colors[g] 281 ## for a in atms: 282 ## try: 283 ## col = a.colors[g] 284 ## except KeyError: 285 ## pass 286 ## if sameColor: 287 ## if col[0]!=firstCol[0] or col[1]!=firstCol[1] or \ 288 ## col[2]!=firstCol[2]: 289 ## sameColor=0 290 ## oldColors.append( col ) 291 292 ## if sameColor: 293 ## oldColors = firstCol 294 295 ## self.addUndoCall( (atmsWithGeom, oldColors, [g]), {'redraw':1}, 296 ## self.undoCmdsString ) 297 298 for g in geomsToColor: 299 # all the atom don't have the entry g in their color dictionary. 300 # Ca-trace, spline and secondary structure create the color 301 # entry for their geometry when computed only 302 atmsWithGeom = atms.get(lambda x, geom=g: x.colors.has_key(geom)) 303 304 if atmsWithGeom is None or not len(atmsWithGeom): 305 continue 306 307 oldColors = [] 308 firstCol = atmsWithGeom[0].colors[g] 309 310 for a in atmsWithGeom: 311 col = a.colors[g] 312 if sameColor: 313 if col[0]!=firstCol[0] or col[1]!=firstCol[1] or \ 314 col[2]!=firstCol[2]: 315 sameColor=0 316 oldColors.append( col ) 317 318 if sameColor: 319 oldColors = oldColors[:1] 320 321 #self.addUndoCall( (atms, oldColors, [g]), {'redraw':1}, 322 # self.undoCmdsString ) 323 self.addUndoCall( (atmsWithGeom, oldColors, [g]), {'redraw':1}, 324 self.undoCmdsString )
325 326
327 - def getChildrenGeomsName(self, mol):
328 geomC = mol.geomContainer 329 # Get the name of the geometries that are child of another one 330 # We only want the parent geometry i.e. 'secondarystructure' 331 # We assume that the geometry name is the same than the key in 332 # the geoms dictionary. 333 childGeomsName = [] 334 for geomName in geomC.geoms.keys(): 335 if geomName in ['master','selectionSpheres']:continue 336 if geomC.geoms[geomName].children != []: 337 names = map(lambda x: x.name, 338 geomC.geoms[geomName].children) 339 childGeomsName = childGeomsName + names 340 return childGeomsName
341 342
343 - def getAvailableGeoms(self, nodes, showUndisplay = 0):
344 """Method to build a dictionary containing all the geometries 345 available in the scene.""" 346 # if no nodes specified no geometries. 347 if not nodes: 348 return 349 molecules, atms = self.getNodes(nodes) 350 geomsAvailable = [] 351 for mol in molecules: 352 #if hasattr(mol,"geomContainer"): 353 geomC = mol.geomContainer 354 childGeomsName = self.getChildrenGeomsName(mol) 355 356 # We only put the one we are ineterested in in the list 357 # of geomsAvailable 358 for geomName in geomC.geoms.keys(): 359 if geomName in ['master','selectionSpheres'] : 360 continue 361 if geomC.atoms.has_key(geomName): 362 if geomName in childGeomsName and geomC.geoms[geomName].children==[]: 363 continue 364 childgnames=[] 365 if geomC.geoms[geomName].children != []: 366 childnames = map(lambda x: x.name,geomC.geoms[geomName].children) 367 childgnames=childgnames+childnames 368 369 for child in childgnames: 370 if geomC.atoms[geomName]==[] and geomC.atoms[child]!=[]: 371 if not geomName in geomsAvailable: 372 geomsAvailable.append(geomName) 373 else: 374 if geomC.atoms[geomName]!=[]: 375 if not geomName in geomsAvailable: 376 geomsAvailable.append(geomName) 377 378 379 return geomsAvailable
380 381
382 - def selectall_cb(self):
383 radioselect = self.idf.entryByName['geomsToColor']['widget'] 384 geoms = radioselect._buttonList 385 selectedgeoms = radioselect.getcurselection() 386 for g in geoms: 387 if not g in selectedgeoms: 388 radioselect.invoke(g)
389 390
391 - def deselectall_cb(self):
392 radioselect = self.idf.entryByName['geomsToColor']['widget'] 393 geoms = radioselect._buttonList 394 selectedgeoms = radioselect.getcurselection() 395 for g in geoms: 396 if g in selectedgeoms: 397 radioselect.invoke(g)
398 399
400 - def showUndisplayed_cb(self):
401 entries = self.idf.entryByName 402 showUndisplay = entries['showUndis']['wcfg']['variable'].get() 403 widget = entries['geomsToColor']['widget'] 404 if showUndisplay == 1: 405 self.addNewEntries(widget,showUndisplay) 406 elif showUndisplay == 0: 407 self.addNewEntries(widget,showUndisplay)
408 409
410 - def addNewEntries(self, widget, showUndisplay):
411 widget.deleteall() 412 nodes = self.vf.getSelection() 413 geomsAvailable = self.getAvailableGeoms(nodes = nodes, 414 showUndisplay=showUndisplay) 415 for g in geomsAvailable: 416 widget.add(g)
417 418
419 - def dismiss_cb(self):
420 if self.cmdForms.has_key('chooseColor'): 421 self.cmdForms['chooseColor'].withdraw()
422
423 - def color_cb(self, colors):
424 self.doitWrapper(self.vf.getSelection(), [colors,], 425 self.geomsToColor, redraw=1)
426 427 428 colorGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 429 'menuButtonName':'Color', 'menuEntryLabel':'Choose Color'} 430 431 ColorGUI = CommandGUI() 432 ColorGUI.addMenuCommand('menuRoot', 'Color', 'Choose Color') 433 434
435 -class ColorFromPalette(ColorCommand, MVAtomICOM):
436 """The ColorFromPalette class is the base class from which all the color commands using a colorPalette implemented for PMV will derive. 437 \nPackage : Pmv 438 \nModule : colorCommands 439 \nClass : ColorFromPalette 440 \nDescription:\n 441 It implements the general functionalities needed to retrieve the colors given a palette and a set of nodes.\n 442 \nSynopsis:\n 443 None <- colorFromPalette(nodes, geomsToColor='all')\n 444 \nRequired Arguments:\n 445 nodes---TreeNodeSet holding the current selection\n 446 geomsToColor---list of the name of geometries to color,default is 'all\n 447 448 """ 449 # THE USER SHOULD BE ABLE TO CREATE A COLORPALETTE AND USE IT TO COLOR THE 450 # SELECTED NODES WITH IT. 451
452 - def __init__(self, func=None):
453 ColorCommand.__init__(self, func) 454 MVAtomICOM.__init__(self) 455 self.flag = self.flag | self.objArgOnly 456 from mglutil.util.defaultPalettes import ChooseColor, \ 457 ChooseColorSortedKeys 458 459 c = 'Color palette' 460 self.palette = ColorPalette('Color palette', ChooseColor, 461 readonly=0, info=c, 462 sortedkeys=ChooseColorSortedKeys )
463
464 - def setupUndoBefore(self, nodes, geomsToColor):
465 # these commands do not require the color argument since colors are 466 # gotten from a palette 467 # we still can use the ColorCommand.setupUndoBefore method by simply 468 # passing None for the color argument 469 ColorCommand.setupUndoBefore(self, nodes, None, geomsToColor)
470 471
472 - def doit(self, nodes, geomsToColor):
473 # these commands do not require the color argument since colors are 474 # gotten from a palette 475 # we still can use the ColorCommand.setupUndoBefore but first we get 476 # the colors. This also insures that the colors are not put inside the 477 # log string for these commands 478 molecules, nodes = self.getNodes(nodes) 479 if nodes is None: 480 return 'ERROR' 481 colors = self.getColors(nodes) 482 ColorCommand.doit(self, nodes, colors, geomsToColor)
483 484
485 - def onAddCmdToViewer(self):
486 # these commands use a color command to undo their effect 487 # so we make sure it is loaded and we place its name into 488 # undoCmdsString 489 if not self.vf.commands.has_key('color'): 490 self.vf.loadCommand('colorCommands', 'color', 'Pmv', 491 topCommand=0) 492 self.undoCmdsString= self.vf.color.name
493 494
495 - def getColors(self, nodes):
496 return self.palette.lookup( nodes )
497 498
499 - def guiCallback(self):
500 nodes = self.vf.getSelection() 501 if not nodes: 502 return 'Error' # to prevent logging 503 val = self.showForm('default', scrolledFrame = 1, 504 width= 500, height = 200, force=1) 505 if val: 506 geomsToColor = val['geomsToColor'] 507 else: 508 geomsToColor = None 509 ## self.warningMsg("ERROR: No geometry to color") 510 return 511 512 self.doitWrapper(nodes, geomsToColor, redraw=1)
513 514
515 - def __call__(self, nodes, geomsToColor='all', **kw):
516 """None <- colorFromPalette(nodes, geomsToColor='all', **kw) 517 \nnodes --- TreeNodeSet holding the current selection 518 \ngeomsToColor --- list of the name of geometries to color, 519 default is 'all'""" 520 if type(nodes) is types.StringType: 521 self.nodeLogString = "'"+nodes+"'" 522 523 nodes = self.vf.expandNodes(nodes) 524 if not nodes: 525 return 'ERROR' 526 geomsToColor = filter(lambda x: x not in [' ', ''], geomsToColor) 527 if not len(geomsToColor): 528 return 'ERROR' 529 if geomsToColor in ['all', '*'] or 'all' in geomsToColor\ 530 or '*' in geomsToColor: 531 geomsToColor = self.getAvailableGeoms(nodes) 532 533 if not len(geomsToColor): 534 self.cleanup() 535 return 'ERROR' 536 537 if not kw.has_key('redraw'): kw['redraw'] = 1 538 status = apply( self.doitWrapper, (nodes, geomsToColor), kw) 539 return status
540 541
542 -class ColorByAtomType(ColorFromPalette):
543 """The colorByAtomType command allows the user to color the given geometry representing the given nodes using the atomtype coloring scheme where:N :Blue ; C : White ; O : Red ; S : Yellow ; H : Cyan; P: Magenta;UNK:green. 544 \nPackage : Pmv 545 \nModule : colorCommands 546 \nClass : ColorByAtomType 547 \nCommand : colorbyAtomType 548 \nDescription:\n 549 This coloring scheme gives some information on the atomic composition of 550 the given nodes.\n 551 \nSynopsis:\n 552 None <- colorByAtomType(nodes, geomsToColor='all', **kw)\n 553 nodes : any set of MolKit nodes describing molecular components\n 554 geomsToColor: list of the name of geometries to color default is 'all'\n 555 Keywords: color, atom type\n 556 """
557 - def __init__(self, func=None):
558 ColorFromPalette.__init__(self, func=func) 559 from Pmv.pmvPalettes import AtomElements 560 c = 'Color palette for atom type' 561 self.palette = ColorPalette('AtomElements', colorDict=AtomElements, 562 readonly=0, info=c, 563 lookupMember='element')
564 565 colorByAtomTypeGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 566 'menuButtonName':'Color', 567 'menuEntryLabel':'by Atom Type'} 568 569 ColorByAtomTypeGUI = CommandGUI() 570 ColorByAtomTypeGUI.addMenuCommand('menuRoot', 'Color', 'by Atom Type') 571 572 573
574 -class ColorByDG(ColorFromPalette):
575 """The colorByDG command allows the user to color the given geometries representing the given nodes using the following David Goodsell coloring 576 scheme. 577 \nPackage : Pmv 578 \nModule : colorCommands 579 \nClass : ColorByDG 580 \nCommand : colorByDG 581 \nSynopsis:\n 582 None <--- colorByDG(nodes, geomsToColor='all', **kw)\n 583 \nArguments:\n 584 nodes --- any set of MolKit nodes describing molecular components\n 585 geomsToColor --- list of the name of geometries to color default is 'all'\n 586 Keywords --- color, David Goodsell coloring scheme\n 587 """ 588
589 - def __init__(self, func=None):
590 ColorFromPalette.__init__(self, func=func) 591 from Pmv.pmvPalettes import DavidGoodsell, DavidGoodsellSortedKeys 592 c = 'Color palette for coloring using David Goodsells colors' 593 self.palette = ColorPaletteFunction('DavidGoodsell', 594 DavidGoodsell, readonly=0, 595 info=c, 596 sortedkeys=DavidGoodsellSortedKeys, 597 lookupFunction=self.lookupFunc) 598 599 self.DGatomIds=['ASPOD1','ASPOD2','GLUOE1','GLUOE2', 'SERHG', 600 'THRHG1','TYROH','TYRHH', 601 'LYSNZ','LYSHZ1','LYSHZ2','LYSHZ3','ARGNE','ARGNH1','ARGNH2', 602 'ARGHH11','ARGHH12','ARGHH21','ARGHH22','ARGHE','GLNHE21', 603 'GLNHE22','GLNHE2', 604 'ASNHD2','ASNHD21', 'ASNHD22','HISHD1','HISHE2' , 605 'CYSHG', 'HN']
606
607 - def lookupFunc(self, atom):
608 assert isinstance(atom, Atom) 609 if atom.name in ['HN']: 610 atom.atomId = atom.name 611 else: 612 atom.atomId=atom.parent.type+atom.name 613 if atom.atomId not in self.DGatomIds: 614 atom.atomId=atom.element 615 return atom.atomId
616 617 618 colorByDGGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 619 'menuButtonName':'Color', 'menuEntryLabel':'by DG colors'} 620 621 ColorByDGGUI= CommandGUI() 622 ColorByDGGUI.addMenuCommand('menuRoot', 'Color', 'by DG colors') 623 624 625
626 -class ColorByResidueType(ColorFromPalette):
627 """The colorByResidueType command allows the user to color the given geometries representing the given nodes using the Rasmol coloring scheme. 628 \nPackage : Pmv 629 \nModule : colorCommands 630 \nClass : ColorByResidueType 631 \nCommand : colorByResidueType 632 \nwhere:\n 633 ASP, GLU bright red CYS, MET yellow\n 634 LYS, ARG blue SER, THR orange\n 635 PHE, TYR mid blue ASN, GLN cyan\n 636 GLY light grey LEU, VAL, ILE green\n 637 ALA dark grey TRP pink\n 638 HIS pale blue PRO flesh\n 639 \nSynopsis:\n 640 None <- colorByResidueType(nodes, geomsToColor='all', **kw)\n 641 nodes --- any set of MolKit nodes describing molecular components.\n 642 geomsToColor --- list of the name of geometries to color default is 'all'\n 643 Keywords --- color, Rasmol, residue type\n 644 """ 645
646 - def __init__(self, func=None):
647 ColorFromPalette.__init__(self, func=func) 648 from Pmv.pmvPalettes import RasmolAmino, RasmolAminoSortedKeys 649 c = 'Color palette for Rasmol like residues types' 650 self.palette = ColorPalette('RasmolAmino', RasmolAmino, readonly=0, 651 info=c, 652 sortedkeys = RasmolAminoSortedKeys, 653 lookupMember='type')
654 655
656 - def getColors(self, nodes):
657 return self.palette.lookup( nodes.findType(Residue) )
658 659 colorByResdueTypeGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 660 'menuButtonName':'Color', 661 'menuEntryLabel':'RasmolAmino', 662 'menuCascadeName':'by Residue Type'} 663 664 ColorByResidueTypeGUI = CommandGUI() 665 ColorByResidueTypeGUI.addMenuCommand('menuRoot', 'Color', 'RasmolAmino', 666 cascadeName = 'by Residue Type') 667 668 669
670 -class ColorShapely(ColorFromPalette):
671 """The colorByShapely command allows the user to color the given geometries representing the given nodes using the Shapely coloring scheme where each residue has a different color. (For more information please refer to the pmv tutorial). 672 \nPackage : Pmv 673 \nModule : colorCommands 674 \nClass : ColorByResidueType 675 \nCommand : colorByResidueType 676 \nSynopsis:\n 677 None <- colorByShapely(nodes, geomsToColor='all', **kw)\n 678 nodes --- any set of MolKit nodes describing molecular components\n 679 geomsToColor --- list of the name of geometries to color default is 'all'\n 680 Keywords --- color, shapely, residue type\n 681 """
682 - def __init__(self, func=None):
683 ColorFromPalette.__init__(self, func=func) 684 from Pmv.pmvPalettes import Shapely 685 c = 'Color palette for shapely residues types' 686 self.palette = ColorPalette('Shapely', Shapely, readonly=0, info=c, 687 lookupMember='type')
688 689
690 - def getColors(self, nodes):
691 if not nodes: return "ERROR" 692 return self.palette.lookup( nodes.findType(Residue) )
693 694 colorShapelyGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 695 'menuButtonName':'Color', 'menuEntryLabel':'Shapely', 696 'menuCascadeName':'by Residue Type'} 697 698 ColorShapelyGUI = CommandGUI() 699 ColorShapelyGUI.addMenuCommand('menuRoot', 'Color', 'Shapely', 700 cascadeName = 'by Residue Type') 701 702 ## 703 ## FIXME ramp should be a colorMap object with editor and stuff 704 ## it should be possible to pass it as an argument. if none is given a default 705 ## a colorMap object with a default RGBRamp would be used 706 ## This is also true for Palettes. 707 ## 708 from DejaVu.colorTool import Map 709
710 -class ColorFromRamp(ColorFromPalette):
711 """The colorFromRamp class implements the functionality to color the given geometries representing the given nodes using a colorMap created from the Ramp. 712 \nPackage : Pmv 713 \nModule : colorCommands 714 \nClass : ColorFromRamp 715 \nSynopsis:\n 716 None <- colorFromRamp(nodes, geomsToColor='all', **kw)\n 717 nodes --- any set of MolKit nodes describing molecular components\n 718 geomsToColor --- list of the name of geometries to color default is 'all'\n 719 Keywords --- color, ramp\n 720 """ 721
722 - def __init__(self):
723 ColorFromPalette.__init__(self) 724 self.flag = self.flag | self.objArgOnly 725 from DejaVu.colorTool import RGBRamp, Map 726 self.ramp = RGBRamp()
727
728 -class ColorByChain(ColorFromPalette):
729 """The colorByChain command allows the user to color the given geometries representing the given nodes by chain. A different color is assigned to each chain. 730 \nPackage : Pmv 731 \nModule : colorCommands 732 \nClass : ColorByChain 733 \nCommand : colorByChain 734 \nSynopsis:\n 735 None <- colorByChain(nodes, geomsToColor='all', **kw)\n 736 nodes --- any set of MolKit nodes describing molecular components\n 737 geomsToColor --- list of the name of geometries to color default is 'all'\n 738 Keywords --- color, chain\n 739 """ 740
741 - def __init__(self, func=None):
742 ColorFromPalette.__init__(self, func=func) 743 from mglutil.util.defaultPalettes import Rainbow, RainbowSortedKey 744 c = 'Color palette chain number' 745 746 self.palette = ColorPaletteFunction('Rainbow', Rainbow, readonly=0, 747 info=c, 748 lookupFunction = lambda x, 749 length = len(RainbowSortedKey):\ 750 x.number%length, 751 sortedkeys = RainbowSortedKey)
752
753 - def onAddObjectToViewer(self, obj):
754 for c in obj.chains: 755 c.number = self.vf.Mols.chains.index(c) 756 if not self.vf.commands.has_key('color'): 757 self.vf.loadCommand('colorCommands', 'color', 'Pmv', 758 topCommand=0) 759 self.undoCmdsString= self.vf.color.name 760 self.cleanup()
761
762 - def getColors(self, nodes):
763 colors = self.palette.lookup(nodes.findType(Chain)) 764 return colors
765 766 colorByChainGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 767 'menuButtonName':'Color', 'menuEntryLabel':'by Chain'} 768 769 ColorByChainGUI = CommandGUI() 770 ColorByChainGUI.addMenuCommand('menuRoot', 'Color', 'by Chain') 771
772 -class ColorByMolecule(ColorFromPalette):
773 """The colorByChain command allows the user to color the given geometries representing the given nodes by molecules. A different color is assigned to each molecule. 774 \nPackage : Pmv 775 \nModule : colorCommands 776 \nClass : ColorByChain 777 \nCommand : colorByChain 778 \nSynopsis:\n 779 None <- colorByMolecule(nodes, geomsToColor='all', **kw)\n 780 nodes --- any set of MolKit nodes describing molecular components\n 781 geomsToColor --- list of the name of geometries to color default is 'all'\n 782 Keywords --- color, chain\n 783 """
784 - def __init__(self, func=None):
785 ColorFromPalette.__init__(self, func=func) 786 from mglutil.util.defaultPalettes import Rainbow, RainbowSortedKey 787 c = 'Color palette molecule number' 788 self.palette = ColorPaletteFunction( 789 'Rainbow', Rainbow, readonly=0, info=c, 790 lookupFunction = lambda x, length=len(RainbowSortedKey): \ 791 x.number%length, sortedkeys=RainbowSortedKey)
792
793 - def onAddObjectToViewer(self, obj):
794 obj.number = self.vf.Mols.index(obj) 795 if not self.vf.commands.has_key('color'): 796 self.vf.loadCommand('colorCommands', 'color', 'Pmv', 797 topCommand=0) 798 self.undoCmdsString= self.vf.color.name 799 self.cleanup()
800
801 - def getColors(self, nodes):
802 if not nodes: return 803 colors = self.palette.lookup(nodes.top) 804 return colors
805 806 colorByMoleculeGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 807 'menuButtonName':'Color', 808 'menuEntryLabel':'by Molecules'} 809 810 ColorByMoleculeGUI = CommandGUI() 811 ColorByMoleculeGUI.addMenuCommand('menuRoot', 'Color', 'by Molecules') 812 813
814 -class ColorByInstance(ColorFromPalette):
815 """Command to color the current selection by instance using a Rainbow palette. 816 \nPackage : Pmv 817 \nModule : colorCommands 818 \nClass : ColorByInstance 819 \nCommand : colorByInstance 820 \nSynopsis:\n 821 None <- colorByInstance(nodes, geomsToColor='all', **kw)\n 822 nodes --- any set of MolKit nodes describing molecular components\n 823 geomsToColor --- list of the name of geometries to color default is 'all'\n 824 """
825 - def __init__(self, func=None):
826 ColorFromPalette.__init__(self, func=func) 827 from mglutil.util.defaultPalettes import Rainbow, RainbowSortedKey 828 c = 'Color palette molecule number' 829 self.palette = ColorPaletteFunction( 830 'Rainbow', Rainbow, readonly=0, info=c, 831 lookupFunction = lambda x, length=len(RainbowSortedKey): \ 832 x%length, sortedkeys=RainbowSortedKey)
833 834
835 - def onAddObjectToViewer(self, obj):
836 obj.number = self.vf.Mols.index(obj) 837 if not self.vf.commands.has_key('color'): 838 self.vf.loadCommand('colorCommands', 'color', 'Pmv', 839 topCommand=0) 840 self.undoCmdsString= self.vf.color.name 841 self.cleanup()
842 843
844 - def doit(self, nodes, geomsToColor):
845 molecules, nodes = self.getNodes(nodes) 846 if nodes is None : 847 return 'ERROR' 848 for m in molecules: 849 geomc = m.geomContainer 850 for g in geomsToColor: 851 ge = geomc.geoms[g] 852 colors = self.palette.lookup(range(len(ge.instanceMatrices))) 853 ge.Set(materials=colors, inheritMaterial=0, tagModified=False) 854 ge.SetForChildren(inheritMaterial=True, recursive=True)
855 # ColorCommand.doit(self, m, colors, geomsToColor) 856 857 858 colorByInstanceGuiDescr = {'widgetType':'Menu', 'menuBarName':'menuRoot', 859 'menuButtonName':'Color', 860 'menuEntryLabel':'by Instance'} 861 862 ColorByInstanceGUI = CommandGUI() 863 ColorByInstanceGUI.addMenuCommand('menuRoot', 'Color', 'by Instance') 864 865
866 -class ColorByProperties(ColorCommand):
867 """ 868 Command to color the current selection according to the integer 869 or float properties, or by defining a function. 870 \nPackage : Pmv 871 \nModule : colorCommands 872 \nClass : ColorByProperties 873 \nCommand : colorByProperties 874 \nSynopsis:\n 875 None <- colorByProperties(nodes, geomsToColor, property,colormap='rgb256', **kw)\n 876 nodes --- any set of MolKit nodes describing molecular components\n 877 geomsToColor --- list of the name of geometries to color default is 'all'\n 878 property --- property name of type integer or float or property defined by a function returning a list of float or int.\n 879 colormap --- either a string representing a colormap or a DejaVu.ColorMap instance.\n 880 """ 881 882 levelOrder = {'Atom':0 , 883 'Residue':1, 884 'Chain':2, 885 'Molecule':3 } 886
887 - def __init__(self, func=None):