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

Source Code for Module generic_fns.selection

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003, 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 F_OK, access 
 24  from re import compile, match 
 25   
 26   
27 -class Selection:
28 - def __init__(self, relax):
29 """Base class containing functions for the manipulation of residue selection.""" 30 31 self.relax = relax
32 33
34 - def reverse(self, run=None):
35 """Function for the reversal of residue selection.""" 36 37 # Create the list of runs. 38 self.runs = self.relax.generic.runs.list_of_runs(run) 39 40 # Loop over the runs. 41 for self.run in self.runs: 42 # Test if the run exists. 43 if not self.run in self.relax.data.run_names: 44 raise RelaxNoRunError, self.run 45 46 # Test if sequence data is loaded. 47 if not len(self.relax.data.res[self.run]): 48 raise RelaxNoSequenceError, self.run 49 50 # Loop over the sequence and reverse the selection flag. 51 for i in xrange(len(self.relax.data.res[self.run])): 52 # Remap the data structure 'self.relax.data.res[self.run][i]'. 53 data = self.relax.data.res[self.run][i] 54 55 # Reverse the selection. 56 if data.select: 57 data.select = 0 58 else: 59 data.select = 1
60 61
62 - def sel_all(self, run=None):
63 """Function for selecting all residues.""" 64 65 # Create the list of runs. 66 self.runs = self.relax.generic.runs.list_of_runs(run) 67 68 # Loop over the runs. 69 for self.run in self.runs: 70 # Test if the run exists. 71 if not self.run in self.relax.data.run_names: 72 raise RelaxNoRunError, self.run 73 74 # Test if sequence data is loaded. 75 if not len(self.relax.data.res[self.run]): 76 raise RelaxNoSequenceError, self.run 77 78 # Loop over the sequence and set the selection flag to 1. 79 for i in xrange(len(self.relax.data.res[self.run])): 80 self.relax.data.res[self.run][i].select = 1
81 82
83 - def sel_read(self, run=None, file=None, dir=None, boolean='OR', change_all=0, column=None):
84 """Select the residues contained in the given file. 85 86 @param run: The run name. 87 @type run: str 88 @param file: The name of the file. 89 @type file: str 90 @param dir: The directory containing the file. 91 @type dir: str 92 @param boolean: The boolean operator used to select the spin systems with. It can be 93 one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. 94 @type boolean: str 95 @param change_all: A flag which if set will set the selection to solely those of the file. 96 @type change_all: int 97 @param column: The whitespace separated column in which the residue numbers are 98 located. 99 @type column: int 100 """ 101 102 # Extract the data from the file. 103 file_data = self.relax.IO.extract_data(file, dir) 104 105 # Count the number of header lines. 106 header_lines = 0 107 for i in xrange(len(file_data)): 108 try: 109 int(file_data[i][column]) 110 except: 111 header_lines = header_lines + 1 112 else: 113 break 114 115 # Remove the header. 116 file_data = file_data[header_lines:] 117 118 # Strip the data. 119 file_data = self.relax.IO.strip(file_data) 120 121 # Create the list of residues to select. 122 select = [] 123 for i in xrange(len(file_data)): 124 try: 125 select.append(int(file_data[i][column])) 126 except: 127 raise RelaxError, "Improperly formatted file." 128 129 # Create the list of runs. 130 self.runs = self.relax.generic.runs.list_of_runs(run) 131 132 # Loop over the runs. 133 for self.run in self.runs: 134 # Test if the run exists. 135 if not self.run in self.relax.data.run_names: 136 raise RelaxNoRunError, self.run 137 138 # Test if sequence data is loaded. 139 if not len(self.relax.data.res[self.run]): 140 raise RelaxNoSequenceError, self.run 141 142 # Loop over the sequence. 143 for i in xrange(len(self.relax.data.res[self.run])): 144 # Remap the data structure 'self.relax.data.res[self.run][i]'. 145 data = self.relax.data.res[self.run][i] 146 147 # The spin system is in the new selection list. 148 if data.num in select: 149 new_select = 1 150 else: 151 new_select = 0 152 153 # Select just the residues in the file. 154 if change_all: 155 data.select = new_select 156 157 # Boolean selections. 158 if boolean == 'OR': 159 data.select = data.select or new_select 160 elif boolean == 'NOR': 161 data.select = not (data.select or new_select) 162 elif boolean == 'AND': 163 data.select = data.select and new_select 164 elif boolean == 'NAND': 165 data.select = not (data.select and new_select) 166 elif boolean == 'XOR': 167 data.select = not (data.select and new_select) and (data.select or new_select) 168 elif boolean == 'XNOR': 169 data.select = (data.select and new_select) or not (data.select or new_select) 170 else: 171 raise RelaxError, "Unknown boolean operator " + `boolean`
172 173 174
175 - def sel_res(self, run=None, num=None, name=None, boolean='OR', change_all=0):
176 """Select specific residues. 177 178 @param run: The run name. 179 @type run: str 180 @param num: The residue number. 181 @type num: int or regular expression str 182 @param name: The residue name. 183 @type name: regular expression str 184 @param boolean: The boolean operator used to select the spin systems with. It can be 185 one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. 186 @type boolean: str 187 @param change_all: A flag which if set will set the selection to solely those residues 188 specified. 189 @type change_all: int 190 """ 191 192 # Test if the residue number is a valid regular expression. 193 if type(num) == str: 194 try: 195 compile(num) 196 except: 197 raise RelaxRegExpError, ('residue number', num) 198 199 # Test if the residue name is a valid regular expression. 200 if name: 201 try: 202 compile(name) 203 except: 204 raise RelaxRegExpError, ('residue name', name) 205 206 # Create the list of runs. 207 self.runs = self.relax.generic.runs.list_of_runs(run) 208 209 # Loop over the runs. 210 no_match = 1 211 for self.run in self.runs: 212 # Test if the run exists. 213 if not self.run in self.relax.data.run_names: 214 raise RelaxNoRunError, self.run 215 216 # Test if sequence data is loaded. 217 if not len(self.relax.data.res[self.run]): 218 raise RelaxNoSequenceError, self.run 219 220 # Loop over the sequence. 221 for i in xrange(len(self.relax.data.res[self.run])): 222 # Remap the data structure 'self.relax.data.res[self.run][i]'. 223 data = self.relax.data.res[self.run][i] 224 225 # Initialise the new selection flag. 226 new_select = 0 227 228 # Set the new selection flag if the residue matches 'num'. 229 if type(num) == int: 230 if data.num == num: 231 new_select = 1 232 elif type(num) == str: 233 if match(num, `data.num`): 234 new_select = 1 235 236 # Set the new selection flag if the residue matches 'name'. 237 if name != None: 238 if match(name, data.name): 239 new_select = 1 240 241 # Select just the specified residues. 242 if change_all: 243 data.select = new_select 244 245 # Boolean selections. 246 if boolean == 'OR': 247 data.select = data.select or new_select 248 elif boolean == 'NOR': 249 data.select = not (data.select or new_select) 250 elif boolean == 'AND': 251 data.select = data.select and new_select 252 elif boolean == 'NAND': 253 data.select = not (data.select and new_select) 254 elif boolean == 'XOR': 255 data.select = not (data.select and new_select) and (data.select or new_select) 256 elif boolean == 'XNOR': 257 data.select = (data.select and new_select) or not (data.select or new_select) 258 else: 259 raise RelaxError, "Unknown boolean operator " + `boolean` 260 261 # Match flag. 262 if new_select: 263 no_match = 0 264 265 # No residue matched. 266 if no_match: 267 print "No residues match."
268 269
270 - def unsel_all(self, run=None):
271 """Function for unselecting all residues.""" 272 273 # Create the list of runs. 274 self.runs = self.relax.generic.runs.list_of_runs(run) 275 276 # Loop over the runs. 277 for self.run in self.runs: 278 # Test if the run exists. 279 if not self.run in self.relax.data.run_names: 280 raise RelaxNoRunError, self.run 281 282 # Test if sequence data is loaded. 283 if not len(self.relax.data.res[self.run]): 284 raise RelaxNoSequenceError, self.run 285 286 # Loop over the sequence and set the selection flag to 0. 287 for i in xrange(len(self.relax.data.res[self.run])): 288 self.relax.data.res[self.run][i].select = 0
289 290
291 - def unsel_read(self, run=None, file=None, dir=None, change_all=None, column=None):
292 """Function for unselecting the residues contained in a file.""" 293 294 # Extract the data from the file. 295 file_data = self.relax.IO.extract_data(file, dir) 296 297 # Count the number of header lines. 298 header_lines = 0 299 for i in xrange(len(file_data)): 300 try: 301 int(file_data[i][column]) 302 except: 303 header_lines = header_lines + 1 304 else: 305 break 306 307 # Remove the header. 308 file_data = file_data[header_lines:] 309 310 # Strip the data. 311 file_data = self.relax.IO.strip(file_data) 312 313 # Create the list of residues to unselect. 314 unselect = [] 315 for i in xrange(len(file_data)): 316 try: 317 unselect.append(int(file_data[i][column])) 318 except: 319 raise RelaxError, "Improperly formatted file." 320 321 # Create the list of runs. 322 self.runs = self.relax.generic.runs.list_of_runs(run) 323 324 # Loop over the runs. 325 no_match = 1 326 for self.run in self.runs: 327 # Test if the run exists. 328 if not self.run in self.relax.data.run_names: 329 raise RelaxNoRunError, self.run 330 331 # Test if sequence data is loaded. 332 if not len(self.relax.data.res[self.run]): 333 raise RelaxNoSequenceError, self.run 334 335 # Loop over the sequence. 336 for i in xrange(len(self.relax.data.res[self.run])): 337 # Remap the data structure 'self.relax.data.res[self.run][i]'. 338 data = self.relax.data.res[self.run][i] 339 340 # Select all residues. 341 if change_all: 342 data.select = 1 343 344 # Unselect the residue if it is in the list unselect. 345 if data.num in unselect: 346 data.select = 0 347 348 # Match flag. 349 no_match = 0 350 351 # No residue matched. 352 if no_match: 353 print "No residues match."
354 355
356 - def unsel_res(self, run=None, num=None, name=None, change_all=None):
357 """Function for unselecting specific residues.""" 358 359 # Test if the residue number is a valid regular expression. 360 if type(num) == str: 361 try: 362 compile(num) 363 except: 364 raise RelaxRegExpError, ('residue number', num) 365 366 # Test if the residue name is a valid regular expression. 367 if name: 368 try: 369 compile(name) 370 except: 371 raise RelaxRegExpError, ('residue name', name) 372 373 # Create the list of runs. 374 self.runs = self.relax.generic.runs.list_of_runs(run) 375 376 # Loop over the runs. 377 no_match = 1 378 for self.run in self.runs: 379 # Test if the run exists. 380 if not self.run in self.relax.data.run_names: 381 raise RelaxNoRunError, self.run 382 383 # Test if sequence data is loaded. 384 if not len(self.relax.data.res[self.run]): 385 raise RelaxNoSequenceError, self.run 386 387 # Loop over the sequence. 388 for i in xrange(len(self.relax.data.res[self.run])): 389 # Remap the data structure 'self.relax.data.res[self.run][i]'. 390 data = self.relax.data.res[self.run][i] 391 392 # Select all residues. 393 if change_all: 394 data.select = 1 395 396 # Skip the residue if there is no match to 'num'. 397 if type(num) == int: 398 if not data.num == num: 399 continue 400 if type(num) == str: 401 if not match(num, `data.num`): 402 continue 403 404 # Skip the residue if there is no match to 'name'. 405 if name != None: 406 if not match(name, data.name): 407 continue 408 409 # Unselect the residue. 410 data.select = 0 411 412 # Match flag. 413 no_match = 0 414 415 # No residue matched. 416 if no_match: 417 print "No residues match."
418