1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module for selecting and deselecting spins."""
25
26
27 from warnings import warn
28
29
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
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
122 pipes.test()
123
124
125 if not exists_mol_res_spin_data():
126 raise RelaxNoSequenceError
127
128
129 if change_all:
130 for spin in spin_loop():
131 spin.select = True
132
133
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
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
140 if spin == None:
141 warn(RelaxNoSpinWarning(id))
142 continue
143
144
145 if change_all:
146 spin.select = False
147
148
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
185 pipes.test()
186
187
188 if not exists_mol_res_spin_data():
189 raise RelaxNoSequenceError
190
191
192 if change_all:
193 for spin in spin_loop():
194 spin.select = True
195
196
197 for spin in spin_loop(spin_id):
198
199 if change_all:
200 spin.select = False
201
202
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
221 """Query if the molecule is selected.
222
223 @keyword selection: The molecule ID string.
224 @type selection: str
225 """
226
227
228 select = False
229 for spin in spin_loop(selection):
230 if spin.select:
231 select = True
232 break
233
234
235 return select
236
237
239 """Query if the residue is selected.
240
241 @keyword selection: The residue ID string.
242 @type selection: str
243 """
244
245
246 select = False
247 for spin in spin_loop(selection):
248 if spin.select:
249 select = True
250 break
251
252
253 return select
254
255
257 """Query if the spin is selected.
258
259 @keyword selection: The molecule ID string.
260 @type selection: str
261 """
262
263
264 spin = return_spin(selection)
265
266
267 return spin.select
268
269
292
293
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
362 pipes.test()
363
364
365 if not exists_mol_res_spin_data():
366 raise RelaxNoSequenceError
367
368
369 if change_all:
370
371 for spin in spin_loop():
372 spin.select = False
373
374
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
377 spin = return_spin(id)
378
379
380 if spin == None:
381 warn(RelaxNoSpinWarning(id))
382 continue
383
384
385 if change_all:
386 spin.select = True
387
388
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
425 pipes.test()
426
427
428 if not exists_mol_res_spin_data():
429 raise RelaxNoSequenceError
430
431
432 if change_all:
433
434 for spin in spin_loop():
435 spin.select = False
436
437
438 for spin in spin_loop(spin_id):
439
440 if change_all:
441 spin.select = True
442
443
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