forked from nogaleslab/Microtubule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTavg_break_v9.py
342 lines (303 loc) · 9.19 KB
/
MTavg_break_v9.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
#!/usr/bin/env python
from EMAN2 import *
import math
import optparse
import os,sys
import glob
import numpy as np
def setupParserOptions():
parser = optparse.OptionParser()
parser.set_usage("%prog -f <EB3clK_5.par>")
parser.add_option("-f",dest="fpar",type="string",metavar="FILE",
help="EB3clK_5.par")
parser.add_option("-s",dest="stack",type="string",metavar="FILE",
help="start.hed")
#parser.add_option("--pf", dest="pf", type="int", metavar="int",
# help="PF")
parser.add_option("--outmrc", action="store_true",dest="outmrc",default=False,
help="output to mrc format")
parser.add_option("--offsetshxy", action="store_true",dest="offsetshxy",default=False,
help="offset shxy")
parser.add_option("--apix", dest="apix", type="float", metavar="FLOAT",
help="pixel size in angstroms")
options,args = parser.parse_args()
if len(args) > 1:
parser.error("Unknown commandline options: " +str(args))
if len(sys.argv) < 2:
parser.print_help()
sys.exit()
params={}
for i in parser.option_list:
if isinstance(i.dest,str):
params[i.dest] = getattr(options,i.dest)
return params
def getMTlist(l1):
l2 = [x for x in l1 if x[0]!='C' and x!='\n']
MTlist_dup = [int(x.split()[7]) for x in l2]
MTlist = list(set(MTlist_dup))
MTlist.sort()
return MTlist
def grepMT(l1,MT):
#f1 = file(fname)
#l1 = f1.readlines()
l2 = [x for x in l1 if x[0]!='C' and x!='\n']
l3 = [x for x in l2 if int(x.split()[7])==MT]
#f1.close()
return l3
def mymedian(mylist):
import copy
n = len(mylist)
if n%2 == 1:
return np.median(mylist)
else:
mylist2 = copy.copy(mylist)
mylist2.sort()
return np.median(mylist2[1:])
def convertSameLine(l_MT,params):
apix = params['apix']
if params['pf'] == 12:
RISE = 10.44/apix
TWIST = -29.86
elif params['pf'] == 13:
RISE = 9.5/apix
TWIST = -27.71
elif params['pf'] == 14:
RISE = 8.85/apix
TWIST = -25.76
else:
print "please specify PF"
sys.exit()
MONOMER = 41.0/apix
n = len(l_MT)
psilist = [float(x.split()[1]) for x in l_MT]
thetalist = [float(x.split()[2]) for x in l_MT]
philist = [float(x.split()[3]) for x in l_MT]
shxlist = [float(x.split()[4]) for x in l_MT]
shylist = [float(x.split()[5]) for x in l_MT]
MT = int(l_MT[0].split()[7])
#if os.path.isfile('picklist.txt'):
# print "will use picklist.txt\r",
# phi_MT,sign_MT = GetPhi(MT,philist)
#else:
# phi_MT = np.median(philist)
# sign_MT = 1
for i in range(n):
dphi = phi_MT - philist[i]
phi_new = philist[i] + TWIST*round(dphi/TWIST)
if abs(phi_new-philist[i]) > 15.0:
#scale = (phi_new-philist[i])/TWIST
scale = round(dphi/TWIST)
shxlist[i] += scale*RISE*math.cos(-psilist[i]/180.0*math.pi)
shylist[i] += scale*RISE*math.sin(-psilist[i]/180.0*math.pi)
philist[i] = phi_new
# shift 1 monomer
if sign_MT < 0:
shxlist[i] += MONOMER*math.cos(-psilist[i]/180.0*math.pi)
shylist[i] += MONOMER*math.sin(-psilist[i]/180.0*math.pi)
loc = findMinShxy(shxlist,shylist)
#print "MT = %d, loc = %d"%(MT,loc)
for i in range(loc+1,n):
shxlist[i],shylist[i] = findClosestShxy(shxlist[i],shylist[i],shxlist[i-1],shylist[i-1],psilist[i],apix)
for i in range(loc-1,-1,-1):
shxlist[i],shylist[i] = findClosestShxy(shxlist[i],shylist[i],shxlist[i+1],shylist[i+1],psilist[i],apix)
l_MT_new = []
#FORMAT = "%7d%8.2f%8.2f%8.2f%8.2f%8.2f%7.0f.%6d%9.1f%9.1f%8.2f%7.2f%8.2f\n"
FORMAT = "%7d%8.2f%8.2f%8.2f%10.2f%10.2f%8d%6d%9.1f%9.1f%8.2f%8.2f%10d%11.4f%8.2f%8.2f\n"
for i in range(n):
t1 = l_MT[i].split()
count = int(t1[0])
#psi = float(t1[1])
#theta = float(t1[2])
#phi = float(t1[3])
#shx = float(t1[4])
#shy = float(t1[5])
psi = psilist[i]
theta = thetalist[i]
phi = philist[i]
shx = shxlist[i]
shy = shylist[i]
mag = float(t1[6])
micro = int(t1[7])
df1 = float(t1[8])
df2 = float(t1[9])
angast = float(t1[10])
occ = float(t1[11])
logP = float(t1[12])
sigma = float(t1[13])
score = float(t1[14])
change = float(t1[15])
l_MT_new.append(FORMAT%(count,psi,theta,phi,shx,shy,mag,micro,df1,df2,angast,occ,logP,sigma,score,change))
return l_MT_new
def getoffset(mylist):
aboffset = 999
offset = 999
for i in mylist:
if abs(i) < aboffset:
aboffset = abs(i)
offset = i
return offset
def MakeMTavg(l_MT,stack,nx,offsetshxy,apix):
start = int(l_MT[0].split()[0])
end = int(l_MT[-1].split()[0])
MTstack = EMData.read_images(stack,range(start-1,end))
MTavg = EMData(nx,nx)
MTavg.to_zero()
n = len(l_MT)
philist = []
thetalist = []
logPlist = []
sigmalist = []
scorelist = []
changelist = []
if offsetshxy:
shxlist = [float(x.split()[4]) for x in l_MT]
shylist = [float(x.split()[5]) for x in l_MT]
shx_offset = getoffset(shxlist)
shy_offset = getoffset(shylist)
else:
shx_offset = 0
shy_offset = 0
for i in range(n):
img = MTstack[i]
t1 = l_MT[i].split()
psi = float(t1[1])
theta = float(t1[2])
phi = float(t1[3])
shx = float(t1[4])-shx_offset
shy = float(t1[5])-shy_offset
mag = float(t1[6])
micro = int(t1[7])
df1 = float(t1[8])
df2 = float(t1[9])
angast = float(t1[10])
#occ = float(t1[11])
logP = float(t1[12])
sigma = float(t1[13])
score = float(t1[14])
change = float(t1[15])
#
philist.append(phi)
thetalist.append(theta)
logPlist.append(logP)
sigmalist.append(sigma)
scorelist.append(score)
changelist.append(change)
# Frealign applies the shifts first and then the rotations.
t1 = Transform()
t1.set_trans(-shx/apix,-shy/apix)
img2 = img.process("xform",{"transform":t1})
t2 = Transform()
t2.set_rotation({"type":"2d","alpha":-psi})
img3 = img2.process("xform",{"transform":t2})
MTavg.add(img3)
phi_MT = mymedian(philist)
theta_MT = mymedian(thetalist)
logP_MT = np.median(logPlist)
sigma_MT = np.median(sigmalist)
score_MT = np.median(scorelist)
change_MT = np.median(changelist)
#FORMAT = "%7d%8.2f%8.2f%8.2f%10.2f%10.2f%8d%6d%9.1f%9.1f%8.2f%8.2f%10d%11.4f%8.2f%8.2f\n"
FORMAT2 = "%8.2f%8.2f%8.2f%10.2f%10.2f%8d%6d%9.1f%9.1f%8.2f%8.2f%10d%11.4f%8.2f%8.2f\n"
l_MTavg = FORMAT2%(0,theta_MT,phi_MT,0,0,mag,micro,df1,df2,angast,100,logP_MT,sigma_MT,score_MT,change_MT)
return MTavg,l_MTavg
def mainloop(params):
apix = params['apix']
f1 = file(params["fpar"])
ll1 = f1.readlines()
l1 = [x for x in ll1 if x[0]!='C' and x!='\n']
MTlist = getMTlist(l1)
#print MTlist
#nMT = len(MTlist)
lastMT = int(l1[-1].split()[7])
#print lastMT
MTparlist = [[] for i in range(lastMT+1)]
for i in l1:
MT = int(i.split()[7])
MTparlist[MT].append(i)
# first calculate the num of particles
count = 0
SEG = 7
for MT in MTlist:
#print "pre-working on MT %d\t\r"%MT,
#l_MT = grepMT(l1,MT)
l_MT = MTparlist[MT]
nptcl = len(l_MT)
if nptcl < 3:
continue
nSEG = nptcl/SEG
if nptcl%SEG > SEG-2 or nSEG == 0:
nSEG += 1
count += nSEG
stack = params["stack"]
im = EMData(stack,0)
nx = im.get_xsize()
del im
print "# of MTs: %d"%count
if params['outmrc']:
print "Allocating space for MTavgstack...\n"
MTavgstack = EMData(nx,nx,count)
MTavgstack.write_image("MTavgstack_break.mrc")
print "Done allocating"
f2 = file("%s_MTavg_break"%params["fpar"],"w")
#FORMAT = "%7d%8.2f%8.2f%8.2f%8.2f%8.2f%8.0f%6d%9.1f%9.1f%8.2f%7.2f%8.2f\n"
FORMAT = "%7d%8.2f%8.2f%8.2f%10.2f%10.2f%8d%6d%9.1f%9.1f%8.2f%8.2f%10d%11.4f%8.2f%8.2f\n"
if params['offsetshxy']:
offsetshxy = 1
else:
offsetshxy = 0
# reset count = 0
count = 0
from progressbar import ProgressBar
pbar = ProgressBar()
for MT in pbar(MTlist):
#print "working on MT %d\t\r"%MT,
#l_MT = grepMT(l1,MT)
l_MT = MTparlist[MT]
nptcl = len(l_MT)
if nptcl < 3:
continue
tmp0 = l_MT[0].split()
mag = float(tmp0[6])
#df1 = float(tmp0[8])
#df2 = float(tmp0[9])
angast = float(tmp0[10])
pres = float(tmp0[11])
nSEG = nptcl/SEG
ttt2 = nptcl%SEG
if nptcl%SEG > SEG-2 or nSEG == 0:
nSEG += 1
for i in range(nSEG):
start = i*SEG
#end = (i+1)*SEG
if i == nSEG-1:
end = nptcl
else:
end = (i+1)*SEG
l_MT_SEG = l_MT[start:end]
#l_MT_SEG_new = convertSameLine(l_MT_SEG,params)
MTavg,l_MTavg = MakeMTavg(l_MT_SEG,stack,nx,offsetshxy,apix)
if params['outmrc']:
region = Region(0,0,count,nx,nx,1)
MTavg.write_image("MTavgstack_break.mrc",0,EMUtil.get_image_ext_type("mrc"), False, region, EMUtil.EMDataType.EM_FLOAT, True)
else:
MTavg.write_image("MTavgstack_break.hed",-1)
f2.write("%7d%s"%(count+1,l_MTavg))
count += 1
f1.close()
f2.close()
if __name__ == "__main__":
params = setupParserOptions()
if params['outmrc']:
try:
os.remove("MTavgstack_break.mrc")
except:
pass
else:
try:
os.remove("MTavgstack_break.hed")
os.remove("MTavgstack_break.img")
except:
pass
#for i in glob.glob("MTavgstack_break.*"):
# os.remove(i)
mainloop(params)