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_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 # Test if the current data pipe exists. 227 pipes.test() 228 229 # Test if sequence data exists. 230 if not exists_mol_res_spin_data(): 231 raise RelaxNoSequenceError 232 233 # Check the arguments. 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 # Merge the colour args. 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 # Create the macro. 250 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 251 252 # Loop over the commands and execute them. 253 for command in commands: 254 molmol_obj.exec_cmd(command)
255 256
257 -def macro_run(file=None, dir=None):
258 """Execute the Molmol macro from the given text file. 259 260 @keyword file: The name of the macro file to execute. 261 @type file: str 262 @keyword dir: The name of the directory where the macro file is located. 263 @type dir: str 264 """ 265 266 # Open the file for reading. 267 file_path = get_file_path(file, dir) 268 file = open_read_file(file, dir) 269 270 # Loop over the commands and apply them. 271 for command in file.readlines(): 272 molmol_obj.exec_cmd(command)
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 # Test if the current data pipe exists. 301 pipes.test() 302 303 # Test if sequence data exists. 304 if not exists_mol_res_spin_data(): 305 raise RelaxNoSequenceError 306 307 # Check the arguments. 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 # Merge the colour args. 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 # Create the macro. 324 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 325 326 # File name. 327 if file == None: 328 file = data_type + '.mac' 329 330 # Open the file for writing. 331 file_path = get_file_path(file, dir) 332 file = open_write_file(file, dir, force) 333 334 # Loop over the commands and write them. 335 for command in commands: 336 file.write(command + "\n") 337 338 # Close the file. 339 file.close() 340 341 # Add the file to the results file list. 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
348 -def ribbon():
349 """Apply the Molmol ribbon style.""" 350 351 # Calculate the protons. 352 molmol_obj.exec_cmd("CalcAtom 'H'") 353 molmol_obj.exec_cmd("CalcAtom 'HN'") 354 355 # Calculate the secondary structure. 356 molmol_obj.exec_cmd("CalcSecondary") 357 358 # Execute the ribbon macro. 359 molmol_obj.exec_cmd("XMacStand ribbon.mac")
360 361
362 -def tensor_pdb(file=None):
363 """Display the diffusion tensor geometric structure. 364 365 @keyword file: The name of the PDB file containing the tensor geometric object. 366 @type file: str 367 """ 368 369 # Test if the current data pipe exists. 370 pipes.test() 371 372 # To overlay the structure with the diffusion tensor, select all and reorient to the PDB frame. 373 molmol_obj.exec_cmd("SelectAtom ''") 374 molmol_obj.exec_cmd("SelectBond ''") 375 molmol_obj.exec_cmd("SelectAngle ''") 376 molmol_obj.exec_cmd("SelectDist ''") 377 molmol_obj.exec_cmd("SelectPrim ''") 378 molmol_obj.exec_cmd("RotateInit") 379 molmol_obj.exec_cmd("MoveInit") 380 381 # Read in the tensor PDB file and force Molmol to recognise the CONECT records (not that it will show the bonds)! 382 molmol_obj.exec_cmd("ReadPdb " + file) 383 file_parts = split(file, '.') 384 molmol_obj.exec_cmd("SelectMol '@" + file_parts[0] + "'") 385 molmol_obj.exec_cmd("CalcBond 1 1 1") 386 387 # Apply the 'ball/stick' style to the tensor. 388 molmol_obj.exec_cmd("SelectAtom '0'") 389 molmol_obj.exec_cmd("SelectBond '0'") 390 molmol_obj.exec_cmd("SelectAtom ':TNS'") 391 molmol_obj.exec_cmd("SelectBond ':TNS'") 392 molmol_obj.exec_cmd("XMacStand ball_stick.mac") 393 394 # Touch up. 395 molmol_obj.exec_cmd("RadiusAtom 1") 396 molmol_obj.exec_cmd("SelectAtom ':TNS@C*'") 397 molmol_obj.exec_cmd("RadiusAtom 1.5")
398 399
400 -def view():
401 """Start Molmol.""" 402 403 # Open a Molmol pipe. 404 if molmol_obj.running(): 405 raise RelaxError("The Molmol pipe already exists.") 406 else: 407 molmol_obj.open_gui()
408