1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """Module containing functions for BMRB support."""
24
25
26 from os import F_OK, access
27 import sys
28
29
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
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
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
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
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
96 for i in range(N):
97
98 spin = return_spin(spin_ids[i])
99 if spin:
100 continue
101
102
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])
104
105
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
114 metadata_cleanup()
115
116
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
127 sample_conds = []
128
129
130 for data in star.sample_conditions.loop():
131
132 sample_conds.append(data['sf_framecode'])
133
134
135 return sample_conds
136
137
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
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
159 return mol_names
160
161
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
172 N = 0
173
174
175 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2']
176
177
178 for key in keys:
179 if key in data and data[key]:
180 N = len(data[key])
181 break
182
183
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
201 if not dep_check.bmrblib_module:
202 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib')
203
204
205 pipe_name = cdp_name()
206 if not pipe_name:
207 raise RelaxNoPipeError
208
209
210 if not ds[pipe_name].is_empty():
211 raise RelaxError("The current data pipe is not empty.")
212
213
214 file_path = get_file_path(file_name=file, dir=dir)
215
216
217 if not access(file_path, F_OK):
218 raise RelaxFileError(file_path)
219
220
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
239 if not dep_check.bmrblib_module:
240 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib')
241
242
243 pipe_name = cdp_name()
244 if not pipe_name:
245 raise RelaxNoPipeError
246
247
248 if file == None:
249 raise RelaxError("The file name must be specified.")
250
251
252 if isinstance(file, str):
253
254 if dir == 'pipe_name':
255 dir = pipe_name
256
257
258 file = get_file_path(file, dir)
259
260
261 if access(file, F_OK) and not force:
262 raise RelaxFileOverwriteError(file, 'force flag')
263
264
265 print("Opening the file '%s' for writing." % file)
266
267
268 mkdir_nofail(dir, verbosity=0)
269
270
271 info = Info_box()
272
273
274 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']):
275
276 bib = info.bib[key]
277
278
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
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
285 api = return_api(pipe_name=pipe_name)
286 api.bmrb_write(file, version=version)
287
288
289 if isinstance(file, str):
290 add_result_file(type='text', label='BMRB', file=file)
291