# bx-python / src / cluster.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``` ```/* Kanwei Li, 2009 Inspired by previous ClusterTree This clustering algorithm uses a binary tree structure. Nodes correspond to non-overlapping intervals, where overlapping means that the distance between two intervals is less or equal to max_dist, which is the max separation. The tree self-balances using rotations based on the binomial sequence. Merges among nodes are performed whenever a node is changed/added that will cause other nodes to form a new cluster. */ #include #include #include #include "cluster.h" #define ALLOC(pt) (malloc(sizeof(pt))) static int min(int a, int b) { if( a < b ) return a; else return b; } static int max(int a, int b) { if( a > b ) return a; else return b; } /* Create new tree with given max_dist (max distance between intervals to be considered a cluster), and min_intervals, the minimum number of intervals needed for a cluster to be considered significant */ clustertree* create_clustertree(int max_dist, int min_intervals) { clustertree *tree = ALLOC(clustertree); tree->max_dist = max_dist; tree->min_intervals = min_intervals; tree->root = NULL; return tree; } static interval* create_interval(int start, int end, int id) { interval *ival = ALLOC(interval); ival->start = start; ival->end = end; ival->id = id; ival->next = NULL; return ival; } static clusternode* create_node(int start, int end, int id) { clusternode *new_node = ALLOC(clusternode); new_node->start = start; new_node->end = end; new_node->interval_head = create_interval(start, end, id); new_node->interval_tail = new_node->interval_head; new_node->num_ivals = 1; new_node->left = NULL; new_node->right = NULL; double uniform = ((double)rand()) / (RAND_MAX); if (uniform == 1.0) uniform = 0; new_node->priority = (int)ceil( (-1.0 / log(.5)) * log( -1.0 / (uniform - 1))); return new_node; } static void recursively_free_intervals(interval *ival) { interval *next; if(ival) { next = ival->next; free(ival); recursively_free_intervals(next); } } static void recursively_free_nodes(clusternode *node) { if(node) { recursively_free_nodes(node->left); recursively_free_nodes(node->right); recursively_free_intervals(node->interval_head); free(node); } } void free_tree(clustertree *tree) { recursively_free_nodes(tree->root); free(tree); } void cluster_rotateright(clusternode **node) { clusternode* root = (*node)->left; (*node)->left = (*node)->left->right; root->right = (*node); *node = root; } void cluster_rotateleft(clusternode **node) { clusternode* root = (*node)->right; (*node)->right = (*node)->right->left; root->left = (*node); *node = root; } /* Go down the tree and merge nodes if necessary */ void cluster_fixup(clustertree *tree, clusternode **ln, clusternode **rn) { clusternode* local = *ln; clusternode* root = *rn; int maxstart = max(root->start, local->start); int maxend = max(local->end, root->end); int minstart = min(root->start, local->start); int minend = min(root->end, local->end); if( maxstart - minend <= tree->max_dist ) { /* Have to merge this node and children */ root->start = minstart; root->end = maxend; root->interval_tail->next = local->interval_head; root->interval_tail = local->interval_tail; root->num_ivals += local->num_ivals; if( local->right) cluster_fixup(tree, &(local->right), rn); if( local->left) cluster_fixup(tree, &(local->left), rn); if((local->right == NULL) && (local->left == NULL)) { free(local); *ln = NULL; } else if(local->right) { *ln = local->right; free(local); } else if (local->left) { *ln = local->left; free(local); } return; } // Even if we miss, we still have to check children if(local->left) { cluster_fixup(tree, &(local->left), rn); } if(local->right) { cluster_fixup(tree, &(local->right), rn); } } /* Pyrex "getregions" implements this. Only used for C debugging */ void clustereach(clustertree *tree, clusternode *node) { interval* ival; if (node == NULL) { exit(1); /* Shouldn't happen */ } if (node->left != NULL) { clustereach(tree, node->left); } printf("Node: %d\t%d\n", node->start, node->end); ival = node->interval_head; while(ival) { printf("\tInterval %d: %d\t%d\n", ival->id, ival->start, ival->end); ival = ival->next; } if (node->right != NULL) { clustereach(tree, node->right); } } void clusteritr_recursive(clustertree *tree, clusternode *node, treeitr* *itr) { treeitr *newitr; if (node == NULL) { return; } if (node->right != NULL) { clusteritr_recursive(tree, node->right, itr); } if (node->num_ivals >= tree->min_intervals) { newitr = ALLOC(treeitr); newitr->next = *itr; newitr->node = node; *itr = newitr; } if (node->left != NULL) { clusteritr_recursive(tree, node->left, itr); } } /* Create an infix iterator */ treeitr* clusteritr(clustertree *tree) { treeitr *itr = NULL; clusteritr_recursive(tree, tree->root, &itr); if (itr != NULL) { return itr; } return NULL; } /* Free iterator (tail recursive) */ void freeclusteritr(treeitr *itr) { treeitr *next; if (itr == NULL) { return; } next = itr->next; free(itr); freeclusteritr(next); } /* Insert based on the start position of intervals */ clusternode* clusternode_insert(clustertree *tree, clusternode *node, int start, int end, int id) { int oldstart; int oldend; interval* ival; // printf("Inserting %d %d %d\n", start, end, id); if (node == NULL) { node = create_node(start, end, id); } else if ( (start - tree->max_dist) > node->end ) { /* We're to the right of this cluster */ node->right = clusternode_insert(tree, node->right, start, end, id); if (node->priority < node->right->priority) cluster_rotateleft(&node); } else if ( (end + tree->max_dist) < node->start) { /* We're to the left of this cluster */ node->left = clusternode_insert(tree, node->left, start, end, id); if (node->priority < node->left->priority) cluster_rotateright(&node); } else { /* We're in the range of this cluster */ /* Update the start and end to match to new values */ oldstart = node->start; oldend = node->end; node->start = min(start, node->start); node->end = max(end, node->end); ival = create_interval(start, end, id); ival->next = node->interval_head; /* Add this interval as the head of the interval list */ node->interval_head = ival; node->num_ivals += 1; if ( oldstart > node->start && node->left != NULL ) { /* New interval added to the start, and there's a left child */ cluster_fixup(tree, &(node->left), &node); } if ( oldend < node->end && node->right != NULL ) { /* New interval added to the end, and there's a right child */ cluster_fixup(tree, &(node->right), &node); } } return node; } int main() { // Simple test clustertree* tree = create_clustertree(0, 1); tree->root = clusternode_insert(tree, tree->root, 3, 4, 0); tree->root = clusternode_insert(tree, tree->root, 6, 7, 1); tree->root = clusternode_insert(tree, tree->root, 9, 10, 2); tree->root = clusternode_insert(tree, tree->root, 1, 2, 3); tree->root = clusternode_insert(tree, tree->root, 3, 8, 4); clustereach(tree, tree->root); return 0; } ```