| 1 | // This file is part of A2Amapper. |
| 2 | // Copyright (c) 2005 J. Craig Venter Institute |
| 3 | // Author: Brian Walenz |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received (LICENSE.txt) a copy of the GNU General Public |
| 16 | // License along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | #ifndef OVERLAP_SPANTREE_H |
| 20 | #define OVERLAP_SPANTREE_H |
| 21 | |
| 22 | #include "overlap.H" |
| 23 | |
| 24 | class spanTree { |
| 25 | public: |
| 26 | spanTree() { |
| 27 | _tree = dict_create(DICTCOUNT_T_MAX, spanCompare); |
| 28 | }; |
| 29 | ~spanTree() { |
| 30 | dict_free_nodes(_tree); |
| 31 | dict_free(_tree); |
| 32 | }; |
| 33 | |
| 34 | void addNewSpan(uint32 iid, uint32 len) { |
| 35 | span_t *span = new span_t(iid, 0, len); |
| 36 | dict_alloc_insert(_tree, span, 0L); |
| 37 | }; |
| 38 | |
| 39 | uint32 size(void) { |
| 40 | return((uint32)dict_count(_tree)); |
| 41 | }; |
| 42 | |
| 43 | void addMatch(atacMatch *match, uint32 side, uint32 color) { |
| 44 | |
| 45 | // Query the tree for the first match before this position. |
| 46 | // We're guaranteed to find one before, since the tree was |
| 47 | // initialized with a span for the whole sequence. |
| 48 | // |
| 49 | span_t *span = 0L; |
| 50 | uint32 beg = 0; |
| 51 | uint32 end = 0; |
| 52 | |
| 53 | if (side == 0) { |
| 54 | span = new span_t(match->iid1, match->pos1, match->pos1 + match->len1); |
| 55 | beg = match->pos1; |
| 56 | end = match->pos1 + match->len1; |
| 57 | } else { |
| 58 | span = new span_t(match->iid2, match->pos2, match->pos2 + match->len2); |
| 59 | beg = match->pos2; |
| 60 | end = match->pos2 + match->len2; |
| 61 | } |
| 62 | |
| 63 | dnode_t *node = dict_upper_bound(_tree, span); |
| 64 | delete span; |
| 65 | span = (span_t *)dnode_getkey(node); |
| 66 | |
| 67 | // We need to split the span pointed to by node, iterate through |
| 68 | // all the spans, and split the last one. |
| 69 | |
| 70 | if (span->_beg != beg) { |
| 71 | span_t *l = 0L; |
| 72 | span_t *r = 0L; |
| 73 | span->split(beg, l, r); |
| 74 | |
| 75 | // Kill this node, insert the new ones |
| 76 | |
| 77 | dict_delete(_tree, node); |
| 78 | dnode_destroy(node); |
| 79 | dict_alloc_insert(_tree, l, 0L); |
| 80 | dict_alloc_insert(_tree, r, 0L); |
| 81 | |
| 82 | delete span; |
| 83 | span = r; |
| 84 | |
| 85 | // Argh! Now find the node we just inserted... |
| 86 | |
| 87 | node = dict_lookup(_tree, r); |
| 88 | } |
| 89 | |
| 90 | // Until we hit the last span, add the match to the span |
| 91 | |
| 92 | while (span->_end < end) { |
| 93 | span->addMatch(match->matchiid, color); |
| 94 | node = dict_next(_tree, node); |
| 95 | span = (span_t *)dnode_getkey(node); |
| 96 | } |
| 97 | |
| 98 | // We're at the last span, so split it like the beginning |
| 99 | |
| 100 | if (span->_end != end) { |
| 101 | span_t *l = 0L; |
| 102 | span_t *r = 0L; |
| 103 | span->split(end, l, r); |
| 104 | |
| 105 | // Kill this node, insert the new ones |
| 106 | |
| 107 | dict_delete(_tree, node); |
| 108 | dnode_destroy(node); |
| 109 | dict_alloc_insert(_tree, l, 0L); |
| 110 | dict_alloc_insert(_tree, r, 0L); |
| 111 | |
| 112 | delete span; |
| 113 | span = l; |
| 114 | } |
| 115 | |
| 116 | // FInally, add the match to the last span |
| 117 | |
| 118 | span->addMatch(match->matchiid, color); |
| 119 | }; |
| 120 | |
| 121 | dict_t *_tree; |
| 122 | dict_load_t _load; |
| 123 | }; |
| 124 | |
| 125 | #endif // OVERLAP_SPANTREE_H |