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-2006  Edward d'Auvergne                                  # 
 16  # Copyright (C) 2006-2014  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  # Dependency checks. 
 37  import dep_check 
 38   
 39  # Eliminate the ^[[?1034h escape code being produced on Linux systems by the import of the readline module. 
 40  import os 
 41  if 'TERM' in os.environ and os.environ['TERM'] == 'xterm': 
 42      os.environ['TERM'] = 'linux' 
 43   
 44  # Set up the Python 2 and 3 work-arounds. 
 45  import lib.compat 
 46   
 47  # Python modules. 
 48  import numpy 
 49  from optparse import Option, OptionGroup, OptionParser 
 50  from os import F_OK, access, getpid, putenv 
 51  if dep_check.cprofile_module: 
 52      import cProfile as profile 
 53  elif dep_check.profile_module: 
 54      import profile 
 55  import pstats 
 56  from pydoc import pager 
 57  import sys 
 58   
 59  # relax modules. 
 60  from info import Info_box 
 61  import lib.errors 
 62  from lib.io import io_streams_log, io_streams_tee 
 63  import lib.warnings 
 64  from multi import Application_callback, load_multiprocessor 
 65  from prompt import interpreter 
 66  from status import Status; status = Status() 
 67  import user_functions 
 68  import version 
 69   
 70   
 71  # Set up the user functions. 
 72  user_functions.initialise() 
 73   
 74  # Modify the environmental variables. 
 75  putenv('PDBVIEWER', 'vmd') 
 76   
 77   
