1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 from os import F_OK, access
24 from re import compile, match
25
26
29 """Base class containing functions for the manipulation of residue selection."""
30
31 self.relax = relax
32
33
60
61
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
103 file_data = self.relax.IO.extract_data(file, dir)
104
105
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
116 file_data = file_data[header_lines:]
117
118
119 file_data = self.relax.IO.strip(file_data)
120
121
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
130 self.runs = self.relax.generic.runs.list_of_runs(run)
131
132
133 for self.run in self.runs:
134
135 if not self.run in self.relax.data.run_names:
136 raise RelaxNoRunError, self.run
137
138
139 if not len(self.relax.data.res[self.run]):
140 raise RelaxNoSequenceError, self.run
141
142
143 for i in xrange(len(self.relax.data.res[self.run])):
144
145 data = self.relax.data.res[self.run][i]
146
147
148 if data.num in select:
149 new_select = 1
150 else:
151 new_select = 0
152
153
154 if change_all:
155 data.select = new_select
156
157
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
193 if type(num) == str:
194 try:
195 compile(num)
196 except:
197 raise RelaxRegExpError, ('residue number', num)
198
199
200 if name:
201 try:
202 compile(name)
203 except:
204 raise RelaxRegExpError, ('residue name', name)
205
206
207 self.runs = self.relax.generic.runs.list_of_runs(run)
208
209
210 no_match = 1
211 for self.run in self.runs:
212
213 if not self.run in self.relax.data.run_names:
214 raise RelaxNoRunError, self.run
215
216
217 if not len(self.relax.data.res[self.run]):
218 raise RelaxNoSequenceError, self.run
219
220
221 for i in xrange(len(self.relax.data.res[self.run])):
222
223 data = self.relax.data.res[self.run][i]
224
225
226 new_select = 0
227
228
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
237 if name != None:
238 if match(name, data.name):
239 new_select = 1
240
241
242 if change_all:
243 data.select = new_select
244
245
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
262 if new_select:
263 no_match = 0
264
265
266 if no_match:
267 print "No residues match."
268
269
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
295 file_data = self.relax.IO.extract_data(file, dir)
296
297
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
308 file_data = file_data[header_lines:]
309
310
311 file_data = self.relax.IO.strip(file_data)
312
313
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
322 self.runs = self.relax.generic.runs.list_of_runs(run)
323
324
325 no_match = 1
326 for self.run in self.runs:
327
328 if not self.run in self.relax.data.run_names:
329 raise RelaxNoRunError, self.run
330
331
332 if not len(self.relax.data.res[self.run]):
333 raise RelaxNoSequenceError, self.run
334
335
336 for i in xrange(len(self.relax.data.res[self.run])):
337
338 data = self.relax.data.res[self.run][i]
339
340
341 if change_all:
342 data.select = 1
343
344
345 if data.num in unselect:
346 data.select = 0
347
348
349 no_match = 0
350
351
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
360 if type(num) == str:
361 try:
362 compile(num)
363 except:
364 raise RelaxRegExpError, ('residue number', num)
365
366
367 if name:
368 try:
369 compile(name)
370 except:
371 raise RelaxRegExpError, ('residue name', name)
372
373
374 self.runs = self.relax.generic.runs.list_of_runs(run)
375
376
377 no_match = 1
378 for self.run in self.runs:
379
380 if not self.run in self.relax.data.run_names:
381 raise RelaxNoRunError, self.run
382
383
384 if not len(self.relax.data.res[self.run]):
385 raise RelaxNoSequenceError, self.run
386
387
388 for i in xrange(len(self.relax.data.res[self.run])):
389
390 data = self.relax.data.res[self.run][i]
391
392
393 if change_all:
394 data.select = 1
395
396
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
405 if name != None:
406 if not match(name, data.name):
407 continue
408
409
410 data.select = 0
411
412
413 no_match = 0
414
415
416 if no_match:
417 print "No residues match."
418