File size: 12,048 Bytes
8efb4bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#ifndef PR_GRID_H
#define PR_GRID_H

#include <MoleculeGrid.h>
#include <Atom.h>

#include <vector>

class ProbGrid : public MoleculeGrid {
 public:
  ProbGrid(const Surface &surface, const float inDelta, const float maxRadius);

  template<class MoleculeT>
  void markProbs(const MoleculeT &M);

  float getProb(const Vector3 &point) const;
 private:
  std::vector< float > probs;
};

class VolumeGrid : public MoleculeGrid {
 public:
  VolumeGrid(const Surface &surface, const float inDelta, const float maxRadius);
  void computeVolumes(const float radius, const float maxRadius);
  void getVolumeFuncAndNormal(const Vector3 &center, float &volFunc, Vector3& volNormal) const;
  float getVolumeFunc(const Vector3 &center) const;
  const Vector3& getVolumeNormal(const Vector3 &center) const;
 private:
  std::vector< Vector3 > volumeNormals;
  std::vector< float > volumeFunctions;
};

class ResidueGrid : public MoleculeGrid {
 public:
  ResidueGrid(const Surface &surface, const float inDelta, const float maxRadius);

  template<class MoleculeT>
  void markResidues(const MoleculeT &M);
  int getResidueEntry(const Vector3 &point) const;
  void printGrid(std::ofstream& file) const;
 protected:
  std::vector< int > residues;
};

class AtomsGrid : public MoleculeGrid {
 public:
  AtomsGrid(const Surface &surface, const float inDelta, const float maxRadius);

  template<class MoleculeT>
  void markAtoms(const MoleculeT &M, float radius);
  const std::vector<unsigned int>& getAtoms(const Vector3 &point) const;
  unsigned int getAtomsNumber(const Vector3 &point) const;
 private:
  std::vector<std::vector<unsigned int > > atoms;
};

// stores closest atom number for every grid point
class AtomGrid : public MoleculeGrid {
 public:
  AtomGrid(const Surface &surface, const float inDelta, const float maxRadius):
    MoleculeGrid(surface, inDelta, maxRadius), atoms_(maxEntry) {}
  template<class MoleculeT>
  void markAtoms(const MoleculeT &mol, std::vector<float>& radii);

  // get volume corresponding to each atom
  template<class MoleculeT>
  void getVolume(const MoleculeT &mol, std::vector<float>& volume);

  unsigned int getAtom(const Vector3 &point) const;

 private:
  std::vector<unsigned int> atoms_;
};

class AtomTypeGrid : public MoleculeGrid {
 public:
  AtomTypeGrid(const Surface &surface, const float inDelta, const float maxRadius);

  template<class MoleculeT>
  void markAtomTypes(const MoleculeT &M);
  unsigned int getAtomType(const Vector3 &point) const {
     int index = getIndexForPoint(point);
     if(!isValidIndex(index))
       return 0;
     return atomTypes[index];
  }
 private:
  std::vector<unsigned int > atomTypes;
};


class SurfacePointGrid : public MoleculeGrid {
 public:
  SurfacePointGrid(const Surface &surface, const float inDelta, const float maxRadius);
  void computeDistAndPointers(const Surface &surface, bool precise = false);

  const SurfacePoint* getSpPointer(const Vector3& point) const {
    int index = getIndexForPoint(point);
    if (!isValidIndex(index))
      return NULL;
    return spPointers[index];
  }

  bool getDistAndSpPointer(const Vector3& point, float& distance, SurfacePoint* spPoint) const {
    int index = getIndexForPoint(point);
    if (!isValidIndex(index))
      return false;
    distance=grid[index];
    spPoint = (SurfacePoint *)spPointers[index];
    return true;
  }

  float getExactDist2(const Vector3& point) const {
    int index = getIndexForPoint(point);
    if(!isValidIndex(index) || spPointers[index]==NULL)
      return MAX_FLOAT*MAX_FLOAT;
    return point.dist2(spPointers[index]->position());
  }

  float getExactDist(const Vector3& point) const {
    int index = getIndexForPoint(point);
    if(!isValidIndex(index))
      return MAX_FLOAT;
    if(spPointers[index]==NULL)
      return grid[index];
    if(grid[index] < 0)
      return -(point.dist(spPointers[index]->position()));
    else
      return point.dist(spPointers[index]->position());
  }

