File: sideChainOptimizer.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 (279 lines) | stat: -rw-r--r-- 8,573 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
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/DOCKING/COMMON/sideChainOptimizer.h>
#include <BALL/SCORING/COMMON/gridBasedScoring.h>
#include <BALL/SYSTEM/timer.h>

using namespace std;

namespace BALL
{
	SideChainOptimizer::SideChainOptimizer(ScoringFunction* sf)
	{
		scoring_function_ = sf;
		frag_db_ = new FragmentDB("fragments/Fragments.db");
		rotamer_lib_ = new RotamerLibrary("rotamers/bbind02.May.lib", *frag_db_);
		residues_.clear();

		for (set<Residue*> ::const_iterator it = scoring_function_->flexible_residues_.begin();
			it!=scoring_function_->flexible_residues_.end(); it++)
		{
			residues_.push_back(*it);
		}
	}

	SideChainOptimizer::~SideChainOptimizer()
	{
		delete frag_db_;
		delete rotamer_lib_;
	}


	void SideChainOptimizer::setFlexibleResidues(const set<String>& residues_IDs)
	{
		residues_.clear();
		Protein* protein = dynamic_cast<Protein*>(scoring_function_->getReceptor());
		System* system;
		if (!protein && (system = dynamic_cast < System* > (scoring_function_->getReceptor())))
		{
			protein = dynamic_cast<Protein*>(system->getMolecule(0));
		}
		if (!protein)
		{
			throw BALL::Exception::GeneralException(__FILE__, __LINE__, "SideChainOptimizer::setFlexibleResidues() error", "Receptor is no Protein!");
		}
		set<Residue*> residue_set;
		for (ResidueIterator it = protein->beginResidue(); +it; it++)
		{
			String id = it->getID();
			if (residues_IDs.find(id) != residues_IDs.end())
			{
				residues_.push_back(&*it);
				residue_set.insert(&*it);
			}
		}
		scoring_function_->setFlexibleResidues(residue_set);
	}


	void SideChainOptimizer::optimize()
	{
		if (residues_.empty())
		{
			throw BALL::Exception::GeneralException(__FILE__, __LINE__, "SideChainOptimizer::optimize() error", "No Residues to be optimized! Use setFlexibleResidues() first!");
		}

		Log.level(5)<<"Starting optimization of "<<residues_.size()<<" receptor residues ... "<<endl;
		Timer timer;
		timer.start();
		bool change = false;
		HashGrid3<Atom*>* hashgrid_backup = scoring_function_->hashgrid_;
		scoring_function_->hashgrid_ = scoring_function_->flexible_residues_hashgrid_;

		scoring_function_->ScoringFunction::update();
		scoring_function_->ScoringFunction::updateScore();
		double best_score = scoring_function_->getScore();
		list<pair<const Residue*, const Rotamer*> > changed_residues_list;
		list<pair<String, Size> > new_rotamers; // contains pairs of residue ID, rotamer ID

		for (list < Residue* > ::iterator it = residues_.begin(); it != residues_.end(); it++)
		{
			ResidueRotamerSet* rotamer_set = rotamer_lib_->getRotamerSet(**it);

			list<Vector3> old_positions;
			for (AtomIterator atom_it = (*it)->beginAtom(); +atom_it; atom_it++)
			{
				old_positions.push_back(atom_it->getPosition());
			}

			Size no_rotamers = 0;
			Size no_invalid = 0;
			// Test all rotamers for the current residue
			ResidueRotamerSet::ConstIterator best_rotamer;
			Size best_rotamer_id = 0;
			bool found_new_rotamer = false;
			for (ResidueRotamerSet::ConstIterator it2 = rotamer_set->begin(); it2 != rotamer_set->end(); it2++, no_rotamers++)
			{
				bool assignment_ok = scoring_function_->assignRotamer(*it, rotamer_set, &*it2);

				// residue could not be assigned due to overlaps
				if (!assignment_ok)
				{
					continue;
					no_invalid++;
				}

				scoring_function_->ScoringFunction::update();
				scoring_function_->ScoringFunction::updateScore();
				double score = scoring_function_->getScore();
				if (score < (best_score-0.01))
				{
					best_score = score;
					best_rotamer = it2;
					best_rotamer_id = no_rotamers;
					found_new_rotamer = true;
					change = true;
				}
			}

			if (no_invalid > 0)
			{
				cout<<no_invalid<<" out of "<<no_rotamers<<" rotamer could not be used due to overlaps."<<endl;
			}

			if (found_new_rotamer)
			{
				rotamer_set->setRotamer(**it, *best_rotamer);
				Log.level(5)<<"  new rotamer for "<<(*it)->getFullName()<<(*it)->getID()<<" found. new score = "<<best_score<<endl;
				changed_residues_list.push_back(make_pair(*it, &*best_rotamer));
				new_rotamers.push_back(make_pair((*it)->getID(), best_rotamer_id));
			}
			else
			{
				scoring_function_->resetResiduePositions(*it, old_positions);
				Log.level(5)<<"  no better rotamer for "<<(*it)->getFullName()<<(*it)->getID()<<" found."<<endl;
			}
		}

		String s = "";
		for (list < pair < String, Size > > ::iterator it = new_rotamers.begin(); it != new_rotamers.end(); )
		{
			s += it->first+":"+String(it->second);
			it++;
			if (it != new_rotamers.end()) s += ", ";
		}
		if (s != "")
		{
			scoring_function_->getLigand()->setProperty("receptor_rotamers", s);
		}

		timer.stop();
		Log.level(5)<<"Optimization of receptor residue finished after "<<timer.getClockTime()<<" seconds."<<endl;

		bool use_precalc_flexgrids = true;

		if (change)
		{
			GridBasedScoring* gbs = dynamic_cast<GridBasedScoring*>(scoring_function_);
			if (gbs)
			{
				if (!use_precalc_flexgrids)
				{
					gbs->precalculateGrids(true);
				}
				else
				{
					gbs->loadFlexibleResidueScoreGrids(changed_residues_list);
				}
			}
		}

		scoring_function_->hashgrid_ = hashgrid_backup;
	}


