1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for interfacing with PyMOL."""
24
25
26 import dep_check
27
28
29 if dep_check.pymol_module:
30 import pymol
31 from math import pi
32 from numpy import float64, transpose, zeros
33 from os import F_OK, access, pardir, sep
34 PIPE, Popen = None, None
35 if dep_check.subprocess_module:
36 from subprocess import PIPE, Popen
37 from tempfile import mktemp
38 from time import sleep
39
40
41 from lib.errors import RelaxError, RelaxNoPdbError, RelaxNoSequenceError
42 from lib.geometry.rotations import euler_to_R_zyz, R_to_axis_angle
43 from lib.io import delete, file_root, get_file_path, open_read_file, open_write_file, test_binary
44 from pipe_control import pipes
45 from pipe_control.mol_res_spin import exists_mol_res_spin_data
46 from pipe_control.result_files import add_result_file
47 from specific_analyses.setup import get_specific_fn
48 from status import Status; status = Status()
49
50
52 """The PyMOL execution object."""
53
55 """Set up the PyMOL execution object.
56
57 @keyword exec_mode: The execution mode which can be either 'module' or 'external'.
58 @type exec_mode: None or str
59 """
60
61
62 self.command_history = ""
63
64
65 self.exec_mode = exec_mode
66 if not exec_mode:
67 if dep_check.pymol_module:
68 self.exec_mode = 'module'
69 self.open = False
70 else:
71 self.exec_mode = 'external'
72
73
74 - def clear_history(self):
75 """Clear the PyMOL command history."""
76
77 self.command_history = ""
78
79
80 - def exec_cmd(self, command=None, store_command=True):
81 """Execute a PyMOL command.
82
83 @param command: The PyMOL command to send into the program.
84 @type command: str
85 @param store_command: A flag specifying if the command should be stored in the history
86 variable.
87 @type store_command: bool
88 """
89
90
91 if not self.running():
92 self.open_gui()
93
94
95 if self.exec_mode == 'module':
96 pymol.cmd.do(command)
97 else:
98 self.pymol.write(command + '\n')
99
100
101 if store_command:
102 self.command_history = self.command_history + command + "\n"
103
104
106 """Open the PyMOL GUI."""
107
108
109 if self.exec_mode == 'module':
110
111 pymol.finish_launching()
112 self.open = True
113
114
115 if self.exec_mode == 'external':
116
117 test_binary('pymol')
118
119
120 if Popen == None:
121 raise RelaxError("The subprocess module is not available in this version of Python.")
122
123
124 self.pymol = Popen(['pymol', '-qpK'], stdin=PIPE).stdin
125
126
127 if len(self.command_history) > 0:
128 self.exec_cmd(self.command_history, store_command=0)
129 return
130
131
132 if hasattr(cdp, 'structure'):
133 self.open_pdb()
134
135
137 """Open the PDB file in PyMOL."""
138
139
140 if not self.running():
141 return
142
143
144 self.exec_cmd("reinitialize")
145
146
147 open_files = []
148 for model in cdp.structure.structural_data:
149 for mol in model.mol:
150
151 file_path = None
152 if access(mol.file_name, F_OK):
153 file_path = mol.file_name
154
155
156 if file_path == None and hasattr(mol, 'file_path') and mol.file_path != None:
157 file_path = mol.file_path + sep + mol.file_name
158 if not access(file_path, F_OK):
159 file_path = None
160
161
162 if file_path == None and hasattr(mol, 'file_path_abs') and mol.file_path_abs != None:
163 file_path = mol.file_path_abs + sep + mol.file_name
164 if not access(file_path, F_OK):
165 file_path = None
166
167
168 if file_path == None and hasattr(mol, 'file_path') and mol.file_path != None:
169 file_path = pardir + sep + mol.file_path + sep + mol.file_name
170 if not access(file_path, F_OK):
171 file_path = None
172
173
174 if file_path == None:
175 file_path = mol.file_name
176
177
178 if file_path in open_files:
179 continue
180
181
182 if file_path in open_files:
183 continue
184
185
186 self.exec_cmd("load " + file_path)
187
188
189 open_files.append(file_path)
190
191
193 """Test if PyMOL is running.
194
195 @return: Whether the Molmol pipe is open or not.
196 @rtype: bool
197 """
198
199
200 if self.exec_mode == 'module':
201 return self.open
202
203
204 if self.exec_mode == 'external':
205
206 if not hasattr(self, 'pymol'):
207 return False
208
209
210 try:
211 self.pymol.write('\n')
212 except IOError:
213 return False
214
215
216 return True
217
218
219
220
221 pymol_obj = Pymol('external')
222 """Pymol data container instance."""
223
224
225
227 """Apply the PyMOL cartoon style and colour by secondary structure."""
228
229
230 pipes.test()
231
232
233 if not hasattr(cdp, 'structure'):
234 raise RelaxNoPdbError
235
236
237 open_files = []
238 for model in cdp.structure.structural_data:
239 for mol in model.mol:
240
241 pdb_file = mol.file_name
242 if mol.file_path:
243 pdb_file = mol.file_path + sep + pdb_file
244 id = file_root(pdb_file)
245
246
247 if pdb_file in open_files:
248 continue
249
250
251 open_files.append(pdb_file)
252
253
254 pymol_obj.exec_cmd("cmd.hide('everything'," + repr(id) + ")")
255
256
257 pymol_obj.exec_cmd("cmd.show('cartoon'," + repr(id) + ")")
258
259
260 pymol_obj.exec_cmd("util.cbss(" + repr(id) + ", 'red', 'yellow', 'green')")
261
262
264 """Function for sending PyMOL commands to the program pipe.
265
266 @param command: The command to send into the program.
267 @type command: str
268 """
269
270
271 pymol_obj.exec_cmd(command)
272
273
275 """Display the cone geometric object.
276
277 @keyword file: The name of the file containing the cone geometric object.
278 @type file: str
279 """
280
281
282 pymol_obj.exec_cmd("load " + file)
283
284
285
286
287
288
289 pymol_obj.exec_cmd("select (resn AVE,AXE,SIM)")
290
291
292 pymol_obj.exec_cmd("show stick, 'sele'")
293
294
295 pymol_obj.exec_cmd("color cyan, 'sele'")
296
297
298 pymol_obj.exec_cmd("select (resn AVE,AXE,SIM and symbol N)")
299
300
301 pymol_obj.exec_cmd("hide ('sele')")
302
303
304 pymol_obj.exec_cmd("cmd.label(\"sele\",\"name\")")
305
306
307
308
309
310
311 pymol_obj.exec_cmd("select (resn CON,EDG)")
312
313
314 pymol_obj.exec_cmd("hide ('sele')")
315
316
317 pymol_obj.exec_cmd("show sticks, 'sele'")
318
319
320 pymol_obj.exec_cmd("color white, 'sele'")
321
322
323 pymol_obj.exec_cmd("set stick_radius,0.15000")
324
325
326 pymol_obj.exec_cmd("set stick_transparency, 0.3")
327
328
329
330
331
332
333 pymol_obj.exec_cmd("cmd.delete('sele')")
334
335
336
337
338
339
340 if hasattr(cdp, 'ave_pos_beta'):
341
342 ave_pos_R = zeros((3, 3), float64)
343 ave_pos_alpha = 0.0
344 if hasattr(cdp, 'ave_pos_alpha') and cdp.ave_pos_alpha != None:
345 ave_pos_alpha = cdp.ave_pos_alpha
346 euler_to_R_zyz(ave_pos_alpha, cdp.ave_pos_beta, cdp.ave_pos_gamma, ave_pos_R)
347
348
349 R = transpose(ave_pos_R)
350
351
352 axis, angle = R_to_axis_angle(R)
353
354
355 for i in range(len(cdp.domain_to_pdb)):
356 if cdp.domain_to_pdb[i][0] != cdp.ref_domain:
357 pdb = cdp.domain_to_pdb[i][1]
358
359
360 pymol_obj.exec_cmd("cmd.rotate([%s, %s, %s], %s, '%s', origin=[%s, %s, %s])" % (axis[0], axis[1], axis[2], angle/pi*180.0, pdb, cdp.pivot[0], cdp.pivot[1], cdp.pivot[2]))
361
362
363 -def create_macro(data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None):
364 """Create an array of PyMOL commands.
365
366 @keyword data_type: The data type to map to the structure.
367 @type data_type: str
368 @keyword style: The style of the macro.
369 @type style: str
370 @keyword colour_start: The starting colour of the linear gradient.
371 @type colour_start: str or RBG colour array (len 3 with vals from 0 to 1)
372 @keyword colour_end: The ending colour of the linear gradient.
373 @type colour_end: str or RBG colour array (len 3 with vals from 0 to 1)
374 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
375 @type colour_list: str or None
376 @return: The list of PyMOL commands.
377 @rtype: list of str
378 """
379
380
381 macro = get_specific_fn('pymol_macro', cdp.pipe_type)
382
383
384 commands = macro(data_type, style, colour_start, colour_end, colour_list)
385
386
387 return commands
388
389
390 -def macro_apply(data_type=None, style="classic", colour_start_name=None, colour_start_rgb=None, colour_end_name=None, colour_end_rgb=None, colour_list=None):
391 """Execute a PyMOL macro.
392
393 @keyword data_type: The data type to map to the structure.
394 @type data_type: str
395 @keyword style: The style of the macro.
396 @type style: str
397 @keyword colour_start_name: The name of the starting colour of the linear gradient.
398 @type colour_start_name: str
399 @keyword colour_start_rgb: The RGB array starting colour of the linear gradient.
400 @type colour_start_rgb: RBG colour array (len 3 with vals from 0 to 1)
401 @keyword colour_end_name: The name of the ending colour of the linear gradient.
402 @type colour_end_name: str
403 @keyword colour_end_rgb: The RGB array ending colour of the linear gradient.
404 @type colour_end_rgb: RBG colour array (len 3 with vals from 0 to 1)
405 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
406 @type colour_list: str or None
407 """
408
409
410 pipes.test()
411
412
413 if not exists_mol_res_spin_data():
414 raise RelaxNoSequenceError
415
416
417 if colour_start_name != None and colour_start_rgb != None:
418 raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.")
419 if colour_end_name != None and colour_end_rgb != None:
420 raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.")
421
422
423 if colour_start_name != None:
424 colour_start = colour_start_name
425 else:
426 colour_start = colour_start_rgb
427 if colour_end_name != None:
428 colour_end = colour_end_name
429 else:
430 colour_end = colour_end_rgb
431
432
433 pymol_obj.clear_history()
434
435
436 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)
437
438
439 try:
440
441 tmpfile = "%s.pml" % mktemp()
442
443
444 file = open(tmpfile, 'w')
445
446
447 for command in commands:
448 file.write("%s\n" % command)
449 file.close()
450
451
452 pymol_obj.exec_cmd("@%s" % tmpfile)
453
454
455 sleep(3)
456
457
458 finally:
459
460 delete(tmpfile, fail=False)
461
462
479
480
481 -def macro_write(data_type=None, style="classic", colour_start_name=None, colour_start_rgb=None, colour_end_name=None, colour_end_rgb=None, colour_list=None, file=None, dir=None, force=False):
482 """Create a PyMOL macro file.
483
484 @keyword data_type: The data type to map to the structure.
485 @type data_type: str
486 @keyword style: The style of the macro.
487 @type style: str
488 @keyword colour_start_name: The name of the starting colour of the linear gradient.
489 @type colour_start_name: str
490 @keyword colour_start_rgb: The RGB array starting colour of the linear gradient.
491 @type colour_start_rgb: RBG colour array (len 3 with vals from 0 to 1)
492 @keyword colour_end_name: The name of the ending colour of the linear gradient.
493 @type colour_end_name: str
494 @keyword colour_end_rgb: The RGB array ending colour of the linear gradient.
495 @type colour_end_rgb: RBG colour array (len 3 with vals from 0 to 1)
496 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
497 @type colour_list: str or None
498 @keyword file: The name of the macro file to create.
499 @type file: str
500 @keyword dir: The name of the directory to place the macro file into.
501 @type dir: str
502 @keyword force: Flag which if set to True will cause any pre-existing file to be overwritten.
503 @type force: bool
504 """
505
506
507 pipes.test()
508
509
510 if not exists_mol_res_spin_data():
511 raise RelaxNoSequenceError
512
513
514 if colour_start_name != None and colour_start_rgb != None:
515 raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.")
516 if colour_end_name != None and colour_end_rgb != None:
517 raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.")
518
519
520 if colour_start_name != None:
521 colour_start = colour_start_name
522 else:
523 colour_start = colour_start_rgb
524 if colour_end_name != None:
525 colour_end = colour_end_name
526 else:
527 colour_end = colour_end_rgb
528
529
530 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)
531
532
533 if file == None:
534 file = data_type + '.pml'
535
536
537 file_path = get_file_path(file, dir)
538 file = open_write_file(file, dir, force)
539
540
541 for command in commands:
542 file.write(command + "\n")
543
544
545 file.close()
546
547
548 add_result_file(type='pymol', label='PyMOL', file=file_path)
549
550
629
630
632 """Display the XH bond vector distribution.
633
634 @keyword file: The vector distribution PDB file.
635 @type file: str
636 """
637
638
639 pipes.test()
640
641
642 id = file_root(file)
643
644
645 pymol_obj.exec_cmd("load " + file)
646
647
648
649
650
651
652 pymol_obj.exec_cmd("cmd.show('surface', " + repr(id) + ")")
653
654
663