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

Source Code for Module generic_fns.sequence

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003, 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   
24 -class Sequence:
25 - def __init__(self, relax):
26 """Class containing functions specific to amino-acid sequence.""" 27 28 self.relax = relax
29 30
31 - def add(self, run=None, res_num=None, res_name=None, select=1):
32 """Function for adding a residue onto the sequence.""" 33 34 # Test if the run exists. 35 if not run in self.relax.data.run_names: 36 raise RelaxNoRunError, run 37 38 # Initialise the sequence data if no sequence currently exists. 39 if not self.relax.data.res.has_key(run): 40 # Add the run to 'self.relax.data.res'. 41 self.relax.data.res.add_list(run) 42 43 # Test if the residue number already exists. 44 for i in xrange(len(self.relax.data.res[run])): 45 if self.relax.data.res[run][i].num == res_num: 46 raise RelaxError, "The residue number '" + `res_num` + "' already exists in the sequence." 47 48 # Residue index. 49 index = len(self.relax.data.res[run]) 50 51 # Append a data container. 52 self.relax.data.res[run].add_item() 53 54 # Insert the data. 55 self.relax.data.res[run][index].num = res_num 56 self.relax.data.res[run][index].name = res_name 57 self.relax.data.res[run][index].select = select
58 59
60 - def copy(self, run1=None, run2=None):
61 """Function for copying the sequence from run1 to run2.""" 62 63 # Test if run1 exists. 64 if not run1 in self.relax.data.run_names: 65 raise RelaxNoRunError, run1 66 67 # Test if run2 exists. 68 if not run2 in self.relax.data.run_names: 69 raise RelaxNoRunError, run2 70 71 # Test if the sequence data for run1 is loaded. 72 if not self.relax.data.res.has_key(run1): 73 raise RelaxNoSequenceError, run1 74 75 # Test if the sequence data already exists. 76 if self.relax.data.res.has_key(run2): 77 raise RelaxSequenceError, run2 78 79 # Add run2 to 'self.relax.data.res'. 80 self.relax.data.res.add_list(run2) 81 82 # Copy the data. 83 for i in xrange(len(self.relax.data.res[run1])): 84 # Append a data container to run2. 85 self.relax.data.res[run2].add_item() 86 87 # Insert the data. 88 self.relax.data.res[run2][i].num = self.relax.data.res[run1][i].num 89 self.relax.data.res[run2][i].name = self.relax.data.res[run1][i].name 90 self.relax.data.res[run2][i].select = self.relax.data.res[run1][i].select
91 92
93 - def data_names(self):
94 """Function for returning a list of names of data structures associated with the sequence.""" 95 96 return [ 'res' ]
97 98
99 - def delete(self, run=None):
100 """Function for deleting the sequence.""" 101 102 # Test if the run exists. 103 if not run in self.relax.data.run_names: 104 raise RelaxNoRunError, run 105 106 # Test if the sequence data is loaded. 107 if not self.relax.data.res.has_key(run): 108 raise RelaxNoSequenceError, run 109 110 # Delete the data. 111 del(self.relax.data.res[run]) 112 113 # Clean up the runs. 114 self.relax.generic.runs.eliminate_unused_runs()
115 116
117 - def display(self, run=None):
118 """Function for displaying the sequence.""" 119 120 # Test if the run exists. 121 if not run in self.relax.data.run_names: 122 raise RelaxNoRunError, run 123 124 # Test if the sequence data is loaded. 125 if not self.relax.data.res.has_key(run): 126 raise RelaxNoSequenceError, run 127 128 # Print a header. 129 print "%-8s%-8s%-10s" % ("Number", "Name", "Selected") 130 131 # Print the sequence. 132 for i in xrange(len(self.relax.data.res[run])): 133 print "%-8i%-8s%-10i" % (self.relax.data.res[run][i].num, self.relax.data.res[run][i].name, self.relax.data.res[run][i].select)
134 135
136 - def load_PDB_sequence(self, run=None):
137 """Function for loading the sequence out of a PDB file. 138 139 This needs to be modified to handle multiple peptide chains. 140 """ 141 142 # Print out. 143 print "\nLoading the sequence from the PDB file.\n" 144 145 # Reassign the sequence of the first structure. 146 if self.relax.data.pdb[run].structures[0].peptide_chains: 147 res = self.relax.data.pdb[run].structures[0].peptide_chains[0].residues 148 elif self.relax.data.pdb[run].structures[0].nucleotide_chains: 149 res = self.relax.data.pdb[run].structures[0].nucleotide_chains[0].residues 150 else: 151 raise RelaxNoPdbChainError 152 153 # Add the run to 'self.relax.data.res'. 154 self.relax.data.res.add_list(run) 155 156 # Loop over the sequence. 157 for i in xrange(len(res)): 158 # Append a data container. 159 self.relax.data.res[run].add_item() 160 161 # Insert the data. 162 self.relax.data.res[run][i].num = res[i].number 163 self.relax.data.res[run][i].name = res[i].name 164 self.relax.data.res[run][i].select = 1
165 166
167 - def read(self, run=None, file=None, dir=None, num_col=0, name_col=1, sep=None):
168 """Function for reading sequence data.""" 169 170 # Test if the run exists. 171 if not run in self.relax.data.run_names: 172 raise RelaxNoRunError, run 173 174 # Test if the sequence data has already been read. 175 if self.relax.data.res.has_key(run): 176 raise RelaxSequenceError, run 177 178 # Extract the data from the file. 179 file_data = self.relax.IO.extract_data(file, dir) 180 181 # Count the number of header lines. 182 header_lines = 0 183 for i in xrange(len(file_data)): 184 try: 185 int(file_data[i][num_col]) 186 except: 187 header_lines = header_lines + 1 188 else: 189 break 190 191 # Remove the header. 192 file_data = file_data[header_lines:] 193 194 # Strip data. 195 file_data = self.relax.IO.strip(file_data) 196 197 # Do nothing if the file does not exist. 198 if not file_data: 199 raise RelaxFileEmptyError 200 201 # Test if the sequence data is valid. 202 for i in xrange(len(file_data)): 203 try: 204 int(file_data[i][num_col]) 205 except ValueError: 206 raise RelaxError, "Sequence data is invalid." 207 208 # Add the run to 'self.relax.data.res'. 209 self.relax.data.res.add_list(run) 210 211 # Fill the array 'self.relax.data.res[run]' with data containers and place sequence data into the array. 212 for i in xrange(len(file_data)): 213 # Append a data container. 214 self.relax.data.res[run].add_item() 215 216 # Insert the data. 217 self.relax.data.res[run][i].num = int(file_data[i][num_col]) 218 self.relax.data.res[run][i].name = file_data[i][name_col] 219 self.relax.data.res[run][i].select = 1
220 221
222 - def sort(self, run=None):
223 """Function for sorting the sequence by residue number.""" 224 225 # Test if the run exists. 226 if not run in self.relax.data.run_names: 227 raise RelaxNoRunError, run 228 229 # Test if the sequence data is loaded. 230 if not self.relax.data.res.has_key(run): 231 raise RelaxNoSequenceError, run 232 233 # Sort the sequence. 234 self.relax.data.res[run].sort(self.sort_cmpfunc)
235 236
237 - def sort_cmpfunc(self, x, y):
238 """Sequence comparison function given to the ListType function 'sort'.""" 239 240 if x.num > y.num: 241 return 1 242 elif x.num < y.num: 243 return -1 244 elif x.num == y.num: 245 return 0
246 247
248 - def write(self, run=None, file=None, dir=None, force=0):
249 """Function for writing sequence data.""" 250 251 # Test if the run exists. 252 if not run in self.relax.data.run_names: 253 raise RelaxNoRunError, run 254 255 # Test if the sequence data is loaded. 256 if not self.relax.data.res.has_key(run): 257 raise RelaxNoSequenceError, run 258 259 # Open the file for writing. 260 seq_file = self.relax.IO.open_write_file(file, dir, force) 261 262 # Loop over the sequence. 263 for i in xrange(len(self.relax.data.res[run])): 264 # Residue number. 265 seq_file.write("%-5i" % self.relax.data.res[run][i].num) 266 267 # Residue name. 268 seq_file.write("%-6s" % self.relax.data.res[run][i].name) 269 270 # New line. 271 seq_file.write("\n") 272 273 # Close the results file. 274 seq_file.close()
275