79 getLeavesId(tree, nodeId, leaves);
85 if (!tree.hasNode(nodeId))
87 if (tree.isLeaf(nodeId))
89 leaves.push_back(nodeId);
91 vector<int> sons = tree.getSonsId(nodeId);
92 for (
size_t i = 0; i < sons.size(); i++)
94 getLeavesId(tree, sons[i], leaves);
100 if (!tree.hasNode(nodeId))
104 if (tree.isLeaf(nodeId))
108 vector<int> sons = tree.getSonsId(nodeId);
109 for (
size_t i = 0; i < sons.size(); ++i)
111 n += getNumberOfLeaves(tree, sons[i]);
122 searchLeaf(tree, nodeId, name,
id);
136 if (tree.isLeaf(nodeId))
138 if (tree.getNodeName(nodeId) == name)
140 id =
new int(nodeId);
145 for (
size_t i = 0; i < sons.size(); i++)
147 searchLeaf(tree, nodeId, name,
id);
156 getNodesId(tree, nodeId, nodes);
162 if (!tree.hasNode(nodeId))
164 vector<int> sons = tree.getSonsId(nodeId);
165 for (
size_t i = 0; i < sons.size(); i++)
167 getNodesId(tree, sons[i], nodes);
169 nodes.push_back(nodeId);
176 if (!tree.hasNode(nodeId))
179 vector<int> sons = tree.getSonsId(nodeId);
180 for (
size_t i = 0; i < sons.size(); i++)
182 size_t c = getDepth(tree, sons[i]) + 1;
193 if (!tree.hasNode(nodeId))
196 vector<int> sons = tree.getSonsId(nodeId);
197 for (
size_t i = 0; i < sons.size(); i++)
199 size_t c = getDepths(tree, sons[i], depths) + 1;
211 if (!tree.hasNode(nodeId))
214 vector<int> sons = tree.getSonsId(nodeId);
215 for (
size_t i = 0; i < sons.size(); i++)
218 if (tree.hasDistanceToFather(sons[i]))
219 dist = tree.getDistanceToFather(sons[i]);
222 double c = getHeight(tree, sons[i]) + dist;
233 if (!tree.hasNode(nodeId))
236 vector<int> sons = tree.getSonsId(nodeId);
237 for (
size_t i = 0; i < sons.size(); i++)
240 if (tree.hasDistanceToFather(sons[i]))
241 dist = tree.getDistanceToFather(sons[i]);
244 double c = getHeights(tree, sons[i], heights) + dist;
256 if (!tree.hasNode(nodeId))
259 if (tree.isLeaf(nodeId))
261 s << tree.getNodeName(nodeId);
266 vector<int> sonsId = tree.getSonsId(nodeId);
267 s << nodeToParenthesis(tree, sonsId[0], writeId);
268 for (
size_t i = 1; i < sonsId.size(); i++)
270 s <<
"," << nodeToParenthesis(tree, sonsId[i], writeId);
276 if (tree.isLeaf(nodeId))
282 if (tree.hasBranchProperty(nodeId, BOOTSTRAP))
283 s << (
dynamic_cast<const Number<double>*
>(tree.getBranchProperty(nodeId, BOOTSTRAP))->getValue());
285 if (tree.hasDistanceToFather(nodeId))
286 s <<
":" << tree.getDistanceToFather(nodeId);
294 if (!tree.hasNode(nodeId))
297 if (tree.isLeaf(nodeId))
299 s << tree.getNodeName(nodeId);
304 vector<int> sonsId = tree.getSonsId(nodeId);
305 s << nodeToParenthesis(tree, sonsId[0], bootstrap, propertyName);
306 for (
size_t i = 1; i < sonsId.size(); i++)
308 s <<
"," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
314 if (tree.hasBranchProperty(nodeId, BOOTSTRAP))
315 s << (
dynamic_cast<const Number<double>*
>(tree.getBranchProperty(nodeId, BOOTSTRAP))->getValue());
319 if (tree.hasBranchProperty(nodeId, propertyName))
320 s << *(dynamic_cast<const BppString*>(tree.getBranchProperty(nodeId, propertyName)));
323 if (tree.hasDistanceToFather(nodeId))
324 s <<
":" << tree.getDistanceToFather(nodeId);
335 vector<int> sonsId = tree.
getSonsId(rootId);
339 for (
size_t i = 0; i < sonsId.size(); i++)
341 s <<
"," << nodeToParenthesis(tree, sonsId[i], writeId);
346 if (sonsId.size() > 0)
348 s << nodeToParenthesis(tree, sonsId[0], writeId);
349 for (
size_t i = 1; i < sonsId.size(); i++)
351 s <<
"," << nodeToParenthesis(tree, sonsId[i], writeId);
367 vector<int> sonsId = tree.
getSonsId(rootId);
371 for (
size_t i = 0; i < sonsId.size(); i++)
373 s <<
"," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
378 s << nodeToParenthesis(tree, sonsId[0], bootstrap, propertyName);
379 for (
size_t i = 1; i < sonsId.size(); i++)
381 s <<
"," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
393 s << *(dynamic_cast<const BppString*>(tree.
getBranchProperty(rootId, propertyName)));
404 if (!tree.hasNode(nodeId1))
406 if (!tree.hasNode(nodeId2))
409 vector<int> pathMatrix1;
410 vector<int> pathMatrix2;
412 int nodeUp = nodeId1;
413 while (tree.hasFather(nodeUp))
415 pathMatrix1.push_back(nodeUp);
416 nodeUp = tree.getFatherId(nodeUp);
418 pathMatrix1.push_back(nodeUp);
421 while (tree.hasFather(nodeUp))
423 pathMatrix2.push_back(nodeUp);
424 nodeUp = tree.getFatherId(nodeUp);
426 pathMatrix2.push_back(nodeUp);
429 size_t tmp1 = pathMatrix1.size();
430 size_t tmp2 = pathMatrix2.size();
432 while ((tmp1 > 0) && (tmp2 > 0))
434 if (pathMatrix1[tmp1 - 1] != pathMatrix2[tmp2 - 1])
440 for (
size_t y = 0; y < tmp1; ++y)
442 path.push_back(pathMatrix1[y]);
445 path.push_back(pathMatrix1[tmp1]);
446 for (
size_t j = tmp2; j > 0; --j)
448 path.push_back(pathMatrix2[j - 1]);
461 vector<int> path = getPathBetweenAnyTwoNodes(tree, nodeId1, nodeId2,
false);
463 for (
size_t i = 0; i < path.size(); i++)
474 if (!tree.hasNode(nodeId))
477 if (tree.hasDistanceToFather(nodeId))
478 brLen[0] = tree.getDistanceToFather(nodeId);
480 throw NodeException(
"TreeTools::getbranchLengths(). No branch length.", nodeId);
481 vector<int> sons = tree.getSonsId(nodeId);
482 for (
size_t i = 0; i < sons.size(); i++)
484 Vdouble sonBrLen = getBranchLengths(tree, sons[i]);
485 for (
size_t j = 0; j < sonBrLen.size(); j++)
487 brLen.push_back(sonBrLen[j]);
497 if (!tree.hasNode(nodeId))
499 if (includeAncestor && !tree.hasDistanceToFather(nodeId))
500 throw NodeException(
"TreeTools::getTotalLength(). No branch length.", nodeId);
501 double length = includeAncestor ? tree.getDistanceToFather(nodeId) : 0;
502 vector<int> sons = tree.getSonsId(nodeId);
503 for (
size_t i = 0; i < sons.size(); i++)
505 length += getTotalLength(tree, sons[i],
true);
514 if (!tree.hasNode(nodeId))
516 vector<int> nodes = getNodesId(tree, nodeId);
517 for (
size_t i = 0; i < nodes.size(); i++)
519 tree.setDistanceToFather(nodes[i], brLen);
527 if (!tree.hasNode(nodeId))
529 vector<int> nodes = getNodesId(tree, nodeId);
530 for (
size_t i = 0; i < nodes.size(); i++)
532 if (!tree.hasDistanceToFather(nodes[i]))
533 tree.setDistanceToFather(nodes[i], brLen);
541 if (!tree.hasNode(nodeId))
543 vector<int> nodes = getNodesId(tree, nodeId);
544 for (
size_t i = 0; i < nodes.size(); i++)
546 if (tree.hasFather(nodes[i]))
548 if (!tree.hasDistanceToFather(nodes[i]))
549 throw NodeException(
"TreeTools::scaleTree(). Branch with no length", nodes[i]);
550 double brLen = tree.getDistanceToFather(nodes[i]) * factor;
551 tree.setDistanceToFather(nodes[i], brLen);
560 if (!tree.hasNode(nodeId))
562 vector<int> sons = tree.getSonsId(nodeId);
563 vector<size_t> h(sons.size());
564 for (
size_t i = 0; i < sons.size(); i++)
566 h[i] = initBranchLengthsGrafen(tree, sons[i]);
568 size_t thish = sons.size() == 0 ? 0 : VectorTools::sum<size_t>(h) + sons.size() - 1;
569 for (
size_t i = 0; i < sons.size(); i++)
571 tree.setDistanceToFather(sons[i], (
double)(thish - h[i]));
578 initBranchLengthsGrafen(tree, tree.
getRootId());
589 double& heightRaised)
592 if (!tree.hasNode(nodeId))
594 vector<int> sons = tree.getSonsId(nodeId);
595 vector<double> hr(sons.size());
597 for (
size_t i = 0; i < sons.size(); i++)
600 if (tree.hasDistanceToFather(son))
603 computeBranchLengthsGrafen(tree, sons[i], power, total, h, hr[i]);
604 double d = h + tree.getDistanceToFather(son);
609 throw NodeException (
"TreeTools::computeBranchLengthsGrafen. Branch length lacking.", son);
611 heightRaised = pow(height / total, power) * total;
612 for (
size_t i = 0; i < sons.size(); i++)
614 tree.setDistanceToFather(sons[i], heightRaised - hr[i]);
621 int rootId = tree.getRootId();
624 initBranchLengthsGrafen(tree);
627 double totalHeight = getHeight(tree, rootId);
629 computeBranchLengthsGrafen(tree, rootId, power, totalHeight, h, hr);
638 vector<int> sons = tree.
getSonsId(nodeId);
639 vector<double> h(sons.size());
643 for (
size_t i = 0; i < sons.size(); i++)
648 h[i] = convertToClockTree(tree, son);
654 throw NodeException (
"TreeTools::convertToClockTree. Branch length lacking.", son);
657 l /= (
double)sons.size();
660 for (
size_t i = 0; i < sons.size(); i++)
673 vector<int> sons = tree.
getSonsId(nodeId);
674 vector<double> h(sons.size());
678 for (
size_t i = 0; i < sons.size(); i++)
683 h[i] = convertToClockTree2(tree, son);
689 throw NodeException (
"TreeTools::convertToClockTree2. Branch length lacking.", son);
692 l /= (
double)sons.size();
693 for (
size_t i = 0; i < sons.size(); i++)
695 scaleTree(tree, sons[i], h[i] > 0 ? l / h[i] : 0);
706 for (
size_t i = 0; i < names.size(); i++)
709 for (
size_t j = 0; j < i; j++)
711 (*mat)(i, j) = (*mat)(j, i) = getDistanceBetweenAnyTwoNodes(tree, tree.
getLeafId(names[i]), tree.
getLeafId(names[j]));
725 double dmid = (*dist)(pos[0], pos[1]) / 2;
729 double d1 = getDistanceBetweenAnyTwoNodes(tree, id1, rootId);
730 double d2 = getDistanceBetweenAnyTwoNodes(tree, id2, rootId);
731 int current = d2 > d1 ? id2 : id1;
743 if (brother == current)
755 for (
size_t i = 0; i < sonsId.size(); i++)
757 int subMax = getMaxId(tree, sonsId[i]);
768 vector<int> ids = getNodesId(tree,
id);
769 sort(ids.begin(), ids.end());
771 for (
size_t i = 0; i < ids.size(); i++)
773 if (ids[i] != (
int)i)
777 return (
int)ids.size();
784 vector<int> ids = tree.getNodesId();
785 sort(ids.begin(), ids.end());
786 for (
size_t i = 1; i < ids.size(); i++)
788 if (ids[i] == ids[i - 1])
802 vector<BipartitionList*> vecBipL;
803 for (
size_t i = 0; i < vecTr.size(); i++)
810 for (
size_t i = 0; i < vecTr.size(); i++)
824 vector<size_t> size1, size2;
846 for (
size_t i = 0; i < nbbip; i++)
850 if (size1[i] != size2[i])
855 for (
size_t i = 0; i < nbbip; i++)
857 for (jj = 0; jj < nbbip; jj++)
875 vector<size_t> size1, size2;
880 throw Exception(
"Distinct leaf sets between trees ");
905 bipOK2.push_back(
false);
929 *missing_in_tr1 = missing1;
931 *missing_in_tr2 = missing2;
932 return missing1 + missing2;
939 vector<BipartitionList*> vecBipL;
941 vector<size_t> bipSize;
945 for (
size_t i = 0; i < vecTr.size(); i++)
950 for (
size_t i = 0; i < vecTr.size(); i++)
958 for (
size_t i = 0; i < nbBip; i++)
961 bipScore.push_back(1);
965 for (
size_t i = nbBip; i > 0; i--)
967 if (bipScore[i - 1] == 0)
969 for (
size_t j = i - 1; j > 0; j--)
971 if (bipScore[j - 1] && bipSize[i - 1] == bipSize[j - 1] && mergedBipL->
areIdentical(i - 1, j - 1))
980 for (
size_t i = nbBip; i > 0; i--)
982 if (bipScore[i - 1] == 0)
984 bipScore.erase(bipScore.begin() + i - 1);
993 bipScore.push_back(vecTr.size());
1003 vector<size_t> bipScore;
1004 vector<string> tr0leaves;
1008 if (vecTr.size() == 0)
1009 throw Exception(
"TreeTools::thresholdConsensus. Empty vector passed");
1014 tr0leaves = vecTr[0]->getLeavesNames();
1015 for (
size_t i = 1; i < vecTr.size(); i++)
1018 throw Exception(
"TreeTools::thresholdConsensus. Distinct leaf sets between trees");
1022 bipL = bipartitionOccurrences(vecTr, bipScore);
1028 score =
static_cast<int>(bipScore[i - 1]) / static_cast<double>(vecTr.size());
1029 if (score <= threshold && score != 1.)
1055 return thresholdConsensus(vecTr, 0., checkNames);
1062 return thresholdConsensus(vecTr, 0.5, checkNames);
1069 return thresholdConsensus(vecTr, 1., checkNames);
1084 BioNJ bionjTreeBuilder(
false,
false);
1107 vector<size_t> occurences;
1120 bootstrapValues[i] = (double) occurences[j] * 100. / (
double) vecTr.size();
1126 for (
size_t i = 0; i < index.size(); i++)
1128 if (!tree.
isLeaf(index[i]))
1140 int currentId = nodeId;
1141 while (tree.hasFather(currentId))
1143 currentId = tree.getFatherId(currentId);
1144 ids.push_back(currentId);
1153 if (nodeIds.size() == 0)
1154 throw Exception(
"TreeTools::getLastCommonAncestor(). You must provide at least one node id.");
1155 vector< vector<int> > ancestors(nodeIds.size());
1156 for (
size_t i = 0; i < nodeIds.size(); i++)
1158 ancestors[i] = getAncestors(tree, nodeIds[i]);
1159 ancestors[i].insert(ancestors[i].begin(), nodeIds[i]);
1161 int lca = tree.getRootId();
1165 if (ancestors[0].size() <= count)
1167 int current = ancestors[0][ancestors[0].size() - count - 1];
1168 for (
size_t i = 1; i < nodeIds.size(); i++)
1170 if (ancestors[i].size() <= count)
1172 if (ancestors[i][ancestors[i].size() - count - 1] != current)
1188 throw Exception(
"The tree has to be rooted on the branch of interest to determine the midpoint position of the root");
1191 throw Exception(
"The tree is multifurcated, which is not allowed.");
1198 double x = bestRootPosition_(tree, sonsIds.at(0), sonsIds.at(1), length);
1213 m1 = statFromNode_(tree, nodeId1);
1214 m2 = statFromNode_(tree, nodeId2);
1215 A = 4 * m1.
N * (m2.
N * length) * length;
1216 B = 4 * length * (m2.
N * m1.
sum - m1.
N * m2.
sum - length * m1.
N * m2.
N);
1250 vector<int> sonsId = tree.
getSonsId(rootId);
1251 for (
size_t i = 0; i < sonsId.size(); i++)
1253 mtmp = statFromNode_(tree, sonsId.at(i));
1256 m.
sum += mtmp.
sum + bLength * mtmp.
N;