| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : SEC_abspos.cxx // |
|---|
| 4 | // Purpose : Encapsulates helix position access // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in July 2007 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =============================================================== // |
|---|
| 11 | |
|---|
| 12 | #include <cstdlib> |
|---|
| 13 | #include <cstring> |
|---|
| 14 | |
|---|
| 15 | #include "SEC_abspos.hxx" |
|---|
| 16 | |
|---|
| 17 | using namespace std; |
|---|
| 18 | |
|---|
| 19 | void XString::set_length(size_t len) { |
|---|
| 20 | if (number_found && x_string_len<len) freeset(number_found, 0); |
|---|
| 21 | x_string_len = len; |
|---|
| 22 | initialized = false; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | XString::XString(size_t ali_length) |
|---|
| 26 | : abspos(0) |
|---|
| 27 | , number_found(0) |
|---|
| 28 | { |
|---|
| 29 | int len = ali_length+1; // need one more (cause 'x's are written behind position) |
|---|
| 30 | x_string = (char*)malloc((len+1)*sizeof(*x_string)); |
|---|
| 31 | memset(x_string, '.', len); |
|---|
| 32 | x_string[len] = 0; |
|---|
| 33 | set_length(len); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | XString::XString(const char *saved_x_string, size_t saved_length, size_t ali_length) |
|---|
| 37 | : abspos(0) |
|---|
| 38 | , number_found(0) |
|---|
| 39 | { |
|---|
| 40 | size_t xlen = ali_length+1; |
|---|
| 41 | |
|---|
| 42 | sec_assert(saved_length == strlen(saved_x_string)); |
|---|
| 43 | sec_assert(saved_length == xlen || saved_length == xlen-1); |
|---|
| 44 | |
|---|
| 45 | x_string = (char*)malloc((xlen+1)*sizeof(*x_string)); |
|---|
| 46 | memcpy(x_string, saved_x_string, saved_length+1); |
|---|
| 47 | |
|---|
| 48 | if (saved_length == xlen-1) { // normal case |
|---|
| 49 | x_string[xlen-1] = '.'; // additional position is a gap (SAI 'HELIX' should have a gap at end) |
|---|
| 50 | x_string[xlen] = 0; // (see also comments in get_x_string()) |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | set_length(xlen); |
|---|
| 54 | initialize(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void XString::initialize() |
|---|
| 58 | { |
|---|
| 59 | // detect number of 'x's in x_string : |
|---|
| 60 | { |
|---|
| 61 | size_t len = 0; |
|---|
| 62 | int x = 0; |
|---|
| 63 | |
|---|
| 64 | while (char c = x_string[len]) { |
|---|
| 65 | if (c == 'x') x++; |
|---|
| 66 | len++; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | sec_assert(len == x_string_len); |
|---|
| 70 | |
|---|
| 71 | if (abspos) { // re-initialization |
|---|
| 72 | if (x_count<x) freeset(abspos, 0); // abspos array too small |
|---|
| 73 | } |
|---|
| 74 | x_count = x; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | if (!abspos) abspos = (size_t*)malloc(x_count * sizeof(*abspos)); |
|---|
| 78 | if (!number_found) number_found = (int*)malloc(x_string_len * sizeof(*number_found)); |
|---|
| 79 | |
|---|
| 80 | // init abspos and number_found : |
|---|
| 81 | { |
|---|
| 82 | int pos = 0; |
|---|
| 83 | int x = 0; |
|---|
| 84 | |
|---|
| 85 | while (char c = x_string[pos]) { |
|---|
| 86 | number_found[pos] = x; |
|---|
| 87 | if (c == 'x') { |
|---|
| 88 | abspos[x] = pos; |
|---|
| 89 | x++; |
|---|
| 90 | } |
|---|
| 91 | pos++; |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | initialized = true; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | XString::~XString() |
|---|
| 99 | { |
|---|
| 100 | free(x_string); |
|---|
| 101 | free(abspos); |
|---|
| 102 | free(number_found); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | size_t XString::getAbsPos(int x) const |
|---|
| 106 | { |
|---|
| 107 | sec_assert(initialized); |
|---|
| 108 | sec_assert(x >= 0 && x<x_count); |
|---|
| 109 | size_t pos = abspos[x]; |
|---|
| 110 | sec_assert(pos<x_string_len); |
|---|
| 111 | return pos; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | int XString::getXleftOf(size_t pos) const |
|---|
| 115 | { |
|---|
| 116 | sec_assert(initialized); |
|---|
| 117 | sec_assert(pos<x_string_len); |
|---|
| 118 | int x = number_found[pos]; |
|---|
| 119 | sec_assert(x >= 0 && x<x_count); |
|---|
| 120 | return x; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | const char *XString::get_x_string() const { |
|---|
| 124 | sec_assert(initialized); |
|---|
| 125 | |
|---|
| 126 | static char *copy = 0; |
|---|
| 127 | static size_t copy_alloc = 0; |
|---|
| 128 | |
|---|
| 129 | size_t bufsize = x_string_len+1; |
|---|
| 130 | |
|---|
| 131 | if (!copy || copy_alloc<bufsize) { |
|---|
| 132 | freeset(copy, (char*)malloc(bufsize)); |
|---|
| 133 | copy_alloc = bufsize; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | memcpy(copy, x_string, x_string_len+1); |
|---|
| 137 | |
|---|
| 138 | int add_pos = x_string_len-1; |
|---|
| 139 | |
|---|
| 140 | if (copy[add_pos] == '.') { // can be removed - added again after reload |
|---|
| 141 | copy[add_pos] = 0; // hide internal additional position |
|---|
| 142 | } |
|---|
| 143 | else { |
|---|
| 144 | // happens only if there's a helix on last alignment position. |
|---|
| 145 | // In this case we save the additional position to DB, which will |
|---|
| 146 | // lead the user to reformat his alignment (which will add a gap |
|---|
| 147 | // at end of SAI HELIX) |
|---|
| 148 | } |
|---|
| 149 | return copy; |
|---|
| 150 | } |
|---|
| 151 | |
|---|