Package pipe_control :: Module grace
[hide private]
[frames] | no frames]

Source Code for Module pipe_control.grace

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Module for interfacing with Grace (also known as Xmgrace, Xmgr, and ace).""" 
 24   
 25  # Python module imports. 
 26  from os import system 
 27  from warnings import warn 
 28   
 29  # relax module imports. 
 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   
43 -def axis_setup(data_type=None, norm=True):
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 # Axis specific settings. 55 axes = ['x', 'y'] 56 seq_type = [None, None] 57 axis_labels = [None, None] 58 for i in range(2): 59 # Determine the sequence data type. 60 if data_type[i] == 'res_num': 61 seq_type[i] = 'res' 62 63 # Analysis specific methods for making labels. 64 analysis_spec = False 65 if pipes.cdp_name(): 66 # Flag for making labels. 67 analysis_spec = True 68 69 # Specific value and error, conversion factor, and units returning functions. 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 # Test if the axis data type is a minimisation statistic. 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 # Some axis default values for spin data. 79 if data_type[i] == 'res_num': 80 # Residue only data. 81 if seq_type[i] == 'res': 82 # X-axis label. 83 if not axis_labels[i]: 84 axis_labels[i] = "Residue number" 85 86 # Spin only data. 87 if seq_type[i] == 'spin': 88 # X-axis label. 89 if not axis_labels[i]: 90 axis_labels[i] = "Spin number" 91 92 # Mixed data. 93 if seq_type[i] == 'mixed': 94 # X-axis label. 95 if not axis_labels[i]: 96 axis_labels[i] = "Spin identification string" 97 98 # Some axis default values for other data types. 99 else: 100 # Label. 101 if analysis_spec and not axis_labels[i]: 102 # Get the units. 103 units = return_units(data_type[i]) 104 105 # Set the label. 106 axis_labels[i] = return_grace_string(data_type[i]) 107 108 # Add units. 109 if units: 110 axis_labels[i] = axis_labels[i] + "\\N (" + units + ")" 111 112 # Normalised data. 113 if norm and axes[i] == 'y': 114 axis_labels[i] = axis_labels[i] + " \\N\\q(normalised)\\Q" 115 116 # Return the data. 117 return seq_type, axis_labels
118 119
120 -def determine_seq_type(spin_id=None):
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 # Count the molecules, residues, and spins. 132 num_mol = count_molecules(spin_id) 133 num_res = count_residues(spin_id) 134 num_spin = count_spins(spin_id) 135 136 # Only residues. 137 if num_mol == 1 and num_spin == 1: 138 return 'res' 139 140 # Only spins. 141 if num_mol == 1 and num_res == 1: 142 return 'spin' 143 144 # Mixed. 145 return 'mixed'
146 147
148 -def get_data_types():
149 """Get all of the data types to plot for the current data pipe. 150 151 @return: A list of lists of all the allowable data type descriptions and their values. 152 @rtype: list of list of str 153 """ 154 155 # Get the specific functions (return an empty list if a RelaxError occurs). 156 try: 157 data_names = specific_analyses.setup.get_specific_fn('data_names', cdp.pipe_type, raise_error=False) 158 return_data_desc = specific_analyses.setup.get_specific_fn('return_data_desc', cdp.pipe_type, raise_error=False) 159 except: 160 return [] 161 162 # The data names, if they exist. 163 names = data_names(set='params') 164 165 # Initialise the list and then add the sequence data. 166 data = [] 167 data.append(["Spin sequence", 'spin']) 168 169 # Loop over the parameters. 170 for name in (data_names(set='params') + data_names(set='generic')): 171 # Get the description. 172 try: 173 desc = return_data_desc(name) 174 except: 175 return [] 176 177 # No description. 178 if not desc: 179 text = name 180 181 # The text. 182 else: 183 text = "'%s': %s" % (name, desc) 184 185 # Append the description. 186 data.append([text, name]) 187 188 # Return the data. 189 return data
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 # Test the binary file string corresponds to a valid executable. 205 test_binary(grace_exe) 206 207 # File path. 208 file_path = get_file_path(file, dir) 209 210 # Run Grace. 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 # Test if the current pipe exists. 236 pipes.test() 237 238 # Test if the sequence data is loaded. 239 if not exists_mol_res_spin_data(): 240 raise RelaxNoSequenceError 241 242 # Test if the plot_data argument is one of 'value', 'error', or 'sim'. 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 # Test if the simulations exist. 247 if plot_data == 'sim' and not hasattr(cdp, 'sim_number'): 248 raise RelaxNoSimError 249 250 # Open the file for writing. 251 file_path = get_file_path(file, dir) 252 file = open_write_file(file, dir, force) 253 254 # Get the data. 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 # Convert the graph type. 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 # No data, so close the empty file and exit. 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 # Get the axis information. 274 data_type = [x_data_type, y_data_type] 275 seq_type, axis_labels = axis_setup(data_type=data_type, norm=norm) 276 277 # Write the header. 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 # Write the data. 281 write_xy_data(data, file=file, graph_type=graph_type, norm=[norm]) 282 283 # Close the file. 284 file.close() 285 286 # Add the file to the results file list. 287 add_result_file(type='grace', label='Grace', file=file_path)
288