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);
224 if (root_changed_cb) {
245 if (node_deleted_cb) node_deleted_cb(node_deleted_cd, del);
249 root_changed_cb =
cb;
250 root_changed_cd = cd;
254 node_deleted_cb =
cb;
255 node_deleted_cd = cd;
286 remove_remarks_from_this_and_parent();
296 grandfather->
leftlen += brothersLen;
297 grandfather->
leftson = brother;
301 grandfather->
rightlen += brothersLen;
304 brother->
father = grandfather;
305 if (!grandfather->
father) {
341 if (!
father) error =
"Can't move the root of the tree";
342 else if (!new_brother->
father) error =
"Can't move to the root of the tree";
343 else if (new_brother->
father ==
father) error =
"Already there";
344 else if (new_brother ==
father) error =
"Already there";
345 else if (!
father->
father) error =
"Can't move son of root";
346 else if (new_brother->
is_inside(
this)) error =
"Can't move a subtree into itself";
365 remove_remarks_from_this_and_parent();
367 new_brother->remove_remarks_from_this_and_parent();
384 if (!grandfather->
father) {
385 grandfather->remove_root_remark();
399 AP_tree *brother_father = new_brother->get_father();
402 if (brother_father->
leftson == new_brother) {
403 laenge = brother_father->
leftlen;
404 laenge -= brother_father->
leftlen = laenge * rel_pos;
405 brother_father->
leftson = new_tree;
409 laenge -= brother_father->
rightlen = laenge * rel_pos;
410 brother_father->
rightson = new_tree;
414 new_brother->
father = new_tree;
416 new_tree->
father = brother_father;
420 new_root->remove_root_remark();
445 void AP_tree::load_node_info() {
457 get_leftson()->load_subtree_info();
458 get_rightson()->load_subtree_info();
463 #if defined(DEVEL_RALF)
464 #define DEBUG_tree_write_byte
476 #if defined(DEBUG_tree_write_byte)
477 printf(
"[tree_write_byte] deleting db entry %p\n", gbd);
478 #endif // DEBUG_tree_write_byte
486 #if defined(DEBUG_tree_write_byte)
487 printf(
"[tree_write_byte] created node-container %p\n", node->
gb_node);
488 #endif // DEBUG_tree_write_byte
493 #if defined(DEBUG_tree_write_byte)
494 printf(
"[tree_write_byte] created db entry %p\n", gbd);
495 #endif // DEBUG_tree_write_byte
523 error = get_leftson()->tree_write_tree_rek(gb_tree);
524 if (!error) error = get_rightson()->tree_write_tree_rek(gb_tree);
539 error = get_root_node()->tree_write_tree_rek(
get_gb_tree());
547 if (gb_tree_exists) {
548 error =
"tree already exists";
555 if (gb_tree_exists) {
576 if (ofKeeledGroups) {
578 get_father()->swap_node_info(other->get_father(),
false);
582 else if (
this == other) {
583 gb_assert(keeledOver && other->keeledOver);
584 inverseLeft = !inverseLeft;
589 std::swap(keeledOver, other->keeledOver);
599 AP_tree *dest_parent = dest->get_father();
602 error =
GBS_global_string(
"cannot move group '%s' (would create partial overlap with '%s')", parent->
name, dest_parent->
name);
605 error =
"invalid move of keeled group to tree-root";
609 parent->load_node_info();
610 dest_parent->load_node_info();
621 dest->load_node_info();
633 if (!src_normal && !src_keeled) {
634 error =
"Please select a valid source group";
642 error = swap_group_with(dest,
true);
643 if (error && src_normal) {
645 error = swap_group_with(dest,
false);
646 if (error) error =
GBS_global_string(
"Neighter keeled nor normal group can be moved that way:\n%s\n%s", error1, error);
650 error = swap_group_with(dest,
false);
669 #if defined(ASSERTION_USED) || defined(UNIT_TESTS)
686 static void default_shader_never_shades() {
ap_assert(0); }
710 inline void AP_tree::recalc_hidden() {
715 inline void AP_tree::recalc_view_sum(
const group_scaling& gscaling) {
724 const unsigned MIN_GROUP_SIZE = 2
U;
725 unsigned squared_size = unsigned(pow(
double(
gr.
leaf_sum), gscaling.
pow) * gscaling.
linear);
731 gr.
view_sum = get_leftson()->gr.view_sum + get_rightson()->gr.view_sum;
744 error = get_leftson()->update_and_write_folding(gb_tree, gscaling);
745 if (!error) error = get_rightson()->update_and_write_folding(gb_tree, gscaling);
747 recalc_view_sum(gscaling);
771 GB_ERROR error = update_and_write_folding(gb_tree, *gscaling);
778 fath->recalc_view_sum(*gscaling);
779 fath = fath->get_father();
819 recalc_view_sum(gscaling);
843 : get_leftson()->count_leafs() + get_rightson()->count_leafs();
864 int l = get_leftson()->colorize(hashptr);
865 int r = get_rightson()->colorize(hashptr);
877 #if defined(DEVEL_RALF) && 0
878 fputs(
" - AP_tree::compute_tree() called\n", stderr);
887 update_subtree_information<ShadedValue>(*gscaling);
894 get_root_node()->load_subtree_info();
921 void AP_tree::buildLeafList_rek(
AP_tree **list,
long& num) {
924 get_leftson()->buildLeafList_rek(list, num);
925 get_rightson()->buildLeafList_rek(list, num);
939 buildLeafList_rek(list, count);
956 for (
long i=0; i<count; i++) {
957 bool removeNode =
false;
978 if (!get_root_node()) {
992 template <
typename T>
997 char *mean_min_max_impl()
const;
998 char *mean_min_max_percent_impl()
const;
1001 const char *set_buf(
char *content)
const { freeset(buf, content);
return buf; }
1034 const char *
mean_min_max()
const {
return count ? set_buf(mean_min_max_impl()) :
"<not available>"; }
1035 const char *
mean_min_max_percent()
const {
return count ? set_buf(mean_min_max_percent_impl()) :
"<not available>"; }
1047 count += other.count;
1052 template<
typename T>
1056 template<
typename T>
1072 double min_rel_diff;
1073 double min_abs_diff;
1084 double perform_marking(
AP_tree *at,
bool& marked) {
1104 double max = perform_marking(at->get_leftson(), marked_left) + at->
leftlen;
1105 double min = perform_marking(at->get_rightson(), marked_right) + at->
rightlen;
1107 bool max_is_left =
true;
1110 max_is_left =
false;
1113 double abs_diff = max-
min;
1116 double rel_diff = (max == 0.0) ? 0.0 : abs_diff/
max;
1119 if (abs_diff>min_abs_diff && rel_diff>min_rel_diff) {
1127 if (!marked_right) {
1139 marked = marked_left||marked_right;
1147 "Mean absolute diff: %s\n"
1148 "Mean relative diff: %s",
1155 : min_rel_diff(min_rel_diff_),
1156 min_abs_diff(min_abs_diff_),
1162 perform_marking(root, dummy);
1166 char *diffs_all = meanDiffs(absdiff, reldiff);
1167 char *diffs_marked = meanDiffs(absdiff_marked, reldiff_marked);
1171 int zeroleafs = leafs-nonzeroleafs;
1172 int zeroedges = multifurcs+zeroleafs;
1173 int realedges = edges-zeroedges;
1174 int furcs = nodes-leafs;
1175 int realfurcs = furcs-multifurcs;
1186 "Unrooted tree contains %*i furcations,\n"
1187 " of which %*i are multifurcations,\n"
1188 " i.e. %*i are \"real\" furcations.\n"
1190 "Unrooted tree contains %*i edges,\n"
1191 " of which %*i have a length > zero.\n"
1195 "%i subtrees have been marked:\n"
1199 node_digits, multifurcs,
1200 node_digits, realfurcs,
1202 node_digits, realedges,
1230 int depthInc = atLen == 0.0 ? 0 : 1;
1259 : min_depth(min_depth_),
1260 min_rootdist(min_rootdist_),
1270 double balanced_depth = log10(leafs) / log10(2);
1273 "The optimal mean depth of a tree with %i leafs\n"
1278 "mean distance: %s\n"
1280 "%i species (%.2f%%) have been marked:\n"
1282 "mean distance: %s\n"
1288 marked, marked/double(leafs)*100.0,
1307 DepthMarker dmarker(
this, min_depth, min_rootdist);
1318 Distance min, max, mean;
1331 "Mean mean distance: %s\n"
1332 "Mean min. distance: %s\n"
1333 "Mean max. distance: %s",
1342 typedef map<AP_tree*, Distance> DistanceMap;
1344 DistanceMap downdist;
1361 calc_downdist(at->get_leftson(), at->
leftlen) +
1362 calc_downdist(at->get_rightson(), at->
rightlen) +
1365 return downdist[at];
1375 ap_assert(updist.find(father) != updist.end());
1376 ap_assert(downdist.find(brother) != downdist.end());
1378 updist[at] = updist[
father] + downdist[brother] + len;
1381 ap_assert(downdist.find(brother) != downdist.end());
1383 updist[at] = downdist[brother]+len;
1387 calc_updist(at->get_leftson(), at->
leftlen);
1388 calc_updist(at->get_rightson(), at->
rightlen);
1399 void calc_distance_stats(
AP_tree *at) {
1401 ap_assert(updist.find(at) != updist.end());
1403 const Distance& upwards = updist[at];
1413 calc_distance_stats(at->get_leftson());
1414 calc_distance_stats(at->get_rightson());
1421 : distSum(root->sum_child_lengths()),
1422 progress(
"Analysing distances", root->count_leafs()*3L)
1424 calc_downdist(root->get_leftson(), root->
leftlen);
1425 calc_downdist(root->get_rightson(), root->
rightlen);
1427 calc_updist(root->get_leftson(), root->
leftlen);
1428 calc_updist(root->get_rightson(), root->
rightlen);
1430 calc_distance_stats(root);
1434 char *alldists_report = alldists.
get_report();
1435 char *markeddists_report = markeddists.
get_report();
1438 "Overall in-tree-distance (ITD): %.3f\n"
1439 " per-species-distance (PSD): %.6f\n"
1441 "Distance statistic for %i leafs:\n"
1442 "(each leaf to all other leafs)\n"
1446 "Distance statistic for %i marked leafs:\n"
1452 markeddists.
get_count(), markeddists_report);
1454 free(markeddists_report);
1455 free(alldists_report);
1470 int lSons =
ap_mark_degenerated(at->get_leftson(), degeneration_factor, max_degeneration);
1471 int rSons =
ap_mark_degenerated(at->get_rightson(), degeneration_factor, max_degeneration);
1473 double this_degeneration = 0;
1476 this_degeneration = rSons/double(lSons);
1477 if (this_degeneration >= degeneration_factor) {
1482 else if (rSons<lSons) {
1483 this_degeneration = lSons/double(rSons);
1484 if (this_degeneration >= degeneration_factor) {
1489 if (this_degeneration >= max_degeneration) {
1490 max_degeneration = this_degeneration;
1501 double max_degeneration = 0;
1503 return max_degeneration;
1549 double diff = fabs(bl - br);
1550 if (l < diff * 1.1) l = diff * 1.1;
1551 double go = (bl + br + l) * .5;
1567 relinker(node->
gb_node, node->
name, organism_hash);
1585 void AP_tree::reset_child_angles() {
1588 get_leftson()->reset_child_angles();
1589 get_rightson()->reset_child_angles();
1593 void AP_tree::reset_child_linewidths() {
1596 get_leftson()->reset_child_linewidths();
1597 get_rightson()->reset_child_linewidths();
1604 get_leftson()->set_linewidth_recursive(width);
1605 get_rightson()->set_linewidth_recursive(width);
1609 void AP_tree::reset_child_layout() {
1614 get_leftson()->reset_child_layout();
1615 get_rightson()->reset_child_layout();
1622 get_leftson()->reset_subtree_spreads();
1623 get_rightson()->reset_subtree_spreads();
1628 if (!
is_leaf()) reset_child_angles();
1632 if (!
is_leaf()) reset_child_linewidths();
1637 if (!
is_leaf()) reset_child_layout();
1642 if (!
father)
return false;
1643 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)
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 GBT_link_tree(TreeNode *tree, GBDATA *gb_main, bool show_status, int *zombies, int *duplicates)
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)