-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_tilt_pair_file.py
executable file
·129 lines (108 loc) · 4.36 KB
/
make_tilt_pair_file.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
#!/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 -p <path/to/images> -o <output> --Uext=[untiltExtension] --Text=[tiltExtension]")
parser.add_option("-p",dest="path",type="string",metavar="FILE",
help="Absolute path to the folder containing tilt-mates")
parser.add_option("-o",dest="output",type="string",metavar="FILE",
help="Output file to contain tilt mates")
parser.add_option("--Uext",dest="Uext",type="string", metavar="STRING",
help="Untilted micrograph extension (e.g. '00', 'u')")
parser.add_option("--Text",dest="Text",type="string", metavar="STRING",
help="Tilted micrograph extension (e.g. '01', 't')")
parser.add_option("--leginon",action="store_true",dest="leginon",default=False,
help="Flag if tilt mates came from leginon")
parser.add_option("-d", action="store_true",dest="debug",default=False,
help="debug")
options,args = parser.parse_args()
if len(args) > 1:
parser.error("Unknown commandline options: " +str(args))
if len(sys.argv) < 4:
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 params['path']:
print "\nWarning: no path specified\n"
elif not os.path.exists(params['path']):
print "\nError: path '%s' does not exist\n" % params['path']
sys.exit()
if os.path.exists(params['output']):
print "\nError: output file %s already exists. Exiting\n" %(params['output'])
sys.exit()
if not params['Text']:
print "\nWarning: no tilted micrograph extension specified\n"
sys.exit()
if not params['Uext']:
print "\nWarning: no untilted micrograph extension specified\n"
sys.exit()
#==================
def start(param):
o1 = open(params['output'],'w') #output file
first=1
#Number of untilted micrographs:
numUntilt = len(glob.glob('%s/*%s.mrc' %(param['path'],param['Uext'])))
if param['debug'] is True:
print 'Number of untilted micrographs = %i' %(numUntilt)
#Number of tilted micrographs:
numTilt = len(glob.glob('%s/*%s.mrc' %(param['path'],param['Text'])))
if param['debug'] is True:
print 'Number of tilted micrographs = %i' %(numTilt)
if numTilt != numUntilt:
print 'Warning: Number of untilted and tilted micrographs are unequal. Check output file to confirm they are correctly matched!'
totalMicros = numTilt + numUntilt
tiltedList = glob.glob('%s/*%s.mrc' %(param['path'],param['Text']))
for tilt in sorted(tiltedList):
tiltOrig = tilt
if params['leginon'] is True:
#parse this type of filename: 14jul09b_00009hl_00_00008en_00.mrc
tiltsplit=tilt.split('hl')
if params['debug'] is True:
print tiltsplit
untiltMiddleChange = '_'+param['Uext'][-2:]+'_'+tiltsplit[1][4:]
tilt=tiltsplit[0]+'hl'+untiltMiddleChange
if params['debug'] is True:
print tilt
#Retrieve untilted micrograph pair filename
tiltNoExt = tilt.split('%s'%(param['Text']+'.'))
if params['debug'] is True:
print tiltNoExt
numPartsTilt = len(tiltNoExt)
i = 0
untilt = ''
while i < numPartsTilt-1:
if i == 0:
untilt = untilt+tiltNoExt[i]
i = i + 1
continue
untilt = untilt+tiltNoExt[i]
if params['debug'] is True:
print untilt
i = i + 1
#Check that tilt mates exist
if os.path.exists('%s' %(untilt+'%s.mrc'%(param['Uext']))) is False:
if params['debug'] is True:
print '%s' %(untilt+'%s.mrc'%(param['Uext']))
print 'No tilt mate for %s' %(tilt)
continue
o1.write('%s\t%s\n' %(untilt+'%s.mrc'%(param['Uext']),tiltOrig))
#==============================
if __name__ == "__main__":
params=setupParserOptions()
checkConflicts(params)
start(params)