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

Source Code for Module generic_fns.bmrb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-2011 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 info import Info_box 
 37  from relax_errors import RelaxError, RelaxFileError, RelaxFileOverwriteError, RelaxNoModuleInstallError, RelaxNoPipeError 
 38  from relax_io import get_file_path, mkdir_nofail 
 39  import specific_fns 
 40  from version import version_full 
 41   
 42   
43 -def display(version='3.1'):
44 """Display the results in the BMRB NMR-STAR format.""" 45 46 # Test if bmrblib is installed. 47 if not dep_check.bmrblib_module: 48 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 49 50 # Test if the current data pipe exists. 51 if not ds.current_pipe: 52 raise RelaxNoPipeError 53 54 # Specific results writing function. 55 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[ds.current_pipe].pipe_type, raise_error=False) 56 57 # Write the results. 58 write_function(sys.stdout, version=version)
59 60
61 -def generate_sequence(N=0, spin_ids=None, spin_nums=None, spin_names=None, res_nums=None, res_names=None, mol_names=None):
62 """Generate the sequence data from the BRMB information. 63 64 @keyword N: The number of spins. 65 @type N: int 66 @keyword spin_ids: The list of spin IDs. 67 @type spin_ids: list of str 68 @keyword spin_nums: The list of spin numbers. 69 @type spin_nums: list of int or None 70 @keyword spin_names: The list of spin names. 71 @type spin_names: list of str or None 72 @keyword res_nums: The list of residue numbers. 73 @type res_nums: list of int or None 74 @keyword res_names: The list of residue names. 75 @type res_names: list of str or None 76 @keyword mol_names: The list of molecule names. 77 @type mol_names: list of str or None 78 """ 79 80 # The blank data. 81 if not spin_nums: 82 spin_nums = [None] * N 83 if not spin_names: 84 spin_names = [None] * N 85 if not res_nums: 86 res_nums = [None] * N 87 if not res_names: 88 res_names = [None] * N 89 if not mol_names: 90 mol_names = [None] * N 91 92 # Generate the spin IDs. 93 spin_ids = [] 94 for i in range(N): 95 spin_ids.append(generate_spin_id(mol_name=mol_names[i], res_num=res_nums[i], spin_name=spin_names[i])) 96 97 # Loop over the spin data. 98 for i in range(N): 99 # The spin already exists. 100 spin = return_spin(spin_ids[i]) 101 if spin: 102 continue 103 104 # The residue container. 105 if not mol_names: 106 res_id = generate_spin_id(res_num=res_nums[i], res_name=res_names[i]) 107 else: 108 res_id = generate_spin_id(mol_name=mol_names[i], res_num=res_nums[i], res_name=res_names[i]) 109 110 # The spin container needs to be named. 111 res_cont = return_residue(res_id) 112 if res_cont and len(res_cont.spin) == 1 and res_cont.spin[0].name == None and res_cont.spin[0].num == None: 113 # Name and number the spin. 114 res_cont.spin[0].name = spin_names[i] 115 res_cont.spin[0].num = spin_nums[i] 116 117 # Jump to the next spin. 118 continue 119 120 # Create the spin. 121 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])
122 123
124 -def list_sample_conditions(star):
125 """Get a listing of all the sample conditions. 126 127 @param star: The NMR-STAR dictionary object. 128 @type star: NMR_STAR instance 129 @return: The list of sample condition names. 130 @rtype: list of str 131 """ 132 133 # Init. 134 sample_conds = [] 135 136 # Get the sample conditions. 137 for data in star.sample_conditions.loop(): 138 # Store the framecode. 139 sample_conds.append(data['sf_framecode']) 140 141 # Return the names. 142 return sample_conds
143 144
145 -def molecule_names(data, N=0):
146 """Generate the molecule names list. 147 148 @param data: An element of data from bmrblib. 149 @type data: dict 150 @return: The list of molecule names. 151 @rtype: list of str 152 """ 153 154 # The molecule index and name. 155 mol_index = [] 156 for i in range(N): 157 if 'entity_ids' in data.keys() and data['entity_ids'] != None and data['entity_ids'][i] != None: 158 mol_index.append(int(data['entity_ids'][i]) -1 ) 159 else: 160 mol_index = [0]*N 161 mol_names = [] 162 for i in range(N): 163 mol_names.append(cdp.mol[mol_index[i]].name) 164 165 # Return the names. 166 return mol_names
167 168
169 -def num_spins(data):
170 """Determine the number of spins in the given data. 171 172 @param data: An element of data from bmrblib. 173 @type data: dict 174 @return: The number of spins. 175 @rtype: int 176 """ 177 178 # The number of spins. 179 N = 0 180 181 # List of keys containing sequence information. 182 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2'] 183 184 # Loop over the keys until a list is found. 185 for key in keys: 186 if key in data.keys() and data[key]: 187 N = len(data[key]) 188 break 189 190 # Return the number. 191 return N
192 193
194 -def read(file=None, directory=None, version=None, sample_conditions=None):
195 """Read the contents of a BMRB NMR-STAR formatted file. 196 197 @keyword file: The name of the BMRB STAR formatted file. 198 @type file: str 199 @keyword directory: The directory where the file is located. 200 @type directory: None or str 201 @keyword version: The BMRB version to force the reading. 202 @type version: None or str 203 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 204 @type sample_conditions: None or str 205 """ 206 207 # Test if bmrblib is installed. 208 if not dep_check.bmrblib_module: 209 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 210 211 # Test if the current data pipe exists. 212 if not ds.current_pipe: 213 raise RelaxNoPipeError 214 215 # Make sure that the data pipe is empty. 216 if not ds[ds.current_pipe].is_empty(): 217 raise RelaxError("The current data pipe is not empty.") 218 219 # Get the full file path. 220 file_path = get_file_path(file_name=file, dir=directory) 221 222 # Fail if the file does not exist. 223 if not access(file_path, F_OK): 224 raise RelaxFileError(file_path) 225 226 # Specific results reading function. 227 read_function = specific_fns.setup.get_specific_fn('bmrb_read', ds[ds.current_pipe].pipe_type) 228 229 # Read the results. 230 read_function(file_path, version=version, sample_conditions=sample_conditions)
231 232
233 -def write(file=None, directory=None, version='3.1', force=False):
234 """Create a BMRB NMR-STAR formatted file.""" 235 236 # Test if bmrblib is installed. 237 if not dep_check.bmrblib_module: 238 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 239 240 # Test if the current data pipe exists. 241 if not ds.current_pipe: 242 raise RelaxNoPipeError 243 244 # The special data pipe name directory. 245 if directory == 'pipe_name': 246 directory = ds.current_pipe 247 248 # Specific results writing function. 249 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[ds.current_pipe].pipe_type) 250 251 # Get the full file path. 252 file_path = get_file_path(file, directory) 253 254 # Fail if the file already exists and the force flag is False. 255 if access(file_path, F_OK) and not force: 256 raise RelaxFileOverwriteError(file_path, 'force flag') 257 258 # Print out. 259 print("Opening the file '%s' for writing." % file_path) 260 261 # Create the directories. 262 mkdir_nofail(directory, verbosity=0) 263 264 # Get the info box. 265 info = Info_box() 266 267 # Add the relax citations. 268 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): 269 # Alias the bib entry. 270 bib = info.bib[key] 271 272 # Add. 273 cdp.exp_info.add_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) 274 275 # Add the relax software package. 276 cdp.exp_info.software_setup(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) 277 278 # Execute the specific BMRB writing code. 279 write_function(file_path, version=version)
280