-
Notifications
You must be signed in to change notification settings - Fork 7
/
Topology.py
244 lines (185 loc) · 9.12 KB
/
Topology.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
# Copyright (c) 2014-2018 Matteo Degiacomi and Valentina Erastova
#
# Assemble is free software ;
# you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation ;
# either version 2 of the License, or (at your option) any later version.
# Assemble is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY ;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with Assemble ;
# if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#
# Authors : Matteo Degiacomi, [email protected], Valentina Erastova, [email protected]
import numpy as np
class Topology(object):
def __init__(self):
self.head=-1
self.tail=-1
def load(self,topfile):
b=[]
a=[]
d=[]
i=[]
m=[]
self.replace_cter=[] #cter substitutions
self.replace_nter=[] #nter substitutions
f = open(topfile, 'r+')
target=""
for line in f:
w=line.split()
if len(w)>0 and w[0]=="[" :
target=w[1]
elif len(w)>0 and ";" in w[0]:
continue
elif len(w)>0:
if target=="bonds":
b.append(w)
elif target=="angles":
a.append(w)
elif target =="dihedrals" :
d.append(w)
elif target == "impropers":
i.append(w)
elif target =="mapping":
m.append(w)
elif target == "cterminal":
if len(w)==2:
self.tail=w
self.replace_cter.append(w)
elif target == "nterminal":
if len(w)==2:
self.head=w
self.replace_nter.append(w)
else:
raise IOError("ERROR: unknown %s section in topology file %s"%(target, topfile))
f.close()
self.bonds=np.array(b).astype(str)
self.angles=np.array(a).astype(str)
self.dihedrals=np.array(d).astype(str)
self.impropers=np.array(i).astype(str)
self.mapping=np.array(m).astype(str)
#self.replace_nter=np.array(sn).astype("S10") #nter replacements
#self.replace_cter=np.array(sc).astype("S10") #cter replacements
if self.head==-1:
raise IOError("head not found!")
if self.tail==-1:
raise IOError("tail not found!")
#substitute bonds, angles and dihedrals according to terminal section
#( call once to transform molecule into terminal molecule)
# WARNING: number of chars of tailing element should not be greater than the greatest length in topology
def make_terminal(self,target):
if target=="nterminal":
subst=self.replace_nter
elif target=="cterminal":
subst=self.replace_cter
else:
raise IOError("target should be equal to nterminal or cterminal")
for i in range(0,len(subst),1):
n=subst[i]
if len(n)==2: #atom substitution
test=self.mapping[:,0]==n[0]
if np.any(test):
self.mapping[test,1]=n[-1]
elif len(n)==3 and len(self.bonds)>0: #bond substitution
test=np.logical_and(self.bonds[:,0]==n[0],self.bonds[:,1]==n[1])
if np.any(test):
self.bonds[test,2]=n[-1]
elif len(n)==4 and len(self.angles)>0: #angle substitution
test=np.logical_and(np.logical_and(self.angles[:,0]==n[0],self.angles[:,1]==n[1]),self.angles[:,2]==n[2])
if np.any(test):
self.angles[test,3]=n[-1]
elif len(n)==5: #dihedral or improper substitution
if len(self.dihedrals)>0:
test=np.logical_and(np.logical_and(np.logical_and(self.dihedrals[:,0]==n[0],self.dihedrals[:,1]==n[1]),\
self.dihedrals[:,2]==n[2]),self.dihedrals[:,3]==n[3])
if np.any(test):
self.dihedrals[test,4]=n[-1]
if len(self.impropers)>0:
test=np.logical_and(np.logical_and(np.logical_and(self.impropers[:,0]==n[0],self.impropers[:,1]==n[1]),\
self.impropers[:,2]==n[2]),self.impropers[:,3]==n[3])
if np.any(test):
self.impropers[test,4]=n[-1]
else:
raise IOError("Cannot understand line %s in %s section!"%(n,target))
#search bond type connecting current molecule with next one
def search_next_bond(self,a,b):
#a is current molecule, b next one
#case a +b
test1=np.array(np.where(self.bonds[:,0:2]=="+%s"%b))[0]
test2=np.array(np.where(self.bonds[:,0:2]==a))[0]
r=self.bonds[np.intersect1d(test1,test2),:]
return r
#search bond type connecting current molecule with previous one
def search_prev_bond(self,a,b):
#a is current molecule, b next one
#case -a b
test1=np.array(np.where(self.bonds[:,0:2]==b))[0]
test2=np.array(np.where(self.bonds[:,0:2]=="-%s"%a))[0]
r=self.bonds[np.intersect1d(test1,test2),:]
return r
#search angle type connecting current molecule with next one
def search_next_angle(self,a,b,sign):
#a (and c) is current molecule, b next one
#c a +b
if sign == "+": #caller is current molecule in polymer
r=filter(lambda x: np.any(x=="+%s"%b) and np.any(x==a) and len([elem for elem in x[0:3] if "+" in elem])==1, self.angles)
#-c -a b
elif sign=="-": #caller is next molecule in polymer
r=filter(lambda x: np.any(x=="-%s"%a) and np.any(x==b) and len([elem for elem in x[0:3] if "-" in elem])==2, self.angles)
else:
raise IOError("ERROR: sign must be - or +")
return np.array(list(r))
#search angle type connecting current molecule with previous one
def search_prev_angle(self,a,b,sign):
#a is current molecule, c b next one
#c b -a
if sign=="-": #caller is next molecule in polymer
r=filter(lambda x: np.any(x=="-%s"%a) and np.any(x==b) and len([elem for elem in x[0:3] if "-" in elem])==1, self.angles)
#+c +b a
elif sign=="+": #caller is current molecule in polymer
r=filter(lambda x: np.any(x=="+%s"%b) and np.any(x==a) and len([elem for elem in x[0:3] if "+" in elem])==2, self.angles)
else:
raise IOError("ERROR: sign must be - or +")
return np.array(list(r))
#search dihedral type connecting current molecule with next one
def search_next_dihedral(self,a,b,sign):
#a is current molecule, b next one
#-d -c -a b
if sign=="-": #caller is next molecule in polymer
r=filter(lambda x: np.any(x=="-%s"%a) and np.any(x==b) and len([elem for elem in x[0:4] if "-" in elem])==3, self.dihedrals)
#d c a +b
elif sign=="+": #caller is current molecule in polymer
r=filter(lambda x: np.any(x=="+%s"%b) and np.any(x==a) and len([elem for elem in x[0:4] if "+" in elem])==1, self.dihedrals)
else:
raise IOError("ERROR: sign must be - or +")
return np.array(list(r))
#search dihedral type connecting current molecule with previous one
def search_prev_dihedral(self,a,b,sign):
#a is current molecule, b next one
#d c b -a
if sign=="-": #caller is next molecule in polymer
r=filter(lambda x: np.any(x=="-%s"%a) and np.any(x==b) and len([elem for elem in x[0:4] if "-" in elem])==1, self.dihedrals)
#+d +c +b a
elif sign=="+": #caller is current molecule in polymer
r=filter(lambda x: np.any(x=="+%s"%b) and np.any(x==a) and len([elem for elem in x[0:4] if "+" in elem])==3, self.dihedrals)
else:
raise IOError("ERROR: sign must be - or +")
return np.array(list(r))
def check_existence(self,atom):
if np.any(self.mapping[:,1]==atom):
return True
else:
return False
if __name__=="__main__":
Top=Topology()
#Top.load("database/cis-PI-monomer.txt")
#Top.load("database/vinyl-PBD-monomer.txt")
Top.load("database/cis-PI-monomer.txt")
#print(Top.search_prev_dihedral('ZZ','AA','-'))
#print(Top.search_next_dihedral('ZZ','AA','+'))
Top.make_terminal("nterminal")
Top.make_terminal("cterminal")
print(Top.mapping)
print(Top.bonds)
print(Top.angles)
print(Top.dihedrals)