-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode.cpp
146 lines (131 loc) · 4.57 KB
/
node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* hybrid-Lambda is used to simulate gene trees given species network under
* coalescent process.
*
* Copyright (C) 2010 -- 2015 Sha (Joe) Zhu
*
* This file is part of hybrid-Lambda.
*
* hybrid-Lambda is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include"node.hpp"
Node::Node(){
label="";
node_content="";
//num_child=0;
num_descndnt=0;
num_descndnt_interior=0;
parent1=NULL;
parent2=NULL;
this->brchlen1_ = 0.0;
this->brchlen2_ = 0.0;
this->rank_ = 0;
this->e_num_ = 0;
this->e_num2_ = 0;
//visit=0;
descndnt_of_hybrid=false;
tip_bool=false;
//clade=" ";
this->height_ = 1.0/0.0;
//prob_to_hybrid_left=1.0;
this->visited_ = false;
this->set_label1_starts_at(0);
this->set_label2_starts_at(0);
}
void Node::print( bool is_Net ){
cout << setw(12) << label;
if ( is_Net ) cout << setw(6) << this->hybrid();
if ( is_Net ) cout << setw(8) << descndnt_of_hybrid;
cout << setw(5) << tip_bool;
if (this->parent1) cout << setw (11) << (parent1->label);
else cout << " ";
cout << setw (12) << this->height();
cout << setw (12) << this->brchlen1();
if (is_Net){
if (this->parent2) cout << setw (11) << (parent2->label);
else cout << " ";
cout<<setw (12) << this->brchlen2();
}
cout << setw (7) << this->child.size();
cout << setw (8) << num_descndnt;
cout << setw(4) << num_descndnt_interior;
cout << setw(6) << this->rank() << " ";
//for (size_t i=0;i<descndnt.size();i++){
//cout<<setw (1)<<descndnt[i];
//}
cout << setw(2)<<this->e_num();
if ( is_Net ) cout << setw(3) << this->e_num2();
cout << " " << this->clade;
//cout<<endl;
}
/*! \brief Add child node to parent node */
void Node::add_child( Node *child_node, /*! pointer to the child node*/
size_t adding_to_parent){
this->child.push_back(child_node);
if ( (adding_to_parent == 2) | (child_node->parent1 != NULL)){
child_node->parent2 = (this);
//child_node->hybrid = true;
} else {
assert(adding_to_parent == 1);
child_node->parent1 = (this);
}
}
/*! \brief Rank network node from the bottom.
*
* Child node has lower rank than the parent node. Tip nodes have rank one, the root node has the highest rank
*/
void Node::CalculateRank(){
if ( this->tip_bool ) this->rank_ = 1;
else {
size_t child_max_rank = 0;
for ( size_t ith_child = 0; ith_child < this->child.size(); ith_child++ ){
this->child[ith_child]->CalculateRank();
child_max_rank = max( child_max_rank, this->child[ith_child]->rank() );
}
this->rank_ = child_max_rank + 1;
}
}
bool Node::find_descndnt ( string &name, NAMETYPE type ){
if ( this->tip_bool ) {
string tmp = ( type == TAXA ) ? this->name : this->label;
return ( tmp == name ) ? true : false;
}
else {
bool descndnt_found = false;
for (size_t i = 0; i < this->child.size(); i++ ){
descndnt_found = this->child[i]->find_descndnt( name, type );
if ( descndnt_found ) break;
}
return descndnt_found;
}
}
/*! \brief label a node if its a tip node */
void Node::find_tip(){
if ( this->child.size() == 0) this->tip_bool = true;
else {
for ( size_t ith_child = 0; ith_child < this->child.size(); ith_child++ ){
(*this->child[ith_child]).find_tip();
//this->child[ith_child]->find_tip();
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////// consider removed
/*! \brief Label a node if its a descendant of a hybrid node */
void Node::find_hybrid_descndnt(){
if ( this->tip_bool ) return;
for ( size_t ith_child = 0; ith_child < this->child.size(); ith_child++){
if ( this->hybrid() || this->descndnt_of_hybrid ) this->child[ith_child]->descndnt_of_hybrid = true;
this->child[ith_child]->find_hybrid_descndnt();
}
}