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

Source Code for Module generic_fns.molmol

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2004 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 open_pipe(self):
43 """Function for opening a Molmol pipe.""" 44 45 # Open the Molmol pipe. 46 self.relax.data.molmol = popen("molmol -f -", 'w', 0) 47 48 # Execute the command history. 49 if len(self.command_history) > 0: 50 self.write(self.command_history, store_command=0) 51 return 52 53 # Test if the PDB file has been loaded. 54 if hasattr(self.relax.data, 'pdb'): 55 self.open_pdb() 56 57 # Run InitAll to remove everything from molmol. 58 else: 59 self.write("InitAll yes")
60 61
62 - def open_pdb(self, run=None):
63 """Function for opening the PDB file in Molmol.""" 64 65 # Argument. 66 if run: 67 self.run = run 68 69 # Test if the pipe is open. 70 if not self.pipe_open(): 71 return 72 73 # Run InitAll to remove everything from molmol. 74 self.write("InitAll yes") 75 76 # Open the PDB. 77 self.write("ReadPdb " + self.relax.data.pdb[self.run].file_name)
78 79
80 - def pipe_open(self):
81 """Function for testing if the Molmol pipe is open.""" 82 83 # Test if a pipe has been opened. 84 if not hasattr(self.relax.data, 'molmol'): 85 return 0 86 87 # Test if the pipe has been broken. 88 try: 89 self.relax.data.molmol.write('\n') 90 except IOError: 91 return 0 92 93 # The pipe is open. 94 return 1
95 96
97 - def view(self, run=None):
98 """Function for running Molmol.""" 99 100 # Arguments. 101 self.run = run 102 103 # Open a Molmol pipe. 104 if self.pipe_open(): 105 raise RelaxError, "The Molmol pipe already exists." 106 else: 107 self.open_pipe()
108 109
110 - def write(self, command=None, store_command=1):
111 """Function for writing to the Molmol pipe. 112 113 This function is also used to execute a user supplied Molmol command. 114 """ 115 116 # Reopen the pipe if needed. 117 if not self.pipe_open(): 118 self.open_pipe() 119 120 # Write the command to the pipe. 121 self.relax.data.molmol.write(command + '\n') 122 123 # Place the command in the command history. 124 if store_command: 125 self.command_history = self.command_history + command + "\n"
126