14 #include "../Distance/BioNJ.h"
18 #include "../Parsimony/DRTreeParsimonyScore.h"
19 #include "../Model/Nucleotide/JCnuc.h"
45 getLeavesId(tree, nodeId, leaves);
55 leaves.push_back(nodeId);
57 vector<int> sons = tree.
getSonsId(nodeId);
58 for (
size_t i = 0; i < sons.size(); i++)
60 getLeavesId(tree, sons[i], leaves);
74 vector<int> sons = tree.
getSonsId(nodeId);
75 for (
size_t i = 0; i < sons.size(); ++i)
77 n += getNumberOfLeaves(tree, sons[i]);
87 searchLeaf(tree, nodeId, name,
id);
104 id =
new int(nodeId);
109 for (
size_t i = 0; i < sons.size(); i++)
111 searchLeaf(tree, nodeId, name,
id);
120 getNodesId(tree, nodeId, nodes);
128 vector<int> sons = tree.
getSonsId(nodeId);
129 for (
size_t i = 0; i < sons.size(); i++)
131 getNodesId(tree, sons[i], nodes);
133 nodes.push_back(nodeId);
143 vector<int> sons = tree.
getSonsId(nodeId);
144 for (
size_t i = 0; i < sons.size(); i++)
146 size_t c = getDepth(tree, sons[i]) + 1;
160 vector<int> sons = tree.
getSonsId(nodeId);
161 for (
size_t i = 0; i < sons.size(); i++)
163 size_t c = getDepths(tree, sons[i], depths) + 1;
178 vector<int> sons = tree.
getSonsId(nodeId);
179 for (
size_t i = 0; i < sons.size(); i++)
186 double c = getHeight(tree, sons[i]) + dist;
200 vector<int> sons = tree.
getSonsId(nodeId);
201 for (
size_t i = 0; i < sons.size(); i++)
208 double c = getHeights(tree, sons[i], heights) + dist;
230 vector<int> sonsId = tree.
getSonsId(nodeId);
231 s << nodeToParenthesis(tree, sonsId[0], writeId);
232 for (
size_t i = 1; i < sonsId.size(); i++)
234 s <<
"," << nodeToParenthesis(tree, sonsId[i], writeId);
269 vector<int> sonsId = tree.
getSonsId(nodeId);
270 s << nodeToParenthesis(tree, sonsId[0], bootstrap, propertyName);
271 for (
size_t i = 1; i < sonsId.size(); i++)
273 s <<
"," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
301 vector<int> sonsId = tree.
getSonsId(rootId);
305 for (
size_t i = 0; i < sonsId.size(); i++)
307 s <<
"," << nodeToParenthesis(tree, sonsId[i], writeId);
312 if (sonsId.size() > 0)
314 s << nodeToParenthesis(tree, sonsId[0], writeId);
315 for (
size_t i = 1; i < sonsId.size(); i++)
317 s <<
"," << nodeToParenthesis(tree, sonsId[i], writeId);
333 vector<int> sonsId = tree.
getSonsId(rootId);
338 for (
size_t i = 0; i < sonsId.size(); i++)
340 s <<
"," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
345 s << nodeToParenthesis(tree, sonsId[0], bootstrap, propertyName);
346 for (
size_t i = 1; i < sonsId.size(); i++)
348 s <<
"," << nodeToParenthesis(tree, sonsId[i], bootstrap, propertyName);
376 vector<int> pathMatrix1;
377 vector<int> pathMatrix2;
379 int nodeUp = nodeId1;
382 pathMatrix1.push_back(nodeUp);
385 pathMatrix1.push_back(nodeUp);
390 pathMatrix2.push_back(nodeUp);
393 pathMatrix2.push_back(nodeUp);
396 size_t tmp1 = pathMatrix1.size();
397 size_t tmp2 = pathMatrix2.size();
399 while ((tmp1 > 0) && (tmp2 > 0))
401 if (pathMatrix1[tmp1 - 1] != pathMatrix2[tmp2 - 1])
407 for (
size_t y = 0; y < tmp1; ++y)
409 path.push_back(pathMatrix1[y]);
412 path.push_back(pathMatrix1[tmp1]);
413 for (
size_t j = tmp2; j > 0; --j)
415 path.push_back(pathMatrix2[j - 1]);
428 vector<int> path = getPathBetweenAnyTwoNodes(tree, nodeId1, nodeId2,
false);
430 for (
size_t i = 0; i < path.size(); i++)
447 throw NodeException(
"TreeTools::getbranchLengths(). No branch length.", nodeId);
448 vector<int> sons = tree.
getSonsId(nodeId);
449 for (
size_t i = 0; i < sons.size(); i++)
451 Vdouble sonBrLen = getBranchLengths(tree, sons[i]);
452 for (
size_t j = 0; j < sonBrLen.size(); j++)
454 brLen.push_back(sonBrLen[j]);
467 throw NodeException(
"TreeTools::getTotalLength(). No branch length.", nodeId);
469 vector<int> sons = tree.
getSonsId(nodeId);
470 for (
size_t i = 0; i < sons.size(); i++)
472 length += getTotalLength(tree, sons[i],
true);
483 vector<int> nodes = getNodesId(tree, nodeId);
484 for (
size_t i = 0; i < nodes.size(); i++)
496 vector<int> nodes = getNodesId(tree, nodeId);
497 for (
size_t i = 0; i < nodes.size(); i++)
510 vector<int> nodes = getNodesId(tree, nodeId);
511 for (
size_t i = 0; i < nodes.size(); i++)
516 throw NodeException(
"TreeTools::scaleTree(). Branch with no length", nodes[i]);
529 vector<int> sons = tree.
getSonsId(nodeId);
530 vector<size_t> h(sons.size());
531 for (
size_t i = 0; i < sons.size(); i++)
533 h[i] = initBranchLengthsGrafen(tree, sons[i]);
535 size_t thish = sons.size() == 0 ? 0 : VectorTools::sum<size_t>(h) + sons.size() - 1;
536 for (
size_t i = 0; i < sons.size(); i++)
545 initBranchLengthsGrafen(tree, tree.
getRootId());
556 double& heightRaised)
560 vector<int> sons = tree.
getSonsId(nodeId);
561 vector<double> hr(sons.size());
563 for (
size_t i = 0; i < sons.size(); i++)
569 computeBranchLengthsGrafen(tree, sons[i], power, total, h, hr[i]);
575 throw NodeException (
"TreeTools::computeBranchLengthsGrafen. Branch length lacking.", son);
577 heightRaised =
std::pow(height / total, power) * total;
578 for (
size_t i = 0; i < sons.size(); i++)
589 initBranchLengthsGrafen(tree);
592 double totalHeight = getHeight(tree, rootId);
594 computeBranchLengthsGrafen(tree, rootId, power, totalHeight, h, hr);
603 vector<int> sons = tree.
getSonsId(nodeId);
604 vector<double> h(sons.size());
608 for (
size_t i = 0; i < sons.size(); i++)
613 h[i] = convertToClockTree(tree, son);
619 throw NodeException (
"TreeTools::convertToClockTree. Branch length lacking.", son);
622 l /= (double)sons.size();
625 for (
size_t i = 0; i < sons.size(); i++)
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] = convertToClockTree2(tree, son);
654 throw NodeException (
"TreeTools::convertToClockTree2. Branch length lacking.", son);
657 l /= (double)sons.size();
658 for (
size_t i = 0; i < sons.size(); i++)
660 scaleTree(tree, sons[i], h[i] > 0 ? l / h[i] : 0);
670 auto mat = make_unique<DistanceMatrix>(names);
671 for (
size_t i = 0; i < names.size(); i++)
674 for (
size_t j = 0; j < i; j++)
676 (*mat)(i, j) = (*mat)(j, i) = getDistanceBetweenAnyTwoNodes(tree, tree.
getLeafId(names[i]), tree.
getLeafId(names[j]));
686 throw Exception(
"TreeTools::midpointRooting(Tree). This function is deprecated, use TreeTemplateTools::midRoot instead!");
689 auto dist = getDistanceMatrix(tree);
691 double dmid = (*dist)(pos[0], pos[1]) / 2;
692 int id1 = tree.
getLeafId(dist->getName(pos[0]));
693 int id2 = tree.
getLeafId(dist->getName(pos[1]));
695 double d1 = getDistanceBetweenAnyTwoNodes(tree, id1, rootId);
696 double d2 = getDistanceBetweenAnyTwoNodes(tree, id2, rootId);
697 int current = d2 > d1 ? id2 : id1;
708 if (brother == current)
720 for (
size_t i = 0; i < sonsId.size(); i++)
722 int subMax = getMaxId(tree, sonsId[i]);
733 vector<int> ids = getNodesId(tree,
id);
734 sort(ids.begin(), ids.end());
736 for (
size_t i = 0; i < ids.size(); i++)
738 if (ids[i] != (
int)i)
742 return (
int)ids.size();
750 sort(ids.begin(), ids.end());
751 for (
size_t i = 1; i < ids.size(); i++)
753 if (ids[i] == ids[i - 1])
767 vector<unique_ptr<BipartitionList>> vecBipL;
768 for (
size_t i = 0; i < vecTr.size(); i++)
770 vecBipL.push_back(make_unique<BipartitionList>(*vecTr[i]));
780 vector<unique_ptr<BipartitionList>> vecBipL;
781 for (
size_t i = 0; i < vecTr.size(); i++)
783 vecBipL.push_back(make_unique<BipartitionList>(*vecTr[i]));
794 unique_ptr<BipartitionList> bipL1, bipL2;
795 vector<size_t> size1, size2;
802 bipL1 = make_unique<BipartitionList>(tr1,
true);
803 bipL1->removeTrivialBipartitions();
804 bipL1->removeRedundantBipartitions();
805 bipL1->sortByPartitionSize();
806 bipL2 = make_unique<BipartitionList>(tr2,
true);
807 bipL2->removeTrivialBipartitions();
808 bipL2->removeRedundantBipartitions();
809 bipL2->sortByPartitionSize();
812 if (bipL1->getNumberOfBipartitions() != bipL2->getNumberOfBipartitions())
814 nbbip = bipL1->getNumberOfBipartitions();
817 for (
size_t i = 0; i < nbbip; i++)
819 size1.push_back(bipL1->getPartitionSize(i));
820 size2.push_back(bipL1->getPartitionSize(i));
821 if (size1[i] != size2[i])
826 for (
size_t i = 0; i < nbbip; i++)
828 for (jj = 0; jj < nbbip; jj++)
844 unique_ptr<BipartitionList> bipL1, bipL2;
846 vector<size_t> size1, size2;
851 throw Exception(
"Distinct leaf sets between trees ");
857 bipL1 = make_unique<BipartitionList>(tr1,
true);
858 bipL1->removeTrivialBipartitions();
859 bipL1->sortByPartitionSize();
860 bipL2 = make_unique<BipartitionList>(tr2,
true);
861 bipL2->removeTrivialBipartitions();
862 bipL2->sortByPartitionSize();
865 for (i = 0; i < bipL1->getNumberOfBipartitions(); i++)
867 size1.push_back(bipL1->getPartitionSize(i));
869 for (i = 0; i < bipL2->getNumberOfBipartitions(); i++)
871 size2.push_back(bipL2->getPartitionSize(i));
874 for (i = 0; i < bipL2->getNumberOfBipartitions(); i++)
876 bipOK2.push_back(
false);
881 for (i = 0; i < bipL1->getNumberOfBipartitions(); i++)
883 for (j = 0; j < bipL2->getNumberOfBipartitions(); j++)
893 if (j == bipL2->getNumberOfBipartitions())
897 missing1 =
static_cast<int>(bipL2->getNumberOfBipartitions()) -
static_cast<int>(bipL1->getNumberOfBipartitions()) + missing2;
900 *missing_in_tr1 = missing1;
902 *missing_in_tr2 = missing2;
903 return missing1 + missing2;
910 vector<unique_ptr<BipartitionList>> vecBipL;
911 unique_ptr<BipartitionList> mergedBipL;
912 vector<size_t> bipSize;
916 for (
size_t i = 0; i < vecTr.size(); i++)
918 vecBipL.push_back(make_unique<BipartitionList>(*vecTr[i]));
923 mergedBipL->removeTrivialBipartitions();
924 nbBip = mergedBipL->getNumberOfBipartitions();
926 for (
size_t i = 0; i < nbBip; i++)
928 bipSize.push_back(mergedBipL->getPartitionSize(i));
929 bipScore.push_back(1);
933 for (
size_t i = nbBip; i > 0; i--)
935 if (bipScore[i - 1] == 0)
937 for (
size_t j = i - 1; j > 0; j--)
939 if (bipScore[j - 1] && bipSize[i - 1] == bipSize[j - 1] && mergedBipL->areIdentical(i - 1, j - 1))
948 for (
size_t i = nbBip; i > 0; i--)
950 if (bipScore[i - 1] == 0)
952 bipScore.erase(bipScore.begin() +
static_cast<ptrdiff_t
>(i - 1));
953 mergedBipL->deleteBipartition(i - 1);
958 mergedBipL->addTrivialBipartitions(
false);
959 for (
size_t i = 0; i < mergedBipL->getNumberOfElements(); i++)
961 bipScore.push_back(vecTr.size());
971 vector<size_t> bipScore;
972 vector<string> tr0leaves;
973 unique_ptr<BipartitionList> bipL;
976 if (vecTr.size() == 0)
977 throw Exception(
"TreeTools::thresholdConsensus. Empty vector passed");
982 tr0leaves = vecTr[0]->getLeavesNames();
983 for (
size_t i = 1; i < vecTr.size(); i++)
986 throw Exception(
"TreeTools::thresholdConsensus. Distinct leaf sets between trees");
990 bipL = bipartitionOccurrences(vecTr, bipScore);
992 for (
size_t i = bipL->getNumberOfBipartitions(); i > 0; i--)
994 if (bipL->getPartitionSize(i - 1) == 1)
996 score =
static_cast<int>(bipScore[i - 1]) /
static_cast<double>(vecTr.size());
997 if (score <= threshold && score != 1.)
999 bipL->deleteBipartition(i - 1);
1004 for (
size_t j = bipL->getNumberOfBipartitions(); j > i; j--)
1006 if (!bipL->areCompatible(i - 1, j - 1))
1008 bipL->deleteBipartition(i - 1);
1014 return bipL->toTree();
1021 return thresholdConsensus(vecTr, 0., checkNames);
1028 return thresholdConsensus(vecTr, 0.5, checkNames);
1035 return thresholdConsensus(vecTr, 1., checkNames);
1042 throw Exception(
"TreeTools::MRP not updated.");
1077 vector<size_t> occurences;
1079 auto bpList = bipartitionOccurrences(vecTr, occurences);
1087 for (
size_t j = 0; j < bpList->getNumberOfBipartitions(); j++)
1091 bootstrapValues[i] = format >= 0 ? round(
static_cast<double>(occurences[j]) *
std::pow(10., 2 + format) /
static_cast<double>(vecTr.size())) /
std::pow(10., format) :
static_cast<double>(occurences[j]);
1097 for (
size_t i = 0; i < index.size(); i++)
1099 if (!tree.
isLeaf(index[i]))
1109 int currentId = nodeId;
1113 ids.push_back(currentId);
1122 if (nodeIds.size() == 0)
1123 throw Exception(
"TreeTools::getLastCommonAncestor(). You must provide at least one node id.");
1124 vector< vector<int>> ancestors(nodeIds.size());
1125 for (
size_t i = 0; i < nodeIds.size(); i++)
1127 ancestors[i] = getAncestors(tree, nodeIds[i]);
1128 ancestors[i].insert(ancestors[i].begin(), nodeIds[i]);
1134 if (ancestors[0].size() <=
count)
1136 int current = ancestors[0][ancestors[0].size() -
count - 1];
1137 for (
size_t i = 1; i < nodeIds.size(); i++)
1139 if (ancestors[i].size() <=
count)
1141 if (ancestors[i][ancestors[i].size() -
count - 1] != current)
1157 throw Exception(
"The tree has to be rooted on the branch of interest to determine the midpoint position of the root");
1160 throw Exception(
"The tree is multifurcated, which is not allowed.");
1167 double x = bestRootPosition_(tree, sonsIds.at(0), sonsIds.at(1), length);
1182 m1 = statFromNode_(tree, nodeId1);
1183 m2 = statFromNode_(tree, nodeId2);
1184 A = 4 * m1.
N * (m2.
N * length) * length;
1185 B = 4 * length * (m2.
N * m1.
sum - m1.
N * m2.
sum - length * m1.
N * m2.
N);
1219 vector<int> sonsId = tree.
getSonsId(rootId);
1220 for (
size_t i = 0; i < sonsId.size(); i++)
1222 mtmp = statFromNode_(tree, sonsId.at(i));
1225 m.
sum += mtmp.
sum + bLength * mtmp.
N;
1237 throw Exception(
"TreeTools::MRPMultilabel not updated.");
This class deals with the bipartitions defined by trees.
size_t getNumberOfBipartitions() const
General exception thrown when something is wrong with a particular node.
Exception thrown when something is wrong with a particular node.
Interface for phylogenetic tree objects.
virtual std::vector< int > getSonsId(int parentId) const =0
virtual bool isRooted() const =0
Tell if the tree is rooted.
virtual std::vector< int > getNodesId() const =0
virtual bool hasFather(int nodeId) const =0
virtual void setDistanceToFather(int nodeId, double length)=0
virtual void setBranchProperty(int nodeId, const std::string &name, const Clonable &property)=0
virtual std::string getNodeName(int nodeId) const =0
virtual Clonable * getBranchProperty(int nodeId, const std::string &name)=0
virtual int getFatherId(int parentId) const =0
virtual bool unroot()=0
Unroot a rooted tree.
virtual bool hasNode(int nodeId) const =0
virtual bool isLeaf(int nodeId) const =0
virtual bool isMultifurcating() const =0
Tell if the tree is multifurcating.
virtual bool hasDistanceToFather(int nodeId) const =0
virtual double getDistanceToFather(int nodeId) const =0
virtual bool hasNoSon(int nodeId) const =0
virtual void newOutGroup(int nodeId)=0
Root a tree by specifying an outgroup.
virtual bool hasBranchProperty(int nodeId, const std::string &name) const =0
virtual int getRootId() const =0
virtual int getLeafId(const std::string &name) const =0
virtual std::vector< std::string > getLeavesNames() const =0
std::string toString(T t)
std::size_t count(const std::string &s, const std::string &pattern)
Defines the basic types of data flow nodes.
std::vector< double > Vdouble
ExtendedFloat pow(const ExtendedFloat &ef, double exp)