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