| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : AP_filter.hxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #ifndef AP_FILTER_HXX |
|---|
| 12 | #define AP_FILTER_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 | #define af_assert(cond) arb_assert(cond) |
|---|
| 25 | |
|---|
| 26 | typedef unsigned char uchar; |
|---|
| 27 | |
|---|
| 28 | enum AWT_FILTER_SIMPLIFY { |
|---|
| 29 | AWT_FILTER_SIMPLIFY_NOT_INITIALIZED = -1, |
|---|
| 30 | AWT_FILTER_SIMPLIFY_NONE = 0, |
|---|
| 31 | AWT_FILTER_SIMPLIFY_DNA, |
|---|
| 32 | AWT_FILTER_SIMPLIFY_PROTEIN, |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | class AP_filter { |
|---|
| 36 | bool *filter_mask; // true means "use position" |
|---|
| 37 | size_t filter_len; // length of 'filter_mask' |
|---|
| 38 | size_t real_len; // how many 'true's are in 'filter_mask' |
|---|
| 39 | long update; // timestamp |
|---|
| 40 | |
|---|
| 41 | uchar simplify[256]; // base -> simplified base |
|---|
| 42 | AWT_FILTER_SIMPLIFY simplify_type; |
|---|
| 43 | |
|---|
| 44 | size_t *bootstrap; // bootstrap[i] points to random filter positions [0..real_len[ |
|---|
| 45 | |
|---|
| 46 | size_t *filterpos_2_seqpos; // filterpos -> sequencepos |
|---|
| 47 | |
|---|
| 48 | #if defined(ASSERTION_USED) |
|---|
| 49 | mutable bool checked_for_validity; |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | void calc_filterpos_2_seqpos(); |
|---|
| 53 | |
|---|
| 54 | void init(size_t size); |
|---|
| 55 | void make_permeable(size_t size); |
|---|
| 56 | |
|---|
| 57 | size_t bootstrapped_filterpos(size_t bpos) const { |
|---|
| 58 | af_assert(does_bootstrap()); |
|---|
| 59 | af_assert(bpos<real_len); |
|---|
| 60 | size_t fpos = bootstrap[bpos]; |
|---|
| 61 | af_assert(fpos<real_len); |
|---|
| 62 | return fpos; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public: |
|---|
| 66 | AP_filter(size_t size); // permeable filter (passes all columns) |
|---|
| 67 | AP_filter(const char *filter, const char *zerobases, size_t size); |
|---|
| 68 | AP_filter(const AP_filter& other); |
|---|
| 69 | ~AP_filter(); |
|---|
| 70 | DECLARE_ASSIGNMENT_OPERATOR(AP_filter); |
|---|
| 71 | |
|---|
| 72 | long get_timestamp() const { return update; } |
|---|
| 73 | size_t get_filtered_length() const { return real_len; } |
|---|
| 74 | size_t get_length() const { return filter_len; } |
|---|
| 75 | |
|---|
| 76 | bool use_position(size_t pos) const { // returns true if filter is set for position 'pos' |
|---|
| 77 | af_assert(checked_for_validity); |
|---|
| 78 | af_assert(pos<filter_len); |
|---|
| 79 | return filter_mask[pos]; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | const size_t *get_filterpos_2_seqpos() const { |
|---|
| 83 | if (!filterpos_2_seqpos) { |
|---|
| 84 | // this is no modification, it's lazy initialization: |
|---|
| 85 | const_cast<AP_filter*>(this)->calc_filterpos_2_seqpos(); |
|---|
| 86 | } |
|---|
| 87 | return filterpos_2_seqpos; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | void enable_simplify(AWT_FILTER_SIMPLIFY type); // default is AWT_FILTER_SIMPLIFY_NONE |
|---|
| 91 | const uchar *get_simplify_table() const { |
|---|
| 92 | if (simplify_type == AWT_FILTER_SIMPLIFY_NOT_INITIALIZED) { |
|---|
| 93 | // this is no modification, it's lazy initialization: |
|---|
| 94 | const_cast<AP_filter*>(this)->enable_simplify(AWT_FILTER_SIMPLIFY_NONE); |
|---|
| 95 | } |
|---|
| 96 | return simplify; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void enable_bootstrap(); |
|---|
| 100 | bool does_bootstrap() const { return bootstrap; } |
|---|
| 101 | |
|---|
| 102 | size_t bootstrapped_seqpos(size_t bpos) const { |
|---|
| 103 | size_t fpos = bootstrapped_filterpos(bpos); |
|---|
| 104 | size_t spos = (get_filterpos_2_seqpos())[fpos]; |
|---|
| 105 | af_assert(spos<filter_len); |
|---|
| 106 | return spos; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | char *to_string() const; // convert to 0/1 string |
|---|
| 110 | |
|---|
| 111 | char *blowup_string(char *filtered_string, char insert) const; |
|---|
| 112 | |
|---|
| 113 | GB_ERROR is_invalid() const { |
|---|
| 114 | /*! returns error |
|---|
| 115 | * - if filter is based on an empty alignment (i.e. no alignment is selected) |
|---|
| 116 | * - if all positions are filtered out (i.e. filtered sequences will be empty) |
|---|
| 117 | */ |
|---|
| 118 | |
|---|
| 119 | #if defined(ASSERTION_USED) |
|---|
| 120 | checked_for_validity = true; |
|---|
| 121 | #endif |
|---|
| 122 | if (get_filtered_length()) return NULL; |
|---|
| 123 | if (get_length()) return "Sequence completely filtered out (no columns left)"; |
|---|
| 124 | return "No alignment selected"; |
|---|
| 125 | } |
|---|
| 126 | #if defined(ASSERTION_USED) |
|---|
| 127 | bool was_checked_for_validity() const { return checked_for_validity; } |
|---|
| 128 | #endif |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | class AP_weights { |
|---|
| 134 | size_t len; |
|---|
| 135 | GB_UINT4 *weights; |
|---|
| 136 | |
|---|
| 137 | public: |
|---|
| 138 | |
|---|
| 139 | AP_weights(const AP_filter *fil); // dummy weights (all columns weighted equal) |
|---|
| 140 | AP_weights(const GB_UINT4 *w, size_t wlen, const AP_filter *fil); |
|---|
| 141 | AP_weights(const AP_weights& other); |
|---|
| 142 | ~AP_weights(); |
|---|
| 143 | DECLARE_ASSIGNMENT_OPERATOR(AP_weights); |
|---|
| 144 | |
|---|
| 145 | GB_UINT4 weight(size_t idx) const { |
|---|
| 146 | af_assert(idx<len); |
|---|
| 147 | return weights[idx]; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | size_t length() const { return len; } |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | long AP_timer(); |
|---|
| 154 | |
|---|
| 155 | #else |
|---|
| 156 | #error AP_filter.hxx included twice |
|---|
| 157 | #endif // AP_FILTER_HXX |
|---|