-
Notifications
You must be signed in to change notification settings - Fork 1
/
CTFFIND4_to_STAR.py
executable file
·154 lines (126 loc) · 5.03 KB
/
CTFFIND4_to_STAR.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
#!/usr/bin/env python
import optparse
from sys import *
import os,sys,re
from optparse import OptionParser
import glob
import subprocess
from os import system
import linecache
import time
#=========================
def setupParserOptions():
parser = optparse.OptionParser()
parser.set_usage("%prog -i <ctf from appion> --path=<path to micros> --appion=<appion base name to remove> --cs=<cs> --kev=<kev> ")
parser.add_option("-i",dest="ctf",type="string",metavar="FILE",
help="CTFFIND4 output file 'ctf_param.txt'")
parser.add_option("--path",dest="folder",type="string",metavar="STRING",
help="Relative path to micrographs that Relion will use (e.g. 'Micrographs')")
parser.add_option("--mag",dest="mag",type="int",metavar="INT",
help="Nominal magnification of microscope")
parser.add_option("--detectorpix",dest="detector",type="float",metavar="float",
help="Physical detector pixel size. Note: Detector pix / mag = apix")
parser.add_option("--reslim",dest="reslim",type="float",metavar="float",
help="Resolution fit limit for micrograph")
parser.add_option("-d", action="store_true",dest="debug",default=False,
help="debug")
options,args = parser.parse_args()
if len(args) > 0:
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 checkConflicts(params):
if not os.path.exists(params['ctf']):
print "\nError: CTF file '%s' does not exist\n" % params['CTF']
sys.exit()
if os.path.exists('all_micrographs_ctf.star'):
print "\nError: all_micrograhps_ctf.star already exists, exiting.\n"
sys.exit()
#===============================
def convertToRelionCTF(params):
relionOut = writeRelionHeader()
out = open('all_micrographs_ctf.star','w')
ctf = open(params['ctf'],'r')
for line in ctf:
l = line.split()
if l[-1] == 'ResLimit':
'''2.7,200,1.35,0.07,#cs,ht,apix,ampcontrast#Micro DF1 DF2 Astig ResLimit'''
ampcontrast=float(line.split(',')[3])
cs=float(line.split(',')[0])
kev=float(line.split(',')[1])
continue
if float(l[-1]) > params['reslim']:
if params['debug'] is True:
print 'skipping micrograph %s bc res limit is %f' %(l[0],float(l[-1]))
continue
micro=l[0]
#Prepare micrograph name
microname = '%s/' %(params['folder'])+micro
if params['debug'] is True:
print microname
#Get defocus information
df1 = float(l[1])
df2 = float(l[2])
astig = float(l[3])
crosscorr = float(l[4])
relionOut+='%s %.6f %.6f %.6f %.6f %.6f %.6f %.6g %.6f %.6f\n' %(microname,df1,df2,astig,kev,cs,ampcontrast,params['mag'],params['detector'],crosscorr)
ctflog = '%s/' %(params['folder'])+micro[:-4]+'_ctffind3.log'
#Check if new ctf log file exists
if os.path.exists(ctflog):
print '%s already exists. Exiting.' %(ctflog)
sys.exit()
#Open new ctf log file
ctf='\n'
ctf+=' CTF DETERMINATION, V3.5 (9-Mar-2013)\n'
ctf+=' Distributed under the GNU General Public License (GPL)\n'
ctf+='\n'
ctf+=' Parallel processing: NCPUS = 4\n'
ctf+='\n'
ctf+=' Input image file name\n'
ctf+='%s\n' %(microname)
ctf+='\n'
ctf+='\n'
ctf+=' Output diagnostic file name\n'
ctf+='%s.ctf\n'%(microname[:-4])
ctf+='\n'
ctf+='\n'
ctf+=' CS[mm], HT[kV], AmpCnst, XMAG, DStep[um]\n'
ctf+=' %.1f %.1f %.2f %.1f %.3f\n' %(cs,kev,ampcontrast,params['mag'],params['detector'])
ctf+='\n'
ctf+='\n'
ctf+=' DFMID1 DFMID2 ANGAST CC\n'
ctf+='\n'
ctf+=' %.2f\t%.2f\t%.2f\t%.5f\tFinal Values\n' %(df1,df2,astig,crosscorr)
outctf = open(ctflog,'w')
outctf.write(ctf)
outctf.close()
out.write(relionOut)
#================================
def writeRelionHeader():
relion='\n'
relion+='data_\n'
relion+='\n'
relion+='loop_\n'
relion+='_rlnMicrographName #1\n'
relion+='_rlnDefocusU #2\n'
relion+='_rlnDefocusV #3\n'
relion+='_rlnDefocusAngle #4\n'
relion+='_rlnVoltage #5\n'
relion+='_rlnSphericalAberration #6\n'
relion+='_rlnAmplitudeContrast #7\n'
relion+='_rlnMagnification #8\n'
relion+='_rlnDetectorPixelSize #9\n'
relion+='_rlnCtfFigureOfMerit #10\n'
return relion
#==============================
if __name__ == "__main__":
params=setupParserOptions()
checkConflicts(params)
convertToRelionCTF(params)