Skip to content

Commit

Permalink
fixed support setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin Molloy committed Mar 24, 2024
1 parent 9975593 commit b5bfe82
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 33 deletions.
8 changes: 6 additions & 2 deletions src/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ int Instance::parse(int argc, char **argv) {
support_low = 0.0;
support_high = 1.0;
support_default = 1.0;
support_threshold = 0.0;
contract = false;
}

// Print options for tree building
Expand Down Expand Up @@ -743,8 +745,10 @@ void Instance::refine_trees() {
}

void Instance::prepare_trees() {
for (Tree *t : input)
t->prepare(weight_mode, support_low, support_high, support_threshold);
for (Tree *t : input) {
t->prepare(weight_mode, support_low, support_high, contract, support_threshold);
// std::cout << t->to_string() << std::endl;
}
}

void Instance::prepare_root_taxa() {
Expand Down
64 changes: 41 additions & 23 deletions src/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ size_t Tree::refine() {
return count;
}

void Tree::prepare(std::string weight_mode, weight_t low, weight_t high, weight_t threshold) {
void Tree::prepare(std::string weight_mode, weight_t low, weight_t high, bool contract, weight_t threshold) {
//std::cout << display_tree(root) << std::endl;
prepare_tree(root, weight_mode, low, high, threshold);
prepare_tree(root, weight_mode, low, high, contract, threshold);
}

index_t Tree::size() {
Expand Down Expand Up @@ -208,9 +208,8 @@ size_t Tree::refine_tree(Node *root) {
while (root->children.size() > 2) {
index_t i = rand() % root->children.size(), j = i;
while (j == i) j = rand() % root->children.size();
Node *new_root = new Node(pseudonym());
new_root->support = this->support_low; // polytomies always set to min support value
//Node *new_root = new Node(pseudonym(), true);
Node *new_root = new Node(pseudonym(), true);
new_root->support = this->support_low;
total ++;
new_root->children.push_back(root->children[i]);
new_root->children.push_back(root->children[j]);
Expand All @@ -224,38 +223,57 @@ size_t Tree::refine_tree(Node *root) {
return total;
}

void Tree::prepare_tree(Node *root, std::string weight_mode, weight_t low, weight_t high, weight_t threshold) {
void Tree::prepare_tree(Node *root, std::string weight_mode, weight_t low, weight_t high, bool contract, weight_t threshold) {
//assert(root->children.size() == 0 || root->children.size() == 2);

weight_t s = root->support;

// Map support to 0 and 1 interval
if (s < low || s > high) s = low;
s = (s - low) / (high - low);

// Contract low support branches by setting support to 0
if (s < threshold) s = 0;

// If not weighting by branch support, set support values to 1
// do for fast mode as well because will be used for support estimation
// this is already done in instance.cpp
if (weight_mode == "n" || weight_mode == "f" || weight_mode == "l")
if (s > 0) s = 1;
bool isfake = root->isfake;

if (weight_mode == "f") {
// No other support value is possible
s = 1.0;
} else {
if (weight_mode == "h" || weight_mode == "s" || contract) {
// Map support to 0 and 1 interval
if (s < low || s > high) s = low;
s = (s - low) / (high - low);
}
if (weight_mode == "n" || weight_mode == "l") {
if (isfake) {
s = 0.0;
} else if (contract) {
if (s < threshold) s = 0.0;
else s = 1.0;
} else {
s = 1.0;
}
}
else {
if (isfake) {
s = 0.0;
} else if (contract) {
if (s < threshold) s = 0.0;
}
}
}

// Save these values
root->support = s;
root->support_[0] = 1 - s;
root->support_[1] = 1;

// Handle length values
if (weight_mode == "h" || weight_mode == "l")
if (weight_mode == "h" || weight_mode == "l") {
root->length_ = exp(- root->length);
else
root->length_ = 1;
}
else {
root->length = 0.0;
root->length_ = 1.0;
}

// Continue
for (Node *child : root->children)
prepare_tree(child, weight_mode, low, high, threshold);
prepare_tree(child, weight_mode, low, high, contract, threshold);
}

void Tree::clear_states(Node *root) {
Expand Down
8 changes: 4 additions & 4 deletions src/tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Node {
friend class SpeciesTree;
public:
Node(index_t index);
//Node(index_t index, bool isfake);
Node(index_t index, bool isfake);
~Node();
void new_states(index_t size);
void delete_states();
Expand All @@ -26,7 +26,7 @@ class Node {
std::vector<Node *> children;
index_t index, size, depth;
weight_t s1, s2, support, length;
//bool isfake;
bool isfake;

weight_t /* **doublet, */ *singlet;
// std::map<index_t, weight_t> doublet;
Expand Down Expand Up @@ -56,7 +56,7 @@ class Tree {
std::string to_string();
std::string to_string_basic();
size_t refine();
void prepare(std::string weight_mode, weight_t low, weight_t high, weight_t threshold);
void prepare(std::string weight_mode, weight_t low, weight_t high, bool contract, weight_t threshold);
index_t size();
std::unordered_map<index_t, index_t> &get_indices();
weight_t ***build_graph(Taxa &subset);
Expand Down Expand Up @@ -99,7 +99,7 @@ class Tree {
const std::unordered_map<std::string, std::string> &indiv2taxon);
Node *build_subtree_from(Node *root);
size_t refine_tree(Node *root);
void prepare_tree(Node *root, std::string weight_mode, weight_t low, weight_t high, weight_t threshold);
void prepare_tree(Node *root, std::string weight_mode, weight_t low, weight_t high, bool contract, weight_t threshold);
//void resolve_support(Node *root);
void add_indices(Node *root, std::vector<index_t> &indices);
void get_leaves(Node *root, std::vector<Node *> *leaves);
Expand Down
6 changes: 3 additions & 3 deletions src/tree_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ Node::Node(index_t index) {
length = 0;
parent = NULL;
size = -1;
//isfake = false;
isfake = false;
}

/*Node::Node(index_t index, bool isfake) {
Node::Node(index_t index, bool isfake) {
this->index = index;
support = length = 0;
parent = NULL;
size = -1;
this->isfake = isfake;
}*/
}

Node::~Node() {
for (Node *child : children)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.3
3.0.4

0 comments on commit b5bfe82

Please sign in to comment.