OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
init.py
Go to the documentation of this file.
1 import __main__
2 import sys
3 import os.path
4 import optparse
5 from ost import io, mol, seq, geom, conop, gui, settings, gfx
6 
7 import ost
8 try:
9  from ost import img
10  import ost.img.alg
11  _img_present=True
12 except ImportError:
13  _img_present=False
14  pass
15 
16 import httplib
17 
18 from PyQt4 import QtGui, QtCore
19 from ost.gui.scene.init_inspector import _InitInspector
20 from ost.gui.init_menubar import _InitMenuBar
21 from ost.gui.init_spacenav import _InitSpaceNav
22 from ost.gui.init_context_menu import _InitContextMenu
23 from ost.gui.init_splash import _InitSplash
24 from ost.gui.dng import termuse
25 from ost.gui.dng import superpositiondialog
26 from ost.gui.scene.remote import RemoteLoader
27 
28 import ost.gui.dng.menu
29 from PyQt4.QtGui import *
30 def _my_exit(code):
31  QtGui.QApplication.instance().quit()
32  gui.GostyApp.Instance().ProcessEvents()
33  sys._exit(code)
34 
35 sys._exit=sys.exit
36 sys.exit=_my_exit
37 
38 def _InitRuleBasedProcessor():
39  compound_lib_path=os.path.join(ost.GetSharedDataPath(), 'compounds.chemlib')
40  if os.path.exists(compound_lib_path):
41  conop_inst=conop.Conopology.Instance()
42  compound_lib=conop.CompoundLib.Load(compound_lib_path)
43  conop_inst.SetDefaultLib(compound_lib)
44  io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
45 
46 # switch to rule-based processor for high fidelity if compounds.chemlib is
47 # available
48 _InitRuleBasedProcessor()
49 
50 def _CheckRestore():
51  settings = QtCore.QSettings()
52  restore = settings.value("restore_settings",QtCore.QVariant(False)).toBool()
53  if not restore:
54  settings.clear()
55  settings.setValue("restore_settings",QtCore.QVariant(True))
56 
57 def _InitPanels(app):
58  panels = app.perspective.panels
59  panels.AddWidgetToPool('ost.gui.FileBrowser', -1)
60  panels.AddWidgetToPool('ost.gui.PythonShell', 1)
61  panels.AddWidgetToPool('ost.gui.SceneWin', 1)
62  panels.AddWidgetToPool('ost.gui.SequenceViewer', 1)
63  panels.AddWidgetToPool('ost.gui.MessageWidget', 1)
64  if not panels.Restore("ui/perspective/panels"):
65  panels.AddWidget(gui.PanelPosition.LEFT_PANEL, app.scene_win)
66  panels.AddWidgetByName(gui.PanelPosition.LEFT_PANEL,
67  'ost.gui.FileBrowser', False)
68  panels.AddWidget(gui.PanelPosition.BOTTOM_PANEL, app.seq_viewer)
69  panels.AddWidget(gui.PanelPosition.BOTTOM_PANEL, app.py_shell)
70  panels.AddWidget(gui.PanelPosition.RIGHT_PANEL, app.message_widget)
71  return False
72  return True
73 
74 
75 def _InitFrontEnd(try_stereo):
76  _CheckRestore()
77  app=gui.GostyApp.Instance()
78  app.SetAppTitle("DNG")
79  app.TryStereo(try_stereo)
80  main_area=app.perspective.main_area
81  _InitSpaceNav(app)
82  _InitContextMenu(app)
83  main_area.AddPersistentWidget("3D Scene", "gl_win" , app.gl_win,
84  int(QtCore.Qt.WindowMaximized))
85  _InitInspector(app)
87  app.perspective.Restore()
88  additional_modules=getattr(__main__, 'ADDITIONAL_GUI_MODULES', [])
89  for module_name in additional_modules:
90  __import__(module_name)
91  app.ProcessEvents()
92 
93 
94 
95  if not _InitPanels(app):
96  _InitSplash()
97 
98 def _load_files():
99  for pdb_id in options.pdb_ids:
100  pdb_id, sel=_SplitIDSel(pdb_id)
101  selection=_get_selection_query(sel)
102  gui.FileLoader.LoadFrom(pdb_id,"pdb.org",selection)
103 
104  input_files=[_SplitIDSel(arg) for arg in loading_list]
105  for f in input_files:
106  selection=_get_selection_query(f[1])
107  gui.FileLoader.LoadObject(f[0],selection)
108 
109 def _get_selection_query(sel):
110  if len(options.query)>0:
111  if len(sel)>0:
112  return '(%s) and (%s)' % (options.query, sel)
113  else:
114  return options.query
115  elif len(sel)>0:
116  return sel
117  return ""
118 
119 def _execute_script():
120  script=script_argv[0]
121  sys_argv_backup=sys.argv
122  sys.argv=script_argv
123  try:
124  execfile(script, __main__.__dict__)
125  finally:
126  sys.argv=sys_argv_backup
127 
128 def show_help(option, opt, value, parser):
129  parser.print_help()
130  QtGui.QApplication.instance().exit()
131  sys.exit(-1)
132 
133 def parse_script_option(option, opt, value, parser):
134  script_argv.append(value)
135  for arg in parser.rargs:
136  script_argv.append(arg)
137  del parser.rargs[0:len(parser.rargs)]
138 
139 def _SplitIDSel(name):
140  pos=name.find('[')
141  if pos>-1:
142  return name[:pos], name[pos+1:-1]
143  return name, ''
144 
145 def stop():
146  gui.GostyApp.Instance().StopScript()
147 
148 
149 loading_list=[]
150 script_argv=[]
151 #images=[]
152 #viewers=[]
153 usage = 'usage: dng [options] [files to load]'
154 class OstOptionParser(optparse.OptionParser):
155  def __init__(self, **kwargs):
156  optparse.OptionParser.__init__(self, **kwargs)
157  def exit(self, status_code, error_message):
158  print error_message,
159  QtGui.QApplication.instance().exit()
160  sys.exit(-1)
161 
162 parser=OstOptionParser(usage=usage,conflict_handler="resolve")
163 parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
164 parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2,
165  help="sets the verbosity level [default: %default]")
166 parser.add_option("-s", "--script", action="callback", default=[], dest="script", type="string", callback=parse_script_option, help="executes a script (syntax: -s SCRIPT [options] [args]) Anything that follows this option is passed to the script")
167 parser.add_option("-p", "--pdb_id", dest="pdb_ids", default=[],action="append", help="PDB file ID. The file will be retrieved from PDB")
168 parser.add_option("-b", "--processor", dest="processor", default="HEURISTIC", help="Type of processor used by the progam (either RULE_BASED or HEURISTIC) [default: %default]")
169 parser.add_option("-c", "--compound_library", dest="complib", default="compounds.chemlib", help="Compound library for the RULE_BASED processor (only used if --processor option is set to RULE_BASED, otherwise ignored [default: %default]")
170 parser.add_option("-q", "--query", dest="query", default="", help="Selection query to be highlighted automatically upon loading (only used together with -p option or when a PDB file is loaded, otherwise ignored [default: None]")
171 parser.add_option("-S","--stereo", dest="try_stereo", default=False, action="store_true",help="try to get a quad-buffer stereo visual")
172 parser.disable_interspersed_args()
173 (options, args) = parser.parse_args()
174 
175 if len(parser.rargs)!=0:
176  for rargs_string in parser.rargs:
177  if not rargs_string.endswith('.py'):
178  loading_list.append(rargs_string)
179  else:
180  print 'Error: one of the files to load is a Python script, use -s flag to execute it\n'
181  QtGui.QApplication.instance().exit()
182  sys.exit(-1)
183 
184 if len(options.script)!=0:
185  script_argv=options.script
186 
187 if options.processor=="RULE_BASED":
188  from ost import conop
189  if os.path.exists(options.complib):
190  compound_lib=conop.CompoundLib.Load(options.complib)
191  conop.SetDefaultLib(compound_lib)
192  io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
193 
194 home = os.getenv('HOME') or os.getenv('USERPROFILE')
195 _ostrc=os.path.join(home, '.ostrc')
196 if os.path.exists(_ostrc):
197  try:
198  exec(open(_ostrc))
199  except Exception, e:
200  print e
201 else:
202  rcfile=open(_ostrc,"w")
203  print >> rcfile, '# This python file is parsed by ost and dng at startup'
204  print >> rcfile, '# Its content is made available in the global namespace'
205  print >> rcfile, '# It can be used to define custom variables and functions'
206  print >> rcfile, '# For example:'
207  print >> rcfile, '# IMPORTANT_DIR="path/to/important/dir"'
208  rcfile.close()
209 
210 ost.gui.PushVerbosityLevel(options.vlevel)
211 working_dir=settings.GetValue("DNG_WORKING_DIR",None)
212 
213 if working_dir != None and os.path.isdir(working_dir):
214  os.chdir(working_dir)
215 
216 _InitFrontEnd(options.try_stereo)
217 
218 if len(loading_list)!=0 or len(options.pdb_ids)!=0:
219  _load_files()
220  gfx.Scene().Autoslab()
221 if len(script_argv)!=0:
222  _execute_script()
def parse_script_option
Definition: init.py:133
main class for organization and root for the graphical display
Definition: scene.hh:81
def PushVerbosityLevel
Definition: __init__.py:231
String DLLEXPORT_OST_BASE GetSharedDataPath()