 protected:
  // sets distance func. falue for index to be the distance from p.
  // and p to be the closest point
  bool setDistAndPointer(int index, const SurfacePoint* p) {
    float d = getPointForIndex(index).dist(p->position());
    if (d >= grid[index]) return false;
    grid[index] = d;
    spPointers[index] = p;
    return true;
  }
 private:
  std::vector<const SurfacePoint*> spPointers;
};

template<class MoleculeT>
void ProbGrid::markProbs(const MoleculeT &M) {
  std::vector<float> weights;
  weights.insert(weights.end(), maxEntry, 0.0);
  for(typename MoleculeT::const_iterator it=M.begin(); it!=M.end(); it++) {
    float atomRadius = it->getAtomRadius();
    float radius = atomRadius*2; //may be +1 is enough

    int centerIndex = getIndexForPoint(it->position());
    if (!isValidIndex(centerIndex)) {
      std::cerr << "Error: Point out of grid" << std::endl;
      exit(1);
    }

    int intRadius = getIntGridRadius(radius);
    int radius2 = intRadius*intRadius;

    int i_bound,j_bound,k_bound;
    i_bound = intRadius;
    for (int i = -i_bound; i <= i_bound; i++ ) {
      j_bound = (int)sqrt(radius2 - i*i);
      for (int j = -j_bound; j <= j_bound; j++) {
	k_bound = (int)sqrt(radius2 - i*i - j*j);
	for (int k = -k_bound; k <= k_bound; k++) {
	  int index = centerIndex + i + xGridNum * j + xyGridNum * k;
	  if(isValidIndex(index) && grid[index] <= 0) {
	    Vector3 point = getPointForIndex(index);
	    float dist = point.dist(it->position());
	    if(dist == 0.0) {
	      probs[index]=it->getSteadyProb();
	      continue; }
	    float weight = atomRadius/dist;
	    if(weight <= weights[index])
	      continue;
	    weights[index]=weight;
	    probs[index]=it->getSteadyProb();
	  }
	}
      }
    }
  }
  weights.clear();
}

template<class MoleculeT>
void ResidueGrid::markResidues(const MoleculeT &M) {
  std::vector<float> weights;
  weights.insert(weights.end(), maxEntry, 0.0);
  for(typename MoleculeT::const_iterator it=M.begin(); it!=M.end(); it++) {
    float atomRadius = it->getRadius();
    float radius = atomRadius*2; //may be +1 is enough

    int centerIndex = getIndexForPoint(it->position());
    if (!isValidIndex(centerIndex)) {
      std::cerr << "Error: Point out of grid" << std::endl;
      exit(1);
    }

    int intRadius = getIntGridRadius(radius);
    int radius2 = intRadius*intRadius;

    int i_bound,j_bound,k_bound;
    i_bound = intRadius;
    for (int i = -i_bound; i <= i_bound; i++ ) {
      j_bound = (int)sqrt(radius2 - i*i);
      for (int j = -j_bound; j <= j_bound; j++) {
	k_bound = (int)sqrt(radius2 - i*i - j*j);
	for (int k = -k_bound; k <= k_bound; k++) {
	  int index = centerIndex + i + xGridNum * j + xyGridNum * k;
	  if(isValidIndex(index) && grid[index] <= 0) {
	    Vector3 point = getPointForIndex(index);
	    float dist = point.dist(it->position());
	    if(dist == 0.0) {
	      if(it->isBackbone())
		residues[index] = M.residueEntry(it->chainId(), it->residueSequenceID()) * -1;
	      else
		residues[index] = M.residueEntry(it->chainId(), it->residueSequenceID());
	      continue;
	    }
	    float weight = atomRadius/dist;
	    if(weight <= weights[index])
	      continue;
	    weights[index] = weight;
	    if(it->isBackbone())
	      residues[index] = M.residueEntry(it->chainId(), it->residueSequenceID()) * -1;
	    else
	      residues[index] = M.residueEntry(it->chainId(), it->residueSequenceID());
	  }
	}
      }
    }
  }
  weights.clear();
}

template<class MoleculeT>
void AtomsGrid::markAtoms(const MoleculeT &M, float radius) {
  int atomIndex=0;
  int intRadius = getIntGridRadius(radius);
  int radius2 = intRadius*intRadius;

  for(typename MoleculeT::const_iterator it=M.begin(); it!=M.end(); it++, atomIndex++) {
    int centerIndex = getIndexForPoint(it->position());
    if (!isValidIndex(centerIndex)) {
      std::cerr << "Error: Point out of grid" << std::endl;
      exit(1);
    }

    int i_bound,j_bound,k_bound;
    i_bound = intRadius;
    for (int i = -i_bound; i <= i_bound; i++ ) {
      j_bound = (int)sqrt(radius2 - i*i);
      for (int j = -j_bound; j <= j_bound; j++) {
	k_bound = (int)sqrt(radius2 - i*i - j*j);
	for (int k = -k_bound; k <= k_bound; k++) {
	  int index = centerIndex + i + xGridNum * j + xyGridNum * k;
	  if(isValidIndex(index)) {
	    atoms[index].push_back(atomIndex);
	  }
	}
      }
    }
  }
}

