File: distanceBasedSeqs2Tree.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (554 lines) | stat: -rw-r--r-- 23,134 bytes parent folder | download | duplicates (5)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// $Id: distanceBasedSeqs2Tree.cpp 6002 2009-03-20 19:39:03Z privmane $

#include "distanceBasedSeqs2Tree.h"
#include "uniDistribution.h"
#include "distanceTable.h"
#include "bestAlpha.h"
#include "siteSpecificRate.h"
#include "someUtil.h"
#include "bblEM.h"
#include "tamura92.h"
#include "bestTamura92param.h"
#include "bestGtrModelParams.h"
#include <float.h>
#include "replacementModelSSRV.h"
#include "trivialAccelerator.h"

// **********************************************************************
// *** The basic non-iterative versions *********************************
// **********************************************************************

tree distanceBasedSeqs2Tree::seqs2Tree(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;

	// Calculate distance table
	tree et;
	VVdouble distTable;
	vector<string> vNames;
	giveDistanceTable(_distM,sc,distTable,vNames,_weights);

	// Build tree from the distance table
	et = _dist2et->computeTree(distTable, vNames, _constraintTreePtr);

	LOG(6,<<"# distanceBasedSeqs2Tree::seqs2Tree: The reconsructed tree:"<<endl);
	LOGDO(6,et.output(myLog::LogFile()));

	return et;
}

tree distanceBasedSeqs2Tree::seqs2TreeBootstrap(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	return seqs2Tree(sc, weights, constraintTreePtr);
}

// **********************************************************************
// *** iterativeDistanceSeqs2Tree ***************************************
// **********************************************************************

iterativeDistanceSeqs2Tree::iterativeDistanceSeqs2Tree(likeDist &distM, distances2Tree &dist2et, const Vdouble *weights,
													   const MDOUBLE epsilonLikelihoodImprovement, 
													   const MDOUBLE epsilonLikelihoodImprovement4alphaOptimiz, 
													   const MDOUBLE epsilonLikelihoodImprovement4BBL, 
													   const int maxIterationsBBL)
	: distanceBasedSeqs2Tree(distM, dist2et, weights),
	  _epsilonLikelihoodImprovement             ( epsilonLikelihoodImprovement             ),
	  _epsilonLikelihoodImprovement4alphaOptimiz( epsilonLikelihoodImprovement4alphaOptimiz),
	  _epsilonLikelihoodImprovement4BBL         ( epsilonLikelihoodImprovement4BBL         ),
	  _maxIterationsBBL                         ( maxIterationsBBL                         )
{
	// Check that the stochasticProcess in likeDist is not const
	if (distM.isTheInternalStochasticProcessConst()) {
		errorMsg::reportError("iterativeDistanceSeqs2Tree::iterativeDistanceSeqs2Tree: The stochasticProcess in the given likeDist object is const. A non-const stochasticProcess is required.");
	}

	// Keep a pointer to the stochasticProcess in distM, so that we will be able to change its alpha, etc.
	_spPtr = &(distM.getNonConstStochasticProcess());
	if (_spPtr->categories() >1)
		_alpha = (static_cast<gammaDistribution*>(_spPtr->distr()))->getAlpha();
	else
		_alpha=-99.9;				// this should never be used

}

// *** Iterative tree building ******************************************
tree iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternal(const sequenceContainer &sc, bool initSideInfoGiven) {
	LOGDO(3,printTime(myLog::LogFile()));
	LOG(3,<<"# iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternal:"<<endl<<"# Initial tree:"<<endl);
	seqs2TreeOneIterationInternal(sc, initSideInfoGiven);

	return seqs2TreeIterativeInternalInitTreeGiven(sc, true, _newTree, _newAlpha);
}

// *** Iterative tree building, given an initial tree and alpha *********
// *** Optimize branch lengths and sideInfo for the given tree topology
tree iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven(const sequenceContainer &sc, const tree &initTree) {
	LOG(7,<<"# iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven: Started optimizeSideInfo. ");
	LOGDO(7,printTime(myLog::LogFile()));
	_newTree=initTree;
	_newTreeLogLikelihood=optimizeSideInfo(sc, _newTree);
	LOG(7,<<"# iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven: Finished optimizeSideInfo. ");
	LOGDO(7,printTime(myLog::LogFile()));

	return seqs2TreeIterativeInternalInitTreeGiven(sc, true, _newTree, _newAlpha);
}

