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