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

Source Code for Module Pmv.genparserCommands

  1  ############################################################################# 
  2  # 
  3  # Author: Kevin Chan, Michel F. SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  # 
 10  #$Header: /opt/cvs/python/packages/share1.5/Pmv/genparserCommands.py,v 1.7 2004/03/22 19:29:39 sophiec Exp $ 
 11  # 
 12  #$Id: genparserCommands.py,v 1.7 2004/03/22 19:29:39 sophiec Exp $ 
 13  # 
 14  from ViewerFramework.VFCommand import CommandGUI 
 15  ##  from ViewerFramework.gui import InputForm, InputFormDescr 
 16  from mglutil.gui.InputForm.Tk.gui import InputForm, InputFormDescr 
 17   
 18  from Pmv.mvCommand import MVCommand 
 19  from MolKit.protein import Protein 
 20  from MolKit.genPdbParser import GenPdbParser, IndexSpecs, ColumnSpecs 
 21  from MolKit.molecule import Atom 
 22  from mglutil.gui.BasicWidgets.Tk.customizedWidgets import SaveButton, LoadButton 
 23  import types, os, string 
 24  import Pmw, Tkinter 
 25   
 26   
 27  from Pmv.fileCommands import PDBReader 
 28   
29 -class GenPDBReader(PDBReader):
30 """ Command to load PDB files using parser with user defined 31 specifications """ 32 33
34 - def __init__(self):
35 PDBReader.__init__(self) 36 self.specfileTypes = [('all', '*')] 37 self.specfileBrowserTitle = 'load specification file: '
38 39
40 - def _doit(self, filename, specs):
41 """ does the parsing of the pdb file using specs object""" 42 newparser = GenPdbParser(filename, specs) 43 mols = newparser.parse() 44 newmol = [] 45 for m in mols: 46 mol = self.vf.addMolecule(m) 47 if mol==None: 48 del newparser 49 return [] 50 if hasattr(self, "done"): 51 self.done.release() 52 newmol.append(mol) 53 return newmol
54 55
56 - def doit(self, filename, specfile):
57 """ takes a pdb file name and a specification file name """ 58 # self.log(filename, specfile) 59 specs = self.extractSpecs(specfile) 60 if specs: 61 mols = self._doit(filename, specs) 62 return mols 63 else: 64 import traceback,sys 65 print "ERROR IN READING SPECIFICATIONS:%s"%specfile 66 traceback.print_exc() 67 print "---------- End of error message ----------" 68 if self.vf.hasGui and self.vf.guiVisible \ 69 and self.vf.withShell: 70 self.vf.GUI.pyshell.top.deiconify()
71 ## mesg = "error in reading specifications from " + specfile 72 ## self.warningMsg(mesg) 73
74 - def __call__(self, filename, specfile, **kw):
75 """mol <- genreadPDB(filename, specfile, **kw) 76 filename: path name of the PDB file 77 specfile: a specification file name 78 """ 79 return apply ( self.doitWrapper, (filename, specfile), kw )
80
81 - def guiCallback(self, event=None, *args, **kw):
82 cmdmenuEntry = self.GUI.menu[4]['label'] 83 file = self.vf.askFileOpen(types=self.fileTypes, 84 idir = self.lastDir, 85 title=self.fileBrowserTitle) 86 mol = None 87 if file != None: 88 self.lastDir = os.path.split(file)[0] 89 #args = (file,) 90 self.vf.GUI.configMenuEntry(self.GUI.menuButton, cmdmenuEntry, 91 state = 'disabled') 92 #mol = self.vf.tryto(self.doit, file) 93 specfile = self.vf.askFileOpen(types=self.specfileTypes, 94 idir = self.lastDir, 95 title=self.specfileBrowserTitle) 96 if specfile != None: 97 ## RecSpec = self.extractSpecs(specfile) 98 ## if RecSpec: 99 try: 100 mol = self.doitWrapper(file, specfile, redraw=1) 101 self.vf.GUI.configMenuEntry(self.GUI.menuButton, 102 cmdmenuEntry,state = 'normal') 103 except: 104 import traceback,sys 105 print "ERROR IN LOADING FILE:%s"%file 106 traceback.print_exc() 107 print "---------- End of error message ----------" 108 if self.vf.hasGui and self.vf.guiVisible \ 109 and self.vf.withShell: 110 self.vf.GUI.pyshell.top.deiconify() 111 ## self.vf.GUI.configMenuEntry(self.GUI.menuButton, 112 ## cmdmenuEntry,state = 'normal') 113 114 ## try: 115 ## mol = self.doit(file, RecSpec) 116 ## except: 117 ## msg = "error in loading file: " + file 118 ## self.warningMsg(msg) 119 self.vf.GUI.configMenuEntry(self.GUI.menuButton, 120 cmdmenuEntry,state = 'normal') 121 return mol
122
123 - def extractSpecs(self, file):
124 """ takes a specification file in proper format and creates a specs 125 object """ 126 f = open(file, 'r') 127 lines = f.readlines(30) 128 ## num = len(string.split(lines[0])) 129 ## if num==4: 130 parts = string.split(lines[0]) 131 if len(parts)==4 and parts[0]=='FieldName' and parts[1]=='From' and \ 132 parts[2]=='To' and parts[3]=='Type': 133 Specs = ColumnSpecs() 134 for line in lines[1:]: 135 vals = string.split(line) 136 if len(vals)==4: 137 specEntry = {'field_name':vals[0], 138 'from':int(vals[1]), 139 'to':int(vals[2]), 140 'var_type':vals[3] 141 } 142 Specs.define_field(specEntry) 143 elif len(parts)==3 and parts[0]=='FieldName' and parts[1]=='Index' \ 144 and parts[2]=='Type': 145 Specs = IndexSpecs() 146 for line in lines[1:]: 147 vals = string.split(line) 148 if len(vals)==3: 149 specEntry = {'field_name':vals[0], 150 'index':int(vals[1]), 151 'var_type':vals[2] 152 } 153 Specs.define_field(specEntry) 154 else: return None 155 if not (Specs.DefFieldsDict.has_key('x') and 156 Specs.DefFieldsDict.has_key('y') and 157 Specs.DefFieldsDict.has_key('z')): 158 msg = "specs for atom coordinates (x,y,z) are missing" 159 self.warningMsg(msg) 160 return None 161 return Specs
162 163 genPDBReaderGuiDescr = {'widgetType':'menuRoot', 'menuBarName':'File', 164 'menuButtonName':'Read PDB with Gen Parser ...', 165 'index':0} 166 167 GenPDBReaderGUI = CommandGUI() 168 GenPDBReaderGUI.addMenuCommand('menuRoot', 'File', 169 'Read PDB with Gen Parser ...',index=0) 170 171 172 FieldNames = ('serial', 'name', 'altLoc', 'resName', 'chainID', 173 'resSeq', 'iCode', 'x', 'y', 'z', 'occupancy', 174 'tempFactor', 'segID', 'element', 'charge', 'other') 175 variableTypes = ('int', 'character', 'float', 'alphabetic', 'string') 176 specEntries = ('field_name', 'var_type', 'from', 'to', 'index') 177 178
179 -class DefinePdbSpecifications(MVCommand):
180 """ class to define some pdb specifications in a dictionary and save them 181 in a file """ 182
183 - def __init__(self):
184 """ constructor """ 185 MVCommand.__init__(self) 186 # iformStatus tells if these exist and if the widgets are visible 187 self.iFormStatus = {'columnspecs':0, 'columnwidgets':0, 188 'cwidgetsshow':0, 189 'indexspecs':0, 'indexwidgets':0, 190 'iwidgetsshow':0} 191 self.maxIndex = None 192 self.active = 0 193 self.ccurSpecFile = '' 194 self.icurSpecFile = ''
195
196 - def extractSpecs(self, file):
197 """ takes a specification file in proper format and creates a specs 198 object """ 199 f = open(file, 'r') 200 lines = f.readlines(30) 201 ## num = len(string.split(lines[0])) 202 ## if num==4: 203 parts = string.split(lines[0]) 204 if len(parts)==4 and parts[0]=='FieldName' and parts[1]=='From' and \ 205 parts[2]=='To' and parts[3]=='Type': 206 Specs = ColumnSpecs() 207 for line in lines[1:]: 208 vals = string.split(line) 209 if len(vals)==4: 210 specEntry = {'field_name':vals[0], 211 'from':int(vals[1]), 212 'to':int(vals[2]), 213 'var_type':vals[3] 214 } 215 Specs.define_field(specEntry) 216 # elif num==3: 217 elif len(parts)==3 and parts[0]=='FieldName' and parts[1]=='Index' \ 218 and parts[2]=='Type': 219 Specs = IndexSpecs() 220 for line in lines[1:]: 221 vals = string.split(line) 222 if len(vals)==3: 223 specEntry = {'field_name':vals[0], 224 'index':int(vals[1]), 225 'var_type':vals[2] 226 } 227 Specs.define_field(specEntry) 228 else: return None 229 ## if not (Specs.DefFieldsDict.has_key('x') and 230 ## Specs.DefFieldsDict.has_key('y') and 231 ## Specs.DefFieldsDict.has_key('z')): return None 232 return Specs
233 234
235 - def compareFrom(self, e1, e2):
236 return cmp(e1['from'], e2['from'])
237
238 - def compareIndex(self, e1, e2):
239 return cmp(e1['index'], e2['index'])
240
241 - def update_SpecBox(self):
242 """ writes the specs in self.Specs to the ScrolledText box in gui. """ 243 entriesString = '' 244 if self.Specs.specType=='c': 245 entrylist = [] 246 for entryname in (self.Specs.DefFieldsDict.keys() + 247 self.Specs.UserFieldsDict.keys()): 248 e = self.Specs.DefFieldsDict[entryname] 249 entrylist.append(e) 250 entrylist.sort(self.compareFrom) 251 for entry in entrylist: 252 entryname = entry['field_name'] 253 e = self.Specs.DefFieldsDict[entryname] 254 entrystring = string.ljust(entryname, 12) +\ 255 string.ljust(str(e['from']), 4) +\ 256 string.ljust(str(e['to']), 4) +\ 257 e['var_type'] + '\n' 258 ## for key in self.Specs.DefFieldsDict[entryname].keys(): 259 ## if key != 'field_name': 260 ## val = self.Specs.DefFieldsDict[entryname][key] 261 ## entrystring = entrystring + '%s'%val 262 ## entrystring = entrystring + '\n' 263 entriesString = entriesString + entrystring 264 elif self.Specs.specType=='i': 265 entrylist = [] 266 for entryname in (self.Specs.DefFieldsDict.keys() + 267 self.Specs.UserFieldsDict.keys()): 268 e = self.Specs.DefFieldsDict[entryname] 269 entrylist.append(e) 270 entrylist.sort(self.compareIndex) 271 for entry in entrylist: 272 entryname = entry['field_name'] 273 e = self.Specs.DefFieldsDict[entryname] 274 entrystring = string.ljust(entryname, 13) +\ 275 string.ljust(str(e['index']), 6) +\ 276 e['var_type'] + '\n' 277 entriesString = entriesString + entrystring 278 else: return 279 specboxentry = self.idf.entryByName['%s_specBox'%self.Specs.specType] 280 specboxentry['widget'].settext(entriesString)
281 282
283 - def clearSpecs_cb(self, event = None):
284 """ clears the current specifications in the gui """ 285 if self.iFormStatus['cwidgetsshow']: 286 self.Specs = ColumnSpecs() 287 self.idf.entryByName['c_save']['ifile'] = None 288 self.ccurSpecFile = '' 289 elif self.iFormStatus['iwidgetsshow']: 290 self.Specs = IndexSpecs() 291 self.idf.entryByName['i_save']['ifile'] = None 292 self.icurSpecFile = '' 293 self.update_SpecBox() 294 self.idf.entryByName['SpecFileName']['widget'].configure(text='')
295
296 - def loadSpecs_cb(self, filename, event = None):
297 """ callback function to load specs from a file to the gui """ 298 Specs = self.extractSpecs(filename) 299 bad = 0 300 if Specs: 301 fparts = os.path.split(filename) 302 if isinstance(Specs, ColumnSpecs): 303 self.column_cb() 304 self.Specs = self.columnSpecs = Specs 305 self.idf.entryByName['byColumn']['wcfg']['variable'].set('c') 306 self.idf.entryByName['c_save']['idir'] = fparts[0] 307 self.idf.entryByName['c_save']['ifile'] = fparts[1] 308 self.ccurSpecFile = filename 309 elif isinstance(Specs, IndexSpecs): 310 self.index_cb() 311 self.Specs = self.indexSpecs = Specs 312 self.idf.entryByName['byIndex']['wcfg']['variable'].set('i') 313 self.idf.entryByName['i_save']['idir'] = fparts[0] 314 self.idf.entryByName['i_save']['ifile'] = fparts[1] 315 self.icurSpecFile = filename 316 else: bad = 1 317 else: bad = 1 318 if bad: 319 msg = "error: could not load specification file" 320 self.vf.warningMsg(msg) 321 else: 322 self.update_SpecBox() 323 self.idf.entryByName['SpecFileName']['widget'].configure(text=filename)
324 325
326 - def saveSpecs_cb(self, filename, event = None):
327 """ callback function to save pdb specs to filename """ 328 if not (self.Specs.DefFieldsDict.has_key('x') and 329 self.Specs.DefFieldsDict.has_key('y') and 330 self.Specs.DefFieldsDict.has_key('z')): 331 msg = "specs for atom coordinates (x,y,z) are required to parse a pdb file" 332 self.warningMsg(msg) 333 return 334 335 fparts = os.path.split(filename) 336 self.write_from_specBox(filename) 337 self.idf.entryByName['SpecFileName']['widget'].configure(text=filename) 338 if self.iFormStatus['cwidgetsshow']: 339 self.idf.entryByName['c_save']['idir'] = fparts[0] 340 self.idf.entryByName['c_save']['ifile'] = fparts[1] 341 self.ccurSpecFile = filename 342 elif self.iFormStatus['iwidgetsshow']: 343 self.idf.entryByName['i_save']['idir'] = fparts[0] 344 self.idf.entryByName['i_save']['ifile'] = fparts[1] 345 self.icurSpecFile = filename
346 ## self.writeDicts(filename, 347 ## (self.Specs.DefFieldsDict, self.Specs.UserFieldsDict), 348 ## ('DefFieldsDict', 'UserFieldsDict')) 349
350 - def write_from_specBox(self, filename):
351 """ takes text from specBox and writes it to a file """ 352 if self.Specs.specType=='c': 353 text = 'FieldName From To Type \n' + \ 354 self.idf.entryByName['c_specBox']['widget'].get() 355 elif self.Specs.specType=='i': 356 text = 'FieldName Index Type \n' + \ 357 self.idf.entryByName['i_specBox']['widget'].get() 358 359 if text: 360 f = open(filename, 'w') 361 f.write(text) 362 f.close()
363 364 # not implemented now 365 ## def writeDicts(self, filename, dicts, dictnames): 366 ## """ takes filename, dictionaries, and names of dictionaries. Writes 367 ## the dictionaries in proper python format in the file. """ 368 ## f = open(filename, 'w') 369 ## i = 0 370 ## for dict in dicts: 371 ## dictstring = dictnames[0] + '= {' 372 ## for key in dict.keys(): 373 ## val = dict[key] 374 ## if type(val)==types.StringType: val = "'%s'"%val 375 ## dicstring = dictstring + " '%s'"%key +':' + '%s,'%str(val) 376 ## dictstring = dictstring[:-1] + '}\n' 377 ## f.write(dictstring) 378 ## i = i+1 379 ## f.close() 380
381 - def addToSpecs_cb(self, event = None):
382 """ callback function to add button. Adds the specs in the entry 383 fields to the self.Specs object. """ 384 fieldspec = {} 385 for wName in self.smallidf.entryByName.keys(): 386 if wName in specEntries: 387 # check that there is an entry 388 widget = self.smallidf.entryByName[wName]['widget'] 389 if wName in ('index', 'from', 'to'): 390 fieldspec[wName] = int(widget.get()) 391 else: fieldspec[wName] = widget.get() 392 self.Specs.define_field(fieldspec) 393 self.update_SpecBox()
394
395 - def removeFromSpecs_cb(self, event = None):
396 """ callback to remove button. Removes spec given by user from the 397 self.Specs object. """ 398 # check that there is an entry 399 widget = self.smallidf.entryByName['field_name']['widget'] 400 fieldspec = {'field_name':widget.get()} 401 self.Specs.remove_field(fieldspec) 402 self.update_SpecBox()
403
404 - def col_selectfield_cb(self, fieldname, event = None):
405 """ callback to put current values of fieldname into the entryfield of 406 the gui """ 407 ebn = self.smallidf.entryByName 408 if self.Specs.DefFieldsDict.has_key(fieldname) or \ 409 self.Specs.UserFieldsDict.has_key(fieldname): 410 ebn['from']['widget'].setentry(self.Specs.get(fieldname, 'from')) 411 ebn['to']['widget'].setentry(self.Specs.get(fieldname, 'to')) 412 ebn['var_type']['widget'].component('entryfield').setentry(self.Specs.get(fieldname, 'var_type')) 413 else: 414 ebn['from']['widget'].clear() 415 ebn['to']['widget'].clear()
416
417 - def ind_selectfield_cb(self, fieldname, event = None):
418 """ callback to put current values of fieldname into the entryfield of 419 the gui """ 420 ebn = self.smallidf.entryByName 421 if self.Specs.DefFieldsDict.has_key(fieldname) or \ 422 self.Specs.UserFieldsDict.has_key(fieldname): 423 ebn['index']['widget'].setentry(self.Specs.get(fieldname, 'index')) 424 ebn['var_type']['widget'].component('entryfield').setentry(self.Specs.get(fieldname, 'var_type')) 425 else: 426 ebn['index']['widget'].clear()
427
428 - def defineColSpecs_cb(self, event = None):
429 """ callback that creates an input form with fields for the user to 430 define the field specifications in column format. callback of a 431 define/modify button. 432 """ 433 # if it exists, bring to front ? 434 if not hasattr(self, 'smallform'): 435 smallidf = self.smallidf = \ 436 InputFormDescr(title = 'Define Column Specs:') 437 smallidf.append({'widgetType':Pmw.ComboBox, 438 'name':'field_name', 439 'wcfg':{'label_text':'Field Name:', 440 'labelpos':'w', 'label_width':25, 441 'scrolledlist_items':FieldNames, 442 'selectioncommand':self.col_selectfield_cb, 443 'entryfield_validate':'alphabetic', 444 'entry_width':12}, 445 'gridcfg':{'row':0, 'column':0, 'sticky':'w', 446 'columnspan':3} 447 }) 448 smallidf.append({'widgetType':Pmw.EntryField, 449 'name':'from', 450 'wcfg':{'labelpos':'w', 'label_width':25, 451 'label_text':'from column (int 1-80):', 452 'entry_width':2, 453 'validate':{'validator':'numeric', 454 'min':1, 455 'max':80}}, 456 'gridcfg':{'row':1, 'column':0, 'sticky':'w', 457 'columnspan':3} 458 }) 459 smallidf.append({'widgetType':Pmw.EntryField, 460 'name':'to', 461 'wcfg':{'labelpos':'w', 'label_width':25, 462 'label_text':'to column (int 1-80):', 463 'entry_width':2, 464 'validate':{'validator':'numeric', 465 'min':1, 466 'max':80}}, 467 'gridcfg':{'row':2, 'column':0, 'sticky':'w', 468 'columnspan':3} 469 }) 470 smallidf.append({'widgetType':Pmw.ComboBox, 471 'name':'var_type', 472 'defaultValue':'string', 473 'wcfg':{'label_text':'Field value type:', 474 'labelpos':'w', 'label_width':25, 475 'scrolledlist_items':variableTypes, 476 'entry_width':12}, 477 'gridcfg':{'row':3, 'column':0, 'sticky':'w', 478 'columnspan':3} 479 }) 480 smallidf.append({'widgetType':Tkinter.Button, 481 'name':'Add', 482 'wcfg':{'text':'Add', 483 'command':self.addToSpecs_cb}, 484 'gridcfg':{'row':4, 'column':0, 'sticky':'ew'} 485 }) 486 smallidf.append({'widgetType':Tkinter.Button, 487 'name':'Remove', 488 'wcfg':{'text':'Remove', 489 'command':self.removeFromSpecs_cb}, 490 'gridcfg':{'row':4, 'column':1, 'sticky':'ew'} 491 }) 492 smallidf.append({'widgetType':Tkinter.Button, 493 'name':'dismiss', 494 'wcfg':{'text':'dismiss', 495 'command':self.dismiss_smallform_cb}, 496 'gridcfg':{'row':4, 'column':2, 'sticky':'ew'} 497 }) 498 self.smallform = self.vf.getUserInput(smallidf, 499 modal = 0, blocking = 0)
500
501 - def defineIndSpecs_cb(self, event = None):
502 """ callback that creates an input form with fields for the user to 503 define the field specifications in index format. callback of a 504 define/modify button. 505 """ 506 # if it exists, bring to front ? 507 if not hasattr(self, 'smallform'): 508 smallidf = self.smallidf \ 509 = InputFormDescr(title = 'Define Index Specs:') 510 # show split records??? 511 smallidf.append({'widgetType':Pmw.ComboBox, 512 'name':'field_name', 513 'wcfg':{'label_text':'Field Name:', 514 'labelpos':'w', 'label_width':25, 515 'scrolledlist_items':FieldNames, 516 'selectioncommand':self.ind_selectfield_cb, 517 'entryfield_validate':'alphabetic', 518 'entry_width':12}, 519 'gridcfg':{'row':0, 'column':0, 'sticky':'w', 520 'columnspan':3} 521 }) 522 smallidf.append({'widgetType':Pmw.EntryField, 523 'name':'index', 524 'wcfg':{'labelpos':'w', 'label_width':25, 525 'label_text':'index(int):', 526 'entry_width':2, 527 'validate':{'validator':'numeric', 528 'min':1, 'max':self.maxIndex 529 }}, 530 'gridcfg':{'row':1, 'column':0, 'sticky':'w', 531 'columnspan':3} 532 }) 533 smallidf.append({'widgetType':Pmw.ComboBox, 534 'name':'var_type', 535 'defaultValue':'string', 536 'wcfg':{'label_text':'Field value type:', 537 'labelpos':'w', 'label_width':25, 538 'scrolledlist_items':variableTypes, 539 'entry_width':12}, 540 'gridcfg':{'row':2, 'column':0, 'sticky':'w', 541 'columnspan':3} 542 }) 543 smallidf.append({'widgetType':Tkinter.Button, 544 'name':'Add', 545 'wcfg':{'text':'Add', 546 'command':self.addToSpecs_cb}, 547 'gridcfg':{'row':3, 'column':0, 'sticky':'ew'} 548 }) 549 smallidf.append({'widgetType':Tkinter.Button, 550 'name':'Remove', 551 'wcfg':{'text':'Remove', 552 'command':self.removeFromSpecs_cb}, 553 'gridcfg':{'row':3, 'column':1, 'sticky':'ew'} 554 }) 555 smallidf.append({'widgetType':Tkinter.Button, 556 'name':'dismiss', 557 'wcfg':{'text':'dismiss', 558 'command':self.dismiss_smallform_cb}, 559 'gridcfg':{'row':3, 'column':2, 'sticky':'ew'} 560 }) 561 self.smallform = self.vf.getUserInput(smallidf, 562 modal = 0, blocking = 0)
563
564 - def hide_widget_group(self, grouptype):
565 """ hides widgets of grouptype (either 'i' or 'c'). called by either 566 column_cb or index_cb """ 567 idfentries = self.idf.entryByName 568 for name in idfentries.keys(): 569 if name[:2]==(grouptype+'_'): 570 idfentries[name]['widget'].grid_forget() 571 if hasattr(self, 'smallform'): 572 self.smallform.destroy() 573 del self.smallform, self.smallidf 574 self.iFormStatus['%swidgetsshow'%grouptype] = 0
575
576 - def show_widget_group(self, grouptype):
577 """ shows existing widgets of grouptype (either 'i' or 'c'). called by 578 either column_cb or index_cb 579 """ 580 idfentries = self.idf.entryByName 581 for name in idfentries.keys(): 582 if name[:2]==(grouptype+'_'): 583 apply(idfentries[name]['widget'].grid,(),idfentries[name]['gridcfg']) 584 self.iFormStatus['%swidgetsshow'%grouptype] = 1 585 if grouptype=='c': 586 self.Specs = self.columnSpecs 587 self.idf.entryByName['SpecFileName']['widget'].configure(text=self.ccurSpecFile) 588 elif grouptype=='i': 589 self.Specs = self.indexSpecs 590 self.idf.entryByName['SpecFileName']['widget'].configure(text=self.icurSpecFile)
591 592
593 - def createColumnWidgets(self):
594 """ adds widgets to self.idf and self.form useful for column 595 specification format. called bycolumn_cb """ 596 entries = [] 597 entries.append({'widgetType':Tkinter.Button, 'name':'c_define_modify_specs', 598 'wcfg':{'text':'Define/Modify...', 599 'command':self.defineColSpecs_cb, 600 'width':12}, 601 'gridcfg':{'row':5, 'column':0, 'columnspan':2} 602 #, 'sticky':'e'} 603 }) 604 entries.append({'widgetType':Pmw.ScrolledText, 'name':'c_specBox', 605 'wcfg':{'labelpos':'nw', 606 'label_text':'FieldName From To Type ', 607 'text_width':30, 'text_height':5, 608 'text_font':('Courier New', 10), 609 'label_font':('Courier New', 10)}, 610 'gridcfg':{'row':5, 'column':2, 'rowspan':3, 611 'columnspan':2} 612 #'sticky':'w'} 613 }) 614 if self.ccurSpecFile: 615 fparts = os.path.split(self.ccurSpecFile) 616 else: fparts = ['.', None] 617 ## entries.append({'widgetType':'SaveButton', 'name':'c_save', 618 ## 'wcfg':{'text':'Save', 619 ## 'width':12}, 620 ## 'callback':self.saveSpecs_cb, 621 ## 'gridcfg':{'row':6, 'column':0, 'columnspan':2},#, 'sticky':'w'}, 622 ## 'idir':fparts[0], 'ifile':fparts[1] 623 ## }) 624 625 entries.append({'widgetType':SaveButton, 626 'name':'c_save', 627 'wcfg':{'buttonType':Tkinter.Button, 628 'title':'Save Specifications in File...', 629 'types':[('All Files','*')], 630 'idir':fparts[0], 631 'ifile':fparts[1], 632 'callback':self.saveSpecs_cb, 633 'widgetwcfg':{'text':'Save', 634 'width':12}}, 635 'gridcfg':{'row':6, 'column':0, 'columnspan':2},#, 'sticky':'w'}, 636 }) 637 entries.append({'widgetType':Tkinter.Button, 'name':'c_clear', 638 'wcfg':{'text':'Clear', 639 'command':self.clearSpecs_cb, 640 'width':12}, 641 'gridcfg':{'row':7, 'column':0, 'columnspan':2} 642 }) 643 644 for entry in entries: 645 self.form.addEntry(entry) 646 self.idf.entryByName[entry['name']] = entry 647 self.iFormStatus['columnwidgets'] = 1 648 self.iFormStatus['cwidgetsshow'] = 1 649 self.idf.entryByName['SpecFileName']['widget'].configure(text = self.ccurSpecFile)
650 651
652 - def createIndexWidgets(self):
653 """ adds widgets to self.idf and self.form useful to specify in index 654 format. called by index_cb """ 655 entries = [] 656 entries.append({'widgetType':Tkinter.Button, 'name':'i_define_modify_specs', 657 'wcfg':{'text':'Define/Modify...', 658 'command':self.defineIndSpecs_cb, 659 'width':12}, 660 'gridcfg':{'row':5, 'column':0, 'columnspan':2}#, 'sticky':'e'} 661 }) 662 entries.append({'widgetType':Pmw.ScrolledText, 'name':'i_specBox', 663 'wcfg':{'labelpos':'nw', 664 'label_text':'FieldName Index Type ', 665 'text_width':30, 'text_height':5, 666 'text_font':('Courier New', 10), 667 'label_font':('Courier New', 10)}, 668 'gridcfg':{'row':5, 'column':2, 'rowspan':3, 669 'columnspan':2} 670 #'sticky':'w'} 671 }) 672 if self.icurSpecFile: fparts = os.path.split(self.icurSpecFile) 673 else: fparts = ['.', None] 674 ## entries.append({'widgetType':'SaveButton', 'name':'i_save', 675 ## 'wcfg':{'text':'Save', 676 ## 'width':12}, 677 ## 'callback':self.saveSpecs_cb, 678 ## 'gridcfg':{'row':6, 'column':0, 'columnspan':2},#, 'sticky':'w'}, 679 ## 'idir':fparts[0], 'ifile':fparts[1] 680 ## }) 681 entries.append({'widgetType':SaveButton, 682 'name':'i_save', 683 'wcfg':{'buttonType':Tkinter.Button, 684 'title':'Save Specifications in File...', 685 'types':[('All Files','*')], 686 'idir':fparts[0], 687 'ifile':fparts[1], 688 'callback':self.saveSpecs_cb, 689 'widgetwcfg':{'text':'Save', 690 'width':12}}, 691 'gridcfg':{'row':6, 'column':0, 'columnspan':2}, 692 }) 693 694 entries.append({'widgetType':Tkinter.Button, 'name':'i_clear', 695 'wcfg':{'text':'Clear', 696 'command':self.clearSpecs_cb, 697 'width':12}, 698 'gridcfg':{'row':7, 'column':0, 'columnspan':2} 699 }) 700 701 for entry in entries: 702 self.form.addEntry(entry) 703 self.idf.entryByName[entry['name']] = entry 704 self.iFormStatus['indexwidgets'] = 1 705 self.iFormStatus['iwidgetsshow'] = 1 706 self.idf.entryByName['SpecFileName']['widget'].configure(text = self.icurSpecFile)
707
708 - def column_cb(self, event=None):
709 """ callback of radiobutton to use column format pdb specifications 710 """ 711 Status = self.iFormStatus 712 if Status['indexwidgets'] and Status['iwidgetsshow']: 713 self.hide_widget_group('i') 714 if Status['columnwidgets']: 715 if not Status['cwidgetsshow']: self.show_widget_group('c') 716 else: 717 self.Specs = self.columnSpecs = ColumnSpecs() 718 self.iFormStatus['columnspecs'] = 1 719 self.createColumnWidgets() 720 w = self.idf.entryByName['index_label']['widget'].configure(text = '')
721
722 - def index_cb(self, event=None):
723 """ callback of radiobutton to use index format pdb specifications 724 """ 725 Status = self.iFormStatus 726 if Status['columnwidgets'] and Status['cwidgetsshow']: 727 self.hide_widget_group('c') 728 if Status['indexwidgets']: 729 if not Status['iwidgetsshow']: self.show_widget_group('i') 730 else: 731 self.Specs = self.indexSpecs = IndexSpecs() 732 self.iFormStatus['indexspecs'] = 1 733 self.createIndexWidgets() 734 self.indexLabel()
735
736 - def guiCallback(self, event=None, *args, **kw):
737 738 text = ' 1 2 3 4 5 6 7 8\n12345678901234567890123456789012345678901234567890123456789012345678901234567890' 739 if self.active: return 740 self.active = 1 741 idf = self.idf = InputFormDescr(title = 'Enter Pdb Specifications:') 742 ## idf.append({'widgetType':'OpenButton', 'name':'readFile', 743 ## 'callback':self.fileinScrolledText_cb, 744 ## 'wcfg':{'text':'load pdb file...'}, 745 ## 'gridcfg':{'row':0, 'column':0, 'sticky':'w', 'pady':5}, 746 ## 'types':[('pdb files', '*.pdb'), ('all', '*')] 747 ## }) 748 idf.append({'widgetType':LoadButton, 749 'name':'readFile', 750 'wcfg':{'buttonType':Tkinter.Button, 751 'title':'Load PDB File.', 752 'types':[('pdb files', '*.pdb'), ('all', '*')], 753 'callback':self.fileinScrolledText_cb, 754 'widgetwcfg':{'text':'load pdb file...'}}, 755 'gridcfg':{'row':0, 'column':0, 'sticky':'w', 'pady':5} 756 }) 757 758 idf.append({'widgetType':Tkinter.Label, 'name':'FileName', 759 'gridcfg':{'row':0, 'column':0, 'columnspan':4} 760 }) 761 idf.append({'widgetType':Pmw.ScrolledText, 'name':'fileView', 762 'wcfg':{'labelpos':'nw', 'label_text':text, 763 'text_height':10, 'text_width':80, 764 'text_font':('Courier New',10), 765 'label_font':('Courier New',10), 766 'vscrollmode':'static' 767 }, 768 'gridcfg':{'row':1, 'column':0, 'columnspan':4}#, 769 #'pady':5} 770 }) 771 idf.append({'widgetType':Tkinter.Label, 'name':'index_label', 772 'wcfg':{'font':('Courier New', 10)}, 773 'gridcfg':{'row':2, 'column':0, 'columnspan':4, 774 'sticky':'w', 'pady':5} 775 }) 776 spectype = Tkinter.StringVar() 777 idf.append({'widgetType':Tkinter.Radiobutton, 'name':'byColumn', 778 'wcfg':{'text':'Specify by column', 779 'variable':spectype, 780 'value':'c', 781 'command':self.column_cb}, 782 'gridcfg':{'row':3, 'column':0}#, 'columnspan':2} 783 }) 784 idf.append({'widgetType':Tkinter.Radiobutton, 'name':'byIndex', 785 'wcfg':{'text':'Specify by index', 786 'variable':spectype, 787 'value':'i', 788 'command':self.index_cb}, 789 'gridcfg':{'row':3, 'column':1}#, 'columnspan':2} 790 }) 791 ## idf.append({'widgetType':'OpenButton', 'name':'load', 792 ## 'wcfg':{'text':'Load spec file...'}, 793 ## 'callback':self.loadSpecs_cb, 794 ## 'gridcfg':{'row':3, 'column':2, 'sticky':'ew' 795 ## }, 796 ## #'columnspan':2}, 797 ## 'idir':'.', 798 ## 'types':[('all', '*')] 799 ## }) 800 idf.append({'widgetType':LoadButton, 801 'name':'load', 802 'wcfg':{'buttonType':Tkinter.Button, 803 'title':'Load Specification File ', 804 'idir':'.', 805 'types':[('all', '*')], 806 'callback':self.loadSpecs_cb, 807 'widgetwcfg':{'text':'Load spec file...'}}, 808 'gridcfg':{'row':3, 'column':2, 'sticky':'ew'} 809 }) 810 811 idf.append({'widgetType':Tkinter.Label, 'name':'SpecFileName', 812 'gridcfg':{'row':4, 'column':0, 'columnspan':4, 813 'pady':5} 814 }) 815 idf.append({'name':'dismiss', 816 'widgetType':Tkinter.Button, 817 'wcfg':{'text':'Dismiss', 818 'command': self.dismiss_cb}, 819 'gridcfg':{'row':3, 'column':3, 'sticky':'ew'}#, 820 #'columnspan':2} 821 }) 822 823 self.form = self.vf.getUserInput(idf, modal = 0, blocking = 0) 824 for N in range(4): 825 self.form.f.columnconfigure(N, minsize=168)
826 827
828 - def indexLabel(self):
829 """ creates a label that indexes each space-separated field """ 830 text = self.idf.entryByName['fileView']['widget'].get() 831 lines = string.split(text, '\n') 832 w = self.idf.entryByName['index_label']['widget'] 833 for l in lines: 834 if l[:4]=='ATOM': 835 labeltext = l 836 recs = string.split(l) 837 self.maxIndex = len(recs)-1 838 j = 1 839 for rec in recs[1:]: 840 if j > 9 and len(rec)==1: 841 rec = ' ' + rec 842 num = str(j) 843 else: 844 num = string.center('%i'%j, len(rec)) 845 ind = string.find(labeltext, '%i'%(j-1)) 846 if j==1: ind = 0 847 labeltext = labeltext[:ind] + \ 848 string.replace(labeltext[ind:],rec,num,1) 849 j = j + 1 850 labeltext = 'Index' + labeltext[5:] 851 w.configure(text = labeltext) 852 if hasattr(self, 'smallidf'): 853 self.smallidf.entryByName['index']['widget'].configure(validate = {'validator':'numeric', 'min':1, 'max':self.maxIndex}) 854 return 855 w.configure(text = '') 856 self.maxIndex = None 857 if hasattr(self, 'smallidf'): 858 self.smallidf.entryByName['index']['widget'].configure(validate = {'validator':'numeric', 'min':1, 'max':self.maxIndex})
859 860
861 - def fileinScrolledText_cb(self, filename, event = None):
862 """ take a file and puts its text into the scrolled text box """ 863 scrolledText = self.idf.entryByName['fileView']['widget'] 864 scrolledText.clear() 865 scrolledText.importfile(filename) 866 self.idf.entryByName['FileName']['widget'].configure(text=filename) 867 if self.iFormStatus['iwidgetsshow']: 868 self.indexLabel()
869
870 - def dismiss_cb(self, event = None):
871 self.form.destroy() 872 self.active = 0 873 if hasattr(self, 'smallform'): 874 self.smallform.destroy() 875 del self.smallform, self.smallidf 876 self.iFormStatus['columnwidgets'] = 0 877 self.iFormStatus['cwidgetsshow'] = 0 878 self.iFormStatus['indexwidgets'] = 0 879 self.iFormStatus['iwidgetsshow'] = 0
880
881 - def dismiss_smallform_cb(self, event = None):
882 self.smallform.destroy() 883 del self.smallform, self.smallidf
884 885 definePdbSpecificationsGuiDescr = {'widgetType':'menuRoot', 'menuBarName':'File', 886 'menuButtonName':'Define Pdb Specifications...', 887 'index':0} 888 889 DefinePdbSpecificationsGUI = CommandGUI() 890 DefinePdbSpecificationsGUI.addMenuCommand('menuRoot', 'File', 891 'Define Pdb Specifications...', 892 index = 0) 893 894 commandList = [ 895 {'name':'genreadPDB', 'cmd':GenPDBReader(), 'gui':GenPDBReaderGUI}, 896 {'name':'defPdbSpecs', 'cmd':DefinePdbSpecifications(), 897 'gui':DefinePdbSpecificationsGUI} 898 ] 899
900 -def initModule(viewer):
901 902 for dict in commandList: 903 viewer.addCommand( dict['cmd'], dict['name'], dict['gui'])
904 905 906 # self.loadCommand('genparserCommands', 'defPdbSpecs', 'Pmv') 907 # self.loadCommand('genparserCommands', 'genreadPDB', 'Pmv') 908 ## import pdb 909 ## pdb.run('self.commands["genreadPDB"].guiCallback()') 910