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

Source Code for Module generic_fns.bmrb

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2008-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 containing functions for BMRB support.""" 
 24   
 25  # Python module imports. 
 26  from os import F_OK, access 
 27  import sys 
 28   
 29  # relax module imports. 
 30  from data import Relax_data_store; ds = Relax_data_store() 
 31  from data.exp_info import ExpInfo 
 32  import dep_check 
 33  from generic_fns import exp_info 
 34  from generic_fns.mol_res_spin import create_spin, generate_spin_id, metadata_cleanup, return_residue, return_spin, set_spin_element, set_spin_isotope 
 35  from generic_fns.pipes import cdp_name 
 36  from generic_fns.result_files import add_result_file 
 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, isotopes=None, elements=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 @keyword isotopes: The optional list of isotope types. 74 @type isotopes: list of str or None 75 @keyword elements: The optional list of element types. 76 @type elements: list of str or None 77 """ 78 79 # The blank data. 80 if not spin_nums: 81 spin_nums = [None] * N 82 if not spin_names: 83 spin_names = [None] * N 84 if not res_nums: 85 res_nums = [None] * N 86 if not res_names: 87 res_names = [None] * N 88 if not mol_names: 89 mol_names = [None] * N 90 91 # Generate the spin IDs. 92 spin_ids = [] 93 for i in range(N): 94 spin_ids.append(generate_spin_id(mol_name=mol_names[i], res_num=res_nums[i], spin_name=spin_names[i])) 95 96 # Loop over the spin data. 97 for i in range(N): 98 # The spin already exists. 99 spin = return_spin(spin_ids[i]) 100 if spin: 101 continue 102 103 # Create the spin. 104 spin = 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]) 105 106 # Set the spin isotope and element. 107 spin_id = spin._spin_ids[0] 108 if elements: 109 set_spin_element(spin_id=spin_id, element=elements[i], force=True) 110 if isotopes and elements: 111 isotope = "%s%s" % (isotopes[i], elements[i]) 112 set_spin_isotope(spin_id=spin_id, isotope=isotope, force=True) 113 114 # Clean up the spin metadata. 115 metadata_cleanup()
116 117
118 -def list_sample_conditions(star):
119 """Get a listing of all the sample conditions. 120 121 @param star: The NMR-STAR dictionary object. 122 @type star: NMR_STAR instance 123 @return: The list of sample condition names. 124 @rtype: list of str 125 """ 126 127 # Init. 128 sample_conds = [] 129 130 # Get the sample conditions. 131 for data in star.sample_conditions.loop(): 132 # Store the framecode. 133 sample_conds.append(data['sf_framecode']) 134 135 # Return the names. 136 return sample_conds
137 138
139 -def molecule_names(data, N=0):
140 """Generate the molecule names list. 141 142 @param data: An element of data from bmrblib. 143 @type data: dict 144 @return: The list of molecule names. 145 @rtype: list of str 146 """ 147 148 # The molecule index and name. 149 mol_index = [] 150 for i in range(N): 151 if 'entity_ids' in data.keys() and data['entity_ids'] != None and data['entity_ids'][i] != None: 152 mol_index.append(int(data['entity_ids'][i]) -1 ) 153 else: 154 mol_index = [0]*N 155 mol_names = [] 156 for i in range(N): 157 mol_names.append(cdp.mol[mol_index[i]].name) 158 159 # Return the names. 160 return mol_names
161 162
163 -def num_spins(data):
164 """Determine the number of spins in the given data. 165 166 @param data: An element of data from bmrblib. 167 @type data: dict 168 @return: The number of spins. 169 @rtype: int 170 """ 171 172 # The number of spins. 173 N = 0 174 175 # List of keys containing sequence information. 176 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2'] 177 178 # Loop over the keys until a list is found. 179 for key in keys: 180 if key in data.keys() and data[key]: 181 N = len(data[key]) 182 break 183 184 # Return the number. 185 return N
186 187
188 -def read(file=None, dir=None, version=None, sample_conditions=None):
189 """Read the contents of a BMRB NMR-STAR formatted file. 190 191 @keyword file: The name of the BMRB STAR formatted file. 192 @type file: str 193 @keyword dir: The directory where the file is located. 194 @type dir: None or str 195 @keyword version: The BMRB version to force the reading. 196 @type version: None or str 197 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 198 @type sample_conditions: None or str 199 """ 200 201 # Test if bmrblib is installed. 202 if not dep_check.bmrblib_module: 203 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 204 205 # Test if the current data pipe exists. 206 pipe_name = cdp_name() 207 if not pipe_name: 208 raise RelaxNoPipeError 209 210 # Make sure that the data pipe is empty. 211 if not ds[pipe_name].is_empty(): 212 raise RelaxError("The current data pipe is not empty.") 213 214 # Get the full file path. 215 file_path = get_file_path(file_name=file, dir=dir) 216 217 # Fail if the file does not exist. 218 if not access(file_path, F_OK): 219 raise RelaxFileError(file_path) 220 221 # Specific results reading function. 222 read_function = specific_fns.setup.get_specific_fn('bmrb_read', ds[pipe_name].pipe_type) 223 224 # Read the results. 225 read_function(file_path, version=version, sample_conditions=sample_conditions)
226 227
228 -def write(file=None, dir=None, version='3.1', force=False):
229 """Create a BMRB NMR-STAR formatted file. 230 231 @keyword file: The name of the file to create or a file object. 232 @type file: str or file object 233 @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. 234 @type dir: str or None 235 @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. 236 @type version: str 237 @keyword force: A flag which if True will allow a currently existing file to be overwritten. 238 @type force: bool 239 """ 240 241 # Test if bmrblib is installed. 242 if not dep_check.bmrblib_module: 243 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 244 245 # Test if the current data pipe exists. 246 pipe_name = cdp_name() 247 if not pipe_name: 248 raise RelaxNoPipeError 249 250 # Check the file name. 251 if file == None: 252 raise RelaxError("The file name must be specified.") 253 254 # A file object. 255 if isinstance(file, str): 256 # The special data pipe name directory. 257 if dir == 'pipe_name': 258 dir = pipe_name 259 260 # Get the full file path. 261 file = get_file_path(file, dir) 262 263 # Fail if the file already exists and the force flag is False. 264 if access(file, F_OK) and not force: 265 raise RelaxFileOverwriteError(file, 'force flag') 266 267 # Print out. 268 print("Opening the file '%s' for writing." % file) 269 270 # Create the directories. 271 mkdir_nofail(dir, verbosity=0) 272 273 # Specific results writing function. 274 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[pipe_name].pipe_type) 275 276 # Get the info box. 277 info = Info_box() 278 279 # Add the relax citations. 280 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): 281 # Alias the bib entry. 282 bib = info.bib[key] 283 284 # Add. 285 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) 286 287 # Add the relax software package. 288 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) 289 290 # Execute the specific BMRB writing code. 291 write_function(file, version=version) 292 293 # Add the file to the results file list. 294 if isinstance(file, str): 295 add_result_file(type='text', label='BMRB', file=file)
296