// *** Iterative tree building, given an initial tree and alpha *********
// *** If sideInfo is not given - calculate it for the fixed tree and alpha
tree iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven(const sequenceContainer &sc, bool initSideInfoGiven, const tree &initTree, MDOUBLE initAlpha) {
	_newTree=initTree;
	_newAlpha=initAlpha;

	LOGDO(3,printTime(myLog::LogFile()));
	LOG(3,<<"# iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven"<<endl);
	if (!initSideInfoGiven) {
		_newTreeLogLikelihood=calcSideInfoGivenTreeAndAlpha(sc, initTree, initAlpha);
	}
	int iterationNum = 0;
	LOGDO(3,printTime(myLog::LogFile()));
	LOG(3,<<"# iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven:"<<endl<<"# The given initial tree:"<<endl);
	LOGDO(3,_newTree.output(myLog::LogFile()));
  
	do {
		++iterationNum;
		LOGDO(5,printTime(myLog::LogFile()));
		LOG(3,<<"# Iteration "<<iterationNum<<":"<<endl);

		// save the best tree so far, and its likelihood and the sideInfo that was calculated for it
		_et=_newTree;				
		_treeLogLikelihood=_newTreeLogLikelihood;
		acceptSideInfo();
		LOG(7,<<"# Side info for the tree"<<endl);
		LOGDO(7,printSideInfo(myLog::LogFile()));

		seqs2TreeOneIterationInternal(sc, true);

	} while (_newTreeLogLikelihood > _treeLogLikelihood + _epsilonLikelihoodImprovement);

	LOGDO(3,printTime(myLog::LogFile()));
	LOG(3,<<"# iterativeDistanceSeqs2Tree::seqs2TreeIterativeInternalInitTreeGiven:"<<endl<<"# Finished iterative distance-based tree reconstruction, done "<<iterationNum<<" iterations"<<endl);
	return _et;
}

// *** Tree building procedure that is called iteratively **********************
void iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal(const sequenceContainer &sc, const bool sideInfoSet) {

	// 1. Calculate distance table
	VVdouble distTable;
	vector<string> vNames;
	LOG(7,<<"# iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal: Started giveDistanceTable. ");
	LOGDO(7,printTime(myLog::LogFile()));
	if (!sideInfoSet) { // Then use homogeneous rates

		// Create homogeneous likeDist
		_alpha = 1.5;		// Since no ASRV side info is known yet, we set an initial alpha for bestAlphaAndBBL optimizations
		uniDistribution distribution;
		stochasticProcess* uniDistSp = NULL;
		replacementModelSSRV* rmSSRV = 
			dynamic_cast<replacementModelSSRV*>(_spPtr->getPijAccelerator()->getReplacementModel());
		if (!rmSSRV) {
			uniDistSp = new stochasticProcess(&distribution, _spPtr->getPijAccelerator());
		} else {
			trivialAccelerator pijAcc(rmSSRV->getBaseRM());
			uniDistSp = new stochasticProcess(&distribution, &pijAcc);
		}
		likeDist homogeneousDist(*uniDistSp,static_cast<likeDist*>(_distM)->getToll());

		giveDistanceTable(&homogeneousDist,sc,distTable,vNames,_weights);
		delete uniDistSp;

	} else {			// use the side information
		utilizeSideInfo();
		giveDistanceTable(_distM,sc,distTable,vNames,_weights);
	}
	LOG(7,<<"# iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal: Finished giveDistanceTable, started distances2Tree::computeTree. ");
	LOGDO(7,printTime(myLog::LogFile()));

	// 2. Build tree from the distance table
	_newTree = _dist2et->computeTree(distTable, vNames, _constraintTreePtr);
	LOG(7,<<"# iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal: Finished distances2Tree::computeTree, started optimizeSideInfo. ");
	LOGDO(7,printTime(myLog::LogFile()));

	// 3. Optimize branch lengths and side info for the tree topology
	_newTreeLogLikelihood=optimizeSideInfo(sc, _newTree);
	LOG(7,<<"# iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal: Finished distances2Tree::optimizeSideInfo. ");
	LOGDO(7,printTime(myLog::LogFile()));

	if (!sideInfoSet) {
		LOG(5,<<"# iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal:"<<endl<<"# Homogeneous rates tree"<<endl);
	} else {
		LOG(5,<<"# iterativeDistanceSeqs2Tree::seqs2TreeOneIterationInternal:"<<endl<<"# Tree based on alpha"<<endl);
	}
	LOGDO(5,_newTree.output(myLog::LogFile()));
	LOG(5,<<"# Log likelihood:"<<endl<<_newTreeLogLikelihood<<endl);
}

