-
Notifications
You must be signed in to change notification settings - Fork 1
/
to_g16_input_v2.py
231 lines (151 loc) · 5.97 KB
/
to_g16_input_v2.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 14 10:34:37 2022
@author: chingchinglam
update from version 1:
update functions to accommodate radicals
"""
def right_spacing(coor_list):
coor_list_space=[]
for cline in coor_list:
split_coor=cline.split()
new_coor=''
space1=18
space2=4
space3=4
if len(split_coor[0]) == 2:
space1=17
for o in split_coor:
if '-' in o:
index_list=[]
index_list.append(split_coor.index(o))
if 1 in index_list:
space1=17
elif 2 in index_list:
space2=3
elif 3 in index_list:
space3=3
new_coor=str(split_coor[0])+space1*' '+str(split_coor[1])+'00'+space2*' '+str(split_coor[2])+'00'+space3*' '+str(split_coor[3])+'00\n'
coor_list_space.append(new_coor)
return coor_list_space
def sdf2gjfs(filename,keywords,space, priority_ls, per, radical = False, rmAtom_ls=[]):
sdf_output=[]
with open(filename) as output:
for line in output:
sdf_output+= [line]
END_index_list =[k for k,d in enumerate(sdf_output) if d=='M END\n']
atom_no=int(sdf_output[3][0:3])
len_mol=END_index_list[0]
division1=[]
for j in END_index_list:
indv_mol=sdf_output[j-len_mol:j+1]
#atom_no=int(indv_mol[3].split(' ')[1])
indv_mol_atom1=indv_mol[4:4+atom_no]
indv_mol_atom2=[j[30:35]+j[0:31] for j in indv_mol_atom1]
division1.append(indv_mol_atom2)
### to accommodate radicals
if radical == True:
division1_rm=[]
for conf in division1:
division1_rm.append([conf[idx] for idx in range(0,len(conf)) if idx+1 not in rmAtom_ls])
division2=[right_spacing(ls) for ls in division1_rm]
else:
division2=[right_spacing(ls) for ls in division1]
###
division3=[]
for structure in division2:
coor_content=''
for individual_structure in structure:
coor_content+=str(individual_structure)
content=keywords+coor_content+space
division3.append(content)
len_priority_ls = len(priority_ls)
priority_ls1=[x+1 for x in priority_ls]
len_per_priority_ls = round(len(priority_ls)*per)
select = [y+1 for y in priority_ls]
select1 = select[:len_per_priority_ls]
select2 = priority_ls[:len_per_priority_ls]
notselect = select[len_per_priority_ls:]
#notselect2 = priority_ls[len_per_priority_ls:]
division4=[]
for o in select2:
division4.append(division3[o])
filename_list2=[]
for num2 in select1:
filename_list2.append(filename[:-4]+'_'+str(num2)+'.gjf')
for c,d in zip(filename_list2,division4):
#print(c,'\n',d)
file = open(c, "w")
file.write(d)
file.close()
result_txt='CONFPASS - summary \n \n priority_ls: '+str(priority_ls1)+'\n'
result_txt+='\n To be optimised (with .gjf files generated):'+str(select1)+'\n \n waitlisted: '
result_txt+=str(notselect)+ '\n \n Total number of conformers: '+str(len_priority_ls)
if radical == True:
result_txt+='\n radical: '+ str(radical)
result_txt+=' \n rmAtom_ls: '+str(rmAtom_ls)
file = open(filename[:-4]+'_summary.txt', "w")
file.write(result_txt)
file.close()
#print(result_txt)
def sdf2gjfs_v2(filename,keywords,space,numbering, by_num='Yes', radical = False, rmAtom_ls=[]):
sdf_output=[]
with open(filename) as output:
for line in output:
sdf_output+= [line]
END_index_list =[k for k,d in enumerate(sdf_output) if d=='M END\n']
atom_no=int(sdf_output[3][0:3])
len_mol=END_index_list[0]
division1=[]
for j in END_index_list:
indv_mol=sdf_output[j-len_mol:j+1]
#atom_no=int(indv_mol[3].split(' ')[1])
indv_mol_atom1=indv_mol[4:4+atom_no]
indv_mol_atom2=[j[30:35]+j[0:31] for j in indv_mol_atom1]
division1.append(indv_mol_atom2)
### to accommodate radicals
if radical == True:
division1_rm=[]
for conf in division1:
division1_rm.append([conf[idx] for idx in range(0,len(conf)) if idx+1 not in rmAtom_ls])
division2=[right_spacing(ls) for ls in division1_rm]
else:
division2=[right_spacing(ls) for ls in division1]
###
division3=[]
for structure in division2:
coor_content=''
for individual_structure in structure:
coor_content+=str(individual_structure)
content=keywords+coor_content+space
division3.append(content)
name = filename.split('/')[-1][:-4]
if by_num=='No':
filename_ls=[]
num_ls=list(range(1,len(division3)+1))
for idx in num_ls:
filename_ls.append(name+'_'+str(idx)+'.gjf')
#double check
#for b,a in zip(filename_ls,division3):
#print(b,'\n',a)
#write the file
for ifilename, gcontent in zip(filename_ls,division3):
file = open(ifilename, "w")
file.write(gcontent)
file.close()
else:
select=[y-1 for y in numbering]
select1=[y+1 for y in select]
division4=[]
for o in select:
division4.append(division3[o])
filename_list2=[]
for num2 in select1:
filename_list2.append(name+'_'+str(num2)+'.gjf')
for c,d in zip(filename_list2,division4):
## write the file
#print(c,'\n',d)
file = open(c, "w")
file.write(d)
file.close()