// ============================================================ //
//                                                              //
//   File      : TreeTools.cxx                                  //
//   Purpose   :                                                //
//                                                              //
//   Institute of Microbiology (Technical University Munich)    //
//   www.arb-home.de                                            //
//                                                              //
// ============================================================ //

#include "TreeRead.h"
#include <TreeNode.h>

void TREE_scale(TreeNode *tree, double length_scale, double bootstrap_scale, WarningConsumer warn) {
    if (tree->leftson) {
        if (is_marked_as_default_len(tree->leftlen)) tree->leftlen  = DEFAULT_BRANCH_LENGTH;
        else                                         tree->leftlen *= length_scale;

        TREE_scale(tree->get_leftson(), length_scale, bootstrap_scale, warn);
    }
    if (tree->rightson) {
        if (is_marked_as_default_len(tree->rightlen)) tree->rightlen  = DEFAULT_BRANCH_LENGTH;
        else                                          tree->rightlen *= length_scale;

        TREE_scale(tree->get_rightson(), length_scale, bootstrap_scale, warn);
    }

    if (!tree->is_leaf() && !tree->is_root_node()) {
        bool skip = tree->is_son_of_root() && tree->is_rightson() && !tree->get_brother()->is_leaf(); // modify root edge only once
        if (!skip) {
            double bootstrap;
            switch (tree->parse_bootstrap(bootstrap, warn)) {
                case REMARK_BOOTSTRAP: tree->set_bootstrap(bootstrap*bootstrap_scale); break;
                case REMARK_OTHER:     break; // do not touch custom node remarks
                case REMARK_NONE:      break; // does not scale implicit 100% bootstrap "value" (intentionally)
            }
        }
    }
}


