| 1 | // ============================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : BI_basepos.hxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ============================================================= // |
|---|
| 10 | |
|---|
| 11 | #ifndef BI_BASEPOS_HXX |
|---|
| 12 | #define BI_BASEPOS_HXX |
|---|
| 13 | |
|---|
| 14 | #ifndef ARBDB_BASE_H |
|---|
| 15 | #include <arbdb_base.h> |
|---|
| 16 | #endif |
|---|
| 17 | #ifndef ARB_ASSERT_H |
|---|
| 18 | #include <arb_assert.h> |
|---|
| 19 | #endif |
|---|
| 20 | #ifndef ARBTOOLS_H |
|---|
| 21 | #include <arbtools.h> |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | #ifndef bi_assert |
|---|
| 25 | #define bi_assert(bed) arb_assert(bed) |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | typedef bool (*char_predicate_fun)(char); |
|---|
| 29 | |
|---|
| 30 | class CharPredicate { // general predicate for char |
|---|
| 31 | bool isTrue[256]; |
|---|
| 32 | public: |
|---|
| 33 | explicit CharPredicate(char_predicate_fun is_true) { |
|---|
| 34 | for (int i = 0; i<256; ++i) { // IRRELEVANT_LOOP |
|---|
| 35 | isTrue[i] = is_true(safeCharIndex((unsigned char)i)); |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | bool applies(char c) const { return isTrue[safeCharIndex(c)]; } |
|---|
| 40 | bool applies(unsigned char c) const { return isTrue[c]; } |
|---|
| 41 | bool applies(int i) const { bi_assert(i == (unsigned char)i); return isTrue[i]; } |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | typedef char_predicate_fun is_gap_fun; |
|---|
| 45 | |
|---|
| 46 | class BasePosition : virtual Noncopyable { |
|---|
| 47 | int absLen; |
|---|
| 48 | int baseCount; |
|---|
| 49 | |
|---|
| 50 | int *abs2rel; |
|---|
| 51 | int *rel2abs; |
|---|
| 52 | |
|---|
| 53 | void setup() { |
|---|
| 54 | absLen = 0; |
|---|
| 55 | baseCount = 0; |
|---|
| 56 | abs2rel = NULp; |
|---|
| 57 | rel2abs = NULp; |
|---|
| 58 | } |
|---|
| 59 | void cleanup() { |
|---|
| 60 | delete [] abs2rel; |
|---|
| 61 | delete [] rel2abs; |
|---|
| 62 | setup(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public: |
|---|
| 66 | void initialize(const char *seq, int size); |
|---|
| 67 | void initialize(const char *seq, int size, const CharPredicate& is_gap); |
|---|
| 68 | |
|---|
| 69 | BasePosition() { setup(); } |
|---|
| 70 | BasePosition(const char *seq, int size) { setup(); initialize(seq, size); } |
|---|
| 71 | BasePosition(const char *seq, int size, const CharPredicate& is_gap) { setup(); initialize(seq, size, is_gap); } |
|---|
| 72 | ~BasePosition() { cleanup(); } |
|---|
| 73 | |
|---|
| 74 | bool gotData() const { return abs2rel; } |
|---|
| 75 | |
|---|
| 76 | int abs_2_rel(int abs) const { |
|---|
| 77 | // returns the number of base characters in range [0..abs-1]! |
|---|
| 78 | // (i.e. result is in [0..bases]) |
|---|
| 79 | // 'abs' has to be in range [0..abs_count()] |
|---|
| 80 | |
|---|
| 81 | bi_assert(gotData()); |
|---|
| 82 | if (abs<0) return 0; |
|---|
| 83 | if (abs > absLen) abs = absLen; |
|---|
| 84 | return abs2rel[abs]; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | int rel_2_abs(int rel) const { |
|---|
| 88 | // 'rel' is [0..N-1] (0 = 1st base, 1 = 2nd base) |
|---|
| 89 | // returns abs. position of that base |
|---|
| 90 | |
|---|
| 91 | bi_assert(gotData()); |
|---|
| 92 | if (rel >= baseCount) rel = baseCount-1; |
|---|
| 93 | if (rel<0) return 0; |
|---|
| 94 | return rel2abs[rel]; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | int first_base_abspos() const { return rel_2_abs(0); } |
|---|
| 98 | int last_base_abspos() const { return rel_2_abs(baseCount-1); } |
|---|
| 99 | |
|---|
| 100 | int base_count() const { return baseCount; } |
|---|
| 101 | int abs_count() const { return absLen; } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | struct BI_ecoli_ref : public BasePosition { |
|---|
| 107 | BI_ecoli_ref() {} |
|---|
| 108 | |
|---|
| 109 | void init(const char *seq, int size) { initialize(seq, size); } |
|---|
| 110 | GB_ERROR init(GBDATA *gb_main); |
|---|
| 111 | GB_ERROR init(GBDATA *gb_main, char *alignment_name, char *ref_name); |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | #else |
|---|
| 116 | #error BI_basepos.hxx included twice |
|---|
| 117 | #endif // BI_BASEPOS_HXX |
|---|