Package generic_fns :: Module molmol
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.molmol

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2011 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module for interfacing with Molmol.""" 
 25   
 26  # Python module imports. 
 27  from os import sep 
 28  from string import split 
 29  from subprocess import PIPE, Popen 
 30  from time import sleep 
 31   
 32  # relax module imports. 
 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   
41 -class Molmol:
42 """The Molmol execution object.""" 43
44 - def __init__(self):
45 """Set up the Molmol execution object.""" 46 47 # Variable for storing the Molmol command history. 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 # Reopen the pipe if needed. 71 if not self.running(): 72 self.open_gui() 73 74 # Write the command to the pipe. 75 self.molmol.write(command + '\n') 76 77 # Place the command in the command history. 78 if store_command: 79 self.command_history = self.command_history + command + "\n"
80 81
82 - def open_gui(self):
83 """Open a Molmol pipe.""" 84 85 # Test that the Molmol binary exists. 86 test_binary('molmol') 87 88 # Open Molmol as a pipe. 89 self.molmol = Popen(['molmol', '-f', '-'], stdin=PIPE).stdin 90 91 # Execute the command history. 92 if len(self.command_history) > 0: 93 self.exec_cmd(self.command_history, store_command=0) 94 return 95 96 # Wait a little while for Molmol to initialise. 97 sleep(2) 98 99 # Test if the PDB file has been loaded. 100 if hasattr(cdp, 'structure'): 101 self.open_pdb() 102 103 # Run InitAll to remove everything from Molmol. 104 else: 105 self.molmol.write("InitAll yes\n")
106 107
108 - def open_pdb(self):
109 """Open the PDB file in Molmol.""" 110 111 # Test if Molmol is running. 112 if not self.running(): 113 return 114 115 # Run InitAll to remove everything from molmol. 116 self.exec_cmd("InitAll yes") 117 118 # Open the PDB files. 119 open_files = [] 120 for model in cdp.structure.structural_data: 121 for mol in model.mol: 122 # The file path. 123 file = mol.file_name 124 if mol.file_path: 125 file = mol.file_path + sep + file 126 127 # Already loaded. 128 if file in open_files: 129 continue 130 131 # Open the file in Molmol. 132 self.exec_cmd("ReadPdb " + file) 133 134 # Add to the open file list. 135 open_files.append(file)
136 137
138 - def running(self):
139 """Test if Molmol is running. 140 141 @return: Whether the Molmol pipe is open or not. 142 @rtype: bool 143 """ 144 145 # Pipe exists. 146 if not hasattr(self, 'molmol'): 147 return False 148 149 # Test if the pipe has been broken. 150 try: 151 self.molmol.write('\n') 152 except IOError: 153 import sys 154 sys.stderr.write("Broken pipe") 155 return False 156 157 # Molmol is running 158 return True
159 160 161 162 # Initialise the Molmol executable object. 163 molmol_obj = Molmol() 164 """Molmol data container instance.""" 165 166 167 168
169 -def command(command):
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 # Pass the command to Molmol. 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 # Specific Molmol macro creation function. 198 macro = get_specific_fn('molmol_macro', cdp.pipe_type) 199 200 # Get the macro. 201 commands = macro(data_type, style, colour_start, colour_end, colour_list) 202 203 # Return the macro commands. 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 # Test if the current data pipe exists. 223 pipes.test() 224 225 # Test if sequence data exists. 226 if not exists_mol_res_spin_data(): 227 raise RelaxNoSequenceError 228 229 # Create the macro. 230 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 231 232 # Loop over the commands and execute them. 233 for command in commands: 234 molmol_obj.exec_cmd(command)
235 236
237 -def macro_run(file=None, dir=None):
238 """Execute the Molmol macro from the given text file. 239 240 @keyword file: The name of the macro file to execute. 241 @type file: str 242 @keyword dir: The name of the directory where the macro file is located. 243 @type dir: str 244 """ 245 246 # Open the file for reading. 247 file_path = get_file_path(file, dir) 248 file = open_read_file(file, dir) 249 250 # Loop over the commands and apply them. 251 for command in file.readlines(): 252 molmol_obj.exec_cmd(command)
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 # Test if the current data pipe exists. 277 pipes.test() 278 279 # Test if sequence data exists. 280 if not exists_mol_res_spin_data(): 281 raise RelaxNoSequenceError 282 283 # Create the macro. 284 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 285 286 # File name. 287 if file == None: 288 file = data_type + '.mac' 289 290 # Open the file for writing. 291 file_path = get_file_path(file, dir) 292 file = open_write_file(file, dir, force) 293 294 # Loop over the commands and write them. 295 for command in commands: 296 file.write(command + "\n") 297 298 # Close the file. 299 file.close() 300 301 # Add the file to the results file list. 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
308 -def ribbon():
309 """Apply the Molmol ribbon style.""" 310 311 # Calculate the protons. 312 molmol_obj.exec_cmd("CalcAtom 'H'") 313 molmol_obj.exec_cmd("CalcAtom 'HN'") 314 315 # Calculate the secondary structure. 316 molmol_obj.exec_cmd("CalcSecondary") 317 318 # Execute the ribbon macro. 319 molmol_obj.exec_cmd("XMacStand ribbon.mac")
320 321
322 -def tensor_pdb(file=None):
323 """Display the diffusion tensor geometric structure. 324 325 @keyword file: The name of the PDB file containing the tensor geometric object. 326 @type file: str 327 """ 328 329 # Test if the current data pipe exists. 330 pipes.test() 331 332 # To overlay the structure with the diffusion tensor, select all and reorient to the PDB frame. 333 molmol_obj.exec_cmd("SelectAtom ''") 334 molmol_obj.exec_cmd("SelectBond ''") 335 molmol_obj.exec_cmd("SelectAngle ''") 336 molmol_obj.exec_cmd("SelectDist ''") 337 molmol_obj.exec_cmd("SelectPrim ''") 338 molmol_obj.exec_cmd("RotateInit") 339 molmol_obj.exec_cmd("MoveInit") 340 341 # Read in the tensor PDB file and force Molmol to recognise the CONECT records (not that it will show the bonds)! 342 molmol_obj.exec_cmd("ReadPdb " + file) 343 file_parts = split(file, '.') 344 molmol_obj.exec_cmd("SelectMol '@" + file_parts[0] + "'") 345 molmol_obj.exec_cmd("CalcBond 1 1 1") 346 347 # Apply the 'ball/stick' style to the tensor. 348 molmol_obj.exec_cmd("SelectAtom '0'") 349 molmol_obj.exec_cmd("SelectBond '0'") 350 molmol_obj.exec_cmd("SelectAtom ':TNS'") 351 molmol_obj.exec_cmd("SelectBond ':TNS'") 352 molmol_obj.exec_cmd("XMacStand ball_stick.mac") 353 354 # Touch up. 355 molmol_obj.exec_cmd("RadiusAtom 1") 356 molmol_obj.exec_cmd("SelectAtom ':TNS@C*'") 357 molmol_obj.exec_cmd("RadiusAtom 1.5")
358 359
360 -def view():
361 """Start Molmol.""" 362 363 # Open a Molmol pipe. 364 if molmol_obj.running(): 365 raise RelaxError("The Molmol pipe already exists.") 366 else: 367 molmol_obj.open_gui()
368