forked from AdrienCerdan/cgenff2pmx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgenff2pmx.py
414 lines (403 loc) · 15.4 KB
/
cgenff2pmx.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python
# coding: utf-8
import sys, getopt, os, re, shutil
def read_bonds(bond,prm_file):
file2 = open(prm_file, 'r')
Lines2 = file2.readlines()
part_bonds=0
for line in Lines2:
if not line.strip(): #skip empty line
continue
LL=line.strip()
# find the bonds section
if LL == "[ bondtypes ]":
part_bonds=1
continue
if part_bonds==1:
if LL[0]==";":
continue
elif LL[0]=="[": # end of the bonds section
part_bonds=0
continue
else:
# split the string
bond_ff=LL.split()[0:]
# test the atoms involved in the bond in both direction
if (bond_ff[0:2] == bond) or (bond_ff[0:2] == bond[::-1]):
# return the bond parameter from the .prm
return bond_ff
def read_angles(angle,prm_file):
# similar to read bond but with 3 atoms involved
file2 = open(prm_file, 'r')
Lines2 = file2.readlines()
part_angles=0
for line in Lines2:
if not line.strip(): #skip empty line
continue
LL=line.strip()
if LL == "[ angletypes ]":
part_angles=1
continue
if part_angles==1:
if LL[0]==";":
continue
elif LL[0]=="[":
part_angles=0
continue
else:
angle_ff=LL.split()[0:]
if (angle_ff[0:3] == angle) or (angle_ff[0:3] == angle[::-1]) :
return angle_ff
def read_dihedrals(dihedral,prm_file):
# similar to read bond but with 4 atoms involved
# also works with duplicated dihedral section with impropers from cgenff, but all dihedrals are merged
file2 = open(prm_file, 'r')
Lines2 = file2.readlines()
part_dihedrals=0
for line in Lines2:
if not line.strip(): #skip empty line
continue
LL=line.strip()
if LL == "[ dihedraltypes ]":
part_dihedrals=1
continue
if part_dihedrals==1:
if LL[0]==";":
continue
elif LL[0]=="[":
part_dihedrals=0
continue
else:
dihedral_ff=LL.split()[0:]
if (dihedral_ff[0:4] == dihedral) or (dihedral_ff[0:4] == dihedral[::-1]):
return dihedral_ff
# MAIN #
# should handles errors if the bond-angle-dihedral types are missing in the .prm while using read_*
def main(argv):
print(' $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
print(' $ CGENFF2PMX.py $')
print(' $ Parse .itp and .prm to merge them $')
print(' $ from cgenff_charmm2gmx.py to PMX $')
print(' $ By Adrien Cerdan 2022 $')
print(' $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
inputitp = ''
inputprm = ''
inputpdb = ''
outputitp = ''
pdbispresent=0
try:
opts, args = getopt.getopt(argv,"hs:i:p:o:",["iitpfile=","iprmfile=","ipdbfile=","oitpfile="])
except getopt.GetoptError:
print('cgenff2pmx.py -i <inputfileITP> -p <inputfilePRM> -s <inputfilePDB> -o <outputfileITP>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('cgenff2pmx.py -i <inputfileITP> -p <inputfilePRM> -s <inputfilePDB> -o <outputfileITP>')
sys.exit()
elif opt in ("-i", "--iitpfile"):
inputitp = arg
elif opt in ("-p", "--iprmfile"):
inputprm = arg
elif opt in ("-s", "--ipdbfile"):
inputpdb = arg
pdbispresent=1
elif opt in ("-o", "--oitpfile"):
outputitp = arg
print('Input itp file is ', inputitp)
print('Input prm file is ', inputprm)
if pdbispresent==1:
print('Input pdb file is ', inputpdb)
print('Output itp file is ', outputitp)
file1 = open(inputitp, 'r')
Lines1 = file1.readlines()
count = 0
part_moleculetype=0
part_atoms=0
part_bonds=0
part_pairs=0
part_angles=0
part_dihedrals=0
part_vs=0
part_exclusions=0
dict_atoms={}
with open(outputitp, 'w') as f:
f.write('; $ CGENFF2PMX.py $\n')
f.write('; Parse .itp and .prm to merge them\n')
f.write('; from cgenff_charmm2gmx.py to PMX\n')
f.write('; By Adrien Cerdan 2022 \n')
f.write('\n')
# Read lines
for line in Lines1:
if not line.strip(): #skip empty line
continue
LL=line.strip()
# Enter moleculetype section: check if it can contains some usefull information but should be ok
if LL == "[ moleculetype ]":
if part_moleculetype==0:
#print('moleculetype')
with open(outputitp, 'a') as f:
f.writelines(LL)
part_moleculetype=1
continue
if part_moleculetype==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
continue
elif LL=="[ atoms ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_moleculetype=0
part_atoms=1
#print("atoms")
continue
else:
pairs_out=LL.split()
#print(pairs_out)
with open(outputitp, 'a') as f:
for line in pairs_out:
f.write(' '+"".join(line) + "\t")
f.writelines('\n')
continue
# Enter atoms section: nothing to do, just copy
if part_atoms==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
f.writelines(LL+'\n')
continue
elif LL[0]=="[":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_atoms=0
part_bonds=1
#print("bonds")
continue
else:
#print(LL)
atom_line=LL.split()
if LL.split()[4][0]=='L':
print("LP detected for atom: ",LL.split()[0])
if pdbispresent==1:
print("Old line: ", atom_line)
list_atom_name=list(atom_line[4])
list_atom_name[0]="E"
str_atom_name=''.join(list_atom_name)
atom_line[4]=str_atom_name
print("New line: ", atom_line)
backuppdb=inputpdb+".bck"
print("Backup .pdb: ",inputpdb," into :",backuppdb)
shutil.copyfile(inputpdb, backuppdb)
print("Modifying .pdb")
with open(inputpdb, "r") as sources:
lines = sources.readlines()
with open(inputpdb, "w") as sources:
for line in lines:
sources.write(re.sub("LP", 'EP', line))
else:
if os.path.exists(outputitp):
os.remove(outputitp)
else:
print("The file does not exist")
sys.exit("ERROR\nLP is detected and needs to be corrected in .pdb but the file was not provided.\nPlease submit a .pdb with the option -s file.pdb.\nAbnormal termination. ERROR.")
with open(outputitp, 'a') as f:
for line in atom_line:
f.write("\t"+"".join(line))
f.writelines('\n')
#f.writelines(' '+LL+'\n')
# Create the dictionary where atom number is associated to an atom type to read .prm
idx_atom=int(LL.split()[0])
atom_type=LL.split()[1]
dict_atoms[idx_atom]=atom_type
# Enter bonds section: use read_bonds on .prm then copy
if part_bonds==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
f.writelines(LL+'\n')
continue
elif LL=="[ pairs ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_bonds=0
part_pairs=1
#print("pairs")
continue
else:
# read atom indexes forming the bond
bond_idx=LL.split()[0:2]
bond=[dict_atoms[int(bond_idx[0])],dict_atoms[int(bond_idx[1])]]
# read the parameters from .prm using read_bonds
bond_ff=read_bonds(bond,inputprm)
bond_ff[0]=bond_idx[0]
bond_ff[1]=bond_idx[1]
with open(outputitp, 'a') as f:
for line in bond_ff:
f.write("\t"+"".join(line))
f.writelines('\n')
#print(bond_ff)
continue
# Enter pairs section: nothing tot do I guess, just copy
if part_pairs==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
f.writelines(LL+'\n')
continue
elif LL=="[ angles ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_pairs=0
part_angles=1
#print("angles")
continue
else:
pairs_out=LL.split()
#print(pairs_out)
with open(outputitp, 'a') as f:
for line in pairs_out:
f.write("\t"+"".join(line))
f.writelines('\n')
continue
# Enter Angles section: use read_angles to extract them from .prm
if part_angles==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
#f.writelines('\n')
f.writelines(LL+'\n')
continue
elif LL=="[ dihedrals ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_angles=0
part_dihedrals=1
#print("dihedrals")
continue
else:
# like in bonds but with angles
angle_idx=LL.split()[0:3]
angle=[dict_atoms[int(angle_idx[0])],dict_atoms[int(angle_idx[1])],dict_atoms[int(angle_idx[2])]]
angle_ff=read_angles(angle,inputprm)
angle_ff[0]=angle_idx[0]
angle_ff[1]=angle_idx[1]
angle_ff[2]=angle_idx[2]
with open(outputitp, 'a') as f:
for line in angle_ff:
f.write("\t"+"".join(line))
f.writelines('\n')
#print(angle_ff)
continue
# enter in dihedral section: read_dihedrals is called to extract them from .prm
if part_dihedrals==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
#f.writelines('\n')
f.writelines(LL+'\n')
continue
elif LL=="[ dihedrals ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_angles=0
part_dihedrals=1
#print("dihedrals2")
continue
#elif re.search('[ virtual_sites. ]', LL):
#elif fnmatch.fnmatch(LL,"[ virtual_sites? ]"):
elif LL=="[ virtual_sites2 ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_dihedrals=0
part_vs=1
#print("VS")
continue
elif LL=="[ virtual_sites3 ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_dihedrals=0
part_vs=1
#print("VS")
continue
elif LL=="[ exclusions ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_vs=0
part_exclusions=1
#print("angles")
continue
else:
# like bonds and angles
dihedral_idx=LL.split()[0:4]
dihedral=[dict_atoms[int(dihedral_idx[0])],dict_atoms[int(dihedral_idx[1])],dict_atoms[int(dihedral_idx[2])],dict_atoms[int(dihedral_idx[3])]]
dihedral_ff=read_dihedrals(dihedral,inputprm)
dihedral_ff[0]=dihedral_idx[0]
dihedral_ff[1]=dihedral_idx[1]
dihedral_ff[2]=dihedral_idx[2]
dihedral_ff[3]=dihedral_idx[3]
with open(outputitp, 'a') as f:
for line in dihedral_ff:
f.write("\t"+"".join(line))
f.writelines('\n')
#print(dihedral_ff)
continue
# Enter virtual site section: nothing to do I guess, just copy
if part_vs==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
f.writelines(LL+'\n')
continue
elif LL=="[ exclusions ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_vs=0
part_exclusions=1
#print("angles")
continue
elif LL=="[ virtual_sites3 ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_dihedrals=0
part_vs=1
#print("VS")
continue
else:
pairs_out=LL.split()
#print(pairs_out)
with open(outputitp, 'a') as f:
for line in pairs_out:
f.write("\t"+"".join(line))
f.writelines('\n')
continue
# Enter exclusions section: nothing to do I guess, just copy
if part_exclusions==1:
if LL[0]==";":
with open(outputitp, 'a') as f:
f.writelines(LL+'\n')
continue
elif LL=="[ exclusions ]":
with open(outputitp, 'a') as f:
f.writelines('\n')
f.writelines(LL+'\n')
part_vs=0
part_exclusions=1
#print("angles")
continue
else:
pairs_out=LL.split()
#print(pairs_out)
with open(outputitp, 'a') as f:
for line in pairs_out:
f.write("\t"+"".join(line))
f.writelines('\n')
continue
#print(dict_atoms)
if __name__ == "__main__":
main(sys.argv[1:])