// Perform one bootstrap iteration, assuming that side info has been set (as if acceptSideInfo has been called)
tree iterativeDistanceSeqs2Tree::seqs2TreeBootstrap(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	LOG(3,<<"# iterativeDistanceSeqs2Tree::seqs2TreeBootstrap: Started a single bootstrap iteration. ");
	LOGDO(3,printTime(myLog::LogFile()));
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;

	// Calculate distance table
	tree localScopeEt;
	VVdouble distTable;
	vector<string> vNames;
	utilizeSideInfo();
	giveDistanceTable(_distM,sc,distTable,vNames,_weights);

	// Build tree from the distance table
	localScopeEt = _dist2et->computeTree(distTable,vNames, _constraintTreePtr);

	LOG(3,<<"# iterativeDistanceSeqs2Tree::seqs2TreeBootstrapInternal:"<<endl<<"# Bootstrap tree based on alpha, without optimizations"<<endl);
	LOGDO(3,localScopeEt.output(myLog::LogFile()));

	return localScopeEt;
}

/********************************
 * commonAlphaDistanceSeqs2Tree *
 ********************************/
tree commonAlphaDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, MDOUBLE initAlpha, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_alpha = initAlpha;
	_weights = weights;
	return seqs2TreeIterativeInternal(sc, true);
}

tree commonAlphaDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternal(sc, false);
}

tree commonAlphaDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, initTree);
}

tree commonAlphaDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, MDOUBLE initAlpha, const Vdouble *weights, const tree* constraintTreePtr) {
	_alpha = initAlpha;
	_weights = weights;

	_constraintTreePtr=constraintTreePtr;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, true, initTree, initAlpha);
}

// NOTE! This version is a NON-ITERATIVE version that uses the side info supplied by the user
tree commonAlphaDistanceSeqs2Tree::seqs2Tree(const sequenceContainer &sc, MDOUBLE alpha, const Vdouble *weights, const tree* constraintTreePtr) {
	_weights = weights;
	_alpha = alpha;
	_constraintTreePtr=constraintTreePtr;
	seqs2TreeOneIterationInternal(sc, true);
	return _newTree;
}

tree commonAlphaDistanceSeqs2Tree::seqs2TreeBootstrap(const sequenceContainer &sc, const MDOUBLE alpha, const Vdouble *weights, const tree* constraintTreePtr) {
	_weights = weights;
	_alpha = alpha;
	return static_cast<iterativeDistanceSeqs2Tree *>(this)->seqs2TreeBootstrap(sc, weights, constraintTreePtr);
}

// NOTE! This version calls ITERATIVE seqs2Tree because side info is not given by the user, so we have to generate and optimize it
tree commonAlphaDistanceSeqs2Tree::seqs2Tree(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	return seqs2TreeIterative(sc,weights,constraintTreePtr);
}

