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

Source Code for Module generic_fns.selection

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