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

Source Code for Module pipe_control.grace

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2014 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  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   
42 -def axis_setup(data_type=None, norm=True):
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 # Axis specific settings. 54 axes = ['x', 'y'] 55 seq_type = [None, None] 56 axis_labels = [None, None] 57 for i in range(2): 58 # Determine the sequence data type. 59 if data_type[i] == 'res_num': 60 seq_type[i] = 'res' 61 62 # Analysis specific methods for making labels. 63 analysis_spec = False 64 if pipes.cdp_name(): 65 # Flag for making labels. 66 analysis_spec = True 67 68 # The specific analysis API object. 69 api = return_api() 70 71 # Some axis default values for spin data. 72 if data_type[i] == 'res_num': 73 # Residue only data. 74 if seq_type[i] == 'res': 75 # X-axis label. 76 if not axis_labels[i]: 77 axis_labels[i] = "Residue number" 78 79 # Spin only data. 80 if seq_type[i] == 'spin': 81 # X-axis label. 82 if not axis_labels[i]: 83 axis_labels[i] = "Spin number" 84 85 # Mixed data. 86 if seq_type[i] == 'mixed': 87 # X-axis label. 88 if not axis_labels[i]: 89 axis_labels[i] = "Spin identification string" 90 91 # Some axis default values for other data types. 92 else: 93 # Label. 94 if analysis_spec and not axis_labels[i]: 95 # Get the units. 96 units = api.return_units(data_type[i]) 97 98 # Set the label. 99 axis_labels[i] = api.return_grace_string(data_type[i]) 100 101 # Add units. 102 if units: 103 axis_labels[i] = axis_labels[i] + "\\N (" + units + ")" 104 105 # Normalised data. 106 if norm and axes[i] == 'y': 107 axis_labels[i] = axis_labels[i] + " \\N\\q(normalised)\\Q" 108 109 # Return the data. 110 return seq_type, axis_labels
111 112
113 -def determine_seq_type(spin_id=None):
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 # Count the molecules, residues, and spins. 125 num_mol = count_molecules(spin_id) 126 num_res = count_residues(spin_id) 127 num_spin = count_spins(spin_id) 128 129 # Only residues. 130 if num_mol == 1 and num_spin == 1: 131 return 'res' 132 133 # Only spins. 134 if num_mol == 1 and num_res == 1: 135 return 'spin' 136 137 # Mixed. 138 return 'mixed'
139 140
141 -def get_data_types():
142 """Get all of the data types to plot for the current data pipe. 143 144 @return: A list of lists of all the allowable data type descriptions and their values. 145 @rtype: list of list of str 146 """ 147 148 # The specific analysis API object. 149 api = return_api() 150 151 # Return an empty list if the required functions are absent. 152 if not hasattr(api, 'data_names') or not hasattr(api, 'return_data_desc'): 153 return [] 154 155 # The data names, if they exist. 156 names = api.data_names(set='params') 157 158 # Initialise the list and then add the sequence data. 159 data = [] 160 data.append(["Spin sequence", 'spin']) 161 162 # Loop over the parameters. 163 for name in (api.data_names(set='params') + api.data_names(set='generic') + api.data_names(set='min')): 164 # Get the description. 165 try: 166 desc = api.return_data_desc(name) 167 except: 168 return [] 169 170 # No description. 171 if not desc: 172 text = name 173 174 # The text. 175 else: 176 text = "'%s': %s" % (name, desc) 177 178 # Append the description. 179 data.append([text, name]) 180 181 # Return the data. 182 return data
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 # Test the binary file string corresponds to a valid executable. 198 test_binary(grace_exe) 199 200 # File path. 201 file_path = get_file_path(file, dir) 202 203 # Run Grace. 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 # Test if the current pipe exists. 229 pipes.test() 230 231 # Test if the sequence data is loaded. 232 if not exists_mol_res_spin_data(): 233 raise RelaxNoSequenceError 234 235 # Test if the plot_data argument is one of 'value', 'error', or 'sim'. 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 # Test if the simulations exist. 240 if plot_data == 'sim' and not hasattr(cdp, 'sim_number'): 241 raise RelaxNoSimError 242 243 # Open the file for writing. 244 file_path = get_file_path(file, dir) 245 file = open_write_file(file, dir, force) 246 247 # Get the data. 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 # Convert the graph type. 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 # No data, so close the empty file and exit. 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 # Get the axis information. 267 data_type = [x_data_type, y_data_type] 268 seq_type, axis_labels = axis_setup(data_type=data_type, norm=norm) 269 270 # Write the header. 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 # Write the data. 274 write_xy_data(data, file=file, graph_type=graph_type, norm=[norm]) 275 276 # Close the file. 277 file.close() 278 279 # Add the file to the results file list. 280 add_result_file(type='grace', label='Grace', file=file_path)
281