	void SideChainOptimizer::findFlexibleSideChains(set<Residue*>& residues, const double& dist_cutoff, const double& min_B_factor)
	{
		int overlaps = 0;
		double backup_nonbonded_cutoff_2 = scoring_function_->nonbonded_cutoff_2_;
		scoring_function_->nonbonded_cutoff_2_ = pow(dist_cutoff, 2);
		AtomPairVector* pv = scoring_function_->createNonbondedPairVector(scoring_function_->hashgrid_, overlaps, 1, 0, 0);
		scoring_function_->nonbonded_cutoff_2_ = backup_nonbonded_cutoff_2;

		// Add all residues within cutoff distance to any ref ligand atom
		residues.clear();
		for (AtomPairVector::iterator it = pv->begin(); it != pv->end(); it++)
		{
			Residue* residue = it->second->getResidue();
			String name = residue->getName();

			// Ignore small residues
			if (name != "GLY" && name != "ALA"
				&& name!="PRO" && name!="VAL")
			{
				residues.insert(residue);
			}
		}
		delete pv;
		Log.level(10)<<"Found "<<residues.size()<<" residues near reference ligand."<<endl;

		// Check temperature factor for selected residues
		for (set<Residue*> ::iterator it = residues.begin(); it != residues.end(); )
		{
			double b_factor = 0;
			int no_pdbatoms = 0;
			for (AtomIterator atom_it = (*it)->beginAtom(); +atom_it; atom_it++)
			{
				PDBAtom* pdbatom = dynamic_cast<PDBAtom*>(&*atom_it);
				if (!pdbatom) continue;
				b_factor += pdbatom->getTemperatureFactor();
				no_pdbatoms++;
			}
			b_factor /= no_pdbatoms;
			if (b_factor < min_B_factor)
			{
				set<Residue*>::iterator del_it = it;
				it++;
				residues.erase(del_it);
			}
			else it++;
		}
		Log.level(10)<<residues.size()<<" of those residues have a temperature factor >="<<min_B_factor<<endl;

		// Check whether there is at least one other possible rotamer for each selected rotamer, i.e. one that does not produce sterical clashes.
		for (set<Residue*> ::iterator it = residues.begin(); it != residues.end(); )
		{
			ResidueRotamerSet* rotamer_set = rotamer_lib_->getRotamerSet(**it);

			list<Vector3> old_positions;
			for (AtomIterator atom_it = (*it)->beginAtom(); +atom_it; atom_it++)
			{
				old_positions.push_back(atom_it->getPosition());
			}

			Size no_rotamers = 0;
			Size no_valid_rotamers = 0;

			// Test all rotamers for the current residue
			ResidueRotamerSet::ConstIterator best_rotamer;
			for (ResidueRotamerSet::ConstIterator it2 = rotamer_set->begin(); it2 != rotamer_set->end(); it2++, no_rotamers++)
			{
				bool assignment_ok = scoring_function_->assignRotamer(*it, rotamer_set, &*it2);
				if (assignment_ok)
				{
					double rmsd = 0;
					int no_atoms = 0;

					// check whether the applied rotamer if any different for the one observed in the crystal structure
					list<Vector3>::iterator list_it = old_positions.begin();
					for (AtomIterator atom_it = (*it)->beginAtom(); +atom_it; atom_it++, list_it++)
					{
						rmsd = pow(atom_it->getPosition().getDistance(*list_it), 2);
						no_atoms++;
					}
					rmsd /= no_atoms;
					rmsd = sqrt(rmsd);

					if (rmsd > 0.25)
					{
						no_valid_rotamers++;
					}
				}
			}

			scoring_function_->resetResiduePositions(*it, old_positions);

			if (no_valid_rotamers == 0)
			{
				set<Residue*>::iterator del_it = it;
				it++;
				residues.erase(del_it);
			}
			else it++;
		}

		Log.level(10)<<"For "<<residues.size()<<" of those residues, at least one rotamer different from that observed in the crystal structure exists."<<endl;
	}
}