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

Source Code for Module generic_fns.molmol

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004, 2006 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  from os import popen 
 24   
 25   
26 -class Molmol:
27 - def __init__(self, relax):
28 """Class containing the functions for viewing molecules.""" 29 30 self.relax = relax 31 32 # Initialise the command history (for reopening Molmol pipes). 33 self.clear_history()
34 35
36 - def clear_history(self):
37 """Function for clearing the Molmol command history.""" 38 39 self.command_history = ""
40 41
42 - def create_macro(self):
43 """Function for creating an array of Molmol commands.""" 44 45 # Function type. 46 self.function_type = self.relax.data.run_types[self.relax.data.run_names.index(self.run)] 47 48 # Specific Molmol macro creation function. 49 molmol_macro = self.relax.specific_setup.setup('molmol_macro', self.function_type) 50 51 # Get the macro. 52 self.commands = molmol_macro(self.run, self.data_type, self.style, self.colour_start, self.colour_end, self.colour_list)
53 54
55 - def macro_exec(self, run=None, data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None):
56 """Function for executing a Molmol macro.""" 57 58 # Arguments. 59 self.run = run 60 self.data_type = data_type 61 self.style = style 62 self.colour_start = colour_start 63 self.colour_end = colour_end 64 self.colour_list = colour_list 65 66 # Test if the run exists. 67 if not self.run in self.relax.data.run_names: 68 raise RelaxNoRunError, self.run 69 70 # Test if the sequence data is loaded. 71 if not self.relax.data.res.has_key(self.run): 72 raise RelaxNoSequenceError, self.run 73 74 # Create the macro. 75 self.create_macro() 76 77 # Loop over the commands and execute them. 78 for command in self.commands: 79 self.pipe_write(command)
80 81
82 - def open_pdb(self, run=None):
83 """Function for opening the PDB file in Molmol.""" 84 85 # Argument. 86 if run: 87 self.run = run 88 89 # Test if the pipe is open. 90 if not self.pipe_open_test(): 91 return 92 93 # Run InitAll to remove everything from molmol. 94 self.pipe_write("InitAll yes") 95 96 # Open the PDB. 97 self.pipe_write("ReadPdb " + self.relax.data.pdb[self.run].file_name)
98 99
100 - def pipe_open(self):
101 """Function for opening a Molmol pipe.""" 102 103 # Test that the Molmol binary exists. 104 self.relax.IO.test_binary('molmol') 105 106 # Open the Molmol pipe. 107 self.relax.data.molmol = popen("molmol -f -", 'w', 0) 108 109 # Execute the command history. 110 if len(self.command_history) > 0: 111 self.pipe_write(self.command_history, store_command=0) 112 return 113 114 # Test if the PDB file has been loaded. 115 if hasattr(self.relax.data, 'pdb') and self.relax.data.pdb.has_key(self.run): 116 self.open_pdb() 117 118 # Run InitAll to remove everything from molmol. 119 else: 120 self.pipe_write("InitAll yes")
121 122
123 - def pipe_open_test(self):
124 """Function for testing if the Molmol pipe is open.""" 125 126 # Test if a pipe has been opened. 127 if not hasattr(self.relax.data, 'molmol'): 128 return 0 129 130 # Test if the pipe has been broken. 131 try: 132 self.relax.data.molmol.write('\n') 133 except IOError: 134 return 0 135 136 # The pipe is open. 137 return 1
138 139
140 - def pipe_write(self, command=None, store_command=1):
141 """Function for writing to the Molmol pipe. 142 143 This function is also used to execute a user supplied Molmol command. 144 """ 145 146 # Reopen the pipe if needed. 147 if not self.pipe_open_test(): 148 self.pipe_open() 149 150 # Write the command to the pipe. 151 self.relax.data.molmol.write(command + '\n') 152 153 # Place the command in the command history. 154 if store_command: 155 self.command_history = self.command_history + command + "\n"
156 157
158 - def view(self, run=None):
159 """Function for running Molmol.""" 160 161 # Arguments. 162 self.run = run 163 164 # Open a Molmol pipe. 165 if self.pipe_open_test(): 166 raise RelaxError, "The Molmol pipe already exists." 167 else: 168 self.pipe_open()
169 170
171 - def write(self, run=None, data_type=None, style="classic", colour_start=None, colour_end=None, colour_list=None, file=None, dir=None, force=0):
172 """Function for creating a Molmol macro.""" 173 174 # Arguments. 175 self.run = run 176 self.data_type = data_type 177 self.style = style 178 self.colour_start = colour_start 179 self.colour_end = colour_end 180 self.colour_list = colour_list 181 182 # Test if the run exists. 183 if not self.run in self.relax.data.run_names: 184 raise RelaxNoRunError, self.run 185 186 # Test if the sequence data is loaded. 187 if not self.relax.data.res.has_key(self.run): 188 raise RelaxNoSequenceError, self.run 189 190 # Create the macro. 191 self.create_macro() 192 193 # File name. 194 if file == None: 195 file = data_type + '.mac' 196 197 # Open the file for writing. 198 file = self.relax.IO.open_write_file(file, dir, force) 199 200 # Loop over the commands and write them. 201 for command in self.commands: 202 file.write(command + "\n") 203 204 # Close the file. 205 file.close()
206