File: __init__.py

package info (click to toggle)
pymol 2.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 43,400 kB
  • sloc: cpp: 482,465; python: 81,253; ansic: 27,428; sh: 94; makefile: 32; csh: 8
file content (626 lines) | stat: -rw-r--r-- 18,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
'''
PyMOL Molecular Graphics System
Copyright (c) Schrodinger, Inc.

Supported ways to launch PyMOL:

  If $PYMOL_PATH is a non-default location, it must be set and exported
  before launching PyMOL.

  From a terminal:

    shell> python /path/to/pymol/__init__.py [args]

  From a python main thread:

    >>> # with GUI
    >>> import pymol
    >>> pymol.finish_launching()

    >>> # without GUI
    >>> import pymol
    >>> pymol.finish_launching(['pymol', '-cq'])

'''

from __future__ import absolute_import
from __future__ import print_function

import os
import sys
import __main__

if __name__ == '__main__':

    # PyMOL launched as "python pymol/__init__.py"
    # or via execfile(".../pymol/__init__.py",...) from main
    # or as "python -m pymol.__init__"

    if 'pymol' not in sys.modules:
        # "python /abc/pymol/__init__.py" will add /abc/pymol to PYTHONPATH
        # (we don't want that), but not /abc and not the current directory (we
        # want those)

        pymol_base = os.path.dirname(os.path.realpath(__file__))
        site_packages = os.path.dirname(pymol_base)

        # remove /abc/pymol
        if pymol_base in sys.path:
            sys.path.remove(pymol_base)

        # add /abc
        if site_packages not in sys.path:
            sys.path.insert(0, site_packages)

        # add current directory
        if '' not in sys.path:
            sys.path.insert(0, '')

    # arguments default to sys.argv... but also support execfile(...)
    # from a terminal where the user could set pymol_argv
    args = getattr(__main__, "pymol_argv", None)

    # standard launch (consume main thread)
    import pymol
    pymol.launch(args)

    # this should never be reached because PyMOL will exit the process
    raise SystemExit

IS_PY2 = sys.version_info[0] == 2
IS_PY3 = sys.version_info[0] == 3
IS_WINDOWS = sys.platform.startswith('win')
IS_MACOS = sys.platform.startswith('darwin')
IS_LINUX = sys.platform.startswith('linux')

if IS_PY3:
    # legacy string API, still used by Pmw for example
    import string
    for attr in ['capitalize', 'count', 'find', 'index', 'lower',
            'replace', 'rstrip', 'split', 'strip', 'upper', 'zfill']:
        setattr(string, attr, getattr(str, attr))
    string.letters      = string.ascii_letters
    string.lowercase    = string.ascii_lowercase
    string.join = lambda words, sep=' ': sep.join(words)
    string.atoi = int

    import _thread as thread
else:
    import thread

from imp import find_module
import copy
import threading
import re
import time
import traceback
import math

from . import invocation

def _init_internals(_pymol):

    # Create a temporary object "stored" in the PyMOL global namespace
    # for usage with evaluate based-commands such as alter

    _pymol.stored = Scratch_Storage()

    # Create a permanent object in the PyMOL global namespace
    # that will be picked and unpickled along with the session

    _pymol.session = Session_Storage()

    # This global will be non-None if logging is active
    # (global variable used for efficiency)

    _pymol._log_file = None

    # This global will be non-None if an external gui
    # exists. It mainly exists so that events which occur
    # in the Python thread can be handed off to the
    # external GUI thread through one or more FIFO Queues
    # (global variable used for efficiency)

    _pymol._ext_gui = None

    # lists of functions to call when saving and restoring pymol session objects
    # The entry 'None' represents the PyMOL C-API function call

    _pymol._session_save_tasks = [ None ]
    _pymol._session_restore_tasks = [ None ]

    # cached results (as a list):
    # [ [size, (hash1, hash2, ... ), (inp1, inp2, ...), output],
    #   [size, (hash1, hash2, ... ), (inp1, inp2, ...), output],
    #   ... ]

    _pymol._cache = []

    # standard input reading thread

    _pymol._stdin_reader_thread = None

    # stored views

    _pymol._view_dict = {}
    _pymol._view_dict_sc = None

    # stored scenes

    _pymol._scene_dict_sc = None
    _pymol._scene_counter = 1
    _pymol._scene_quit_on_action = ''

    # get us a private invocation pseudo-module

    _pymol._invocation = Scratch_Storage()
    _pymol._invocation.options = copy.deepcopy(invocation.options)
    _pymol._invocation.get_user_config = invocation.get_user_config
    _pymol._invocation.parse_args = invocation.parse_args

    # these locks are to be shared by all PyMOL instances within a
    # single Python interpeter

    _pymol.lock_api = threading.RLock() # mutex for API calls from the outside
    _pymol.lock_api_c = threading.RLock() # mutex for C management of python threads
    _pymol.lock_api_status = threading.RLock() # mutex for PyMOL status info
    _pymol.lock_api_glut = threading.RLock() # mutex for GLUT avoidance
    _pymol.lock_api_data = threading.RLock() # mutex for internal data structures