MDOUBLE commonAlphaDistanceSeqs2Tree::optimizeSideInfo(const sequenceContainer &sc, tree &et)
{
	if (dynamic_cast<tamura92*>(_spPtr->getPijAccelerator()->getReplacementModel())) {
		// Optimizing params of the tamura92 model
		bestTamura92ParamAlphaAndBBL optimizer(et, sc, *_spPtr, _weights, 5, _epsilonLikelihoodImprovement/*0.05*/,
											   _epsilonLikelihoodImprovement4alphaOptimiz/*0.01*/, 
											   _epsilonLikelihoodImprovement4alphaOptimiz/*0.01*/, 
											   _epsilonLikelihoodImprovement4alphaOptimiz/*0.01*/, 
											   _epsilonLikelihoodImprovement4BBL/*0.01*/,
											   5.0, _maxIterationsBBL, _alpha, 5.0 );
		_newAlpha=optimizer.getBestAlpha();
		return(optimizer.getBestL());

	} else if (dynamic_cast<gtrModel*>(_spPtr->getPijAccelerator()->getReplacementModel())) {
		// Optimizing params of the gtr model
		bestGtrModel optimizer(et, sc, *_spPtr, _weights, 5,
							   _epsilonLikelihoodImprovement,
							   _epsilonLikelihoodImprovement4alphaOptimiz,
							   true, true);
		_newAlpha=optimizer.getBestAlpha();
		return(optimizer.getBestL());

	} else {
		bestAlphaAndBBL optimizer(et, sc, *_spPtr, _weights, _alpha, 5.0,
								  _epsilonLikelihoodImprovement4BBL/*0.01*/, _epsilonLikelihoodImprovement4alphaOptimiz,
								  _maxIterationsBBL);
		_newAlpha=optimizer.getBestAlpha();
		return(optimizer.getBestL());
	}
}

MDOUBLE commonAlphaDistanceSeqs2Tree::calcSideInfoGivenTreeAndAlpha(const sequenceContainer &sc, const tree &et, MDOUBLE alpha) 
{
	_newAlpha = alpha;
	(static_cast<gammaDistribution*>(_spPtr->distr()))->setAlpha(alpha);
	return likelihoodComputation::getTreeLikelihoodAllPosAlphTheSame(et, sc, *_spPtr, _weights);
}

void commonAlphaDistanceSeqs2Tree::acceptSideInfo()
{
	_alpha = _newAlpha;
}

void commonAlphaDistanceSeqs2Tree::utilizeSideInfo()
{
	// set new alpha value in the sp that is used in _distM
	(static_cast<gammaDistribution*>(_spPtr->distr()))->setAlpha(_alpha);
	LOG(10,<<"# utilizing alpha"<<endl<<_alpha<<endl<<endl);

}

void commonAlphaDistanceSeqs2Tree::printSideInfo(ostream& out) const
{
	out<<"Alpha: "<<_alpha<<endl;
}

// non virtual
void commonAlphaDistanceSeqs2Tree::setSideInfo(const MDOUBLE alpha)
{
	_alpha=alpha;
}

MDOUBLE commonAlphaDistanceSeqs2Tree::getSideInfo() const
{
	return _alpha;
}

/******************************
 * rate4siteDistanceSeqs2Tree *
 ******************************/
tree rate4siteDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const Vdouble &initRates, const Vdouble *weights, const tree* constraintTreePtr) {
	_rates = initRates;
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternal(sc, true);
}

tree rate4siteDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternal(sc, false);
}

tree rate4siteDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, initTree);
}

tree rate4siteDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, MDOUBLE initAlpha, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, false, initTree, initAlpha);
}

// NOTE! This version is a NON-ITERATIVE version that uses the side info supplied by the user
tree rate4siteDistanceSeqs2Tree::seqs2Tree(const sequenceContainer &sc, const Vdouble &rates, const Vdouble *weights, const tree* constraintTreePtr) {
	_weights = weights;
	_rates = rates;
	_constraintTreePtr=constraintTreePtr;

	seqs2TreeOneIterationInternal(sc, true);
	return _newTree;
}

tree rate4siteDistanceSeqs2Tree::seqs2TreeBootstrap(const sequenceContainer &sc, const Vdouble &rates, const Vdouble *weights, const tree* constraintTreePtr) {
	_weights = weights;
	_rates = rates;
	return static_cast<iterativeDistanceSeqs2Tree *>(this)->seqs2TreeBootstrap(sc, weights, constraintTreePtr);
}

// NOTE! This version calls ITERATIVE seqs2Tree because side info is not given by the user, so we have to generate and optimize it
tree rate4siteDistanceSeqs2Tree::seqs2Tree(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	return seqs2TreeIterative(sc,weights,constraintTreePtr);
}

