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 res = self.relax.data.pdb[run].structures[0].peptide_chains[0].residues 147 148 # Add the run to 'self.relax.data.res'. 149 self.relax.data.res.add_list(run) 150 151 # Loop over the sequence. 152 for i in xrange(len(res)): 153 # Append a data container. 154 self.relax.data.res[run].add_item() 155 156 # Insert the data. 157 self.relax.data.res[run][i].num = res[i].number 158 self.relax.data.res[run][i].name = res[i].name 159 self.relax.data.res[run][i].select = 1
160 161
162 - def read(self, run=None, file=None, dir=None, num_col=0, name_col=1, sep=None):
163 """Function for reading sequence data.""" 164 165 # Test if the run exists. 166 if not run in self.relax.data.run_names: 167 raise RelaxNoRunError, run 168 169 # Test if the sequence data has already been read. 170 if self.relax.data.res.has_key(run): 171 raise RelaxSequenceError, run 172 173 # Extract the data from the file. 174 file_data = self.relax.IO.extract_data(file, dir) 175 176 # Count the number of header lines. 177 header_lines = 0 178 for i in xrange(len(file_data)): 179 try: 180 int(file_data[i][num_col]) 181 except: 182 header_lines = header_lines + 1 183 else: 184 break 185 186 # Remove the header. 187 file_data = file_data[header_lines:] 188 189 # Strip data. 190 file_data = self.relax.IO.strip(file_data) 191 192 # Do nothing if the file does not exist. 193 if not file_data: 194 raise RelaxFileEmptyError 195 196 # Test if the sequence data is valid. 197 for i in xrange(len(file_data)): 198 try: 199 int(file_data[i][num_col]) 200 except ValueError: 201 raise RelaxError, "Sequence data is invalid." 202 203 # Add the run to 'self.relax.data.res'. 204 self.relax.data.res.add_list(run) 205 206 # Fill the array 'self.relax.data.res[run]' with data containers and place sequence data into the array. 207 for i in xrange(len(file_data)): 208 # Append a data container. 209 self.relax.data.res[run].add_item() 210 211 # Insert the data. 212 self.relax.data.res[run][i].num = int(file_data[i][num_col]) 213 self.relax.data.res[run][i].name = file_data[i][name_col] 214 self.relax.data.res[run][i].select = 1
215 216
217 - def sort(self, run=None):
218 """Function for sorting the sequence by residue number.""" 219 220 # Test if the run exists. 221 if not run in self.relax.data.run_names: 222 raise RelaxNoRunError, run 223 224 # Test if the sequence data is loaded. 225 if not self.relax.data.res.has_key(run): 226 raise RelaxNoSequenceError, run 227 228 # Sort the sequence. 229 self.relax.data.res[run].sort(self.sort_cmpfunc)
230 231
232 - def sort_cmpfunc(self, x, y):
233 """Sequence comparison function given to the ListType function 'sort'.""" 234 235 if x.num > y.num: 236 return 1 237 elif x.num < y.num: 238 return -1 239 elif x.num == y.num: 240 return 0
241 242
243 - def write(self, run=None, file=None, dir=None, force=0):
244 """Function for writing sequence data.""" 245 246 # Test if the run exists. 247 if not run in self.relax.data.run_names: 248 raise RelaxNoRunError, run 249 250 # Test if the sequence data is loaded. 251 if not self.relax.data.res.has_key(run): 252 raise RelaxNoSequenceError, run 253 254 # Open the file for writing. 255 seq_file = self.relax.IO.open_write_file(file, dir, force) 256 257 # Loop over the sequence. 258 for i in xrange(len(self.relax.data.res[run])): 259 # Residue number. 260 seq_file.write("%-5i" % self.relax.data.res[run][i].num) 261 262 # Residue name. 263 seq_file.write("%-6s" % self.relax.data.res[run][i].name) 264 265 # New line. 266 seq_file.write("\n") 267 268 # Close the results file. 269 seq_file.close()
270