Package generic_fns :: Module bmrb
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.bmrb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Module containing functions for BMRB support.""" 
 25   
 26  # Python module imports. 
 27  from os import F_OK, access 
 28  import sys 
 29   
 30  # relax module imports. 
 31  from data import Relax_data_store; ds = Relax_data_store() 
 32  from data.exp_info import ExpInfo 
 33  import dep_check 
 34  from generic_fns import exp_info 
 35  from generic_fns.mol_res_spin import create_spin, generate_spin_id, return_residue, return_spin 
 36  from generic_fns.pipes import cdp_name 
 37  from info import Info_box 
 38  from relax_errors import RelaxError, RelaxFileError, RelaxFileOverwriteError, RelaxNoModuleInstallError, RelaxNoPipeError 
 39  from relax_io import get_file_path, mkdir_nofail 
 40  import specific_fns 
 41  from status import Status; status = Status() 
 42  from version import version_full 
 43   
 44   
45 -def display(version='3.1'):
46 """Display the results in the BMRB NMR-STAR format. 47 48 @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. 49 @type version: str 50 """ 51 52 # Call the write() function with stdout. 53 write(file=sys.stdout, version=version)
54 55
56 -def generate_sequence(N=0, spin_ids=None, spin_nums=None, spin_names=None, res_nums=None, res_names=None, mol_names=None):
57 """Generate the sequence data from the BRMB information. 58 59 @keyword N: The number of spins. 60 @type N: int 61 @keyword spin_ids: The list of spin IDs. 62 @type spin_ids: list of str 63 @keyword spin_nums: The list of spin numbers. 64 @type spin_nums: list of int or None 65 @keyword spin_names: The list of spin names. 66 @type spin_names: list of str or None 67 @keyword res_nums: The list of residue numbers. 68 @type res_nums: list of int or None 69 @keyword res_names: The list of residue names. 70 @type res_names: list of str or None 71 @keyword mol_names: The list of molecule names. 72 @type mol_names: list of str or None 73 """ 74 75 # The blank data. 76 if not spin_nums: 77 spin_nums = [None] * N 78 if not spin_names: 79 spin_names = [None] * N 80 if not res_nums: 81 res_nums = [None] * N 82 if not res_names: 83 res_names = [None] * N 84 if not mol_names: 85 mol_names = [None] * N 86 87 # Generate the spin IDs. 88 spin_ids = [] 89 for i in range(N): 90 spin_ids.append(generate_spin_id(mol_name=mol_names[i], res_num=res_nums[i], spin_name=spin_names[i])) 91 92 # Loop over the spin data. 93 for i in range(N): 94 # The spin already exists. 95 spin = return_spin(spin_ids[i]) 96 if spin: 97 continue 98 99 # The residue container. 100 if not mol_names: 101 res_id = generate_spin_id(res_num=res_nums[i], res_name=res_names[i]) 102 else: 103 res_id = generate_spin_id(mol_name=mol_names[i], res_num=res_nums[i], res_name=res_names[i]) 104 105 # The spin container needs to be named. 106 res_cont = return_residue(res_id) 107 if res_cont and len(res_cont.spin) == 1 and res_cont.spin[0].name == None and res_cont.spin[0].num == None: 108 # Name and number the spin. 109 res_cont.spin[0].name = spin_names[i] 110 res_cont.spin[0].num = spin_nums[i] 111 112 # Jump to the next spin. 113 continue 114 115 # Create the spin. 116 create_spin(spin_num=spin_nums[i], spin_name=spin_names[i], res_num=res_nums[i], res_name=res_names[i], mol_name=mol_names[i])
117 118
119 -def list_sample_conditions(star):
120 """Get a listing of all the sample conditions. 121 122 @param star: The NMR-STAR dictionary object. 123 @type star: NMR_STAR instance 124 @return: The list of sample condition names. 125 @rtype: list of str 126 """ 127 128 # Init. 129 sample_conds = [] 130 131 # Get the sample conditions. 132 for data in star.sample_conditions.loop(): 133 # Store the framecode. 134 sample_conds.append(data['sf_framecode']) 135 136 # Return the names. 137 return sample_conds
138 139
140 -def molecule_names(data, N=0):
141 """Generate the molecule names list. 142 143 @param data: An element of data from bmrblib. 144 @type data: dict 145 @return: The list of molecule names. 146 @rtype: list of str 147 """ 148 149 # The molecule index and name. 150 mol_index = [] 151 for i in range(N): 152 if 'entity_ids' in data.keys() and data['entity_ids'] != None and data['entity_ids'][i] != None: 153 mol_index.append(int(data['entity_ids'][i]) -1 ) 154 else: 155 mol_index = [0]*N 156 mol_names = [] 157 for i in range(N): 158 mol_names.append(cdp.mol[mol_index[i]].name) 159 160 # Return the names. 161 return mol_names
162 163
164 -def num_spins(data):
165 """Determine the number of spins in the given data. 166 167 @param data: An element of data from bmrblib. 168 @type data: dict 169 @return: The number of spins. 170 @rtype: int 171 """ 172 173 # The number of spins. 174 N = 0 175 176 # List of keys containing sequence information. 177 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2'] 178 179 # Loop over the keys until a list is found. 180 for key in keys: 181 if key in data.keys() and data[key]: 182 N = len(data[key]) 183 break 184 185 # Return the number. 186 return N
187 188
189 -def read(file=None, dir=None, version=None, sample_conditions=None):
190 """Read the contents of a BMRB NMR-STAR formatted file. 191 192 @keyword file: The name of the BMRB STAR formatted file. 193 @type file: str 194 @keyword dir: The directory where the file is located. 195 @type dir: None or str 196 @keyword version: The BMRB version to force the reading. 197 @type version: None or str 198 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 199 @type sample_conditions: None or str 200 """ 201 202 # Test if bmrblib is installed. 203 if not dep_check.bmrblib_module: 204 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 205 206 # Test if the current data pipe exists. 207 pipe_name = cdp_name() 208 if not pipe_name: 209 raise RelaxNoPipeError 210 211 # Make sure that the data pipe is empty. 212 if not ds[pipe_name].is_empty(): 213 raise RelaxError("The current data pipe is not empty.") 214 215 # Get the full file path. 216 file_path = get_file_path(file_name=file, dir=dir) 217 218 # Fail if the file does not exist. 219 if not access(file_path, F_OK): 220 raise RelaxFileError(file_path) 221 222 # Specific results reading function. 223 read_function = specific_fns.setup.get_specific_fn('bmrb_read', ds[pipe_name].pipe_type) 224 225 # Read the results. 226 read_function(file_path, version=version, sample_conditions=sample_conditions)
227 228
229 -def write(file=None, dir=None, version='3.1', force=False):
230 """Create a BMRB NMR-STAR formatted file. 231 232 @keyword file: The name of the file to create or a file object. 233 @type file: str or file object 234 @keyword dir: The optional directory to place the file into. If set to 'pipe_name', then it will be placed in a directory with the same name as the current data pipe. 235 @type dir: str or None 236 @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. 237 @type version: str 238 @keyword force: A flag which if True will allow a currently existing file to be overwritten. 239 @type force: bool 240 """ 241 242 # Test if bmrblib is installed. 243 if not dep_check.bmrblib_module: 244 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 245 246 # Test if the current data pipe exists. 247 pipe_name = cdp_name() 248 if not pipe_name: 249 raise RelaxNoPipeError 250 251 # Check the file name. 252 if file == None: 253 raise RelaxError("The file name must be specified.") 254 255 # A file object. 256 if isinstance(file, str): 257 # The special data pipe name directory. 258 if dir == 'pipe_name': 259 dir = pipe_name 260 261 # Get the full file path. 262 file = get_file_path(file, dir) 263 264 # Fail if the file already exists and the force flag is False. 265 if access(file, F_OK) and not force: 266 raise RelaxFileOverwriteError(file, 'force flag') 267 268 # Print out. 269 print("Opening the file '%s' for writing." % file) 270 271 # Create the directories. 272 mkdir_nofail(dir, verbosity=0) 273 274 # Specific results writing function. 275 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[pipe_name].pipe_type) 276 277 # Get the info box. 278 info = Info_box() 279 280 # Add the relax citations. 281 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): 282 # Alias the bib entry. 283 bib = info.bib[key] 284 285 # Add. 286 exp_info.citation(cite_id=id, authors=bib.author2, doi=bib.doi, pubmed_id=bib.pubmed_id, full_citation=bib.cite_short(doi=False, url=False), title=bib.title, status=bib.status, type=bib.type, journal_abbrev=bib.journal, journal_full=bib.journal_full, volume=bib.volume, issue=bib.number, page_first=bib.page_first, page_last=bib.page_last, year=bib.year) 287 288 # Add the relax software package. 289 exp_info.software(name=exp_info.SOFTWARE['relax'].name, version=version_full(), vendor_name=exp_info.SOFTWARE['relax'].authors, url=exp_info.SOFTWARE['relax'].url, cite_ids=['relax_ref1', 'relax_ref2'], tasks=exp_info.SOFTWARE['relax'].tasks) 290 291 # Execute the specific BMRB writing code. 292 write_function(file, version=version) 293 294 # Add the file to the results file list. 295 if isinstance(file, str): 296 if not hasattr(cdp, 'result_files'): 297 cdp.result_files = [] 298 cdp.result_files.append(['text', 'BMRB', file]) 299 status.observers.result_file.notify()
300