Commit 3bf1fd52 authored by Elias Dohmen's avatar Elias Dohmen 🎓

added TreeTest (bifurcation)

parent fe3e6dd1
Pipeline #702 passed with stages
in 1 minute and 55 seconds
......@@ -692,6 +692,46 @@ Tree<TreeNodeType>::~Tree()
// TODO Auto-generated destructor stub
}
/**
* \brief Checks if a tree is strictly bifurcating.
* \details For a strictly bifurcating tree every inner node can just have exactly two child nodes;
* based on the throwError parameter the function either returns boolean (true = tree is strictly bifurcating)
* or throws at first non-bifurcating node a runtime error and quits.
*
* \param tree tree to be checked for bifurcation
* \param throwError if set to true a runtime error is thrown at first non-bifurcating node instead of a false return value
*
* \relates Tree
*/
template <typename DataType>
bool isBifurcatingTree(const Tree<DataType> &tree, bool throwError = false) {
bool bifur = true;
for (auto cNode = tree.postorderBegin(); cNode != tree.postorderEnd(); ++cNode) {
if (!cNode->isLeaf()) {
unsigned int cnum = cNode->nChildren();
if (cnum == 1) {
if (throwError) {
throw std::runtime_error("Error: Not a bifurcating tree. Node with just one child detected - Node-ID " + cNode->id);
}
else {
bifur = false;
}
}
if (cnum > 2) {
if (throwError) {
throw std::runtime_error("Error: Not a bifurcating tree. Node with more than two childs detected - Node-ID " + cNode->id);
}
else {
bifur = false;
}
}
}
}
return bifur;
}
/** @} */ // PhyloGroup
} /* namespace BioSeqDataLib */
......
......@@ -33,10 +33,38 @@
#include "../../src/utility/Matrix.hpp"
#include "../../src/phylogeny/Tree.hpp"
#include "../../src/phylogeny/PhylogeneticTree.hpp"
BOOST_AUTO_TEST_SUITE(Tree_Test)
BOOST_AUTO_TEST_CASE( Bifurcation_Test )
{
// bifurcating tree
BioSeqDataLib::PhylogeneticTree<int> bifurTree;
bifurTree.str2tree("(((C:2.000000,(A:1.000000,B:3.000000)),(D:1.000000,E:1.500000)),OG);"); // bifurcation
bool bifurTreeBifur = BioSeqDataLib::isBifurcatingTree(bifurTree);
BOOST_CHECK_EQUAL(bifurTreeBifur, true);
// monofurcating tree
BioSeqDataLib::PhylogeneticTree<int> monofurTree;
monofurTree.str2tree("((((C),(A,B)),(D,E)),OG);"); // monofurcation
bool monoTreeBifur = BioSeqDataLib::isBifurcatingTree(monofurTree);
BOOST_CHECK_EQUAL(monoTreeBifur, false);
BOOST_CHECK_THROW(BioSeqDataLib::isBifurcatingTree(monofurTree, true), std::runtime_error);
// multifurcating tree
BioSeqDataLib::PhylogeneticTree<int> multifurTree;
multifurTree.str2tree("(((C,B,A),(D,E)),OG);"); // multifurcation
bool multiTreeBifur = BioSeqDataLib::isBifurcatingTree(multifurTree);
BOOST_CHECK_EQUAL(multiTreeBifur, false);
BOOST_CHECK_THROW(BioSeqDataLib::isBifurcatingTree(multifurTree, true), std::runtime_error);
}
BOOST_AUTO_TEST_SUITE_END()
#endif /* TREETEST_HPP_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment