source: tags/arb-6.0.4/STAT/st_ml.hxx

Last change on this file was 10221, checked in by westram, 11 years ago
  • removed duplicated callback typedefs
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : st_ml.hxx                                         //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#ifndef ST_ML_HXX
12#define ST_ML_HXX
13
14#ifndef ARBDB_BASE_H
15#include <arbdb_base.h>
16#endif
17#ifndef _GLIBCXX_ALGORITHM
18#include <algorithm>
19#endif
20#ifndef ATTRIBUTES_H
21#include <attributes.h>
22#endif
23#ifndef ARBTOOLS_H
24#include <arbtools.h>
25#endif
26#ifndef CB_H
27#include <cb.h>
28#endif
29
30#define st_assert(cond) arb_assert(cond)
31
32class ColumnStat;
33class MostLikelySeq;
34
35class AW_root;
36class AW_awar;
37class AW_window;
38
39class AP_tree;
40class AP_tree_root;
41class GBT_TREE;
42class AP_filter;
43class WeightedFilter;
44
45typedef unsigned char ST_ML_Color;
46
47typedef float ST_FLOAT;
48// typedef double ST_FLOAT; // careful, using double has quite an impact on the memory footprint
49
50enum DNA_Base {
51    ST_A,
52    ST_C,
53    ST_G,
54    ST_T,
55    ST_GAP,
56    ST_MAX_BASE,                                    // [can't be changed w/o recoding several parts]
57    ST_UNKNOWN = -1
58};
59
60struct ST_base_vector {
61    ST_FLOAT b[ST_MAX_BASE];                        // acgt-
62    int      ld_lik;
63    ST_FLOAT lik;                                   // likelihood = 2^ld_lik * lik * (b[0] + b[1] + b[2] ..)
64
65    void setTo(ST_FLOAT freq) {
66        for (int base = ST_A; base<ST_MAX_BASE; ++base) {
67            b[base] = freq;
68        }
69    }
70    void multiplyWith(ST_FLOAT factor) {
71        for (int base = ST_A; base<ST_MAX_BASE; ++base) {
72            b[base] *= factor;
73        }
74    }
75    void increaseBy(ST_FLOAT inc) {
76        for (int base = ST_A; base<ST_MAX_BASE; ++base) {
77            b[base] += inc;
78        }
79    }
80    void makeInverseOf(const ST_base_vector& other, ST_FLOAT other_min_freq) {
81        for (int base = ST_A; base<ST_MAX_BASE; ++base) {
82            b[base] = other_min_freq / other.b[base];
83        }
84    }
85
86    void setBase(const ST_base_vector& frequencies, char base);
87
88    void check_overflow();
89    void print();
90
91    ST_FLOAT summarize() const { return b[ST_A] + b[ST_C] + b[ST_G] + b[ST_T] + b[ST_GAP]; }
92    ST_FLOAT min_frequency() { return std::min(std::min(std::min(b[ST_A], b[ST_C]), std::min(b[ST_G], b[ST_T])), b[ST_GAP]); }
93    ST_FLOAT max_frequency() { return std::max(std::max(std::max(b[ST_A], b[ST_C]), std::max(b[ST_G], b[ST_T])), b[ST_GAP]); }
94
95    inline ST_base_vector& operator *= (const ST_base_vector& other);
96};
97
98class ST_rate_matrix {
99    // symmetric matrix containing only two different values
100
101    ST_FLOAT diag;                                  // value for indices [0][0], [1][1]...
102    ST_FLOAT rest;                                  // other indices
103
104public:
105
106    ST_rate_matrix() : diag(0.0), rest(0.0) {}
107    void set(double dist, double TT_ratio);
108    inline void transform(const ST_base_vector& in, ST_base_vector& out) const;
109    void print();
110};
111
112class ST_ML : virtual Noncopyable {
113
114    /* Note: Because we have only limited memory we split the
115     * sequence into several ST_MAX_SEQ_PART-sized parts during stat-calculation
116     */
117
118    char    *alignment_name;
119    GB_HASH *hash_2_ap_tree;                        // hash table to get from name to tree_node
120    GB_HASH *keep_species_hash;                     // temporary hash to find ???
121    int      refresh_n;
122    int     *not_valid;                             // which columns are valid
123
124    AP_tree_root *tree_root;
125    int           latest_modification;              // last mod
126    size_t        first_pos;                        // first..
127    size_t        last_pos;                         // .. and last alignment position of current slice
128
129    AW_CB0     postcalc_cb;
130    AW_window *cb_window;
131
132    GBDATA *gb_main;
133
134    ColumnStat *column_stat;
135    // if column_stat == 0 -> rates and ttratio are owned by ST_ML,
136    // otherwise they belong to column_stat
137    const float *rates;                             // column independent
138    const float *ttratio;                           // column independent
139
140    ST_base_vector *base_frequencies;               // column independent
141    ST_base_vector *inv_base_frequencies;           // column independent
142
143    double max_dist;                                // max_dist for rate_matrices
144    double step_size;                               // max_dist/step_size matrices
145
146    int             max_rate_matrices;              // number of rate_matrices
147    ST_rate_matrix *rate_matrices;                  // one matrix for each distance
148
149    bool is_initialized;
150
151    size_t distance_2_matrices_index(int distance) {
152        if (distance<0) distance = -distance;       // same matrix for neg/pos distance
153
154        return distance >= max_rate_matrices
155            ? (max_rate_matrices-1)
156            : size_t(distance);
157    }
158
159    MostLikelySeq *getOrCreate_seq(AP_tree *node);
160
161    const MostLikelySeq *get_mostlikely_sequence(AP_tree *node);
162    void                 undo_tree(AP_tree *node);  // opposite of get_mostlikely_sequence()
163
164    void insert_tree_into_hash_rek(AP_tree *node);
165    void create_matrices(double max_disti, int nmatrices);
166    void create_frequencies();
167   
168    static long delete_species(const char *key, long val, void *cd_st_ml);
169
170public:
171
172    ST_ML(GBDATA *gb_main);
173    ~ST_ML();
174
175    void create_column_statistic(AW_root *awr, const char *awarname, AW_awar *awar_default_alignment);
176    ColumnStat *get_column_statistic() { return column_stat; }
177
178    GB_ERROR calc_st_ml(const char           *tree_name,
179                        const char           *alignment_name,
180                        const char           *species_names, // 0 -> all [marked] species (else species_names is a (char)1 separated list of species)
181                        int                   marked_only,
182                        ColumnStat           *colstat,
183                        const WeightedFilter *weighted_filter) __ATTR__USERESULT;
184
185    void cleanup();
186
187    const AP_filter *get_filter() const;
188    size_t get_filtered_length() const;
189    size_t get_alignment_length() const;
190
191    ST_rate_matrix& get_matrix_for(int distance) {
192        return rate_matrices[distance_2_matrices_index(distance)];
193    }
194
195    GBDATA *get_gb_main() const { return gb_main; }
196    const GBT_TREE *get_gbt_tree() const;
197    size_t count_species_in_tree() const;
198
199    AP_tree *find_node_by_name(const char *species_name);
200
201    void set_postcalc_callback(AW_CB0 postcalc_cb_, AW_window *cb_window_) {
202        postcalc_cb = postcalc_cb_;
203        cb_window   = cb_window_;
204    }
205    void do_postcalc_callback() { if (postcalc_cb) postcalc_cb(cb_window); }
206
207    size_t get_first_pos() const { return first_pos; }
208    size_t get_last_pos() const { return last_pos; }
209
210    double get_step_size() const { return step_size; }
211
212    const ST_base_vector *get_inv_base_frequencies() const { return inv_base_frequencies; }
213    const ST_base_vector& get_base_frequency_at(size_t pos) const { return base_frequencies[pos]; }
214    float get_rate_at(size_t pos) const { return rates[pos]; }
215
216    void set_modified(int *what = 0);
217    void set_refresh();                             // set flag for refresh
218
219    void clear_all();                               // delete all caches
220
221    MostLikelySeq *get_ml_vectors(const char *species_name, AP_tree *node, size_t start_ali_pos, size_t end_ali_pos);
222    ST_ML_Color *get_color_string(const char *species_name, AP_tree *node, size_t start_ali_pos, size_t end_ali_pos);
223
224    bool update_ml_likelihood(char *result[4], int& latest_update, const char *species_name, AP_tree *node);
225
226    int refresh_needed();
227};
228
229#else
230#error st_ml.hxx included twice
231#endif // ST_ML_HXX
Note: See TracBrowser for help on using the repository browser.