template<class MoleculeT>
void AtomGrid::markAtoms(const MoleculeT &mol, std::vector<float>& radii) {
  std::vector<float> weights;
  weights.insert(weights.end(), maxEntry, 0.0);
  int atomIndex=0;
  for(typename MoleculeT::const_iterator it=mol.begin(); it!=mol.end(); it++, atomIndex++) {
    int centerIndex = getIndexForPoint(it->position());
    if (!isValidIndex(centerIndex)) {
      std::cerr << "Error: Point out of grid" << std::endl;
      exit(1);
    }
    float atomRadius = radii[atomIndex];
    float radius = atomRadius*2; //may be +1 is enough
    int intRadius = getIntGridRadius(radius);
    int radius2 = intRadius*intRadius;

    int i_bound,j_bound,k_bound;
    i_bound = intRadius;
    for (int i = -i_bound; i <= i_bound; i++ ) {
      j_bound = (int)sqrt(radius2 - i*i);
      for (int j = -j_bound; j <= j_bound; j++) {
	k_bound = (int)sqrt(radius2 - i*i - j*j);
	for (int k = -k_bound; k <= k_bound; k++) {
	  int index = centerIndex + i + xGridNum * j + xyGridNum * k;
	  if(isValidIndex(index)) {
	    Vector3 point = getPointForIndex(index);
	    float dist = point.dist(it->position());
	    if(dist == 0.0) {
	      atoms_[index] = atomIndex;
	      continue;
	    }
	    float weight = radius/dist;
	    if(weight <= weights[index]) continue;
	    weights[index] = weight;
	    atoms_[index] = atomIndex;
	  }
	}
      }
    }
  }
  weights.clear();
}

template<class MoleculeT>
void AtomGrid::getVolume(const MoleculeT &mol, std::vector<float>& volume) {
  std::vector<int> fullCube(mol.size(), 0), halfCube(mol.size(), 0);
  for(int i=0; i< (int)grid.size();i++) {
    if(grid[i] < 0)
      fullCube[atoms_[i]]++;
    if(grid[i] == 0)
      halfCube[atoms_[i]]++;
  }
  float cubeVolume = delta*delta*delta;
  float halfVolume = cubeVolume/2;
  volume.resize(mol.size(), 0.0);
  for(unsigned int i=0; i<mol.size(); i++) {
    volume[i] = cubeVolume * fullCube[i] + halfVolume * halfCube[i];
  }
}

template<class MoleculeT>
void AtomTypeGrid::markAtomTypes(const MoleculeT &M) {
  std::vector<float> weights;
  weights.insert(weights.end(), maxEntry, 0.0);
  int atomIndex=0;
  for(typename MoleculeT::const_iterator it=M.begin(); it!=M.end(); it++, atomIndex++) {
    float atomRadius = it->getRadius();
    float radius = atomRadius*2; //may be +1 is enough
    int centerIndex = getIndexForPoint(it->position());
    if (!isValidIndex(centerIndex)) {
      std::cerr << "Error: Point out of grid" << std::endl;
      exit(1);
    }

    int intRadius = getIntGridRadius(radius);
    int radius2 = intRadius*intRadius;

    int i_bound,j_bound,k_bound;
    i_bound = intRadius;
    for (int i = -i_bound; i <= i_bound; i++ ) {
      j_bound = (int)sqrt(radius2 - i*i);
      for (int j = -j_bound; j <= j_bound; j++) {
	k_bound = (int)sqrt(radius2 - i*i - j*j);
	for (int k = -k_bound; k <= k_bound; k++) {
	  int index = centerIndex + i + xGridNum * j + xyGridNum * k;
	  if(isValidIndex(index)) {
	    Vector3 point = getPointForIndex(index);
	    float dist = point.dist(it->position());
	    if(dist == 0.0) {
	      atomTypes[index] = it->getChemType();
	      continue;
	    }
	    float weight = atomRadius/dist;
	    if(weight <= weights[index])
	      continue;
	    weights[index] = weight;
	    atomTypes[index] = it->getChemType();
	  }
	}
      }
    }
  }
  weights.clear();
}

#endif