-
Notifications
You must be signed in to change notification settings - Fork 4
/
ConnGO_xyz2sdf.py
233 lines (207 loc) · 10.8 KB
/
ConnGO_xyz2sdf.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os, sys
import linecache
import numpy as np
current_dir_path = os.getcwd()
control_file = current_dir_path + "/control.inp"
input_sdf = sys.argv[1]
input_xyz = sys.argv[2]
thresh = sys.argv[3]
output_sdf = sys.argv[4]
output_com = sys.argv[5]
rmsd_name = sys.argv[6]
charge = sys.argv[7]
multiplicity = sys.argv[8]
mem = sys.argv[9]
nproc = sys.argv[10]
#def convert_sdf(input_sdf, input_xyz, thresh, output_sdf, output_com, rmsd_name):
settings = [ "long_bond", "thresh_MPAD", "thresh_MaxAD"]
### READING CONTROL FILE
value = {}
with open(control_file, "r") as ctrl:
u = 0
while u < len((settings)):
for line in ctrl:
if settings[u] in line:
val_line = line.split(":")
if "#" in val_line[1]:
val_split = val_line[1].split("#")
value[settings[u]] = val_split[0].strip()
else:
value[settings[u]] = val_line[1].strip()
break
u = u +1
with open(input_sdf, "r") as i_sdf:
num_lines_isdf = sum(1 for line_isdf in i_sdf)
P = linecache.getline(input_sdf, 4)
P_lsp = P.split()
num_atoms_sdf = int(P_lsp[0])
count_ext_sdf = int(P_lsp[1])
R1=np.zeros((num_atoms_sdf,3))
R3=np.zeros((num_atoms_sdf,3))
conn_arr=np.zeros((count_ext_sdf,3),dtype=int)
dist1 = np.zeros(count_ext_sdf)
dist2 = np.zeros(count_ext_sdf)
Sy = []
with open(output_sdf, "w") as new_sdf_f:
for new_sdf_l in range(1,5):
pl = linecache.getline(input_sdf, new_sdf_l)
new_sdf_f.write(pl)
S1 = []
for l_isdf in range(5, num_atoms_sdf+5):
l1 = linecache.getline(input_sdf, l_isdf)
lsp1 = l1.split()
ster_l = l1.split(" ",14)
S1.append(ster_l[14].strip())
cart1=np.zeros(3)
cart1 = np.asarray([ float(lsp1[0]) , float(lsp1[1]), float(lsp1[2]) ], dtype=float)
R1[l_isdf-5][0:3]=cart1
Sy.append(lsp1[3])
i = 0
C1= []
C2= []
#for l_isdf1 in range(5+ num_atoms_sdf, 5+num_atoms_sdf +count_ext_sdf):
for l_isdf1 in range(5+ num_atoms_sdf, num_lines_isdf-1):
l2 = linecache.getline(input_sdf, l_isdf1)
C1.append(l2.split())
C2.append(l2.strip())
#C1.append(l2.strip())
lsp2 = l2.split()
conn1 = [ int(lsp2[0]), int(lsp2[1]) , int(lsp2[2]) ]
conn_arr[i][0:3]=conn1
#print(i,conn1)
R12=np.zeros(3)
R12=R1[conn1[0]-1][0:3] - R1[conn1[1]-1][0:3]
dR12=R12[0]**2 + R12[1]**2 + R12[2]**2
dist1[i]= round(np.sqrt(dR12),4)
i = i + 1
with open(output_com,"w") as new_com_f:
new_com_f.write("%mem="+mem+"\n%nproc="+nproc+"\n")
new_com_f.write("#P METHOD Geom=Connectivity\n\n")
new_com_f.write(output_com + "\n\n")
new_com_f.write(charge + " "+ multiplicity +"\n")
with open(rmsd_name, "w") as new_rmsd_file:
with open(input_xyz, "r") as i_xyz:
num_lines_ixyz = sum(1 for line_ixyz in i_xyz)
num_atoms_ixyz = linecache.getline(input_xyz, 1)
with open(output_sdf,"a") as new_sdf_f:
for l_i in range(3, 3+int(num_atoms_ixyz)):
li3 = linecache.getline(input_xyz, l_i)
new_com_f.write(li3)
lisp3 = li3.split()
sdf_newline1 = " "+ lisp3[1] + " " +lisp3[2] + " " + lisp3[3] + " " + Sy[l_i-3] + " " + S1[l_i-3] + "\n"
new_sdf_f.write(sdf_newline1)
cart2 = np.zeros(3)
cart2 = np.asarray( [ float(lisp3[1]) , float(lisp3[2]), float(lisp3[3]) ], dtype=float )
R3[l_i-3][0:3] = cart2
for tmp in range(len(C1)):
#new_sdf_f.write(C1[tmp] + "\n")
new_sdf_f.write(C2[tmp] + "\n")
new_sdf_f.write("M END\n$$$$")
#print(C1[1][0], C1[1][3])
full =[]
s =1
for q in range(len(C1)):
for k in range(len(C1[q])):
full.append(C1[q][k])
for q in range(len(full)):
full[q] = int(full[q])
print(max(full))
new_com_f.write("\n")
#for no in range(len(C1)):
for no in range(max(full)):
E=[]
E.append(no+1)
#for tmp2 in range(max(full)):
for tmp2 in range(len(C1)):
if int(C1[tmp2][0]) == no+1:
E.append(int(C1[tmp2][1]))
#E.append(int(C1[tmp2][3]))
#E.append(float(C1[tmp2][6]))
E.append(float(C1[tmp2][2]))
E_str = " "
for e in range(len(E)):
E_str = E_str + " "+ str(E[e])
#print(E_str)
new_com_f.write(E_str + "\n")
new_com_f.write("\n\n\n")
nl1=1
nl2=-2
for i in range(0, count_ext_sdf):
R13 = np.zeros(3)
R13=R3[conn_arr[i][0]-1][0:3] - R3[conn_arr[i][1]-1][0:3]
dR13=R13[0]**2 + R13[1]**2 + R13[2]**2
dist2[i]= round(np.sqrt(dR13),4)
str1='s'
str2='s'
if ( dist1[i] > float(thresh)):
str1='l'
nl1=nl1*0
if ( dist2[i] > float(thresh)):
str2='l'
nl2=nl2*0
if ( conn_arr[i][2] == 1):
#print(conn_arr[i][0],conn_arr[i][1],conn_arr[i][2],Sy[conn_arr[i][0]-1],"-",Sy[conn_arr[i][1]-1], dist1[i], str1, dist2[i], str2)
rm1 = str(conn_arr[i][0]) +" "+ str(conn_arr[i][1]) +" "+str(conn_arr[i][2]) +" "+ str(Sy[conn_arr[i][0]-1]) +" - "+ str(Sy[conn_arr[i][1]-1]) +" "+ str(dist1[i]) +" "+ str(str1) +" "+ str(dist2[i]) +" "+ str(str2) + "\n"
new_rmsd_file.write(rm1)
elif ( conn_arr[i][2] == 2):
rm2 =str(conn_arr[i][0]) +" "+ str(conn_arr[i][1]) +" "+ str(conn_arr[i][2]) +" "+ str(Sy[conn_arr[i][0]-1]) +" = "+ str(Sy[conn_arr[i][1]-1]) +" "+ str(dist1[i]) +" "+ str(str1) +" "+ str(dist2[i]) +" "+ str(str2) + "\n"
new_rmsd_file.write(rm2)
elif ( conn_arr[i][2] == 3):
rm3 = str(conn_arr[i][0]) +" "+ str(conn_arr[i][1]) +" "+ str(conn_arr[i][2]) +" "+ str(Sy[conn_arr[i][0]-1]) +" # "+ str(Sy[conn_arr[i][1]-1]) +" "+ str(dist1[i]) +" "+ str(str1) +" "+ str(dist2[i]) +" "+ str(str2) + "\n"
new_rmsd_file.write(rm3)
else:
new_rmsd_file.write("ERROR: Unknown bond order encountered in SDF!")
# print(dist2)
f1_check = 0
if ( nl1+nl2 == -1):
new_rmsd_file.write('\n** Geometries in file-1 and file-2 seem alright, no broken structures detected **\n\n')
elif ( nl1+nl2 == -2):
new_rmsd_file.write('File-1 contains long bond!\n')
f1_check = 1
elif ( nl1+nl2 == 1):
new_rmsd_file.write('File-2 contains long bond!\n')
elif ( nl1+nl2 == 0):
new_rmsd_file.write('Both files contain long bond!\n')
RMSD=np.sqrt(sum((dist1-dist2)**2)/count_ext_sdf)
MaxAD=np.max(np.abs(dist1-dist2))
MPAD=100*sum(abs((dist1-dist2)/dist1))/count_ext_sdf
new_rmsd_file.write("== Mean square deviation of bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MSD= " + str(round(RMSD,4)) + " Ang \n\n")
new_rmsd_file.write("== Maximum absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MaxAD= " + str(round(MaxAD,4)) + " Ang\n\n")
if "tier1_vs_tier2" in rmsd_name: # For tier2 - the threshold criteria is MPAD ONLY. MaxAD is waived. if you want to change this, modify this if statement
#if MPAD < 5.0:
if MPAD < float(value["thresh_MPAD"]):
new_rmsd_file.write("== Mean percentage absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MPAD= " +str(round(MPAD,4)) + " \n\n")
new_rmsd_file.write("== Outcome of the Connectivity preserving Geometry Optimization\n")
new_rmsd_file.write("** ConnGO PASS [MPAD < 5] **\n")
else:
if f1_check == 1:
new_rmsd_file.write("== Mean percentage absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MPAD= " +str(round(MPAD,4)) + " \n\n")
new_rmsd_file.write("== Outcome of the Connectivity preserving Geometry Optimization\n")
new_rmsd_file.write("** ConnGO PASS [MPAD < 5] **\n")
else:
new_rmsd_file.write("== Mean percentage absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MPAD= " +str(round(MPAD,4)) + " \n\n")
new_rmsd_file.write("== Outcome of the Connectivity preserving Geometry Optimization\n")
new_rmsd_file.write("** ConnGO FAIL [MPAD < 5] **\n")
else: # For all other tiers, joint threshold of MaxAD and MPAD is enforced.
#if MaxAD < 0.2 and MPAD < 5.0:
if MaxAD < float(value["thresh_MaxAD"]) and MPAD < float(value["thresh_MPAD"]):
new_rmsd_file.write("== Mean percentage absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MPAD= " +str(round(MPAD,4)) + " \n\n")
new_rmsd_file.write("== Outcome of the Connectivity preserving Geometry Optimization\n")
new_rmsd_file.write("** ConnGO PASS [MPAD < 5, MaxAD < 0.2 Angstrom] **\n")
else:
if f1_check == 1:
new_rmsd_file.write("== Mean percentage absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MPAD= " +str(round(MPAD,4)) + " \n\n")
new_rmsd_file.write("== Outcome of the Connectivity preserving Geometry Optimization\n")
new_rmsd_file.write("** ConnGO PASS [MPAD < 5, MaxAD < 0.2 Angstrom] **\n")
else:
new_rmsd_file.write("== Mean percentage absolute deviation in bond lengths from file-1 and file-2\n")
new_rmsd_file.write("MPAD= " +str(round(MPAD,4)) + " \n\n")
new_rmsd_file.write("== Outcome of the Connectivity preserving Geometry Optimization\n")
new_rmsd_file.write("** ConnGO FAIL [MPAD < 5, MaxAD < 0.2 Angstrom] **\n")