forked from EmanueleRaineri/cvlr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
162 lines (138 loc) · 3.75 KB
/
tree.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
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
struct pos_tnode{
uint64_t pos;
unsigned int count;
struct pos_tnode *left;
struct pos_tnode *right;
uint64_t index;
};
void pos_treeprint(struct pos_tnode *p){
if (p != NULL) {
pos_treeprint(p->left);
printf("@%lu %u %lu\n", p->pos, p->count, p->index );
pos_treeprint(p->right);
}
}
// sort genomic position by walking the tree in preorder
uint64_t pos_index(struct pos_tnode *p, uint64_t idx0){
if ( p != NULL ){
idx0 = pos_index(p->left, idx0);
p->index = idx0 ;
idx0 = pos_index(p->right, idx0+1);
}
return idx0;
}
// returns the index of a gpos
int pos_index_of(struct pos_tnode *p, uint64_t pos){
int res;
if ( p != NULL ) {
if ( pos == p->pos ) res = p-> index;
if ( pos < p->pos ) res = pos_index_of(p->left, pos);
if (pos > p->pos ) res = pos_index_of(p->right, pos);
} else {
res = -1;
}
return res;
}
struct pos_tnode *talloc(void){
return (struct pos_tnode *) malloc(sizeof(struct pos_tnode));
}
// here p is the root
struct pos_tnode *pos_addtree(struct pos_tnode *p, unsigned int pos){
if (NULL == p){
//fprintf(stderr, "allocating\n");
p = talloc();
p->pos = pos;
p->count = 1;
p->left = p->right = NULL;
}
else if ( pos == p->pos ) p->count++;
else if ( pos < p->pos) p->left = pos_addtree( p->left, pos );
else p->right = pos_addtree( p->right, pos );
return p;
}
void unroll_pos_tree(struct pos_tnode *p, uint64_t* sorted_pos){
if (NULL == p) return;
sorted_pos[p->index] = p->pos;
unroll_pos_tree(p->left, sorted_pos);
unroll_pos_tree(p->right, sorted_pos);
}
int count_pos_tnodes(struct pos_tnode *p){
if (NULL == p){return 0;}
else {
return 1 + count_pos_tnodes(p->left) + count_pos_tnodes(p->right);
}
}
uint64_t min_pos(struct pos_tnode *p){
if (NULL == p->left) return p->pos;
else return min_pos(p->left);
}
uint64_t max_pos(struct pos_tnode *p){
if (NULL == p->right) return p->pos;
else return max_pos(p->right);
}
/* rname */
struct rname_tnode{
char *rname;
unsigned int count;
struct rname_tnode *left;
struct rname_tnode *right;
uint64_t index;
};
void rname_treeprint(struct rname_tnode *p){
if (p != NULL) {
rname_treeprint(p->left);
printf("@%s %u %lu\n", p->rname, p->count, p->index );
rname_treeprint(p->right);
}
}
uint64_t rname_index(struct rname_tnode *p, uint64_t idx0){
if ( p != NULL ){
idx0 = rname_index(p->left, idx0);
p->index = idx0 ;
idx0 = rname_index(p->right, idx0+1);
}
return idx0;
}
int rname_index_of(struct rname_tnode *p, char* rname){
int res;
if ( p != NULL ) {
int cmp = strcmp(rname, p->rname);
if ( 0 == cmp) res= p-> index;
if ( cmp < 0 ) res = rname_index_of(p->left, rname);
if (cmp >0 ) res = rname_index_of(p->right, rname);
} else {
res = -1;
}
return res;
}
struct rname_tnode *rname_talloc(void){
return (struct rname_tnode *) malloc(sizeof(struct rname_tnode));
}
struct rname_tnode *rname_addtree(struct rname_tnode *p, char* rname){
if (NULL == p){
p = rname_talloc();
p->rname = strdup(rname);
p->count = 1;
p->left = p->right = NULL;
}
else if ( 0 == strcmp( rname, p-> rname) ) p->count++;
else if ( strcmp(rname, p-> rname) <0 ) p->left = rname_addtree(p->left, rname);
else p->right = rname_addtree(p->right, rname);
return p;
}
void unroll_rname_tree(struct rname_tnode *p, char** sorted_rname){
if (NULL == p) return;
sorted_rname[p->index] = p->rname;
unroll_rname_tree(p->left, sorted_rname);
unroll_rname_tree(p->right, sorted_rname);
}
int count_rname_tnodes(struct rname_tnode *p){
if (NULL == p){return 0;}
else {
return 1 + count_rname_tnodes(p->left) + count_rname_tnodes(p->right);
}
}