// $Id: bestTamura92param.cpp 962 2006-11-07 15:13:34Z privmane $ #include "bestTamura92param.h" #include using namespace std; #include "bblEM.h" #include "bblEMProportionalEB.h" #include "bblLSProportionalEB.h" #include "numRec.h" #include "logFile.h" #include "bestAlpha.h" bestTamura92ParamFixedTree::bestTamura92ParamFixedTree(const tree& et, // find best TrTv and theta const sequenceContainer& sc, stochasticProcess& sp, const Vdouble * weights, const int maxTotalIterations, const MDOUBLE epsilonLikelihoodImprovment, const MDOUBLE epsilonLoglikelihoodForTrTvOptimization, const MDOUBLE epsilonLoglikelihoodForThetaOptimization, const MDOUBLE upperBoundOnTrTv) { LOG(5,<<"Starting bestTamura92ParamFixedTree: find Best TrTv and theta"< oldL+epsilonLikelihoodImprovment) { prevTrTv = _bestTrTv; prevTheta = _bestTheta; oldL = newL; _bestL = newL; } else { if (newL>oldL) { _bestL = newL; } else { _bestL = oldL; _bestTrTv = prevTrTv; _bestTheta = prevTheta; } break; } } } bestTamura92ParamAndBBL::bestTamura92ParamAndBBL(tree& et, //find best TrTv, theta and best BBL const sequenceContainer& sc, stochasticProcess& sp, const Vdouble * weights, const int maxTotalIterations, const MDOUBLE epsilonLikelihoodImprovment, const MDOUBLE epsilonLoglikelihoodForTrTvOptimization, const MDOUBLE epsilonLoglikelihoodForThetaOptimization, const MDOUBLE epsilonLoglikelihoodForBBL, const MDOUBLE upperBoundOnTrTv, const int maxBBLIterations){ LOG(5,<<"Starting bestTamura92ParamAndBBL: find best TrTv, theta and BBL"<(sp.getPijAccelerator()->getReplacementModel()))->changeTrTv(_bestTrTv); // optimize Theta newL = -brent(0.0, prevTheta, 1.0, C_evalTheta(et,sc,sp,weights), epsilonLoglikelihoodForThetaOptimization, &_bestTheta); (static_cast(sp.getPijAccelerator()->getReplacementModel()))->changeTheta(_bestTheta); // optimize branch lengths bblEM bblEM1(et,sc,sp,NULL,maxBBLIterations,epsilonLoglikelihoodForBBL);//maxIterations=1000 newL =bblEM1.getTreeLikelihood(); // check for improvement in the likelihood if (newL > oldL+epsilonLikelihoodImprovment) { prevTrTv = _bestTrTv; prevTheta = _bestTheta; oldL = newL; _bestL = newL; prevTree = et; } else { if (newL>oldL) { _bestL = newL; } else { _bestL = oldL; _bestTrTv = prevTrTv; _bestTheta = prevTheta; et = prevTree; } break; } } } bestTamura92ParamAlphaAndBBL::bestTamura92ParamAlphaAndBBL( //find best TrTv, theta, Alpha and best branch lengths tree& et, const sequenceContainer& sc, stochasticProcess& sp, const Vdouble * weights, const int maxTotalIterations, const MDOUBLE epsilonLikelihoodImprovment, const MDOUBLE epsilonLoglikelihoodForTrTvOptimization, const MDOUBLE epsilonLoglikelihoodForThetaOptimization, const MDOUBLE epsilonLoglikelihoodForAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForBBL, const MDOUBLE upperBoundOnTrTv, const int maxBBLIterations, const MDOUBLE initAlpha, const MDOUBLE upperBoundOnAlpha) { MDOUBLE oldL = VERYSMALL; MDOUBLE newL = VERYSMALL; // first guess for the parameters MDOUBLE prevTrTv = static_cast(sp.getPijAccelerator()->getReplacementModel())->getTrTv(); MDOUBLE prevTheta = static_cast(sp.getPijAccelerator()->getReplacementModel())->getTheta(); MDOUBLE prevAlpha = initAlpha; tree prevTree; for (int i=0; i < maxTotalIterations; ++i) { // optimize TrTv newL = -brent(0.0, prevTrTv, upperBoundOnTrTv, C_evalTrTvParam(et,sc,sp,weights), epsilonLoglikelihoodForTrTvOptimization, &_bestTrTv); (static_cast(sp.getPijAccelerator()->getReplacementModel()))->changeTrTv(_bestTrTv); // optimize Theta newL = -brent(0.0, prevTheta, 1.0, C_evalTheta(et,sc,sp,weights), epsilonLoglikelihoodForThetaOptimization, &_bestTheta); (static_cast(sp.getPijAccelerator()->getReplacementModel()))->changeTheta(_bestTheta); // optimize Alpha newL = -brent(0.0, prevAlpha, upperBoundOnAlpha, C_evalAlpha(et,sc,sp,weights), epsilonLoglikelihoodForAlphaOptimization, &_bestAlpha); (static_cast(sp.distr()))->setAlpha(_bestAlpha); LOG(5,<<"# bestTamura92ParamAlphaAndBBL::bestTamura92ParamAlphaAndBBL iteration " << i << ": after param optimization:" < oldL+epsilonLikelihoodImprovment) { oldL = newL; _bestL = newL; prevTrTv = _bestTrTv; prevTheta = _bestTheta; prevAlpha = _bestAlpha; prevTree = et; } else { if (newL>oldL) { _bestL = newL; } else { _bestL = oldL; _bestTrTv = prevTrTv; _bestTheta = prevTheta; et = prevTree; } break; } } } bestTamura92ParamAlphaAndBBLProportional::bestTamura92ParamAlphaAndBBLProportional( //find best TrTv, theta, loca Alpha for each gene, global Alpha and best branch lengths tree& et, vector& sc, multipleStochasticProcess* msp, gammaDistribution* pProportionDist, Vdouble initLocalAlphas, Vdouble initLocalKappas, Vdouble initLocalThetas, const MDOUBLE upperBoundOnLocalAlpha, const MDOUBLE initGlobalAlpha, const MDOUBLE upperBoundOnGlobalAlpha, const MDOUBLE upperBoundOnTrTv, const int maxTotalIterations, const int maxBBLIterations, const bool optimizeSelectedBranches, const bool optimizeTree, const string branchLengthOptimizationMethod, const bool optimizeLocalParams, const bool optimizeGlobalAlpha, const Vdouble * weights, const MDOUBLE epsilonLikelihoodImprovment, const MDOUBLE epsilonLoglikelihoodForLocalTrTvOptimization, const MDOUBLE epsilonLoglikelihoodForLocalThetaOptimization, const MDOUBLE epsilonLoglikelihoodForLocalAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForGlobalAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForBBL) { LOG(5,<<"Starting bestTamura92ParamAlphaAndBBLProportional"<getSPVecSize()); //doubleRep oldL(VERYSMALL);//DR //doubleRep newL;//DR MDOUBLE oldL = VERYSMALL; MDOUBLE newL; //doubleRep epsilonLoglikelihoodForGlobalAlphaOptimizationDR(epsilonLoglikelihoodForGlobalAlphaOptimization);//DR _bestLvec.resize(msp->getSPVecSize(),0.0); _bestLocalAlphaVec = initLocalAlphas; _bestGlobalAlpha = initGlobalAlpha; int spIndex; _bestTrTvVec = currentTrTvVec; _bestThetaVec = currentThetaVec; pProportionDist->setAlpha(_bestGlobalAlpha); for(spIndex = 0;spIndex < msp->getSPVecSize();++spIndex){ (static_cast(msp->getSp(spIndex)->getPijAccelerator()->getReplacementModel()))->changeTheta(_bestThetaVec[spIndex]);//safety (static_cast(msp->getSp(spIndex)->getPijAccelerator()->getReplacementModel()))->changeTrTv(_bestTrTvVec[spIndex]); (static_cast(msp->getSp(spIndex)->distr()))->setAlpha(_bestLocalAlphaVec[spIndex]); } //first compute the likelihood; _bestLvec = likelihoodComputation::getTreeLikelihoodProportionalAllPosAlphTheSame(et,sc,msp,pProportionDist,weights); MDOUBLE ax_local = 0.0; MDOUBLE c_TrTv_x = upperBoundOnTrTv; MDOUBLE c_theta_x = 1.0; MDOUBLE c_localAlpha_x = upperBoundOnLocalAlpha; for (int i=0; i < maxTotalIterations; ++i) { if(optimizeLocalParams){ for(spIndex = 0;spIndex < msp->getSPVecSize();++spIndex){ //optimize Theta MDOUBLE theta_x(_bestThetaVec[spIndex]); newLvec[spIndex] = -brent(ax_local,theta_x,c_theta_x, C_evalLocalTheta(et,sc[spIndex],*msp->getSp(spIndex),pProportionDist,weights), epsilonLoglikelihoodForLocalThetaOptimization, ¤tThetaVec[spIndex]); if (newLvec[spIndex] >= _bestLvec[spIndex]) { _bestLvec[spIndex] = newLvec[spIndex]; _bestThetaVec[spIndex] = currentThetaVec[spIndex]; } else {//likelihood went down! LOG(2,<<"likelihood went down in optimizing TrTv param"<(msp->getSp(spIndex)->getPijAccelerator()->getReplacementModel()))->changeTheta(_bestThetaVec[spIndex]);//safety //optimize TrTv MDOUBLE TrTv_x(_bestTrTvVec[spIndex]); newLvec[spIndex] = -brent(ax_local,TrTv_x,c_TrTv_x, C_evalLocalTrTvParam(et,sc[spIndex],*msp->getSp(spIndex),pProportionDist,weights), epsilonLoglikelihoodForLocalTrTvOptimization, ¤tTrTvVec[spIndex]); if (newLvec[spIndex] >= _bestLvec[spIndex]) { _bestLvec[spIndex] = newLvec[spIndex]; _bestTrTvVec[spIndex] = currentTrTvVec[spIndex]; } else {//likelihood went down! LOG(2,<<"likelihood went down in optimizing TrTv param"<(msp->getSp(spIndex)->getPijAccelerator()->getReplacementModel()))->changeTrTv(_bestTrTvVec[spIndex]);//safety //optimize local alpha MDOUBLE localAlpha_x(_bestLocalAlphaVec[spIndex]); newLvec[spIndex] = -brent(ax_local,localAlpha_x, c_localAlpha_x, C_evalLocalAlpha(et,sc[spIndex],*msp->getSp(spIndex),pProportionDist,weights), epsilonLoglikelihoodForLocalAlphaOptimization, ¤tLocalAlphaVec[spIndex]); if (newLvec[spIndex] >= _bestLvec[spIndex]) { _bestLvec[spIndex] = newLvec[spIndex]; _bestLocalAlphaVec[spIndex] = currentLocalAlphaVec[spIndex]; } else {//likelihood went down! LOG(2,<<"likelihood went down in optimizing local alpha"<(msp->getSp(spIndex)->distr()))->setAlpha(_bestLocalAlphaVec[spIndex]); //safety } LOGnOUT(2,<<"Done with Tamura92 local params optimization. LL: "<= sumVdouble(_bestLvec)) { _bestGlobalAlpha = currentGlobalAlpha; } else {//likelihood went down! LOG(2,<<"likelihood went down in optimizing global alpha"<setAlpha(_bestGlobalAlpha); //safety //whether or not likelihood has improved we need to update _bestLvec _bestLvec = likelihoodComputation::getTreeLikelihoodProportionalAllPosAlphTheSame(et,sc,msp,pProportionDist,weights); LOGnOUT(2,<<"Done with global alpha optimization"< oldL+epsilonLikelihoodImprovment) { //all params have already been updated oldL = sumVdouble(_bestLvec); } else { break; } LOGnOUT(4,<<"Done with optimization iteration "<