Last active
March 23, 2017 14:42
-
-
Save netheril96/a98f4599abbc11c244bfe38956d56145 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import math | |
import sys | |
import re | |
def main(): | |
fSTAR = sys.argv[1] | |
header_dict = {} | |
with open(fSTAR, 'r') as f: | |
for num, line in enumerate(f): | |
sline = line.strip() | |
if not sline or sline == 'data_' or sline == 'loop_': | |
continue | |
match = re.match(r'_rln(\w+)\s+#(\d+)', sline) | |
if match: | |
header_dict[match.group(1).lower()] = int(match.group(2)) - 1 | |
continue | |
sp = sline.split() | |
try: | |
volt = float(sp[header_dict['voltage']]) * 1000.0 | |
dU = float(sp[header_dict['defocusu']]) | |
dV = float(sp[header_dict['defocusv']]) | |
dT = float(sp[header_dict['defocusangle']]) * (math.pi / 180) | |
c = float(sp[header_dict['sphericalaberration']]) * 1e7 | |
aC = float(sp[header_dict['amplitudecontrast']]) | |
iN = sp[header_dict['imagename']] | |
mN = sp[header_dict['micrographname']] | |
gI = int(sp[header_dict['groupnumber']]) | |
except (ValueError, IndexError): | |
sys.stderr.write( | |
'Warning: skipping line #{} ({}) that cannot be parsed\n'.format(num + 1, sline)) | |
continue | |
except KeyError as e: | |
sys.stderr.write( | |
'Header does not include {}. This is an invalid star file\n'.format(str(e))) | |
sys.exit(2) | |
print '{volt:12.6f} \ | |
{dU:12.6f} \ | |
{dV:12.6f} \ | |
{dT:12.6f} \ | |
{c:12.6f} \ | |
{aC:12.6f} \ | |
{phaseShift:12.6f} \ | |
{iN} \ | |
{mN} \ | |
{coordX:12.6f}, \ | |
{coordY:12.6f}, \ | |
{gI}, \ | |
{classI} \ | |
{quat0:12.6f}, \ | |
{quat1:12.6f}, \ | |
{quat2:12.6f}, \ | |
{quat3:12.6f}, \ | |
{transX:12.6f}, \ | |
{transY:12.6f}, \ | |
{score:12.6f}'.format(volt=volt, | |
dU=dU, | |
dV=dV, | |
dT=dT, | |
c=c, | |
aC=aC, | |
phaseShift=0, | |
iN=iN, | |
mN=mN, | |
coordX=0, | |
coordY=0, | |
gI=gI, | |
classI=0, | |
quat0=0, | |
quat1=0, | |
quat2=0, | |
quat3=0, | |
transX=0, | |
transY=0, | |
score=0) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment