| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : AP_sequence.hxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #ifndef AP_SEQUENCE_HXX |
|---|
| 12 | #define AP_SEQUENCE_HXX |
|---|
| 13 | |
|---|
| 14 | #ifndef ALIVIEW_HXX |
|---|
| 15 | #include <AliView.hxx> |
|---|
| 16 | #endif |
|---|
| 17 | #ifndef ARBTOOLS_H |
|---|
| 18 | #include <arbtools.h> |
|---|
| 19 | #endif |
|---|
| 20 | #ifndef ARB_ASSERT_H |
|---|
| 21 | #include <arb_assert.h> |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | #define ap_assert(cond) arb_assert(cond) |
|---|
| 25 | |
|---|
| 26 | typedef double AP_FLOAT; |
|---|
| 27 | |
|---|
| 28 | long AP_timer(); |
|---|
| 29 | |
|---|
| 30 | class AP_sequence : virtual Noncopyable { |
|---|
| 31 | mutable AP_FLOAT cached_wbc; // result for weighted_base_count(); <0.0 means "not initialized" |
|---|
| 32 | const AliView *ali; |
|---|
| 33 | |
|---|
| 34 | GBDATA *gb_sequence; // points to species/ali_xxx/data (or NULL if unbound, e.g. inner nodes in tree) |
|---|
| 35 | bool has_sequence; // true -> sequence was set() |
|---|
| 36 | long update; |
|---|
| 37 | |
|---|
| 38 | static long global_combineCount; |
|---|
| 39 | |
|---|
| 40 | protected: |
|---|
| 41 | void mark_sequence_set(bool is_set) { |
|---|
| 42 | if (is_set != has_sequence) { |
|---|
| 43 | update = is_set ? AP_timer() : 0; |
|---|
| 44 | has_sequence = is_set; |
|---|
| 45 | cached_wbc = -1.0; |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | static void inc_combine_count() { global_combineCount++; } |
|---|
| 50 | |
|---|
| 51 | virtual AP_FLOAT count_weighted_bases() const = 0; |
|---|
| 52 | |
|---|
| 53 | virtual void set(const char *sequence) = 0; |
|---|
| 54 | virtual void unset() = 0; |
|---|
| 55 | |
|---|
| 56 | void do_lazy_load() const; |
|---|
| 57 | |
|---|
| 58 | public: |
|---|
| 59 | AP_sequence(const AliView *aliview); |
|---|
| 60 | virtual ~AP_sequence() {} |
|---|
| 61 | |
|---|
| 62 | virtual AP_sequence *dup() const = 0; // used to dup derived class |
|---|
| 63 | |
|---|
| 64 | virtual AP_FLOAT combine(const AP_sequence* lefts, const AP_sequence *rights, char *mutation_per_site = 0) = 0; |
|---|
| 65 | virtual void partial_match(const AP_sequence* part, long *overlap, long *penalty) const = 0; |
|---|
| 66 | |
|---|
| 67 | static long combine_count() { return global_combineCount; } |
|---|
| 68 | |
|---|
| 69 | GB_ERROR bind_to_species(GBDATA *gb_species); |
|---|
| 70 | void unbind_from_species(); |
|---|
| 71 | bool is_bound_to_species() const { return gb_sequence != NULL; } |
|---|
| 72 | GBDATA *get_bound_species_data() const { return gb_sequence; } |
|---|
| 73 | |
|---|
| 74 | void lazy_load_sequence() const { |
|---|
| 75 | if (!has_sequence && is_bound_to_species()) do_lazy_load(); |
|---|
| 76 | } |
|---|
| 77 | void ensure_sequence_loaded() const { |
|---|
| 78 | lazy_load_sequence(); |
|---|
| 79 | ap_assert(has_sequence); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | bool got_sequence() const { return has_sequence; } |
|---|
| 83 | void forget_sequence() { |
|---|
| 84 | if (has_sequence) { |
|---|
| 85 | unset(); |
|---|
| 86 | update = 0; |
|---|
| 87 | has_sequence = false; |
|---|
| 88 | cached_wbc = -1.0; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | AP_FLOAT weighted_base_count() const { // returns < 0.0 if no sequence! |
|---|
| 93 | if (cached_wbc<0.0) cached_wbc = count_weighted_bases(); |
|---|
| 94 | return cached_wbc; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | size_t get_sequence_length() const { return ali->get_length(); } // filtered length |
|---|
| 98 | const AP_filter *get_filter() const { return ali->get_filter(); } |
|---|
| 99 | const AP_weights *get_weights() const { return ali->get_weights(); } |
|---|
| 100 | |
|---|
| 101 | const AliView *get_aliview() const { return ali; } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | #else |
|---|
| 106 | #error AP_sequence.hxx included twice |
|---|
| 107 | #endif // AP_SEQUENCE_HXX |
|---|