def get_version_message(v=None):
    '''
    Get an informative product + version string
    '''
    if not v:
        v = _cmd.get_version()

    p = "PyMOL %s " % v[0]
    p += "Incentive Product" if invocation.options.incentive_product else \
         "Open-Source"

    if v[5]:
        p += ", svn rev %s" % v[5]

    return p

def guess_pymol_path():
    '''
    Guess PYMOL_PATH from typical locations and return it as string.
    '''
    init_file = os.path.abspath(__file__)

    pymol_path_candidates = [
        # $PYMOL_PATH == <site-packages>/pymol/pymol_path
        os.path.join(os.path.dirname(init_file), 'pymol_path'),

        # $PYMOL_PATH/modules/pymol/__init__.py
        re.sub(r"[\/\\]modules[\/\\]pymol[\/\\]__init__\.py[c]*$", "", init_file),

        # /usr/share/pymol
        os.path.join(sys.prefix, 'share', 'pymol'),

        find_module('pymol')[1]
    ]

    for pymol_path in pymol_path_candidates:
        if os.path.isdir(pymol_path):
            return pymol_path

    return '.'

def setup_environ():
    # guess PYMOL_PATH if unset
    if 'PYMOL_PATH' not in os.environ:
        os.environ['PYMOL_PATH'] = guess_pymol_path()

    # other PyMOL variables
    if 'PYMOL_DATA' not in os.environ:
        os.environ['PYMOL_DATA'] = os.path.join(os.environ['PYMOL_PATH'], 'data')
    if 'PYMOL_SCRIPTS' not in os.environ:
        os.environ['PYMOL_SCRIPTS'] = os.path.join(os.environ['PYMOL_PATH'], 'scripts')
    os.environ['TUT'] = os.path.join(os.environ['PYMOL_DATA'], 'tut')

    # auto-detect bundled FREEMOL (if present)
    if 'FREEMOL' not in os.environ:
        for test_path in ['ext', 'freemol']:
            test_path = os.path.join(os.environ['PYMOL_PATH'], test_path)
            if os.path.isdir(test_path):
                os.environ['FREEMOL'] = test_path
                break

    # include FREEMOL's libpy in sys.path (if present)
    if 'FREEMOL' in os.environ:
        freemol_libpy = os.path.join(os.environ['FREEMOL'], "libpy")
        if os.path.isdir(freemol_libpy) and freemol_libpy not in sys.path:
            sys.path.append(freemol_libpy)

    # set Tcl/Tk environment if we ship it in ext/lib
    pymol_path = os.environ['PYMOL_PATH']
    for varname, dirname in [
            ('TCL_LIBRARY', 'tcl8.5'),
            ('TK_LIBRARY', 'tk8.5')]:
        dirname = os.path.join(pymol_path, "ext", "lib", dirname)
        if os.path.isdir(dirname):
            os.environ[varname] = dirname

def exec_str(self, string):
    '''
    Execute string in "self" namespace (used from C)
    '''
    try:
        exec(string, self.__dict__, self.__dict__)
    except Exception:
        traceback.print_exc()
    return None

def exec_deferred(self):
    '''
    Execute the stuff from invocations.options.deferred
    '''
    try:
        from socket import error as socket_error
    except ImportError:
        socket_error = None
        print('import socket failed')

    cmd = self.cmd
    _pymol = cmd._pymol

    # read from stdin (-p)
    if self.invocation.options.read_stdin and not _pymol._stdin_reader_thread:
        try:
            t = _pymol._stdin_reader_thread = \
                    threading.Thread(target=cmd._parser.stdin_reader)
            t.setDaemon(1)
            t.start()
        except:
            traceback.print_exc()

    # do the deferred stuff
    try:
        if cmd.ready():
            cmd.config_mouse(quiet=1)
            for a in self.invocation.options.deferred:
                if a[0:4] == "_do_":
                    cmd.do(a[4:])
                else:
                    cmd.load(a, quiet=0)
    except CmdException as e:
        print(e)
        print(" Error: Argument processing aborted due to exception (above).")
    except socket_error:
        # this (should) only happen if we're opening a PWG file on startup
        # and the port is busy.  For now, simply bail...
        cmd.wizard("message",["Socket.error: ","",
                              "\\999Assigned socket in use.","",
                              "\\779Is PyMOL already launched?","",
                              "\\966Shutting down..."])
        cmd.refresh()
        cmd.do("time.sleep(2);cmd.quit()")

