36 b = b || isMultifurcating(*node.
getSon(i));
53 nbLeaves += getNumberOfLeaves(*node[i]);
65 names.push_back(node.
getName());
69 vector<string> subNames = getLeavesNames(*node.
getSon(i));
70 for (
size_t j = 0; j < subNames.size(); j++)
72 names.push_back(subNames[j]);
82 ids.push_back(node.
getId());
86 getLeavesId(*node.
getSon(i), ids);
93 const Node* n = &node;
97 ids.push_back(n->
getId());
108 id =
new int(node.
getId());
114 searchLeaf(*node.
getSon(i), name,
id);
126 unsigned int c = getDepth(*node[i]) + 1;
140 unsigned int c = getDepths(*node[i], depths) + 1;
155 const Node* son = node[i];
157 double c = getHeight(*son) + dist;
171 const Node* son = node[i];
173 double c = getHeights(*son, heights) + dist;
191 bool hasColon =
false;
192 for (colonIndex = elt.size(); colonIndex > 0 && elt[colonIndex] !=
')'; colonIndex--)
194 if (elt[colonIndex] ==
':')
206 elt2 = elt.substr(0, colonIndex);
215 string::size_type lastP = elt2.rfind(
')');
216 string::size_type firstP = elt2.find(
'(');
217 if (firstP == string::npos)
227 throw IOException(
"TreeTemplateTools::getElement(). Invalid format: bad closing parenthesis in " + elt2);
250 Element elt = getElement(description);
280 vector<string> elements;
293 ostringstream realName;
313 for (
size_t i = 0; i < elements.size(); i++)
316 Node* son = parenthesisToNode(elements[i], nodeCounter, bootstrap, propertyName, withId, verbose);
330 string::size_type semi = description.rfind(
';');
331 if (semi == string::npos)
332 throw Exception(
"TreeTemplateTools::parenthesisToTree(). Bad format: no semi-colon found.");
333 string content = description.substr(0, semi);
334 unsigned int nodeCounter = 0;
335 Node* node = parenthesisToNode(content, nodeCounter, bootstrap, propertyName, withId, verbose);
336 auto tree = make_unique<TreeTemplate<Node>>();
337 tree->setRootNode(node);
340 tree->resetNodesId();
344 (*ApplicationTools::message) <<
" nodes loaded.";
362 s << nodeToParenthesis(*node[0], writeId);
365 s <<
"," << nodeToParenthesis(*node[i], writeId);
397 s << nodeToParenthesis(*node[0], bootstrap, propertyName);
400 s <<
"," << nodeToParenthesis(*node[i], bootstrap, propertyName);
417 throw Exception(
"TreeTemplateTools::nodeToParenthesis. Property should be a BppString.");
438 s <<
"," << nodeToParenthesis(*node->
getSon(i), writeId);
443 s << nodeToParenthesis(*node->
getSon(0), writeId);
446 s <<
"," << nodeToParenthesis(*node->
getSon(i), writeId);
468 s <<
"," << nodeToParenthesis(*node->
getSon(i), bootstrap, propertyName);
473 s << nodeToParenthesis(*node->
getSon(0), bootstrap, propertyName);
476 s <<
"," << nodeToParenthesis(*node->
getSon(i), bootstrap, propertyName);
493 throw Exception(
"TreeTemplateTools::nodeToParenthesis. Property should be a BppString.");
509 for (
size_t j = 0; j < sonBrLen.size(); j++)
511 brLen.push_back(sonBrLen[j]);
522 throw NodePException(
"TreeTools::getTotalLength(). No branch length.", &node);
526 length += getTotalLength(*node.
getSon(i),
true);
538 setBranchLengths(*node.
getSon(i), brLen);
549 deleteBranchLengths(*node.
getSon(i));
561 setVoidBranchLengths(*node.
getSon(i), brLen);
575 scaleTree(*node.
getSon(i), factor);
584 auto phroot = treetemp.
getRoot();
587 auto tree = make_unique<TreeTemplate<Node>>(root);
593 if (leavesNames.size() == 0)
598 vector<Node*> nodes(leavesNames.size());
600 for (
size_t i = 0; i < leavesNames.size(); ++i)
602 nodes[i] =
new Node(leavesNames[i]);
605 while (nodes.size() > (rooted ? 2 : 3))
608 size_t pos1 = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(nodes.size());
609 Node* node1 = nodes[pos1];
610 nodes.erase(nodes.begin() +
static_cast<ptrdiff_t
>(pos1));
611 size_t pos2 = RandomTools::giveIntRandomNumberBetweenZeroAndEntry<size_t>(nodes.size());
612 Node* node2 = nodes[pos2];
613 nodes.erase(nodes.begin() +
static_cast<ptrdiff_t
>(pos2));
618 nodes.push_back(parent);
622 for (
size_t i = 0; i < nodes.size(); ++i)
626 auto tree = make_unique<TreeTemplate<Node>>(root);
627 tree->resetNodesId();
636 vector<Node*> pathMatrix1;
637 vector<Node*> pathMatrix2;
639 Node* nodeUp = &node1;
642 pathMatrix1.push_back(nodeUp);
645 pathMatrix1.push_back(nodeUp);
650 pathMatrix2.push_back(nodeUp);
653 pathMatrix2.push_back(nodeUp);
655 size_t pos1 = pathMatrix1.size() - 1;
656 size_t pos2 = pathMatrix2.size() - 1;
658 if (pathMatrix1[pos1] != pathMatrix2[pos2])
659 throw Exception(
"TreeTemplateTools::getPathBetweenAnyTwoNodes(). The two nodes do not have any ancestor in common / do not belong to the same tree.");
661 if (pos1 == 0 && pos2 == 0)
664 path.push_back(pathMatrix1[0]);
670 for (
size_t i = (includeAncestorAtEndOfPath ? pathMatrix2.size() : pathMatrix2.size() - 1); i > 0; --i)
672 path.push_back(pathMatrix2[i - 1]);
678 path.insert(path.end(), pathMatrix1.begin(), (includeAncestorAtEndOfPath ? pathMatrix1.end() : --pathMatrix1.end()));
683 while (pathMatrix1[pos1] == pathMatrix2[pos2] && pos1 > 0 && pos2 > 0)
685 commonAnc = pathMatrix1[pos1];
689 path.insert(path.end(), pathMatrix1.begin(), pathMatrix1.begin() +
static_cast<ptrdiff_t
>(pos1 + 1));
690 if (includeAncestor && commonAnc)
691 path.push_back(commonAnc);
695 for (
size_t i = pos2 + 1; i > 0; --i)
697 path.push_back(pathMatrix2[i - 1]);
707 vector<const Node*> path;
708 vector<const Node*> pathMatrix1;
709 vector<const Node*> pathMatrix2;
711 const Node* nodeUp = &node1;
714 pathMatrix1.push_back(nodeUp);
717 pathMatrix1.push_back(nodeUp);
722 pathMatrix2.push_back(nodeUp);
725 pathMatrix2.push_back(nodeUp);
727 size_t pos1 = pathMatrix1.size() - 1;
728 size_t pos2 = pathMatrix2.size() - 1;
730 if (pathMatrix1[pos1] != pathMatrix2[pos2])
731 throw Exception(
"TreeTemplateTools::getPathBetweenAnyTwoNodes(). The two nodes do not have any ancestor in common / do not belong to the same tree.");
733 if (pos1 == 0 && pos2 == 0)
736 path.push_back(pathMatrix1[0]);
742 for (
size_t i = (includeAncestorAtEndOfPath ? pathMatrix2.size() : pathMatrix2.size() - 1); i > 0; --i)
744 path.push_back(pathMatrix2[i - 1]);
750 path.insert(path.end(), pathMatrix1.begin(), (includeAncestorAtEndOfPath ? pathMatrix1.end() : --pathMatrix1.end()));
754 const Node* commonAnc = 0;
755 while (pathMatrix1[pos1] == pathMatrix2[pos2] && pos1 > 0 && pos2 > 0)
757 commonAnc = pathMatrix1[pos1];
761 path.insert(path.end(), pathMatrix1.begin(), pathMatrix1.begin() +
static_cast<ptrdiff_t
>(pos1 + 1));
762 if (commonAnc && includeAncestor)
763 path.push_back(commonAnc);
768 for (
size_t i = pos2 + 1; i > 0; --i)
770 path.push_back(pathMatrix2[i - 1]);
780 vector<const Node*> path = getPathBetweenAnyTwoNodes(node1, node2,
false,
false);
782 for (
size_t i = 0; i < path.size(); ++i)
784 d += path[i]->getDistanceToFather();
793 distsToNodeFather.clear();
804 map<const Node*, vector< pair<string, double>>> leavesDists;
808 processDistsInSubtree_(son, matrix, leavesDists[son]);
813 for (
size_t son1_loc = 0; son1_loc < node->
getNumberOfSons(); ++son1_loc)
815 for (
size_t son2_loc = 0; son2_loc < son1_loc; ++son2_loc)
820 for (vector< pair<string, double>>::iterator son1_leaf = leavesDists[son1].begin();
821 son1_leaf != leavesDists[son1].end();
824 for (vector< pair<string, double>>::iterator son2_leaf = leavesDists[son2].begin();
825 son2_leaf != leavesDists[son2].end();
828 matrix(son1_leaf->first, son2_leaf->first) =
829 matrix(son2_leaf->first, son1_leaf->first) =
830 ( son1_leaf->second + son2_leaf->second );
842 string root_name = node->
getName();
843 for (vector< pair<string, double>>::iterator other_leaf = leavesDists[node->
getSon(0)].begin();
844 other_leaf != leavesDists[node->
getSon(0)].end();
847 matrix(root_name, other_leaf->first) = matrix( other_leaf->first, root_name) = other_leaf->second;
855 distsToNodeFather.clear();
857 for (map<
const Node*, vector<pair<string, double>>>::iterator son = leavesDists.begin(); son != leavesDists.end(); ++son)
859 for (vector< pair<string, double>>::iterator leaf = (son->second).begin(); leaf != (son->second).end(); ++leaf)
861 distsToNodeFather.push_back(make_pair(leaf->first, (leaf->second + nodeToFather)));
869 vector< pair<string, double>> distsToRoot;
870 processDistsInSubtree_(tree.
getRootNode(), *matrix, distsToRoot);
879 vector<const Node*> neighbors2;
880 for (
size_t k = 0; k < neighbors.size(); k++)
882 const Node* n = neighbors[k];
883 if (n != node2 && n != node3)
884 neighbors2.push_back(n);
896 incrementAllIds(node->
getSon(i), increment);
907 getNodePropertyNames(*node.
getSon(i), propertyNames);
917 getNodeProperties(*node.
getSon(i), propertyName, properties);
927 getNodeProperties(*node.
getSon(i), propertyName, properties);
933 for (
auto property: propertyNames)
944 deleteNodeProperties(*node.
getSon(i), propertyNames);
955 getBranchPropertyNames(*node.
getSon(i), propertyNames);
965 getBranchProperties(*node.
getSon(i), propertyName, properties);
975 getBranchProperties(*node.
getSon(i), propertyName, properties);
981 for (
auto property: propertyNames)
992 deleteBranchProperties(*node.
getSon(i), propertyNames);
1010 test &= haveSameOrderedTopology(*n1.
getSon(i), *n2.
getSon(i));
1028 vector<size_t> nbSons;
1029 vector<string> firstLeaves;
1037 nbSons.push_back(otdsub.
size);
1038 firstLeaves.push_back(otdsub.
firstLeaf);
1045 for (
size_t i = 0; i < nbSons.size() - 1; ++i)
1049 if (index.size() == 1 || !orderLeaves)
1057 for (
size_t j = 0; j < index.size(); ++j)
1059 v.push_back(firstLeaves[index[j]]);
1067 nbSons[pos] = nbSons[i];
1074 for (
size_t i = 0; i < nbSons.size() - 1; ++i)
1078 if (index.size() == 1 || !orderLeaves)
1086 for (
size_t j = 0; j < index.size(); ++j)
1088 v.push_back(firstLeaves[index[j]]);
1096 nbSons[pos] = nbSons[i];
1098 nbSons[i] = otd.
size + 1;
1111 if (criterion != MIDROOT_VARIANCE && criterion != MIDROOT_SUM_OF_SQUARES)
1124 pair<Node*, map<string, double>> best_root_branch;
1125 best_root_branch.first = ref_root;
1126 best_root_branch.second [
"position"] = -1;
1127 best_root_branch.second [
"score"] = numeric_limits<double>::max();
1130 getBestRootInSubtree_(tree, criterion, ref_root, best_root_branch);
1134 const double pos = best_root_branch.second[
"position"];
1135 if (pos < 1e-6 or pos > 1 - 1e-6)
1137 tree.
rootAt(pos < 1e-6 ? best_root_branch.first->getFather() : best_root_branch.first);
1144 double root_branch_length = best_root_branch.first->getDistanceToFather();
1145 Node* best_root_father = best_root_branch.first->
getFather();
1147 best_root_father->
removeSon(best_root_branch.first);
1148 best_root_father->
addSon(new_root);
1149 new_root->
addSon(best_root_branch.first);
1152 best_root_branch.first->setDistanceToFather(max((1 - pos) * root_branch_length, 1e-6));
1155 const vector<string> branch_properties = best_root_branch.first->getBranchPropertyNames();
1156 for (vector<string>::const_iterator p = branch_properties.begin(); p != branch_properties.end(); ++p)
1158 new_root->
setBranchProperty(*p, *best_root_branch.first->getBranchProperty(*p));
1164 if (forceBranchRoot)
1167 vector<Node*> root_sons = orig_root->
getSons();
1168 if (root_sons.size() > 2)
1170 Node* nearest = root_sons.at(0);
1171 for (vector<Node*>::iterator n = root_sons.begin(); n !=
1172 root_sons.end(); ++n)
1181 orig_root->
addSon(new_root);
1182 new_root->
addSon(nearest);
1186 for (vector<string>::const_iterator p = branch_properties.begin(); p != branch_properties.end(); ++p)
1215 unresolveUncertainNodes(*son, threshold, property);
1220 if (value < threshold)
1228 subtree.
addSon(i, grandSon);
1240 const vector<Node*> sons = node->
getSons();
1244 for (vector<Node*>::const_iterator son = sons.begin(); son != sons.end(); ++son)
1247 Moments_ son_moment = getSubtreeMoments_(*son);
1251 Moments_ node_moment = getSubtreeMoments_(node);
1260 double min_criterion_value;
1261 double best_position;
1265 const double d = (**son).getDistanceToFather();
1269 double A = 0, B = 0, C = 0;
1270 if (criterion == MIDROOT_SUM_OF_SQUARES)
1272 A = (n1 + n2) * d * d;
1273 B = 2 * d * (m1.
sum - m2.
sum) - 2 * n2 * d * d;
1278 else if (criterion == MIDROOT_VARIANCE)
1280 A = 4 * n1 * n2 * d * d;
1281 B = 4 * d * ( n2 * m1.
sum - n1 * m2.
sum - d * n1 * n2);
1283 + 2 * n1 * d * m2.
sum - 2 * n2 * d * m1.
sum
1289 min_criterion_value = numeric_limits<double>::max();
1290 best_position = 0.5;
1294 min_criterion_value = C - B * B / (4 * A);
1295 best_position = -B / (2 * A);
1296 if (best_position < 0)
1299 min_criterion_value = C;
1301 else if (best_position > 1)
1304 min_criterion_value = A + B + C;
1309 if (min_criterion_value < bestRoot.second[
"score"])
1311 bestRoot.first = *son;
1312 bestRoot.second[
"position"] = best_position;
1313 bestRoot.second[
"score"] = min_criterion_value;
1334 for (
size_t i = 0; i < nsons; ++i)
virtual std::shared_ptr< N > getRoot() const=0
const std::string & nextToken()
General exception thrown when something is wrong with a particular node.
The phylogenetic node class.
virtual std::string getName() const
Get the name associated to this node, if there is one, otherwise throw a NodeException.
virtual Clonable * getNodeProperty(const std::string &name)
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
virtual Node * removeSon(size_t pos)
virtual void deleteDistanceToFather()
Delete the distance to the father node.
virtual int getId() const
Get the node's id.
virtual bool hasBranchProperty(const std::string &name) const
virtual void setId(int id)
Set this node's id.
virtual void addSon(size_t pos, Node *node)
virtual std::vector< Node * > & getSons()
virtual const Node * getSon(size_t pos) const
virtual void setBranchProperty(const std::string &name, const Clonable &property)
Set/add a branch property.
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual bool hasDistanceToFather() const
Tell is this node has a distance to the father.
virtual bool hasNoSon() const
virtual bool isLeaf() const
virtual void deleteNodeProperty(const std::string &name)
void addSubTree(const PhyloTree &tree, std::shared_ptr< PhyloNode > phyloNode)
std::vector< const Node * > getNeighbors() const
virtual Clonable * getBranchProperty(const std::string &name)
virtual void swap(size_t branch1, size_t branch2)
virtual void deleteBranchProperty(const std::string &name)
virtual bool hasFather() const
Tell if this node has a father node.
virtual void setName(const std::string &name)
Give a name or update the name associated to the node.
virtual std::vector< std::string > getBranchPropertyNames() const
virtual std::vector< std::string > getNodePropertyNames() const
virtual bool hasNodeProperty(const std::string &name) const
virtual double getDistanceToFather() const
Get the distance to the father node is there is one, otherwise throw a NodeException.
virtual size_t getNumberOfSons() const
size_t numberOfRemainingTokens() const
bool hasMoreToken() const
const std::string & getToken(size_t pos) const
The phylogenetic tree class.
bool unroot()
Unroot a rooted tree.
bool isRooted() const
Tell if the tree is rooted.
std::vector< std::string > getLeavesNames() const
void rootAt(int nodeId)
Change the root node.
virtual N * getRootNode()
int toInt(const std::string &s, char scientificNotation='e')
double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
std::string removeSurroundingWhiteSpaces(const std::string &s)
bool isEmpty(const std::string &s)
std::string toString(T t)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble