[e1a46d] | 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 |
|
---|
| 6 | import sys, random, math, re
|
---|
| 7 | wrerr=sys.stderr.write
|
---|
| 8 | wrout=sys.stdout.write
|
---|
| 9 |
|
---|
| 10 | # check arguments
|
---|
| 11 | if len(sys.argv) < 5:
|
---|
| 12 | print "Usage: "+sys.argv[0]+" <src{PDB/XYZ}file> <destXYZfile> <offsetID> <offsetXYZ> <srcDBONDfile> [destDBONDfile]"
|
---|
| 13 | sys.exit(1)
|
---|
| 14 |
|
---|
| 15 | EPSILON=1e-3
|
---|
| 16 | CUTOFF=2.
|
---|
| 17 | inputsrc = open(sys.argv[1], "r")
|
---|
| 18 | inputdestPDB = open(sys.argv[2], "r")
|
---|
| 19 | inputsrcDBOND = open(sys.argv[5], "r")
|
---|
| 20 | offsetID=int(sys.argv[3])
|
---|
| 21 | offsetXYZ=int(sys.argv[4])
|
---|
| 22 | entries = sys.argv[1].split(".")
|
---|
| 23 | suffix = entries[-1]
|
---|
| 24 | if len(sys.argv) > 6:
|
---|
| 25 | output = open(sys.argv[6],"w")
|
---|
| 26 | else:
|
---|
| 27 | output = open(sys.argv[5]+".new", "w")
|
---|
| 28 |
|
---|
| 29 | # 1. first parse both PDB files into arrays (id, element, xyz) , therewhile scan BoundaryBoxes
|
---|
| 30 | max = [ 0., 0., 0. ]
|
---|
| 31 | min = [ 0., 0., 0. ]
|
---|
| 32 | x = [ 0., 0., 0. ]
|
---|
| 33 | nr=0
|
---|
| 34 | srcAtoms = []
|
---|
| 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:
|
---|
| 50 | print "Scanning source PDB file "+sys.argv[1]+"."
|
---|
| 51 | for 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 |
|
---|
| 70 | inputsrc.close()
|
---|
| 71 | print "Scanned "+str(nr)+" source atoms."
|
---|
| 72 |
|
---|
| 73 | print "Scanning destination XYZ file "+sys.argv[2]+"."
|
---|
| 74 | destAtoms = []
|
---|
| 75 | nr = 0
|
---|
| 76 | for 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
|
---|
| 88 | inputdestPDB.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 | #
|
---|
| 108 | print "Scanned "+str(nr)+" destination atoms."
|
---|
| 109 |
|
---|
| 110 | # 2. create Linked Cell with minimum distance box length
|
---|
| 111 | print "Found Box bounds [%f,%f]x[%f,%f]x[%f,%f]." % (min[0],max[0],min[1],max[1],min[2],max[2])
|
---|
| 112 | for 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
|
---|
| 117 | cells_x=int(math.ceil(float(max[0])/CUTOFF))+1
|
---|
| 118 | cells_y=int(math.ceil(float(max[1])/CUTOFF))+1
|
---|
| 119 | cells_z=int(math.ceil(float(max[2])/CUTOFF))+1
|
---|
| 120 | print "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)
|
---|
| 123 | cell=[]
|
---|
| 124 | for 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])
|
---|
| 130 | for 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])+"."
|
---|
| 140 | for 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
|
---|
| 152 | srcMatches=0
|
---|
| 153 | destMatches=0
|
---|
| 154 | Map = {}
|
---|
| 155 | i=-1
|
---|
| 156 | j=-1
|
---|
| 157 | k=-1
|
---|
| 158 | l=-1
|
---|
| 159 | e=-1
|
---|
| 160 | r=-1
|
---|
| 161 | t=-1
|
---|
| 162 | m=-1
|
---|
| 163 | for 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
|
---|
| 223 | print "We have "+str(srcMatches)+" matching atoms."
|
---|
| 224 | print "Mapping is:"
|
---|
| 225 | for 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
|
---|
| 231 | line=inputsrcDBOND.readline()
|
---|
| 232 | output.write(line)
|
---|
| 233 | for 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")
|
---|
| 250 | output.close()
|
---|
| 251 |
|
---|
| 252 | # exit
|
---|