-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_tomo_reconstruct.py
284 lines (252 loc) · 10.4 KB
/
batch_tomo_reconstruct.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Dec 8 2021
Script to run batch imod through the command line using a template from one reconstructed tomogram
TODO: NO TILT SIRT (not really important)
TODO: implement multiprocessing for cluster submission
This is used specifically for K3 in McGill. Imod 4.11.8
@author: Huy Bui, McGill
"""
import argparse, os, glob, shutil
from multiprocessing import Pool
def run_eraser(baseName, tempCont):
''' Write eraser and run'''
outCom = open(operation + '.com', 'w')
for line in tempCont:
if line.startswith('InputFile'):
outCom.write('InputFile\t{:s}.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_fixed.mrc\n'.format(baseName))
elif line.startswith('PointModel'):
outCom.write('PointModel\t{:s}_peak.mod\n'.format(baseName))
#elif line.startswith('BinByFactor'):
# outCom.write('BinByFactor\t{:d}.xf\n'.format(binFactor)
else:
outCom.write(line)
outCom.close()
def run_prenewst(baseName, tempCont):
'''Write prenewst and run'''
outCom = open(operation + '.com', 'w')
for line in tempCont:
if line.startswith('InputFile'):
outCom.write('InputFile\t{:s}.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_preali.mrc\n'.format(baseName))
elif line.startswith('TransformFile'):
outCom.write('TransformFile\t{:s}.prexg\n'.format(baseName))
elif line.endswith('.prexf\n'):
outCom.write('{:s}.prexf\n'.format(baseName))
elif line.endswith('.prexg\n'):
outCom.write('{:s}.prexg\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_newst(baseName, tempCont):
'''Write newst and run'''
outCom = open(operation + '.com', 'w')
for line in tempCont:
if line.startswith('InputFile'):
outCom.write('InputFile\t{:s}.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_ali.mrc\n'.format(baseName))
elif line.startswith('TransformFile'):
outCom.write('TransformFile\t{:s}.xf\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_align(baseName, tempCont):
'''Write align and run'''
outCom = open(operation + '.com', 'w')
for line in tempCont:
if line.startswith('ModelFile'):
outCom.write('ModelFile\t{:s}.fid\n'.format(baseName))
elif line.startswith('ImageFile'):
outCom.write('ImageFile\t{:s}_preali.mrc\n'.format(baseName))
elif line.startswith('OutputModelFile'):
outCom.write('OutputModelFile\t{:s}.3dmod\n'.format(baseName))
elif line.startswith('OutputResidualFile'):
outCom.write('OutputResidualFile\t{:s}.resid\n'.format(baseName))
elif line.startswith('OutputFidXYZFile'):
outCom.write('OutputFidXYZFile\t{:s}fid.xyz\n'.format(baseName))
elif line.startswith('OutputTiltFile'):
outCom.write('OutputTiltFile\t{:s}.tlt\n'.format(baseName))
elif line.startswith('OutputXAxisTiltFile'):
outCom.write('OutputXAxisTiltFile\t{:s}.xtilt\n'.format(baseName))
elif line.startswith('OutputTransformFile'):
outCom.write('OutputTransformFile\t{:s}.tltxf\n'.format(baseName))
elif line.startswith('OutputFilledInModel'):
outCom.write('OutputFilledInModel\t{:s}_nogap.fid\n'.format(baseName))
elif line.startswith('TiltFile'):
outCom.write('TiltFile\t{:s}.rawtlt\n'.format(baseName))
elif line.startswith('InputFile1'):
outCom.write('InputFile1\t{:s}.prexg\n'.format(baseName))
elif line.startswith('InputFile2'):
outCom.write('InputFile2\t{:s}.tltxf\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_fid.xf\n'.format(baseName))
elif line.startswith('$b3dcopy -p'):
outCom.write('$b3dcopy -p {:s}_fid.xf {:s}.xf\n'.format(baseName, baseName))
outCom.write('$b3dcopy -p {:s}.tlt {:s}_fid.tlt\n'.format(baseName, baseName))
elif 'patch2imod' in line:
outCom.write('$if (-e {:s}.resid) patch2imod -s 10 {:s}.resid {:s}.resmod\n'.format(baseName, baseName, baseName))
else:
outCom.write(line)
outCom.close()
def run_tilt(baseName, tempCont):
outCom = open('tilt.com', 'w')
for line in tempCont:
if line.startswith('InputProjections'):
outCom.write('InputProjections\t{:s}_ali.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_full_rec.mrc\n'.format(baseName))
elif line.startswith('TILTFILE'):
outCom.write('TILTFILE\t{:s}.tlt\n'.format(baseName))
#elif line.startswith('THICKNESS'):
# outCom.write('THICKNESS\t{:s}\n'.format(thickness)
elif line.startswith('XTILTFILE'):
outCom.write('XTILTFILE\t{:s}.xtilt\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
#def run_tilt_for_sirt(baseName, tempCont):
# Not doing anything yet
def run_newst_3dfind(baseName, tempCont):
outCom = open('newst_3dfind.com', 'w')
for line in tempCont:
if line.startswith('InputFile'):
outCom.write('InputFile\t{:s}.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_3dfind_ali.mrc\n'.format(baseName))
elif line.startswith('TransformFile'):
outCom.write('TransformFile\t{:s}.xf\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_tilt_3dfind(baseName, tempCont):
outCom = open('tilt_3dfind.com', 'w')
for line in tempCont:
if line.startswith('InputProjections'):
outCom.write('InputProjections\t{:s}_3dfind_ali.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_3dfind_rec.mrc\n'.format(baseName))
elif line.startswith('TILTFILE'):
outCom.write('TILTFILE\t{:s}.tlt\n'.format(baseName))
#elif line.startswith('THICKNESS'):
# outCom.write('THICKNESS\t{:s}\n'.format(thickness)
elif line.startswith('XTILTFILE'):
outCom.write('XTILTFILE\t{:s}.xtilt\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_findbeads3d(baseName, tempCont):
outCom = open('findbeads3d.com', 'w')
for line in tempCont:
if line.startswith('InputFile'):
outCom.write('InputFile\t{:s}_3dfind_rec.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_3dfind.mod\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_tilt_3dfind_reproject(baseName, tempCont):
outCom = open('tilt_3dfind_reproject.com', 'w')
for line in tempCont:
if line.startswith('InputProjections'):
outCom.write('InputProjections\t{:s}_3dfind_ali.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_erase.fid\n'.format(baseName))
elif line.startswith('TILTFILE'):
outCom.write('TILTFILE\t{:s}.tlt\n'.format(baseName))
elif line.startswith('XTILTFILE'):
outCom.write('XTILTFILE\t{:s}.xtilt\n'.format(baseName))
elif line.startswith('ProjectModel'):
outCom.write('ProjectModel\t{:s}_3dfind.mod\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_golderaser(baseName, tempCont):
outCom = open('golderaser.com', 'w')
for line in tempCont:
if line.startswith('InputFile'):
outCom.write('InputFile\t{:s}_ali.mrc\n'.format(baseName))
elif line.startswith('OutputFile'):
outCom.write('OutputFile\t{:s}_erase_ali.mrc\n'.format(baseName))
elif line.startswith('ModelFile'):
outCom.write('ModelFile\t{:s}_erase.fid\n'.format(baseName))
else:
outCom.write(line)
outCom.close()
def run_trimvol(baseName, tempCont):
outCom = open('trimvol.com', 'w')
outCom.write('$trimvol -f -rx {:s}_full_rec.mrc {:s}_rec.mrc'.format(baseName, baseName))
outCom.close()
if __name__=='__main__':
parser = argparse.ArgumentParser(description='Batch tomo reconstruct using IMOD')
parser.add_argument('--i', help='Input Tilt Series',required=True)
parser.add_argument('--operation', help='Imod operation (newst/tilt/newst_3dfind/tilt_3dfind/findbeads3d/tilt_3dfind_reproject/golderaser/trimvol)',required=True)
#parser.add_argument('--bin', help='Bin factor',required=True)
parser.add_argument('--template', help='Template file from 1 reconstruction',required=True)
parser.add_argument('--noproc', help='Number of processors',required=False, default=1)
#parser.add_argument('--xsize', help='X size of image',required=True)
#parser.add_argument('--ysize', help='Y size of image',required=True)
print('Only tested with 4.11 only')
args = parser.parse_args()
tempfile = open(args.template, 'r')
tempCont = tempfile.readlines()
operation = args.operation
currentDir = os.getcwd()
tsList = glob.glob(args.i)
for tiltseries in tsList:
tsName = os.path.basename(tiltseries)
if not tiltseries.endswith('.mrc'):
print('---> Processing ' + tiltseries + ': ERROR! Only mrc file supported')
break
baseName = tsName.replace('.mrc','')
tsPath = os.path.dirname(tiltseries)
print('---> Processing ' + tiltseries + ' ...')
print('Change dir to ' + tsPath)
os.chdir(tsPath)
if operation == 'eraser':
run_eraser(baseName, tempCont)
os.system('submfg eraser.com')
shutil.move(baseName + '.mrc', baseName + '_orig.mrc')
print('mv ' + baseName + '_fixed.mrc ' + baseName + '.mrc')
shutil.move(baseName + '_fixed.mrc', baseName + '.mrc')
elif operation == 'prenewst':
run_prenewst(baseName, tempCont)
os.system('submfg prenewst.com')
elif operation == 'align':
run_align(baseName, tempCont)
os.system('submfg align.com')
elif operation == 'newst':
run_newst(baseName, tempCont)
os.system('submfg newst.com')
elif operation == 'tilt':
run_tilt(baseName, tempCont)
os.system('submfg tilt.com')
elif operation == 'newst_3dfind':
run_newst_3dfind(baseName, tempCont)
os.system('submfg newst_3dfind.com')
elif operation == 'tilt_3dfind':
run_tilt_3dfind(baseName, tempCont)
os.system('submfg tilt_3dfind.com')
elif operation == 'findbeads3d':
run_findbeads3d(baseName, tempCont)
os.system('submfg findbeads3d.com')
elif operation == 'tilt_3dfind_reproject':
run_tilt_3dfind_reproject(baseName, tempCont)
os.system('submfg tilt_3dfind_reproject.com')
elif operation == 'golderaser':
run_golderaser(baseName, tempCont)
os.system('submfg golderaser.com')
shutil.move(baseName + '_ali.mrc', baseName + '_ali.mrc~')
print('mv ' + baseName + '_erase_ali.mrc ' + baseName + '_ali.mrc')
shutil.move(baseName + '_erase_ali.mrc', baseName + '_ali.mrc')
elif operation == 'trimvol':
run_trimvol(baseName, tempCont)
os.system('submfg trimvol.com')
else:
print('Unknown option')
exit(1)
os.chdir(currentDir)