18 #ifndef _GLIBCXX_ALGORITHM
22 #define rt_assert(cond) arb_assert(cond)
24 #if defined(DEBUG) || defined(UNIT_TESTS) // UT_DIFF
25 # define PROVIDE_TREE_STRUCTURE_TESTS
27 #if defined(DEVEL_RALF) && defined(PROVIDE_TREE_STRUCTURE_TESTS)
28 # define AUTO_CHECK_TREE_STRUCTURE // Note: dramatically slows down most tree operations
49 #define DEFINE_READ_ACCESSORS(TYPE, ACCESS, MEMBER) \
50 TYPE ACCESS() { return MEMBER; } \
51 const TYPE ACCESS() const { return MEMBER; }
57 bool seenBootstrapDuringLoad;
70 #if defined(UNIT_TESTS)
71 bool detect_groups_without_gbnode;
76 deleteWithNodes(deleteWithNodes_),
77 seenBootstrapDuringLoad(
false),
99 #if defined(UNIT_TESTS)
100 detect_groups_without_gbnode =
false;
107 if (deleteWithNodes) {
114 return seenBootstrapDuringLoad;
117 seenBootstrapDuringLoad = seen;
124 autoModified = modified;
142 const char *end =
NULp;
143 bootstrap = strtod(remark, (
char**)&end);
145 bool is_bootstrap = end[0] ==
'%' && end[1] == 0;
172 const char *end =
NULp;
173 bootstrap = strtod(label, (
char**)&end);
177 bool is_bootstrap = end !=
label;
181 bootstrap = bootstrap/100.0;
184 case ':': label = end+1;
break;
185 case 0: label =
NULp;
break;
187 is_bootstrap =
false;
194 const char *sep = strchr(label,
':');
202 if (label && !label[0]) label =
NULp;
217 return parses_as_bootstrap;
220 #define KEELED_INDICATOR '!' // prefixed to names of "keeled groups" (not meant to be changed)
233 SmartCharPtr remark_branch;
238 const GBT_LEN& length_ref()
const {
return const_cast<TreeNode*
>(
this)->length_ref(); }
255 if (father->keeledOver) {
277 return father == Father &&
283 return father->
leftson ==
this;
291 return this == subtree || (father &&
get_father()->is_inside(subtree));
314 length_ref() = newlen;
360 return remark_branch.content();
364 return remark_branch;
370 remark_branch = newRemark;
380 #if defined(ASSERTION_USED) || defined(PROVIDE_TREE_STRUCTURE_TESTS)
401 return !father || !father->
father;
405 rt_assert(tree_root->get_root_node() ==
this);
408 root->TreeRoot::change_root(
this,
NULp);
426 #if defined(ASSERTION_USED)
436 leftlen(0.0), rightlen(0.0),
477 if (!tree_root)
return NULp;
479 const TreeNode *root = tree_root->get_root_node();
501 bool old_condition = gb_node &&
name;
502 bool new_condition =
name;
504 if (new_condition && !old_condition) {
505 #if defined(UNIT_TESTS)
507 if (tree_root->detect_groups_without_gbnode) {
513 return old_condition;
516 return (
has_group_info() && keeledOver) ? (inverseLeft ? get_leftson() : get_rightson()) :
NULp;
530 return keeledOver ? (inverseLeft ? 1 : 2) : 0;
533 keeledOver = keeledState;
534 inverseLeft = keeledState == 1;
566 if (!skipKeeledBrothers)
break;
569 if (!keeled || keeled == child)
break;
574 parent = child->get_father();
625 inverseLeft = !inverseLeft;
645 : bootstrap(bootstrap_),
646 branchlength(branchlength_),
647 applyAtLeafs(applyAtLeafs_)
650 class LengthCollector;
657 void eliminate_and_collect(
const multifurc_limits& below, LengthCollector& collect);
660 #if defined(PROVIDE_TREE_STRUCTURE_TESTS)
662 #endif // PROVIDE_TREE_STRUCTURE_TESTS
677 #define DEFINE_TREE_ROOT_ACCESSORS(RootType, TreeType) \
678 DEFINE_DOWNCAST_ACCESSORS(TreeType, get_root_node, TreeRoot::get_root_node())
680 #define DEFINE_TREE_RELATIVES_ACCESSORS(TreeType) \
681 DEFINE_DOWNCAST_ACCESSORS(TreeType, get_father, father); \
682 DEFINE_DOWNCAST_ACCESSORS(TreeType, get_leftson, leftson); \
683 DEFINE_DOWNCAST_ACCESSORS(TreeType, get_rightson, rightson); \
684 DEFINE_DOWNCAST_ACCESSORS(TreeType, get_brother, TreeNode::get_brother()); \
685 DEFINE_DOWNCAST_ACCESSORS(TreeType, get_root_node, TreeNode::get_root_node()); \
686 TreeType *findLeafNamed(const char *wantedName) { return DOWNCAST(TreeType*, TreeNode::findLeafNamed(wantedName)); }
688 #define DEFINE_TREE_ACCESSORS(RootType, TreeType) \
689 DEFINE_DOWNCAST_ACCESSORS(RootType, get_tree_root, TreeNode::get_tree_root()); \
690 DEFINE_TREE_RELATIVES_ACCESSORS(TreeType)
696 #if defined(PROVIDE_TREE_STRUCTURE_TESTS)
697 template <
typename TREE>
698 inline Validity tree_is_valid(
const TREE *
tree,
bool acceptNULL) {
699 if (tree)
return tree->is_valid();
700 return Validity(acceptNULL,
"NULp tree");
702 template <
typename TREE>
703 inline bool tree_is_valid_or_dump(
const TREE *
tree,
bool acceptNULL) {
705 if (!valid) fprintf(stderr,
"\ntree is NOT valid (Reason: %s)\n", valid.
why_not());
710 #if defined(AUTO_CHECK_TREE_STRUCTURE)
711 #define ASSERT_VALID_TREE(tree) rt_assert(tree_is_valid_or_dump(tree, false))
712 #define ASSERT_VALID_TREE_OR_NULL(tree) rt_assert(tree_is_valid_or_dump(tree, true))
714 #define ASSERT_VALID_TREE(tree)
715 #define ASSERT_VALID_TREE_OR_NULL(tree)
716 #endif // AUTO_CHECK_TREE_STRUCTURE
718 #if defined(PROVIDE_TREE_STRUCTURE_TESTS) && defined(UNIT_TESTS)
720 #define TEST_EXPECT_VALID_TREE(tree) TEST_VALIDITY(tree_is_valid(tree, false))
721 #define TEST_EXPECT_VALID_TREE_OR_NULL(tree) TEST_VALIDITY(tree_is_valid(tree, true))
722 #define TEST_EXPECT_VALID_TREE__BROKEN(tree,why) TEST_VALIDITY__BROKEN(tree_is_valid(tree, false), why)
723 #define TEST_EXPECT_VALID_TREE_OR_NULL__BROKEN(tree,why) TEST_VALIDITY__BROKEN(tree_is_valid(tree, true), why)
727 #define TEST_EXPECT_VALID_TREE(tree)
728 #define TEST_EXPECT_VALID_TREE_OR_NULL(tree)
729 #define TEST_EXPECT_VALID_TREE__BROKEN(tree)
730 #define TEST_EXPECT_VALID_TREE_OR_NULL__BROKEN(tree)
791 GBT_LEN adjacent_distance()
const;
792 GBT_LEN length_or_adjacent_distance()
const {
795 if (len>0.0)
return len;
797 return adjacent_distance();
806 #if defined(UNIT_TESTS) // UT_DIFF
807 friend void TEST_edges();
824 from(otherEdge.from),
875 TreeNode *father = to->get_father();
886 TreeNode *father = from->get_father();
898 TreeNode *father = to->get_father();
909 TreeNode *father = from->get_father();
925 return from == otherEdge.from && to == otherEdge.to;
954 TreeNode *father = son->get_father();
966 TreeNode *root_node = root->get_root_node();
971 #error TreeNode.h included twice
ARB_edge(const ARB_edge &otherEdge)
void set_bootstrap(double bootstrap)
void set_branchlength_unrooted(GBT_LEN newlen)
bool was_auto_modified() const
void compute_tree() OVERRIDE
DECLARE_ASSIGNMENT_OPERATOR(ARB_edge)
ARB_edge(TreeNode *From, TreeNode *To)
void destroyNode(TreeNode *node) const OVERRIDE
void virtually_distribute_length(GBT_LEN len, TreeNode::LengthCollector &collect) const
void bootstrap2branchlen()
bool operator==(const ARB_edge &otherEdge) const
TreeNode * makeNode() const OVERRIDE
#define implicated(hypothesis, conclusion)
TreeNode * findLeafNamed(const char *wantedName)
MARK_NONFINAL_METHOD(TreeRoot, change_root,(TreeNode *, TreeNode *))
int calc_clade_level() const
const TreeNode * get_root_node() const
TreeNode * find_parent_clade()
bool has_group_info() const
void unlink_from_father()
const char * why_not() const
GBT_LEN get_branchlength_unrooted() const
virtual void compute_tree()=0
bool is_edge_from_leaf() const
ARB_edge_type get_type() const
void setKeeledState(int keeledState)
TreeRoot * get_tree_root() const
GBT_LEN reset_length_and_bootstrap()
TreeNode * ancestor_common_with(TreeNode *other)
bool remark_will_parse_as_bootstrap(const char *label)
ARB_edge previous() const
bool operator!=(const ARB_edge &otherEdge) const
char * ARB_strpartdup(const char *start, const char *end)
const TreeNode * find_parent_with_groupInfo(bool skipKeeledBrothers=false) const
ARB_edge rootEdge(TreeRoot *root)
void use_as_remark(const SmartCharPtr &newRemark)
multifurc_limits(double bootstrap_, double branchlength_, bool applyAtLeafs_)
void branchlenXbootstrap()
void destroy(TreeRoot *viaRoot)
bool keelsDownGroup(const TreeNode *toSon) const
void reset_branchlengths()
#define DOWNCAST(totype, expr)
SimpleTree(SimpleRoot *sroot)
bool has_valid_root_remarks() const
ARB_edge parentEdge(TreeNode *son)
GBT_RemarkType parse_remark(const char *remark, double &bootstrap)
const TreeNode * get_brother() const
POS_TREE1 * get_father() const
void branchlen2bootstrap()
ARB_edge(TreeNode *From, TreeNode *To, ARB_edge_type Type)
static void destroy(TreeNode *that, TreeRoot *root)
const TreeNode * keelTarget() const
bool in_other_branch_than(const TreeNode *other) const
bool is_son_of_root() const
const TreeNode * ancestor_common_with(const TreeNode *other) const
virtual TreeNode * makeNode() const =0
void swap_node_info(TreeNode *other, bool ofKeeledGroups)
int keeledStateInfo() const
CONSTEXPR_INLINE int leafs_2_edges(int leafs, TreeModel model)
ARB_edge leafEdge(TreeNode *leaf)
GBT_RemarkType parse_bootstrap(double &bootstrap, WarningConsumer report)
DEFINE_READ_ACCESSORS(TreeNode *, get_father, father)
bool is_root_node() const
CONSTEXPR_INLINE_Cxx14 void swap(unsigned char &c1, unsigned char &c2)
bool is_keeled_group() const
bool parse_treelabel(const char *&label, double &bootstrap, char *&remark)
bool in_same_branch_as(const TreeNode *other) const
TreeNode * get_root_node()
ARB_edge counter_previous() const
static void destroy(TreeNode *that)
void multifurcate_whole_tree(const multifurc_limits &below)
virtual unsigned get_leaf_count() const =0
bool is_son_of(const TreeNode *Father) const
CONSTEXPR_INLINE bool valid(SpeciesCreationMode m)
TreeRoot(bool deleteWithNodes_)
TreeNode * find_parent_with_groupInfo(bool skipKeeledBrothers=false)
bool is_ancestor_of(const TreeNode *descendant) const
void reorder_tree(TreeOrder mode)
bool knownNonNull(const void *nonnull)
GBT_LEN root_distance() const
bool has_no_remark() const
bool has_bootstrap() const
bool is_edge_to_leaf() const
void set_branchlength_preserving(GBT_LEN new_len)
TreeNode * fixDeletedSon()
GBT_LEN intree_distance_to(const TreeNode *other) const
bool is_inner_node_with_remark() const
bool is_inside(const TreeNode *subtree) const
DEFINE_READ_ACCESSORS(TreeNode *, get_root_node, rootNode)
void announce_tree_constructed()
void fixKeeledOrientation()
void scale_branchlengths(double factor)
void set_branchlength(GBT_LEN newlen)
void set_tree_root(TreeRoot *new_root)
void set_remark(const char *newRemark)
GBT_LEN sum_child_lengths() const
ARB_edge counter_next() const
bool is_inner_edge() const
GB_ERROR apply_aci_to_remarks(const char *aci, const GBL_call_env &callEnv)
ARB_edge find_innermost_edge()
GBT_LEN get_branchlength() const
virtual void destroyNode(TreeNode *node) const =0
void destroy(TreeNode *that)
virtual void change_root(TreeNode *old, TreeNode *newroot)
void set_length(GBT_LEN len)
const char * get_remark() const
const TreeNode * find_parent_clade() const
const SmartCharPtr & get_remark_ptr() const
void set_bootstrap_seen(bool seen)
void(* WarningConsumer)(const char *message)
const char * get_group_name() const
unsigned get_leaf_count() const OVERRIDE
bool is_normal_group() const
void set_auto_modified(bool modified)
TreeNode * source() const
static int iteration_count(int leafs_in_tree)