35 for (i=0; i<rate_len; i++) {
36 if (rates[i] > max) max = rates[i];
39 for (i=0; i<rate_len; i++) {
40 putchar(
'0' + (
int)(rates[i]/max*9.9));
46 memset ((
char *)
this, 0,
sizeof(
AP_rates));
55 for (
int i=0; i<rate_len; i++) {
69 for (j=i=0; i<rate_len; j++) {
87 root_changed_cb(
NULp), root_changed_cd(
NULp),
88 node_deleted_cb(
NULp), node_deleted_cd(
NULp),
118 if (gb_species_data) {
126 if (gb_species_data) {
167 new_brother->
father = new_root;
187 new_brother->get_father()->remove_root_remark();
190 new_tree->leftson =
this;
191 new_tree->rightson = new_brother;
195 if (new_brother->
father) {
209 new_tree->rightlen = laenge;
210 new_brother->
father = new_tree;
215 if (!new_tree->father) troot->
change_root(new_brother, new_tree);
216 new_tree->set_tree_root(troot);
226 if (root_changed_cb) {
247 if (node_deleted_cb) node_deleted_cb(node_deleted_cd, del);
251 root_changed_cb =
cb;
252 root_changed_cd = cd;
256 node_deleted_cb =
cb;
257 node_deleted_cd = cd;
288 remove_remarks_from_this_and_parent();
298 grandfather->
leftlen += brothersLen;
299 grandfather->
leftson = brother;
303 grandfather->
rightlen += brothersLen;
306 brother->
father = grandfather;
307 if (!grandfather->
father) {
343 if (!
father) error =
"Can't move the root of the tree";
344 else if (!new_brother->
father) error =
"Can't move to the root of the tree";
345 else if (new_brother->
father ==
father) error =
"Already there";
346 else if (new_brother ==
father) error =
"Already there";
347 else if (!
father->
father) error =
"Can't move son of root";
348 else if (new_brother->
is_inside(
this)) error =
"Can't move a subtree into itself";
367 remove_remarks_from_this_and_parent();
369 new_brother->remove_remarks_from_this_and_parent();
386 if (!grandfather->
father) {
387 grandfather->remove_root_remark();
401 AP_tree *brother_father = new_brother->get_father();
404 if (brother_father->
leftson == new_brother) {
405 laenge = brother_father->
leftlen;
406 laenge -= brother_father->
leftlen = laenge * rel_pos;
407 brother_father->
leftson = new_tree;
411 laenge -= brother_father->
rightlen = laenge * rel_pos;
412 brother_father->
rightson = new_tree;
416 new_brother->
father = new_tree;
418 new_tree->
father = brother_father;
422 new_root->remove_root_remark();
447 void AP_tree::load_node_info() {
459 get_leftson()->load_subtree_info();
460 get_rightson()->load_subtree_info();
465 #if defined(DEVEL_RALF)
466 #define DEBUG_tree_write_byte
478 #if defined(DEBUG_tree_write_byte)
479 printf(
"[tree_write_byte] deleting db entry %p\n", gbd);
480 #endif // DEBUG_tree_write_byte
488 #if defined(DEBUG_tree_write_byte)
489 printf(
"[tree_write_byte] created node-container %p\n", node->
gb_node);
490 #endif // DEBUG_tree_write_byte
495 #if defined(DEBUG_tree_write_byte)
496 printf(
"[tree_write_byte] created db entry %p\n", gbd);
497 #endif // DEBUG_tree_write_byte
525 error = get_leftson()->tree_write_tree_rek(gb_tree);
526 if (!error) error = get_rightson()->tree_write_tree_rek(gb_tree);
541 error = get_root_node()->tree_write_tree_rek(
get_gb_tree());
549 if (gb_tree_exists) {
550 error =
"tree already exists";
557 if (gb_tree_exists) {
578 if (ofKeeledGroups) {
580 get_father()->swap_node_info(other->get_father(),
false);
584 else if (
this == other) {
585 gb_assert(keeledOver && other->keeledOver);
586 inverseLeft = !inverseLeft;
591 std::swap(keeledOver, other->keeledOver);
601 AP_tree *dest_parent = dest->get_father();
604 error =
GBS_global_string(
"cannot move group '%s' (would create partial overlap with '%s')", parent->
name, dest_parent->
name);
607 error =
"invalid move of keeled group to tree-root";
611 parent->load_node_info();
612 dest_parent->load_node_info();
623 dest->load_node_info();
635 if (!src_normal && !src_keeled) {
636 error =
"Please select a valid source group";
644 error = swap_group_with(dest,
true);
645 if (error && src_normal) {
647 error = swap_group_with(dest,
false);
648 if (error) error =
GBS_global_string(
"Neighter keeled nor normal group can be moved that way:\n%s\n%s", error1, error);
652 error = swap_group_with(dest,
false);
671 #if defined(ASSERTION_USED) || defined(UNIT_TESTS)
688 static void default_shader_never_shades() {
ap_assert(0); }
712 inline void AP_tree::recalc_hidden() {
717 inline void AP_tree::recalc_view_sum(
const group_scaling& gscaling) {
726 const unsigned MIN_GROUP_SIZE = 2
U;
727 unsigned squared_size = unsigned(pow(
double(
gr.
leaf_sum), gscaling.
pow) * gscaling.
linear);
733 gr.
view_sum = get_leftson()->gr.view_sum + get_rightson()->gr.view_sum;
746 error = get_leftson()->update_and_write_folding(gb_tree, gscaling);
747 if (!error) error = get_rightson()->update_and_write_folding(gb_tree, gscaling);
749 recalc_view_sum(gscaling);
773 GB_ERROR error = update_and_write_folding(gb_tree, *gscaling);
780 fath->recalc_view_sum(*gscaling);
781 fath = fath->get_father();
821 recalc_view_sum(gscaling);
845 : get_leftson()->count_leafs() + get_rightson()->count_leafs();
866 int l = get_leftson()->colorize(hashptr);
867 int r = get_rightson()->colorize(hashptr);
879 #if defined(DEVEL_RALF) && 0
880 fputs(
" - AP_tree::compute_tree() called\n", stderr);
889 update_subtree_information<ShadedValue>(*gscaling);
896 get_root_node()->load_subtree_info();
923 void AP_tree::buildLeafList_rek(
AP_tree **list,
long& num) {
926 get_leftson()->buildLeafList_rek(list, num);
927 get_rightson()->buildLeafList_rek(list, num);
941 buildLeafList_rek(list, count);
958 for (
long i=0; i<count; i++) {
959 bool removeNode =
false;
980 if (!get_root_node()) {
994 template <
typename T>
999 char *mean_min_max_impl()
const;
1000 char *mean_min_max_percent_impl()
const;
1003 const char *set_buf(
char *content)
const { freeset(buf, content);
return buf; }
1036 const char *
mean_min_max()
const {
return count ? set_buf(mean_min_max_impl()) :
"<not available>"; }
1037 const char *
mean_min_max_percent()
const {
return count ? set_buf(mean_min_max_percent_impl()) :
"<not available>"; }
1049 count += other.count;
1054 template<
typename T>
1058 template<
typename T>
1074 double min_rel_diff;
1075 double min_abs_diff;
1086 double perform_marking(
AP_tree *at,
bool& marked) {
1106 double max = perform_marking(at->get_leftson(), marked_left) + at->
leftlen;
1107 double min = perform_marking(at->get_rightson(), marked_right) + at->
rightlen;
1109 bool max_is_left =
true;
1112 max_is_left =
false;
1115 double abs_diff = max-
min;
1118 double rel_diff = (max == 0.0) ? 0.0 : abs_diff/
max;
1121 if (abs_diff>min_abs_diff && rel_diff>min_rel_diff) {
1129 if (!marked_right) {
1141 marked = marked_left||marked_right;
1149 "Mean absolute diff: %s\n"
1150 "Mean relative diff: %s",
1157 : min_rel_diff(min_rel_diff_),
1158 min_abs_diff(min_abs_diff_),
1164 perform_marking(root, dummy);
1168 char *diffs_all = meanDiffs(absdiff, reldiff);
1169 char *diffs_marked = meanDiffs(absdiff_marked, reldiff_marked);
1173 int zeroleafs = leafs-nonzeroleafs;
1174 int zeroedges = multifurcs+zeroleafs;
1175 int realedges = edges-zeroedges;
1176 int furcs = nodes-leafs;
1177 int realfurcs = furcs-multifurcs;
1188 "Unrooted tree contains %*i furcations,\n"
1189 " of which %*i are multifurcations,\n"
1190 " i.e. %*i are \"real\" furcations.\n"
1192 "Unrooted tree contains %*i edges,\n"
1193 " of which %*i have a length > zero.\n"
1197 "%i subtrees have been marked:\n"
1201 node_digits, multifurcs,
1202 node_digits, realfurcs,
1204 node_digits, realedges,
1232 int depthInc = atLen == 0.0 ? 0 : 1;
1261 : min_depth(min_depth_),
1262 min_rootdist(min_rootdist_),
1272 double balanced_depth = log10(leafs) / log10(2);
1275 "The optimal mean depth of a tree with %i leafs\n"
1280 "mean distance: %s\n"
1282 "%i species (%.2f%%) have been marked:\n"
1284 "mean distance: %s\n"
1290 marked, marked/double(leafs)*100.0,
1309 DepthMarker dmarker(
this, min_depth, min_rootdist);
1320 Distance min, max, mean;
1333 "Mean mean distance: %s\n"
1334 "Mean min. distance: %s\n"
1335 "Mean max. distance: %s",
1344 typedef map<AP_tree*, Distance> DistanceMap;
1346 DistanceMap downdist;
1363 calc_downdist(at->get_leftson(), at->
leftlen) +
1364 calc_downdist(at->get_rightson(), at->
rightlen) +
1367 return downdist[at];
1377 ap_assert(updist.find(father) != updist.end());
1378 ap_assert(downdist.find(brother) != downdist.end());
1380 updist[at] = updist[
father] + downdist[brother] + len;
1383 ap_assert(downdist.find(brother) != downdist.end());
1385 updist[at] = downdist[brother]+len;
1389 calc_updist(at->get_leftson(), at->
leftlen);
1390 calc_updist(at->get_rightson(), at->
rightlen);
1401 void calc_distance_stats(
AP_tree *at) {
1403 ap_assert(updist.find(at) != updist.end());
1405 const Distance& upwards = updist[at];
1415 calc_distance_stats(at->get_leftson());
1416 calc_distance_stats(at->get_rightson());
1423 : distSum(root->sum_child_lengths()),
1424 progress(
"Analysing distances", root->count_leafs()*3L)
1426 calc_downdist(root->get_leftson(), root->
leftlen);
1427 calc_downdist(root->get_rightson(), root->
rightlen);
1429 calc_updist(root->get_leftson(), root->
leftlen);
1430 calc_updist(root->get_rightson(), root->
rightlen);
1432 calc_distance_stats(root);
1436 char *alldists_report = alldists.
get_report();
1437 char *markeddists_report = markeddists.
get_report();
1440 "Overall in-tree-distance (ITD): %.3f\n"
1441 " per-species-distance (PSD): %.6f\n"
1443 "Distance statistic for %i leafs:\n"
1444 "(each leaf to all other leafs)\n"
1448 "Distance statistic for %i marked leafs:\n"
1454 markeddists.
get_count(), markeddists_report);
1456 free(markeddists_report);
1457 free(alldists_report);
1472 int lSons =
ap_mark_degenerated(at->get_leftson(), degeneration_factor, max_degeneration);
1473 int rSons =
ap_mark_degenerated(at->get_rightson(), degeneration_factor, max_degeneration);
1475 double this_degeneration = 0;
1478 this_degeneration = rSons/double(lSons);
1479 if (this_degeneration >= degeneration_factor) {
1484 else if (rSons<lSons) {
1485 this_degeneration = lSons/double(rSons);
1486 if (this_degeneration >= degeneration_factor) {
1491 if (this_degeneration >= max_degeneration) {
1492 max_degeneration = this_degeneration;
1503 double max_degeneration = 0;
1505 return max_degeneration;
1551 double diff = fabs(bl - br);
1552 if (l < diff * 1.1) l = diff * 1.1;
1553 double go = (bl + br + l) * .5;
1569 relinker(node->
gb_node, node->
name, organism_hash);
1587 void AP_tree::reset_child_angles() {
1590 get_leftson()->reset_child_angles();
1591 get_rightson()->reset_child_angles();
1595 void AP_tree::reset_child_linewidths() {
1598 get_leftson()->reset_child_linewidths();
1599 get_rightson()->reset_child_linewidths();
1606 get_leftson()->set_linewidth_recursive(width);
1607 get_rightson()->set_linewidth_recursive(width);
1611 void AP_tree::reset_child_layout() {
1616 get_leftson()->reset_child_layout();
1617 get_rightson()->reset_child_layout();
1624 get_leftson()->reset_subtree_spreads();
1625 get_rightson()->reset_subtree_spreads();
1630 if (!
is_leaf()) reset_child_angles();
1634 if (!
is_leaf()) reset_child_linewidths();
1639 if (!
is_leaf()) reset_child_layout();
1644 if (!
father)
return false;
1645 return get_father()->is_inside_folded_group();
GBDATA * get_gb_main() const
ValueCounter< double > distances
void reset_subtree_angles()
DECLARE_ASSIGNMENT_OPERATOR(ValueCounter< T >)
long remove_leafs(AWT_RemoveType awt_remove_type)
void change_root(TreeNode *old, TreeNode *newroot) FINAL_OVERRIDE
void buildLeafList(AP_tree **&list, long &num)
ValueCounter< int > depths_marked
void recompute_and_write_folding()
int tree_read_byte(GBDATA *tree, const char *key, int init)
GB_ERROR move_group_to(AP_tree *new_group) __ATTR__USERESULT
static void relink_tree_rek(AP_tree *node, void(*relinker)(GBDATA *&ref_gb_node, char *&ref_name, GB_HASH *organism_hash), GB_HASH *organism_hash)
#define implicated(hypothesis, conclusion)
static GB_ERROR tree_write_byte(GBDATA *gb_tree, AP_tree *node, short i, const char *key, int init)
GB_ERROR GBT_link_tree(TreeNode *tree, GBDATA *gb_main, bool show_status)
long GBS_write_hash(GB_HASH *hs, const char *key, long val)
CONSTEXPR_INLINE int nodes_2_edges(int nodes)
const TreeNode * get_root_node() const
void perform_marking(AP_tree *at, AP_FLOAT atLen)
bool has_group_info() const
virtual void moveNextTo(AP_tree *new_brother, AP_FLOAT rel_pos)
void unlink_from_father()
char * init(AP_filter *fil)
DepthMarker(AP_tree *root, int min_depth_, double min_rootdist_)
void set_node_deleted_callback(AP_nodeDelCb cb, void *cd)
GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error)
long get_timestamp() const
POS_TREE1 * get_father() const
bool is_folded_group() const
TreeRoot * get_tree_root() const
const char * mean_min_max() const
char * get_report() const
const char * analyse_distances()
GB_ERROR tree_write_tree_rek(GBDATA *gb_tree)
const char * GBS_global_string(const char *templat,...)
ShadedValue calc_shaded_leaf_GC(GBDATA *) const OVERRIDE
void(* AP_nodeDelCb)(void *cd, AP_tree *del)
void count_distance(const Distance &d)
AP_tree_root(AliView *aliView, AP_sequence *seq_proto, bool add_delete_callbacks, const group_scaling *scaling)
static GB_ERROR tree_write_float(GBDATA *gb_tree, AP_tree *node, float f, const char *key, float init)
int colorize(GB_HASH *hashptr)
void GBS_free_hash(GB_HASH *hs)
const char * mark_long_branches(double min_rel_diff, double min_abs_diff, double &found_max_abs_diff)
GB_ERROR cantMoveNextTo(AP_tree *new_brother)
virtual int to_GC(const ShadedValue &val) const =0
int get_max_depth() const
ValueCounter< double > distances_marked
GB_ERROR GB_push_transaction(GBDATA *gbd)
static double ap_just_tree_rek(AP_tree *at)
void reset_subtree_linewidths()
virtual void insert(AP_tree *new_brother)
int to_GC(const ShadedValue &) const OVERRIDE
#define DOWNCAST(totype, expr)
GB_ERROR GB_delete(GBDATA *&source)
unsigned count_leafs() const
const char * get_report() const
virtual AP_tree * REMOVE()
#define ASSERT_VALID_TREE_OR_NULL(tree)
bool has_valid_root_remarks() const
virtual ShadedValue calc_shaded_inner_GC(const ShadedValue &left, float left_ratio, const ShadedValue &right) const =0
void set_linewidth_recursive(int width)
POS_TREE1 * get_father() const
CONSTEXPR_INLINE int leafs_2_nodes(int leafs, TreeModel model)
GB_ERROR GB_await_error()
static int diff(int v1, int v2, int v3, int v4, int st, int en)
GBDATA * GB_create_container(GBDATA *father, const char *key)
void compute_tree() FINAL_OVERRIDE
bool is_son_of_root() const
GBDATA * get_gb_tree() const
float tree_read_float(GBDATA *tree, const char *key, float init)
bool isSet() const
test if SmartPtr is not NULp
GBDATA * GB_create(GBDATA *father, const char *key, GB_TYPES type)
virtual TreeNode * makeNode() const =0
void swap_node_info(TreeNode *other, bool ofKeeledGroups)
virtual GB_ERROR saveToDB() __ATTR__USERESULT
ValueCounter< double > Distance
virtual GB_ERROR loadFromDB(const char *name) __ATTR__USERESULT
ValueCounter(const ValueCounter< T > &other)
GB_ERROR GBT_write_tree(GBDATA *gb_main, const char *tree_name, TreeNode *tree)
double get_max_rootdist() const
static void error(const char *msg)
GBDATA * GB_get_root(GBDATA *gbd)
bool is_species_updated()
const char * mean_min_max_percent() const
bool is_root_node() const
void relink_tree(GBDATA *gb_main, void(*relinker)(GBDATA *&ref_gb_node, char *&ref_name, GB_HASH *organism_hash), GB_HASH *organism_hash)
CONSTEXPR_INLINE_Cxx14 void swap(unsigned char &c1, unsigned char &c2)
GB_ERROR loadFromDB(const char *name) FINAL_OVERRIDE
bool is_keeled_group() const
float GB_read_float(GBDATA *gbd)
GB_write_int const char GB_write_autoconv_string WRITE_SKELETON(write_pointer, GBDATA *,"%p", GB_write_pointer) char *AW_awa if)(!gb_var) return strdup("")
void reset_subtree_spreads()
void update_settings() OVERRIDE
void(* AP_rootChangedCb)(void *cd, AP_tree *old, AP_tree *newroot)
int GB_read_flag(GBDATA *gbd)
void reset_both_child_linewidths()
bool is_inside_folded_group() const
bool has_been_set() const
size_t get_filtered_length() const
double mark_degenerated_branches(double degeneration_factor)
GB_ERROR saveToDB() OVERRIDE
EdgeDistances(AP_tree *root)
static void ap_tree_node_deleted(GBDATA *, AP_tree *tree)
virtual void update_settings()=0
virtual void initial_insert(AP_tree *new_brother, AP_tree_root *troot)
fputs(TRACE_PREFIX, stderr)
static int ap_mark_duplicates_rek(AP_tree *at, GB_HASH *seen_species)
const char * get_report() const
ValueCounter< T > operator+(const ValueCounter< T > &c1, const ValueCounter< T > &c2)
void GB_write_flag(GBDATA *gbd, long flag)
void reset_subtree_layout()
bool has_correct_mark_flags() const
bool is_inside(const TreeNode *subtree) const
bool AW_color_groups_active()
void GB_touch(GBDATA *gbd)
int GB_read_byte(GBDATA *gbd)
void justify_branch_lenghs(GBDATA *gb_main)
GB_ERROR GBT_write_float(GBDATA *gb_container, const char *fieldpath, float content)
TreeNode * makeNode() const OVERRIDE
void fixKeeledOrientation()
GB_ERROR GB_write_byte(GBDATA *gbd, int i)
virtual ShadedValue calc_shaded_leaf_GC(GBDATA *gb_node) const =0
void GB_remove_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback &dbcb)
ValueCounter< T > & operator+=(const T &inc)
void set_tree_root(TreeRoot *new_root)
static ARB_init_perl_interface init
void aw_message(const char *msg)
double get_max_abs_diff() const
GBDATA * find_group_name_entry(TreeNode *node)
long GB_read_clock(GBDATA *gbd)
ValueCounter< int > depths
const group_scaling * get_group_scaling() const
const char * mark_deep_leafs(int min_depth, double min_rootdist, int &found_max_depth, double &found_max_rootdist)
LongBranchMarker(AP_tree *root, double min_rel_diff_, double min_abs_diff_)
GBT_LEN get_branchlength() const
GBDATA * GBT_find_tree(GBDATA *gb_main, const char *tree_name)
GB_transaction ta(gb_var)
#define ASSERT_VALID_TREE(tree)
void set_gb_tree_and_name(GBDATA *gbTree, const char *name)
virtual void change_root(TreeNode *old, TreeNode *newroot)
static void set_tree_shader(AP_TreeShader *new_shader)
void set_linewidth(int width)
int calc_leaf_GC(GBDATA *gb_node, bool is_marked) const
int calc_inner_GC(int left_gc, int right_gc) const
bool use_position(size_t pos) const
void set_root_changed_callback(AP_rootChangedCb cb, void *cd)
const char * get_report() const
GBDATA * get_gb_main(DbSel db)
GB_ERROR relink() __ATTR__USERESULT
ShadedValue calc_shaded_inner_GC(const ShadedValue &, float, const ShadedValue &) const OVERRIDE
void set_gb_tree(GBDATA *gbTree)
bool is_normal_group() const
void reset_both_child_angles()
long GBS_read_hash(const GB_HASH *hs, const char *key)
void destroyNode(TreeNode *node) const OVERRIDE
GBDATA * GB_entry(GBDATA *father, const char *key)
void inform_about_delete(AP_tree *old)
char * GBS_global_string_copy(const char *templat,...)
void reset_child_spread()
virtual AP_UPDATE_FLAGS check_update()
GB_HASH * GBS_create_hash(long estimated_elements, GB_CASE case_sens)
static int ap_mark_degenerated(AP_tree *at, double degeneration_factor, double &max_degeneration)
GBDATA * GBT_get_species_data(GBDATA *gb_main)