def adapt_to_hardware(self):
    '''
    optimize for (or workaround) specific hardware
    '''
    cmd = self.cmd

    vendor, renderer, version = cmd.get_renderer()

    # Quadro cards don't support GL_BACK in stereo contexts
    if vendor.startswith('NVIDIA'):
        if 'Quadro' in renderer:
            if invocation.options.show_splash:
                print(" Adapting to Quadro hardware.")
            cmd.set('stereo_double_pump_mono', 1)

    elif vendor.startswith('Mesa'):
        if renderer[0:18]=='Mesa GLX Indirect':
            pass

    elif vendor.startswith('ATI'):
        if renderer[0:17] == 'FireGL2 / FireGL3':  # obsolete ?
            if invocation.options.show_splash:
                print(" Adapting to FireGL hardware.")
            cmd.set('line_width', 2, quiet=1)

        if IS_WINDOWS:
            if sys.getwindowsversion()[0] > 5:
                # prevent color corruption by calling glFlush etc.
                cmd.set('ati_bugs', 1)

        if 'Radeon HD' in renderer:
            if invocation.options.show_splash:
                print(" Adjusting settings to improve performance for ATI cards.")

            if cmd.get_setting_int("use_shaders")==0:
                # limit frame rate to 30 fps to avoid ATI "jello"
                # where screen updates fall way behind the user.
                cmd.set("max_ups", 30)

    elif vendor.startswith('Microsoft'):
        if renderer[0:17] == 'GDI Generic':
            cmd.set('light_count', 1)
            cmd.set('spec_direct', 0.7)

    elif vendor.startswith("Intel"):
        if "Express" in renderer:
            if invocation.options.show_splash:
                print(" Disabling shaders for Intel Express graphics")
            cmd.set("use_shaders", 0)

    elif (vendor == 'nouveau'
            or ' R300 ' in vendor # V: X.Org R300 Project, R: Gallium 0.4 on ATI RV370
            ):
        if invocation.options.show_splash:
            print(" Detected blacklisted graphics driver.  Disabling shaders.")
        cmd.set("use_shaders", 0)

    # find out how many processors we have, and adjust hash
    # table size to reflect available RAM

    try:
        import multiprocessing
        ncpu = multiprocessing.cpu_count()
        if ncpu > 1:
             cmd.set("max_threads", ncpu)
             if invocation.options.show_splash:
                  print(" Detected %d CPU cores."%ncpu, end=' ')
                  print(" Enabled multithreaded rendering.")
    except:
        pass

    # store our adapted state as default
    cmd.reinitialize("store")

def launch_gui(self):
    '''
    Launch if requested:
    - external GUI
    '''
    pymol_path = os.getenv('PYMOL_PATH', '')

    try:
        poll = IS_MACOS

        if self.invocation.options.external_gui == 3:
            if 'DISPLAY' not in os.environ:
                os.environ['DISPLAY'] = ':0.0'

        if self.invocation.options.external_gui in (1, 3):
            __import__(self.invocation.options.gui)
            sys.modules[self.invocation.options.gui].__init__(self, poll,
                    skin = self.invocation.options.skin)

            # import plugin system
            import pymol.plugins

    except:
        traceback.print_exc()

def prime_pymol():
    '''
    Set the current thread as the glutThread
    '''
    global glutThread

    if not glutThread:
        glutThread = thread.get_ident()


def _launch_no_gui():
    import pymol2

    p = pymol2.SingletonPyMOL()
    p.start()

    # TODO sufficient?
    while (p.idle() or p.getRedisplay() or
            invocation.options.keep_thread_alive or
            cmd.get_modal_draw() or
            cmd.get_setting_int('keep_alive') or
            cmd._pymol._stdin_reader_thread is not None):
        p.draw()

    # TODO needed?
    cmd.sync()
    p.stop()


def launch(args=None, block_input_hook=0):
    '''
    Run PyMOL with args

    Only returns if we are running pretend GLUT.
    '''
    if args is None:
        args = sys.argv
    invocation.parse_args(args)

    if invocation.options.gui == 'pmg_qt':
        if invocation.options.no_gui:
            return _launch_no_gui()

        try:
            from pmg_qt import pymol_qt_gui
            sys.exit(pymol_qt_gui.execapp())
        except ImportError:
            print('Qt not available, using GLUT/Tk interface')
            invocation.options.gui = 'pmg_tk'

    prime_pymol()
    _cmd.runpymol(_cmd._get_global_C_object(), block_input_hook)

