-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtal.py
executable file
·319 lines (243 loc) · 8.48 KB
/
xtal.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
'''
Crystallographic symmetry related commands.
(c) 2010-2012 Thomas Holder
License: BSD-2-Clause
'''
from pymol import cmd, CmdException
def cellbasis(angles, edges):
'''
DESCRIPTION
API only. For the unit cell with given angles and edge lengths calculate
the basis transformation (vectors) as a 4x4 numpy.array
'''
from math import cos, sin, radians, sqrt
import numpy
rad = [radians(i) for i in angles]
basis = numpy.identity(4)
basis[0][1] = cos(rad[2])
basis[1][1] = sin(rad[2])
basis[0][2] = cos(rad[1])
basis[1][2] = (cos(rad[0]) - basis[0][1]*basis[0][2])/basis[1][1]
basis[2][2] = sqrt(1 - basis[0][2]**2 - basis[1][2]**2)
edges.append(1.0)
return basis * edges # numpy.array multiplication!
def supercell(a=1, b=1, c=1, object=None, color='green', name='supercell', withmates=1):
'''
DESCRIPTION
Draw a supercell, as requested by Nicolas Bock on the pymol-users
mailing list (Subject: [PyMOL] feature request: supercell construction
Date: 04/12/2010 10:12:17 PM (Mon, 12 Apr 2010 14:12:17 -0600))
USAGE
supercell a, b, c [, object [, color [, name [, withmates]]]]
ARGUMENTS
a, b, c = integer: repeat cell in x,y,z direction a,b,c times
{default: 1,1,1}
object = string: name of object to take cell definition from
color = string: color of cell {default: blue}
name = string: name of the cgo object to create {default: supercell}
withmates = bool: also create symmetry mates in displayed cells
{default: 1}
SEE ALSO
show cell
'''
import numpy
from pymol import cgo
if object is None:
object = cmd.get_object_list()[0]
withmates = int(withmates)
sym = cmd.get_symmetry(object)
cell_edges = sym[0:3]
cell_angles = sym[3:6]
basis = cellbasis(cell_angles, cell_edges)
assert isinstance(basis, numpy.ndarray)
ts = list()
for i in range(int(a)):
for j in range(int(b)):
for k in range(int(c)):
ts.append([i,j,k])
obj = [
cgo.BEGIN,
cgo.LINES,
]
for t in ts:
shift = basis[0:3,0:3] * t
shift = shift[:,0] + shift[:,1] + shift[:,2]
for i in range(3):
vi = basis[0:3,i]
vj = [
numpy.array([0.,0.,0.]),
basis[0:3,(i+1)%3],
basis[0:3,(i+2)%3],
basis[0:3,(i+1)%3] + basis[0:3,(i+2)%3]
]
for j in range(4):
obj.append(cgo.VERTEX)
obj.extend((shift + vj[j]).tolist())
obj.append(cgo.VERTEX)
obj.extend((shift + vj[j] + vi).tolist())
if withmates:
groupname = 'm%d%d%d' % tuple(t)
symexpcell(groupname + '_', object, *t)
cmd.group(groupname, groupname + '_*')
obj.append(cgo.END)
cmd.delete(name)
cmd.load_cgo(obj, name)
cmd.color(color, name)
def symexpcell(prefix='mate', object=None, a=0, b=0, c=0):
'''
DESCRIPTION
Creates all symmetry-related objects for the specified object that
occur with their bounding box center within the unit cell.
USAGE
symexpcell prefix, object, [a, b, c]
ARGUMENTS
prefix = string: prefix of new objects
object = string: object for which to create symmetry mates
a, b, c = integer: create neighboring cell {default: 0,0,0}
SEE ALSO
symexp
'''
import numpy
from pymol import xray
if object is None:
object = cmd.get_object_list()[0]
sym = cmd.get_symmetry(object)
cell_edges = sym[0:3]
cell_angles = sym[3:6]
spacegroup = sym[6]
basis = cellbasis(cell_angles, cell_edges)
basis = numpy.matrix(basis)
extent = cmd.get_extent(object)
center = sum(numpy.array(extent)) * 0.5
center = numpy.matrix(center.tolist() + [1.0]).T
center_cell = basis.I * center
extra_shift = [[float(i)] for i in (a,b,c)]
spacegroup = xray.space_group_map.get(spacegroup, spacegroup)
i = 0
matrices = xray.sg_sym_to_mat_list(spacegroup)
for mat in matrices:
i += 1
mat = numpy.matrix(mat)
shift = numpy.floor(mat * center_cell)
mat[0:3,3] -= shift[0:3,0]
mat[0:3,3] += extra_shift
mat = basis * mat * basis.I
mat_list = list(mat.flat)
name = '%s%d' % (prefix, i)
cmd.create(name, object)
cmd.transform_object(name, mat_list, 0)
cmd.color(i+1, name)
def pdbremarks(filename):
'''
DESCRIPTION
API only. Read REMARK lines from PDB file. Return dictionary with
remarkNum as key and list of lines as value.
'''
remarks = dict()
if not cmd.is_string(filename):
f = filename
elif filename[-3:] == '.gz':
import gzip
f = gzip.open(filename)
else:
f = open(filename)
for line in f:
recname = line[0:6]
if recname == 'REMARK':
num = int(line[7:10])
lstring = line[11:]
remarks.setdefault(num, []).append(lstring)
return remarks
def biomolecule(name=None, filename=None, prefix=None, number=1, suffix=None,
quiet=0):
'''
DESCRIPTION
Create biological unit (quaternary structure) as annotated by the REMARK
350 BIOMOLECULE record.
USAGE
biomolecule name [, filename [, prefix [, number ]]]
ARGUMENTS
name = string: name of object and basename of PDB file, if
filename is not given {default: first loaded object}
filename = string: file to read remarks from {default: <name>.pdb}
prefix = string: prefix for new objects {default: <name>}
EXAMPLE
fetch 1rmv, bsync=0
biomolecule 1rmv
'''
import os
from .importing import local_mirror_pdb
try:
import numpy
except ImportError:
numpy = None
number, quiet = int(number), int(quiet)
if name is None:
name = cmd.get_object_list()[0]
if prefix is None:
prefix = name
if suffix is None:
suffix = str(number)
if filename is None:
candidates = [
'%s.pdb' % (name),
'%s/%s.pdb' % (cmd.get('fetch_path'), name),
local_mirror_pdb(name),
]
for filename in candidates:
if os.path.exists(filename):
break
else:
print('please provide filename')
raise CmdException
if not quiet:
print('loading from %s' % (filename))
remarks = pdbremarks(filename)
if 350 not in remarks:
print('There is no REMARK 350 in ' + filename)
raise CmdException
current = 1
biomt = {current: {}}
chains = tuple()
for line in remarks[350]:
if line.startswith('BIOMOLECULE:'):
current = int(line[12:])
biomt[current] = {}
elif line.startswith('APPLY THE FOLLOWING TO CHAINS:'):
chains = tuple(chain.strip() for chain in line[30:].split(','))
elif line.startswith(' AND CHAINS:'):
chains += tuple(chain.strip() for chain in line[30:].split(','))
elif line.startswith(' BIOMT'):
row = int(line[7])
num = int(line[8:12])
vec = line[12:].split()
vec = list(map(float, vec))
biomt[current].setdefault(chains, dict()).setdefault(num, []).extend(vec)
if number not in biomt or len(biomt[number]) == 0:
print(' Error: no BIOMOLECULE number %d' % (number))
raise CmdException
if numpy is not None:
mat_source = numpy.reshape(cmd.get_object_matrix(name), (4,4))
mat_source = numpy.matrix(mat_source)
for chains, matrices in biomt[number].items():
for num in matrices:
mat = matrices[num][0:12]
mat.extend([0,0,0,1])
copy = '%s_%s_%d' % (prefix, suffix, num)
if not quiet:
print('creating %s' % (copy))
cmd.create(copy, 'model %s and chain %s' % (name, '+'.join(chains)))
cmd.alter(copy, 'segi="%d"' % (num))
if numpy is not None:
mat = mat_source * numpy.reshape(mat, (4,4)) * mat_source.I
mat = list(mat.flat)
cmd.transform_object(copy, mat)
cmd.disable(name)
cmd.group('%s_%s' % (prefix, suffix), '%s_%s_*' % (prefix, suffix))
cmd.extend('supercell', supercell)
cmd.extend('symexpcell', symexpcell)
cmd.extend('biomolecule', biomolecule)
# tab-completion of arguments
cmd.auto_arg[0]['biomolecule'] = cmd.auto_arg[0]['pseudoatom']
cmd.auto_arg[3]['supercell'] = cmd.auto_arg[0]['pseudoatom']
# vi: ts=4:sw=4:smarttab:expandtab