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