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

Source Code for Module pipe_control.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_store import Relax_data_store; ds = Relax_data_store() 
 31  import dep_check 
 32  from info import Info_box 
 33  from lib.errors import RelaxError, RelaxFileError, RelaxFileOverwriteError, RelaxNoModuleInstallError, RelaxNoPipeError 
 34  from lib.io import get_file_path, mkdir_nofail 
 35  from pipe_control import exp_info 
 36  from pipe_control.mol_res_spin import create_spin, generate_spin_id, metadata_cleanup, return_spin, set_spin_element, set_spin_isotope 
 37  from pipe_control.pipes import cdp_name 
 38  from pipe_control.result_files import add_result_file 
 39  from specific_analyses.api import return_api 
 40  from status import Status; status = Status() 
 41  from version import version_full 
 42   
 43   
44 -def display(version='3.1'):
45 """Display the results in the BMRB NMR-STAR format. 46 47 @keyword version: The NMR-STAR version to create. This can be either '2.1', '3.0', or '3.1'. 48 @type version: str 49 """ 50 51 # Call the write() function with stdout. 52 write(file=sys.stdout, version=version)
53 54
55 -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):
56 """Generate the sequence data from the BRMB information. 57 58 @keyword N: The number of spins. 59 @type N: int 60 @keyword spin_ids: The list of spin IDs. 61 @type spin_ids: list of str 62 @keyword spin_nums: The list of spin numbers. 63 @type spin_nums: list of int or None 64 @keyword spin_names: The list of spin names. 65 @type spin_names: list of str or None 66 @keyword res_nums: The list of residue numbers. 67 @type res_nums: list of int or None 68 @keyword res_names: The list of residue names. 69 @type res_names: list of str or None 70 @keyword mol_names: The list of molecule names. 71 @type mol_names: list of str or None 72 @keyword isotopes: The optional list of isotope types. 73 @type isotopes: list of str or None 74 @keyword elements: The optional list of element types. 75 @type elements: list of str or None 76 """ 77 78 # The blank data. 79 if not spin_nums: 80 spin_nums = [None] * N 81 if not spin_names: 82 spin_names = [None] * N 83 if not res_nums: 84 res_nums = [None] * N 85 if not res_names: 86 res_names = [None] * N 87 if not mol_names: 88 mol_names = [None] * N 89 90 # Generate the spin IDs. 91 spin_ids = [] 92 for i in range(N): 93 spin_ids.append(generate_spin_id(mol_name=mol_names[i], res_num=res_nums[i], spin_name=spin_names[i])) 94 95 # Loop over the spin data. 96 for i in range(N): 97 # The spin already exists. 98 spin = return_spin(spin_id=spin_ids[i]) 99 if spin: 100 continue 101 102 # Create the spin. 103 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])[0] 104 105 # Set the spin isotope and element. 106 spin_id = spin._spin_ids[0] 107 if elements: 108 set_spin_element(spin_id=spin_id, element=elements[i], force=True) 109 if isotopes and elements: 110 isotope = "%s%s" % (isotopes[i], elements[i]) 111 set_spin_isotope(spin_id=spin_id, isotope=isotope, force=True) 112 113 # Clean up the spin metadata. 114 metadata_cleanup()
115 116
117 -def list_sample_conditions(star):
118 """Get a listing of all the sample conditions. 119 120 @param star: The NMR-STAR dictionary object. 121 @type star: NMR_STAR instance 122 @return: The list of sample condition names. 123 @rtype: list of str 124 """ 125 126 # Init. 127 sample_conds = [] 128 129 # Get the sample conditions. 130 for data in star.sample_conditions.loop(): 131 # Store the framecode. 132 sample_conds.append(data['sf_framecode']) 133 134 # Return the names. 135 return sample_conds
136 137
138 -def molecule_names(data, N=0):
139 """Generate the molecule names list. 140 141 @param data: An element of data from bmrblib. 142 @type data: dict 143 @return: The list of molecule names. 144 @rtype: list of str 145 """ 146 147 # The molecule index and name. 148 mol_index = [] 149 for i in range(N): 150 if 'entity_ids' in data and data['entity_ids'] != None and data['entity_ids'][i] != None: 151 mol_index.append(int(data['entity_ids'][i]) -1 ) 152 else: 153 mol_index = [0]*N 154 mol_names = [] 155 for i in range(N): 156 mol_names.append(cdp.mol[mol_index[i]].name) 157 158 # Return the names. 159 return mol_names
160 161
162 -def num_spins(data):
163 """Determine the number of spins in the given data. 164 165 @param data: An element of data from bmrblib. 166 @type data: dict 167 @return: The number of spins. 168 @rtype: int 169 """ 170 171 # The number of spins. 172 N = 0 173 174 # List of keys containing sequence information. 175 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2'] 176 177 # Loop over the keys until a list is found. 178 for key in keys: 179 if key in data and data[key]: 180 N = len(data[key]) 181 break 182 183 # Return the number. 184 return N
185 186
187 -def read(file=None, dir=None, version=None, sample_conditions=None):
188 """Read the contents of a BMRB NMR-STAR formatted file. 189 190 @keyword file: The name of the BMRB STAR formatted file. 191 @type file: str 192 @keyword dir: The directory where the file is located. 193 @type dir: None or str 194 @keyword version: The BMRB version to force the reading. 195 @type version: None or str 196 @keyword sample_conditions: The sample condition label to read. Only one sample condition can be read per data pipe. 197 @type sample_conditions: None or str 198 """ 199 200 # Test if bmrblib is installed. 201 if not dep_check.bmrblib_module: 202 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib') 203 204 # Test if the current data pipe exists. 205 pipe_name = cdp_name() 206 if not pipe_name: 207 raise RelaxNoPipeError 208 209 # Make sure that the data pipe is empty. 210 if not ds[pipe_name].is_empty(): 211 raise RelaxError("The current data pipe is not empty.") 212 213 # Get the full file path. 214 file_path = get_file_path(file_name=file, dir=dir) 215 216 # Fail if the file does not exist. 217 if not access(file_path, F_OK): 218 raise RelaxFileError(file_path) 219 220 # Read the results. 221 api = return_api(pipe_name=pipe_name) 222 api.bmrb_read(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 # Get the info box. 271 info = Info_box() 272 273 # Add the relax citations. 274 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']): 275 # Alias the bib entry. 276 bib = info.bib[key] 277 278 # Add. 279 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) 280 281 # Add the relax software package. 282 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) 283 284 # Execute the specific BMRB writing code. 285 api = return_api(pipe_name=pipe_name) 286 api.bmrb_write(file, version=version) 287 288 # Add the file to the results file list. 289 if isinstance(file, str): 290 add_result_file(type='text', label='BMRB', file=file)
291