1 from re import match
2 from os import chmod
3 import sys
4
5 from common_ops import common_ops
6
9 "Class used to create and process input and output for the program Modelfree 4."
10
11 self.mf = mf
12
13 print "Model-free analysis based on " + self.mf.usr_param.method + " model selection."
14 self.ask_stage()
15 title = "<<< Stage " + self.mf.data.stage + " - "
16 title = title + self.mf.usr_param.method + " model selection >>>\n\n\n"
17
18 if self.mf.debug:
19 self.mf.file_ops.init_log_file(title)
20
21 self.update_data()
22 self.extract_relax_data()
23
24 if self.mf.debug:
25 self.log_input_info()
26
27 if match('^AIC$', self.mf.usr_param.method) or match('^AICc$', self.mf.usr_param.method):
28 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
29 self.model_selection = self.mf.modsel.asymptotic
30 elif match('^BIC$', self.mf.usr_param.method):
31 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
32 self.model_selection = self.mf.modsel.asymptotic
33 elif match('^Bootstrap$', self.mf.usr_param.method):
34 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
35 self.model_selection = self.mf.modsel.bootstrap
36 elif match('^CV$', self.mf.usr_param.method):
37 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
38 self.model_selection = self.mf.modsel.cv
39 elif match('^Expect$', self.mf.usr_param.method):
40 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
41 self.model_selection = self.mf.modsel.exp_overall_disc
42 elif match('^Farrow$', self.mf.usr_param.method):
43 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
44 self.model_selection = self.mf.modsel.farrow
45 elif match('^Palmer$', self.mf.usr_param.method):
46 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5', 'f-m1m2', 'f-m1m3']
47 if self.mf.data.num_data_sets > 3:
48 self.mf.data.runs.append('f-m2m4')
49 self.mf.data.runs.append('f-m2m5')
50 self.mf.data.runs.append('f-m3m4')
51 self.model_selection = self.mf.modsel.palmer
52 elif match('^Overall$', self.mf.usr_param.method):
53 message = "See the file 'modsel/overall_disc.py' for details.\n"
54 self.mf.file_ops.read_file('op_data', message)
55 self.mf.data.overall_disc.op_data = self.mf.file_ops.open_file(file_name='op_data')
56 self.mf.data.runs = ['m1', 'm2', 'm3', 'm4', 'm5']
57
58 else:
59 raise NameError, "The model-free analysis method is not set correctly. Check self.method in the file 'usr_param.py', quitting program."
60
61 self.mf.data.mfin.default_data()
62 self.goto_stage()
63
64
66 "User input of stage number."
67
68 print "\n[ Select the stage for model-free analysis ]\n"
69 print "The stages are:"
70 print " Stage 1 (1): Creation of the files for the model-free calculations for models 1 to 5."
71 print " Stage 2 (2): Model selection and creation of a final run."
72 print " Stage 3 (3): Extraction of the data."
73
74 while 1:
75 input = raw_input('> ')
76 valid_stages = ['1', '2', '3']
77 if input in valid_stages:
78 self.mf.data.stage = input
79 break
80 else:
81 print "Invalid stage number. Choose either 1, 2, or 3."
82 if match('2', self.mf.data.stage):
83 while 1:
84 print "Stage 2 has the following two options for the final run:"
85 print " (a): No optimization of the diffusion tensor."
86 print " (b): Optimization of the diffusion tensor."
87 input = raw_input('> ')
88 valid_stages = ['a', 'b']
89 if input in valid_stages:
90 self.mf.data.stage = self.mf.data.stage + input
91 break
92 else:
93 print "Invalid option, choose either a or b."
94
95 print "The stage chosen is " + self.mf.data.stage + "\n"
96
97
99 "Close the mfin, mfdata, mfmodel, mfpar, and run files, and make the run file executable."
100
101 self.mf.mfin.close()
102 self.mf.mfdata.close()
103 self.mf.mfmodel.close()
104 self.mf.mfpar.close()
105 self.mf.run.close()
106 chmod(dir + '/run', 0777)
107
108
110 """Create the Modelfree input file mfdata.
111
112 This function is run once for each residue. If the flag variable is set to 0, all data
113 for this residue will be excluded. If the exclude_set variable is given, the data flag
114 corresponding to that set will be set to 0 (Used by the cross-validation method).
115 """
116
117 mfdata = self.mf.mfdata
118
119 mfdata.write("\nspin " + self.mf.data.relax_data[0][res][1] + "_" + self.mf.data.relax_data[0][res][0] + "\n")
120 for i in range(self.mf.data.num_ri):
121 mfdata.write('%-7s' % self.mf.data.data_types[i])
122 mfdata.write('%-10s' % self.mf.data.frq_label[self.mf.data.remap_table[i]])
123 mfdata.write('%20s' % self.mf.data.relax_data[i][res][2])
124 mfdata.write('%20s' % self.mf.data.relax_data[i][res][3])
125 mfdata.write(' %-3s\n' % flag)
126
127
129 "Create the Modelfree input file mfin"
130
131 mfin = self.mf.mfin
132
133 mfin.write("optimization tval\n\n")
134 mfin.write("seed 0\n\n")
135 mfin.write("search grid\n\n")
136 mfin.write("diffusion " + self.mf.data.mfin.diff + " " + self.mf.data.mfin.diff_search + "\n\n")
137 mfin.write("algorithm " + self.mf.data.mfin.algorithm + "\n\n")
138 if match('y', self.mf.data.mfin.sims):
139 mfin.write("simulations " + self.mf.data.mfin.sim_type + " " + self.mf.data.mfin.num_sim)
140 mfin.write(" " + self.mf.data.mfin.trim + "\n\n")
141 elif match('n', self.mf.data.mfin.sims):
142 mfin.write("simulations none\n\n")
143 mfin.write("selection " + self.mf.data.mfin.selection + "\n\n")
144 mfin.write("sim_algorithm " + self.mf.data.mfin.algorithm + "\n\n")
145 mfin.write("fields " + `self.mf.data.num_frq`)
146 for frq in range(self.mf.data.num_frq):
147 mfin.write(" " + `self.mf.data.frq[frq]*1e-6`)
148 mfin.write("\n")
149
150 mfin.write('%-7s' % 'tm')
151 mfin.write('%14s' % self.mf.usr_param.tm['val'])
152 mfin.write('%2s' % self.mf.usr_param.tm['flag'])
153 mfin.write('%3s' % self.mf.usr_param.tm['bound'])
154 mfin.write('%5s' % self.mf.usr_param.tm['lower'])
155 mfin.write('%6s' % self.mf.usr_param.tm['upper'])
156 mfin.write('%4s\n' % self.mf.usr_param.tm['steps'])
157
158 mfin.write('%-7s' % 'Dratio')
159 mfin.write('%14s' % self.mf.usr_param.dratio['val'])
160 mfin.write('%2s' % self.mf.usr_param.dratio['flag'])
161 mfin.write('%3s' % self.mf.usr_param.dratio['bound'])
162 mfin.write('%5s' % self.mf.usr_param.dratio['lower'])
163 mfin.write('%6s' % self.mf.usr_param.dratio['upper'])
164 mfin.write('%4s\n' % self.mf.usr_param.dratio['steps'])
165
166 mfin.write('%-7s' % 'Theta')
167 mfin.write('%14s' % self.mf.usr_param.theta['val'])
168 mfin.write('%2s' % self.mf.usr_param.theta['flag'])
169 mfin.write('%3s' % self.mf.usr_param.theta['bound'])
170 mfin.write('%5s' % self.mf.usr_param.theta['lower'])
171 mfin.write('%6s' % self.mf.usr_param.theta['upper'])
172 mfin.write('%4s\n' % self.mf.usr_param.theta['steps'])
173
174 mfin.write('%-7s' % 'Phi')
175 mfin.write('%14s' % self.mf.usr_param.phi['val'])
176 mfin.write('%2s' % self.mf.usr_param.phi['flag'])
177 mfin.write('%3s' % self.mf.usr_param.phi['bound'])
178 mfin.write('%5s' % self.mf.usr_param.phi['lower'])
179 mfin.write('%6s' % self.mf.usr_param.phi['upper'])
180 mfin.write('%4s\n' % self.mf.usr_param.phi['steps'])
181
182
184 "Create the M1 or M2 section of the Modelfree input file mfmodel"
185
186 mfmodel = self.mf.mfmodel
187
188 if match('M1', type):
189 mfmodel.write("\nspin " + self.mf.data.relax_data[0][res][1] + "_" + self.mf.data.relax_data[0][res][0] + "\n")
190 else:
191 mfmodel.write("\n")
192
193
194 mfmodel.write('%-3s' % type)
195 mfmodel.write('%-6s' % 'tloc')
196 mfmodel.write('%-6s' % md['tloc']['start'])
197 mfmodel.write('%-4s' % md['tloc']['flag'])
198 mfmodel.write('%-2s' % md['tloc']['bound'])
199 mfmodel.write('%11s' % md['tloc']['lower'])
200 mfmodel.write('%12s' % md['tloc']['upper'])
201 mfmodel.write(' %-4s\n' % md['tloc']['steps'])
202
203 mfmodel.write('%-3s' % type)
204 mfmodel.write('%-6s' % 'Theta')
205 mfmodel.write('%-6s' % md['theta']['start'])
206 mfmodel.write('%-4s' % md['theta']['flag'])
207 mfmodel.write('%-2s' % md['theta']['bound'])
208 mfmodel.write('%11s' % md['theta']['lower'])
209 mfmodel.write('%12s' % md['theta']['upper'])
210 mfmodel.write(' %-4s\n' % md['theta']['steps'])
211
212 mfmodel.write('%-3s' % type)
213 mfmodel.write('%-6s' % 'Sf2')
214 mfmodel.write('%-6s' % md['sf2']['start'])
215 mfmodel.write('%-4s' % md['sf2']['flag'])
216 mfmodel.write('%-2s' % md['sf2']['bound'])
217 mfmodel.write('%11s' % md['sf2']['lower'])
218 mfmodel.write('%12s' % md['sf2']['upper'])
219 mfmodel.write(' %-4s\n' % md['sf2']['steps'])
220
221 mfmodel.write('%-3s' % type)
222 mfmodel.write('%-6s' % 'Ss2')
223 mfmodel.write('%-6s' % md['ss2']['start'])
224 mfmodel.write('%-4s' % md['ss2']['flag'])
225 mfmodel.write('%-2s' % md['ss2']['bound'])
226 mfmodel.write('%11s' % md['ss2']['lower'])
227 mfmodel.write('%12s' % md['ss2']['upper'])
228 mfmodel.write(' %-4s\n' % md['ss2']['steps'])
229
230 mfmodel.write('%-3s' % type)
231 mfmodel.write('%-6s' % 'te')
232 mfmodel.write('%-6s' % md['te']['start'])
233 mfmodel.write('%-4s' % md['te']['flag'])
234 mfmodel.write('%-2s' % md['te']['bound'])
235 mfmodel.write('%11s' % md['te']['lower'])
236 mfmodel.write('%12s' % md['te']['upper'])
237 mfmodel.write(' %-4s\n' % md['te']['steps'])
238
239 mfmodel.write('%-3s' % type)
240 mfmodel.write('%-6s' % 'Rex')
241 mfmodel.write('%-6s' % md['rex']['start'])
242 mfmodel.write('%-4s' % md['rex']['flag'])
243 mfmodel.write('%-2s' % md['rex']['bound'])
244 mfmodel.write('%11s' % md['rex']['lower'])
245 mfmodel.write('%12s' % md['rex']['upper'])
246 mfmodel.write(' %-4s\n' % md['rex']['steps'])
247
248
250 "Create the Modelfree input file mfpar"
251
252 mfpar = self.mf.mfpar
253
254 mfpar.write("\nspin " + self.mf.data.relax_data[0][res][1] + "_" + self.mf.data.relax_data[0][res][0] + "\n")
255
256 mfpar.write('%-14s' % "constants")
257 mfpar.write('%-6s' % self.mf.data.relax_data[0][res][0])
258 mfpar.write('%-7s' % self.mf.usr_param.const['nucleus'])
259 mfpar.write('%-8s' % self.mf.usr_param.const['gamma'])
260 mfpar.write('%-8s' % `self.mf.usr_param.const['rxh']*1e10`)
261 mfpar.write('%-8s\n' % `self.mf.usr_param.const['csa']*1e6`)
262
263 mfpar.write('%-10s' % "vector")
264 mfpar.write('%-4s' % self.mf.usr_param.vector['atom1'])
265 mfpar.write('%-4s\n' % self.mf.usr_param.vector['atom2'])
266
267
269 "Create the file 'run' to execute the model-free run"
270
271 self.mf.run.write("modelfree4 -i mfin -d mfdata -p mfpar -m mfmodel -o mfout -e out")
272 if self.mf.usr_param.diff == 'axial':
273
274
275 cmd = 'cp ' + self.mf.usr_param.pdb_full + ' ' + dir
276 system(cmd)
277 self.mf.run.write(" -s " + self.mf.usr_param.pdb_file)
278 self.mf.run.write("\n")
279
280
282 "Extract the model-free results."
283
284 for model in self.mf.data.runs:
285 mfout = self.mf.file_ops.read_file(model + '/mfout')
286 mfout_lines = mfout.readlines()
287 mfout.close()
288 print "Extracting model-free data from " + model + "/mfout."
289 num_res = len(self.mf.data.relax_data[0])
290 if match('^m', model):
291 self.mf.data.model = self.mf.star.extract(mfout_lines, num_res, self.mf.usr_param.chi2_lim, self.mf.usr_param.ftest_lim, ftest='n')
292 if match('^f', model):
293 self.mf.data.model = self.mf.star.extract(mfout_lines, num_res, self.mf.usr_param.chi2_lim, self.mf.usr_param.ftest_lim, ftest='y')
294
295
326
327
357
358
360 "Open the mfin, mfdata, mfmodel, mfpar, and run files for writing."
361
362 self.mf.mfin = open(dir + '/mfin', 'w')
363 self.mf.mfdata = open(dir + '/mfdata', 'w')
364 self.mf.mfmodel = open(dir + '/mfmodel', 'w')
365 self.mf.mfpar = open(dir + '/mfpar', 'w')
366 self.mf.run = open(dir + '/run', 'w')
367
368
437
438
440 raise NameError, "Stage 3 not implemented yet.\n"
441
442
444 "Creation of the files for the Modelfree calculations for the models in self.mf.data.runs."
445
446 for model in self.mf.data.runs:
447 if match('^m', model):
448 print "Creating input files for model " + model
449 if self.mf.debug:
450 self.mf.log.write("\n\n<<< Model " + model + " >>>\n\n")
451 elif match('^f', model):
452 print "Creating input files for the F-test " + model
453 if self.mf.debug:
454 self.mf.log.write("\n\n<<< F-test " + model + " >>>\n\n")
455 else:
456 raise NameError, "The run '" + model + "'does not start with an m or f, quitting program!\n\n"
457 self.mf.file_ops.mkdir(dir=model)
458 self.open_mf_files(dir=model)
459 self.set_run_flags(model)
460
461 if self.mf.debug:
462 self.log_params('M1', self.mf.usr_param.md1)
463 self.log_params('M2', self.mf.usr_param.md2)
464
465 if match('^m', model):
466 self.mf.data.mfin.selection = 'none'
467 self.create_mfin()
468 elif match('^f', model):
469 self.mf.data.mfin.selection = 'ftest'
470 self.create_mfin()
471 self.create_run(dir=model)
472 for res in range(len(self.mf.data.relax_data[0])):
473
474 self.create_mfdata(res)
475
476 self.create_mfmodel(res, self.mf.usr_param.md1, type='M1')
477 if match('^f', model):
478 self.create_mfmodel(res, self.mf.usr_param.md2, type='M2')
479
480 self.create_mfpar(res)
481 self.close_mf_files(dir=model)
482
483
515
516
518 "Set the options for the initial runs."
519
520 self.mf.data.mfin.sims = 'n'
521
522
524 "Set the options for the final run."
525
526 self.mf.data.mfin.sims = 'y'
527