-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjanacifhelper.py
348 lines (293 loc) · 13.8 KB
/
janacifhelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""A program to provide some CIF items from standard JANA2006 output.
JANA CIF Helper
This program converts several parameters from JANA2006 into standardized CIF items for copying and pasting into the
final CIF. Just drag-and-drop the *.ref or *.m41 on the script icon and a short CIF file with the additional items will
be created.
Caveat: This program is not meant to be robust but to provide results quickly (and somewhat dirtily).
"""
import argparse
import datetime
import math
import os
import sys
import traceback
import CifFile
__author__ = 'Dennis Wiedemann'
__copyright__ = 'Copyright 2019, Dr. Dennis Wiedemann'
__credits__ = ['Dennis Wiedemann']
__license__ = 'MIT'
__version__ = '0.3.0'
__maintainer__ = 'Dennis Wiedemann'
__email__ = 'dennis.wiedemann@chem.tu-berlin.de'
__status__ = 'Development'
OUTPUT_FILE_ADDITION = '_add.cif' # Extension of the output CIF file
BLOCK_NAME = 'global' # Name of the CIF data block to store information in
PHASE_NUMBER = 1 # Number of phase to put information out on, meaningless for single-phase refinements
NUM_LEN = 9 # Length of numbers (in characters) stored in *.m41
class Suppressor(object):
"""Suppress text output to `stdout`.
"""
def __enter__(self):
self.stdout = sys.stdout
sys.stdout = self
def __exit__(self, type, value, traceback):
sys.stdout = self.stdout
if type is not None:
raise
def write(self, x): pass
def ordinal(n):
"""Return an ordinal number representation for a given cardinal.
:param n: cardinal number
:type n: int
:return: ordinal number
:rtype: str
"""
return '%d%s' % (n, 'tsnrhtdd'[(math.floor(n // 10) % 10 != 1) * (n % 10 < 4) * n % 10::4])
def iucr_string(values):
"""Convert a central value (average) and its s.u. into an IUCr compliant number representation.
:param values: pair of central value (average) and s.u.
:type values: tuple((float, float))
:return: IUCr compliant representation
:rtype: str
"""
if values[1] == 0 or values[1] is None: # No or zero s.u. given
return str(values[0])
sig_pos = math.floor(math.log10(abs(values[1]))) # position of first significant digit
sig_3 = math.trunc(abs(values[1]) * 10 ** (2 - sig_pos)) / 10 ** (2 - sig_pos) # 1st three significant s.u. digits
sig_3 *= 10 ** -(sig_pos + 1) # s.u. moved directly behind decimal separator (final range: 0.100-0.999)
if sig_3 < 0.195: # round to two digits (final s.u. range: 0.10-0.19)
su = round(abs(values[1]), 1 - sig_pos)
avg = round(values[0], 1 - sig_pos)
sig_len = 2
elif sig_3 < 0.950: # round to one digit (final s.u. range: 0.2-0.9)
su = round(abs(values[1]), -sig_pos)
avg = round(values[0], -sig_pos)
sig_len = 1
else: # round to two digits and move forward (final s.u.: 0.10)
sig_pos += 1
su = round(abs(values[1]), 1 - sig_pos)
avg = round(values[0], 1 - sig_pos)
sig_len = 2
if sig_pos > 0: # only integral part for s.u. >= 1.95
sign_shift = -1 if values[0] < 0 else 0
avg_str = ('{:' + str(sig_pos + sign_shift) + '.0f}').format(avg).strip()
su_str = ('{:' + str(sig_pos) + '.0f}').format(su)
else: # fractional and possibly integral part for s.u. < 1.95
avg_str = ('{:.' + str(-sig_pos + sig_len - 1) + 'f}').format(avg)
su_str = '{:.0f}'.format(abs(su / 10 ** (sig_pos - sig_len + 1)))
return '{:s}({:s})'.format(avg_str, su_str)
def nibble_numbers(input_line, count, length=NUM_LEN):
"""Return fixed-length numbers cut from the beginning of a string.
:param input_line: line to nibble the numbers from
:type input_line: str
:param count: numbers of numbers to nibble
:type count: int
:param length: length of the nibbles
:type length: int
:return: numbers
:rtype: list(floats)
"""
numbers = []
for i in range(0, count):
numbers.append(float(input_line[i * length:(i + 1) * length]))
return numbers
# Parse arguments
parser = argparse.ArgumentParser(description='Convert parameters from JANA2006 output into standardized CIF items.')
parser.add_argument('input', metavar='input file', type=argparse.FileType('rb'),
help='name of the input *.ref or *.m41 file')
parser.add_argument('-v', '--version', action='version', version=__version__)
args = parser.parse_args()
# Construct file names
name_stem = os.path.splitext(args.input.name)[0]
name_m41 = name_stem + '.m41'
name_ref = name_stem + '.ref'
name_cif = name_stem + OUTPUT_FILE_ADDITION
# Greeting
print("Hiiieee! My name is yours, what's JANA CIF Helper?\n")
# Scan for and read the values from *.m41
print('Reading from *.m41 ...', end='')
skipped = []
phase_select = []
select = {}
is_multi_phase = False
with open(name_m41, 'r') as read_file:
# Read header values (selections)
for line in read_file:
if line.startswith('skipfrto'):
skipped.append((line.split()[1], line.split()[2]))
elif line.startswith('phase'):
is_multi_phase = True
line = read_file.readline() + read_file.readline()
phase_select.append(dict(zip(line.split()[::2], line.split()[1::2])))
elif line.startswith('end'):
break
else:
select.update(zip(line.split()[::2], line.split()[1::2]))
# Read shift parameters
while not line.startswith('# Shift'):
line = read_file.readline()
shift = dict(zip(['zero', 'sycos', 'sysin'], nibble_numbers(read_file.readline(), 3)))
# Read background parameters
while not line.startswith('# Background'):
line = read_file.readline()
background = []
for i in range(0, math.trunc(int(select['bckgnum']) / 6)):
background.append(nibble_numbers(read_file.readline(), 6))
if (int(select['bckgnum']) % 6) != 0:
background.append(nibble_numbers(read_file.readline(), int(select['bckgnum']) % 6))
background = [item for sublist in background for item in sublist]
# Read asymmetry parameters
while not line.startswith('# Asymmetry'):
line = read_file.readline()
if select['asymm'] == '1':
asymm = nibble_numbers(read_file.readline(), 1)[0]
# Read profile parameters
phase_count = 0 if is_multi_phase else PHASE_NUMBER
profile = {}
while not phase_count == PHASE_NUMBER:
line = read_file.readline()
while not line.startswith('### phase'):
line = read_file.readline()
phase_count += 1
while not line.startswith('# Gaussian'):
line = read_file.readline()
for key, value in zip(['GU', 'GV', 'GW', 'GP'], nibble_numbers(read_file.readline(), 4)):
profile[key] = value
while not line.startswith('# Lorentzian'):
line = read_file.readline()
for key, value in zip(['LX', 'LXe', 'LY', 'LYe'], nibble_numbers(read_file.readline(), 4)):
profile[key] = value
# Read shift s.u.'s
while not line.startswith('# Shift'):
line = read_file.readline()
shift_su = dict(zip(['zero', 'sycos', 'sysin'], nibble_numbers(read_file.readline(), 3)))
# Read background s.u.'s
while not line.startswith('# Background'):
line = read_file.readline()
background_su = []
for i in range(0, math.trunc(int(select['bckgnum']) / 6)):
background_su.append(nibble_numbers(read_file.readline(), 6))
if (int(select['bckgnum']) % 6) != 0:
background_su.append(nibble_numbers(read_file.readline(), int(select['bckgnum']) % 6))
background_su = [item for sublist in background_su for item in sublist]
# Read asymmetry s.u.'s
while not line.startswith('# Asymmetry'):
line = read_file.readline()
if select['asymm'] == '1':
asymm_su = nibble_numbers(read_file.readline(), 1)[0]
# Read profile s.u.'s
phase_count = 0 if is_multi_phase else PHASE_NUMBER
profile_su = {}
while not phase_count == PHASE_NUMBER:
line = read_file.readline()
while not line.startswith('### phase'):
line = read_file.readline()
phase_count += 1
while not line.startswith('# Gaussian'):
line = read_file.readline()
for key, value in zip(['GU', 'GV', 'GW', 'GP'], nibble_numbers(read_file.readline(), 4)):
profile_su[key] = value
while not line.startswith('# Lorentzian'):
line = read_file.readline()
for key, value in zip(['LX', 'LXe', 'LY', 'LYe'], nibble_numbers(read_file.readline(), 4)):
profile_su[key] = value
print(' Done.')
# Scan for and read the values from *.ref
print('Reading from *.ref ...', end='')
phase_count = 0
with open(name_ref, 'r') as read_file:
for line in read_file:
if line.startswith('Cycle RFobs'):
phase_count += 1
if phase_count == PHASE_NUMBER:
while line != '\n':
line_old = line
line = read_file.readline()
n_obs = int(line_old.split()[5])
n_all = int(line_old.split()[6])
rb_obs = float(line_old.split()[9])
print(' Done.')
# Construct CIF
print('Assembling CIF items ...', end='')
cif = CifFile.CifFile()
cif[BLOCK_NAME] = CifFile.CifBlock()
cif_block = cif[BLOCK_NAME]
# Set variables
cif.header_comment = ''
cif.set_grammar('1.1')
# Store introductory items
cif_block['_audit_conform_dict_name'] = ['cif_core.dic', 'cif_pd.dic']
cif_block['_audit_conform_dict_version'] = ['2.4.5', '1.0.1']
cif_block['_audit_conform_dict_location'] = ['ftp://ftp.iucr.org/pub/cif_core.dic', 'ftp://ftp.iucr.org/pub/cif_pd.dic']
cif_block.CreateLoop(['_audit_conform_dict_name', '_audit_conform_dict_version', '_audit_conform_dict_location'])
cif_block['_audit_creation_method'] = 'JANA CIF Helper v.' + __version__
cif_block['_audit_creation_date'] = datetime.datetime.now().strftime('%Y-%m-%d')
# Store extracted items
if select['absor'] == '1':
cif_block['_exptl_absorpt_correction_type'] = 'cylinder'
cif_block['_exptl_absorpt_process_details'] = '\ncorrection for a cylindrical sample with \\mR = ' + select['mir'] \
+ ' as implemented in JANA2006 (Pet\\<r\\\'i\\<cek et al., 2014)'
cif_block['_reflns_number_gt'] = '{:d}'.format(n_obs)
cif_block['_reflns_number_total'] = '{:d}'.format(n_all)
cif_block['_refine_ls_R_I_factor'] = '{:2.4f}'.format(rb_obs / 100)
skipped_string = []
for region in skipped:
skipped_string.append('from {:3.2f} to {:3.2f}\\%: '.format(float(region[0]), float(region[1])))
cif_block['_pd_proc_info_excluded_regions'] = skipped_string
cif_block.CreateLoop(['_pd_proc_info_excluded_regions'])
pd_proc_ls_special_details = '\n'
if shift['zero'] != '0.000000':
pd_proc_ls_special_details = 'zero-point correction: ' + iucr_string((shift['zero'] / 100, shift_su['zero'] / 100))\
+ '\\%'
cif_block['_pd_proc_ls_special_details'] = pd_proc_ls_special_details
_pd_proc_ls_profile_function = '\n'
proffun = phase_select[PHASE_NUMBER - 1]['proffun'] if is_multi_phase else select['proffun']
if proffun == '3':
_pd_proc_ls_profile_function += 'pseudo-Voigt profile according to Thompson, Cox & Hastings (1987): G~U~ = '
_pd_proc_ls_profile_function += '0' if profile['GU'] == 0.0 else iucr_string((profile['GU'], profile_su['GU']))
_pd_proc_ls_profile_function += ', G~V~ = '
_pd_proc_ls_profile_function += '0' if profile['GV'] == 0.0 else iucr_string((profile['GV'], profile_su['GV']))
_pd_proc_ls_profile_function += ', G~W~ = '
_pd_proc_ls_profile_function += '0' if profile['GW'] == 0.0 else iucr_string((profile['GW'], profile_su['GW']))
if profile['GP'] != 0.0:
_pd_proc_ls_profile_function += ', G~P~ = ' + iucr_string((profile['GP'], profile_su['GP']))
_pd_proc_ls_profile_function += ', L~X~ = '
_pd_proc_ls_profile_function += '0' if profile['LX'] == 0.0 else iucr_string((profile['LX'], profile_su['LX']))
_pd_proc_ls_profile_function += ', L~Y~ = '
_pd_proc_ls_profile_function += '0' if profile['LY'] == 0.0 else iucr_string((profile['LY'], profile_su['LY']))
if profile['LXe'] != 0.0:
_pd_proc_ls_profile_function += ', L~Xe~ = ' + iucr_string((profile['LXe'], profile_su['LXe']))
if profile['LYe'] != 0.0:
_pd_proc_ls_profile_function += ', L~Ye~ = ' + iucr_string((profile['LYe'], profile_su['LYe']))
if select['asymm'] == '1':
_pd_proc_ls_profile_function += ';\nasymmetry correction according to Howard (1982): P = ' \
+ iucr_string((asymm, asymm_su))
cif_block['_pd_proc_ls_profile_function'] = _pd_proc_ls_profile_function
_pd_proc_ls_background_function = '\n'
if select['manbckg'] == '1':
_pd_proc_ls_background_function += 'manual background (visual estimation, unrefined)'
if select['manbckg'] == '1' and select['bckgtype'] == '1':
_pd_proc_ls_background_function += ' interpolated by '
if select['bckgtype'] == '1':
_pd_proc_ls_background_function += select['bckgnum'] + ' Legendre polynomials [1st: ' \
+ iucr_string((background[0], background_su[0]))
for i in range(2, int(select['bckgnum']) + 1):
_pd_proc_ls_background_function += ', {:s}: '.format(ordinal(i)) \
+ iucr_string((background[i - 1], background_su[i - 1]))
_pd_proc_ls_background_function += ']'
cif_block['_pd_proc_ls_background_function'] = _pd_proc_ls_background_function
print(' Done.')
# Store static items
cif_block['_reflns_threshold_expression'] = 'I>3\\s(I)'
cif_block['_refine_ls_structure_factor_coef'] = 'Inet'
cif_block['_refine_ls_matrix_type'] = 'fullcycle'
cif_block['_refine_ls_weighting_details'] = 'w=1/[\\s^2^(I)+(0.01*I)^2^]'
# Output CIF
print('Writing to %s ...' % name_cif, end='')
with open(name_cif, 'w') as write_file, Suppressor():
write_file.write(cif.WriteOut())
print(' Done.')
# Sendoff
print('\nYour makeup is NOT terrible. Byyyeee!')