source: util/src/ReduceDBondFiletoSubset.py.in@ 48cd93

Last change on this file since 48cd93 was e1a46d, checked in by Frederik Heber <heber@…>, 16 years ago

All of my python script put into ESPACK and adapted with @PYTHON@ and so on.

  • Property mode set to 100755
File size: 7.4 KB
Line 
1#!@PYTHON@
2#
3# from an original PDB and its DBOND file for a subset PDB (dest) file we create the respective DBOND file, i.e. containing only bonds between atoms
4# in the subset
5
6import sys, random, math, re
7wrerr=sys.stderr.write
8wrout=sys.stdout.write
9
10# check arguments
11if len(sys.argv) < 5:
12 print "Usage: "+sys.argv[0]+" <src{PDB/XYZ}file> <destXYZfile> <offsetID> <offsetXYZ> <srcDBONDfile> [destDBONDfile]"
13 sys.exit(1)
14
15EPSILON=1e-3
16CUTOFF=2.
17inputsrc = open(sys.argv[1], "r")
18inputdestPDB = open(sys.argv[2], "r")
19inputsrcDBOND = open(sys.argv[5], "r")
20offsetID=int(sys.argv[3])
21offsetXYZ=int(sys.argv[4])
22entries = sys.argv[1].split(".")
23suffix = entries[-1]
24if len(sys.argv) > 6:
25 output = open(sys.argv[6],"w")
26else:
27 output = open(sys.argv[5]+".new", "w")
28
29# 1. first parse both PDB files into arrays (id, element, xyz) , therewhile scan BoundaryBoxes
30max = [ 0., 0., 0. ]
31min = [ 0., 0., 0. ]
32x = [ 0., 0., 0. ]
33nr=0
34srcAtoms = []
35#if "xyz" in suffix:
36# print "Scanning source XYZ file "+sys.argv[1]+"."
37# for line in inputsrc:
38# if (len(entries)<4 or (entries[0]!="O" and entries[0]!="Si" and entries[0]!="Ca")):
39# continue
40# entries = line.split()
41# for n in range(3):
42# x[n] = float(entries[offsetXYZ+n])
43# if x[n] > max[n]:
44# max[n] = x[n]
45# if x[n] < min[n]:
46# min[n] = x[n]
47# srcAtoms.append([int(entries[offsetID]), x[0], x[1], x[2]])
48# nr+=1
49#elif "pdb" in suffix:
50print "Scanning source PDB file "+sys.argv[1]+"."
51for line in inputsrc:
52 if "#" in line:
53 continue
54 if "END" in line:
55 break
56 if "ATOM" in line:
57 entries = line.split()
58 for n in range(3):
59 x[n] = float(entries[offsetXYZ+n])
60 if x[n] > max[n]:
61 max[n] = x[n]
62 if x[n] < min[n]:
63 min[n] = x[n]
64 srcAtoms.append([int(entries[offsetID]), x[0], x[1], x[2]])
65 nr+=1
66#else:
67# print "Cannot recognize input source file format: $suffix."
68# sys.exit(1)
69
70inputsrc.close()
71print "Scanned "+str(nr)+" source atoms."
72
73print "Scanning destination XYZ file "+sys.argv[2]+"."
74destAtoms = []
75nr = 0
76for line in inputdestPDB:
77 entries = line.split()
78 if (len(entries)<4 or (entries[0]!="O" and entries[0]!="Si" and entries[0]!="Ca")):
79 continue
80 for n in range(3):
81 x[n] = float(entries[1+n])
82 if x[n] > max[n]:
83 x[n]-=max[n]
84 if x[n] < min[n]:
85 x[n]+=max[n]
86 destAtoms.append([nr, x[0], x[1], x[2]])
87 nr+=1
88inputdestPDB.close()
89
90#nr=0
91#for line in inputdestPDB:
92# if "#" in line:
93# continue
94# if "END" in line:
95# break
96# if "ATOM" in line:
97# entries = line.split()
98# for n in range(3):
99# x[n] = float(entries[offsetXYZ+n])
100# if x[n] > max[n]:
101# max[n] = x[n]
102# if x[n] < min[n]:
103# min[n] = x[n]
104# destAtoms.append([int(entries[offsetID]), x[0], x[1], x[2]])
105# nr+=1
106#inputdestPDB.close()
107#
108print "Scanned "+str(nr)+" destination atoms."
109
110# 2. create Linked Cell with minimum distance box length
111print "Found Box bounds [%f,%f]x[%f,%f]x[%f,%f]." % (min[0],max[0],min[1],max[1],min[2],max[2])
112for i in range(3): # shift by minimum if below zero
113 if min[i] < 0:
114 max[i]-=min[i]
115 else:
116 min[i]=0
117cells_x=int(math.ceil(float(max[0])/CUTOFF))+1
118cells_y=int(math.ceil(float(max[1])/CUTOFF))+1
119cells_z=int(math.ceil(float(max[2])/CUTOFF))+1
120print "Number of cells in each axis direction (%f,%f,%f)." % (cells_x, cells_y, cells_z)
121
122# 3. put each atom into its cell, lists may contain multiple atoms, mark src(0) or dest (1)
123cell=[]
124for i in range(cells_x):
125 cell.append([])
126 for j in range(cells_y):
127 cell[i].append([])
128 for k in range(cells_z):
129 cell[i][j].append([0])
130for i in range(len(srcAtoms)):
131 atom = srcAtoms[i]
132 for n in range(3):
133 x[n] = int(math.floor(float(atom[1+n])/CUTOFF))
134 try:
135 cell[x[0]][x[1]][x[2]][0]+=1
136 cell[x[0]][x[1]][x[2]].append([0,i]) # 0 means src
137 except IndexError:
138 print "!IndexError with indices "+str(x[0])+" "+str(x[1])+" "+str(x[2])+" on source atom "+str(atom)+"."
139 #print "Source atom "+str(i)+" goes to cell "+str(x[0])+","+str(x[1])+","+str(x[2])+"."
140for i in range(len(destAtoms)):
141 atom = destAtoms[i]
142 for n in range(3):
143 x[n] = int(math.floor(float(atom[1+n])/CUTOFF))
144 try:
145 cell[x[0]][x[1]][x[2]][0]+=1
146 cell[x[0]][x[1]][x[2]].append([1,i]) # 1 means dest
147 except IndexError:
148 print "!IndexError with indices "+str(x[0])+" "+str(x[1])+" "+str(x[2])+" on destination atom "+str(atom)+"."
149 #print "Destination atom "+str(i)+" goes to cell "+str(x[0])+","+str(x[1])+","+str(x[2])+"."
150
151# 4. go through each cell and match (src, dest)-pairs by closest distance, warn if greater than EPSILON
152srcMatches=0
153destMatches=0
154Map = {}
155i=-1
156j=-1
157k=-1
158l=-1
159e=-1
160r=-1
161t=-1
162m=-1
163for i in range(cells_x):
164 for j in range(cells_y):
165 for k in range(cells_z):
166
167 #go through every atom in cell
168 try:
169 for l in range(1, cell[i][j][k][0]+1):
170 if cell[i][j][k][l][0] != 0: # skip if it's not a src atom
171 continue
172 atom1=cell[i][j][k][l][1]
173 #print "Current source atom is "+str(srcAtoms[atom1][0])+"."
174 currentPair=[atom1,-1]
175 oldDist=0.
176 # go through cell and all lower neighbours
177 for e in range(i-1,i+2):
178 #if on boarder continue periodic
179 #if e>cells_x-1:
180 # e=e-cells_x
181 if (e < 0) or (e >= cells_x):
182 continue
183 for r in range(j-1,j+2):
184 #if on boarder continue periodic
185 #if r>cells_y-1:
186 # r=r-cells_y
187 if (r < 0) or (r >= cells_y):
188 continue
189 for t in range(k-1,k+2):
190 #if on boarder continue periodic
191 #if t>cells_z-1:
192 # t=t-cells_z
193 if (t < 0) or (t >= cells_z):
194 continue
195 #go through all atoms in cell
196 for m in range(1, cell[e][r][t][0]+1):
197 if cell[e][r][t][m][0] != 1: # skip if it's not a dest atom
198 continue
199 atom2=cell[e][r][t][m][1]
200 #print "Current destination atom is "+str(destAtoms[atom2][0])+"."
201 dist=0
202 tmp=0
203 for n in range(3):
204 tmp = srcAtoms[atom1][1+n] - destAtoms[atom2][1+n]
205 dist += tmp*tmp
206 #print "Squared distance between the two is "+str(dist)+"."
207 if ((oldDist > dist) or ((currentPair[1] == -1) and (dist<EPSILON))):
208 currentPair[1] = atom2
209 oldDist = dist
210 if currentPair[1] == -1:
211 #wrerr("Could not find a suitable partner for srcAtom (%d,%d)!\n" % (srcAtoms[currentPair[0]][0],currentPair[1]))
212 Map[ srcAtoms[currentPair[0]][0] ] = currentPair[1]
213 else:
214 #print "Found a suitable partner for srcAtom "+str(srcAtoms[currentPair[0]][0])+","+str(destAtoms[currentPair[1]][0])+"."
215 srcMatches+=1
216 destMatches+=1
217 Map[ srcAtoms[currentPair[0]][0] ] = destAtoms[currentPair[1]][0]
218 except IndexError:
219 wrerr("Index Error: (%d,%d,%d)[%d] and (%d,%d,%d)[%d]\n" % (i,j,k,l,e,r,t,m))
220 break
221
222# 5. print the listing
223print "We have "+str(srcMatches)+" matching atoms."
224print "Mapping is:"
225for key in Map:
226 if Map[key] != -1:
227 print str(key)+" -> "+str(Map[key])
228#print Map # work also
229
230# 6. use the listing to rewrite the dbond file, store under given filename
231line=inputsrcDBOND.readline()
232output.write(line)
233for line in inputsrcDBOND:
234 if "#" in line:
235 continue
236 entries=line.split()
237 flag=0
238 for n in range(len(entries)):
239 try:
240 if Map[ int(entries[n]) ] == -1:
241 flag=1
242 except KeyError:
243 print "entries["+str(n)+"] = "+str(int(entries[n]))+" does not exist in Map."
244 flag=1
245 if flag==1:
246 continue
247 for n in range(len(entries)): # only difference to ReduceDBondFile.py is not to change the ids
248 output.write("%d\t" % (int(entries[n])))
249 output.write("\n")
250output.close()
251
252# exit
Note: See TracBrowser for help on using the repository browser.