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

Source Code for Module generic_fns.pdb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2005 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 math import sqrt 
 24  from Numeric import Float64, dot, zeros 
 25  from os import F_OK, access 
 26  import Scientific.IO.PDB 
 27   
 28   
29 -class PDB:
30 - def __init__(self, relax):
31 """Class containing the PDB related functions.""" 32 33 self.relax = relax 34 35 # Print flag. 36 self.print_flag = 1
37 38
39 - def load_structures(self):
40 """Function for loading the structures from the PDB file.""" 41 42 # Use pointers (references) if the PDB data exists in another run. 43 for run in self.relax.data.run_names: 44 if self.relax.data.pdb.has_key(run) and hasattr(self.relax.data.pdb[run], 'structures') and self.relax.data.pdb[run].file_name == self.file and self.relax.data.pdb[run].model == self.model: 45 # Make a pointer to the data. 46 self.relax.data.pdb[self.run].structures = self.relax.data.pdb[run].structures 47 48 # Print out. 49 if self.print_flag: 50 print "Using the structures from the run " + `run` + "." 51 for i in xrange(len(self.relax.data.pdb[self.run].structures)): 52 print self.relax.data.pdb[self.run].structures[i] 53 54 # Exit this function. 55 return 56 57 # Initialisation. 58 self.relax.data.pdb[self.run].structures = [] 59 60 # Load the structure i from the PDB file. 61 if type(self.model) == int: 62 # Print out. 63 if self.print_flag: 64 print "Loading structure " + `self.model` + " from the PDB file." 65 66 # Load the structure into 'str'. 67 str = Scientific.IO.PDB.Structure(self.file_path, self.model) 68 69 # Test the structure. 70 if len(str) == 0: 71 raise RelaxPdbLoadError, self.file_path 72 73 # Print the PDB info. 74 if self.print_flag: 75 print str 76 77 # Place the structure in 'self.relax.data.pdb[self.run]'. 78 self.relax.data.pdb[self.run].structures.append(str) 79 80 81 # Load all structures. 82 else: 83 # Print out. 84 if self.print_flag: 85 print "Loading all structures from the PDB file." 86 87 # First model. 88 i = 1 89 90 # Loop over all the other structures. 91 while 1: 92 # Load the pdb files. 93 str = Scientific.IO.PDB.Structure(self.file_path, i) 94 95 # No model 1. 96 if len(str) == 0 and i == 1: 97 str = Scientific.IO.PDB.Structure(self.file_path) 98 if len(str) == 0: 99 raise RelaxPdbLoadError, self.file_path 100 101 # Test if the last structure has been reached. 102 if len(str) == 0: 103 del str 104 break 105 106 # Print the PDB info. 107 if self.print_flag: 108 print str 109 110 # Place the structure in 'self.relax.data.pdb[self.run]'. 111 self.relax.data.pdb[self.run].structures.append(str) 112 113 # Increment i. 114 i = i + 1
115 116
117 - def load(self, run=None, file=None, dir=None, model=None, heteronuc=None, proton=None, load_seq=1, calc_vectors=1, fail=1, print_flag=1):
118 """The pdb loading function.""" 119 120 # Arguments. 121 self.run = run 122 self.file = file 123 self.dir = dir 124 self.model = model 125 self.heteronuc = heteronuc 126 self.proton = proton 127 self.load_seq = load_seq 128 self.calc_vectors = calc_vectors 129 self.fail = fail 130 self.print_flag = print_flag 131 132 # Tests. 133 ######## 134 135 # Test if the run exists. 136 if not run in self.relax.data.run_names: 137 raise RelaxNoRunError, run 138 139 # Test if PDB data corresponding to the run already exists. 140 if self.relax.data.pdb.has_key(self.run): 141 raise RelaxPdbError, self.run 142 143 # Test if sequence data is loaded. 144 if not self.load_seq and not len(self.relax.data.res[self.run]): 145 raise RelaxNoSequenceError, self.run 146 147 # The file path. 148 self.file_path = self.relax.IO.file_path(self.file, self.dir) 149 150 # Test if the file exists. 151 if not access(self.file_path, F_OK): 152 if fail: 153 raise RelaxFileError, ('PDB', self.file_path) 154 else: 155 if self.print_flag: 156 print "The PDB file " + `self.file_path` + " cannot be found, no structures will be loaded." 157 return 158 159 160 # Data creation. 161 ################ 162 163 # Add the run to the PDB data structure. 164 self.relax.data.pdb.add_item(self.run) 165 166 # File name. 167 self.relax.data.pdb[self.run].file_name = self.file_path 168 169 # Model. 170 self.relax.data.pdb[self.run].model = model 171 172 # Nuclei. 173 self.relax.data.pdb[self.run].proton = proton 174 self.relax.data.pdb[self.run].heteronuc = heteronuc 175 176 177 # Load the structures. 178 ###################### 179 180 self.load_structures() 181 182 183 # Finish. 184 ######### 185 186 # Sequence loading. 187 if self.load_seq and not self.relax.data.res.has_key(self.run): 188 self.relax.generic.sequence.load_PDB_sequence(self.run) 189 190 # Load into Molmol (if running). 191 self.relax.generic.molmol.open_pdb(self.run) 192 193 # Calculate the unit XH vectors. 194 if calc_vectors: 195 self.vectors()
196 197
198 - def set_vector(self, run=None, res=None, xh_vect=None):
199 """Function for setting the XH unit vectors.""" 200 201 # Place the XH unit vector in 'self.relax.data.res'. 202 self.relax.data.res[run][res].xh_vect = xh_vect
203 204
205 - def vectors(self):
206 """Function for calculating the XH unit vector from the loaded structure.""" 207 208 # Print out. 209 if self.print_flag: 210 print "\nCalculating unit XH vectors.\n" 211 212 # Number of structures. 213 num_str = len(self.relax.data.pdb[self.run].structures) 214 215 # Create a temporary vector list for each residue. 216 for i in xrange(len(self.relax.data.res[self.run])): 217 self.relax.data.res[self.run][i].xh_vect = [] 218 219 # Loop over the structures. 220 for i in xrange(num_str): 221 # Print out. 222 if self.print_flag: 223 print "\nStructure " + `i + 1` + "\n" 224 225 # Reassign the first peptide chain of the first structure. 226 pdb_residues = self.relax.data.pdb[self.run].structures[i].peptide_chains[0].residues 227 228 # Loop over the sequence. 229 for j in xrange(len(self.relax.data.res[self.run])): 230 # Find the corresponding residue in the PDB. 231 pdb_res = None 232 for k in xrange(len(pdb_residues)): 233 if self.relax.data.res[self.run][j].num == pdb_residues[k].number: 234 pdb_res = pdb_residues[k] 235 break 236 if pdb_res == None: 237 raise RelaxNoResError, self.relax.data.res[self.run][j].num 238 239 # Test if the proton atom exists for residue i. 240 if not pdb_res.atoms.has_key(self.proton): 241 if self.print_flag: 242 print "The proton atom " + `self.proton` + " could not be found for residue '" + `self.relax.data.res[self.run][j].num` + " " + self.relax.data.res[self.run][j].name + "'." 243 self.relax.data.res[self.run][j].xh_vect.append(None) 244 245 # Test if the heteronucleus atom exists for residue i. 246 elif not pdb_res.atoms.has_key(self.heteronuc): 247 if self.print_flag: 248 print "The heteronucleus atom " + `self.heteronuc` + " could not be found for residue '" + `self.relax.data.res[self.run][j].num` + " " + self.relax.data.res[self.run][j].name + "'." 249 self.relax.data.res[self.run][j].xh_vect.append(None) 250 251 # Calculate the vector. 252 else: 253 # Get the proton position. 254 posH = pdb_res.atoms[self.proton].position.array 255 256 # Get the heteronucleus position. 257 posX = pdb_res.atoms[self.heteronuc].position.array 258 259 # Calculate the normalised vector. 260 vector = posH - posX 261 self.relax.data.res[self.run][j].xh_vect.append(vector / sqrt(dot(vector, vector))) 262 263 # Print out. 264 if self.print_flag: 265 if num_str > 1: 266 print "\nCalculating and averaging the unit XH vectors from all structures." 267 else: 268 print "\nCalculating the unit XH vectors from the structure." 269 270 # Average the vectors and convert xh_vect from an array of vectors to a vector. 271 for i in xrange(len(self.relax.data.res[self.run])): 272 # No vectors. 273 if self.relax.data.res[self.run][i].xh_vect[0] == None: 274 del self.relax.data.res[self.run][i].xh_vect 275 continue 276 277 # Average vectors. 278 ave_vector = zeros(3, Float64) 279 280 # Sum the vectors. 281 for j in xrange(num_str): 282 # Sum. 283 ave_vector = ave_vector + self.relax.data.res[self.run][i].xh_vect[j] 284 285 # Average the vector. 286 ave_vector = ave_vector / num_str 287 288 # Replace the temporary vector list with the normalised average vector. 289 self.relax.data.res[self.run][i].xh_vect = ave_vector / sqrt(dot(ave_vector, ave_vector))
290