00001 import __main__
00002 import sys
00003 import os.path
00004 import optparse
00005 from ost import io, mol, seq, geom, conop, gui, settings, gfx
00006
00007 import ost
00008 try:
00009 from ost import img
00010 import ost.img.alg
00011 _img_present=True
00012 except ImportError:
00013 _img_present=False
00014 pass
00015
00016 import httplib
00017
00018 from PyQt4 import QtGui, QtCore
00019 from ost.gui.scene.init_inspector import _InitInspector
00020 from ost.gui.init_menubar import _InitMenuBar
00021 from ost.gui.init_spacenav import _InitSpaceNav
00022 from ost.gui.init_context_menu import _InitContextMenu
00023 from ost.gui.init_splash import _InitSplash
00024 from ost.gui.dng import termuse
00025 from ost.gui.dng import superpositiondialog
00026 from ost.gui.scene.remote import RemoteLoader
00027
00028 import ost.gui.dng.menu
00029 from PyQt4.QtGui import *
00030 def _my_exit(code):
00031 QtGui.QApplication.instance().quit()
00032 gui.GostyApp.Instance().ProcessEvents()
00033 sys._exit(code)
00034
00035 sys._exit=sys.exit
00036 sys.exit=_my_exit
00037
00038 def _InitRuleBasedProcessor():
00039 compound_lib_path=os.path.join(ost.GetSharedDataPath(), 'compounds.chemlib')
00040 if os.path.exists(compound_lib_path):
00041 conop_inst=conop.Conopology.Instance()
00042 compound_lib=conop.CompoundLib.Load(compound_lib_path)
00043 conop_inst.SetDefaultLib(compound_lib)
00044 io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
00045
00046
00047
00048 _InitRuleBasedProcessor()
00049
00050 def _CheckRestore():
00051 settings = QtCore.QSettings()
00052 restore = settings.value("restore_settings",QtCore.QVariant(False)).toBool()
00053 if not restore:
00054 settings.clear()
00055 settings.setValue("restore_settings",QtCore.QVariant(True))
00056
00057 def _InitPanels(app):
00058 panels = app.perspective.panels
00059 panels.AddWidgetToPool('ost.gui.FileBrowser', -1)
00060 panels.AddWidgetToPool('ost.gui.PythonShell', 1)
00061 panels.AddWidgetToPool('ost.gui.SceneWin', 1)
00062 panels.AddWidgetToPool('ost.gui.SequenceViewer', 1)
00063 panels.AddWidgetToPool('ost.gui.MessageWidget', 1)
00064 if not panels.Restore("ui/perspective/panels"):
00065 panels.AddWidget(gui.PanelPosition.LEFT_PANEL, app.scene_win)
00066 panels.AddWidgetByName(gui.PanelPosition.LEFT_PANEL,
00067 'ost.gui.FileBrowser', False)
00068 panels.AddWidget(gui.PanelPosition.BOTTOM_PANEL, app.seq_viewer)
00069 panels.AddWidget(gui.PanelPosition.BOTTOM_PANEL, app.py_shell)
00070 panels.AddWidget(gui.PanelPosition.RIGHT_PANEL, app.message_widget)
00071 return False
00072 return True
00073
00074
00075 def _InitFrontEnd(try_stereo):
00076 _CheckRestore()
00077 app=gui.GostyApp.Instance()
00078 app.SetAppTitle("DNG")
00079 app.TryStereo(try_stereo)
00080 main_area=app.perspective.main_area
00081 _InitSpaceNav(app)
00082 _InitContextMenu(app)
00083 main_area.AddPersistentWidget("3D Scene", "gl_win" , app.gl_win,
00084 int(QtCore.Qt.WindowMaximized))
00085 _InitInspector(app)
00086 ost.gui.dng.menu._InitMenu()
00087 app.perspective.Restore()
00088 additional_modules=getattr(__main__, 'ADDITIONAL_GUI_MODULES', [])
00089 for module_name in additional_modules:
00090 __import__(module_name)
00091 app.ProcessEvents()
00092
00093
00094
00095 if not _InitPanels(app):
00096 _InitSplash()
00097
00098 def _load_files():
00099 for pdb_id in options.pdb_ids:
00100 pdb_id, sel=_SplitIDSel(pdb_id)
00101 selection=_get_selection_query(sel)
00102 gui.FileLoader.LoadFrom(pdb_id,"pdb.org",selection)
00103
00104 input_files=[_SplitIDSel(arg) for arg in loading_list]
00105 for f in input_files:
00106 selection=_get_selection_query(f[1])
00107 gui.FileLoader.LoadObject(f[0],selection)
00108
00109 def _get_selection_query(sel):
00110 if len(options.query)>0:
00111 if len(sel)>0:
00112 return '(%s) and (%s)' % (options.query, sel)
00113 else:
00114 return options.query
00115 elif len(sel)>0:
00116 return sel
00117 return ""
00118
00119 def _execute_script():
00120 script=script_argv[0]
00121 sys_argv_backup=sys.argv
00122 sys.argv=script_argv
00123 try:
00124 execfile(script, __main__.__dict__)
00125 finally:
00126 sys.argv=sys_argv_backup
00127
00128 def show_help(option, opt, value, parser):
00129 parser.print_help()
00130 QtGui.QApplication.instance().exit()
00131 sys.exit(-1)
00132
00133 def parse_script_option(option, opt, value, parser):
00134 script_argv.append(value)
00135 for arg in parser.rargs:
00136 script_argv.append(arg)
00137 del parser.rargs[0:len(parser.rargs)]
00138
00139 def _SplitIDSel(name):
00140 pos=name.find('[')
00141 if pos>-1:
00142 return name[:pos], name[pos+1:-1]
00143 return name, ''
00144
00145 def stop():
00146 gui.GostyApp.Instance().StopScript()
00147
00148
00149 loading_list=[]
00150 script_argv=[]
00151
00152
00153 usage = 'usage: dng [options] [files to load]'
00154 class OstOptionParser(optparse.OptionParser):
00155 def __init__(self, **kwargs):
00156 optparse.OptionParser.__init__(self, **kwargs)
00157 def exit(self, status_code, error_message):
00158 print error_message,
00159 QtGui.QApplication.instance().exit()
00160 sys.exit(-1)
00161
00162 parser=OstOptionParser(usage=usage,conflict_handler="resolve")
00163 parser.add_option("-h", "--help", action="callback", callback=show_help, help="show this help message and exit")
00164 parser.add_option("-v", "--verbosity_level", action="store", type="int", dest="vlevel", default=2,
00165 help="sets the verbosity level [default: %default]")
00166 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")
00167 parser.add_option("-p", "--pdb_id", dest="pdb_ids", default=[],action="append", help="PDB file ID. The file will be retrieved from PDB")
00168 parser.add_option("-b", "--processor", dest="processor", default="HEURISTIC", help="Type of processor used by the progam (either RULE_BASED or HEURISTIC) [default: %default]")
00169 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]")
00170 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]")
00171 parser.add_option("-S","--stereo", dest="try_stereo", default=False, action="store_true",help="try to get a quad-buffer stereo visual")
00172 parser.disable_interspersed_args()
00173 (options, args) = parser.parse_args()
00174
00175 if len(parser.rargs)!=0:
00176 for rargs_string in parser.rargs:
00177 if not rargs_string.endswith('.py'):
00178 loading_list.append(rargs_string)
00179 else:
00180 print 'Error: one of the files to load is a Python script, use -s flag to execute it\n'
00181 QtGui.QApplication.instance().exit()
00182 sys.exit(-1)
00183
00184 if len(options.script)!=0:
00185 script_argv=options.script
00186
00187 if options.processor=="RULE_BASED":
00188 from ost import conop
00189 if os.path.exists(options.complib):
00190 compound_lib=conop.CompoundLib.Load(options.complib)
00191 conop.SetDefaultLib(compound_lib)
00192 io.profiles['DEFAULT'].processor = conop.RuleBasedProcessor(compound_lib)
00193
00194 home = os.getenv('HOME') or os.getenv('USERPROFILE')
00195 _ostrc=os.path.join(home, '.ostrc')
00196 if os.path.exists(_ostrc):
00197 try:
00198 exec(open(_ostrc))
00199 except Exception, e:
00200 print e
00201 else:
00202 rcfile=open(_ostrc,"w")
00203 print >> rcfile, '# This python file is parsed by ost and dng at startup'
00204 print >> rcfile, '# Its content is made available in the global namespace'
00205 print >> rcfile, '# It can be used to define custom variables and functions'
00206 print >> rcfile, '# For example:'
00207 print >> rcfile, '# IMPORTANT_DIR="path/to/important/dir"'
00208 rcfile.close()
00209
00210 ost.gui.PushVerbosityLevel(options.vlevel)
00211 working_dir=settings.GetValue("DNG_WORKING_DIR",None)
00212
00213 if working_dir != None and os.path.isdir(working_dir):
00214 os.chdir(working_dir)
00215
00216 _InitFrontEnd(options.try_stereo)
00217
00218 if len(loading_list)!=0 or len(options.pdb_ids)!=0:
00219 _load_files()
00220 gfx.Scene().Autoslab()
00221 if len(script_argv)!=0:
00222 _execute_script()