-
Notifications
You must be signed in to change notification settings - Fork 11
/
scale_relion_starFile_between_pixelSizes.py
executable file
·94 lines (77 loc) · 3.25 KB
/
scale_relion_starFile_between_pixelSizes.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
#!/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
import shutil
#=========================
def setupParserOptions():
parser = optparse.OptionParser()
parser.set_usage("%prog -i <particles.star file> -o <output name>")
parser.add_option("-i",dest="star",type="string",metavar="FILE",
help="_data.star file containing per-particle entries from auto-refine")
parser.add_option("-o",dest="output",type="string",metavar="FILE",
help="Output file name with updated particle info")
parser.add_option("--newparticlename",dest="particlepath",type="string",metavar="STRING",
help="Suffix for new pixel size data")
parser.add_option("--oldpix",dest="oldpix",type="float",metavar="FLOAT",
help="Old pixel size")
parser.add_option("--newpix",dest="newpix",type="float",metavar="FLOAT",
help="New pixel size")
parser.add_option("--newdetectorpix",dest="newdetector",type="float",metavar="FLOAT",
help="New detector pixel size. Must match up with mag & pixel size.")
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 params['star']:
print "\nWarning: no .star file specified\n"
elif not os.path.exists(params['star']):
print "\nError: star file '%s' does not exist\n" % params['star']
sys.exit()
if os.path.exists(params['output']):
print "\nError: output file %s already exists, exiting.\n" %(params['output'])
sys.exit()
#==============================
def updateshifts(params):
f1 = open(params['star'],'r')
if os.path.exists(params['output']):
print "\nOutput file %s already exists, updating information.\n" %(params['output'])
shutil.move(params['output'],'tmpFile.txt')
f1 = open('tmpFile.txt','r')
outfile = open(params['output'],'w')
for line in f1:
if len(line) > 40:
l = line.split()
l[5] = str(params['newdetector'])
l[15] = str((float(l[15])*params['oldpix'])/params['newpix'])
l[16] = str((float(l[16])*params['oldpix'])/params['newpix'])
l[9] = l[9][:-5]+"%s"%(params['particlepath'])
line = " ".join(l)
line = line+"\n"
outfile.write(line)
f1.close()
if os.path.exists('tmpFile.txt'):
os.remove('tmpFile.txt')
#==============================
if __name__ == "__main__":
params=setupParserOptions()
checkConflicts(params)
if params['oldpix'] and params['newpix']:
updateshifts(params)