-
Notifications
You must be signed in to change notification settings - Fork 6
/
listprep-foramber.py
206 lines (176 loc) · 4.95 KB
/
listprep-foramber.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
#!/bin/env python3
#This listprep version takes PSF-file (Xplor formatted) as argument and creates all lists from it.
#Note: Modifed so no longer writes atom types list as this list may not be accurate in the PSF-file w.r.t. Amber atom types
import sys
import os
import re
from decimal import *
psffile=sys.argv[1]
getcontext().prec=6
#Opening input and output
#Remove old outputfile
try:
os.remove('save-new.chm')
except OSError:
pass
tcgfile = open('save-new.chm', 'a')
def isWhole(x):
if(x%1 == 0):
return True
else:
return False
#Here getting atom types from XPLOR file (printed in there but not regular PSF file)
atomtypes=[]
bla=[]
charges=[]
resid=[]
segnames=[]
atomlist=[]
with open(psffile) as gh:
for line in gh:
if re.search('!NATOM', line):
numatoms=int(line.split()[0])
for i in range(numatoms):
temp=next(gh)
temp=temp.rstrip('\n')
xatomtypes=temp.split()[5]
atomtypes.append(xatomtypes)
xcharges=temp.split()[6]
xresid=temp.split()[2]
xsegnames=temp.split()[1]
xatomlist=temp.split()[0]
charges.append(xcharges)
resid.append(xresid)
segnames.append(xsegnames)
atomlist.append(xatomlist)
#with open('new.psf') as fh:
# for line in fh:
# if re.search('!NATOM', line):
# numatoms=int(line.split()[0])
# for i in range(numatoms):
# temp=next(fh)
# temp=temp.rstrip('\n')
# xcharges=temp.split()[6]
# xresid=temp.split()[2]
# xsegnames=temp.split()[1]
# xatomlist=temp.split()[0]
# charges.append(xcharges)
# resid.append(xresid)
# segnames.append(xsegnames)
# atomlist.append(xatomlist)
#print(atomlist)
print("resid atomlist is:", len(atomlist))
print("resid length is:", len(resid))
print("segnames length is:", len(segnames))
#Going through resid list and stripping out duplicate values
#Also creating bsegnames list of same size
prev=94354354
bresid=[]
bsegnames=[]
count=0
counter=1
n=1
atombeg=atomlist[0]
atomsinres=[]
reslistatoms=[]
for j in resid:
#print("j is:", j)
#print("count is:", count)
#print("counter is:", counter)
j=int(j)
# Atomlist for each residue
if (j != prev and prev != 94354354):
#print("Atomsinres is:", atomsinres)
reslistatoms.append(atomsinres)
atomsinres=[]
atomsinres.append(atomlist[count])
#Creating bresid and bsegnames
if j != prev:
j=int(j)
bresid.append(j)
bsegnames.append(segnames[count])
prev=j
#Making sure the last atoms for last residue gets printed
if counter == len(resid):
#print("Atomsinres is:", atomsinres)
reslistatoms.append(atomsinres)
count=count+1
counter=counter+1
#Charge groups
#Using Decimal here so that floats add up to whole numbers
sum=Decimal(0)
count=0
groups=[]
templist=[]
#Grabbing and printing out
tcgfile.write("set groups {")
for i in charges:
count=count+1
templist.extend([count])
stemplist=count
#print("Curr sum is ", sum)
#print("Charge is:", i)
i=Decimal(i)
sum=sum + i
if isWhole(sum) == True:
#print("Sum is", sum)
tcgfile.write("{")
for item in templist:
tcgfile.write("%s " % item)
tcgfile.write("} ")
templist=[]
sum=0
tcgfile.write("}\n")
# Summing up charge
fsum=Decimal(0)
for b in charges:
#print("NewCharge is:", b)
#print("NewCharge in Decimal is:", Decimal(b))
#print("Curr sum is ", fsum)
b=Decimal(b)
fsum=fsum + b
print("Total charge of system is", fsum)
# Printing out charges
tcgfile.write("set charges {")
for item in charges:
tcgfile.write("%s " % item)
#Ending tcl list
tcgfile.write("}\n")
#Printing pdbresidues list
tcgfile.write("set pdbresidues {{")
beginres=int(resid[0])
residlength=len(resid)-1
endres=int(resid[residlength])+1
#print("Length of segnames:", len(segnames))
#print("Length of bresid:", len(bresid))
#print(segnames)
#Printing out resids
count=0
for x in bresid:
x=int(x)
bseg=bsegnames[count]
#print(bseg)
tcgfile.write(bseg)
tcgfile.write("-%s " % x)
count=count+1
tcgfile.write("} ")
tcgfile.write("{")
for item in reslistatoms:
tcgfile.write("{")
tcgfile.write( " ".join(item))
tcgfile.write("} ")
tcgfile.write("}}\n")
#Printing out special pdbresidues (without segnames)
tcgfile.write("set residuegroups {")
count=0
for item in reslistatoms:
tcgfile.write("{")
tcgfile.write( " ".join(item))
tcgfile.write("} ")
tcgfile.write("}\n")
# Not printing atom types here because of AMber. Getting them from prmtop instead
#Printing out atom types
#tcgfile.write("set types {")
#for item in atomtypes:
# tcgfile.write("%s " % item)
#tcgfile.write("}")