| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : di_matr.hxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #ifndef DI_MATR_HXX |
|---|
| 12 | #define DI_MATR_HXX |
|---|
| 13 | |
|---|
| 14 | #ifndef AP_PRO_A_NUCS_HXX |
|---|
| 15 | #include <AP_pro_a_nucs.hxx> |
|---|
| 16 | #endif |
|---|
| 17 | #ifndef AP_TREE_HXX |
|---|
| 18 | #include <AP_Tree.hxx> |
|---|
| 19 | #endif |
|---|
| 20 | #ifndef AP_MATRIX_HXX |
|---|
| 21 | #include <AP_matrix.hxx> |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | #define di_assert(cond) arb_assert(cond) |
|---|
| 25 | |
|---|
| 26 | #define AWAR_DIST_PREFIX "dist/" |
|---|
| 27 | #define AWAR_DIST_CORR_TRANS AWAR_DIST_PREFIX "correction/trans" |
|---|
| 28 | #define AWAR_DIST_SAVE_MATRIX_BASE "tmp/" AWAR_DIST_PREFIX "save_matrix" |
|---|
| 29 | |
|---|
| 30 | #define AWAR_DIST_DIST_PREFIX AWAR_DIST_PREFIX "dist/" |
|---|
| 31 | #define AWAR_DIST_MIN_DIST AWAR_DIST_DIST_PREFIX "lower" |
|---|
| 32 | #define AWAR_DIST_MAX_DIST AWAR_DIST_DIST_PREFIX "upper" |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | enum DI_TRANSFORMATION { |
|---|
| 36 | DI_TRANSFORMATION_NONE, |
|---|
| 37 | DI_TRANSFORMATION_SIMILARITY, |
|---|
| 38 | DI_TRANSFORMATION_JUKES_CANTOR, |
|---|
| 39 | DI_TRANSFORMATION_FELSENSTEIN, |
|---|
| 40 | |
|---|
| 41 | DI_TRANSFORMATION_PAM, |
|---|
| 42 | DI_TRANSFORMATION_CATEGORIES_HALL, |
|---|
| 43 | DI_TRANSFORMATION_CATEGORIES_BARKER, |
|---|
| 44 | DI_TRANSFORMATION_CATEGORIES_CHEMICAL, |
|---|
| 45 | |
|---|
| 46 | DI_TRANSFORMATION_HAESCH, |
|---|
| 47 | DI_TRANSFORMATION_KIMURA, |
|---|
| 48 | DI_TRANSFORMATION_OLSEN, |
|---|
| 49 | DI_TRANSFORMATION_FELSENSTEIN_VOIGT, |
|---|
| 50 | DI_TRANSFORMATION_OLSEN_VOIGT, |
|---|
| 51 | DI_TRANSFORMATION_ML |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | enum DI_MATRIX_TYPE { |
|---|
| 55 | DI_MATRIX_FULL, |
|---|
| 56 | DI_MATRIX_COMPRESSED |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | class DI_MATRIX; |
|---|
| 60 | class AW_root; |
|---|
| 61 | class AP_sequence_parsimony; |
|---|
| 62 | class AP_sequence_simple_protein; |
|---|
| 63 | |
|---|
| 64 | class DI_ENTRY : virtual Noncopyable { |
|---|
| 65 | public: |
|---|
| 66 | DI_ENTRY(GBDATA *gbd, class DI_MATRIX *phmatri); |
|---|
| 67 | DI_ENTRY(char *namei, class DI_MATRIX *phmatri); |
|---|
| 68 | ~DI_ENTRY(); |
|---|
| 69 | |
|---|
| 70 | DI_MATRIX *phmatrix; |
|---|
| 71 | AP_sequence *sequence; |
|---|
| 72 | AP_sequence_parsimony *sequence_parsimony; // if exist ok |
|---|
| 73 | AP_sequence_simple_protein *sequence_protein; |
|---|
| 74 | long seq_len; |
|---|
| 75 | char *name; |
|---|
| 76 | char *full_name; |
|---|
| 77 | AP_FLOAT gc_bias; |
|---|
| 78 | int group_nr; // species belongs to group number xxxx |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | typedef long DI_MUT_MATR[AP_MAX][AP_MAX]; |
|---|
| 82 | |
|---|
| 83 | enum DI_SAVE_TYPE { |
|---|
| 84 | DI_SAVE_PHYLIP_COMP, |
|---|
| 85 | DI_SAVE_READABLE, |
|---|
| 86 | DI_SAVE_TABBED |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | class BI_helix; |
|---|
| 90 | |
|---|
| 91 | enum LoadWhat { DI_LOAD_ALL, DI_LOAD_MARKED, DI_LOAD_LIST }; |
|---|
| 92 | |
|---|
| 93 | class MatrixOrder { |
|---|
| 94 | GB_HASH *name2pos; // key = species name, value = order in sort_tree [1..n] |
|---|
| 95 | // if no sort tree was specified, name2pos is NULL |
|---|
| 96 | int leafs; // number of leafs |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | void insert_in_hash(GBT_TREE *tree) { |
|---|
| 100 | if (tree->is_leaf) { |
|---|
| 101 | arb_assert(tree->name); |
|---|
| 102 | ASSERT_RESULT(long, 0, GBS_write_hash(name2pos, tree->name, ++leafs)); |
|---|
| 103 | } |
|---|
| 104 | else { |
|---|
| 105 | insert_in_hash(tree->rightson); |
|---|
| 106 | insert_in_hash(tree->leftson); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public: |
|---|
| 111 | MatrixOrder(GBDATA *gb_main, GB_CSTR sort_tree_name); |
|---|
| 112 | |
|---|
| 113 | bool defined() const { return leafs; } |
|---|
| 114 | int get_size() const { return leafs; } |
|---|
| 115 | |
|---|
| 116 | int get_index(const char *name) const { |
|---|
| 117 | // return 1 for lowest and 'leafs' for highest species in sort-tee |
|---|
| 118 | // return 0 for all species missing in sort-tree |
|---|
| 119 | return defined() ? GBS_read_hash(name2pos, name) : -1; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void applyTo(class TreeOrderedSpecies **gb_species_array, size_t array_size) const; |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | typedef void (*DI_MATRIX_CB)(); |
|---|
| 126 | |
|---|
| 127 | class DI_MATRIX : virtual Noncopyable { |
|---|
| 128 | friend class DI_ENTRY; |
|---|
| 129 | |
|---|
| 130 | GBDATA *gb_species_data; |
|---|
| 131 | long seq_len; |
|---|
| 132 | char cancel_columns[256]; |
|---|
| 133 | AW_root *aw_root; // only link |
|---|
| 134 | long entries_mem_size; |
|---|
| 135 | AliView *aliview; |
|---|
| 136 | |
|---|
| 137 | public: |
|---|
| 138 | bool is_AA; |
|---|
| 139 | DI_ENTRY **entries; |
|---|
| 140 | long nentries; |
|---|
| 141 | AP_smatrix *matrix; |
|---|
| 142 | DI_MATRIX_TYPE matrix_type; |
|---|
| 143 | |
|---|
| 144 | DI_MATRIX(const AliView& aliview, AW_root *awr); |
|---|
| 145 | ~DI_MATRIX(); |
|---|
| 146 | |
|---|
| 147 | GBDATA *get_gb_main() const { return aliview->get_gb_main(); } |
|---|
| 148 | const char *get_aliname() const { return aliview->get_aliname(); } |
|---|
| 149 | const AliView *get_aliview() const { return aliview; } |
|---|
| 150 | |
|---|
| 151 | char *load(LoadWhat what, const MatrixOrder& order, bool show_warnings, GBDATA **species_list); |
|---|
| 152 | char *unload(); |
|---|
| 153 | const char *save(char *filename, enum DI_SAVE_TYPE type); |
|---|
| 154 | |
|---|
| 155 | void clear(DI_MUT_MATR &hits); |
|---|
| 156 | void make_sym(DI_MUT_MATR &hits); |
|---|
| 157 | void rate_write(DI_MUT_MATR &hits, FILE *out); |
|---|
| 158 | long *create_helix_filter(BI_helix *helix, const AP_filter *filter); |
|---|
| 159 | // 0 non helix 1 helix; compressed filter |
|---|
| 160 | GB_ERROR calculate_rates(DI_MUT_MATR &hrates, DI_MUT_MATR &nrates, DI_MUT_MATR &pairs, long *filter); |
|---|
| 161 | GB_ERROR haeschoe(const char *path); |
|---|
| 162 | double corr(double dist, double b, double & sigma); |
|---|
| 163 | GB_ERROR calculate(AW_root *awr, char *cancel, double alpha, DI_TRANSFORMATION transformation, bool *aborted_flag); |
|---|
| 164 | char *calculate_overall_freqs(double rel_frequencies[AP_MAX], char *cancel_columns); |
|---|
| 165 | GB_ERROR calculate_pro(DI_TRANSFORMATION transformation, bool *aborted_flag); |
|---|
| 166 | void analyse(); |
|---|
| 167 | |
|---|
| 168 | int search_group(GBT_TREE *node, GB_HASH *hash, long *groupcnt, char *groupname, DI_ENTRY **groups); // @@ OLIVER |
|---|
| 169 | char *compress(GBT_TREE *tree); |
|---|
| 170 | }; |
|---|
| 171 | |
|---|
| 172 | class DI_GLOBAL_MATRIX : virtual Noncopyable { |
|---|
| 173 | DI_MATRIX *matrix; |
|---|
| 174 | DI_MATRIX_CB changed_cb; |
|---|
| 175 | |
|---|
| 176 | void announce_change() { if (changed_cb) changed_cb(); } |
|---|
| 177 | |
|---|
| 178 | public: |
|---|
| 179 | DI_GLOBAL_MATRIX() : matrix(NULL), changed_cb(NULL) {} |
|---|
| 180 | ~DI_GLOBAL_MATRIX() { forget(); } |
|---|
| 181 | |
|---|
| 182 | DI_MATRIX *get() { return matrix; } |
|---|
| 183 | void set(DI_MATRIX *new_global) { di_assert(!matrix); matrix = new_global; announce_change(); } |
|---|
| 184 | void forget() { delete matrix; matrix = NULL; announce_change(); } |
|---|
| 185 | void replaceBy(DI_MATRIX *new_global) { forget(); set(new_global); } |
|---|
| 186 | |
|---|
| 187 | bool exists() const { return matrix != NULL; } |
|---|
| 188 | |
|---|
| 189 | void set_changed_cb(DI_MATRIX_CB cb) { |
|---|
| 190 | announce_change(); |
|---|
| 191 | changed_cb = cb; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | DI_MATRIX *swap(DI_MATRIX *other) { |
|---|
| 195 | DI_MATRIX *prev = matrix; |
|---|
| 196 | matrix = other; |
|---|
| 197 | announce_change(); |
|---|
| 198 | return prev; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | void forget_if_not_has_type(DI_MATRIX_TYPE wanted_type) { |
|---|
| 202 | if (matrix && matrix->matrix_type != wanted_type) { |
|---|
| 203 | forget(); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | }; |
|---|
| 207 | |
|---|
| 208 | extern DI_GLOBAL_MATRIX GLOBAL_MATRIX; |
|---|
| 209 | |
|---|
| 210 | class WeightedFilter; |
|---|
| 211 | struct save_matrix_params { |
|---|
| 212 | const char *awar_base; |
|---|
| 213 | const WeightedFilter *weighted_filter; |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | AW_window *DI_create_save_matrix_window(AW_root *aw_root, save_matrix_params *save_params); |
|---|
| 217 | |
|---|
| 218 | #else |
|---|
| 219 | #error di_matr.hxx included twice |
|---|
| 220 | #endif // DI_MATR_HXX |
|---|