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 F_OK, access, 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 lib.errors import RelaxError, RelaxNoSequenceError 
 37  from lib.io import get_file_path, open_read_file, open_write_file, test_binary 
 38  from pipe_control.mol_res_spin import exists_mol_res_spin_data 
 39  from pipe_control import pipes 
 40  from pipe_control.result_files import add_result_file 
 41  from specific_analyses.api import return_api 
 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_path = None 
132                  if access(mol.file_name, F_OK): 
133                      file_path = mol.file_name 
134   
135                   
136                  if file_path == None and hasattr(mol, 'file_path') and mol.file_path != None: 
137                      file_path = mol.file_path + sep + mol.file_name 
138                      if not access(file_path, F_OK): 
139                          file_path = None 
140   
141                   
142                  if file_path == None and hasattr(mol, 'file_path_abs') and mol.file_path_abs != None: 
143                      file_path = mol.file_path_abs + sep + mol.file_name 
144                      if not access(file_path, F_OK): 
145                          file_path = None 
146   
147                   
148                  if file_path == None and hasattr(mol, 'file_path') and mol.file_path != None: 
149                      file_path = pardir + sep + mol.file_path + sep + mol.file_name 
150                      if not access(file_path, F_OK): 
151                          file_path = None 
152   
153                   
154                  if file_path == None: 
155                      file_path = mol.file_name 
156   
157                   
158                  if file_path in open_files: 
159                      continue 
160   
161                   
162                  self.exec_cmd("ReadPdb " + file_path) 
163   
164                   
165                  open_files.append(file_path) 
 166   
167   
169          """Test if Molmol is running. 
170   
171          @return:    Whether the Molmol pipe is open or not. 
172          @rtype:     bool 
173          """ 
174   
175           
176          if not hasattr(self, 'molmol'): 
177              return False 
178   
179           
180          try: 
181              self.molmol.write('\n') 
182          except IOError: 
183              import sys 
184              sys.stderr.write("Broken pipe") 
185              return False 
186   
187           
188          return True 
  189   
190   
191   
192   
193  molmol_obj = Molmol() 
194  """Molmol data container instance.""" 
195   
196   
197   
198   
200      """Function for sending Molmol commands to the program pipe. 
201   
202      @param command: The command to send into the program. 
203      @type command:  str 
204      """ 
205   
206       
207      molmol_obj.exec_cmd(command) 
 208   
209   
210 -def create_macro(data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None): 
 211      """Create an array of Molmol commands. 
212   
213      @keyword data_type:     The data type to map to the structure. 
214      @type data_type:        str 
215      @keyword style:         The style of the macro. 
216      @type style:            str 
217      @keyword colour_start:  The starting colour of the linear gradient. 
218      @type colour_start:     str or RBG colour array (len 3 with vals from 0 to 1) 
219      @keyword colour_end:    The ending colour of the linear gradient. 
220      @type colour_end:       str or RBG colour array (len 3 with vals from 0 to 1) 
221      @keyword colour_list:   The colour list to search for the colour names.  Can be either 'molmol' or 'x11'. 
222      @type colour_list:      str or None 
223      @return:                The list of Molmol commands. 
224      @rtype:                 list of str 
225      """ 
226   
227       
228      api = return_api() 
229      commands = api.molmol_macro(data_type, style, colour_start, colour_end, colour_list) 
230   
231       
232      return commands 
 233   
234   
235 -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): 
 236      """Execute a Molmol macro. 
237   
238      @keyword data_type:         The data type to map to the structure. 
239      @type data_type:            str 
240      @keyword style:             The style of the macro. 
241      @type style:                str 
242      @keyword colour_start_name: The name of the starting colour of the linear gradient. 
243      @type colour_start_name:    str 
244      @keyword colour_start_rgb:  The RGB array starting colour of the linear gradient. 
245      @type colour_start_rgb:     RBG colour array (len 3 with vals from 0 to 1) 
246      @keyword colour_end_name:   The name of the ending colour of the linear gradient. 
247      @type colour_end_name:      str 
248      @keyword colour_end_rgb:    The RGB array ending colour of the linear gradient. 
249      @type colour_end_rgb:       RBG colour array (len 3 with vals from 0 to 1) 
250      @keyword colour_list:       The colour list to search for the colour names.  Can be either 'molmol' or 'x11'. 
251      @type colour_list:          str or None 
252      """ 
253   
254       
255      pipes.test() 
256   
257       
258      if not exists_mol_res_spin_data(): 
259          raise RelaxNoSequenceError 
260   
261       
262      if colour_start_name != None and colour_start_rgb != None: 
263          raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.") 
264      if colour_end_name != None and colour_end_rgb != None: 
265          raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.") 
266   
267       
268      if colour_start_name != None: 
269          colour_start = colour_start_name 
270      else: 
271          colour_start = colour_start_rgb 
272      if colour_end_name != None: 
273          colour_end = colour_end_name 
274      else: 
275          colour_end = colour_end_rgb 
276   
277       
278      commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 
279   
280       
281      for command in commands: 
282          molmol_obj.exec_cmd(command) 
 283   
284   
301   
302   
303 -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): 
 304      """Create a Molmol macro. 
305   
306      @keyword data_type:         The data type to map to the structure. 
307      @type data_type:            str 
308      @keyword style:             The style of the macro. 
309      @type style:                str 
310      @keyword colour_start_name: The name of the starting colour of the linear gradient. 
311      @type colour_start_name:    str 
312      @keyword colour_start_rgb:  The RGB array starting colour of the linear gradient. 
313      @type colour_start_rgb:     RBG colour array (len 3 with vals from 0 to 1) 
314      @keyword colour_end_name:   The name of the ending colour of the linear gradient. 
315      @type colour_end_name:      str 
316      @keyword colour_end_rgb:    The RGB array ending colour of the linear gradient. 
317      @type colour_end_rgb:       RBG colour array (len 3 with vals from 0 to 1) 
318      @keyword colour_list:       The colour list to search for the colour names.  Can be either 'molmol' or 'x11'. 
319      @type colour_list:          str or None 
320      @keyword file:              The name of the macro file to create. 
321      @type file:                 str 
322      @keyword dir:               The name of the directory to place the macro file into. 
323      @type dir:                  str 
324      @keyword force:             Flag which if set to True will cause any pre-existing file to be overwritten. 
325      @type force:                bool 
326      """ 
327   
328       
329      pipes.test() 
330   
331       
332      if not exists_mol_res_spin_data(): 
333          raise RelaxNoSequenceError 
334   
335       
336      if colour_start_name != None and colour_start_rgb != None: 
337          raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.") 
338      if colour_end_name != None and colour_end_rgb != None: 
339          raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.") 
340   
341       
342      if colour_start_name != None: 
343          colour_start = colour_start_name 
344      else: 
345          colour_start = colour_start_rgb 
346      if colour_end_name != None: 
347          colour_end = colour_end_name 
348      else: 
349          colour_end = colour_end_rgb 
350   
351       
352      commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 
353   
354       
355      if file == None: 
356          file = data_type + '.mac' 
357   
358       
359      file_path = get_file_path(file, dir) 
360      file = open_write_file(file, dir, force) 
361   
362       
363      for command in commands: 
364          file.write(command + "\n") 
365   
366       
367      file.close() 
368   
369       
370      add_result_file(type='molmol', label='Molmol', file=file_path) 
 371   
372   
385   
386   
423   
424   
433