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