| Bsoft 2.0.5 Bernard's software package

rwtree.cpp
Go to the documentation of this file.
1
9 #include "rwtree.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
23 Bnode* read_tree(char* filename)
24 {
25 FILE *ftree;
26 if ( ( ftree = fopen(filename, "r") ) == NULL ) return NULL;
27
28 if ( verbose & VERB_LABEL )
29 cout << "Reading file: " << filename << endl;
30
31 fseek(ftree, 0, SEEK_END);
32 long filesize = ftell(ftree);
33 fseek(ftree, 0, SEEK_SET);
34
35 char* buffer = (char *) malloc(filesize);
36 if ( fread( buffer, filesize, 1, ftree ) < 1 ) {
37 fclose(ftree);
38 return NULL;
39 }
40
41 fclose(ftree);
42
43 char* aptr = buffer;
44 while ( *aptr != '(' ) aptr++;
45 unsigned int i, j(0);
46 for ( i=0; i<strlen(aptr); i++ ) if ( !isspace(aptr[i]) ) {
47 aptr[j] = aptr[i];
48 j++;
49 }
50 aptr[j] = 0;
51
52 if ( verbose & VERB_DEBUG )
53 cout << buffer << endl << endl;
54
55 // The root node is trifurcated because the tree is "unrooted"
56 Bnode* root = tree_read_node(&aptr, NULL);
57
58 int itip(0);
59 tree_calculate_angle(root, &itip, M_PI*2.0/root->number);
60
61 if ( verbose & VERB_PROCESS ) {
62 tree_show(root);
63 cout << "Number of tree tips: " << root->number << endl;
64 }
65
66 return root;
67 }
68
75 Bnode* tree_read_node(char** handle, Bnode* one)
76 {
77 Bnode* node = new Bnode;
78 memset(node, 0, sizeof(Bnode));
79
80 node->one = one;
81 node->label[0] = '.';
82 int i;
83 char astring[128];
84
85 // Every internal node starts with a '(' and is delimited by a ','
86 if ( **handle == '(' ) {
87 *handle += 1;
88 node->two = tree_read_node(handle, node);
89 *handle += 1;
90 node->three = tree_read_node(handle, node);
91 node->number += node->two->number + node->three->number;
92 *handle += 1;
93 if ( !one ) {
94 node->one = tree_read_node(handle, node);
95 node->number += node->one->number;
96 }
97 } else {
98 node->number = 1; // Count the tip node
99 if ( **handle == ',' ) *handle += 1;
100 for ( i=0; **handle != ':' && **handle != 0; i++, *handle += 1 )
101 node->label[i] = **handle; // Get the node label
102 }
103
104 *handle += 1;
105
106 // Get the branch length
107 if ( strlen(*handle) > 0 ) {
108 for ( i=0; **handle != ',' && **handle != ')'; i++, *handle += 1 )
109 astring[i] = **handle;
110 astring[i] = 0;
111 sscanf(astring, "%f", &node->length);
112 if ( node->length == 0 && strlen(astring) > 0 ) node->length = 0.000001;
113 }
114
115 if ( **handle == ')' ) *handle += 1;
116
117
118 if ( verbose & VERB_DEBUG )
119 cout << "DEBUG tree_read_node: " << &node << " " << &node->one << " "
120 << &node->two << " " << &node->three << " " << node->number
121 << " " << node->label << " " << node->length << endl;
122
123 return node;
124 }
125
133 int tree_calculate_angle(Bnode* node, int* itip, float tip_angle)
134 {
135 if ( node->two ) {
136 tree_calculate_angle(node->two, itip, tip_angle);
137 tree_calculate_angle(node->three, itip, tip_angle);
138 node->angle = (node->two->angle + node->three->angle)/2;
139 } else {
140 node->angle = (*itip)*tip_angle;
141 *itip += 1;
142 }
143
144 if ( node->length == 0 ) // Third branch of the root node
145 tree_calculate_angle(node->one, itip, tip_angle);
146
147 if ( verbose & VERB_DEBUG )
148 cout << "DEBUG tree_calculate_angle: " << *itip << " " << &node << " "
149 << &node->one << " " << &node->two << " " << &node->three << " "
150 << node->number << " " << node->label << " " << node->length << " " << node->angle << endl;
151
152 return 0;
153 }
154
160 int tree_delete_node(Bnode* node)
161 {
162 if ( node->two ) {
163 tree_delete_node(node->two);
164 tree_delete_node(node->three);
165 }
166
167 if ( node->length == 0 ) // Third branch of the root node
168 tree_delete_node(node->one);
169
170 delete node;
171
172 return 0;
173 }
174
184 int tree_rotate(Bnode* node, float angle)
185 {
186 if ( node->length == 0 ) // Third branch of the root node
187 tree_rotate(node->one, angle);
188
189 if ( node->two ) {
190 tree_rotate(node->two, angle);
191 tree_rotate(node->three, angle);
192 }
193
194 node->angle += angle;
195
196 return 0;
197 }
198
204 int tree_show(Bnode* node)
205 {
206 cout << " - " << node->label << " (" << node->length << ")";
207
208 if ( node->length == 0 ) // Third branch of the root node
209 tree_show(node->one);
210
211 if ( node->two ) {
212 tree_show(node->two);
213 tree_show(node->three);
214 }
215
216 if ( node->number < 2 ) cout << endl;
217
218 return 0;
219 }
utilities.h
Header file for general utilities.
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
read_tree
Bnode * read_tree(char *filename)
Reads a phylip format tree file.
Definition: rwtree.cpp:23
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:107
M_PI
#define M_PI
Definition: utilities.h:59
rwtree.h
Header file for reading (and writing) trees.
verbose
int verbose
tree_calculate_angle
int tree_calculate_angle(Bnode *node, int *itip, float tip_angle)
Calculates an angle for each node for plotting.
Definition: rwtree.cpp:133
tree_show
int tree_show(Bnode *node)
Shows a node with all daughter nodes.
Definition: rwtree.cpp:204
Bnode::two
Bnode * two
Definition: rwtree.h:25
Bnode::label
char label[100]
Definition: rwtree.h:27
Bnode::length
float length
Definition: rwtree.h:28
tree_read_node
Bnode * tree_read_node(char **handle, Bnode *one)
Reads a node into a tree structure.
Definition: rwtree.cpp:75
Bnode::angle
float angle
Definition: rwtree.h:29
Bnode::number
int number
Definition: rwtree.h:26
Bnode::three
Bnode * three
Definition: rwtree.h:25
Bnode
Definition: rwtree.h:24
tree_delete_node
int tree_delete_node(Bnode *node)
Deletes a node with all daughter nodes.
Definition: rwtree.cpp:160
Bnode::one
Bnode * one
Definition: rwtree.h:25
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:106
tree_rotate
int tree_rotate(Bnode *node, float angle)
Adds an angle to all nodes.
Definition: rwtree.cpp:184