// $Id: njConstrain.cpp 962 2006-11-07 15:13:34Z privmane $ #include "definitions.h" #include #include "njConstrain.h" #include "logFile.h" njConstraint::njConstraint(const tree& starttree, const tree& constraintTree):_cTree(constraintTree), _interTreeMap(){ vector currentNodes; starttree.getAllLeaves(currentNodes,starttree.getRoot()); vector constraintNodes; _cTree.getAllLeaves(constraintNodes,_cTree.getRoot()); assert(currentNodes.size()==constraintNodes.size()); map name2Node; for (vector::iterator vec_iter=constraintNodes.begin();vec_iter!=constraintNodes.end();++vec_iter){ // name2Node[test];//=*vec_iter; name2Node[(*vec_iter)->name()]=*vec_iter; } for (vector::iterator vec_iter2=currentNodes.begin();vec_iter2!=currentNodes.end();++vec_iter2){ assert(name2Node.find((*vec_iter2)->name()) != name2Node.end()); // cant find the taxa in the constratin tree! _interTreeMap[*vec_iter2]=name2Node[(*vec_iter2)->name()]; } } bool njConstraint::isCompatible(const tree::nodeP& n1, const tree::nodeP& n2, const bool verbose) const { bool compatible; assert( _interTreeMap.find(n1) != _interTreeMap.end()); // cant find the taxa in the map! assert( _interTreeMap.find(n2) != _interTreeMap.end()); // cant find the taxa in the map! tree::nodeP s1=_interTreeMap.find(n1)->second; tree::nodeP s2=_interTreeMap.find(n2)->second; if (s1==_cTree.getRoot()) { // we are asking undirected questions from a directed tree compatible = (s2 != _cTree.getRoot()) && (s2->father() != _cTree.getRoot()) && (s2->father()->father() == _cTree.getRoot()); if (verbose) LOG(11,<<"isCompatible - s1 is root"<father() != _cTree.getRoot()) && (s1->father()->father() == _cTree.getRoot()); if (verbose) LOG(11,<<"isCompatible - s2 is root"<father()==s2->father()); } if (verbose) LOG(11,<<"isCompatible:" <name()<<" + "<name()<<"-->" <father()==s2->father()); // we can only do this if both nodes have same father LOG(10,<name()<<" and "<name()<father(); if (fatherNode->getNumberOfSons()==2) { // fatherNode->sons.clear(); return (fatherNode); // no splitting needed } if (s1->father()==t.getRoot() && t.getRoot()->getNumberOfSons()==3) { // no split needed, but the root needs to change LOG(10,<<"************************* spacial case of constratin join"<name()<<" and "<name()<father()->name()<<" and father "<father()->name()<father(); for (int i=0; i<3; ++i) if (t.getRoot()->getSon(i)!= s1 && t.getRoot()->getSon(i)!= s2){ t.rootAt(t.getRoot()->getSon(i)); LOGDO(10,t.output(myLog::LogFile(),tree::ANCESTORID)); LOG(10,<setSon(s1); newNode->setSon(s2); newNode->claimSons(); int k = fatherNode->getNumberOfSons(); fatherNode->removeSon(s1); fatherNode->removeSon(s2); assert (k=fatherNode->getNumberOfSons()+2); // both s1 and s2 should have been skiped // fatherNode->sons.resize(k); t.updateNumberofNodesANDleaves(); t.create_names_to_internal_nodes(); return(newNode); } void njConstraint::join(const tree::nodeP& n1, const tree::nodeP& n2, const tree::nodeP& newFather) { assert(_interTreeMap.find(n1) != _interTreeMap.end()); // cant find the taxa in the map! assert(_interTreeMap.find(n2) != _interTreeMap.end()); // cant find the taxa in the map! assert(_interTreeMap.find(newFather) == _interTreeMap.end()); // should not find the new father in the map! assert(isCompatible(n1,n2)); // tree::nodeP origFather=_interTreeMap.find(n1)->father(); // do tree things LOG(10,<name()<<" AND "<name()<