Module relax
[hide private]
[frames] | no frames]

Source Code for Module relax

  1  ############################################################################### 
  2  #                                                                             # 
  3  #                                    relax                                    # 
  4  #                                                                             # 
  5  #               Protein dynamics by NMR relaxation data analysis              # 
  6  #                                                                             # 
  7  #                             by Edward d'Auvergne                            # 
  8  #                                                                             # 
  9  ############################################################################### 
 10  #                                                                             # 
 11  #                                   Licence                                   # 
 12  #                                                                             # 
 13  # relax, a program for relaxation data analysis.                              # 
 14  #                                                                             # 
 15  # Copyright (C) 2001-2013  Edward d'Auvergne                                  # 
 16  # Copyright (C) 2006-2013  the relax development team                         # 
 17  #                                                                             # 
 18  # This program is free software; you can redistribute it and/or modify        # 
 19  # it under the terms of the GNU General Public License as published by        # 
 20  # the Free Software Foundation, either version 3 of the License, or           # 
 21  # (at your option) any later version.                                         # 
 22  #                                                                             # 
 23  # This program is distributed in the hope that it will be useful,             # 
 24  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 25  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 26  # GNU Library General Public License for more details.                        # 
 27  #                                                                             # 
 28  # You should have received a copy of the GNU General Public License           # 
 29  # along with this program; if not, write to the Free Software                 # 
 30  #                                                                             # 
 31  ############################################################################### 
 32   
 33  # Module docstring. 
 34  """The main module for relax execution.""" 
 35   
 36  # Set up the Python 2 and 3 work-arounds. 
 37  import compat 
 38   
 39  # Dependency checks. 
 40  import dep_check 
 41   
 42  # Python modules. 
 43  from optparse import Option, OptionParser 
 44  from os import F_OK, access, getpid, putenv 
 45  if dep_check.profile_module: 
 46      import profile 
 47  import pstats 
 48  from pydoc import pager 
 49  from re import match 
 50  import sys 
 51   
 52  # relax modules. 
 53  from info import Info_box 
 54  from multi import Application_callback, load_multiprocessor 
 55  from prompt import interpreter 
 56  import relax_errors 
 57  from relax_io import io_streams_log, io_streams_tee 
 58  import relax_warnings 
 59  from status import Status; status = Status() 
 60  import version 
 61   
 62  # Modify the environmental variables. 
 63  putenv('PDBVIEWER', 'vmd') 
 64   
 65   
