1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Functions for interfacing with Flemming Hansen's CATIA program."""
24
25
26 import dep_check
27
28
29 from os import F_OK, access, chdir, getcwd, sep
30 PIPE, Popen = None, None
31 if dep_check.subprocess_module:
32 from subprocess import PIPE, Popen
33 import sys
34
35
36 from lib.dispersion.variables import EXP_TYPE_CPMG_SQ
37 from lib.errors import RelaxError, RelaxDirError
38 from lib.io import mkdir_nofail, open_write_file, test_binary
39 from pipe_control.mol_res_spin import check_mol_res_spin_data, spin_loop
40 from pipe_control.pipes import check_pipe
41 from specific_analyses.relax_disp.checks import check_model_type, check_spectra_id_setup
42 from specific_analyses.relax_disp.data import loop_frq, loop_offset_point, return_param_key_from_data
43
44
46 """Create the CATIA input files.
47
48 @keyword file: The main CATIA execution file.
49 @type file: str
50 @keyword dir: The optional directory to place the files into. If None, then the files will be placed into the current directory.
51 @type dir: str or None
52 @keyword binary: The name of the CATIA binary file. This can include the path to the binary.
53 @type binary: str
54 """
55
56
57 orig_dir = getcwd()
58
59
60 test_binary(binary)
61
62
63 if dir == None:
64 dir = orig_dir
65 if not access(dir, F_OK):
66 raise RelaxDirError('CATIA', dir)
67
68
69 chdir(dir)
70
71
72 try:
73
74 cmd = binary + ' < Fit.catia'
75 pipe = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False)
76
77
78 pipe.stdin.close()
79
80
81 for line in pipe.stdout.readlines():
82
83 if hasattr(line, 'decode'):
84 line = line.decode()
85
86
87 sys.stdout.write(line)
88
89
90 for line in pipe.stderr.readlines():
91
92 if hasattr(line, 'decode'):
93 line = line.decode()
94
95
96 sys.stderr.write(line)
97
98
99 except:
100
101 chdir(orig_dir)
102
103
104 raise
105
106
107 chdir(orig_dir)
108
109
149
150
151 -def write_main_file(file=None, dir=None, output_dir=None, f_tol=1e-25, max_iter=10000000, r1=False, force=False):
152 """Create the main CATIA execution file.
153
154 @keyword file: The main CATIA execution file.
155 @type file: str
156 @keyword dir: The directory to place the files into.
157 @type dir: str or None
158 @keyword output_dir: The CATIA output directory, located within the directory specified by the dir argument. This directory will be created.
159 @type output_dir: str
160 @keyword r1: A flag which if True will cause the R1 data to be used for off-resonance effects.
161 @type r1: bool
162 @keyword force: A flag which if True will cause a pre-existing file to be overwritten.
163 @type force: bool
164 """
165
166
167 catia_in = open_write_file(file_name=file, dir=dir, force=force)
168
169
170 for frq in loop_frq():
171 frq_label = int(frq*1e-6)
172 file_name = "data_set_%i.inp" % frq_label
173 catia_in.write("ReadDataset(%s)\n" % file_name)
174
175
176 catia_in.write("ReadParam_Global(ParamGlobal.inp)\n")
177 catia_in.write("ReadParam_Local(ParamSet1.inp)\n")
178 catia_in.write("\n")
179
180
181 if r1:
182 catia_in.write("ReadParam(Omega;%s;0;1)\n" % shift_file)
183 for frq in loop_frq():
184 frq_label = int(frq*1e-6)
185 param = "R1iph_%s" % frq_label
186 r1_file = "R1_%s.out" % frq_label
187 catia_in.write("ReadParam(%s;%s;0;1)\n" % (param, r1_file))
188 catia_in.write("\n")
189
190
191 catia_in.write("FreeLocalParam(all;Omega;false)\n")
192 for frq in loop_frq():
193 frq_label = int(frq*1e-6)
194 param = "R1iph_%s" % frq_label
195 catia_in.write("FreeLocalParam(all;%s;false)\n" % param)
196 catia_in.write("\n")
197
198
199 catia_in.write("Minimize(print=y;tol=%s;maxiter=%i)\n" % (f_tol, max_iter))
200 catia_in.write("\n")
201
202
203 catia_in.write("PrintParam(%s/GlobalParam.fit;global)\n" % output_dir)
204 catia_in.write("PrintParam(%s/DeltaOmega.fit;DeltaO)\n" % output_dir)
205 catia_in.write("PrintData(%s/)\n" % output_dir)
206 catia_in.write("\n")
207
208
209 catia_in.write("ChiSq(all;all)\n")
210
211
212 catia_in.write("exit(0)\n")
213
214
215 catia_in.close()
216
217
218 -def write_param_files(global_file=None, kex=1500.0, pA=0.95, set_file=None, dir=None, r1=False, force=False):
219 """Create the CATIA parameter files.
220
221 @keyword global_file: The name of the global parameter file.
222 @type global_file: str
223 @keyword set_file: The name of the parameter set file.
224 @type set_file: str
225 @keyword dir: The base directory to place the files into.
226 @type dir: str
227 @keyword r1: A flag which if True will cause the R1 data to be used for off-resonance effects.
228 @type r1: bool
229 @keyword force: A flag which if True will cause a pre-existing file to be overwritten.
230 @type force: bool
231 """
232
233
234 param_file = open_write_file(file_name=global_file, dir=dir, force=force)
235
236
237 param_file.write("kex = %s\n" % kex)
238 param_file.write("pb = %s\n" % (1.0-pA))
239
240
241 param_file.close()
242
243
244 set_file = open_write_file(file_name=set_file, dir=dir, force=force)
245
246
247 params = ['Delta0']
248 values = [0.5]
249 if r1:
250 for frq in loop_frq():
251 params.append("R1iph_%s" % frq_label(frq))
252 values.append(1.5)
253 for frq in loop_frq():
254 params.append("R0iph_%s" % frq_label(frq))
255 values.append(5.0)
256 if r1:
257 params.append("Omega")
258 values.append(0.0)
259
260
261 set_file.write("format = (")
262 for i in range(len(params)):
263 if i != 0:
264 set_file.write(';')
265 set_file.write(params[i])
266 set_file.write(")\n")
267
268
269 set_file.write("* = (")
270 for i in range(len(values)):
271 if i != 0:
272 set_file.write(';')
273 set_file.write("%s" % values[i])
274 set_file.write(")\n")
275
276
277 set_file.close()
278
279
281 """Convert the frequency in Hz to a label in MHz.
282
283 @param frq: The frequency to convert.
284 @type frq: float
285 @return: The frequency in MHz as a label.
286 @rtype: str
287 """
288
289
290 return str(int(frq*1e-6))
291
292
294 """Create the CATIA R2eff input files.
295
296 @keyword input_dir: The special directory for the R2eff input files.
297 @type input_dir: str
298 @keyword base_dir: The base directory to place the files into.
299 @type base_dir: str
300 @keyword force: A flag which if True will cause a pre-existing file to be overwritten.
301 @type force: bool
302 """
303
304
305 dir = base_dir + sep + input_dir
306 mkdir_nofail(dir, verbosity=0)
307
308
309 isotope = None
310 for spin in spin_loop(skip_desel=True):
311 if hasattr(spin, 'isotope'):
312 if isotope == None:
313 isotope = spin.isotope
314 elif spin.isotope != isotope:
315 raise RelaxError("CATIA only supports one spin type.")
316 if isotope == None:
317 raise RelaxError("The spin isotopes have not been specified.")
318
319
320 if isotope == '1H':
321 isotope = 'H1'
322 elif isotope == '13C':
323 isotope = 'C13'
324 elif isotope == '15N':
325 isotope = 'N15'
326
327
328 for frq, mi in loop_frq(return_indices=True):
329
330 frq_string = int(frq*1e-6)
331
332
333 file_name = "data_set_%i.inp" % frq_string
334 set_file = open_write_file(file_name=file_name, dir=base_dir, force=force)
335 id = frq_string
336 set_file.write("ID=%s\n" % id)
337 set_file.write("Sfrq = %s\n" % frq_string)
338 set_file.write("Temperature = %s\n" % 0.0)
339 set_file.write("Nucleus = %s\n" % isotope)
340 set_file.write("Couplednucleus = %s\n" % 'H1')
341 set_file.write("Time_equil = %s\n" % 0.0)
342 set_file.write("Pwx_cp = %s\n" % 0.0)
343 set_file.write("Taub = %s\n" % 0.0)
344 set_file.write("Time_T2 = %s\n"% cdp.relax_time_list[0])
345 set_file.write("Xcar = %s\n" % 0.0)
346 set_file.write("Seqfil = %s\n" % 'CW_CPMG')
347 set_file.write("Minerror = %s\n" % "(2.%;0.5/s)")
348 set_file.write("Basis = (%s)\n" % "Iph_7")
349 set_file.write("Format = (%i;%i;%i)\n" % (0, 1, 2))
350 set_file.write("DataDirectory = %s\n" % (dir+sep))
351 set_file.write("Data = (\n")
352
353
354 for spin, mol_name, res_num, res_name, spin_id in spin_loop(full_info=True, return_id=True, skip_desel=True):
355
356 file_name = "spin%s_%i.cpmg" % (spin_id.replace('#', '_').replace(':', '_').replace('@', '_'), frq_string)
357 spin_file = open_write_file(file_name=file_name, dir=dir, force=force)
358
359
360 spin_file.write("# %18s %20s %20s\n" % ("nu_cpmg(Hz)", "R2(1/s)", "Esd(R2)"))
361
362
363 for offset, point, oi, di in loop_offset_point(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, return_indices=True):
364
365 key = return_param_key_from_data(exp_type=EXP_TYPE_CPMG_SQ, frq=frq, offset=offset, point=point)
366
367
368 if key not in spin.r2eff:
369 continue
370
371
372 spin_file.write("%20.15f %20.15f %20.15f\n" % (point, spin.r2eff[key], spin.r2eff_err[key]))
373
374
375 spin_file.close()
376
377
378 catia_spin_id = "%i%s" % (res_num, spin.name)
379 set_file.write(" [%s;%s];\n" % (catia_spin_id, file_name))
380
381
382 set_file.write(")\n")
383 set_file.close()
384