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

Source Code for Module generic_fns.molmol

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program 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 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for interfacing with Molmol.""" 
 24   
 25  # Dependencies. 
 26  import dep_check 
 27   
 28  # Python module imports. 
 29  from os import 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  # relax module imports. 
 36  from generic_fns.mol_res_spin import exists_mol_res_spin_data 
 37  from generic_fns import pipes 
 38  from generic_fns.result_files import add_result_file 
 39  from relax_errors import RelaxError, RelaxNoSequenceError 
 40  from relax_io import get_file_path, open_read_file, open_write_file, test_binary 
 41  from specific_fns.setup import get_specific_fn 
 42  from status import Status; status = Status() 
 43   
 44   
45 -class Molmol:
46 """The Molmol execution object.""" 47
48 - def __init__(self):
49 """Set up the Molmol execution object.""" 50 51 # Variable for storing the Molmol command history. 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 # Reopen the pipe if needed. 75 if not self.running(): 76 self.open_gui() 77 78 # Write the command to the pipe. 79 self.molmol.write(command + '\n') 80 81 # Place the command in the command history. 82 if store_command: 83 self.command_history = self.command_history + command + "\n"
84 85
86 - def open_gui(self):
87 """Open a Molmol pipe.""" 88 89 # Test that the Molmol binary exists. 90 test_binary('molmol') 91 92 # Python 2.3 and earlier. 93 if Popen == None: 94 raise RelaxError("The subprocess module is not available in this version of Python.") 95 96 # Open Molmol as a pipe. 97 self.molmol = Popen(['molmol', '-f', '-'], stdin=PIPE).stdin 98 99 # Execute the command history. 100 if len(self.command_history) > 0: 101 self.exec_cmd(self.command_history, store_command=0) 102 return 103 104 # Wait a little while for Molmol to initialise. 105 sleep(2) 106 107 # Test if the PDB file has been loaded. 108 if hasattr(cdp, 'structure'): 109 self.open_pdb() 110 111 # Run InitAll to remove everything from Molmol. 112 else: 113 self.molmol.write("InitAll yes\n")
114 115
116 - def open_pdb(self):
117 """Open the PDB file in Molmol.""" 118 119 # Test if Molmol is running. 120 if not self.running(): 121 return 122 123 # Run InitAll to remove everything from molmol. 124 self.exec_cmd("InitAll yes") 125 126 # Open the PDB files. 127 open_files = [] 128 for model in cdp.structure.structural_data: 129 for mol in model.mol: 130 # The file path. 131 file = mol.file_name 132 if mol.file_path: 133 file = mol.file_path + sep + file 134 135 # Already loaded. 136 if file in open_files: 137 continue 138 139 # Open the file in Molmol. 140 self.exec_cmd("ReadPdb " + file) 141 142 # Add to the open file list. 143 open_files.append(file)
144 145
146 - def running(self):
147 """Test if Molmol is running. 148 149 @return: Whether the Molmol pipe is open or not. 150 @rtype: bool 151 """ 152 153 # Pipe exists. 154 if not hasattr(self, 'molmol'): 155 return False 156 157 # Test if the pipe has been broken. 158 try: 159 self.molmol.write('\n') 160 except IOError: 161 import sys 162 sys.stderr.write("Broken pipe") 163 return False 164 165 # Molmol is running 166 return True
167 168 169 170 # Initialise the Molmol executable object. 171 molmol_obj = Molmol() 172 """Molmol data container instance.""" 173 174 175 176
177 -def command(command):
178 """Function for sending Molmol commands to the program pipe. 179 180 @param command: The command to send into the program. 181 @type command: str 182 """ 183 184 # Pass the command to Molmol. 185 molmol_obj.exec_cmd(command)
186 187
188 -def create_macro(data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None):
189 """Create an array of Molmol commands. 190 191 @keyword data_type: The data type to map to the structure. 192 @type data_type: str 193 @keyword style: The style of the macro. 194 @type style: str 195 @keyword colour_start: The starting colour of the linear gradient. 196 @type colour_start: str or RBG colour array (len 3 with vals from 0 to 1) 197 @keyword colour_end: The ending colour of the linear gradient. 198 @type colour_end: str or RBG colour array (len 3 with vals from 0 to 1) 199 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'. 200 @type colour_list: str or None 201 @return: The list of Molmol commands. 202 @rtype: list of str 203 """ 204 205 # Specific Molmol macro creation function. 206 macro = get_specific_fn('molmol_macro', cdp.pipe_type) 207 208 # Get the macro. 209 commands = macro(data_type, style, colour_start, colour_end, colour_list) 210 211 # Return the macro commands. 212 return commands
213 214
215 -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):
216 """Execute a Molmol macro. 217 218 @keyword data_type: The data type to map to the structure. 219 @type data_type: str 220 @keyword style: The style of the macro. 221 @type style: str 222 @keyword colour_start_name: The name of the starting colour of the linear gradient. 223 @type colour_start_name: str 224 @keyword colour_start_rgb: The RGB array starting colour of the linear gradient. 225 @type colour_start_rgb: RBG colour array (len 3 with vals from 0 to 1) 226 @keyword colour_end_name: The name of the ending colour of the linear gradient. 227 @type colour_end_name: str 228 @keyword colour_end_rgb: The RGB array ending colour of the linear gradient. 229 @type colour_end_rgb: RBG colour array (len 3 with vals from 0 to 1) 230 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'. 231 @type colour_list: str or None 232 """ 233 234 # Test if the current data pipe exists. 235 pipes.test() 236 237 # Test if sequence data exists. 238 if not exists_mol_res_spin_data(): 239 raise RelaxNoSequenceError 240 241 # Check the arguments. 242 if colour_start_name != None and colour_start_rgb != None: 243 raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.") 244 if colour_end_name != None and colour_end_rgb != None: 245 raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.") 246 247 # Merge the colour args. 248 if colour_start_name != None: 249 colour_start = colour_start_name 250 else: 251 colour_start = colour_start_rgb 252 if colour_end_name != None: 253 colour_end = colour_end_name 254 else: 255 colour_end = colour_end_rgb 256 257 # Create the macro. 258 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 259 260 # Loop over the commands and execute them. 261 for command in commands: 262 molmol_obj.exec_cmd(command)
263 264
265 -def macro_run(file=None, dir=None):
266 """Execute the Molmol macro from the given text file. 267 268 @keyword file: The name of the macro file to execute. 269 @type file: str 270 @keyword dir: The name of the directory where the macro file is located. 271 @type dir: str 272 """ 273 274 # Open the file for reading. 275 file_path = get_file_path(file, dir) 276 file = open_read_file(file, dir) 277 278 # Loop over the commands and apply them. 279 for command in file.readlines(): 280 molmol_obj.exec_cmd(command)
281 282
283 -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):
284 """Create a Molmol macro. 285 286 @keyword data_type: The data type to map to the structure. 287 @type data_type: str 288 @keyword style: The style of the macro. 289 @type style: str 290 @keyword colour_start_name: The name of the starting colour of the linear gradient. 291 @type colour_start_name: str 292 @keyword colour_start_rgb: The RGB array starting colour of the linear gradient. 293 @type colour_start_rgb: RBG colour array (len 3 with vals from 0 to 1) 294 @keyword colour_end_name: The name of the ending colour of the linear gradient. 295 @type colour_end_name: str 296 @keyword colour_end_rgb: The RGB array ending colour of the linear gradient. 297 @type colour_end_rgb: RBG colour array (len 3 with vals from 0 to 1) 298 @keyword colour_list: The colour list to search for the colour names. Can be either 'molmol' or 'x11'. 299 @type colour_list: str or None 300 @keyword file: The name of the macro file to create. 301 @type file: str 302 @keyword dir: The name of the directory to place the macro file into. 303 @type dir: str 304 @keyword force: Flag which if set to True will cause any pre-existing file to be overwritten. 305 @type force: bool 306 """ 307 308 # Test if the current data pipe exists. 309 pipes.test() 310 311 # Test if sequence data exists. 312 if not exists_mol_res_spin_data(): 313 raise RelaxNoSequenceError 314 315 # Check the arguments. 316 if colour_start_name != None and colour_start_rgb != None: 317 raise RelaxError("The starting colour name and RGB colour array cannot both be supplied.") 318 if colour_end_name != None and colour_end_rgb != None: 319 raise RelaxError("The ending colour name and RGB colour array cannot both be supplied.") 320 321 # Merge the colour args. 322 if colour_start_name != None: 323 colour_start = colour_start_name 324 else: 325 colour_start = colour_start_rgb 326 if colour_end_name != None: 327 colour_end = colour_end_name 328 else: 329 colour_end = colour_end_rgb 330 331 # Create the macro. 332 commands = create_macro(data_type=data_type, style=style, colour_start=colour_start, colour_end=colour_end, colour_list=colour_list) 333 334 # File name. 335 if file == None: 336 file = data_type + '.mac' 337 338 # Open the file for writing. 339 file_path = get_file_path(file, dir) 340 file = open_write_file(file, dir, force) 341 342 # Loop over the commands and write them. 343 for command in commands: 344 file.write(command + "\n") 345 346 # Close the file. 347 file.close() 348 349 # Add the file to the results file list. 350 add_result_file(type='molmol', label='Molmol', file=file_path)
351 352
353 -def ribbon():
354 """Apply the Molmol ribbon style.""" 355 356 # Calculate the protons. 357 molmol_obj.exec_cmd("CalcAtom 'H'") 358 molmol_obj.exec_cmd("CalcAtom 'HN'") 359 360 # Calculate the secondary structure. 361 molmol_obj.exec_cmd("CalcSecondary") 362 363 # Execute the ribbon macro. 364 molmol_obj.exec_cmd("XMacStand ribbon.mac")
365 366
367 -def tensor_pdb(file=None):
368 """Display the diffusion tensor geometric structure. 369 370 @keyword file: The name of the PDB file containing the tensor geometric object. 371 @type file: str 372 """ 373 374 # Test if the current data pipe exists. 375 pipes.test() 376 377 # To overlay the structure with the diffusion tensor, select all and reorient to the PDB frame. 378 molmol_obj.exec_cmd("SelectAtom ''") 379 molmol_obj.exec_cmd("SelectBond ''") 380 molmol_obj.exec_cmd("SelectAngle ''") 381 molmol_obj.exec_cmd("SelectDist ''") 382 molmol_obj.exec_cmd("SelectPrim ''") 383 molmol_obj.exec_cmd("RotateInit") 384 molmol_obj.exec_cmd("MoveInit") 385 386 # Read in the tensor PDB file and force Molmol to recognise the CONECT records (not that it will show the bonds)! 387 molmol_obj.exec_cmd("ReadPdb " + file) 388 file_parts = file.split('.') 389 molmol_obj.exec_cmd("SelectMol '@" + file_parts[0] + "'") 390 molmol_obj.exec_cmd("CalcBond 1 1 1") 391 392 # Apply the 'ball/stick' style to the tensor. 393 molmol_obj.exec_cmd("SelectAtom '0'") 394 molmol_obj.exec_cmd("SelectBond '0'") 395 molmol_obj.exec_cmd("SelectAtom ':TNS'") 396 molmol_obj.exec_cmd("SelectBond ':TNS'") 397 molmol_obj.exec_cmd("XMacStand ball_stick.mac") 398 399 # Touch up. 400 molmol_obj.exec_cmd("RadiusAtom 1") 401 molmol_obj.exec_cmd("SelectAtom ':TNS@C*'") 402 molmol_obj.exec_cmd("RadiusAtom 1.5")
403 404
405 -def view():
406 """Start Molmol.""" 407 408 # Open a Molmol pipe. 409 if molmol_obj.running(): 410 raise RelaxError("The Molmol pipe already exists.") 411 else: 412 molmol_obj.open_gui()
413