forked from leschzinerlab/Relion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_particleStack_starFile.py
executable file
·105 lines (85 loc) · 3.54 KB
/
bin_particleStack_starFile.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
#!/usr/bin/env python
import shutil
import optparse
from sys import *
import os,sys,re
from optparse import OptionParser
import glob
import subprocess
from os import system
#=========================
def setupParserOptions():
parser = optparse.OptionParser()
parser.set_usage("%prog -i [file].star --bin=[factor] --oext=[output]")
parser.add_option("-i",dest="star",type="string",metavar="FILE",
help="Data file (.star) for RELION particle stack")
parser.add_option("--bin",dest="bin",type="int", metavar="INTEGER",
help="Binning factor for particle data set")
parser.add_option("--apix",dest="apix",type="float", metavar="FLOAT",
help="Pixel size for input particles")
parser.add_option("--oext",dest="oext",type="string", metavar="STRING",
help="Output extension for particle stacks and star files")
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) < 6:
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['star']):
print 'File %s does not exist. Exiting' %(params['star'])
sys.exit()
if os.path.exists('%s_%s.star' %(params['star'][:-5],params['oext'])):
print 'File %s_%s.star already exists. Exiting.' %(params['star'][:-5],params['oext'])
sys.exit()
#==============================
def binStack(params):
#Open star file and get particle stack, X & Y coordinates
f1=open(params['star'],'r')
o1=open('%s_%s.star' %(params['star'][:-5],params['oext']),'w')
for line in f1:
if len(line.split()) < 3:
o1.write(line)
continue
linesplit=line.split()
stack=(line.split()[3].split('@')[1])
number=(line.split()[3].split('@')[0])
xcoord=line.split()[1]
ycoord=line.split()[2]
pixelsize=line.split()[-2]
if params['debug'] is True:
print stack
if os.path.exists('%s_%s.mrcs' %(stack[:-5],params['oext'])):
if params['debug'] is True:
print '%s_%s.mrcs already exists, skipping' %(stack[:-5],params['oext'])
if not os.path.exists('%s_%s.mrcs' %(stack[:-5],params['oext'])):
#relion image handler to bin stack
cmd='relion_image_handler --i %s --o %s_%s.mrcs --angpix %f --rescale_angpix %f' %(stack,stack[:-5],params['oext'],params['apix'],float(params['bin'])*params['apix'])
if params['debug'] is True:
print cmd
subprocess.Popen(cmd,shell=True).wait()
#Write new coordinates, stack name, and new detector size into new star file
seq=("%s"%(number),"@","%s_%s.mrcs"%(stack[:-5],params['oext']))
newstack=''.join(seq)
linesplit[3]=newstack
linesplit[1]=str(round(float(xcoord)/float(params['bin'])))
linesplit[2]=str(round(float(ycoord)/float(params['bin'])))
linesplit[-2]=str(float(pixelsize)*float(params['bin']))
linesplit='\t'.join(linesplit)
o1.write('%s\n' %(linesplit))
if not os.path.exists('%s_%s.star' %(stack[:-5],params['oext'])):
shutil.copy('%s.star' %(stack[:-5]),'%s_%s.star' %(stack[:-5],params['oext']))
f1.close()
#==============================
if __name__ == "__main__":
params=setupParserOptions()
checkConflicts(params)
binStack(params)