Package lib :: Package software :: Module bruker_dc
[hide private]
[frames] | no frames]

Source Code for Module lib.software.bruker_dc

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2011-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 the reading of Bruker Dynamics Centre (DC) files.""" 
 24   
 25  # Python module imports. 
 26  from re import search, split 
 27   
 28  # relax module imports. 
 29  from lib.errors import RelaxError 
 30  from lib.io import open_read_file 
 31   
 32   
33 -def convert_relax_data(data):
34 """Determine the relaxation data from the given DC data. 35 36 @param data: The list of Tx, Tx error, and scaling factor for a given residue from the DC file. 37 @type data: list of str 38 """ 39 40 # Convert the value from Tx to Rx. 41 rx = 1.0 / float(data[0]) 42 43 # Remove the scaling. 44 rx_err = float(data[1]) / float(data[2]) 45 46 # Convert the Tx error to an Rx error. 47 rx_err = rx**2 * rx_err 48 49 # Return the value and error. 50 return rx, rx_err
51 52
53 -def get_res_num(data):
54 """Determine the residue number from the given DC data. 55 56 @param data: The list of residue info, split by whitespace, from the DC file. 57 @type data: list of str 58 """ 59 60 # Init. 61 res_num = None 62 63 # Split the data. 64 row = split('([0-9]+)', data) 65 66 # Loop over the new list. 67 for j in range(len(row)): 68 try: 69 res_num = int(row[j]) 70 except ValueError: 71 pass 72 73 # Return the value. 74 return ":%s" % res_num
75 76
77 -def parse_file(file=None, dir=None):
78 """Parse the DC data file and return the extracted data. 79 80 @keyword file: The name of the file to open. 81 @type file: str 82 @keyword dir: The directory containing the file (defaults to the current directory if None). 83 @type dir: str or None 84 @return: The data from the Bruker Dynamics Centre file. This includes the values, the errors, the residue numbers, the integration type, the field strength frequency, the relaxation data type, the isotope, the spin name, and the BDC version number. 85 @rtype: list of float, list of float, list of int, str, float, str, str, str, str 86 """ 87 88 # Extract the data from the file. 89 file_handle = open_read_file(file, dir) 90 lines = file_handle.readlines() 91 file_handle.close() 92 93 # Init. 94 values = [] 95 errors = [] 96 res_nums = [] 97 int_type = None 98 isotope = None 99 spin_name = None 100 101 # Loop over the data. 102 in_ri_data = False 103 for line in lines: 104 # Split the line. 105 row = split("\t", line) 106 107 # Strip the rubbish. 108 for j in range(len(row)): 109 row[j] = row[j].strip() 110 111 # Empty line. 112 if len(row) == 0: 113 continue 114 115 # The DC version. 116 if row[0] == 'generated by:': 117 version = row[1] 118 119 # Check for bad errors. 120 if row[0] == 'Systematic error estimation of data:': 121 # Badness. 122 if row[1] == 'worst case per peak scenario': 123 raise RelaxError("The errors estimation method \"worst case per peak scenario\" is not suitable for model-free analysis. Please go back to the DC and switch to \"average variance calculation\".") 124 125 # The data type. 126 if row[0] == 'Project:': 127 if search('T1', row[1]): 128 ri_type = 'R1' 129 elif search('T2', row[1]): 130 ri_type = 'R2' 131 elif search('NOE', row[1]): 132 ri_type = 'NOE' 133 134 # Get the frequency, converting to Hz. 135 elif row[0] == 'Proton frequency[MHz]:': 136 frq = float(row[1]) * 1e6 137 138 # Inside the relaxation data section. 139 elif row[0] == 'SECTION:' and row[1] == 'results': 140 in_ri_data = True 141 142 # The relaxation data. 143 elif in_ri_data: 144 # Skip the header. 145 if row[0] == 'Peak name': 146 # Catch old PDC files (to fix https://web.archive.org/web/https://gna.org/bugs/?20152). 147 pdc_file = False 148 if ri_type == 'R1' and not search('R1', line): 149 pdc_file = True 150 elif ri_type == 'R2' and not search('R2', line): 151 pdc_file = True 152 if pdc_file: 153 raise RelaxError("The old Protein Dynamics Center (PDC) files are not supported") 154 155 # Skip. 156 continue 157 158 # The residue info. 159 res_nums.append(get_res_num(row[0])) 160 161 # Get the relaxation data. 162 if ri_type != 'NOE': 163 #rx, rx_err = convert_relax_data(row[3:]) 164 rx = float(row[-2]) 165 rx_err = float(row[-1]) 166 else: 167 rx = float(row[-3]) 168 rx_err = float(row[-2]) 169 170 # Append the data. 171 values.append(rx) 172 errors.append(rx_err) 173 174 # The temperature. 175 elif row[0] == 'Temperature (K):': 176 # Set the value (not implemented yet). 177 pass 178 179 # The labelling. 180 elif row[0] == 'Labelling:': 181 # Store the isotope for later use. 182 isotope = row[1] 183 184 # Name the spins. 185 spin_name = split('([A-Z]+)', row[1])[1] 186 187 # The integration method. 188 elif row[0] == 'Used integrals:': 189 # Peak heights. 190 if row[1] == 'peak intensities': 191 int_type = 'height' 192 193 # Peak volumes: 194 if row[1] == 'area integral': 195 int_type = 'volume' 196 197 # Return the data. 198 return values, errors, res_nums, int_type, frq, ri_type, spin_name, isotope, version
199