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 (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, 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
115 -def list_sample_conditions(star):
116 """Get a listing of all the sample conditions. 117 118 @param star: The NMR-STAR dictionary object. 119 @type star: NMR_STAR instance 120 @return: The list of sample condition names. 121 @rtype: list of str 122 """ 123 124 # Init. 125 sample_conds = [] 126 127 # Get the sample conditions. 128 for data in star.sample_conditions.loop(): 129 # Store the framecode. 130 sample_conds.append(data['sf_framecode']) 131 132 # Return the names. 133 return sample_conds
134 135
136 -def molecule_names(data, N=0):
137 """Generate the molecule names list. 138 139 @param data: An element of data from bmrblib. 140 @type data: dict 141 @return: The list of molecule names. 142 @rtype: list of str 143 """ 144 145 # The molecule index and name. 146 mol_index = [] 147 for i in range(N): 148 if 'entity_ids' in data.keys() and data['entity_ids'] != None and data['entity_ids'][i] != None: 149 mol_index.append(int(data['entity_ids'][i]) -1 ) 150 else: 151 mol_index = [0]*N 152 mol_names = [] 153 for i in range(N): 154 mol_names.append(cdp.mol[mol_index[i]].name) 155 156 # Return the names. 157 return mol_names
158 159
160 -def num_spins(data):
161 """Determine the number of spins in the given data. 162 163 @param data: An element of data from bmrblib. 164 @type data: dict 165 @return: The number of spins. 166 @rtype: int 167 """ 168 169 # The number of spins. 170 N = 0 171 172 # List of keys containing sequence information. 173 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2'] 174 175 # Loop over the keys until a list is found. 176 for key in keys: 177 if key in data.keys() and data[key]: 178 N = len(data[key]) 179 break 180 181 # Return the number. 182 return N
183 184
185 -def read(file=None, dir=None, version=None, sample_conditions=None):
186 """Read the contents of a BMRB NMR-STAR formatted file. 187 188 @keyword file: The name of the BMRB STAR formatted file. 189 @type file: str 190 @keyword dir: The directory where the file is located. 191 @type dir: None or str 192 @keyword version: The BMRB version to force the reading. 193 @type version: None or str 194 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 195 @type sample_conditions: None or str 196 """ 197 198 # Test if bmrblib is installed. 199 if not dep_check.bmrblib_module: 200 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 201 202 # Test if the current data pipe exists. 203 pipe_name = cdp_name() 204 if not pipe_name: 205 raise RelaxNoPipeError 206 207 # Make sure that the data pipe is empty. 208 if not ds[pipe_name].is_empty(): 209 raise RelaxError("The current data pipe is not empty.") 210 211 # Get the full file path. 212 file_path = get_file_path(file_name=file, dir=dir) 213 214 # Fail if the file does not exist. 215 if not access(file_path, F_OK): 216 raise RelaxFileError(file_path) 217 218 # Specific results reading function. 219 read_function = specific_fns.setup.get_specific_fn('bmrb_read', ds[pipe_name].pipe_type) 220 221 # Read the results. 222 read_function(file_path, version=version, sample_conditions=sample_conditions)
223 224
225 -def write(file=None, dir=None, version='3.1', force=False):
226 """Create a BMRB NMR-STAR formatted file. 227 228 @keyword file: The name of the file to create or a file object. 229 @type file: str or file object 230 @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. 231 @type dir: str or None 232 @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. 233 @type version: str 234 @keyword force: A flag which if True will allow a currently existing file to be overwritten. 235 @type force: bool 236 """ 237 238 # Test if bmrblib is installed. 239 if not dep_check.bmrblib_module: 240 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 241 242 # Test if the current data pipe exists. 243 pipe_name = cdp_name() 244 if not pipe_name: 245 raise RelaxNoPipeError 246 247 # Check the file name. 248 if file == None: 249 raise RelaxError("The file name must be specified.") 250 251 # A file object. 252 if isinstance(file, str): 253 # The special data pipe name directory. 254 if dir == 'pipe_name': 255 dir = pipe_name 256 257 # Get the full file path. 258 file = get_file_path(file, dir) 259 260 # Fail if the file already exists and the force flag is False. 261 if access(file, F_OK) and not force: 262 raise RelaxFileOverwriteError(file, 'force flag') 263 264 # Print out. 265 print("Opening the file '%s' for writing." % file) 266 267 # Create the directories. 268 mkdir_nofail(dir, verbosity=0) 269 270 # Specific results writing function. 271 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[pipe_name].pipe_type) 272 273 # Get the info box. 274 info = Info_box() 275 276 # Add the relax citations. 277 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): 278 # Alias the bib entry. 279 bib = info.bib[key] 280 281 # Add. 282 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) 283 284 # Add the relax software package. 285 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) 286 287 # Execute the specific BMRB writing code. 288 write_function(file, version=version) 289 290 # Add the file to the results file list. 291 if isinstance(file, str): 292 add_result_file(type='text', label='BMRB', file=file)
293