File: gridAnalysis.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (242 lines) | stat: -rw-r--r-- 6,885 bytes parent folder | download | duplicates (7)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//


#include <BALL/DOCKING/COMMON/gridAnalysis.h>

using namespace std;

namespace BALL
{
	GridAnalysis::GridAnalysis(ScoringFunction* sf, AtomContainer* probe_group)
		: scoring_function_(sf)
	{
		// use a copy to prevent that moving the original compound invalidates the poses obtained by getBestPoses()
		probe_group_ = *probe_group;
		no_best_poses_ = 0;
		resolution_ = -1;
	}

	void GridAnalysis::enableSavingBestPoses(Size number_best_poses)
	{
		no_best_poses_ = number_best_poses;
	}


	void GridAnalysis::setResolution(const double& resolution)
	{
		resolution_ = resolution;
	}

	RegularData3D* GridAnalysis::calculate()
	{
		// restore the ligand after all calculations, in case the scoring-function is also used for something else...
		AtomContainer* backup_ligand = scoring_function_->getLigand();
		best_poses_.clear();

		scoring_function_->setLigand(probe_group_);

		if (resolution_ < 0) // resolution has not been set manually
		{
			double radius = scoring_function_->getLigandRadius();
			resolution_ = radius/2;
			if (resolution_ < 1) resolution_ = 1;
		}

		const HashGrid3<Atom*>* hashgrid = scoring_function_->getHashGrid();
		origin_ = hashgrid->getOrigin();

		Vector3 hashgrid_resolution = hashgrid->getUnit();
		Size no_x_steps = (Size)((hashgrid->getSizeX()*hashgrid_resolution[0])/resolution_);
		Size no_y_steps = (Size)((hashgrid->getSizeY()*hashgrid_resolution[1])/resolution_);
		Size no_z_steps = (Size)((hashgrid->getSizeZ()*hashgrid_resolution[2])/resolution_);

		Vector3 dimension(no_x_steps*resolution_, no_y_steps*resolution_, no_z_steps*resolution_);
		Vector3 resolution(resolution_, resolution_, resolution_);
		RegularData3D* reg3d = new RegularData3D(origin_, dimension, resolution);

		Size no_atoms = scoring_function_->getNoLigandAtoms();
		bool enable_rotation = 0;
		AtomIterator it = probe_group_.beginAtom();
		if (it == probe_group_.endAtom())
		{
			cerr<<"Error, probe group has no atoms!!"<<endl;
			return 0;
		}
		it++;
		if (it != probe_group_.endAtom()) enable_rotation = 1;

		center_ = scoring_function_->getLigandCenter();

		for (Size x = 0; x < no_x_steps; x++)
		{
			for (Size y = 0; y < no_y_steps; y++)
			{

				for (Size z = 0; z < no_z_steps; z++)
				{
					Vector3 position(origin_[0]+(x+0.5)*resolution_, origin_[1]+(y+0.5)*resolution_, origin_[2]+(z+0.5)*resolution_);

					moveProbeGroup_(position);

					scoring_function_->update();
					double score = scoring_function_->updateScore();
					int best_angle_x = 0;
					int best_angle_y = 0;
					int best_angle_z = 0;

					// if probe-group has more than one atom, enable rotation around all three axes and store minimal score in RegularData3D
					if (enable_rotation && score < 0.75e10*no_atoms)
					{
						for (Size i = 1; i < 36; i++)
						{
							rotateProbeGroup_(0, 10);
							scoring_function_->update();
							double score_i = scoring_function_->updateScore();
							if (score_i < score)
							{
								score = score_i;
								best_angle_x = i*10;
							}
						}
						rotateProbeGroup_(0, -350+best_angle_x);
						for (Size i = 1; i < 36; i++)
						{
							rotateProbeGroup_(1, 10);
							scoring_function_->update();
							double score_i = scoring_function_->updateScore();
							if (score_i < score)
							{
								score = score_i;
								best_angle_y = i*10;
							}
						}
						rotateProbeGroup_(1, -350+best_angle_y);
						for (Size i = 1; i < 36; i++)
						{
							rotateProbeGroup_(2, 10);
							scoring_function_->update();
							double score_i = scoring_function_->updateScore();
							if (score_i < score)
							{
								score = score_i;
								best_angle_z = i*10;
							}
						}

						// rotate back to original orientation
						rotateProbeGroup_(2, -350);
						rotateProbeGroup_(1, -best_angle_y);
						rotateProbeGroup_(0, -best_angle_x);
					}

					RegularData3D::IndexType index(x, y, z);
					reg3d->getData(index) = score;

					if (no_best_poses_ > 0) // if saving best positions is enabled
					{
						if (best_poses_.size() < no_best_poses_ || score < best_poses_.rbegin()->first)
						{
							bool insert = 1;

							list<PoseList::iterator> to_be_deleted;

							for (PoseList::iterator it = best_poses_.begin(); it != best_poses_.end(); it++)
							{
								if (it->second.first.getDistance(position) < 3*resolution_)
								{
									if (score < it->first) // current position is better, so remove the other position
									{
										to_be_deleted.push_back(it);
									}
									else // current position is worse, so do not insert it
									{
										insert = false;
										break;
									}
								}
							}

							if (insert)
							{
								for (list < PoseList::iterator > ::iterator it = to_be_deleted.begin(); it != to_be_deleted.end(); it++)
								{
									best_poses_.erase(*it);
								}

								Vector3 rotation(best_angle_x, best_angle_y, best_angle_z);
								best_poses_.insert(make_pair(score, make_pair(position, rotation)));
							}
						}
						if (best_poses_.size() > no_best_poses_)
						{
							PoseList::iterator it = best_poses_.end();
							it--;
							best_poses_.erase(it);
						}
					}
				}
			}
		}

		scoring_function_->setLigand(*backup_ligand);
		return reg3d;
	}


	list<AtomContainer*> GridAnalysis::getBestPoses()
	{
		list<AtomContainer*> container_list;
		if (best_poses_.size() == 0) return container_list; // return empty list

		AtomContainer probe_group_backup = probe_group_;
		Vector3 center_backup = center_;

		for (PoseList::iterator it = best_poses_.begin();
			it!=best_poses_.end(); it++)
		{
			probe_group_ = probe_group_backup;
			center_ = center_backup;
			moveProbeGroup_(it->second.first);
			rotateProbeGroup_(0, (int)it->second.second[0]);
			rotateProbeGroup_(1, (int)it->second.second[1]);
			rotateProbeGroup_(2, (int)it->second.second[2]);

			AtomContainer* new_container = new AtomContainer;
			*new_container = probe_group_; // copy the probe-group in the assigned pose
			container_list.push_back(new_container);
		}

		probe_group_ = probe_group_backup;
		center_ = center_backup;
		return container_list;
	}


	void GridAnalysis::moveProbeGroup_(const Vector3& destination)
	{
		TMatrix4x4<float> M;
		Vector3 translation_vector = destination-center_;
		M.setTranslation(translation_vector);
		for (AtomIterator it = probe_group_.beginAtom(); +it; it++)
		{
			it->setPosition(M*it->getPosition());
		}
		center_ = destination;
	}


	void GridAnalysis::rotateProbeGroup_(int axis, int degree)
	{
		TMatrix4x4<float> M;
		TAngle<float> angle(degree, false);
		Vector3 axis_vector(axis == 0, axis == 1, axis == 2);
		M.setRotation(angle, axis_vector);

		for (AtomIterator it = probe_group_.beginAtom(); it != probe_group_.endAtom(); it++)
		{
			it->setPosition(M*(it->getPosition()-center_)+center_);
		}
	}
}