File: dockingController.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (391 lines) | stat: -rw-r--r-- 11,446 bytes parent folder | download | duplicates (4)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/VIEW/WIDGETS/dockingController.h>
#include <BALL/VIEW/WIDGETS/molecularStructure.h>

#include <BALL/VIEW/DIALOGS/dockResultDialog.h>
#include <BALL/VIEW/DIALOGS/dockProgressDialog.h>

#include <BALL/VIEW/DATATYPE/standardDatasets.h>

#include <BALL/VIEW/KERNEL/message.h>
#include <BALL/VIEW/KERNEL/mainControl.h>

#include <BALL/DOCKING/COMMON/dockResult.h>
#include <BALL/DOCKING/COMMON/conformationSet.h>
#include <BALL/DOCKING/COMMON/dockingAlgorithm.h>
#include <BALL/SCORING/FUNCTIONS/energeticEvaluation.h>
#include <BALL/SCORING/FUNCTIONS/amberEvaluation.h>
#include <BALL/SCORING/FUNCTIONS/randomEvaluation.h>

#ifdef BALL_HAS_FFTW
#include <BALL/VIEW/DIALOGS/geometricFitDialog.h>
#include <BALL/DOCKING/geometricFit.h>
#endif

#include <BALL/VIEW/KERNEL/threads.h>

#include <QtWidgets/QMessageBox>
#include <QtWidgets/QComboBox>

using namespace std;

namespace BALL
{
	namespace VIEW
	{

		DockingController::DockingController(QWidget* parent, const char* name)
			:	QWidget(parent),
				ModularWidget(name),
				dock_dialog_(this),
				dock_result_dialog_(0),
				progress_dialog_(0),
				dock_alg_(0),
				thread_(0)
		{
			#ifdef BALL_VIEW_DEBUG
				Log.info() << "New DockingController " << this << std::endl;
			#endif
			registerWidget(this);
			setObjectName(name);
			hide();
		}

		// Copy constructor.
		DockingController::DockingController(const DockingController& dock_controller)
			: QWidget(),
				ModularWidget(dock_controller),
				dock_dialog_(),
				dock_result_dialog_(0),
				progress_dialog_(0),
				dock_alg_(0),
				thread_(0)
		{}
		
		// Destructor
		DockingController::~DockingController()
		{
			// remark: progress dialog is automatically deleted because its parent is the docking controller
			
			if (thread_ != 0)
			{
				thread_->terminate();
				thread_->wait();
				delete thread_;
			}

			if (dock_alg_ != 0)
			{
				delete dock_alg_;
				dock_alg_ = 0;
			}
		}
		
		// Assignment operator
		const DockingController& DockingController::operator = (const DockingController&)
		{
			return *this;
		}
		
		// Message handling method
		void DockingController::onNotify(Message *message)
		{
			// if (re)docking has finished, start scoring
			// first check if user has aborted (re)docking
            if (RTTI::isKindOf<DockingFinishedMessage>(message))
			{
				DockingFinishedMessage* dfm = RTTI::castTo<DockingFinishedMessage>(*message);
				
				unlockComposites();
				
				progress_dialog_->close();
				delete progress_dialog_;
				progress_dialog_ = 0;
				
				if (dfm->wasAborted())
				{
					if (QMessageBox::question(0, "Request","Do you want to see the current Result?", 
																		 QMessageBox::Yes,
																		 QMessageBox::No,
																		 QMessageBox::NoButton))
					{
						return;
					}
				}
				
				setStatusbarText(tr("Starting scoring..."), true);
				if(!runScoring_((ConformationSet*)(dfm->getConformationSet())))
				{
				 delete dfm->getConformationSet();
				}
				setStatusbarText(tr("Scoring finished."), true);
				return;
			}

			// DatasetControl sends this messages, when user wants to have a look at a DockResult
            if (RTTI::isKindOf<DatasetMessage>(message))
			{
				DatasetMessage* sdrm = RTTI::castTo<DatasetMessage>(*message);
				if (!sdrm->isValid()) return;
				DockResultDataset* set = dynamic_cast<DockResultDataset*>(sdrm->getDataset());
				if (set == 0) return;
				if (set->getType() != DockResultController::type) return;

				if (dock_result_dialog_ == 0) dock_result_dialog_ = new DockResultDialog(this);
					
				dock_result_dialog_->setDockResult(set->getData());
				dock_result_dialog_->setDockedSystem(dynamic_cast<System*>(set->getComposite()));
				dock_result_dialog_->show();
			}
		}
		
