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 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
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
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
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
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
97 for i in range(N):
98
99 spin = return_spin(spin_ids[i])
100 if spin:
101 continue
102
103
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
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 metadata_cleanup()
116
117
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
128 sample_conds = []
129
130
131 for data in star.sample_conditions.loop():
132
133 sample_conds.append(data['sf_framecode'])
134
135
136 return sample_conds
137
138
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
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
160 return mol_names
161
162
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
173 N = 0
174
175
176 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2']
177
178
179 for key in keys:
180 if key in data.keys() and data[key]:
181 N = len(data[key])
182 break
183
184
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
202 if not dep_check.bmrblib_module:
203 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib')
204
205
206 pipe_name = cdp_name()
207 if not pipe_name:
208 raise RelaxNoPipeError
209
210
211 if not ds[pipe_name].is_empty():
212 raise RelaxError("The current data pipe is not empty.")
213
214
215 file_path = get_file_path(file_name=file, dir=dir)
216
217
218 if not access(file_path, F_OK):
219 raise RelaxFileError(file_path)
220
221
222 read_function = specific_fns.setup.get_specific_fn('bmrb_read', ds[pipe_name].pipe_type)
223
224
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
242 if not dep_check.bmrblib_module:
243 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib')
244
245
246 pipe_name = cdp_name()
247 if not pipe_name:
248 raise RelaxNoPipeError
249
250
251 if file == None:
252 raise RelaxError("The file name must be specified.")
253
254
255 if isinstance(file, str):
256
257 if dir == 'pipe_name':
258 dir = pipe_name
259
260
261 file = get_file_path(file, dir)
262
263
264 if access(file, F_OK) and not force:
265 raise RelaxFileOverwriteError(file, 'force flag')
266
267
268 print("Opening the file '%s' for writing." % file)
269
270
271 mkdir_nofail(dir, verbosity=0)
272
273
274 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[pipe_name].pipe_type)
275
276
277 info = Info_box()
278
279
280 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']):
281
282 bib = info.bib[key]
283
284
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
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
291 write_function(file, version=version)
292
293
294 if isinstance(file, str):
295 add_result_file(type='text', label='BMRB', file=file)
296