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

Source Code for Module pipe_control.results

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2002-2004,2007-2008 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 reading/writing/displaying the results in a data pipe.""" 
 24   
 25  # Python module imports. 
 26  from os.path import dirname 
 27  from re import search 
 28  import sys 
 29   
 30  # relax module imports. 
 31  from data_store import Relax_data_store; ds = Relax_data_store() 
 32  from lib.errors import RelaxError, RelaxFileEmptyError 
 33  from lib.io import extract_data, get_file_path, open_read_file, open_write_file, strip 
 34  from pipe_control import interatomic, mol_res_spin, pipes 
 35  from pipe_control.pipes import check_pipe 
 36  from specific_analyses.model_free.back_compat import read_columnar_results 
 37   
 38   
39 -def determine_format(file):
40 """Determine the format of the results file. 41 42 @keyword file: The file object representing the results file. 43 @type file: file object 44 @return: The results file format. This can be 'xml' or 'columnar'. 45 @rtype: str or None 46 """ 47 48 # Header line. 49 header = file.readline() 50 header = header[:-1] # Strip the trailing newline. 51 if hasattr(header, 'decode'): # Python 3 byte type conversion. 52 header = header.decode() 53 54 # Be nice and go back to the start of the file. 55 file.seek(0) 56 57 # XML. 58 if search(r"<\?xml", header): 59 return 'xml' 60 61 # Columnar. 62 if header.split()[0:3] == ['Num', 'Name', 'Selected']: 63 return 'columnar'
64 65
66 -def display():
67 """Displaying the results/contents of the current data pipe.""" 68 69 # Test if the current data pipe exists. 70 check_pipe() 71 72 # Write the results. 73 ds.to_xml(sys.stdout)
74 75
76 -def read(file='results', dir=None):
77 """Function for reading the data out of a file.""" 78 79 # Test if the current data pipe exists. 80 check_pipe() 81 82 # Make sure that the data pipe is empty. 83 if not cdp.is_empty(): 84 raise RelaxError("The current data pipe is not empty.") 85 86 # Get the full file path, for later use. 87 file_path = get_file_path(file_name=file, dir=dir) 88 89 # Open the file. 90 file = open_read_file(file_name=file_path) 91 92 # Determine the format of the file. 93 format = determine_format(file) 94 95 # XML results. 96 if format == 'xml': 97 ds.from_xml(file, dir=dirname(file_path), pipe_to=pipes.cdp_name()) 98 99 # Columnar results (for backwards compatibility with ancient relax results model-free files). 100 elif format == 'columnar': 101 # Extract the data from the file. 102 file_data = extract_data(file=file) 103 104 # Strip data. 105 file_data = strip(file_data) 106 107 # Do nothing if the file does not exist. 108 if not file_data: 109 raise RelaxFileEmptyError 110 111 # Read the results. 112 read_columnar_results(file_data) 113 114 # Unknown results file. 115 else: 116 raise RelaxError("The format of the results file " + repr(file_path) + " cannot be determined.") 117 118 # Update all of the required metadata structures. 119 mol_res_spin.metadata_update() 120 interatomic.metadata_update()
121 122
123 -def write(file="results", dir=None, force=False, compress_type=1, verbosity=1):
124 """Create the results file.""" 125 126 # Test if the current data pipe exists. 127 check_pipe() 128 129 # The special data pipe name directory. 130 if dir == 'pipe_name': 131 dir = pipes.cdp_name() 132 133 # Open the file for writing. 134 results_file = open_write_file(file_name=file, dir=dir, force=force, compress_type=compress_type, verbosity=verbosity) 135 136 # Write the results. 137 ds.to_xml(results_file, pipes=pipes.cdp_name()) 138 139 # Close the results file. 140 results_file.close()
141