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, 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
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
125 sample_conds = []
126
127
128 for data in star.sample_conditions.loop():
129
130 sample_conds.append(data['sf_framecode'])
131
132
133 return sample_conds
134
135
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
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
157 return mol_names
158
159
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
170 N = 0
171
172
173 keys = ['data_ids', 'entity_ids', 'res_names', 'res_nums', 's2']
174
175
176 for key in keys:
177 if key in data.keys() and data[key]:
178 N = len(data[key])
179 break
180
181
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
199 if not dep_check.bmrblib_module:
200 raise RelaxNoModuleInstallError('BMRB library', 'bmrblib')
201
202
203 pipe_name = cdp_name()
204 if not pipe_name:
205 raise RelaxNoPipeError
206
207
208 if not ds[pipe_name].is_empty():
209 raise RelaxError("The current data pipe is not empty.")
210
211
212 file_path = get_file_path(file_name=file, dir=dir)
213
214
215 if not access(file_path, F_OK):
216 raise RelaxFileError(file_path)
217
218
219 read_function = specific_fns.setup.get_specific_fn('bmrb_read', ds[pipe_name].pipe_type)
220
221
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
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 write_function = specific_fns.setup.get_specific_fn('bmrb_write', ds[pipe_name].pipe_type)
272
273
274 info = Info_box()
275
276
277 for id, key in zip(['relax_ref1', 'relax_ref2'], ['dAuvergneGooley08a', 'dAuvergneGooley08b']):
278
279 bib = info.bib[key]
280
281
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
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
288 write_function(file, version=version)
289
290
291 if isinstance(file, str):
292 add_result_file(type='text', label='BMRB', file=file)
293