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=None, colour_end=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: The starting colour of the linear gradient.
215 @type colour_start: str or RBG colour array (len 3 with vals from 0 to 1)
216 @keyword colour_end: The ending colour of the linear gradient.
217 @type colour_end: str or RBG colour array (len 3 with vals from 0 to 1)
218 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
219 @type colour_list: str or None
220 """
221
222
223 pipes.test()
224
225
226 if not exists_mol_res_spin_data():
227 raise RelaxNoSequenceError
228
229
230 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)
231
232
233 for command in commands:
234 molmol_obj.exec_cmd(command)
235
236
253
254
255 -def macro_write(data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None, file=None, dir=None, force=False):
256 """Create a Molmol macro.
257
258 @keyword data_type: The data type to map to the structure.
259 @type data_type: str
260 @keyword style: The style of the macro.
261 @type style: str
262 @keyword colour_start: The starting colour of the linear gradient.
263 @type colour_start: str or RBG colour array (len 3 with vals from 0 to 1)
264 @keyword colour_end: The ending colour of the linear gradient.
265 @type colour_end: str or RBG colour array (len 3 with vals from 0 to 1)
266 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'.
267 @type colour_list: str or None
268 @keyword file: The name of the macro file to create.
269 @type file: str
270 @keyword dir: The name of the directory to place the macro file into.
271 @type dir: str
272 @keyword force: Flag which if set to True will cause any pre-existing file to be overwritten.
273 @type force: bool
274 """
275
276
277 pipes.test()
278
279
280 if not exists_mol_res_spin_data():
281 raise RelaxNoSequenceError
282
283
284 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list)
285
286
287 if file == None:
288 file = data_type + '.mac'
289
290
291 file_path = get_file_path(file, dir)
292 file = open_write_file(file, dir, force)
293
294
295 for command in commands:
296 file.write(command + "\n")
297
298
299 file.close()
300
301
302 if not hasattr(cdp, 'result_files'):
303 cdp.result_files = []
304 cdp.result_files.append(['molmol', 'Molmol', file_path])
305 status.observers.result_file.notify()
306
307
320
321
358
359
368