		// Initializes the popup menu Molecular Mechanics with its checkable submenu Docking.
		void DockingController::initializeWidget(MainControl& main_control)
		{			
			action_ = insertMenuEntry(MainControl::MOLECULARMECHANICS, tr("&Docking"), this,
													      SLOT(startDocking()), "Shortcut|MolecularMechanics|Dock", QKeySequence("Ctrl + D"), 
																tr("Dock two systems."), UIOperationMode::MODE_ADVANCED);
			dock_dialog_.initializeWidget();
		}
		
		// Fetches the preferences from the INIFile
		void DockingController::fetchPreferences(INIFile& file)
		{
			dock_dialog_.fetchPreferences(file);
		}

		// Writes the preferences to the INIFile.
		void DockingController::writePreferences(INIFile& file)
		{
			dock_dialog_.writePreferences(file);
		}
		
		// Updates the state of menu entry Docking in the popup menu Molecular Mechanics.
		void DockingController::checkMenu(MainControl& main_control)
		{
			// if composites are locked disable menu entry "Docking"
			if (main_control.isBusy())
			{
				action_->setEnabled(false);
				return;
			}

			// get to know how many composites are loaded
			CompositeManager& composite_manager = main_control.getCompositeManager();
			Size num_systems = composite_manager.getNumberOfComposites();

			// if no or only one system loaded, disable menu entry "Docking"
			action_->setEnabled(num_systems > 1);
		}
		
		DockDialog& DockingController::getDockDialog()	
		{
		 return dock_dialog_;
		}			
		
		// is called when user clicks on menu entry "Docking"
		void DockingController::startDocking()
		{
			// Dialog is called in docking-modus, not in redocking-modus
			runDocking(false);
		}
		
		// Show docking dialog, check which algorithm is chosen and create new DockingAlgorithm object.
		// Start new Thread and fill/show ProgressDialog.
		void DockingController::runDocking(bool is_redock)
		{
			// Make sure we run just one instance at a time.
			if (getMainControl()->isBusy())
			{
				Log.error() << "Docking already running! " << __FILE__ << " " << __LINE__ << std::endl;
				return;
			}
			
			dock_dialog_.isRedock(is_redock);
			
			//if cancel was pressed in DockDialog, don't start docking
			if (!dock_dialog_.exec()) return;

			if (dock_alg_ != 0)
			{
				delete dock_alg_;
				dock_alg_ = 0;
			}
		
			// check which algorithm is chosen and create a DockingAlgorithm object
			Index index = dock_dialog_.algorithms->currentIndex();
			switch(index)
			{
				case GEOMETRIC_FIT:
#ifdef BALL_HAS_FFTW
					dock_alg_ =  new GeometricFit();
#endif
					break;
			}
			
			if (!dock_alg_ || 
					!dock_dialog_.getSystem1() || 
					!dock_dialog_.getSystem2() || 
					dock_dialog_.getAlgorithmOptions().isEmpty())
			{
			 	return;
			}
			
			// Set up the docking algorithm
			setStatusbarText(tr("Setting up docking algorithm..."), true);
			// keep the larger protein in System A and the smaller one in System B
			// and setup the algorithm
			System& s1 = *dock_dialog_.getSystem1();
			System& s2 = *dock_dialog_.getSystem2();
			if (s1.countAtoms() < s2.countAtoms())
			{
				dock_alg_->setup(s2, s1, dock_dialog_.getAlgorithmOptions());
			}
			else
			{
				dock_alg_->setup(s1, s2, dock_dialog_.getAlgorithmOptions());
			}
			
			if (!(getMainControl()->lockCompositesFor(this))) return;

			if (thread_ != 0)
			{
				thread_->terminate();
				thread_->wait();
				delete thread_;
			}

			thread_ = new DockingThread;
			thread_->setDockingAlgorithm(dock_alg_);
			thread_->setMainControl(getMainControl());

			progress_dialog_ = new DockProgressDialog(this);
			// dialog is deleted by itself when it's closed
			progress_dialog_->fillDialog(dock_dialog_.systems1->currentText(),
																	dock_dialog_.systems2->currentText(),
																	dock_dialog_.algorithms->currentText(),
																	dock_dialog_.scoring_functions->currentText(),
																	dock_dialog_.getAlgorithmOptions(),
																	dock_dialog_.getScoringOptions());
			progress_dialog_->setDockingAlgorithm(dock_alg_);

			// start thread
			// function calls DockingThread::run()
			thread_->start();
			progress_dialog_->show();
		}

