vector<tree::nodeP> findSonsThatHaveToBeSplit(const tree& et, const split& mySplit, const map<string, int> & nameIdMap){ // we assume that split is compatible with the tree and that the split is a subset of the children of the root. // i.e., the node that has to be splitted is the root. vector<tree::nodeP> res; for (int i=0; i < et.getRoot()->getNumberOfSons(); ++i) { if (childIsInTheSplit(et.getRoot()->getSon(i),mySplit,nameIdMap)) { res.push_back(et.getRoot()->getSon(i)); } } return res; }
void fillAllNodesNames(Vstring& Vnames,const tree& tr){ vector<tree::nodeP> vAllNodes; tr.getAllNodes(vAllNodes,tr.getRoot()); Vnames.resize(vAllNodes.size()); for (int i = 0; i<vAllNodes.size();++i) Vnames[vAllNodes[i]->id()] = vAllNodes[i]->name(); }
MDOUBLE likelihoodComputationGL::getProbOfPosUpIsFilledSelectionGam(const int pos,const tree& tr, const sequenceContainer& sc, const vector<vector<stochasticProcess*> >& spVVec,// only needed for sp.freq(let) const suffStatGlobalGamPos& cup, const distribution * distGain, const distribution * distLoss) { doubleRep res =0; int numOfSPs = distGain->categories()*distLoss->categories(); for (int categor = 0; categor < numOfSPs; ++categor) { doubleRep veryTmp =0.0; int gainCategor = fromIndex2gainIndex(categor,distGain->categories(),distLoss->categories()); int lossCategor = fromIndex2lossIndex(categor,distGain->categories(),distLoss->categories()); for (int let =0; let < sc.alphabetSize(); ++let) { veryTmp+=cup.get(categor,tr.getRoot()->id(),let) * spVVec[gainCategor][lossCategor]->freq(let); } res += veryTmp*(distGain->ratesProb(gainCategor)*distLoss->ratesProb(lossCategor)); } if ((res<-EPSILON)){ string err = "Error in likelihoodComputationGL::getProbOfPosUpIsFilledSelectionGam, non probability value (<0) Res="; err+=double2string(convert(res)); errorMsg::reportError(err); }; return convert(res); }
MDOUBLE likelihoodComputationGL::getTreeLikelihoodFromUp2(const tree& tr, const sequenceContainer& sc, const vector<vector<stochasticProcess*> >& spVVec,// only needed for sp.freq(let) const suffStatGlobalGam& cup, //computing the likelihood from up: const distribution * distGain, const distribution * distLoss, unObservableData *unObservableData_p, Vdouble* posLike, const Vdouble * weights) { if(posLike) posLike->clear(); MDOUBLE like = 0; int numOfSPs = distGain->categories()*distLoss->categories(); for (int pos = 0; pos < sc.seqLen(); ++pos) { doubleRep tmp=0; for (int categor = 0; categor < numOfSPs; ++categor) { doubleRep veryTmp =0; int gainCategor = fromIndex2gainIndex(categor,distGain->categories(),distLoss->categories()); int lossCategor = fromIndex2lossIndex(categor,distGain->categories(),distLoss->categories()); for (int let =0; let < sc.alphabetSize(); ++let) { veryTmp+=cup.get(pos,categor,tr.getRoot()->id(),let) * spVVec[gainCategor][lossCategor]->freq(let); } tmp += veryTmp*(distGain->ratesProb(gainCategor)*distLoss->ratesProb(lossCategor)); } if(unObservableData_p) tmp = tmp/(1- exp(unObservableData_p->getlogLforMissingData())); if(posLike) posLike->push_back(log(tmp)); like += log(tmp) * (weights?(*weights)[pos]:1); } return like; }
bool sameTreeTolopogy(tree t1, tree t2){ if (t1.getNodesNum() != t2.getNodesNum()) { errorMsg::reportError("error in function same tree topology (1)"); } tree::nodeP x = t2.getRoot(); while (x->getNumberOfSons() > 0) x= x->getSon(0); t1.rootAt(t1.findNodeByName(x->name())->father()); // now they have the same root t2.rootAt(t2.findNodeByName(x->name())->father()); // now they have the same root map<int,string> names1; treeIterDownTopConst tit1(t1); for (tree::nodeP nodeM = tit1.first(); nodeM != tit1.end(); nodeM = tit1.next()) { vector<string> nameOfChild; for (int i=0; i < nodeM->getNumberOfSons();++i) { nameOfChild.push_back(names1[nodeM->getSon(i)->id()]); } if (nodeM->getNumberOfSons()==0) nameOfChild.push_back(nodeM->name()); sort(nameOfChild.begin(),nameOfChild.end()); string res = "("; for (int k=0; k < nameOfChild.size(); ++k) { res += nameOfChild[k]; } res += ")"; names1[nodeM->id()] = res; } map<int,string> names2; treeIterDownTopConst tit2(t2); for (tree::nodeP nodeM2 = tit2.first(); nodeM2 != tit2.end(); nodeM2 = tit2.next()) { vector<string> nameOfChild; for (int i=0; i < nodeM2->getNumberOfSons();++i) { nameOfChild.push_back(names2[nodeM2->getSon(i)->id()]); } if (nodeM2->getNumberOfSons()==0) nameOfChild.push_back(nodeM2->name()); sort(nameOfChild.begin(),nameOfChild.end()); string res = "("; for (int k=0; k < nameOfChild.size(); ++k) { res += nameOfChild[k]; } res += ")"; names2[nodeM2->id()] = res; } return names1[t1.getRoot()->id()] == names2[t2.getRoot()->id()]; }
tree::nodeP findNodeToSplit(const tree& et, const split& mySplit, const map<string, int> & nameIdMap) { tree::nodeP res; bool foundTheNodeAlready = false; findNodeToSplitRecursive(et.getRoot(),mySplit,res,foundTheNodeAlready,nameIdMap); return res; }
int findSumHelper(tree<int> nums, tree<char> opers) { if (nums.empty()) { return 0; } else { switch (opers.getRoot()->inf) { case '+': return (nums.getRoot()->inf + countEdges(nums)) + findSumHelper(nums.rightTree(), opers.rightTree()) + findSumHelper(nums.leftTree(), opers.leftTree()); case '-': return (nums.getRoot()->inf - countEdges(nums)) + findSumHelper(nums.rightTree(), opers.rightTree()) + findSumHelper(nums.leftTree(), opers.leftTree()); case '*': return (nums.getRoot()->inf * countEdges(nums)) + findSumHelper(nums.rightTree(), opers.rightTree()) + findSumHelper(nums.leftTree(), opers.leftTree()); default: return 0; } } }
vector<string> getSequencesNames(const tree& t){ vector<tree::nodeP> vleaves; t.getAllLeaves(vleaves,t.getRoot()); vector<string> res; vector<tree::nodeP>::const_iterator i = vleaves.begin(); for ( ; i<vleaves.end(); ++i) { res.push_back((*i)->name()); } return res; }
MDOUBLE likelihoodComputation::getLOG_LofPos(const int pos, const tree& et, const sequenceContainer& sc, const stochasticProcess& sp, const MDOUBLE gRate){ // when there is a global rate for this position // using the pij of stochastic process rather than pre computed pij's... vector<MDOUBLE> factors; computeUpAlg cup; suffStatGlobalHomPos ssc; cup.fillComputeUpSpecificGlobalRateFactors(et,sc,pos,sp,ssc,gRate,factors); doubleRep tmp = 0.0; for (int let = 0; let < sp.alphabetSize(); ++let) { doubleRep tmpLcat= ssc.get(et.getRoot()->id(),let)* sp.freq(let);; assert(tmpLcat>=0); tmp+=tmpLcat; } return log(tmp)-factors[et.getRoot()->id()]*log(10.0); }
tree::tree(const tree &otherTree) { _root = NULL; if (otherTree._root == NULL) return; // if tree to copy is empty. createRootNode(); _root->setName(otherTree._root->name()); _root->setID(otherTree._root->id()); _root->setComment(otherTree._root->getComment()); for (int i=0; i <otherTree._root->getNumberOfSons(); ++i) { recursiveBuildTree( _root, otherTree.getRoot()->getSon(i)); } }
// pre-request: // the intermediateNode is the root. // and it has two sons. // resultT1PTR & resultT2PTR are empty trees (root=NULL); void cutTreeToTwoSpecial(const tree& source, tree::nodeP intermediateNode, tree &resultT1PTR, tree &resultT2PTR) { // make sure that you got two empty trees: if (resultT1PTR.getRoot() != NULL) errorMsg::reportError("got a non empty tree1 in function cutTreeToTwoSpecial"); else if (resultT2PTR.getRoot() != NULL) errorMsg::reportError("got a non empty tree2 in function cutTreeToTwoSpecial"); // make sure the the intermediateNode is really an intermediate Node; if ((intermediateNode->getNumberOfSons() !=2 ) || (source.getRoot() != intermediateNode)) { errorMsg::reportError("intermediateNode in function cutTreeToTwoSpecial, is not a real intermediate node "); } resultT1PTR.createRootNode(); resultT1PTR.getRoot()->setName(intermediateNode->name()); resultT2PTR.createRootNode(); resultT2PTR.getRoot()->setName(intermediateNode->name()); resultT1PTR.recursiveBuildTree(resultT1PTR.getRoot(),intermediateNode->getSon(0)); resultT2PTR.recursiveBuildTree(resultT2PTR.getRoot(),intermediateNode->getSon(1)); }
void printDataOnTreeAsBPValues(ostream &out, Vstring &data, tree &tr) { printDataOnTreeAsBPValues(out,data, tr.getRoot()); out<<";"; }
void printTreeWithValuesAsBP(ostream &out, const tree &tr, Vstring values, VVVdouble *probs, int from, int to) { printTreeWithValuesAsBP(out,tr.getRoot(), values,probs,from,to); out<<"["<<values[tr.getRoot()->id()]<<"];"; }
void applySplitToRoot(tree& et, const split& mySplit, const map<string, int> & nameIdMap) { vector<tree::nodeP> sonsThatHaveToBeSplit = findSonsThatHaveToBeSplit(et,mySplit,nameIdMap); splitSonsFromNode(et, et.getRoot(), sonsThatHaveToBeSplit); }