-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.py
executable file
·191 lines (134 loc) · 4.8 KB
/
plane.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python
import sys
import textwrap
import numpy as np
import argparse as arg
from os.path import splitext
from scipy.optimize import minimize
def options():
'''Defines the options of the script.'''
parser = arg.ArgumentParser(description='Fits a plane to a molecule',
formatter_class=arg.ArgumentDefaultsHelpFormatter)
# Optional arguments
parser.add_argument('-f', '--filename', type=str, required=True,
help='''File containing the molecular geometry in G09 format.''')
parser.add_argument('-s', '--sel', nargs='+', type=str, default=None,
help='''Atom selection for the plane calculation.''')
args = parser.parse_args()
return args
def process_selection(string):
string = ','.join(string).replace(',,',',')
try:
f = open(string, 'r')
string = f.readlines()
f.close()
string = ','.join(string).replace(',,',',')
string = string.replace(',', ' ')
string = map(lambda x: x - 1, extend_compact_list(string))
except IOError:
string = string.replace(',', ' ')
string = map(lambda x: x - 1, extend_compact_list(string))
return string
def extend_compact_list(idxs):
extended = []
# Uncomment this line if idxs is a string and not a list
idxs = idxs.split()
for idx in idxs:
to_extend = idx.split('-')
if len(to_extend) > 1:
sel = map(int, to_extend)
extended += range(sel[0],sel[1]+1,1)
else:
extended.append(int(idx))
return extended
def format_selection(intlist):
s = ''
for i in intlist:
s += '%2d ' % (i + 1)
return s
def parse_log(logfile):
structure = []
opt = 0
with open(logfile, 'r') as f:
for line in f:
if "Optimization completed" in line:
opt = 1
# It could be Standard or Input orientation
# depending on nosymm keyword
if "orientation:" in line and opt == 1:
# Skip the head-of-table lines (4 lines)
next(f)
next(f)
next(f)
next(f)
curr_line = next(f).split()
while len(curr_line) == 6:
atom_n = int(curr_line[1])
atom_x = float(curr_line[3])
atom_y = float(curr_line[4])
atom_z = float(curr_line[5])
structure.append([atom_n, atom_x, atom_y, atom_z])
curr_line = next(f).split()
opt = 0
return np.array(structure)
def plane(x, y, parms):
a = parms[0]
b = parms[1]
c = parms[2]
z = a*x + b*y + c
return z
def error(parms, x, y, z):
target = plane(x, y, parms)
err = np.sqrt(np.mean((target - z)**2))
return err
if __name__ == "__main__":
args = options()
# Read Data
outpref = splitext(args.filename)[0]
struct = parse_log(args.filename)
with open("%s.xyz" % outpref, 'w') as f:
f.write("%d\n" % len(struct))
f.write("\n")
np.savetxt(f, struct, fmt="%3d %14.8f %14.8f %14.8f")
if args.sel:
idxs = process_selection(args.sel)
points = struct[idxs,1:]
else:
points = struct[:,1:]
x = points[:,0]
y = points[:,1]
z = points[:,2]
# Calculate the Plane
guess = np.zeros(3)
res = minimize(error, guess, args=(x, y, z))
a = res.x[0]
b = res.x[1]
c = res.x[2]
err = res.fun
print
print("--------- Results of the Fitting Procedure ---------")
print
print("Input: %s" % args.filename)
if args.sel:
print("Selection:\n%s" % textwrap.fill(format_selection(idxs), width=55))
print
print("RMSD: %13.7f" % err)
print("Equation: z = %13.7f*x + %13.7f*y + %13.7f" % (a, b, c))
print
print("Saving the vmd script %s.vmd" % outpref)
print
# Generate a plottable surface
point = np.array([0.0, 0.0, c])
normal = np.array(np.cross([1,0,a], [0,1,b]))
d = -point.dot(normal)
xx, yy = np.meshgrid([x.min(), x.max()], [y.min(), y.max()])
zz = (-normal[0] * xx - normal[1] * yy - d) * 1. /normal[2]
# Save vmd script plotting the plane and the molecule
with open("%s.vmd" % outpref, 'w') as f:
f.write("mol new %s.xyz type xyz\n" % outpref)
f.write("draw material Transparent\n")
f.write("draw color black\n")
f.write("draw triangle {%7.4f %7.4f %7.4f} {%7.4f %7.4f %7.4f} {%7.4f %7.4f %7.4f}\n" %
(xx[0][0], yy[0][0], zz[0][0], xx[0][1], yy[0][1], zz[0][1], xx[1][0], yy[1][0], zz[1][0]))
f.write("draw triangle {%7.4f %7.4f %7.4f} {%7.4f %7.4f %7.4f} {%7.4f %7.4f %7.4f}\n" %
(xx[1][1], yy[1][1], zz[1][1], xx[1][0], yy[1][0], zz[1][0], xx[0][1], yy[0][1], zz[0][1]))