-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpm2txt.py
89 lines (77 loc) · 2.46 KB
/
xpm2txt.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
#!/usr/bin/env python
import sys
"""
Utility tool (python2) to convert xpm files generated by GROMACS to a 3-column text file.
"""
USAGE = "USAGE: xpm2txt.py -f <input xpm file> -o <output txt file> [-s]\n"
USAGE+= "Options:\n"
USAGE+= "\t-s\t(int)\tSorts the output by a given column"
USAGE+= "\n" # always keep this line
# Parse arguments
read_input, read_output, sort = False, False, False
xpm_file, out_file, column_sort = None, None, None
for arg in sys.argv[1:]:
if read_input:
read_input = False
xpm_file = arg
elif read_output:
read_output = False
out_file = arg
elif sort:
sort = False
column_sort = int(arg)
if arg[0] == "-":
if arg == "-f":
read_input = True
continue
elif arg == "-o":
read_output = True
continue
elif arg == "-s":
sort = True
else:
print USAGE
sys.stderr.write('ERROR: Option not recognized: %s\n' %arg)
sys.exit(1)
if not xpm_file:
print USAGE
sys.stderr.write('ERROR: You forgot to provide an input file.\n')
sys.exit(1)
if not out_file:
out_file = "out.txt"
# Parse XPM file
xpm_handle = open(xpm_file)
xpm_data = []
x_axis, y_axis = [], []
letter_to_value = {}
for line in xpm_handle:
if line.startswith("/* x-axis"):
x_axis = map(float, line.split()[2:-2]) # We trim the last value
if line.startswith("/* y-axis"):
y_axis = map(float, line.split()[2:-2]) # We trim the last value
if line.startswith('"') and x_axis and y_axis: # Read data
xpm_data.insert(0, line.strip().strip(',')[1:-1])
if line.startswith('"') and len(line.split()) > 4:
letter = line.split()[0][1:]
value = float(line.split()[-2][1:-1])
letter_to_value[letter] = value
xpm_handle.close()
# Match x/y/data
txt_values = []
for y_index, data_value in enumerate(xpm_data):
y_value = y_axis[y_index]
for x_index, x_value in enumerate(x_axis):
txt_values.append([x_value, y_value, letter_to_value[data_value[x_index]]])
# Apply sorting if requested
if column_sort:
try:
txt_values.sort(key=lambda x: x[column_sort-1])
except IndexError:
print USAGE
sys.stderr.write('ERROR: Column not found (%s)\n' %(column_sort))
sys.exit(1)
# Print to file
out_handle = open(out_file, 'w')
for x, y, z in txt_values:
out_handle.write("%3.5f\t%3.5f\t%3.5f\n" %(x,y,z))
out_handle.close()