forked from Pymol-Scripts/Pymol-script-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Draw_Protein_Dimensions.py
364 lines (287 loc) · 11.7 KB
/
Draw_Protein_Dimensions.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
'''
Calculate and display the dimensions of a protein.
This is a first version, please use at your own risk!
REQUIREMENTS
numpy (http://numpy.scipy.org) that should be built into the newers versions of Pymol
(c) Pablo Guardado Calvo
Based on "inertia_tensor.py" (c) 2010 by Mateusz Maciejewski
License: MIT
'''
from __future__ import print_function
__author__ = 'Pablo Guardado Calvo'
__version__ = '0.2'
__email__ = 'pablo.guardado (at) gmail.com'
__date__ = '13/08/2015'
__modification_date__ = '05/02/2018'
__modification_reason__ = 'Error in the code produced sometimes inverted structures'
###########################################################################################################################################################
# USAGE
#
# The idea behing this script is to calculate an aproximate minimal bounding box to extract the cell dimensions of a protein. To calculate the minimal bounding
# is not trivial and usually the Axis Aligned Bounding Box (AABB) does not show up the real dimensions of the protein. This script calculates the inertia tensor
# of the object, extract the eigenvalues and use them to rotate the molecule (using as rotation matrix the transpose of the eigenvalues matrix). The result is that
# the molecule is oriented with the inertia axis aligned with the cartesian axis. A new Bounding Box is calculated that is called Inertia Axis Aligned Bounding Box
#(IABB), whose volume is always lower than AABB volume, and in many cases will correspond with the lowest volume. Of course, maybe it exists another Bounding Box
# with a lower volume (the minimal Bounding Box).
#
# As always with these type of things, you have to use at your own risk. I did not try all the possible combinations, but if you find a bug, do
# not hesitate to contact me (pablo.guardado (at) gmail.com) or try to modify the code for yourself to correct it.
#
# To load the script just type:
#
# run path-to-the-script/Draw_Protein_Dimensions.py
#
# or if you want something more permanent add the previous line to your .pymolrc file
#
# The script works just typing:
#
# draw_Protein_Dimensions selection
#
# This will draw the cell dimensions of your selection based on a IABB. It also generates the IABB box and the inertia axis, you just need to do "show cgo" to display them.
#
# You could also try:
#
# draw_BB selection
#
# This will draw the AABB and IABB boxes with their cell dimensions and show in the command line their volumes, you can compare both of them.
############################################################################################################################################################
from pymol import cmd, cgo
from pymol.cgo import *
import numpy
from random import randint
def matriz_inercia(selection):
'''
DESCRIPTION
The method calculates the mass center, the inertia tensor and the eigenvalues and eigenvectors
for a given selection. Mostly taken from inertia_tensor.py
'''
model = cmd.get_model(selection)
totmass = 0.0
x,y,z = 0,0,0
for a in model.atom:
m = a.get_mass()
x += a.coord[0]*m
y += a.coord[1]*m
z += a.coord[2]*m
totmass += m
global cM
cM = numpy.array([x/totmass, y/totmass, z/totmass])
I = []
for index in range(9):
I.append(0)
for a in model.atom:
temp_x, temp_y, temp_z = a.coord[0], a.coord[1], a.coord[2]
temp_x -= x
temp_y -= y
temp_z -= z
I[0] += a.get_mass() * (temp_y**2 + temp_z**2)
I[1] -= a.get_mass() * temp_x * temp_y
I[2] -= a.get_mass() * temp_x * temp_z
I[3] -= a.get_mass() * temp_x * temp_y
I[4] += a.get_mass() * (temp_x**2 + temp_z**2)
I[5] -= a.get_mass() * temp_y * temp_z
I[6] -= a.get_mass() * temp_x * temp_z
I[7] -= a.get_mass() * temp_y * temp_z
I[8] += a.get_mass() * (temp_x**2 + temp_y**2)
global tensor
tensor = numpy.array([(I[0:3]), (I[3:6]), (I[6:9])])
global autoval, autovect, ord_autoval, ord_autovect
autoval, autovect = numpy.linalg.eig(tensor)
auto_ord = numpy.argsort(autoval)
ord_autoval = autoval[auto_ord]
ord_autovect_complete = autovect[:, auto_ord].T
ord_autovect = numpy.around(ord_autovect_complete, 3)
return ord_autoval
def draw_inertia_axis(selection):
'''
DESCRIPTION
This method draw the inertia axis calculated with the method matriz_inercia.
'''
matriz_inercia(selection)
axis1 = ord_autovect[0]
x1, y1, z1 = cM[0], cM[1], cM[2]
x2, y2, z2 = cM[0]+50*axis1[0], cM[1]+50*axis1[1], cM[2]+50*axis1[2]
eje1 = [cgo.CYLINDER, x1, y1, z1, x2, y2, z2, 0.6, 1, 0, 0, 1, 0, 0, 0.0]
cmd.load_cgo(eje1, 'Inertia_Axis1')
axis2 = ord_autovect[1]
x3, y3, z3 = cM[0]+40*axis2[0], cM[1]+40*axis2[1], cM[2]+40*axis2[2]
eje1 = [cgo.CYLINDER, x1, y1, z1, x3, y3, z3, 0.6, 1, 0.5, 0, 1, 0.5, 0, 0.0]
cmd.load_cgo(eje1, 'Inertia_Axis2')
axis4 = ord_autovect[2]
x4, y4, z4 = cM[0]+30*axis4[0], cM[1]+30*axis4[1], cM[2]+30*axis4[2]
eje1 = [cgo.CYLINDER, x1, y1, z1, x4, y4, z4, 0.6, 1, 1, 0, 1, 1, 0, 0.0]
cmd.load_cgo(eje1, 'Inertia_Axis3')
def translacion_cM(selection):
'''
DESCRIPTION
Translate the center of mass of the molecule to the origin.
'''
model = cmd.get_model(selection)
totmass = 0.0
x,y,z = 0,0,0
for a in model.atom:
m = a.get_mass()
x += a.coord[0]*m
y += a.coord[1]*m
z += a.coord[2]*m
totmass += m
cM = numpy.array([x/totmass, y/totmass, z/totmass])
trans_array = ([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, -cM[0], -cM[1], -cM[2], 1])
model_trans = cmd.transform_selection(selection, trans_array)
def rotacion_orig(selection):
'''
DESCRIPTION
Find the proper rotation matrix, i.e. the transpose of the matrix formed by the eigenvectors of the inertia tensor
'''
translacion_cM(selection)
matriz_inercia(selection)
global transf, transf_array, ord_autovect_array, transf_array_print
ord_autovect_array = numpy.array([[ord_autovect[0][0], ord_autovect[0][1], ord_autovect[0][2]],
[ord_autovect[1][0], ord_autovect[1][1], ord_autovect[1][2]],
[ord_autovect[2][0], ord_autovect[2][1], ord_autovect[2][2]]])
if numpy.linalg.det(ord_autovect_array) < 0.:
ord_autovect_array = numpy.array([[ord_autovect[2][0], ord_autovect[2][1], ord_autovect[2][2]],
[ord_autovect[1][0], ord_autovect[1][1], ord_autovect[1][2]],
[ord_autovect[0][0], ord_autovect[0][1], ord_autovect[0][2]]])
transf = numpy.transpose(ord_autovect_array)
transf_array = numpy.array([transf[0][0], transf[0][1], transf[0][2], 0,
transf[1][0], transf[1][1], transf[1][2], 0,
transf[2][0], transf[2][1], transf[2][2], 0,
0, 0, 0, 1])
def transformar(selection):
'''
DESCRIPTION
Rotate the molecule and draw the inertia axis.
'''
rotacion_orig(selection)
model_rot = cmd.transform_selection(selection, transf_array, homogenous=0, transpose=1);
draw_inertia_axis(selection)
def draw_AABB(selection):
"""
DESCRIPTION
For a given selection, draw the Axes Aligned bounding box around it without padding. Code taken and modified from DrawBoundingBox.py.
"""
AA_original = selection + "_original"
model_orig = cmd.create(AA_original, selection)
([min_X, min_Y, min_Z],[max_X, max_Y, max_Z]) = cmd.get_extent(AA_original)
print("The Axis Aligned Bounding Box (AABB) dimensions are (%.2f, %.2f, %.2f)" % (max_X-min_X, max_Y-min_Y, max_Z-min_Z))
print("The Axis Aligned Bounding Box (AABB) volume is %.2f A3" % ((max_X-min_X)*(max_Y-min_Y)*(max_Z-min_Z)))
min_X = min_X
min_Y = min_Y
min_Z = min_Z
max_X = max_X
max_Y = max_Y
max_Z = max_Z
boundingBox = [
LINEWIDTH, float(2),
BEGIN, LINES,
COLOR, float(1), float(1), float(0),
VERTEX, min_X, min_Y, min_Z,
VERTEX, min_X, min_Y, max_Z,
VERTEX, min_X, max_Y, min_Z,
VERTEX, min_X, max_Y, max_Z,
VERTEX, max_X, min_Y, min_Z,
VERTEX, max_X, min_Y, max_Z,
VERTEX, max_X, max_Y, min_Z,
VERTEX, max_X, max_Y, max_Z,
VERTEX, min_X, min_Y, min_Z,
VERTEX, max_X, min_Y, min_Z,
VERTEX, min_X, max_Y, min_Z,
VERTEX, max_X, max_Y, min_Z,
VERTEX, min_X, max_Y, max_Z,
VERTEX, max_X, max_Y, max_Z,
VERTEX, min_X, min_Y, max_Z,
VERTEX, max_X, min_Y, max_Z,
VERTEX, min_X, min_Y, min_Z,
VERTEX, min_X, max_Y, min_Z,
VERTEX, max_X, min_Y, min_Z,
VERTEX, max_X, max_Y, min_Z,
VERTEX, min_X, min_Y, max_Z,
VERTEX, min_X, max_Y, max_Z,
VERTEX, max_X, min_Y, max_Z,
VERTEX, max_X, max_Y, max_Z,
END
]
p0 = '_0' + str(randint(0, 100))
p1 = '_1' + str(randint(0, 100))
p2 = '_2' + str(randint(0, 100))
p3 = '_3' + str(randint(0, 100))
cmd.pseudoatom (pos=[min_X, min_Y, min_Z], object=p0)
cmd.pseudoatom (pos=[min_X, min_Y, max_Z], object=p1)
cmd.pseudoatom (pos=[min_X, max_Y, min_Z], object=p2)
cmd.pseudoatom (pos=[max_X, min_Y, min_Z], object=p3)
cmd.distance(None, p0, p3)
cmd.distance(None, p0, p2)
cmd.distance(None, p0, p1)
cmd.hide("nonbonded")
boxName = "box_AABB_" + str(randint(0, 100))
cmd.load_cgo(boundingBox,boxName)
return boxName
def draw_IABB(selection):
"""
DESCRIPTION
For a given selection, draw the Inertia Axes Aligned bounding box around it without padding. Code taken and modified from DrawBoundingBox.py.
"""
transformar(selection)
([minX, minY, minZ],[maxX, maxY, maxZ]) = cmd.get_extent(selection)
print("The Inertia Axis Aligned Bounding Box (IABB) dimensions are (%.2f, %.2f, %.2f)" % (maxX-minX, maxY-minY, maxZ-minZ))
print("The Inertia Axis Aligned Bounding Box (IABB) volume is %.2f A3" % ((maxX-minX)*(maxY-minY)*(maxZ-minZ)))
minX = minX
minY = minY
minZ = minZ
maxX = maxX
maxY = maxY
maxZ = maxZ
boundingBox = [
LINEWIDTH, float(2),
BEGIN, LINES,
COLOR, float(1), float(0), float(0),
VERTEX, minX, minY, minZ,
VERTEX, minX, minY, maxZ,
VERTEX, minX, maxY, minZ,
VERTEX, minX, maxY, maxZ,
VERTEX, maxX, minY, minZ,
VERTEX, maxX, minY, maxZ,
VERTEX, maxX, maxY, minZ,
VERTEX, maxX, maxY, maxZ,
VERTEX, minX, minY, minZ,
VERTEX, maxX, minY, minZ,
VERTEX, minX, maxY, minZ,
VERTEX, maxX, maxY, minZ,
VERTEX, minX, maxY, maxZ,
VERTEX, maxX, maxY, maxZ,
VERTEX, minX, minY, maxZ,
VERTEX, maxX, minY, maxZ,
VERTEX, minX, minY, minZ,
VERTEX, minX, maxY, minZ,
VERTEX, maxX, minY, minZ,
VERTEX, maxX, maxY, minZ,
VERTEX, minX, minY, maxZ,
VERTEX, minX, maxY, maxZ,
VERTEX, maxX, minY, maxZ,
VERTEX, maxX, maxY, maxZ,
END
]
p4 = '_4' + str(randint(0, 100))
p5 = '_5' + str(randint(0, 100))
p6 = '_6' + str(randint(0, 100))
p7 = '_7' + str(randint(0, 100))
cmd.pseudoatom (pos=[minX, minY, minZ], object=p4)
cmd.pseudoatom (pos=[minX, minY, maxZ], object=p5)
cmd.pseudoatom (pos=[minX, maxY, minZ], object=p6)
cmd.pseudoatom (pos=[maxX, minY, minZ], object=p7)
cmd.distance(None, p4, p7)
cmd.distance(None, p4, p6)
cmd.distance(None, p4, p5)
cmd.hide("nonbonded")
boxName = "box_IABB_" + str(randint(0, 100))
cmd.load_cgo(boundingBox,boxName)
return boxName
def draw_BB(selection):
draw_AABB(selection)
draw_IABB(selection)
def draw_Protein_Dimensions(selection):
draw_IABB(selection)
cmd.hide("cgo")
cmd.extend ("draw_Protein_Dimensions", draw_Protein_Dimensions)
cmd.extend ("draw_BB", draw_BB)