MDOUBLE rate4siteDistanceSeqs2Tree::optimizeSideInfo(const sequenceContainer &sc, tree &et)
{
	bblEM optimizer(et, sc, *_spPtr, _weights, _maxIterationsBBL, _epsilonLikelihoodImprovement4BBL);

	// Note: this verstion of ML rates computation can only use a uniDistribution stochasticProcess
	Vdouble likelihoods;
	MDOUBLE treeLogLikelihood = computeML_siteSpecificRate(_newRates, likelihoods, sc, *_spPtr, et,20,_epsilonLikelihoodImprovement);
	//computeEB_EXP_siteSpecificRate
	return(treeLogLikelihood);
}

MDOUBLE rate4siteDistanceSeqs2Tree::calcSideInfoGivenTreeAndAlpha(const sequenceContainer &sc, const tree &et, MDOUBLE alpha) 
{
	_newAlpha = alpha;
	Vdouble likelihoods;
	MDOUBLE treeLogLikelihood = computeML_siteSpecificRate(_newRates, likelihoods, sc, *_spPtr, et,20,_epsilonLikelihoodImprovement);
	//computeEB_EXP_siteSpecificRate
	return(treeLogLikelihood);
}

void rate4siteDistanceSeqs2Tree::acceptSideInfo()
{
	_alpha = _newAlpha;
	_rates = _newRates;
}

void rate4siteDistanceSeqs2Tree::utilizeSideInfo()
{
	(static_cast<givenRatesMLDistance*>(_distM))->setRates(_rates);
	LOG(10,<<"# utilizing rates"<<endl<<_rates<<endl<<endl);

	// set new alpha value in the sp that is used in _distM 
	//  (static_cast<gammaDistribution*>(_spPtr->distr()))->setAlpha(_alpha);
}

void rate4siteDistanceSeqs2Tree::printSideInfo(ostream& out) const
{
	if (_rates.size())
		out<<"ML rates: "<<_rates<<endl;
}

// non virtual
void rate4siteDistanceSeqs2Tree::setSideInfo(const Vdouble &rates)
{
	_rates = rates;
}

const Vdouble& rate4siteDistanceSeqs2Tree::getSideInfo() const
{
	return _rates;
}

/******************************
 * posteriorDistanceSeqs2Tree *
 ********************************/
tree posteriorDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, MDOUBLE initAlpha, const VVdoubleRep &initPosterior, const Vdouble *weights, const tree* constraintTreePtr) {
	_alpha = initAlpha;
	_posterior = initPosterior;
	_weights = weights;
	_constraintTreePtr=constraintTreePtr;
	return seqs2TreeIterativeInternal(sc, true);
}

tree posteriorDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternal(sc, false);
}

tree posteriorDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, initTree);
}

tree posteriorDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, MDOUBLE initAlpha, const Vdouble *weights, const tree* constraintTreePtr) {
	_constraintTreePtr=constraintTreePtr;
	_weights = weights;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, false, initTree, initAlpha);
}

tree posteriorDistanceSeqs2Tree::seqs2TreeIterative(const sequenceContainer &sc, const tree &initTree, MDOUBLE initAlpha, const VVdoubleRep &initPosterior, const Vdouble *weights, const tree* constraintTreePtr) {
	_alpha = initAlpha;
	_posterior = initPosterior;
	_weights = weights;
	_constraintTreePtr=constraintTreePtr;
	return seqs2TreeIterativeInternalInitTreeGiven(sc, true, initTree, initAlpha);
}

// NOTE! This version is a NON-ITERATIVE version that uses the side info supplied by the user
tree posteriorDistanceSeqs2Tree::seqs2Tree(const sequenceContainer &sc, const VVdoubleRep &posterior, const Vdouble *weights, const tree* constraintTreePtr) {
	_weights = weights;
	_posterior = posterior;
	_constraintTreePtr=constraintTreePtr;
	seqs2TreeOneIterationInternal(sc, true);
	return _newTree;
}

