| 1 | #include "interval_tree.h" |
| 2 | |
| 3 | // Constructor for initializing an Interval Tree |
| 4 | IntervalTree::IntervalTree() { _root = NULL; } |
| 5 | |
| 6 | // A utility function to insert a new Interval Search Tree Node |
| 7 | // This is similar to BST Insert. Here the low value of interval |
| 8 | // is used tomaintain BST property |
| 9 | void IntervalTree::insert(ITNode *root, Interval data) { |
| 10 | // Base case: Tree is empty, new node becomes root |
| 11 | if (root == NULL) { |
| 12 | root = new ITNode(data); |
| 13 | _root = root; |
| 14 | } else { |
| 15 | // Get low value of interval at root |
| 16 | int l = root->data->low; |
| 17 | // If root's low value is greater, then new interval goes to |
| 18 | // left subtree |
| 19 | if (data.low < l) { |
| 20 | if (!root->left) { |
| 21 | ITNode *tmpNode = new ITNode(data); |
| 22 | // std::cout << data.low << ":" << data.high << "->insertLeft" << |
| 23 | // std::endl; |
| 24 | root->left = tmpNode; |
| 25 | } else { |
| 26 | insert(root->left, data); |
| 27 | } |
| 28 | } else { |
| 29 | if (!root->right) { |
| 30 | ITNode *tmpNode = new ITNode(data); |
| 31 | // std::cout << data.low << ":" << data.high << "->insertRight" << |
| 32 | // std::endl; |
| 33 | root->right = tmpNode; |
| 34 | } else { |
| 35 | insert(root->right, data); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | // update max value of ancestor node |
| 40 | if (root->max < data.high) root->max = data.high; |
| 41 | } |
| 42 | |
| 43 | // A utility function to check if the 1st interval envelops the second |
| 44 | bool doEnvelop(Interval i1, Interval i2) { |
| 45 | if (i1.low <= i2.low && i1.high >= i2.high) return true; |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | // The main function that searches an interval i in a given |
| 50 | // Interval Tree. |
| 51 | bool IntervalTree::envelopSearch(ITNode *root, Interval i) { |
| 52 | // Base Case, tree is empty |
| 53 | // std::cout << root->data->low << ":" << root->data->high << std::endl; |
| 54 | if (root == NULL) return false; |
| 55 | |
| 56 | // If given interval overlaps with root |
| 57 | if (doEnvelop(*(root->data), i)) return true; |
| 58 | |
| 59 | // If left child of root is present and max of left child is |
| 60 | // greater than or equal to given interval, then i may |
| 61 | // be enveloped by an amplicon in left subtree |
| 62 | if (root->left != NULL && root->left->max >= i.high) |
| 63 | return envelopSearch(root->left, i); |
| 64 | |
| 65 | // Else interval can only be enveloped by amplicon in right subtree |
| 66 | return envelopSearch(root->right, i); |
| 67 | } |
| 68 | |
| 69 | // A helper function for inorder traversal of the tree |
| 70 | void IntervalTree::inOrder(ITNode *root) { |
| 71 | if (root == NULL) return; |
| 72 | inOrder(root->left); |
| 73 | std::cerr << "[" << root->data->low << ", " << root->data->high << "]" |
| 74 | << " max = " << root->max << endl; |
| 75 | inOrder(root->right); |
| 76 | } |
| 77 | |
| 78 | // A stand-alone function to create a tree containing the coordinates of each |
| 79 | // amplicon based on user-specified primer pairs |
| 80 | IntervalTree populate_amplicons(std::string pair_info_file, |
| 81 | std::vector<primer> &primers) { |
| 82 | int amplicon_start = -1; |
| 83 | int amplicon_end = -1; |
| 84 | IntervalTree tree = IntervalTree(); |
| 85 | populate_pair_indices(primers, pair_info_file); |
| 86 | for (auto &p : primers) { |
| 87 | if (p.get_strand() == '+') { |
| 88 | if (p.get_pair_indice() != -1) { |
| 89 | amplicon_start = p.get_start(); |
| 90 | amplicon_end = primers[p.get_pair_indice()].get_end() + 1; |
| 91 | tree.insert(Interval(amplicon_start, amplicon_end)); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return tree; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | // Simple access functions to retrieve node's interval data |
| 100 | Interval ITNode::getData()const{ |
| 101 | return data; |
| 102 | } |
| 103 | // Simple access functions to retrieve node's left child |
| 104 | ITNode ITNode::getLeft()const{ |
| 105 | return left; |
| 106 | } |
| 107 | // Simple access functions to retrieve node's right child |
| 108 | ITNode ITNode::getRight()const{ |
| 109 | return right; |
| 110 | } |
| 111 | // Simple access functions to set node's left child |
| 112 | void ITNode::setLeft(ITNode *node){ |
| 113 | left = node; |
| 114 | } |
| 115 | // Simple access functions to set node's right child |
| 116 | void ITNode::setRight(ITNode *node){ |
| 117 | right = node; |
| 118 | } |
| 119 | |
| 120 | int main() |
| 121 | { |
| 122 | Interval ints[6] = {Interval(15, 20), Interval(30, 10), Interval(17, 19), |
| 123 | Interval(5, 20), Interval(12, 15), Interval(30, 40)}; int n = sizeof(ints) / |
| 124 | sizeof(ints[0]); IntervalTree tree = IntervalTree(); cout << "Hello World" << |
| 125 | endl; |
| 126 | // populate interval tree |
| 127 | for (int i = 0; i < n; i++) |
| 128 | { |
| 129 | tree.insert(ints[i]); |
| 130 | } |
| 131 | |
| 132 | tree.inOrder(); |
| 133 | Interval queries[4] = {Interval(15, 20), Interval(9, 30), Interval(31, 38), |
| 134 | Interval(7, 22)}; int num_tests = sizeof(queries) / sizeof(queries[0]); for (int |
| 135 | i = 0; i < num_tests; i++) |
| 136 | { |
| 137 | cout << "Does " << queries[i].low << ":" << queries[i].high << " Overlap? " << |
| 138 | tree.overlapSearch(queries[i]) << endl; |
| 139 | } |
| 140 | return 0; |
| 141 | } |
| 142 | */ |