78 -def start(mode=None, profile_flag=False):
79 """Execute relax. 80 81 @keyword mode: Force a relax mode, overriding the command line. 82 @type mode: str 83 @keyword profile_flag: Change this flag to True for code profiling. 84 @type profile_flag: bool 85 """ 86 87 # Normal relax operation. 88 relax = Relax() 89 90 # Override normal operation. 91 if mode: 92 # Override the mode. 93 relax.mode = mode 94 95 # Some defaults. 96 relax.script_file = None 97 relax.log_file = None 98 relax.tee_file = None 99 relax.multiprocessor_type = 'uni' 100 relax.n_processors = 1 101 102 # Process the command line arguments. 103 else: 104 relax.arguments() 105 106 # Store some start up info in the status object. 107 status.relax_mode = relax.mode 108 109 # Set up the multi-processor elements. 110 callbacks = Application_callback(master=relax) 111 verbosity = 0 112 if status.debug: 113 verbosity = 1 114 processor = load_multiprocessor(relax.multiprocessor_type, callbacks, processor_size=relax.n_processors, verbosity=verbosity) 115 116 # Place the processor fabric intro string into the info box. 117 info = Info_box() 118 info.multi_processor_string = processor.get_intro_string() 119 120 # Normal relax operation. 121 if not profile_flag: 122 # Execute relax in multi-processor mode (this includes the uni-processor for normal operation). 123 processor.run() 124 125 # relax in profiling mode. 126 else: 127 def print_stats(stats, status=0): 128 pstats.Stats(stats).sort_stats('time', 'name').print_stats()
129 130 # No profile module. 131 if not dep_check.profile_module: 132 sys.stderr.write("The profile module is not available, please install the Python development packages for profiling.\n\n") 133 sys.exit() 134 135 # Run relax in profiling mode. 136 profile.Profile.print_stats = print_stats 137 profile.runctx('processor.run()', globals(), locals()) 138 139 140
141 -class Relax:
142 """The main relax class. 143 144 This contains information about the running state, for example the mode of operation of relax, 145 whether debugging is turned on, etc. 146 """ 147
148 - def __init__(self):
149 """The top level class for initialising the program. 150 151 @keyword mode: Force a relax mode, overriding the command line. 152 @type mode: str 153 """ 154 155 # Get and store the PID of this process. 156 self.pid = getpid()
157 158
159 - def run(self):
160 """Execute relax. 161 162 This is the application callback method executed by the multi-processor framework. 163 """ 164 165 # Set up the warning system. 166 lib.warnings.setup() 167 168 # Logging. 169 if self.log_file: 170 io_streams_log(self.log_file) 171 172 # Tee. 173 elif self.tee_file: 174 io_streams_tee(self.tee_file) 175 176 # Show the version number and exit. 177 if self.mode == 'version': 178 print('relax ' + version.version_full()) 179 return 180 181 # Show the relax info and exit. 182 if self.mode == 'info': 183 # Initialise the information box. 184 info = Info_box() 185 186 # Print the program intro. 187 print(info.intro_text()) 188 189 # Print the system info. 190 print(info.sys_info()) 191 192 # Stop execution. 193 return 194 195 # Run the interpreter for the prompt or script modes. 196 if self.mode == 'prompt' or self.mode == 'script': 197 # Run the interpreter. 198 self.interpreter = interpreter.Interpreter() 199 self.interpreter.run(self.script_file) 200 201 # Execute the relax GUI. 202 elif self.mode == 'gui': 203 # Dependency check. 204 if not dep_check.wx_module: 205 sys.stderr.write("Please install the wx Python module to access the relax GUI.\n\n") 206 return 207 208 # Only import the module in this mode (to improve program start up speeds). 209 import gui 210 211 # Set the GUI flag in the status object. 212 status.show_gui = True 213 214 # Start the relax GUI wx application. 215 app = gui.App(script_file=self.script_file) 216 app.MainLoop() 217 218 # Execute the relax test suite 219 elif self.mode == 'test suite': 220 # Only import the module in the test modes (to improve program start up speeds). 221 from test_suite.test_suite_runner import Test_suite_runner 222 223 # Load the interpreter and turn intros on. 224 self.interpreter = interpreter.Interpreter(show_script=False, raise_relax_error=True) 225 self.interpreter.on() 226 227 # Run the tests. 228 runner = Test_suite_runner(self.tests, timing=self.test_timings) 229 runner.run_all_tests() 230 231 # Execute the relax system tests. 232 elif self.mode == 'system tests': 233 # Only import the module in the test modes (to improve program start up speeds). 234 from test_suite.test_suite_runner import Test_suite_runner 235 236 # Load the interpreter and turn intros on. 237 self.interpreter = interpreter.Interpreter(show_script=False, raise_relax_error=True) 238 self.interpreter.on() 239 240 # Run the tests. 241 runner = Test_suite_runner(self.tests, timing=self.test_timings) 242 runner.run_system_tests() 243 244 # Execute the relax unit tests. 245 elif self.mode == 'unit tests': 246 # Only import the module in the test modes (to improve program start up speeds). 247 from test_suite.test_suite_runner import Test_suite_runner 248 249 # Run the tests. 250 runner = Test_suite_runner(self.tests, timing=self.test_timings) 251 runner.run_unit_tests() 252 253 # Execute the relax GUI tests. 254 elif self.mode == 'GUI tests': 255 # Only import the module in the test modes (to improve program start up speeds). 256 from test_suite.test_suite_runner import Test_suite_runner 257 258 # Run the tests. 259 runner = Test_suite_runner(self.tests, timing=self.test_timings) 260 runner.run_gui_tests() 261 262 # Execute the relax verification tests. 263 elif self.mode == 'verification tests': 264 # Only import the module in the test modes (to improve program start up speeds). 265 from test_suite.test_suite_runner import Test_suite_runner 266 267 # Run the tests. 268 runner = Test_suite_runner(self.tests, timing=self.test_timings) 269 runner.run_verification_tests() 270 271 # Test mode. 272 elif self.mode == 'test': 273 self.test_mode() 274 275 # Licence mode. 276 elif self.mode == 'licence': 277 self.licence() 278 279 # Unknown mode. 280 else: 281 raise lib.errors.RelaxError("The '%s' mode is unknown." % self.mode)
282 283
284 - def arguments(self):
285 """Process the command line arguments.""" 286 287 # Parser object. 288 parser = RelaxParser(self, usage="usage: %prog [options] [script_file]") 289 290 # Recognised command line options for the UI. 291 group = OptionGroup(parser, 'UI options') 292 group.add_option('-p', '--prompt', action='store_true', dest='prompt', default=0, help='launch relax in prompt mode after running any optionally supplied scripts') 293 group.add_option('-g', '--gui', action='store_true', dest='gui', default=0, help='launch the relax GUI') 294 group.add_option('-i', '--info', action='store_true', dest='info', default=0, help='display information about this version of relax') 295 group.add_option('-v', '--version', action='store_true', dest='version', default=0, help='show the version number and exit') 296 group.add_option('--licence', action='store_true', dest='licence', default=0, help='display the licence') 297 group.add_option('--test', action='store_true', dest='test', default=0, help='run relax in test mode') 298 parser.add_option_group(group) 299 300 # Recognised command line options for the multiprocessor. 301 group = OptionGroup(parser, 'Multi-processor options') 302 group.add_option('-m', '--multi', action='store', type='string', dest='multiprocessor', default='uni', help='set multi processor method') 303 group.add_option('-n', '--processors', action='store', type='int', dest='n_processors', default=-1, help='set number of processors (may be ignored)') 304 parser.add_option_group(group) 305 306 # Recognised command line options for IO redirection. 307 group = OptionGroup(parser, 'IO redirection options') 308 group.add_option('-l', '--log', action='store', type='string', dest='log', help='log relax output to the file LOG_FILE', metavar='LOG_FILE') 309 group.add_option('-t', '--tee', action='store', type='string', dest='tee', help='tee relax output to both stdout and the file LOG_FILE', metavar='LOG_FILE') 310 parser.add_option_group(group) 311 312 # Recognised command line options for the test suite. 313 group = OptionGroup(parser, 'Test suite options') 314 group.add_option('-x', '--test-suite', action='store_true', dest='test_suite', default=0, help='execute the full relax test suite') 315 group.add_option('-s', '--system-tests', action='store_true', dest='system_tests', default=0, help='execute the system/functional tests') 316 group.add_option('-u', '--unit-tests', action='store_true', dest='unit_tests', default=0, help='execute the unit tests') 317 group.add_option('--gui-tests', action='store_true', dest='gui_tests', default=0, help='execute the GUI tests') 318 group.add_option('--verification-tests', action='store_true', dest='verification_tests', default=0, help='execute the software verification tests') 319 group.add_option('--time', action='store_true', dest='tt', default=0, help='enable the timing of individual tests in the test suite') 320 parser.add_option_group(group) 321 322 # Recognised command line options for debugging. 323 group = OptionGroup(parser, 'Debugging options') 324 group.add_option('-d', '--debug', action='store_true', dest='debug', default=0, help='enable debugging output') 325 group.add_option('--error-state', action='store_true', dest='error_state', default=0, help='save a pickled state file when a RelaxError occurs') 326 group.add_option('--traceback', action='store_true', dest='traceback', default=0, help='show stack tracebacks on all RelaxErrors and RelaxWarnings') 327 group.add_option('-e', '--escalate', action='store_true', dest='escalate', default=0, help='escalate all warnings to errors') 328 group.add_option('--numpy-raise', action='store_true', dest='numpy_raise', default=0, help='convert numpy warnings to errors') 329 parser.add_option_group(group) 330 331 # Parse the options. 332 (options, args) = parser.parse_args() 333 334 # Debugging options: Debugging flag, escalate flag, traceback flag, and numpy warning to error conversion. 335 if options.debug: 336 status.debug = True 337 if options.escalate: 338 lib.warnings.ESCALATE = True 339 if options.traceback: 340 status.traceback = True 341 lib.warnings.TRACEBACK = True 342 if options.numpy_raise: 343 numpy.seterr(all='raise') 344 if options.error_state: 345 lib.errors.SAVE_ERROR_STATE = True 346 347 # Script prompt interactive inspection flag. 348 if options.prompt: 349 status.prompt = True 350 351 # Logging. 352 if options.log: 353 # Exclusive modes. 354 if options.tee: 355 parser.error("the logging and tee options cannot be set simultaneously") 356 357 # The log file. 358 self.log_file = options.log 359 360 # Fail if the file already exists. 361 if access(self.log_file, F_OK): 362 parser.error("the log file " + repr(self.log_file) + " already exists") 363 else: 364 self.log_file = None 365 366 # Tee. 367 if options.tee: 368 # Exclusive modes. 369 if options.log: 370 parser.error("the tee and logging options cannot be set simultaneously") 371 372 # The tee file. 373 self.tee_file = options.tee 374 375 # Fail if the file already exists. 376 if access(self.tee_file, F_OK): 377 parser.error("the tee file " + repr(self.tee_file) + " already exists") 378 else: 379 self.tee_file = None 380 381 # Test suite mode, therefore the args are the tests to run and not a script file. 382 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests or options.verification_tests: 383 # Store the arguments. 384 self.tests = args 385 386 # Test timings. 387 self.test_timings = False 388 if options.tt: 389 self.test_timings = True 390 391 # The argument is a script. 392 else: 393 # Number of positional arguments should only be 0 or 1. 1 should be the script file. 394 if len(args) > 1: 395 parser.error("incorrect number of arguments") 396 397 # Script file. 398 self.script_file = None 399 if len(args) == 1: 400 self.script_file = args[0] 401 402 # Test if the script file exists. 403 if not access(self.script_file, F_OK): 404 parser.error("the script file " + repr(self.script_file) + " does not exist") 405 406 # Set the multi-processor type and number. 407 self.multiprocessor_type = options.multiprocessor 408 self.n_processors = options.n_processors 409 410 # Checks for the multiprocessor mode. 411 if self.multiprocessor_type == 'mpi4py' and not dep_check.mpi4py_module: 412 parser.error(dep_check.mpi4py_message) 413 414 415 # Determine the relax mode and test for mutually exclusive modes. 416 ################################################################# 417 418 # Show the version number. 419 if options.version: 420 self.mode = 'version' 421 422 # Show the info about this relax version. 423 elif options.info: 424 self.mode = 'info' 425 426 # Run the relax tests. 427 elif options.test_suite or options.system_tests or options.unit_tests or options.gui_tests or options.verification_tests: 428 # Exclusive modes. 429 if options.test: 430 parser.error("executing the relax test suite and running relax in test mode are mutually exclusive") 431 elif options.licence: 432 parser.error("executing the relax test suite and running relax in licence mode are mutually exclusive") 433 434 # Set the mode. 435 if options.test_suite: 436 self.mode = 'test suite' 437 elif options.system_tests: 438 self.mode = 'system tests' 439 elif options.unit_tests: 440 self.mode = 'unit tests' 441 elif options.gui_tests: 442 self.mode = 'GUI tests' 443 elif options.verification_tests: 444 self.mode = 'verification tests' 445 446 # Set the status flag. 447 status.test_mode = True 448 449 # Test mode. 450 elif options.test: 451 # Make sure no script is supplied. 452 if self.script_file: 453 parser.error("a script should not be supplied in test mode") 454 455 # Exclusive modes. 456 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests or options.verification_tests: 457 parser.error("the relax test mode and executing the test suite are mutually exclusive") 458 elif options.licence: 459 parser.error("the relax modes test and licence are mutually exclusive") 460 461 # Set the mode. 462 self.mode = 'test' 463 464 # Licence mode. 465 elif options.licence: 466 # Make sure no script is supplied. 467 if self.script_file: 468 parser.error("a script should not be supplied in test mode") 469 470 # Exclusive modes. 471 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests or options.verification_tests: 472 parser.error("the relax licence mode and executing the test suite are mutually exclusive") 473 elif options.test: 474 parser.error("the relax modes licence and test are mutually exclusive") 475 476 # Set the mode. 477 self.mode = 'licence' 478 479 # GUI. 480 elif options.gui: 481 # Exclusive models. 482 if options.test_suite or options.system_tests or options.unit_tests or options.gui_tests or options.verification_tests: 483 parser.error("the relax GUI mode and testing modes are mutually exclusive") 484 elif options.licence: 485 parser.error("the relax GUI mode and licence mode are mutually exclusive") 486 487 # Missing wx module. 488 if not dep_check.wx_module: 489 # Not installed. 490 if dep_check.wx_module_message == "No module named 'wx'": 491 parser.error("To use the GUI, the wxPython module must be installed.") 492 493 # Broken. 494 else: 495 parser.error("The wxPython installation is broken:\n%s." % dep_check.wx_module_message) 496 497 # Set the mode. 498 self.mode = 'gui' 499 500 # Script mode. 501 elif self.script_file: 502 self.mode = 'script' 503 504 # Prompt mode (default). 505 else: 506 self.mode = 'prompt'
507 508
509 - def licence(self):
510 """Function for displaying the licence.""" 511 512 # Present the GPL using paging. 513 file = open('docs/COPYING') 514 pager(file.read())
515 516
517 - def test_mode(self):
518 """Relax test mode code.""" 519 520 # Don't actually do anything. 521 return
522 523 524
525 -class RelaxParser(OptionParser):
526 - 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):
527 """Subclassed OptionParser class with a replacement error function.""" 528 529 # Relax base class. 530 self.relax = relax 531 532 # Run the __init__ method of the OptionParser class. 533 OptionParser.__init__(self, usage, option_list, option_class, version, conflict_handler, description, formatter, add_help_option, prog)
534 535
536 - def error(self, message):
537 """Replacement error function.""" 538 539 # Usage message. 540 self.print_usage(sys.stderr) 541 542 # Raise a clean error. 543 try: 544 raise lib.errors.RelaxError(message) 545 except lib.errors.AllRelaxErrors: 546 instance = sys.exc_info()[1] 547 sys.stderr.write(instance.__str__()) 548 549 # Exit. 550 sys.exit()
551 552 553 # Start relax if this file is passed to Python. 554 if __name__ == "__main__": 555 start() 556