-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitch.c
293 lines (243 loc) · 14 KB
/
fitch.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Code written by Thien Le in September 2018 implementing Fitch algorithm. The main algorithm is implemented in the first part,
// followed by a unit test suite. For more information on how to run the unit test, refer to that section.
// Some limits:
// The maximum number of nodes (not just leaves) are hardcoded to 1M. This is to facillitate building arrays on the stack
// at compile time. If more than 1M nodes is required, may need to switch to heap implementation (which is slower), or have the compiler
// extend the stack space.
// The maximum number of letters in a leaf name is hardcoded to 10. Again, this is to facillitate arrays on stack.
// Labels must be integer no larger than 31. This is to facillitate bitwise operations in the implementation. Extension is trivial
// but still require some work.
// Compilation: you may need to use -lm flag for some version of GCC since some of the math.h functions would not be imported otherwise.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define UNIT_TEST 1
// Global variable to quickly have a tree on the stack
// If the tree is huge (> 10^6) then may want to switch to heap implementation; but will take more time since malloc is slow
const int N_LIM = (int) 10e6;
const int NAME_LIM = 10;
int root;
int left [N_LIM]; // left child node
int right [N_LIM]; // right child node
int parent [N_LIM]; // parent node
int label [N_LIM]; // digitized taxon id
int set [N_LIM]; // bitset parsimony where bit i is set if label i is in the parsimony set
char name_map [N_LIM][NAME_LIM];
double weight [N_LIM][2]; // adjacency list style weight, the left edge is the first entry, the right edge is the second (currently not used for anything)
// Fitch algorithm
int post_order_dfs(int c){
int cap, cup;
if(left[c] == -1){ // at leaves
set[c] |= (1 << label[c]); // the set at the leave is a singleton
return 0;
}
post_order_dfs(left[c]);
post_order_dfs(right[c]);
cap = set[left[c]] & set[right[c]];
cup = set[left[c]] | set[right[c]];
set[c] = cap ? cap : cup;
return 0;
}
int pre_order_dfs(int c){
int cap = 0;
if(left[c] == -1) // at leaves
return 0;
if(parent[c] == -1 || // at root and short circuit
!(cap = set[c] & set[parent[c]])) // or empty intersection
set[c] &= -set[c]; // clear all bits but the LSB
else
set[c] = cap; // else, set it as the intersection
label[c] = (int) (log(set[c]) / log(2.0));
pre_order_dfs(left[c]);
pre_order_dfs(right[c]);
return 0;
}
int fitch_algorithm(){
post_order_dfs(root);
pre_order_dfs(root);
return 0;
}
// Unit test suite.
// Input: pipe into stdin a Newick string with leaf node of the form <name>?<label>:<edge_weight>
// and internal node of the form <subtree>:<edge_weight>
// Output: the same tree with internal node labelled by the Fitch algorithm is printed to stdout.
// Example input: ((A?10:0.1,B?5:1.2):10,(C?30:0.9,(D?2:0.0,ADF?30:1.2):0.6):0.6);
// Example output: ((A?10:0.100000,B?5:1.200000)?5:10.000000,(C?30:0.900000,(D?2:0.000000,ADF?30:1.200000)?30:0.600000)?30:0.600000)?5;
// notice that internal nodes, as well as the root node now have labels.
//
//--------------------- UNIT TEST CODE
#if UNIT_TEST
#define DEBUG 0
// Unit test initialization
const int READ_WEIGHT_STATE = 2;
const int READ_LABEL_STATE = 1;
const int READ_NAME_STATE = 0;
const int OTHER_STATE = 3;
void write_to_mem(int parent, char * buf, int state){
int node;
node = right[parent] == -1 ? left[parent] : right[parent];
if(buf[0] == 0) return;
if(state == READ_NAME_STATE)
strcpy(name_map[node], buf);
else if (state == READ_LABEL_STATE)
sscanf(buf, "%d", &label[node]);
else if (state == READ_WEIGHT_STATE)
sscanf(buf, "%lf", &weight[parent][right[parent] != -1]); // if the right parent exists then write to the right
buf[0] = 0;
}
void inc_node(int * node_counter, int * cur_state){
(*node_counter)++;
*cur_state = READ_NAME_STATE;
}
void set_parent(int par, int chi){
parent[chi] = par;
if(left[par] == -1)
left[par] = chi;
else
right[par] = chi;
}
void init(){
char cur_char;
char cur_mem[3][10000]; // 1. name; 2. label; 3. weight
int node_counter = 0;
int cur_parent = -1;
int cur_state = OTHER_STATE;
// Clear all variables
root = 0;
cur_mem[0][0] = 0;
cur_mem[1][0] = 0;
cur_mem[2][0] = 0;
memset(left, -1, N_LIM * sizeof(int));
memset(right, -1, N_LIM * sizeof(int));
memset(parent, -1, N_LIM * sizeof(int));
memset(label, -1, N_LIM * sizeof(int));
memset(set, 0, N_LIM * sizeof(int));
// Read from stdin a Newick string a Newick string
while(scanf("%c", &cur_char) == 1){
#if DEBUG
printf("debug: in loop, cur_label is %s, char is %c\n", cur_mem[2], cur_char);
#endif
switch(cur_char){
case '(':
cur_parent = node_counter; // increment level
inc_node(&node_counter, &cur_state);
set_parent(cur_parent, node_counter);
break;
case ',':
if(cur_state != OTHER_STATE) write_to_mem(cur_parent, cur_mem[cur_state], cur_state);
inc_node(&node_counter, &cur_state);
set_parent(cur_parent, node_counter);
break;
case ')':
if(cur_state != OTHER_STATE) write_to_mem(cur_parent, cur_mem[cur_state], cur_state);
cur_parent = parent[cur_parent]; // decrement level
cur_state = OTHER_STATE;
break;
case ':':
if(cur_state != OTHER_STATE) write_to_mem(cur_parent, cur_mem[cur_state], cur_state);
cur_state = READ_WEIGHT_STATE;
break;
case '?':
if(cur_state != OTHER_STATE) write_to_mem(cur_parent, cur_mem[cur_state], cur_state);
cur_state = READ_LABEL_STATE;
break;
case ';':
break;
default:
if(cur_state != OTHER_STATE){
cur_mem[cur_state][strlen(cur_mem[cur_state]) + 1] = 0;
cur_mem[cur_state][strlen(cur_mem[cur_state])] = cur_char;
}
}
}
}
void write_dfs(int c, char * builder){
char buffer[10000];
if(left[c] == -1){
strcat(builder, name_map[c]);
return;
}
// Left child
strcat(builder, "(");
write_dfs(left[c], builder);
sprintf(buffer, "?%d:%lf", label[left[c]], weight[c][0]);
strcat(builder, buffer);
// Right child
strcat(builder, ",");
write_dfs(right[c], builder);
sprintf(buffer, "?%d:%lf", label[right[c]], weight[c][1]);
strcat(builder, buffer);
strcat(builder, ")");
}
int write_newick(){
char buffer[100000];
buffer[0] = 0;
write_dfs(0, buffer);
printf("%s?%d;\n", buffer, label[0]);
return 0;
}
int main(){
init();
#if DEBUG
int i;
int n = 50;
printf("debug: this print out the parent, left, right, label, name_map and set\n");
for(i = 0; i < n; i++){
printf("%d ", parent[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", left[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", right[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", label[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%s ", name_map[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", set[i]);
}
printf("\n");
#endif
fitch_algorithm();
#if DEBUG
// int i;
// int n = 50;
printf("debug: this print out the parent, left, right, label, name_map and set\n");
for(i = 0; i < n; i++){
printf("%d ", parent[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", left[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", right[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", label[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%s ", name_map[i]);
}
printf("\n");
for(i = 0; i < n; i++){
printf("%d ", set[i]);
}
printf("\n");
#endif
write_newick();
}
#endif