| 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 | enum DI_TRANSFORMATION { |
|---|
| 35 | DI_TRANSFORMATION_NONE, |
|---|
| 36 | DI_TRANSFORMATION_SIMILARITY, |
|---|
| 37 | DI_TRANSFORMATION_JUKES_CANTOR, |
|---|
| 38 | DI_TRANSFORMATION_FELSENSTEIN, |
|---|
| 39 | |
|---|
| 40 | DI_TRANSFORMATION_PAM, |
|---|
| 41 | DI_TRANSFORMATION_CATEGORIES_HALL, |
|---|
| 42 | DI_TRANSFORMATION_CATEGORIES_BARKER, |
|---|
| 43 | DI_TRANSFORMATION_CATEGORIES_CHEMICAL, |
|---|
| 44 | |
|---|
| 45 | DI_TRANSFORMATION_HAESCH, |
|---|
| 46 | DI_TRANSFORMATION_KIMURA, |
|---|
| 47 | DI_TRANSFORMATION_OLSEN, |
|---|
| 48 | DI_TRANSFORMATION_FELSENSTEIN_VOIGT, |
|---|
| 49 | DI_TRANSFORMATION_OLSEN_VOIGT, |
|---|
| 50 | DI_TRANSFORMATION_ML, |
|---|
| 51 | |
|---|
| 52 | DI_TRANSFORMATION_FROM_TREE, |
|---|
| 53 | |
|---|
| 54 | DI_TRANSFORMATION_COUNT, // has to be last |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | enum DI_MATRIX_TYPE { |
|---|
| 58 | DI_MATRIX_FULL, |
|---|
| 59 | DI_MATRIX_COMPRESSED |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | class DI_MATRIX; |
|---|
| 63 | class AW_root; |
|---|
| 64 | class AP_sequence_parsimony; |
|---|
| 65 | class AP_sequence_simple_protein; |
|---|
| 66 | |
|---|
| 67 | struct DI_ENTRY : virtual Noncopyable { |
|---|
| 68 | DI_ENTRY(GBDATA *gbd, class DI_MATRIX *phmatri); |
|---|
| 69 | DI_ENTRY(char *namei, class DI_MATRIX *phmatri); |
|---|
| 70 | ~DI_ENTRY(); |
|---|
| 71 | |
|---|
| 72 | DI_MATRIX *phmatrix; |
|---|
| 73 | AP_sequence *sequence; |
|---|
| 74 | AP_sequence_parsimony *sequence_parsimony; // if exist ok |
|---|
| 75 | AP_sequence_simple_protein *sequence_protein; |
|---|
| 76 | long seq_len; |
|---|
| 77 | char *name; |
|---|
| 78 | char *full_name; |
|---|
| 79 | AP_FLOAT gc_bias; |
|---|
| 80 | int group_nr; // species belongs to group number xxxx |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | typedef long DI_MUT_MATR[AP_MAX][AP_MAX]; |
|---|
| 84 | |
|---|
| 85 | enum DI_SAVE_TYPE { |
|---|
| 86 | DI_SAVE_PHYLIP_COMP, |
|---|
| 87 | DI_SAVE_READABLE, |
|---|
| 88 | DI_SAVE_TABBED |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | class BI_helix; |
|---|
| 92 | |
|---|
| 93 | enum LoadWhat { DI_LOAD_ALL, DI_LOAD_MARKED, DI_LOAD_LIST }; |
|---|
| 94 | |
|---|
| 95 | class MatrixOrder : virtual Noncopyable { |
|---|
| 96 | GB_HASH *name2pos; // key = species name, value = order in sort_tree [1..n] |
|---|
| 97 | // if no sort tree was specified, name2pos is NULL |
|---|
| 98 | int leafs; // number of leafs |
|---|
| 99 | |
|---|
| 100 | bool tree_contains_dups; // unused (if true, matrix sorting works partly wrong) |
|---|
| 101 | |
|---|
| 102 | void insert_in_hash(GBT_TREE *tree) { |
|---|
| 103 | if (tree->is_leaf) { |
|---|
| 104 | arb_assert(tree->name); |
|---|
| 105 | if (GBS_write_hash(name2pos, tree->name, ++leafs) != 0) { |
|---|
| 106 | tree_contains_dups = true; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | else { |
|---|
| 110 | insert_in_hash(tree->rightson); |
|---|
| 111 | insert_in_hash(tree->leftson); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | public: |
|---|
| 116 | MatrixOrder(GBDATA *gb_main, GB_CSTR sort_tree_name); |
|---|
| 117 | ~MatrixOrder() { if (name2pos) GBS_free_hash(name2pos); } |
|---|
| 118 | |
|---|
| 119 | bool defined() const { return leafs; } |
|---|
| 120 | int get_size() const { return leafs; } |
|---|
| 121 | |
|---|
| 122 | int get_index(const char *name) const { |
|---|
| 123 | // return 1 for lowest and 'leafs' for highest species in sort-tee |
|---|
| 124 | // return 0 for all species missing in sort-tree |
|---|
| 125 | return defined() ? GBS_read_hash(name2pos, name) : -1; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void applyTo(struct TreeOrderedSpecies **gb_species_array, size_t array_size) const; |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | typedef void (*DI_MATRIX_CB)(); |
|---|
| 132 | |
|---|
| 133 | class DI_MATRIX : virtual Noncopyable { |
|---|
| 134 | friend class DI_ENTRY; |
|---|
| 135 | |
|---|
| 136 | GBDATA *gb_species_data; |
|---|
| 137 | long seq_len; |
|---|
| 138 | char cancel_columns[256]; |
|---|
| 139 | AW_root *aw_root; // only link |
|---|
| 140 | size_t entries_mem_size; |
|---|
| 141 | AliView *aliview; |
|---|
| 142 | |
|---|
| 143 | public: |
|---|
| 144 | // @@@ make members private: |
|---|
| 145 | bool is_AA; |
|---|
| 146 | DI_ENTRY **entries; |
|---|
| 147 | size_t nentries; |
|---|
| 148 | AP_smatrix *matrix; |
|---|
| 149 | DI_MATRIX_TYPE matrix_type; |
|---|
| 150 | |
|---|
| 151 | DI_MATRIX(const AliView& aliview, AW_root *awr); |
|---|
| 152 | ~DI_MATRIX(); |
|---|
| 153 | |
|---|
| 154 | GBDATA *get_gb_main() const { return aliview->get_gb_main(); } |
|---|
| 155 | const char *get_aliname() const { return aliview->get_aliname(); } |
|---|
| 156 | const AliView *get_aliview() const { return aliview; } |
|---|
| 157 | |
|---|
| 158 | GB_ERROR load(LoadWhat what, const MatrixOrder& order, bool show_warnings, GBDATA **species_list) __ATTR__USERESULT; |
|---|
| 159 | char *unload(); |
|---|
| 160 | const char *save(char *filename, enum DI_SAVE_TYPE type); |
|---|
| 161 | |
|---|
| 162 | void clear(DI_MUT_MATR &hits); |
|---|
| 163 | void make_sym(DI_MUT_MATR &hits); |
|---|
| 164 | void rate_write(DI_MUT_MATR &hits, FILE *out); |
|---|
| 165 | long *create_helix_filter(BI_helix *helix, const AP_filter *filter); |
|---|
| 166 | // 0 non helix 1 helix; compressed filter |
|---|
| 167 | GB_ERROR calculate_rates(DI_MUT_MATR &hrates, DI_MUT_MATR &nrates, DI_MUT_MATR &pairs, long *filter); |
|---|
| 168 | GB_ERROR haeschoe(const char *path); |
|---|
| 169 | double corr(double dist, double b, double & sigma); |
|---|
| 170 | |
|---|
| 171 | GB_ERROR calculate(AW_root *awr, char *cancel, double alpha, DI_TRANSFORMATION transformation, bool *aborted_flag); |
|---|
| 172 | char *calculate_overall_freqs(double rel_frequencies[AP_MAX], char *cancel_columns); |
|---|
| 173 | GB_ERROR calculate_pro(DI_TRANSFORMATION transformation, bool *aborted_flag); |
|---|
| 174 | GB_ERROR extract_from_tree(const char *treename, bool *aborted_flag); |
|---|
| 175 | |
|---|
| 176 | void analyse(); |
|---|
| 177 | |
|---|
| 178 | int search_group(GBT_TREE *node, GB_HASH *hash, size_t& groupcnt, char *groupname, DI_ENTRY **groups); |
|---|
| 179 | char *compress(GBT_TREE *tree); |
|---|
| 180 | }; |
|---|
| 181 | |
|---|
| 182 | class DI_GLOBAL_MATRIX : virtual Noncopyable { |
|---|
| 183 | DI_MATRIX *matrix; |
|---|
| 184 | DI_MATRIX_CB changed_cb; |
|---|
| 185 | |
|---|
| 186 | void announce_change() { if (changed_cb) changed_cb(); } |
|---|
| 187 | |
|---|
| 188 | void forget_no_announce() { |
|---|
| 189 | delete matrix; |
|---|
| 190 | matrix = NULL; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | public: |
|---|
| 194 | DI_GLOBAL_MATRIX() : matrix(NULL), changed_cb(NULL) {} |
|---|
| 195 | ~DI_GLOBAL_MATRIX() { forget(); } |
|---|
| 196 | |
|---|
| 197 | DI_MATRIX *get() { return matrix; } |
|---|
| 198 | void set(DI_MATRIX *new_global) { di_assert(!matrix); matrix = new_global; announce_change(); } |
|---|
| 199 | void forget() { |
|---|
| 200 | if (matrix) { |
|---|
| 201 | forget_no_announce(); |
|---|
| 202 | announce_change(); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|
| 205 | void replaceBy(DI_MATRIX *new_global) { forget_no_announce(); set(new_global); } |
|---|
| 206 | |
|---|
| 207 | bool exists() const { return matrix != NULL; } |
|---|
| 208 | |
|---|
| 209 | void set_changed_cb(DI_MATRIX_CB cb) { |
|---|
| 210 | // announce_change(); // do by caller if really needed |
|---|
| 211 | changed_cb = cb; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | DI_MATRIX *swap(DI_MATRIX *other) { |
|---|
| 215 | DI_MATRIX *prev = matrix; |
|---|
| 216 | matrix = other; |
|---|
| 217 | announce_change(); |
|---|
| 218 | return prev; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | bool has_type(DI_MATRIX_TYPE type) const { |
|---|
| 222 | return matrix && matrix->matrix_type == type; |
|---|
| 223 | } |
|---|
| 224 | void forget_if_not_has_type(DI_MATRIX_TYPE wanted_type) { |
|---|
| 225 | if (matrix && matrix->matrix_type != wanted_type) { |
|---|
| 226 | forget(); |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | }; |
|---|
| 230 | |
|---|
| 231 | extern DI_GLOBAL_MATRIX GLOBAL_MATRIX; |
|---|
| 232 | |
|---|
| 233 | class WeightedFilter; |
|---|
| 234 | struct save_matrix_params { |
|---|
| 235 | const char *awar_base; |
|---|
| 236 | const WeightedFilter *weighted_filter; |
|---|
| 237 | }; |
|---|
| 238 | |
|---|
| 239 | AW_window *DI_create_save_matrix_window(AW_root *aw_root, save_matrix_params *save_params); |
|---|
| 240 | |
|---|
| 241 | #else |
|---|
| 242 | #error di_matr.hxx included twice |
|---|
| 243 | #endif // DI_MATR_HXX |
|---|