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 Molmol."""
24
25
26 import dep_check
27
28
29 from os import sep
30 PIPE, Popen = None, None
31 if dep_check.subprocess_module:
32 from subprocess import PIPE, Popen
33 from time import sleep
34
35
36 from generic_fns.mol_res_spin import exists_mol_res_spin_data
37 from generic_fns import pipes
38 from generic_fns.result_files import add_result_file
39 from relax_errors import RelaxError, RelaxNoSequenceError
40 from relax_io import get_file_path, open_read_file, open_write_file, test_binary
41 from specific_fns.setup import get_specific_fn
42 from status import Status; status = Status()
43
44
46 """The Molmol execution object."""
47
49 """Set up the Molmol execution object."""
50
51
52 self.command_history = ""
53
54
55 - def clear_history(self):
56 """Clear the Molmol command history."""
57
58 self.command_history = ""
59
60
61 - def exec_cmd(self, command=None, store_command=True):
62 """Write to the Molmol pipe.
63
64 This function is also used to execute a user supplied Molmol command.
65
66
67 @param command: The Molmol command to send into the program.
68 @type command: str
69 @param store_command: A flag specifying if the command should be stored in the history
70 variable.
71 @type store_command: bool
72 """
73
74
75 if not self.running():
76 self.open_gui()
77
78
79 self.molmol.write(command + '\n')
80
81
82 if store_command:
83 self.command_history = self.command_history + command + "\n"
84
85
87 """Open a Molmol pipe."""
88
89
90 test_binary('molmol')
91
92
93 if Popen == None:
94 raise RelaxError("The subprocess module is not available in this version of Python.")
95
96
97 self.molmol = Popen(['molmol', '-f', '-'], stdin=PIPE).stdin
98
99
100 if len(self.command_history) > 0:
101 self.exec_cmd(self.command_history, store_command=0)
102 return
103
104
105 sleep(2)
106
107
108 if hasattr(cdp, 'structure'):
109 self.open_pdb()
110
111
112 else:
113 self.molmol.write("InitAll yes\n")
114
115
117 """Open the PDB file in Molmol."""
118
119
120 if not self.running():
121 return
122
123
124 self.exec_cmd("InitAll yes")
125
126
127 open_files = []
128 for model in cdp.structure.structural_data:
129 for mol in model.mol:
130
131 file = mol.file_name
132 if mol.file_path:
133 file = mol.file_path + sep + file
134
135
136 if file in open_files:
137 continue
138
139
140 self.exec_cmd("ReadPdb " + file)
141
142
143 open_files.append(file)
144
145
147 """Test if Molmol is running.
148
149 @return: Whether the Molmol pipe is open or not.
150 @rtype: bool
151 """
152
153
154 if not hasattr(self, 'molmol'):
155 return False
156
157
158 try:
159 self.molmol.write('\n')
160 except IOError:
161 import sys
162 sys.stderr.write("Broken pipe")
163 return False
164
165
166 return True
167
168
169
170
171 molmol_obj = Molmol()
172 """Molmol data container instance."""
173
174
175
176
178 """Function for sending Molmol commands to the program pipe.
179
180 @param command: The command to send into the program.
181 @type command: str
182 """
183
184
185 molmol_obj.exec_cmd(command)
186
187
188 -def create_macro(data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None):
189 """Create an array of Molmol commands.
190
191 @keyword data_type: The data type to map to the structure.
192 @type data_type: str
193 @keyword style: The style of the macro.
194 @type style: str
195 @keyword colour_start: The starting colour of the linear gradient.
196 @type colour_start: str or RBG colour array (len 3 with vals from 0 to 1)
197 @keyword colour_end: The ending colour of the linear gradient.
198 @type colour_end: str or RBG colour array (len 3 with vals from 0 to 1)
199 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
200 @type colour_list: str or None
201 @return: The list of Molmol commands.
202 @rtype: list of str
203 """
204
205
206 macro = get_specific_fn('molmol_macro', cdp.pipe_type)
207
208
209 commands = macro(data_type, style, colour_start, colour_end, colour_list)
210
211
212 return commands
213
214
215 -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):
216 """Execute a Molmol macro.
217
218 @keyword data_type: The data type to map to the structure.
219 @type data_type: str
220 @keyword style: The style of the macro.
221 @type style: str
222 @keyword colour_start_name: The name of the starting colour of the linear gradient.
223 @type colour_start_name: str
224 @keyword colour_start_rgb: The RGB array starting colour of the linear gradient.
225 @type colour_start_rgb: RBG colour array (len 3 with vals from 0 to 1)
226 @keyword colour_end_name: The name of the ending colour of the linear gradient.
227 @type colour_end_name: str
228 @keyword colour_end_rgb: The RGB array ending colour of the linear gradient.
229 @type colour_end_rgb: RBG colour array (len 3 with vals from 0 to 1)
230 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
231 @type colour_list: str or None
232 """
233
234
235 pipes.test()
236
237
238 if not exists_mol_res_spin_data():
239 raise RelaxNoSequenceError
240
241
242 if colour_start_name != None and colour_start_rgb != None:
243 raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.")
244 if colour_end_name != None and colour_end_rgb != None:
245 raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.")
246
247
248 if colour_start_name != None:
249 colour_start = colour_start_name
250 else:
251 colour_start = colour_start_rgb
252 if colour_end_name != None:
253 colour_end = colour_end_name
254 else:
255 colour_end = colour_end_rgb
256
257
258 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)
259
260
261 for command in commands:
262 molmol_obj.exec_cmd(command)
263
264
281
282
283 -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):
284 """Create a Molmol macro.
285
286 @keyword data_type: The data type to map to the structure.
287 @type data_type: str
288 @keyword style: The style of the macro.
289 @type style: str
290 @keyword colour_start_name: The name of the starting colour of the linear gradient.
291 @type colour_start_name: str
292 @keyword colour_start_rgb: The RGB array starting colour of the linear gradient.
293 @type colour_start_rgb: RBG colour array (len 3 with vals from 0 to 1)
294 @keyword colour_end_name: The name of the ending colour of the linear gradient.
295 @type colour_end_name: str
296 @keyword colour_end_rgb: The RGB array ending colour of the linear gradient.
297 @type colour_end_rgb: RBG colour array (len 3 with vals from 0 to 1)
298 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
299 @type colour_list: str or None
300 @keyword file: The name of the macro file to create.
301 @type file: str
302 @keyword dir: The name of the directory to place the macro file into.
303 @type dir: str
304 @keyword force: Flag which if set to True will cause any pre-existing file to be overwritten.
305 @type force: bool
306 """
307
308
309 pipes.test()
310
311
312 if not exists_mol_res_spin_data():
313 raise RelaxNoSequenceError
314
315
316 if colour_start_name != None and colour_start_rgb != None:
317 raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.")
318 if colour_end_name != None and colour_end_rgb != None:
319 raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.")
320
321
322 if colour_start_name != None:
323 colour_start = colour_start_name
324 else:
325 colour_start = colour_start_rgb
326 if colour_end_name != None:
327 colour_end = colour_end_name
328 else:
329 colour_end = colour_end_rgb
330
331
332 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)
333
334
335 if file == None:
336 file = data_type + '.mac'
337
338
339 file_path = get_file_path(file, dir)
340 file = open_write_file(file, dir, force)
341
342
343 for command in commands:
344 file.write(command + "\n")
345
346
347 file.close()
348
349
350 add_result_file(type='molmol', label='Molmol', file=file_path)
351
352
365
366
403
404
413