66 -def start(mode=None, profile_flag=False):
67 """Execute relax. 68 69 @keyword mode: Force a relax mode, overriding the command line. 70 @type mode: str 71 @keyword profile_flag: Change this flag to True for code profiling. 72 @type profile_flag: bool 73 """ 74 75 # Normal relax operation. 76 relax = Relax() 77 78 # Override normal operation. 79 if mode: 80 # Override the mode. 81 relax.mode = mode 82 83 # Some defaults. 84 relax.script_file = None 85 relax.log_file = None 86 relax.tee_file = None 87 relax.multiprocessor_type = 'uni' 88 relax.n_processors = 1 89 90 # Process the command line arguments. 91 else: 92 relax.arguments() 93 94 # Store some start up info in the status object. 95 status.relax_mode = relax.mode 96 97 # Set up the multi-processor elements. 98 callbacks = Application_callback(master=relax) 99 verbosity = 0 100 if status.debug: 101 verbosity = 1 102 processor = load_multiprocessor(relax.multiprocessor_type, callbacks, processor_size=relax.n_processors, verbosity=verbosity) 103 104 # Place the processor fabric intro string into the info box. 105 info = Info_box() 106 info.multi_processor_string = processor.get_intro_string() 107 108 # Normal relax operation. 109 if not profile_flag: 110 # Execute relax in multi-processor mode (this includes the uni-processor for normal operation). 111 processor.run() 112 113 # relax in profiling mode. 114 else: 115 def print_stats(stats, status=0): 116 pstats.Stats(stats).sort_stats('cumulative', 'name').print_stats()
117 118 # No profile module. 119 if not dep_check.profile_module: 120 sys.stderr.write("The profile module is not available, please install the Python development packages for profiling.\n\n") 121 sys.exit() 122 123 # Run relax in profiling mode. 124 profile.Profile.print_stats = print_stats 125 profile.runctx('processor.run()', globals(), locals()) 126 127 128
129 -class Relax:
130 """The main relax class. 131 132 This contains information about the running state, for example the mode of operation of relax, 133 whether debugging is turned on, etc. 134 """ 135
136 - def __init__(self):
137 """The top level class for initialising the program. 138 139 @keyword mode: Force a relax mode, overriding the command line. 140 @type mode: str 141 """ 142 143 # Get and store the PID of this process. 144 self.pid = getpid()
145 146
147 - def run(self):
148 """Execute relax. 149 150 This is the application callback method executed by the multi-processor framework. 151 """ 152 153 # Set up the warning system. 154 relax_warnings.setup() 155 156 # Show the version number and exit. 157 if self.mode == 'version': 158 print('relax ' + version.version_full()) 159 return 160 161 # Show the relax info and exit. 162 if self.mode == 'info': 163 # Initialise the information box. 164 info = Info_box() 165 166 # Print the program intro. 167 print(info.intro_text()) 168 169 # Print the system info. 170 print(info.sys_info()) 171 172 # Stop execution. 173 return 174 175 # Logging. 176 if self.log_file: 177 io_streams_log(self.log_file) 178 179 # Tee. 180 elif self.tee_file: 181 io_streams_tee(self.tee_file) 182 183 # Run the interpreter for the prompt or script modes. 184 if self.mode == 'prompt' or self.mode == 'script': 185 # Run the interpreter. 186 self.interpreter = interpreter.Interpreter() 187 self.interpreter.run(self.script_file) 188 189 # Execute the relax GUI. 190 elif self.mode == 'gui': 191 # Dependency check. 192 if not dep_check.wx_module: 193 sys.stderr.write("Please install the wx Python module to access the relax GUI.\n\n") 194 return 195 196 # Only import the module in this mode (to improve program start up speeds). 197 import gui 198 199 # Set the GUI flag in the status object. 200 status.show_gui = True 201 202 # Start the relax GUI wx application. 203 app = gui.App(script_file=self.script_file) 204 app.MainLoop() 205 206 # Execute the relax test suite 207 elif self.mode == 'test suite': 208 # Only import the module in the test modes (to improve program start up speeds). 209 from test_suite.test_suite_runner import Test_suite_runner 210 211 # Load the interpreter and turn intros on. 212 self.interpreter = interpreter.Interpreter(show_script=False, quit=False, raise_relax_error=True) 213 self.interpreter.on() 214 215 # Run the tests. 216 runner = Test_suite_runner(self.tests) 217 runner.run_all_tests() 218 219 # Execute the relax system tests. 220 elif self.mode == 'system tests': 221 # Only import the module in the test modes (to improve program start up speeds). 222 from test_suite.test_suite_runner import Test_suite_runner 223 224 # Load the interpreter and turn intros on. 225 self.interpreter = interpreter.Interpreter(show_script=False, quit=False, raise_relax_error=True) 226 self.interpreter.on() 227 228 # Run the tests. 229 runner = Test_suite_runner(self.tests) 230 runner.run_system_tests() 231 232 # Execute the relax unit tests. 233 elif self.mode == 'unit tests': 234 # Only import the module in the test modes (to improve program start up speeds). 235 from test_suite.test_suite_runner import Test_suite_runner 236 237 # Run the tests. 238 runner = Test_suite_runner(self.tests) 239 runner.run_unit_tests() 240 241 # Execute the relax GUI tests. 242 elif self.mode == 'GUI tests': 243 # Only import the module in the test modes (to improve program start up speeds). 244 from test_suite.test_suite_runner import Test_suite_runner 245 246 # Run the tests. 247 runner = Test_suite_runner(self.tests) 248 runner.run_gui_tests() 249 250 # Test mode. 251 elif self.mode == 'test': 252 self.test_mode() 253 254 # Licence mode. 255 elif self.mode == 'licence': 256 self.licence() 257 258 # Unknown mode. 259 else: 260 raise relax_errors.RelaxError("The '%s' mode is unknown." % self.mode)
261 262
263 - def arguments(self):
264 """Process the command line arguments.""" 265 266 # Parser object. 267 parser = RelaxParser(self, usage="usage: %prog [options] [script_file]") 268 269 # Recognised command line options. 270 parser.add_option('-d', '--debug', action='store_true', dest='debug', default=0, help='enable debugging output') 271 parser.add_option('-l', '--log', action='store', type='string', dest='log', help='log relax output to the file LOG_FILE', metavar='LOG_FILE') 272 parser.add_option('--licence', action='store_true', dest='licence', default=0, help='display the licence') 273 parser.add_option('-t', '--tee', action='store', type='string', dest='tee', help='tee relax output to stdout and the file LOG_FILE', metavar='LOG_FILE') 274 parser.add_option('-g', '--gui', action='store_true', dest='gui', default=0, help='launch the relax GUI') 275 parser.add_option('-p', '--pedantic', action='store_true', dest='pedantic', default=0, help='escalate all warnings to errors') 276 parser.add_option('--test', action='store_true', dest='test', default=0, help='run relax in test mode') 277 parser.add_option('-x', '--test-suite', action='store_true', dest='test_suite', default=0, help='execute the relax test suite') 278 parser.add_option('-s', '--system-tests', action='store_true', dest='system_tests', default=0, help='execute the relax system/functional tests (part of the test suite)') 279 parser.add_option('-u', '--unit-tests', action='store_true', dest='unit_tests', default=0, help='execute the relax unit tests (part of the test suite)') 280 parser.add_option('--gui-tests', action='store_true', dest='gui_tests', default=0, help='execute the relax GUI tests (part of the test suite)') 281 parser.add_option('-i', '--info', action='store_true', dest='info', default=0, help='display information about this version of relax') 282 parser.add_option('-v', '--version', action='store_true', dest='version', default=0, help='show the version number and exit') 283 parser.add_option('-m', '--multi', action='store', type='string', dest='multiprocessor', default='uni', help='set multi processor method') 284 parser.add_option('-n', '--processors', action='store', type='int', dest='n_processors', default=-1, help='set number of processors (may be ignored)') 285 286 # Parse the options. 287 (options, args) = parser.parse_args() 288 289 # Debugging flag. 290 if options.debug: 291 status.debug = True 292 293 # Pedantic flag. 294 if options.pedantic: 295 status.pedantic = True 296 297 # Logging. 298 if options.log: 299 # Exclusive modes. 300 if options.tee: 301 parser.error("the logging and tee options cannot be set simultaneously") 302 303 # The log file. 304 self.log_file = options.log 305 306 # Fail if the file already exists. 307 if access(self.log_file, F_OK): 308 parser.error("the log file " + repr(self.log_file) + " already exists") 309 else: 310 self.log_file = None 311 312 # Tee. 313 if options.tee: 314 # Exclusive modes. 315 if options.log: 316 parser.error("the tee and logging options cannot be set simultaneously") 317 318 # The tee file. 319 self.tee_file = options.tee 320 321 # Fail if the file already exists. 322 if access(self.tee_file, F_OK): 323 parser.error("the tee file " + repr(self.tee_file) + " already exists") 324 else: 325 self.tee_file = None 326 327 # Test suite mode, therefore the args are the tests to run and not a script file. 328 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 329 self.tests = args 330 331 # The argument is a script. 332 else: 333 # Number of positional arguments should only be 0 or 1. 1 should be the script file. 334 if len(args) > 1: 335 parser.error("incorrect number of arguments") 336 337 # Script file. 338 self.script_file = None 339 if len(args) == 1: 340 self.script_file = args[0] 341 342 # Test if the script file exists. 343 if not access(self.script_file, F_OK): 344 parser.error("the script file " + repr(self.script_file) + " does not exist") 345 346 # Set the multi-processor type and number. 347 self.multiprocessor_type = options.multiprocessor 348 self.n_processors = options.n_processors 349 350 # Checks for the multiprocessor mode. 351 if self.multiprocessor_type == 'mpi4py' and not dep_check.mpi4py_module: 352 parser.error(dep_check.mpi4py_message) 353 354 355 # Determine the relax mode and test for mutually exclusive modes. 356 ################################################################# 357 358 # Show the version number. 359 if options.version: 360 self.mode = 'version' 361 362 # Show the info about this relax version. 363 elif options.info: 364 self.mode = 'info' 365 366 # Run the relax tests. 367 elif options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 368 # Exclusive modes. 369 if options.test: 370 parser.error("executing the relax test suite and running relax in test mode are mutually exclusive") 371 elif options.licence: 372 parser.error("executing the relax test suite and running relax in licence mode are mutually exclusive") 373 374 # Set the mode. 375 if options.test_suite: 376 self.mode = 'test suite' 377 elif options.system_tests: 378 self.mode = 'system tests' 379 elif options.unit_tests: 380 self.mode = 'unit tests' 381 elif options.gui_tests: 382 self.mode = 'GUI tests' 383 384 # Set the status flag. 385 status.test_mode = True 386 387 # Test mode. 388 elif options.test: 389 # Make sure no script is supplied. 390 if self.script_file: 391 parser.error("a script should not be supplied in test mode") 392 393 # Exclusive modes. 394 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 395 parser.error("the relax test mode and executing the test suite are mutually exclusive") 396 elif options.licence: 397 parser.error("the relax modes test and licence are mutually exclusive") 398 399 # Set the mode. 400 self.mode = 'test' 401 402 # Licence mode. 403 elif options.licence: 404 # Make sure no script is supplied. 405 if self.script_file: 406 parser.error("a script should not be supplied in test mode") 407 408 # Exclusive modes. 409 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 410 parser.error("the relax licence mode and executing the test suite are mutually exclusive") 411 elif options.test: 412 parser.error("the relax modes licence and test are mutually exclusive") 413 414 # Set the mode. 415 self.mode = 'licence' 416 417 # GUI. 418 elif options.gui: 419 # Exclusive models. 420 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests: 421 parser.error("the relax GUI mode and testing modes are mutually exclusive") 422 elif options.licence: 423 parser.error("the relax GUI mode and licence mode are mutually exclusive") 424 425 # Missing wx module. 426 if not dep_check.wx_module: 427 parser.error("To use the GUI, the wx python module must be installed.") 428 429 # Set the mode. 430 self.mode = 'gui' 431 432 # Script mode. 433 elif self.script_file: 434 self.mode = 'script' 435 436 # Prompt mode (default). 437 else: 438 self.mode = 'prompt'
439 440
441 - def licence(self):
442 """Function for displaying the licence.""" 443 444 # Present the GPL using paging. 445 file = open('docs/COPYING') 446 pager(file.read())
447 448
449 - def test_mode(self):
450 """Relax test mode code.""" 451 452 # Don't actually do anything. 453 return
454 455 456
457 -class RelaxParser(OptionParser):
458 - def __init__(self, relax, usage=None, option_list=None, option_class=Option, version=None, conflict_handler="error", description=None, formatter=None, add_help_option=1, prog=None):
459 """Subclassed OptionParser class with a replacement error function.""" 460 461 # Relax base class. 462 self.relax = relax 463 464 # Run the __init__ method of the OptionParser class. 465 OptionParser.__init__(self, usage, option_list, option_class, version, conflict_handler, description, formatter, add_help_option, prog)
466 467
468 - def error(self, message):
469 """Replacement error function.""" 470 471 # Usage message. 472 self.print_usage(sys.stderr) 473 474 # Raise a clean error. 475 try: 476 raise relax_errors.RelaxError(message) 477 except relax_errors.AllRelaxErrors: 478 instance = sys.exc_info()[1] 479 sys.stderr.write(instance.__str__()) 480 481 # Exit. 482 sys.exit()
483 484 485 # Start relax if this file is passed to Python. 486 if __name__ == "__main__": 487 start() 488