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

Source Code for Module pipe_control.results

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