-
Notifications
You must be signed in to change notification settings - Fork 258
/
colorbyrmsd.py
107 lines (76 loc) · 2.95 KB
/
colorbyrmsd.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
'''
http://pymolwiki.org/index.php/ColorByRMSD
Original Authors: Shivender Shandilya; Jason Vertrees
Complete rewrite by Thomas Holder
License: BSD-2-Clause
'''
from pymol import cmd, CmdException
def colorbyrmsd(mobile, target, doAlign=1, doPretty=1, guide=1, method='super', quiet=1):
'''
DESCRIPTION
Align two structures and show the structural deviations in color to more
easily see variable regions.
Colors each mobile/target atom-pair by distance (the name is a bit
misleading).
Modifies the B-factor columns in your original structures.
ARGUMENTS
mobile = string: atom selection for mobile atoms
target = string: atom selection for target atoms
doAlign = 0 or 1: Superpose selections before calculating distances
{default: 1}
doPretty = 0 or 1: Show nice representation and colors {default: 1}
EXAMPLE
fetch 1ake 4ake, async=0
remove chain B
colorbyrmsd 1ake, 4ake
'''
from chempy import cpv
doAlign, doPretty = int(doAlign), int(doPretty)
guide, quiet = int(guide), int(quiet)
aln, seleboth = '_aln', '_objSelBoth'
try:
align = cmd.keyword[method][0]
except:
print(' Error: no such method: ' + str(method))
raise CmdException
if guide:
mobile = '(%s) and guide' % mobile
target = '(%s) and guide' % target
try:
if doAlign:
# superpose
align(mobile, target)
# get alignment without superposing
align(mobile, target, cycles=0, transform=0, object=aln)
except:
print(' Error: Alignment with method %s failed' % (method))
raise CmdException
cmd.select(seleboth, '(%s) or (%s)' % (mobile, target))
idx2coords = dict()
cmd.iterate_state(-1, seleboth, 'idx2coords[model,index] = (x,y,z)', space=locals())
if cmd.count_atoms('?' + aln, 1, 1) == 0:
# this should ensure that "aln" will be available as selectable object
cmd.refresh()
b_dict = dict()
for col in cmd.get_raw_alignment(aln):
assert len(col) == 2
b = cpv.distance(idx2coords[col[0]], idx2coords[col[1]])
for idx in col:
b_dict[idx] = b
cmd.alter(seleboth, 'b = b_dict.get((model, index), -1)', space=locals())
if doPretty:
cmd.orient(seleboth)
cmd.show_as('cartoon', 'byobj ' + seleboth)
cmd.color('gray', seleboth)
cmd.spectrum('b', 'blue_red', seleboth + ' and b > -0.5')
if not quiet:
print(" ColorByRMSD: Minimum Distance: %.2f" % (min(b_dict.values())))
print(" ColorByRMSD: Maximum Distance: %.2f" % (max(b_dict.values())))
print(" ColorByRMSD: Average Distance: %.2f" % (sum(b_dict.values()) / len(b_dict)))
cmd.delete(aln)
cmd.delete(seleboth)
cmd.extend('colorbyrmsd', colorbyrmsd)
# tab-completion of arguments
cmd.auto_arg[0]['colorbyrmsd'] = cmd.auto_arg[0]['align']
cmd.auto_arg[1]['colorbyrmsd'] = cmd.auto_arg[1]['align']
# vi: ts=4:sw=4:smarttab:expandtab