-
Notifications
You must be signed in to change notification settings - Fork 1
/
xds2mtz.py
executable file
·484 lines (397 loc) · 14.8 KB
/
xds2mtz.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env cctbx.python
from __future__ import print_function
"""
xds2mtz.py
(c) RIKEN 2015. All rights reserved.
Author: Keitaro Yamashita
This software is released under the new BSD License; see LICENSE.
"""
"""
xds2mtz.py : convert XDS_ASCII.HKL or xscale output to mtz file. MTZ file will include all possible columns user would need.
When FRIDEL'S_LAW= FALSE,
MTZ columns will be F(+), F(-), I(+), I(-), IMEAN, FP, DANO, ISYM
When FRIDEL'S_LAW= TRUE,
MTZ columns will be IMEAN, FP
With -x option, phenix.xtriage is executed automatically.
With -t option, ctruncate is used for converting I to F.
"""
import sys, os, optparse, subprocess, re
import mtzutil
from util import call
re_xds_kwd = re.compile("([^ =]+)= *((?:(?! [^ =]+=).)*)")
def unique(mtzin, mtzout, wdir):
##
# unique -> cad -> mtzutil (EXCLUDE FUNI SIGFUNI)
#
logfile = os.path.join(wdir, "xds2mtz.log")
m = mtzutil.MtzFile(os.path.join(wdir,mtzin))
cell = m.get_cell_str()
sg = m.get_spacegroup()[1].decode()
resol = min(m.get_resolution())
call(cmd="unique",
arg="hklout unique.mtz",
wdir=wdir,
stdin="CELL %s\nSYMMETRY '%s'\nLABOUT F=FUNI SIGF=SIGFUNI\nRESOLUTION %f" % (cell, sg, resol),
expects_in=[],
expects_out=["unique.mtz"],
stdout=open(logfile, "a")
)
call(cmd="cad",
arg="hklin1 %s hklin2 %s hklout %s" %(mtzin, "unique.mtz", "unique_cad.mtz"),
wdir=wdir,
stdin="labin file 1 all\nlabin file 2 all\nend\n",
expects_in=[mtzin, "unique.mtz"],
expects_out=["unique_cad.mtz"],
stdout=open(logfile, "a")
)
call(cmd="mtzutils",
arg="hklin %s hklout %s" % ("unique_cad.mtz", mtzout),
wdir=wdir,
stdin="EXCLUDE FUNI SIGFUNI\nRUN\n",
expects_in=["unique_cad.mtz"],
expects_out=[mtzout],
stdout=open(logfile, "a")
)
os.remove(os.path.join(wdir, "unique.mtz"))
os.remove(os.path.join(wdir, "unique_cad.mtz"))
# unique()
def xds2mtz_normal(refl, mtzout, sg, wavelen, use_ctruncate=False, dmin=None, dmax=None):
wdir = os.path.dirname(mtzout)
logfile = os.path.join(wdir, "xds2mtz.log")
if not os.path.exists(os.path.join(wdir, "original")):
os.symlink(refl, os.path.join(wdir, "original"))
##
# prepare XDSCONV.INP and run
#
# for I
print("generating MTZ for IMEAN,SIGIMEAN")
ofs = open(os.path.join(wdir, "XDSCONV.INP"), "w")
ofs.write("OUTPUT_FILE=tmp.hkl CCP4_I\n")
ofs.write("INPUT_FILE=original\n")
ofs.write("GENERATE_FRACTION_OF_TEST_REFLECTIONS=0.0\n")
ofs.write("WILSON_STATISTICS= TRUE\n")
if None not in (dmin, dmax):
ofs.write("INCLUDE_RESOLUTION_RANGE= %s %s\n" % (dmax, dmin))
ofs.close()
call(cmd="xdsconv",
wdir=wdir,
expects_in=["original"],
expects_out=["F2MTZ.INP", "tmp.hkl"],
stdout=open(logfile, "a")
)
call(cmd="f2mtz",
arg="hklout CCP4_I.mtz",
stdin=open(os.path.join(wdir, "F2MTZ.INP")).read(),
wdir=wdir,
expects_in=["tmp.hkl"],
expects_out=["CCP4_I.mtz"],
stdout=open(logfile, "a")
)
# for F
print("generating MTZ for FP,SIGFP")
if use_ctruncate:
call(cmd="ctruncate -hklin CCP4_I.mtz -hklout ctruncate.mtz -colin '/*/*/[IMEAN,SIGIMEAN]'",
wdir=wdir,
expects_in=["CCP4_I.mtz"],
expects_out=["ctruncate.mtz"],
stdout=open(os.path.join(wdir, "ctruncate.log"), "w")
)
call(cmd="cad",
arg="hklin1 ctruncate.mtz hklout CCP4_FI.mtz",
stdin="""\
labin file 1 all
xname file 1 ALL=XDS
dname file 1 ALL=XDS
dwavelength file 1 XDS XDS %s
symmetry %s
end
"""%(wavelen,sg),
wdir=wdir,
expects_in=["ctruncate.mtz"],
expects_out=["CCP4_FI.mtz"],
stdout=open(logfile, "a")
)
else:
ofs = open(os.path.join(wdir, "XDSCONV.INP"), "w")
ofs.write("OUTPUT_FILE=tmp.hkl CCP4_F\n")
ofs.write("INPUT_FILE=original\n")
ofs.write("GENERATE_FRACTION_OF_TEST_REFLECTIONS=0.0\n")
ofs.write("WILSON_STATISTICS= TRUE\n")
if None not in (dmin, dmax):
ofs.write("INCLUDE_RESOLUTION_RANGE= %s %s\n" % (dmax, dmin))
ofs.close()
call(cmd="xdsconv",
wdir=wdir,
expects_in=["original"],
expects_out=["F2MTZ.INP", "tmp.hkl"],
stdout=open(logfile, "a")
)
call(cmd="f2mtz",
arg="hklout CCP4_F.mtz",
stdin=open(os.path.join(wdir, "F2MTZ.INP")).read(),
wdir=wdir,
expects_in=["tmp.hkl"],
expects_out=["CCP4_F.mtz"],
stdout=open(logfile, "a")
)
##
# CAD all mtz files
print("concatenating MTZ files")
call(cmd="cad",
arg="hklin1 CCP4_I.mtz hklin2 CCP4_F.mtz hklout CCP4_FI.mtz",
stdin="""\
labin file 1 all
labin file 2 all
xname file 1 ALL=XDS
xname file 2 ALL=XDS
dname file 1 ALL=XDS
dname file 2 ALL=XDS
dwavelength file 1 XDS XDS %s
symmetry %s
end
"""%(wavelen,sg),
wdir=wdir,
expects_in=["CCP4_I.mtz", "CCP4_F.mtz"],
expects_out=["CCP4_FI.mtz"],
stdout=open(logfile, "a")
)
##
# Generate all unique reflections
print("Genrating all unique reflections")
unique(mtzin="CCP4_FI.mtz", mtzout=os.path.basename(mtzout), wdir=wdir)
# remove files
os.remove(os.path.join(wdir, "CCP4_I.mtz"))
os.remove(os.path.join(wdir, "CCP4_FI.mtz"))
os.remove(os.path.join(wdir, "tmp.hkl"))
os.remove(os.path.join(wdir, "XDSCONV.INP"))
os.remove(os.path.join(wdir, "XDSCONV.LP"))
os.remove(os.path.join(wdir, "F2MTZ.INP"))
if use_ctruncate:
os.remove(os.path.join(wdir, "ctruncate.mtz"))
else:
os.remove(os.path.join(wdir, "CCP4_F.mtz"))
# xds2mtz_anom()
def xds2mtz_anom(refl, mtzout, sg, wavelen, use_ctruncate=False, dmin=None, dmax=None):
wdir = os.path.dirname(mtzout)
logfile = os.path.join(wdir, "xds2mtz.log")
if not os.path.exists(os.path.join(wdir, "original")):
os.symlink(refl, os.path.join(wdir, "original"))
##
# prepare XDSCONV.INP and run
# for I(+), I(-), SIGI(+), SIGI(-)
print("generating MTZ for I(+), I(-), SIGI(+), SIGI(-)")
ofs = open(os.path.join(wdir, "XDSCONV.INP"), "w")
ofs.write("OUTPUT_FILE=tmp.hkl CCP4_I\n")
ofs.write("INPUT_FILE=original\n")
ofs.write("GENERATE_FRACTION_OF_TEST_REFLECTIONS=0.0\n")
ofs.write("WILSON_STATISTICS= TRUE\n")
ofs.write("FRIEDEL'S_LAW= FALSE\n")
if None not in (dmin, dmax):
ofs.write("INCLUDE_RESOLUTION_RANGE= %s %s\n" % (dmax, dmin))
ofs.close()
call(cmd="xdsconv",
wdir=wdir,
expects_in=["original"],
expects_out=["F2MTZ.INP", "tmp.hkl"],
stdout=open(logfile, "a")
)
call(cmd="f2mtz",
arg="hklout CCP4_I.mtz",
stdin=open(os.path.join(wdir, "F2MTZ.INP")).read(),
wdir=wdir,
expects_in=["tmp.hkl"],
expects_out=["CCP4_I.mtz"],
stdout=open(logfile, "a")
)
# for F(+), F(-), SIGF(+), SIGF(-)
print("generating MTZ for F(+), F(-), SIGF(+), SIGF(-)")
if use_ctruncate:
call(cmd="ctruncate -hklin CCP4_I.mtz -hklout ctruncate.mtz -colin '/*/*/[IMEAN,SIGIMEAN]' -colano '/*/*/[I(+),SIGI(+),I(-),SIGI(-)]'",
wdir=wdir,
expects_in=["CCP4_I.mtz"],
expects_out=["ctruncate.mtz"],
stdout=open(os.path.join(wdir, "ctruncate.log"), "w")
)
call(cmd="cad",
arg="hklin1 ctruncate.mtz hklout CCP4_FI.mtz",
stdin="""\
labin file 1 all
xname file 1 ALL=XDS
dname file 1 ALL=XDS
dwavelength file 1 XDS XDS %s
symmetry %s
end
"""%(wavelen,sg),
wdir=wdir,
expects_in=["ctruncate.mtz"],
expects_out=["CCP4_FI.mtz"],
stdout=open(logfile, "a")
)
else:
ofs = open(os.path.join(wdir, "XDSCONV.INP"), "w")
ofs.write("OUTPUT_FILE=tmp.hkl CCP4_F\n")
ofs.write("INPUT_FILE=original\n")
ofs.write("GENERATE_FRACTION_OF_TEST_REFLECTIONS=0.0\n")
ofs.write("WILSON_STATISTICS= TRUE\n")
ofs.write("FRIEDEL'S_LAW= FALSE\n")
if None not in (dmin, dmax):
ofs.write("INCLUDE_RESOLUTION_RANGE= %s %s\n" % (dmax, dmin))
ofs.close()
call(cmd="xdsconv",
wdir=wdir,
expects_in=["original"],
expects_out=["F2MTZ.INP", "tmp.hkl"],
stdout=open(logfile, "a")
)
call(cmd="f2mtz",
arg="hklout CCP4_F.mtz",
stdin=open(os.path.join(wdir, "F2MTZ.INP")).read(),
wdir=wdir,
expects_in=["tmp.hkl"],
expects_out=["CCP4_F.mtz"],
stdout=open(logfile, "a")
)
# for DANO, ISYM
print("generating MTZ for DANO, ISYM")
ofs = open(os.path.join(wdir, "XDSCONV.INP"), "w")
ofs.write("OUTPUT_FILE=tmp.hkl CCP4\n")
ofs.write("INPUT_FILE=original\n")
ofs.write("GENERATE_FRACTION_OF_TEST_REFLECTIONS=0.0\n")
ofs.write("WILSON_STATISTICS= TRUE\n")
ofs.write("FRIEDEL'S_LAW= FALSE\n")
ofs.close()
call(cmd="xdsconv",
wdir=wdir,
expects_in=["original"],
expects_out=["F2MTZ.INP", "tmp.hkl"],
stdout=open(logfile, "a")
)
call(cmd="f2mtz",
arg="hklout CCP4.mtz",
stdin=open(os.path.join(wdir, "F2MTZ.INP")).read(),
wdir=wdir,
expects_in=["tmp.hkl"],
expects_out=["CCP4.mtz"],
stdout=open(logfile, "a")
)
##
# CAD all mtz files
print("concatenating MTZ files")
call(cmd="cad",
arg="hklin1 CCP4_I.mtz hklin2 CCP4_F.mtz hklin3 CCP4.mtz hklout CCP4_FI.mtz",
stdin="""\
labin file 1 all
labin file 2 all
labin file 3 E1=DANO E2=SIGDANO E3=ISYM
xname file 1 ALL=XDS
xname file 2 ALL=XDS
xname file 3 ALL=XDS
dname file 1 ALL=XDS
dname file 2 ALL=XDS
dname file 3 ALL=XDS
dwavelength file 1 XDS XDS %s
symmetry %s
end
"""%(wavelen,sg),
wdir=wdir,
expects_in=["CCP4_I.mtz", "CCP4_F.mtz", "CCP4.mtz"],
expects_out=["CCP4_FI.mtz"],
stdout=open(logfile, "a")
)
##
# Generate all unique reflections
print("Genrating all unique reflections")
unique(mtzin="CCP4_FI.mtz", mtzout=os.path.basename(mtzout), wdir=wdir)
# remove files
os.remove(os.path.join(wdir, "CCP4_I.mtz"))
os.remove(os.path.join(wdir, "CCP4_FI.mtz"))
os.remove(os.path.join(wdir, "tmp.hkl"))
os.remove(os.path.join(wdir, "XDSCONV.INP"))
os.remove(os.path.join(wdir, "XDSCONV.LP"))
os.remove(os.path.join(wdir, "F2MTZ.INP"))
if use_ctruncate:
os.remove(os.path.join(wdir, "ctruncate.mtz"))
else:
os.remove(os.path.join(wdir, "CCP4_F.mtz"))
os.remove(os.path.join(wdir, "CCP4.mtz"))
# xds2mtz_anom()
def xds2mtz(xds_file, dir_name, hklout=None, run_xtriage=False, run_ctruncate=False, dmin=None, dmax=None, force_anomalous=False):
if hklout is None:
hklout = os.path.splitext(os.path.basename(xds_file))[0] + ".mtz"
# if output file already exists, exit.
if os.path.isfile(os.path.join(dir_name, hklout)):
raise Exception(os.path.join(dir_name, hklout), "already exists.")
# read header
header = {}
for l in open(xds_file):
if l.startswith("!END_OF_HEADER"):
break
headers = re_xds_kwd.findall(l[l.index("!")+1:])
for k, v in headers:
if k == "FRIEDEL'S_LAW":
header["FRIEDEL'S_LAW"] = v.strip()
if k == "SPACE_GROUP_NUMBER":
header["SPACE_GROUP_NUMBER"] = v.strip()
if k == "X-RAY_WAVELENGTH":
header["X-RAY_WAVELENGTH"] = v.strip() # XXX could be wrong if XSCALE result
if force_anomalous:
header["FRIEDEL'S_LAW"] = "FALSE"
# make output directory
if not os.path.isdir(dir_name):
os.makedirs(dir_name)
print("Header information read from", xds_file)
for k in header:
print(k, "=", header[k])
print()
##
# convert to MTZ
if header["FRIEDEL'S_LAW"] == "TRUE":
print(xds_file, "is not anomalous dataset.")
print()
xds2mtz_normal(xds_file,
mtzout=os.path.join(dir_name, hklout),
sg=header["SPACE_GROUP_NUMBER"],
wavelen=header.get("X-RAY_WAVELENGTH","0"),
use_ctruncate=run_ctruncate,
dmin=dmin, dmax=dmax)
if run_xtriage:
print("Running xtriage..")
call("phenix.xtriage", arg=hklout,
stdin=None, stdout=sys.stdout, wdir=dir_name)
else:
print(xds_file, "is anomalous dataset.")
print()
xds2mtz_anom(xds_file,
mtzout=os.path.join(dir_name, hklout),
sg=header["SPACE_GROUP_NUMBER"],
wavelen=header.get("X-RAY_WAVELENGTH","0"),
use_ctruncate=run_ctruncate,
dmin=dmin, dmax=dmax)
if run_xtriage:
print("Running xtriage..")
call("phenix.xtriage", arg=hklout + ' input.xray_data.obs_labels="I(+),SIGI(+),I(-),SIGI(-)"',
stdin=None, stdout=sys.stdout, wdir=dir_name)
# xds2mtz()
if __name__ == "__main__":
parser = optparse.OptionParser(usage="usage: %prog [options] [XDS_ASCII.HKL]")
parser.add_option("--dir","-d", action="store", type=str, dest="dir", default="ccp4",
help="output directory")
parser.add_option("--xtriage","-x", action="store_true", dest="run_xtriage", help="run phenix.xtriage")
parser.add_option("--truncate","-t", action="store_true", dest="run_ctruncate", help="use ctruncate to estimate F")
parser.add_option("--anomalous","-a", action="store_true", dest="anomalous", help="force anomalous")
parser.add_option("--dmin", action="store", dest="dmin", help="high resolution cutoff") # as str
parser.add_option("--dmax", action="store", dest="dmax", help="low resolution cutoff") # as str
(opts, args) = parser.parse_args(sys.argv)
##
# the default input file name is "XDS_ASCII.HKL"
#
if len(args) < 2:
xds_file = os.path.abspath("XDS_ASCII.HKL")
else:
xds_file = os.path.abspath(args[1])
if not os.path.isfile(xds_file):
print("Cannot open", xds_file)
sys.exit(1)
xds2mtz(xds_file, dir_name=opts.dir,
run_xtriage=opts.run_xtriage, run_ctruncate=opts.run_ctruncate,
dmin=opts.dmin, dmax=opts.dmax, force_anomalous=opts.anomalous)