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