		EnergeticEvaluation* DockingController::createEvaluationMethod(Index method)
		{
			MolecularStructure* mol_struct = MolecularStructure::getInstance(0);
			if (!mol_struct)
			{
				Log.error() << "Error while scoring with AMBER_FF! " << __FILE__ << " " << __LINE__ << std::endl;
				return 0;
			}

			switch(method)
			{
				case DEFAULT:
					return new EnergeticEvaluation();

				case AMBER_FF:
					return new AmberEvaluation(mol_struct->getAmberFF());

				case MMFF94_FF:
					return new ForceFieldEvaluation(mol_struct->getMMFF94());

				case SELECTED_FF:
					return new ForceFieldEvaluation(mol_struct->getForceField());

				case RANDOM:
					return new RandomEvaluation;
			}

			return 0;
		}

		
		// Apply scoring function which user has chosen.
		// Then, create new DockResult and add new scoring to it.
		// At the end, add the docked system to BALLView structures
		// and send a NewDockResultMessage to insert the DockResult in DatasetControl.
		bool DockingController::runScoring_(ConformationSet* conformation_set)
		{
			if (!conformation_set) return false;

			if (!conformation_set->size())
			{
				Log.error() << "There are no docking results! " << __FILE__ << " " << __LINE__ << std::endl;
				return false;
			}
		
			//check which scoring function is chosen
			Index index = dock_dialog_.scoring_functions->currentIndex();
			EnergeticEvaluation* scoring = createEvaluationMethod(index);
	
			if (!scoring) return false;
			
			// apply scoring function; set new scores in the conformation set
			vector<ConformationSet::Conformation> ranked_conformations;
			try
	   	{
				ranked_conformations = (*scoring)(*conformation_set);
			}
			catch(...)
			{
				Log.error() << "Scoring of docking results failed! " << __FILE__ << " " << __LINE__ << std::endl;
				// delete instance 
				if (scoring != 0)
				{
					delete scoring;
					scoring = 0;
				}
				return false;
			}
			conformation_set->setScoring(ranked_conformations);

			// create new DockResult and add a new scoring to it;
			// we need the name, options and score vector of the scoring function
			DockResult* dock_res = new DockResult(ascii(dock_dialog_.algorithms->currentText()),
																						conformation_set,
																						dock_dialog_.getAlgorithmOptions()); 
			// dock result is deleted by DatasetControl
		
			// sort vector ranked_conformations by snapshot numbers
			sort(ranked_conformations.begin(), ranked_conformations.end());
			
			dock_res->addScoring(ascii(dock_dialog_.scoring_functions->currentText()), 
													 dock_dialog_.getScoringOptions(), ranked_conformations);

			// add docked system to BALLView structures
			const SnapShot& best_result = (*conformation_set)[0];

			System* docked_system = new System(conformation_set->getSystem());
			// system is deleted by main control, when it is removed from BallView
			best_result.applySnapShot(*docked_system);
			getMainControl()->deselectCompositeRecursive(docked_system, true);
			getMainControl()->insert(*docked_system);
			
			// send a Message
			DockResultDataset* set = new DockResultDataset;
			set->setData(dock_res);
			set->setComposite(docked_system);
			set->setType(DockResultController::type);
			set->setName(dock_res->getDockingAlgorithm() + " result");
			notify_(new DatasetMessage(set, DatasetMessage::ADD));

			// delete instance 
			if (scoring != 0)
			{
				delete scoring;
				scoring = 0;
			}
			return true;
		}
		
	} // end of namespace View
} // end of namespace BALL