forked from Pymol-Scripts/Pymol-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickdisplays.py
502 lines (432 loc) · 19.5 KB
/
quickdisplays.py
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
'''
quickdisplays.py
Described at: http://www.pymolwiki.org/quickdisplays
##################################################
Bundled commands for quick default representations.
Type disp_list for listing of functions.
##################################################
Plugin contributed by Andreas Warnecke
##################################################
VERSION NOTES:
1.0 2014 First release
1.1 2016 disabled disp_surf from setting surface_carve_selection
corrected print output
'''
#-------------------------------------------------------------------------------
# IMPORT
from __future__ import print_function
from pymol import cmd
from pymol import stored
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# PRINT LIST OF FUNCTIONS
#-------------------------------------------------------------------------------
def disp_list():
print(
'''
quickdisplay - list of functions.
Enter, e.g. "help disp_ss" for more specific info.
PURPOSE FUNCTION
secondary structure cartoon......disp_ss
ball and stick representation....disp_ball_stick
mesh display.....................disp_mesh
surface display..................disp_surf
Putty b-factor sausage...........disp_putty
'''
)
cmd.extend("disp_list", disp_list)
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# PERCENTILE LIMITS (used below)
#-------------------------------------------------------------------------------
def get_b_limits(input='[0,100]', selection='all'):
from pymol import cmd, stored
import math
try:
limits = str(input)
if limits.startswith('['):
# list
limits = eval(limits)[:2]
else:
# integer
selection = str(selection)
limits = abs(float(limits))
if limits > 50:
return False
stored.temp = []
cmd.iterate(selection, 'stored.temp.append(b)')
stored.temp.sort()
kl = (len(stored.temp) - 1) * limits / 100
fl = math.floor(kl)
cl = math.ceil(kl)
kr = (len(stored.temp) - 1) * (100 - limits) / 100
fr = math.floor(kr)
cr = math.ceil(kr)
limits = [0, 0]
if fl == cl:
limits[0] = stored.temp[int(kl)]
else:
limits[0] = stored.temp[int(kl)] * (cl - kl) + stored.temp[int(kl)] * (kl - fl)
if fr == cr:
limits[1] = stored.temp[int(kr)]
else:
limits[1] = stored.temp[int(kr)] * (cr - kr) + stored.temp[int(kr)] * (kr - fr)
except:
return False
return limits
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# DISPLAY: SECONDARY STRUCTURE
#-------------------------------------------------------------------------------
def disp_ss(
selection='all',
colors='marine red white',
only=False):
'''
DESCRIPTION
Formats the passed object into secondary structure cartoon
USAGE
disp_ss [ selection [, colors [, only ]]]
PARAMETERS
NAME=DEFAULT TYPE FUNCTION
selection='all' <str> input selection
colors='marine red white' <str> any three colors for: sheets, helices and loops
e.g. 'marine red white'
can also be set to either util.cbc, util.rainbow,
or util.chainbow (alone)
set='False' to supress coloring altogether, or enter False
for the coloring to be omitted, e.g. 'marine False green'
only <bool> if True will use show_as; else show
'''
try:
selection = '(' + selection + ')'
colors = str(colors)
only = bool(str(only) != 'False')
except:
print("Input error")
return False
if colors == 'False':
color_s = color_h = color_l = False
elif colors.startswith('util.'):
util_choices = ['util.cbc', 'util.rainbow', 'util.chainbow', 'util.ss']
if colors in util_choices:
color_s = color_h = color_l = '%s ' % util_choices[util_choices.index(colors)]
else:
print("Input error! Please check the color setting using util. is one of:", util_choices)
return False
else:
try:
color_s, color_h, color_l = colors.split()[:3]
if color_s != 'False':
cmd.color(color_s, None)
color_s = 'color %s, ' % color_s
else:
color_s = False
if color_h != 'False':
cmd.color(color_h, None)
color_h = 'color %s, ' % color_h
else:
color_h = False
if color_l != 'False':
cmd.color(color_l, None)
color_l = 'color %s, ' % color_l
else:
color_l = False
except:
print("Input error! Please check that three valid colors (or False) are provided")
return False
for p in cmd.get_object_list(selection):
cmd.set('cartoon_discrete_colors', 'on', p)
# settings
cmd.cartoon('rectangle', 'ss s and %s and %s' % (selection, p))
cmd.cartoon('dumbbell', 'ss h and %s and %s' % (selection, p))
cmd.cartoon('loop', 'ss l+"" and %s and %s' % (selection, p))
# sheets
if color_s:
print(cmd.do(color_s + '(ss s and %s and %s)' % (selection, p)))
cmd.set('cartoon_rect_length', 1.5, p)
cmd.set('cartoon_rect_width', 0.25, p)
# a-helices
if color_h:
print(cmd.do(color_h + '(ss h and %s and %s)' % (selection, p)))
cmd.set('cartoon_dumbbell_length', 1.5, p)
cmd.set('cartoon_dumbbell_width', 0.25, p)
cmd.set('cartoon_dumbbell_radius', 0.2, p)
# loops
if color_l:
print(cmd.do(color_l + '(ss l+"" and %s and %s)' % (selection, p)))
cmd.set('cartoon_loop_radius', 0.25, p)
if only:
cmd.show_as('cartoon', '%s and %s' % (selection, p))
else:
cmd.show('cartoon', '%s and %s' % (selection, p))
cmd.extend("disp_ss", disp_ss)
cmd.auto_arg[0]['disp_ss'] = [lambda: cmd.Shortcut(['all']), 'selection=', ',']
cmd.auto_arg[1]['disp_ss'] = [lambda: cmd.Shortcut(['default', 'blue', 'yellow']), 'color_s=', ',']
cmd.auto_arg[2]['disp_ss'] = [lambda: cmd.Shortcut(['default', 'red', 'blue']), 'color_h=', ',']
cmd.auto_arg[3]['disp_ss'] = [lambda: cmd.Shortcut(['color_l=default, only=False']), 'remaining (defaults)...', '']
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# DISPLAY: BALL AND STICK
#-------------------------------------------------------------------------------
def disp_ball_stick(selection='all', hydrogens=0, only=False):
'''
DESCRIPTION
Formats the passed object into ball and stick
USEAGE
disp_ball_stick [ selection [, hydrogens [, only ]]]
EXAMPLE
fetch 1hpv, async=0
disp_ball_stick
util.cbaw
PARAMETERS
NAME=DEFAULT TYPE FUNCTION
selection='all' <str> input selection
hydrogens <int> -1: remove; 1: add; else: as is
only=False <bool> if True will use show_as; else show
'''
try:
selection = '(' + selection + ')'
hydrogens = int(hydrogens)
only = bool(str(only) != 'False')
except:
print("Input error")
return False
if hydrogens == 1:
cmd.h_add('%s' % selection)
if hydrogens == -1:
cmd.remove('%s and elem H' % selection)
for p in cmd.get_object_list(selection):
cmd.set('valence', 'on', p)
cmd.set('stick_ball', 'on', p)
cmd.set('stick_ball_ratio', 3, p)
cmd.set('stick_radius', 0.12, p)
if only:
cmd.show_as('sticks', '%s' % (selection))
else:
cmd.show('sticks', '%s' % (selection))
cmd.extend("disp_ball_stick", disp_ball_stick)
cmd.auto_arg[0]['disp_ball_stick'] = [lambda: cmd.Shortcut(['all']), 'selection=', ',']
cmd.auto_arg[1]['disp_ball_stick'] = [lambda: cmd.Shortcut(['-1', '0', '1']), 'hydrogens=', ',']
cmd.auto_arg[2]['disp_ball_stick'] = [lambda: cmd.Shortcut(['True', 'False']), 'only=', '']
#-------------------------------------------------------------------------------
def disp_stick_ball(selection='all', hydrogens=0, only=False):
'''
see help disp_stick_ball
'''
#disp_stick_ball - redirected
disp_ball_stick(selection, hydrogens, only)
cmd.extend("disp_stick_ball", disp_stick_ball)
cmd.auto_arg[0]['disp_stick_ball'] = [lambda: cmd.Shortcut(['all']), 'selection=', ',']
cmd.auto_arg[1]['disp_stick_ball'] = [lambda: cmd.Shortcut(['-1', '0', '1']), 'hydrogens=', ',']
cmd.auto_arg[2]['disp_stick_ball'] = [lambda: cmd.Shortcut(['True', 'False']), 'only=', '']
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# DISPLAY: MESH
#-------------------------------------------------------------------------------
def disp_mesh(selection='all', color_m='default', hydrogens=0, only=False, limits=5):
'''
DESCRIPTION
Adds a mesh to the object
Has advanced coloring options and automatically accounts for the hydrogens
USEAGE
disp_mesh [ selection [, color_m [, hydrogens [, only [, limits]]]]]
disp_mesh selection=all, color_m=default
disp_mesh selection=all, color_m=white
disp_mesh selection=all, color_m=putty
PARAMETERS
NAME=DEFAULT TYPE FUNCTION
selection='all' <str> input selection
color_m='default' <str> 'default': as current
'name': colors by color or ramp called name
'putty': b-factor on surface
hydrogens=0 <int> -1: remove; 1: add; else: as is
only=False <bool> if True will use show_as; else show
limits=5 <list or flaot>
applies only if color_m=='putty'
sets the b-factor range limits
<list> [min,max] # absolute values
<float> percentile cutoff (both sides) # relative for each protein
'''
try:
selection = '(' + selection + ')'
color_m = str(color_m)
hydrogens = int(hydrogens)
only = bool(str(only) != 'False')
except:
print("Input error")
return False
if hydrogens == 1:
cmd.h_add('%s' % selection)
if hydrogens == -1:
cmd.remove('%s and elem H' % selection)
for p in cmd.get_object_list(selection):
cmd.set('mesh_width', 0.25, p)
if (color_m == 'putty'):
limits = get_b_limits(limits, p)
if not limits:
print("Input error (limits must be <list> or <float (<=50)>)!")
return False
cmd.set('mesh_color', 'default', p)
cmd.spectrum('b', 'rainbow', '(not hetatm) and %s' % p, minimum='%f' % limits[0], maximum='%f' % limits[1], byres=0)
print("disp_mesh:", p, "displayed in putty mode - mesh as putty - limits=[%.4f,%.4f]" % (limits[0], limits[1]))
else:
cmd.set('mesh_color', color_m, p)
print("disp_mesh: regular mode - mesh - " + p)
if only:
cmd.show_as('mesh', '%s and %s' % (selection, p))
else:
cmd.show('mesh', '%s and %s' % (selection, p))
cmd.rebuild()
cmd.extend("disp_mesh", disp_mesh)
cmd.auto_arg[0]['disp_mesh'] = [lambda: cmd.Shortcut(['all']), 'selection=', ',']
cmd.auto_arg[1]['disp_mesh'] = [lambda: cmd.Shortcut(['default', 'red', 'green', 'blue', 'yellow']), 'color_m=', ',']
cmd.auto_arg[2]['disp_mesh'] = [lambda: cmd.Shortcut(['-1', '0', '1']), 'hydrogens=', ',']
cmd.auto_arg[3]['disp_mesh'] = [lambda: cmd.Shortcut(['only=False, limits=5']), 'remaining (defaults)...', '']
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# DISPLAY: SURFACE AND SURFACE AS PUTTY
#-------------------------------------------------------------------------------
def disp_surf(
selection='all',
color_s='default',
transparency=0,
hydrogens=0,
solvent=0,
ramp_above=1,
only=False,
limits=5):
'''
DESCRIPTION
Advanced surface representation (cf. examples)
USAGE
disp_surf [ selection [, color_s [, transparency [, hydrogens [, solvent [, ramp_above [, only [, limits]]]]]]]]
EXAMPLES
disp_surf # opaque surface with default colors
disp_surf all, white, 0.5 # half-transparent white surface
disp_surf all, putty # b-factor on surface
PARAMETERS
NAME=DEFAULT TYPE FUNCTION
selection='all' <str> input selection
color_s='default' <str> 'default': as current
'name': colors by color or ramp called name
'putty': b-factor on surface (by resi)
transparency=0 <float> set surface transparency
hydrogens=0 <int> -1: remove; 1: add; else: as is
solvent=0 <int> defines 'surface_solvent'
ramp_above=1 <int> defines 'surface_ramp_above_mode'
only=False <bool> if True will use show_as; else show
limits=5 <list or flaot>
applies only if color_s=='putty'
sets the b-factor range limits
<list> [min,max] # absolute values
<float> percentile cutoff (both sides) # relative for each protein
'''
try:
selection = '(' + selection + ')'
color_s = str(color_s)
transparency = float(transparency)
hydrogens = int(hydrogens)
solvent = int(solvent)
ramp_above = int(ramp_above)
only = bool(str(only) != 'False')
except:
print("Input error")
return False
for p in cmd.get_object_list(selection):
if hydrogens == -1:
cmd.remove('%s and elem H' % p)
if hydrogens == 1:
cmd.h_add(p)
# if hydrogens==0: as is
# some defaults (settings can be changed later, too)
cmd.set('surface_carve_cutoff', '4.5', p)
cmd.set('surface_quality', '2', p)
cmd.set('solvent_radius', '1.5', p)
cmd.set('cavity_cull', '2', p)
#cmd.set('surface_carve_selection', p)
# this causes issues when using the command on different objects
# it is better to define surface_carve_selection manually
# defined
cmd.set('surface_solvent', solvent, p)
cmd.set('surface_ramp_above_mode', ramp_above, p)
cmd.set('transparency', transparency, p)
if (color_s == 'putty'):
limits = get_b_limits(limits, p)
if not limits:
print("Input error (limits must be <list> or <float (<=50)>)!")
return False
cmd.set('surface_color', 'default', p)
cmd.spectrum('b', 'rainbow',
'(not hetatm) and %s' % p, minimum='%f' % limits[0],
maximum='%f' % limits[1], byres=0)
print("disp_surf:", p, "displayed in putty mode - surface as putty - limits=[%.4f,%.4f]" % (limits[0], limits[1]))
else:
cmd.set('surface_color', color_s, selection)
print("disp_surf:", p, "displayed as regular surface")
if only:
cmd.show_as('surface', '(%s and %s)' % (selection, p))
else:
cmd.show('surface', '(%s and %s)' % (selection, p))
cmd.extend("disp_surf", disp_surf)
cmd.auto_arg[0]['disp_surf'] = [lambda: cmd.Shortcut(['all']), 'selection=', ',']
cmd.auto_arg[1]['disp_surf'] = [lambda: cmd.Shortcut(['default', 'red', 'green', 'blue', 'yellow']), 'color_s=', ',']
cmd.auto_arg[2]['disp_surf'] = [lambda: cmd.Shortcut(['0', '0.10', '0.20', '0.30', '0.40', '0.50', '0.60', '0.70', '0.80', '0.90', '1.00']), 'transparency=', ',']
cmd.auto_arg[3]['disp_surf'] = [lambda: cmd.Shortcut(['hydrogens=0, solvent=0, ramp_above=1, only=False, limits=5']), 'remaining (deafults)...', ',']
#cmd.auto_arg[4]['disp_surf']=[lambda: cmd.Shortcut(['0']), 'solvent=', ',']
#cmd.auto_arg[5]['disp_surf']=[lambda: cmd.Shortcut(['1']), 'ramp_above=', ',']
#cmd.auto_arg[6]['disp_surf']=[lambda: cmd.Shortcut(['True','False']), 'only=', '']
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# DISPLAY: PUTTY
#-------------------------------------------------------------------------------
def disp_putty(selection='all', limits=10, only=True):
'''
DESCRIPTION
Formats the passed object into a Putty b-factor sausage
USEAGE
disp_putty [ selection ]
selection <str> input selection
limits=10 <list or flaot>
applies only if color_m=='putty'
sets the b-factor range limits (by protein)
<list> [min,max]
<float> percentile cutoff (both sides)
only=True <bool> if True will use show_as; else show
'''
try:
selection = '(' + selection + ')'
only = bool(str(only) != 'False')
except:
print("Input error")
return False
for p in cmd.get_object_list(selection):
limits = get_b_limits(limits, p)
if not limits:
print("Input error (limits must be <list> or <float (<=50)>)!")
return False
# settings
cmd.set('cartoon_discrete_colors', 'off', p)
cmd.set('cartoon_putty_scale_max', 5, p)
cmd.set('cartoon_putty_scale_min', 1, p)
# normalized nonlinear scaling
cmd.set('cartoon_putty_transform', 0, p)
cmd.spectrum('b', 'rainbow', '(not hetatm) and %s' % p, minimum='%f' % limits[0], maximum='%f' % limits[1], byres=1)
cmd.cartoon('putty', '(%s and %s)' % (selection, p))
if only:
cmd.show_as('cartoon', '(%s and %s)' % (selection, p))
else:
cmd.show('cartoon', '(%s and %s)' % (selection, p))
cmd.extend("disp_putty", disp_putty)
cmd.auto_arg[0]['disp_putty'] = [lambda: cmd.Shortcut(['all', 'all and visible']), 'selection=', ',']
cmd.auto_arg[1]['disp_putty'] = [lambda: cmd.Shortcut('0', '5', '10', '[10,50]'), 'limits=', ',']
cmd.auto_arg[2]['disp_putty'] = [lambda: cmd.Shortcut(['True', 'False']), 'only=', '']
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------