tree posteriorDistanceSeqs2Tree::seqs2TreeBootstrap(const sequenceContainer &sc, const VVdoubleRep &posterior, const Vdouble *weights, const tree* constraintTreePtr) {
	_weights = weights;
	_posterior = posterior;
	return static_cast<iterativeDistanceSeqs2Tree *>(this)->seqs2TreeBootstrap(sc, weights, constraintTreePtr);
}

// NOTE! This version calls ITERATIVE seqs2Tree because side info is not given by the user, so we have to generate and optimize it
tree posteriorDistanceSeqs2Tree::seqs2Tree(const sequenceContainer &sc, const Vdouble *weights, const tree* constraintTreePtr) {
  return seqs2TreeIterative(sc, weights, constraintTreePtr);
}

MDOUBLE posteriorDistanceSeqs2Tree::optimizeSideInfo(const sequenceContainer &sc, tree &et)
{
	if (dynamic_cast<tamura92*>(_spPtr->getPijAccelerator()->getReplacementModel())) {
		// Optimizing params of the tamura92 model
		bestTamura92ParamAlphaAndBBL optimizer(et, sc, *_spPtr, _weights, 5, _epsilonLikelihoodImprovement/*0.05*/,
											   _epsilonLikelihoodImprovement4alphaOptimiz/*0.01*/, 
											   _epsilonLikelihoodImprovement4alphaOptimiz/*0.01*/, 
											   _epsilonLikelihoodImprovement4alphaOptimiz/*0.01*/, 
											   _epsilonLikelihoodImprovement4BBL/*0.01*/,
											   5.0, _maxIterationsBBL, _alpha, 5.0 );
		_newAlpha=optimizer.getBestAlpha();
		return(optimizer.getBestL());

	} else if (dynamic_cast<gtrModel*>(_spPtr->getPijAccelerator()->getReplacementModel())) {
		// Optimizing params of the gtr model
		bestGtrModel optimizer(et, sc, *_spPtr, _weights, 5,
							   _epsilonLikelihoodImprovement,
							   _epsilonLikelihoodImprovement4alphaOptimiz,
							   true, true);
		_newAlpha=optimizer.getBestAlpha();
		return(optimizer.getBestL());

	} else {
		bestAlphaAndBBL optimizer(et, sc, *_spPtr, _weights, _alpha, 5.0,
								  _epsilonLikelihoodImprovement4BBL/*0.01*/, _epsilonLikelihoodImprovement4alphaOptimiz,
								  _maxIterationsBBL);
		_newAlpha=optimizer.getBestAlpha();	// cached only to make alpha optimization faster
	}

	// Compute posterior probabilities of rates per site
	return likelihoodComputation::getPosteriorOfRates(et, sc, *_spPtr, _newPosterior);
}

MDOUBLE posteriorDistanceSeqs2Tree::calcSideInfoGivenTreeAndAlpha(const sequenceContainer &sc, const tree &et, MDOUBLE alpha) 
{
	_newAlpha = alpha;
	(static_cast<gammaDistribution*>(_spPtr->distr()))->setAlpha(alpha);
	// Compute posterior probabilities of rates per site
	return likelihoodComputation::getPosteriorOfRates(et, sc, *_spPtr, _newPosterior);
}

void posteriorDistanceSeqs2Tree::acceptSideInfo()
{
	_alpha = _newAlpha;
	_posterior = _newPosterior;
}

void posteriorDistanceSeqs2Tree::utilizeSideInfo()
{
	(static_cast<posteriorDistance*>(_distM))->setPosterior(_posterior);
	LOG(10,<<"# utilizing posterior"<<endl<<_posterior<<endl<<endl);
	// set new alpha value in the sp that is used in _distM 
	//  (static_cast<gammaDistribution*>(_spPtr->distr()))->setAlpha(_alpha);
}

void posteriorDistanceSeqs2Tree::printSideInfo(ostream& out) const
{
	if (_posterior.size())
		out<<_posterior<<endl;
}

// non virtual
void posteriorDistanceSeqs2Tree::setSideInfo(const VVdoubleRep &posterior)
{
	_posterior = posterior;
}

const VVdoubleRep& posteriorDistanceSeqs2Tree::getSideInfo() const
{
	return _posterior;
}