-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_multi.py
More file actions
445 lines (384 loc) · 13.5 KB
/
process_multi.py
File metadata and controls
445 lines (384 loc) · 13.5 KB
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
#!/usr/bin/python
# -*- coding: latin-1 -*-
# Class that takes in a file name and other optional inputs and converts the scafile to an mtz file
from optparse import OptionParser
import aurigaerrors
import os
import sys
import subprocess
from Queue import Queue
from threading import Thread,Lock
from mrdict import modeldict
import shlex
import tempfile
import time
# Global output directory root prefix
auriga_output_directory_root = None
# Check and see if the global output directory is defined
if os.environ.get("AURIGA_OUTPUT_ROOT"):
auriga_output_directory_root=os.environ.get("AURIGA_OUTPUT_ROOT")
else:
# print "Setting directory for all output to %s" % os.path.abspath(os.curdir)
auriga_output_directory_root=os.path.abspath(os.curdir)
# Class to carry out Scalepack to mtz file conversion
class scalatomtz(object):
"""Class that accepts filename, (optional) spacegroup,cell_dimensions, wavelength and number of residues in the ASU as imput. Runs truncate and outputs the mtz file.
"""
def __init__(self,filename,spag=None,cell_dimensions_tuple=None,wavelength=None,number_of_residues_in_asu=130 ):
"""Assume only filename and infer cell dimensions and spacegroup
"""
self.filename = filename.strip()
self.file = open(self.filename.strip(),"r")
self.spag = self.extract_spag()
self.cell_dimensions_string = self.extract_cell_dimensions_string()
self.wavelength = wavelength
self.number_of_residues_in_asu = number_of_residues_in_asu
self.title = os.path.splitext(os.path.split(self.filename)[1])[0] + "_autoproc"
self.proj_name = os.path.splitext(os.path.split(self.filename)[1])[0].upper()
self.scalepack2mtz_dict = dict(title = self.title ,spag = self.spag , cell = self.cell_dimensions_string , wavelength = self.wavelength , proj_name = self.proj_name)
self.outputdir = os.path.join(auriga_output_directory_root,self.proj_name)
if not os.path.exists(self.outputdir):
os.mkdir(self.outputdir)
def extract_cell_dimensions_string(self):
"""
Arguments:
- `self`:
"""
self.file.seek(0)
self.file.readline()
self.file.readline()
cell_dimensions_string = ",".join(self.file.readline().split()[:-1])
# print "CELL DIM STR" , cell_dimensions_string
return cell_dimensions_string
def extract_spag(self):
self.file.seek(0)
self.file.readline()
self.file.readline()
cell_line = self.file.readline().split()
# print "CELL LINE" , cell_line
return cell_line[-1]
def describe(self):
description = """Scalepack file:{self.filename},{self.spag},{self.cell_dimensions_string}""".format(self=self)
# print description
def mtzoutpath(self):
return os.path.join(self.outputdir,"{self.proj_name}_trnfreeR.mtz".format(self=self))
def create_and_return_runscript_file(self):
scrfile1 = open(os.path.join(self.outputdir,"%s_1.sh" % self.proj_name),"w")
scrfile2 = open(os.path.join(self.outputdir,"%s_2.sh" % self.proj_name),"w")
scrfile3 = open(os.path.join(self.outputdir,"%s_3.sh" % self.proj_name),"w")
scrfile4 = open(os.path.join(self.outputdir,"%s_4.sh" % self.proj_name),"w")
outfile_prefix = os.path.join(self.outputdir,self.proj_name)
# print "PROJ_NAME_OUTPREFIX set to ", outfile_prefix ,"DIRECTORY"
scr1 = """#!/bin/sh
#set -e
# bug # 3192 - run-all examples produce harvest files - well to counteract
# this here set HARVESTHOME to somewhere in $CCP4_SCR
HARVESTHOME=${self.outputdir}
export HARVESTHOME
# from /home/hari/official_ccp4/ccp4-6.1.3/examples/unix/runnable
# SCALEPACK2MTZ
#
# h k l I+ SigI+ I- SigI- were extracted from aucn.na4
# (acentric data only), and put into scalepack format.
# This is simply to illustrate the procedure for getting
# scalepack data into CCP4. I don't really know if it
# is a good example.
#
# (You can use the same procedure whether or not you have
# anomalous data.)
scalepack2mtz hklin {self.filename} hklout {outfile_prefix}_junk1.mtz <<eof
name project {self.proj_name} crystal {self.proj_name} dataset {self.proj_name}
symm {self.spag}
end
eof
""".format(self = self,outfile_prefix=outfile_prefix)
scr2 = """#!/bin/sh
# convert Is to Fs and Ds.
truncate hklin {outfile_prefix}_junk1.mtz hklout {outfile_prefix}_junk2.mtz <<eof
title {self.proj_name} data
truncate yes
nresidue {self.number_of_residues_in_asu}
labout F=FP_{self.proj_name} SIGF=SIGFP_{self.proj_name}
end
eof
""".format(self = self,outfile_prefix=outfile_prefix)
scr4 = """#!/bin/sh
# Add free r to reflections
freerflag hklin {outfile_prefix}_trn.mtz hklout {outfile_prefix}_trnfreeR.mtz <<eof
FREERFRAC 0.05
END
eof""".format(self=self, outfile_prefix = outfile_prefix)
scr3 = """#!/bin/sh
# get correct sort order and asymmetric unit
cad hklin1 {outfile_prefix}_junk2.mtz hklout {outfile_prefix}_trn.mtz <<eof
labi file 1 ALL
sort H K L
end
eof
#""".format(self = self,outfile_prefix=outfile_prefix)
scrfile1.write(scr1)
scrfile2.write(scr2)
scrfile3.write(scr3)
scrfile4.write(scr4)
scrfile1.close()
scrfile2.close()
scrfile3.close()
scrfile4.close()
os.chmod(scrfile1.name,0755)
os.chmod(scrfile2.name,0755)
os.chmod(scrfile3.name,0755)
os.chmod(scrfile4.name,0755)
return (scrfile1.name,scrfile2.name,scrfile3.name,scrfile4.name)
# print scrfile.name
# mutex.acquire()
# subprocess.call([scrfile.name])
# sys.stdout.flush()
# mutex.release()
def report(message):
mutex.acquire()
# print message
sys.stdout.flush()
mutex.release()
def safe_write_script(string,filehandle):
mutex.acquire()
filehandle.write(string)
filehandle.close()
mutex.release()
class ScaToMtzConvertor(Thread):
def __init__(self, in_queue, out_queue):
Thread.__init__(self)
self.in_queue = in_queue
self.out_queue = out_queue
def run(self):
while True:
path = self.in_queue.get()
sys.stdout.flush()
if path is None:
self.out_queue.put(None)
break
# print "GOTPATH" , path
report("Converting %s" % path)
myfile = scalatomtz(path)
scrfiles = myfile.create_and_return_runscript_file()
for index,file in enumerate(scrfiles):
subprocess.call([file])
#print "CALLED%s%s" % (index,file) *5
#report(myfile.describe())
report("Done %s" % path)
self.out_queue.put(myfile.mtzoutpath())
class PhaserRunOrchestrator(Thread):
def __init__(self,in_queue,out_queue):
Thread.__init__(self)
self.in_queue = in_queue
self.out_queue = out_queue
def run(self):
while True:
mtzfile = self.in_queue.get()
if mtzfile is None:
self.out_queue.put(None)
break
runner = PhaserRun(mtzfile)
runner.run()
time.sleep(2)
# This has the root of the output files
# print "PUTTING TO OUT PATH" , runner.outfilepath
self.out_queue.put(runner.outfilepath)
class PhaserRun(object):
"""Class that runs automated molecular replacement using phaser
"""
def __init__(self,inputmtzpath):
"""Class looks at input mtz , gets the right pdb and number of molecules from mrdict.mrdict and then launches a phaser run
"""
self.mycomfile = None
self.outfilepath = None
self.proj_name = None
try:
self.data_tuple = modeldict[os.path.splitext(os.path.split(inputmtzpath)[1])[0].split("_")[0].upper()]
self.inputmtzpath = inputmtzpath
self.pdb_path = self.data_tuple[0]
self.num_copies = self.data_tuple[1]
self.proj_name = "_".join(os.path.splitext(os.path.split(self.inputmtzpath)[1])[0].split("_")[0:2]).upper()
self.outfilepath = os.path.join(os.path.join(auriga_output_directory_root,self.proj_name),self.proj_name)
self.mycomfile = """#!/bin/sh
phaser <<eof
TITLE {self.proj_name} phaser run automatic
MODE MR_AUTO
HKLIn {self.inputmtzpath}
LABIn F=FP_{self.proj_name} SIGF=SIGFP_{self.proj_name}
ENSEmble 1 PDBfile {self.pdb_path} IDENtity 0.99
COMPosition PROTein MW 28853 NUM {self.num_copies} #beta
SEARch ENSEmble 1 NUM {self.num_copies}
ROOT {self.outfilepath} # not the default
eof""".format(self=self)
except KeyError:
pass
def run(self):
comfile = open(os.path.join(os.path.join(auriga_output_directory_root,self.proj_name),self.proj_name + "_phaser_input.sh"),"w")
safe_write_script(self.mycomfile,comfile)
os.chmod(comfile.name,0755)
subprocess.call([comfile.name])
# print "PHASER for {self.proj_name} DONE {self.inputmtzpath} Processed . Files written to {self.outfilepath}".format(self=self)
class Refmac5Runner():
def __init__(self,phaseroutput_root):
self.mtzfile = phaseroutput_root.strip() + ".1.mtz"
self.pdbfile = phaseroutput_root.strip() + ".1.pdb"
self.proj_name = os.path.split(phaseroutput_root)[-1]
self.outputdir = os.path.join(auriga_output_directory_root,self.proj_name)
if os.path.exists(self.mtzfile) and os.path.exists(self.pdbfile):
pass
else:
pass
#print "phaser_run files {self.mtzfile} , {self.pdbfile} not FOUND".format( self = self)
def run(self):
#print self.pdbfile
self.comstring = """#!/bin/csh
#
# Example of refinement by refmac
#
set inmtz={self.outputdir}/{self.proj_name}_trnfreeR.mtz
start:
set name = {self.proj_name}
set last = 1
set cycles = 1
set count = 0
while ($count != $cycles)
echo '*******************************************************************'
echo $count
echo '*******************************************************************'
@ curr = $last + 1
#
# Refmac
#
refmac:
refmac5 \
HKLIN $inmtz \
HKLOUT {self.outputdir}/{self.proj_name}.${{curr}}.mtz \
XYZIN {self.outputdir}/{self.proj_name}.${{last}}.pdb \
XYZOUT {self.outputdir}/{self.proj_name}.${{curr}}.pdb \
<< eor
#
#####Do not add hydrogens
#
MAKE_restraints HYDRogens No
#
#####Do not check correctness of all monomers. Rely on users naming
#####One should be careful in using this option.
#
MAKE CHECk 0
#
####Input mtz labels.
#
LABIN FP=FP_{self.proj_name} SIGFP=SIGFP_{self.proj_name} FREE=FreeR_flag
#
####Output mtz labels
#
LABO FC=FC PHIC=PHIC FWT=2FOFCWT PHWT=PH2FOFCWT -
DELFWT=FOFCWT PHDELWT=PHFOFCWT
#
####Restrained refinement. Reflections between 20 1.5Å resolution will be used
#
REFI TYPE RESTrained RESOLUTION 20 1.10
#
####Use maximum likelihood residual
####Use maximum likelihood residual
#
REFI RESI MLKF
#
####Refine isotropic B values.
#
REFI BREF ISOTropic
#
####Use 0.35 as weighting between X-ray and geometry
#
WEIGHT AUTO
#
####Scaling parameters. Use BULK solvent based on Babinet's principle.
####NB: Unless otherwise SOLVENT NO given contribution of bulk solvent
####based on constant value will be used.
#
SCALe TYPE BULK
#
####Fix Babinet's bulk solvent B value to 200.0
#
SCALe LSSCale FIXBulk 200.0
#
####number of refinement cycles
#
NCYC 2
#
####Monitor only overall statistics
#
MONI MEDIUM
end
eor
if ($status) exit
#
@ last++
@ count++
end
""".format(self=self)
self.mycomfile = open(os.path.join(self.outputdir,self.proj_name + "_refmac5_input.sh"),"w")
safe_write_script(self.comstring,self.mycomfile)
os.chmod(self.mycomfile.name,0755)
subprocess.call([self.mycomfile.name])
class Refmac5RunOrchestrator(Thread):
def __init__(self,in_queue,out_queue):
Thread.__init__(self)
# print "REFMAC Orchestrator"
self.in_queue = in_queue
self.out_queue = out_queue
def run(self):
while True:
my_mtz = self.in_queue.get()
if my_mtz is None:
break
runner = Refmac5Runner(my_mtz)
runner.run()
time.sleep(2)
self.out_queue.put(my_mtz)
scafile_in_queue = Queue()
phaser_in_queue = Queue()
mutex = Lock()
THREAD_COUNT = 1
worker_list = []
for i in range(THREAD_COUNT):
worker = ScaToMtzConvertor(scafile_in_queue, phaser_in_queue)
worker.start()
worker_list.append(worker)
phaser_worker_list = []
refmac5_in_queue = Queue()
if __name__ == '__main__':
sca_listfile = "scafiles.txt"
for i in open(sca_listfile,"r"):
scafile_in_queue.put(i)
for i in range(THREAD_COUNT):
scafile_in_queue.put(None)
for worker in worker_list:
worker.join()
for i in range(THREAD_COUNT):
phaser_run = PhaserRunOrchestrator(phaser_in_queue,refmac5_in_queue)
phaser_run.start()
phaser_worker_list.append(phaser_run)
for phaserrunner in phaser_worker_list:
phaserrunner.join()
refmac5_worker_list = []
refmac5_out_queue = Queue()
for i in range(THREAD_COUNT):
refmac5_run = Refmac5RunOrchestrator(refmac5_in_queue,refmac5_out_queue)
# print "RUNNING REFMAC"
refmac5_run.start()
refmac5_worker_list.append(refmac5_run)
for refmac5_runner in refmac5_worker_list:
refmac5_runner.join()
# with 4
#real 1m17.021s
#user 1m2.530s
#sys 0m5.300s
#with 1
#real 1m19.310s
#user 1m4.490s
#sys 0m5.410s
#with 8
#real 1m13.971s
#user 1m1.020s
#sys 0m5.070s