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 the manipulation of parameter and constant values."""
25
26
27 from numpy import ndarray
28 import sys
29
30
31 from generic_fns import minimise, pipes
32 from generic_fns.mol_res_spin import exists_mol_res_spin_data, generate_spin_id_data_array, return_spin, spin_loop
33 from relax_errors import RelaxError, RelaxNoSequenceError, RelaxNoSpinError, RelaxParamSetError, RelaxValueError
34 from relax_io import get_file_path, open_write_file, read_spin_data, write_spin_data
35 import specific_fns
36 from status import Status; status = Status()
37
38
39 -def copy(pipe_from=None, pipe_to=None, param=None):
40 """Copy spin specific data values from pipe_from to pipe_to.
41
42 @param pipe_from: The data pipe to copy the value from. This defaults to the current data
43 pipe.
44 @type pipe_from: str
45 @param pipe_to: The data pipe to copy the value to. This defaults to the current data pipe.
46 @type pipe_to: str
47 @param param: The name of the parameter to copy the values of.
48 @type param: str
49 """
50
51
52 if pipe_from == None:
53 pipe_from = pipes.cdp_name()
54 if pipe_to == None:
55 pipe_to = pipes.cdp_name()
56
57
58 pipes.test(pipe_to)
59
60
61 if not exists_mol_res_spin_data(pipe_from):
62 raise RelaxNoSequenceError(pipe_from)
63
64
65 if not exists_mol_res_spin_data(pipe_to):
66 raise RelaxNoSequenceError(pipe_to)
67
68
69 return_value = specific_fns.setup.get_specific_fn('return_value', pipes.get_type(pipe_from))
70
71
72 for spin in spin_loop(pipe_to):
73
74 value, error = return_value(spin, param)
75
76
77 if value != None or error != None:
78 raise RelaxValueError(param, pipe_to)
79
80
81 for spin, spin_id in spin_loop(pipe_from, return_id=True):
82
83 value, error = return_value(spin, param)
84
85
86 spin_to = return_spin(spin_id, pipe_to)
87
88
89 set(spin_id=spin_to, value=value, error=error, param=param)
90
91
92 minimise.reset_min_stats(pipe_to)
93
94
111
112
114 """Function for sorting and partitioning the parameters and their values.
115
116 The two major partitions are the tensor parameters and the spin specific parameters.
117
118 @param val: The parameter values.
119 @type val: None, number, or list of numbers
120 @param param: The parameter names.
121 @type param: None, str, or list of str
122 @return: A tuple, of length 4, of lists. The first and second elements are the lists of
123 spin specific parameters and values respectively. The third and forth elements
124 are the lists of all other parameters and their values.
125 @rtype: tuple of 4 lists
126 """
127
128
129 is_spin_param = specific_fns.setup.get_specific_fn('is_spin_param', pipes.get_type())
130
131
132 spin_params = []
133 spin_values = []
134 other_params = []
135 other_values = []
136
137
138 if isinstance(param, str):
139
140 if is_spin_param(param):
141 params = spin_params
142 values = spin_values
143
144
145 else:
146 params = other_params
147 values = other_values
148
149
150 if isinstance(val, list) or isinstance(val, ndarray):
151
152 for i in xrange(len(val)):
153 params.append(param)
154
155
156 values = val
157
158
159 else:
160
161 params.append(param)
162
163
164 values.append(val)
165
166
167 elif isinstance(param, list):
168
169 for i in xrange(len(param)):
170
171 if is_spin_param(param[i]):
172 params = spin_params
173 values = spin_values
174
175
176 else:
177 params = other_params
178 values = other_values
179
180
181 params.append(param[i])
182
183
184 if isinstance(val, list) or isinstance(val, ndarray):
185 values.append(val[i])
186 else:
187 values.append(val)
188
189
190
191 return spin_params, spin_values, other_params, other_values
192
193
194 -def read(param=None, scaling=1.0, 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, data_col=None, error_col=None, sep=None, spin_id=None):
195 """Read spin specific data values from a file.
196
197 @keyword param: The name of the parameter to read.
198 @type param: str
199 @keyword scaling: A scaling factor by which all read values are multiplied by.
200 @type scaling: float
201 @keyword file: The name of the file to open.
202 @type file: str
203 @keyword dir: The directory containing the file (defaults to the current directory if None).
204 @type dir: str or None
205 @keyword file_data: An alternative to opening a file, if the data already exists in the correct format. The format is a list of lists where the first index corresponds to the row and the second the column.
206 @type file_data: list of lists
207 @keyword spin_id_col: The column containing the spin ID strings. If supplied, the mol_name_col, res_name_col, res_num_col, spin_name_col, and spin_num_col arguments must be none.
208 @type spin_id_col: int or None
209 @keyword mol_name_col: The column containing the molecule name information. If supplied, spin_id_col must be None.
210 @type mol_name_col: int or None
211 @keyword res_name_col: The column containing the residue name information. If supplied, spin_id_col must be None.
212 @type res_name_col: int or None
213 @keyword res_num_col: The column containing the residue number information. If supplied, spin_id_col must be None.
214 @type res_num_col: int or None
215 @keyword spin_name_col: The column containing the spin name information. If supplied, spin_id_col must be None.
216 @type spin_name_col: int or None
217 @keyword spin_num_col: The column containing the spin number information. If supplied, spin_id_col must be None.
218 @type spin_num_col: int or None
219 @keyword data_col: The column containing the RDC data in Hz.
220 @type data_col: int or None
221 @keyword error_col: The column containing the RDC errors.
222 @type error_col: int or None
223 @keyword sep: The column separator which, if None, defaults to whitespace.
224 @type sep: str or None
225 @keyword spin_id: The spin ID string.
226 @type spin_id: None or str
227 """
228
229
230 pipes.test()
231
232
233 if not exists_mol_res_spin_data():
234 raise RelaxNoSequenceError
235
236
237 if minimise.return_data_name(param):
238
239 min_stat = True
240
241
242 return_value = minimise.return_value
243
244
245 set_fn = minimise.set
246
247
248 else:
249
250 min_stat = False
251
252
253 return_value = specific_fns.setup.get_specific_fn('return_value', pipes.get_type())
254
255
256 set_fn = set
257
258
259 for spin in spin_loop():
260
261 if not spin.select:
262 continue
263
264
265 value, error = return_value(spin, param)
266
267
268 if value != None or error != None:
269 raise RelaxValueError(param)
270
271
272 for data 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, data_col=data_col, error_col=error_col, sep=sep, spin_id=spin_id):
273
274 if data_col and error_col:
275 id, value, error = data
276 elif data_col:
277 id, value = data
278 else:
279 id, error = data
280
281
282 set_fn(val=value, error=error, param=param, spin_id=id)
283
284
285 if not min_stat:
286 minimise.reset_min_stats()
287
288
289 -def set(val=None, param=None, error=None, pipe=None, spin_id=None, force=True, reset=True):
290 """Set global or spin specific data values.
291
292 @keyword val: The parameter values.
293 @type val: None or list
294 @keyword param: The parameter names.
295 @type param: None, str, or list of str
296 @keyword error: The parameter errors.
297 @type error: None, number, or list of numbers
298 @keyword pipe: The data pipe the values should be placed in.
299 @type pipe: None or str
300 @keyword spin_id: The spin identification string.
301 @type spin_id: str
302 @keyword force: A flag forcing the overwriting of current values.
303 @type force: bool
304 @keyword reset: A flag which if True will cause all minimisation statistics to be reset.
305 @type reset: bool
306 """
307
308
309 if pipe:
310 orig_pipe = pipes.cdp_name()
311 pipes.switch(pipe)
312
313
314 pipes.test()
315
316
317 default_value = specific_fns.setup.get_specific_fn('default_value', pipes.get_type())
318 get_param_names = specific_fns.setup.get_specific_fn('get_param_names', pipes.get_type())
319 return_data_name = specific_fns.setup.get_specific_fn('return_data_name', pipes.get_type())
320 set_param_values = specific_fns.setup.get_specific_fn('set_param_values', pipes.get_type())
321
322
323 if isinstance(val, ndarray):
324 val = val.tolist()
325
326
327 if (isinstance(val, float) or isinstance(val, int)) and param == None:
328 raise RelaxError("The combination of a single value '%s' without specifying the parameter name is invalid." % val)
329 if isinstance(val, list) and isinstance(param, str):
330 raise RelaxError("Invalid combination: When multiple values '%s' are specified, either no parameters or a list of parameters must by supplied rather than the single parameter '%s'." % (val, param))
331
332
333 if param == None:
334 param = get_param_names()
335
336
337 if not isinstance(param, list):
338 param = [param]
339
340
341 if val != None and not isinstance(val, list):
342 val = [val] * len(param)
343
344
345 if val == None:
346
347 val = []
348 for i in range(len(param)):
349 val.append(default_value(return_data_name(param[i])))
350
351
352 if val[-1] == None:
353 raise RelaxParamSetError(param[i])
354
355
356 set_param_values(param=param, value=val, spin_id=spin_id, force=force)
357
358
359 if reset:
360 minimise.reset_min_stats()
361
362
363 if pipe:
364 pipes.switch(orig_pipe)
365
366
367 -def write(param=None, file=None, dir=None, bc=False, force=False, return_value=None):
368 """Write data to a file.
369
370 @keyword param: The name of the parameter to write to file.
371 @type param: str
372 @keyword file: The file to write the data to.
373 @type file: str
374 @keyword dir: The name of the directory to place the file into (defaults to the
375 current directory).
376 @type dir: str
377 @keyword bc: A flag which if True will cause the back calculated values to be written.
378 @type bc: bool
379 @keyword force: A flag which if True will cause any pre-existing file to be overwritten.
380 @type force: bool
381 @keyword return_value: An optional function which if supplied will override the default value
382 returning function.
383 @type return_value: None or func
384 """
385
386
387 pipes.test()
388
389
390 if not exists_mol_res_spin_data():
391 raise RelaxNoSequenceError
392
393
394 file_path = get_file_path(file, dir)
395 file = open_write_file(file, dir, force)
396
397
398 write_data(param, file, bc, return_value)
399
400
401 file.close()
402
403
404 if not hasattr(cdp, 'result_files'):
405 cdp.result_files = []
406 cdp.result_files.append(['text', 'Text', file_path])
407 status.observers.result_file.notify()
408
409
410 -def write_data(param=None, file=None, bc=False, return_value=None):
411 """The function which actually writes the data.
412
413 @keyword param: The parameter to write.
414 @type param: str
415 @keyword file: The file to write the data to.
416 @type file: str
417 @keyword bc: A flag which if True will cause the back calculated values to be written.
418 @type bc: bool
419 @keyword return_value: An optional function which if supplied will override the default value returning function.
420 @type return_value: None or func
421 """
422
423
424 if not return_value:
425 return_value = specific_fns.setup.get_specific_fn('return_value', pipes.get_type())
426
427
428 format = "%-30s%-30s"
429
430
431 mol_names = []
432 res_nums = []
433 res_names = []
434 spin_nums = []
435 spin_names = []
436 values = []
437 errors = []
438
439
440 for spin, mol_name, res_num, res_name in spin_loop(full_info=True):
441
442 value, error = return_value(spin, param, bc=bc)
443
444
445 mol_names.append(mol_name)
446 res_nums.append(res_num)
447 res_names.append(res_name)
448 spin_nums.append(spin.num)
449 spin_names.append(spin.name)
450 values.append(value)
451 errors.append(error)
452
453
454 write_spin_data(file, mol_names=mol_names, res_nums=res_nums, res_names=res_names, spin_nums=spin_nums, spin_names=spin_names, data=values, data_name='value', error=errors, error_name='error')
455