1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module for interfacing with Grace (also known as Xmgrace, Xmgr, and ace)."""
24
25
26 from os import system
27 from warnings import warn
28
29
30 from lib.errors import RelaxError, RelaxNoSequenceError, RelaxNoSimError
31 from lib.io import get_file_path, open_write_file, test_binary
32 from lib.software.grace import write_xy_data, write_xy_header
33 from lib.warnings import RelaxWarning
34 import pipe_control
35 from pipe_control.mol_res_spin import count_molecules, count_residues, count_spins, exists_mol_res_spin_data
36 from pipe_control import pipes
37 from pipe_control.result_files import add_result_file
38 from pipe_control.plotting import assemble_data
39 import specific_analyses
40 from status import Status; status = Status()
41
42
44 """Determine the axis information for relax data store specific data.
45
46 @keyword data_type: The axis data category (in the [X, Y] list format).
47 @type data_type: list of str
48 @keyword norm: The normalisation flag which if set to True will cause all graphs to be normalised to a starting value of 1.
49 @type norm: bool
50 @return: The axis information. This includes the sequence type, the list of lower bounds, the list of upper bounds, and the axis labels.
51 @rtype: list of str or None, list of int or None, list of int or None, list of str or None
52 """
53
54
55 axes = ['x', 'y']
56 seq_type = [None, None]
57 axis_labels = [None, None]
58 for i in range(2):
59
60 if data_type[i] == 'res_num':
61 seq_type[i] = 'res'
62
63
64 analysis_spec = False
65 if pipes.cdp_name():
66
67 analysis_spec = True
68
69
70 return_units = specific_analyses.setup.get_specific_fn('return_units', pipes.get_type())
71 return_grace_string = specific_analyses.setup.get_specific_fn('return_grace_string', pipes.get_type())
72
73
74 if data_type[i] and data_type[i] != 'res_num' and pipe_control.minimise.return_data_name(data_type[i]):
75 return_units = pipe_control.minimise.return_units
76 return_grace_string = pipe_control.minimise.return_grace_string
77
78
79 if data_type[i] == 'res_num':
80
81 if seq_type[i] == 'res':
82
83 if not axis_labels[i]:
84 axis_labels[i] = "Residue number"
85
86
87 if seq_type[i] == 'spin':
88
89 if not axis_labels[i]:
90 axis_labels[i] = "Spin number"
91
92
93 if seq_type[i] == 'mixed':
94
95 if not axis_labels[i]:
96 axis_labels[i] = "Spin identification string"
97
98
99 else:
100
101 if analysis_spec and not axis_labels[i]:
102
103 units = return_units(data_type[i])
104
105
106 axis_labels[i] = return_grace_string(data_type[i])
107
108
109 if units:
110 axis_labels[i] = axis_labels[i] + "\\N (" + units + ")"
111
112
113 if norm and axes[i] == 'y':
114 axis_labels[i] = axis_labels[i] + " \\N\\q(normalised)\\Q"
115
116
117 return seq_type, axis_labels
118
119
121 """Determine the spin sequence data type.
122
123 The purpose is to identify systems whereby only spins or only residues exist.
124
125 @keyword spin_id: The spin identification string.
126 @type spin_id: str
127 @return: The spin sequence data type. This can be one of 'spin', 'res,' or 'mixed'.
128 @rtype: str
129 """
130
131
132 num_mol = count_molecules(spin_id)
133 num_res = count_residues(spin_id)
134 num_spin = count_spins(spin_id)
135
136
137 if num_mol == 1 and num_spin == 1:
138 return 'res'
139
140
141 if num_mol == 1 and num_res == 1:
142 return 'spin'
143
144
145 return 'mixed'
146
147
190
191
192 -def view(file=None, dir=None, grace_exe='xmgrace'):
193 """Execute Grace.
194
195 @keyword file: The name of the file to open in Grace.
196 @type file: str
197 @keyword dir: The optional directory containing the file.
198 @type dir: str
199 @keyword grace_exe: The name of the Grace executable file. This should be located within the
200 system path.
201 @type grace_exe: str
202 """
203
204
205 test_binary(grace_exe)
206
207
208 file_path = get_file_path(file, dir)
209
210
211 system(grace_exe + " " + file_path + " &")
212
213
214 -def write(x_data_type='res_num', y_data_type=None, spin_id=None, plot_data='value', file=None, dir=None, force=False, norm=True):
215 """Writing data to a file.
216
217 @keyword x_data_type: The category of the X-axis data.
218 @type x_data_type: str
219 @keyword y_data_type: The category of the Y-axis data.
220 @type y_data_type: str
221 @keyword spin_id: The spin identification string.
222 @type spin_id: str
223 @keyword plot_data: The type of the plotted data, one of 'value', 'error', or 'sim'.
224 @type plot_data: str
225 @keyword file: The name of the Grace file to create.
226 @type file: str
227 @keyword dir: The optional directory to place the file into.
228 @type dir: str
229 @param force: Boolean argument which if True causes the file to be overwritten if it already exists.
230 @type force: bool
231 @keyword norm: The normalisation flag which if set to True will cause all graphs to be normalised to a starting value of 1.
232 @type norm: bool
233 """
234
235
236 pipes.test()
237
238
239 if not exists_mol_res_spin_data():
240 raise RelaxNoSequenceError
241
242
243 if plot_data not in ['value', 'error', 'sim']:
244 raise RelaxError("The plot data argument " + repr(plot_data) + " must be set to either 'value', 'error', 'sim'.")
245
246
247 if plot_data == 'sim' and not hasattr(cdp, 'sim_number'):
248 raise RelaxNoSimError
249
250
251 file_path = get_file_path(file, dir)
252 file = open_write_file(file, dir, force)
253
254
255 data, set_names, graph_type = assemble_data(spin_id, x_data_name=x_data_type, y_data_name=y_data_type, plot_data=plot_data)
256
257
258 if graph_type == 'X,Y':
259 graph_type = 'xy'
260 elif graph_type == 'X,Y,dX':
261 graph_type = 'xydx'
262 elif graph_type == 'X,Y,dY':
263 graph_type = 'xydy'
264 elif graph_type == 'X,Y,dX,dY':
265 graph_type = 'xydxdy'
266
267
268 if not len(data) or not len(data[0]) or not len(data[0][0]):
269 warn(RelaxWarning("No data could be found, creating an empty file."))
270 file.close()
271 return
272
273
274 data_type = [x_data_type, y_data_type]
275 seq_type, axis_labels = axis_setup(data_type=data_type, norm=norm)
276
277
278 write_xy_header(file=file, data_type=data_type, seq_type=seq_type, sets=[len(data[0])], set_names=[set_names], axis_labels=[axis_labels], norm=[norm])
279
280
281 write_xy_data(data, file=file, graph_type=graph_type, norm=[norm])
282
283
284 file.close()
285
286
287 add_result_file(type='grace', label='Grace', file=file_path)
288