def finish_launching(args=None):
    '''
    Start the PyMOL process in a thread
    '''
    global glutThreadObject

    if cmd._COb is not None:
        return

    import pymol

    # legacy
    if args is None:
        args = getattr(pymol, 'pymol_argv', None)
    if args is None:
        args = getattr(__main__, 'pymol_argv', sys.argv)

    if True:
        # run PyMOL in thread
        invocation.options.keep_thread_alive = 1
        cmd.reaper = threading.currentThread()
        glutThreadObject = threading.Thread(target=launch,
                args=(list(args), 1))
        glutThreadObject.start()

    e = threading.Event()

    # wait for the C library to initialize
    while cmd._COb is None:
        e.wait(0.01)

    # make sure symmetry module has time to start...
    while not hasattr(pymol, 'xray'):
        e.wait(0.01)

class CmdException(Exception):
    '''
    Exception type for PyMOL commands
    '''
    label = "Error"
    def __init__(self, message='', label=None):
        self.message = message
        if message:
            self.args = (message,)
        if label:
            self.label = label
    def __str__(self):
        return " %s: %s" % (self.label, self.message)

class IncentiveOnlyException(CmdException):
    '''
    Exception type for features that are not available in Open-Source PyMOL
    '''
    label = "Incentive-Only-Error"
    def __init__(self, message=''):
        if not message:
            try:
                funcname = sys._getframe(1).f_code.co_name
                message = '"%s" is not available in Open-Source PyMOL' % (funcname,)
            except:
                message = 'Not available in Open-Source PyMOL'
        message += '\n\n' \
                    '    Please visit http://pymol.org if you are interested in the\n' \
                    '    full featured "Incentive PyMOL" version.\n'
        super(IncentiveOnlyException, self).__init__(message)

class Scratch_Storage:
    '''
    Generic namespace
    '''
    def __reduce__(self):
        # for loading Python 3 (new-style class) pickle with Python 2
        return (self.__class__, (), self.__dict__)

    def get_unused_name(self, prefix='tmp'):
        '''
        Get an unused name from this namespace
        '''
        i = 1
        while True:
            name = prefix + str(i)
            if not hasattr(self, name):
                setattr(self, name, None)
                return name
            i += 1

class Session_Storage:
    '''
    Generic namespace
    '''
    def __reduce__(self):
        # for loading Python 3 (new-style class) pickle with Python 2
        return (self.__class__, (), self.__dict__)


def _colortype(cmd):
    # backwards compatible color index type for iterate, which used
    # to expose colors as RGB tuples
    get_color_tuple = cmd.get_color_tuple
    class Color(int):
        def __getitem__(self, i):
            return get_color_tuple(self)[i]
        def __len__(self):
            return 3

    return Color


######### VARIABLES ############################

glutThread = 0

######### ENVIRONMENT ##########################

setup_environ()

# initialize instance-specific module/object internals
_init_internals(sys.modules[__name__])

# maximize responsiveness
sys.setcheckinterval(1)

# get X-window support (machine_get_clipboard)
if 'DISPLAY' in os.environ:
    from .xwin import *

########## C MODULE ############################

import pymol._cmd
_cmd = sys.modules['pymol._cmd']

from . import cmd

cmd._COb = None

try:
    import epymol
except ImportError:
    pass

########## WORKAROUND TO PREVENT "import cmd" ##############################
# Previous versions of PyMOL did relative imports and thus allowd
# "import cmd" in pymol scripts to import the pymol.cmd module. To be more
# strict and for compatibility with python3 we use absolute imports now,
# which unfortunately will import an unrelated "cmd" module from the default
# python library, and even worse will corrupt the pymol namespace with it.
# The following causes an import error for "import cmd":

class _NoCmdFinder:
    def find_spec(self, fullname, path=None, target=None):
        if path is None and fullname == 'cmd':
            msg = 'use "from pymol import cmd" instead of "import cmd"'
            raise CmdException(msg)
        return None
    find_module = find_spec

sys.meta_path.insert(0, _NoCmdFinder())

########## LEGACY PRINT STATEMENT FOR PYMOL COMMAND LINE ###################

if IS_PY3:
    def _print_statement(*args, **_):
        '''Legacy Python-2-like print statement for the PyMOL command line'''
        kw = {}
        if args and args[0].startswith('>>'):
            kw['file'] = eval(args[0][2:])
            args = args[1:]
        if args and not args[-1]:
            kw['end'] = ' '
            args = args[:-1]
        args = [eval(a) for a in args]
        print(*args, **kw)

    cmd.extend('print', _print_statement)