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

Source Code for Module generic_fns.selection

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2011 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  # Module docstring. 
 24  """Module for selecting and deselecting spins.""" 
 25   
 26  # Python module imports 
 27  from warnings import warn 
 28   
 29  # relax module imports. 
 30  from generic_fns.mol_res_spin import exists_mol_res_spin_data, generate_spin_id_data_array, return_spin, spin_loop 
 31  from generic_fns import pipes 
 32  from relax_errors import RelaxError, RelaxNoSequenceError 
 33  from relax_io import read_spin_data 
 34  from relax_warnings import RelaxNoSpinWarning 
 35   
 36   
37 -def desel_all():
38 """Deselect all spins. 39 40 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 41 """ 42 43 # Test if the current data pipe exists. 44 pipes.test() 45 46 # Test if sequence data is loaded. 47 if not exists_mol_res_spin_data(): 48 raise RelaxNoSequenceError 49 50 # Loop over the spins and deselect them. 51 for spin in spin_loop(): 52 spin.select = False
53 54
55 -def desel_read(file=None, dir=None, file_data=None, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, sep=None, spin_id=None, boolean='AND', change_all=False):
56 """Deselect the spins contained in the given file. 57 58 @keyword file: The name of the file to open. 59 @type file: str 60 @keyword dir: The directory containing the file (defaults to the current 61 directory if None). 62 @type dir: str or None 63 @keyword file_data: An alternative opening a file, if the data already exists in the 64 correct format. The format is a list of lists where the first 65 index corresponds to the row and the second the column. 66 @type file_data: list of lists 67 @keyword spin_id_col: The column containing the spin ID strings. If supplied, the 68 mol_name_col, res_name_col, res_num_col, spin_name_col, and 69 spin_num_col arguments must be none. 70 @type spin_id_col: int or None 71 @keyword mol_name_col: The column containing the molecule name information. If 72 supplied, spin_id_col must be None. 73 @type mol_name_col: int or None 74 @keyword res_name_col: The column containing the residue name information. If 75 supplied, spin_id_col must be None. 76 @type res_name_col: int or None 77 @keyword res_num_col: The column containing the residue number information. If 78 supplied, spin_id_col must be None. 79 @type res_num_col: int or None 80 @keyword spin_name_col: The column containing the spin name information. If supplied, 81 spin_id_col must be None. 82 @type spin_name_col: int or None 83 @keyword spin_num_col: The column containing the spin number information. If supplied, 84 spin_id_col must be None. 85 @type spin_num_col: int or None 86 @keyword sep: The column separator which, if None, defaults to whitespace. 87 @type sep: str or None 88 @keyword spin_id: The spin ID string used to restrict data loading to a subset of 89 all spins. 90 @type spin_id: None or str 91 @param boolean: The boolean operator used to deselect the spin systems with. It 92 can be one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. 93 This will be ignored if the change_all flag is set. 94 @type boolean: str 95 @keyword change_all: A flag which if True will cause all spins not specified in the 96 file to be selected. Only the boolean operator 'AND' is 97 compatible with this flag set to True (all others will be 98 ignored). 99 @type change_all: bool 100 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 101 @raises RelaxError: If the boolean operator is unknown. 102 """ 103 104 # Test if the current data pipe exists. 105 pipes.test() 106 107 # Test if sequence data is loaded. 108 if not exists_mol_res_spin_data(): 109 raise RelaxNoSequenceError 110 111 # First select all spins if the change_all flag is set. 112 if change_all: 113 for spin in spin_loop(): 114 spin.select = True 115 116 # Then deselect the spins in the file. 117 for id in read_spin_data(file=file, dir=dir, file_data=file_data, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, sep=sep, spin_id=spin_id): 118 # Get the corresponding spin container. 119 spin = return_spin(id) 120 121 # No spin. 122 if spin == None: 123 warn(RelaxNoSpinWarning(id)) 124 continue 125 126 # Deselect the spin. 127 if change_all: 128 spin.select = False 129 130 # Boolean selections. 131 else: 132 if boolean == 'OR': 133 spin.select = spin.select or False 134 elif boolean == 'NOR': 135 spin.select = not (spin.select or False) 136 elif boolean == 'AND': 137 spin.select = spin.select and False 138 elif boolean == 'NAND': 139 spin.select = not (spin.select and False) 140 elif boolean == 'XOR': 141 spin.select = not (spin.select and False) and (spin.select or False) 142 elif boolean == 'XNOR': 143 spin.select = (spin.select and False) or not (spin.select or False) 144 else: 145 raise RelaxError("Unknown boolean operator " + repr(boolean))
146 147
148 -def desel_spin(spin_id=None, boolean='AND', change_all=False):
149 """Deselect specific spins. 150 151 @keyword spin_id: The spin identification string. 152 @type spin_id: str or None 153 @param boolean: The boolean operator used to deselect the spin systems with. It 154 can be one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. 155 This will be ignored if the change_all flag is set. 156 @type boolean: str 157 @keyword change_all: A flag which if True will cause all spins not specified in the 158 file to be selected. Only the boolean operator 'AND' is 159 compatible with this flag set to True (all others will be 160 ignored). 161 @type change_all: bool 162 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 163 @raises RelaxError: If the boolean operator is unknown. 164 """ 165 166 # Test if the current data pipe exists. 167 pipes.test() 168 169 # Test if sequence data is loaded. 170 if not exists_mol_res_spin_data(): 171 raise RelaxNoSequenceError 172 173 # First select all spins if the change_all flag is set. 174 if change_all: 175 for spin in spin_loop(): 176 spin.select = True 177 178 # Loop over the specified spins. 179 for spin in spin_loop(spin_id): 180 # Deselect just the specified residues. 181 if change_all: 182 spin.select = False 183 184 # Boolean selections. 185 else: 186 if boolean == 'OR': 187 spin.select = spin.select or False 188 elif boolean == 'NOR': 189 spin.select = not (spin.select or False) 190 elif boolean == 'AND': 191 spin.select = spin.select and False 192 elif boolean == 'NAND': 193 spin.select = not (spin.select and False) 194 elif boolean == 'XOR': 195 spin.select = not (spin.select and False) and (spin.select or False) 196 elif boolean == 'XNOR': 197 spin.select = (spin.select and False) or not (spin.select or False) 198 else: 199 raise RelaxError("Unknown boolean operator " + repr(boolean))
200 201
202 -def is_mol_selected(selection=None):
203 """Query if the molecule is selected. 204 205 @keyword selection: The molecule ID string. 206 @type selection: str 207 """ 208 209 # Find if any spins are selected. 210 select = False 211 for spin in spin_loop(selection): 212 if spin.select: 213 select = True 214 break 215 216 # Return the state. 217 return select
218 219
220 -def is_res_selected(selection=None):
221 """Query if the residue is selected. 222 223 @keyword selection: The residue ID string. 224 @type selection: str 225 """ 226 227 # Find if any spins are selected. 228 select = False 229 for spin in spin_loop(selection): 230 if spin.select: 231 select = True 232 break 233 234 # Return the state. 235 return select
236 237
238 -def is_spin_selected(selection=None):
239 """Query if the spin is selected. 240 241 @keyword selection: The molecule ID string. 242 @type selection: str 243 """ 244 245 # Get the spin. 246 spin = return_spin(selection) 247 248 # Return the selected state. 249 return spin.select
250 251
252 -def reverse(spin_id=None):
253 """Reversal of spin selections. 254 255 @keyword spin_id: The spin identification string. 256 @type spin_id: str or None 257 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 258 """ 259 260 # Test if the current data pipe exists. 261 pipes.test() 262 263 # Test if sequence data is loaded. 264 if not exists_mol_res_spin_data(): 265 raise RelaxNoSequenceError 266 267 # Loop over the spin systems and reverse the selection flag. 268 for spin in spin_loop(spin_id): 269 # Reverse the selection. 270 if spin.select: 271 spin.select = False 272 else: 273 spin.select = True
274 275
276 -def sel_all():
277 """Select all residues. 278 279 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 280 """ 281 282 # Test if the current data pipe exists. 283 pipes.test() 284 285 # Test if sequence data is loaded. 286 if not exists_mol_res_spin_data(): 287 raise RelaxNoSequenceError 288 289 # Loop over the spins and select them. 290 for spin in spin_loop(): 291 spin.select = True
292 293
294 -def sel_read(file=None, dir=None, file_data=None, spin_id_col=None, mol_name_col=None, res_num_col=None, res_name_col=None, spin_num_col=None, spin_name_col=None, sep=None, spin_id=None, boolean='OR', change_all=False):
295 """Select the spins contained in the given file. 296 297 @keyword file: The name of the file to open. 298 @type file: str 299 @keyword dir: The directory containing the file (defaults to the current 300 directory if None). 301 @type dir: str or None 302 @keyword file_data: An alternative opening a file, if the data already exists in the 303 correct format. The format is a list of lists where the first 304 index corresponds to the row and the second the column. 305 @type file_data: list of lists 306 @keyword spin_id_col: The column containing the spin ID strings. If supplied, the 307 mol_name_col, res_name_col, res_num_col, spin_name_col, and 308 spin_num_col arguments must be none. 309 @type spin_id_col: int or None 310 @keyword mol_name_col: The column containing the molecule name information. If 311 supplied, spin_id_col must be None. 312 @type mol_name_col: int or None 313 @keyword res_name_col: The column containing the residue name information. If 314 supplied, spin_id_col must be None. 315 @type res_name_col: int or None 316 @keyword res_num_col: The column containing the residue number information. If 317 supplied, spin_id_col must be None. 318 @type res_num_col: int or None 319 @keyword spin_name_col: The column containing the spin name information. If supplied, 320 spin_id_col must be None. 321 @type spin_name_col: int or None 322 @keyword spin_num_col: The column containing the spin number information. If supplied, 323 spin_id_col must be None. 324 @type spin_num_col: int or None 325 @keyword sep: The column separator which, if None, defaults to whitespace. 326 @type sep: str or None 327 @keyword spin_id: The spin ID string used to restrict data loading to a subset of 328 all spins. 329 @type spin_id: None or str 330 @param boolean: The boolean operator used to select the spin systems with. It 331 can be one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. 332 This will be ignored if the change_all flag is set. 333 @type boolean: str 334 @keyword change_all: A flag which if True will cause all spins not specified in the 335 file to be deselected. Only the boolean operator 'OR' is 336 compatible with this flag set to True (all others will be 337 ignored). 338 @type change_all: bool 339 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 340 @raises RelaxError: If the boolean operator is unknown. 341 """ 342 343 # Test if the current data pipe exists. 344 pipes.test() 345 346 # Test if sequence data is loaded. 347 if not exists_mol_res_spin_data(): 348 raise RelaxNoSequenceError 349 350 # First deselect all spins if the change_all flag is set. 351 if change_all: 352 # Loop over all spins. 353 for spin in spin_loop(): 354 spin.select = False 355 356 # Then deselect the spins in the file. 357 for id in read_spin_data(file=file, dir=dir, file_data=file_data, spin_id_col=spin_id_col, mol_name_col=mol_name_col, res_num_col=res_num_col, res_name_col=res_name_col, spin_num_col=spin_num_col, spin_name_col=spin_name_col, sep=sep, spin_id=spin_id): 358 # Get the corresponding spin container. 359 spin = return_spin(id) 360 361 # No spin. 362 if spin == None: 363 warn(RelaxNoSpinWarning(id)) 364 continue 365 366 # Select the spin. 367 if change_all: 368 spin.select = True 369 370 # Boolean selections. 371 else: 372 if boolean == 'OR': 373 spin.select = spin.select or True 374 elif boolean == 'NOR': 375 spin.select = not (spin.select or True) 376 elif boolean == 'AND': 377 spin.select = spin.select and True 378 elif boolean == 'NAND': 379 spin.select = not (spin.select and True) 380 elif boolean == 'XOR': 381 spin.select = not (spin.select and True) and (spin.select or True) 382 elif boolean == 'XNOR': 383 spin.select = (spin.select and True) or not (spin.select or True) 384 else: 385 raise RelaxError("Unknown boolean operator " + repr(boolean))
386 387
388 -def sel_spin(spin_id=None, boolean='OR', change_all=False):
389 """Select specific spins. 390 391 @keyword spin_id: The spin identification string. 392 @type spin_id: str or None 393 @param boolean: The boolean operator used to select the spin systems with. It 394 can be one of 'OR', 'NOR', 'AND', 'NAND', 'XOR', or 'XNOR'. 395 This will be ignored if the change_all flag is set. 396 @type boolean: str 397 @keyword change_all: A flag which if True will cause all spins not specified in the 398 file to be deselected. Only the boolean operator 'OR' is 399 compatible with this flag set to True (all others will be 400 ignored). 401 @type change_all: bool 402 @raises RelaxNoSequenceError: If no molecule/residue/spins sequence data exists. 403 @raises RelaxError: If the boolean operator is unknown. 404 """ 405 406 # Test if the current data pipe exists. 407 pipes.test() 408 409 # Test if sequence data is loaded. 410 if not exists_mol_res_spin_data(): 411 raise RelaxNoSequenceError 412 413 # First deselect all spins if the change_all flag is set. 414 if change_all: 415 # Loop over all spins. 416 for spin in spin_loop(): 417 spin.select = False 418 419 # Loop over the specified spins. 420 for spin in spin_loop(spin_id): 421 # Select just the specified residues. 422 if change_all: 423 spin.select = True 424 425 # Boolean selections. 426 else: 427 if boolean == 'OR': 428 spin.select = spin.select or True 429 elif boolean == 'NOR': 430 spin.select = not (spin.select or True) 431 elif boolean == 'AND': 432 spin.select = spin.select and True 433 elif boolean == 'NAND': 434 spin.select = not (spin.select and True) 435 elif boolean == 'XOR': 436 spin.select = not (spin.select and True) and (spin.select or True) 437 elif boolean == 'XNOR': 438 spin.select = (spin.select and True) or not (spin.select or True) 439 else: 440 raise RelaxError("Unknown boolean operator " + repr(boolean))
441