source: branches/help/STAT/ST_window.cxx

Last change on this file was 18730, checked in by westram, 3 years ago
  • remove trailing whitespace from c source.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : ST_window.cxx                                      //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Institute of Microbiology (Technical University Munich)        //
7//   http://www.arb-home.de/                                        //
8//                                                                  //
9// ================================================================ //
10
11#include "st_ml.hxx"
12#include "st_quality.hxx"
13
14#include <gui_aliview.hxx>
15#include <ColumnStat.hxx>
16#include <item_sel_list.h>
17
18#include <awt_filter.hxx>
19#include <awt_config_manager.hxx>
20
21#include <aw_awars.hxx>
22#include <aw_root.hxx>
23#include <aw_msg.hxx>
24
25#include <arbdbt.h>
26#include <arb_progress.h>
27#include <arb_global_defs.h>
28#include <arb_strbuf.h>
29
30#define ST_ML_AWAR "tmp/st_ml/"
31
32#define ST_ML_AWAR_COLSTAT_PREFIX ST_ML_AWAR "colstat/"
33#define ST_ML_AWAR_COLSTAT_NAME   ST_ML_AWAR_COLSTAT_PREFIX "name"
34
35#define ST_ML_AWAR_FILTER_PREFIX    ST_ML_AWAR "filter/"
36#define ST_ML_AWAR_FILTER_ALIGNMENT ST_ML_AWAR_FILTER_PREFIX "alignment"
37#define ST_ML_AWAR_FILTER_NAME      ST_ML_AWAR_FILTER_PREFIX "name"
38#define ST_ML_AWAR_FILTER_FILTER    ST_ML_AWAR_FILTER_PREFIX "filter"
39#define ST_ML_AWAR_FILTER_SIMPLIFY  ST_ML_AWAR_FILTER_PREFIX "simplify"
40
41#define ST_ML_AWAR_CQ_BUCKET_SIZE  ST_ML_AWAR "bucket_size"
42#define ST_ML_AWAR_CQ_MARKED_ONLY  ST_ML_AWAR "marked_only"
43#define ST_ML_AWAR_CQ_DEST_FIELD   ST_ML_AWAR "dest_field"
44#define ST_ML_AWAR_CQ_REPORT       ST_ML_AWAR "report"
45#define ST_ML_AWAR_CQ_KEEP_REPORTS ST_ML_AWAR "keep_reports"
46
47static void st_ok_cb(AW_window *aww, ST_ML *st_ml) {
48    AW_root *root           = aww->get_root();
49    char    *alignment_name = root->awar_string(AWAR_DEFAULT_ALIGNMENT, "-none-", st_ml->get_gb_main())->read_string();
50    char    *tree_name      = root->awar_string(AWAR_TREE, "", st_ml->get_gb_main())->read_string();
51    int      marked_only    = root->awar_int(ST_ML_AWAR_CQ_MARKED_ONLY)->read_int();
52
53    GB_ERROR error = GB_push_transaction(st_ml->get_gb_main());
54    if (!error) {
55        error = st_ml->calc_st_ml(tree_name, alignment_name, NULp, marked_only, st_ml->get_column_statistic(), NULp);
56        if (!error) st_ml->do_postcalc_callback();
57    }
58
59    error = GB_end_transaction(st_ml->get_gb_main(), error);
60    aww->hide_or_notify(error);
61
62    free(tree_name);
63    free(alignment_name);
64}
65
66void STAT_set_postcalc_callback(ST_ML *st_ml, WindowCallbackSimple postcalc_cb, AW_window *cb_win) {
67    st_ml->set_postcalc_callback(postcalc_cb, cb_win);
68}
69
70AW_window *STAT_create_main_window(AW_root *root, ST_ML *st_ml) {
71    AW_window_simple *aws = new AW_window_simple;
72    aws->init(root, "COLUMN_STATISTIC", "COLUMN STATISTIC");
73
74    aws->load_xfig("stat_main.fig");
75
76    aws->callback(AW_POPDOWN);
77    aws->at("close");
78    aws->create_button("CLOSE", "CLOSE", "C");
79
80    aws->callback(makeHelpCallback("st_ml.hlp"));
81    aws->at("help");
82    aws->create_button("HELP", "HELP", "H");
83
84    root->awar_string(ST_ML_AWAR_COLSTAT_NAME, "");
85    root->awar_int(ST_ML_AWAR_CQ_MARKED_ONLY, 1);
86
87    AW_awar *awar_default_alignment = root->awar_string(AWAR_DEFAULT_ALIGNMENT, "-none-", st_ml->get_gb_main());
88    root->awar_string(AWAR_TREE, "", st_ml->get_gb_main());
89
90    st_ml->create_column_statistic(root, ST_ML_AWAR_COLSTAT_NAME, awar_default_alignment);
91
92    aws->at("GO");
93    aws->callback(makeWindowCallback(st_ok_cb, st_ml));
94    aws->create_button("GO", "GO", "G");
95
96    aws->at("awt_csp");
97    aws->callback(makeCreateWindowCallback(COLSTAT_create_selection_window, st_ml->get_column_statistic()));
98    aws->button_length(20);
99    aws->create_button("SELECT_CSP", ST_ML_AWAR_COLSTAT_NAME);
100
101    aws->at("marked");
102    aws->create_toggle_field(ST_ML_AWAR_CQ_MARKED_ONLY, "Calculate for ..", "");
103    aws->insert_toggle("All species", "A", 0);
104    aws->insert_toggle("Marked species", "M", 1);
105    aws->update_toggle_field();
106
107    return aws;
108}
109
110ST_ML *STAT_create_ST_ML(GBDATA *gb_main) {
111    return new ST_ML(gb_main);
112}
113void STAT_destroy_ST_ML(ST_ML*& st_ml) {
114    delete st_ml;
115    st_ml = NULp;
116}
117
118ST_ML_Color *STAT_get_color_string(ST_ML *st_ml, char *species_name, AP_tree *node, int start_ali_pos, int end_ali_pos) {
119    return st_ml->get_color_string(species_name, node, start_ali_pos, end_ali_pos);
120}
121
122bool STAT_update_ml_likelihood(ST_ML *st_ml, char *result[4], int& latest_update, const char *species_name, AP_tree *node) {
123    //! @see ST_ML::update_ml_likelihood()
124    return st_ml->update_ml_likelihood(result, latest_update, species_name, node);
125}
126
127AP_tree *STAT_find_node_by_name(ST_ML *st_ml, const char *species_name) {
128    return st_ml->find_node_by_name(species_name);
129}
130
131struct st_check_cb_data : public Noncopyable {
132    GBDATA         *gb_main;
133    ColumnStat     *colstat;
134    WeightedFilter *filter;
135
136    st_check_cb_data(GBDATA *gb_main_, AW_root *root, const char *columnStatAwarName, const char *filterAwarName, AW_awar *awar_default_alignment) {
137        gb_main = gb_main_;
138        colstat = new ColumnStat(gb_main, root, columnStatAwarName, awar_default_alignment);
139        filter  = new WeightedFilter(gb_main, root, filterAwarName, NULp, awar_default_alignment);
140    }
141};
142
143static GB_ERROR st_remove_entries(AW_root *awr, GBDATA *gb_main, bool verbose) {
144    GB_ERROR error = NULp;
145
146    char *ali_name      = awr->awar(ST_ML_AWAR_FILTER_ALIGNMENT)->read_string();
147    char *dest_field    = awr->awar(ST_ML_AWAR_CQ_DEST_FIELD)->read_string();
148    bool  fieldSelected = strcmp(dest_field, NO_FIELD_SELECTED) != 0;
149
150    int fieldsRemoved    = 0;
151    int qualitiesRemoved = 0;
152
153    {
154        GB_transaction ta(gb_main);
155        long count = GBT_get_species_count(gb_main);
156
157        arb_progress progress("Removing old reports", count);
158        for (GBDATA *gb_species = GBT_first_species(gb_main);
159             gb_species && !error;
160             gb_species = GBT_next_species(gb_species))
161        {
162            if (fieldSelected) {
163                GBDATA *gb_field = GB_entry(gb_species, dest_field);
164                if (gb_field) {
165                    error = GB_delete(gb_field);
166                    if (!error) fieldsRemoved++;
167                }
168            }
169            if (!error) {
170                GBDATA *gb_ali = GB_entry(gb_species, ali_name);
171                if (gb_ali) {
172                    GBDATA *gb_quality = GB_entry(gb_ali, "quality");
173                    if (gb_quality) {
174                        error = GB_delete(gb_quality);
175                        if (!error) qualitiesRemoved++;
176                    }
177                }
178            }
179            progress.inc_and_check_user_abort(error);
180        }
181    }
182
183    if (verbose) {
184        GBS_strstruct note(200);
185        note.cat("Removed ");
186        if (fieldsRemoved>0) {
187            note.nprintf(100, "%i '%s'", fieldsRemoved, dest_field);
188            if (qualitiesRemoved>0) note.cat(" and ");
189        }
190        if (qualitiesRemoved>0) note.nprintf(50, "%i 'quality'", qualitiesRemoved);
191        else if (!fieldsRemoved && !qualitiesRemoved) note.cat("no");
192        note.cat(" entries.");
193        aw_message(note.get_data());
194    }
195
196    free(dest_field);
197    free(ali_name);
198
199    return error;
200}
201
202static void st_remove_entries_cb(AW_window *aww, GBDATA *gb_main) {
203    aw_message_if(st_remove_entries(aww->get_root(), gb_main, true));
204}
205
206static void st_check_cb(AW_window *aww, st_check_cb_data *data) {
207    arb_progress   glob_progress("Chimera check");
208    GB_transaction ta(data->gb_main);
209
210    AW_root *awr = aww->get_root();
211
212    char *ali_name    = awr->awar(ST_ML_AWAR_FILTER_ALIGNMENT)->read_string();
213    int   bucket_size = awr->awar(ST_ML_AWAR_CQ_BUCKET_SIZE)->read_int();
214    char *tree_name   = awr->awar(AWAR_TREE)->read_string();
215    int   marked_only = awr->awar(ST_ML_AWAR_CQ_MARKED_ONLY)->read_int();
216
217    st_report_enum report           = (st_report_enum) awr->awar(ST_ML_AWAR_CQ_REPORT)->read_int();
218    bool           keep_old_reports = awr->awar(ST_ML_AWAR_CQ_KEEP_REPORTS)->read_int();
219
220    GB_ERROR    error      = NULp;
221    const char *dest_field = prepare_and_get_selected_itemfield(awr, ST_ML_AWAR_CQ_DEST_FIELD, data->gb_main, SPECIES_get_selector());
222    if (!dest_field) error = GB_await_error();
223
224    if (!error && !keep_old_reports) {
225        error = st_remove_entries(awr, data->gb_main, false);
226    }
227    if (!error) {
228        error = st_ml_check_sequence_quality(data->gb_main, tree_name, ali_name, data->colstat, data->filter, bucket_size, marked_only, report, dest_field);
229    }
230
231    free(ali_name);
232    free(tree_name);
233
234    error = ta.close(error);
235    if (error) aw_message(error);
236}
237
238static void STAT_create_awars(AW_root *root, GBDATA *gb_main) {
239    root->awar_string(AWAR_DEFAULT_ALIGNMENT, "-none-",    gb_main);
240    root->awar_string(AWAR_TREE,              "", gb_main);
241
242    root->awar_string(ST_ML_AWAR_COLSTAT_NAME, "none");
243
244    root->awar_string(ST_ML_AWAR_FILTER_ALIGNMENT, "none");
245    root->awar_string(ST_ML_AWAR_FILTER_NAME,      "none");
246    root->awar_string(ST_ML_AWAR_FILTER_FILTER,    "");
247    root->awar_int   (ST_ML_AWAR_FILTER_SIMPLIFY,  0);
248
249    root->awar_int   (ST_ML_AWAR_CQ_BUCKET_SIZE,  300);
250    root->awar_int   (ST_ML_AWAR_CQ_MARKED_ONLY,  0);
251    root->awar_string(ST_ML_AWAR_CQ_DEST_FIELD,   "tmp");
252    root->awar_int   (ST_ML_AWAR_CQ_REPORT,       0);
253    root->awar_int   (ST_ML_AWAR_CQ_KEEP_REPORTS, 0);
254
255    root->awar_string(ST_ML_AWAR_FILTER_ALIGNMENT)->map(AWAR_DEFAULT_ALIGNMENT);
256}
257
258static AWT_config_mapping_def chimera_config_mapping[] = {
259    { ST_ML_AWAR_CQ_MARKED_ONLY,  "marked_only" },
260    { ST_ML_AWAR_COLSTAT_NAME,    "colstat" },
261    { ST_ML_AWAR_FILTER_NAME,     "filter" },
262    { ST_ML_AWAR_CQ_BUCKET_SIZE,  "bucketsize" },
263    { ST_ML_AWAR_CQ_DEST_FIELD,   "destfield" }, // no need to store field-type here, because it is always GB_STRING
264    { ST_ML_AWAR_CQ_REPORT,       "report" },
265    { ST_ML_AWAR_CQ_KEEP_REPORTS, "keepold" },
266
267    { NULp, NULp }
268};
269
270AW_window *STAT_create_chimera_check_window(AW_root *root, GBDATA *gb_main) {
271    static AW_window_simple *aws = NULp;
272    if (!aws) {
273        aws = new AW_window_simple;
274        aws->init(root, "CHIMERA_CHECK", "Chimera Check of marked sequences");
275        aws->load_xfig("chimera_check.fig");
276
277        STAT_create_awars(root, gb_main);
278
279        aws->callback(AW_POPDOWN);
280        aws->at("close");
281        aws->create_button("CLOSE", "CLOSE", "C");
282
283        aws->callback(makeHelpCallback("chimera_check.hlp"));
284        aws->at("help");
285        aws->create_button("HELP", "HELP", "H");
286
287        AW_awar *awar_default_alignment = root->awar_string(AWAR_DEFAULT_ALIGNMENT, "-none-", gb_main);
288        st_check_cb_data *cb_data = new st_check_cb_data(gb_main, root, ST_ML_AWAR_COLSTAT_NAME, ST_ML_AWAR_FILTER_NAME, awar_default_alignment); // bound to cb (not freed)
289
290        aws->at("which");
291        {
292            aws->create_option_menu(ST_ML_AWAR_CQ_MARKED_ONLY, true);
293            aws->insert_option("All in tree", "t", 0);
294            aws->insert_option("Only marked and in tree", "m", 1);
295            aws->update_option_menu();
296        }
297
298        aws->at("colstat");
299        aws->callback(makeCreateWindowCallback(COLSTAT_create_selection_window, cb_data->colstat));
300        aws->create_button("SELECT_CSP", ST_ML_AWAR_COLSTAT_NAME);
301
302
303        aws->at("filter");
304        aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, cb_data->filter->get_adfiltercbstruct()));
305        aws->create_button("SELECT_FILTER", ST_ML_AWAR_FILTER_NAME);
306
307        aws->at("sb");
308        aws->create_input_field(ST_ML_AWAR_CQ_BUCKET_SIZE);
309
310        create_itemfield_selection_button(aws, FieldSelDef(ST_ML_AWAR_CQ_DEST_FIELD, gb_main, SPECIES_get_selector(), FIELD_FILTER_STRING_WRITEABLE, "report-field", SF_ALLOW_NEW), "dest");
311
312        aws->at("report");
313        {
314            aws->create_option_menu(ST_ML_AWAR_CQ_REPORT, true);
315            aws->insert_option("No", "N", 0);
316            aws->insert_option("to temporary entry", "t", 1);
317            aws->insert_option("to permanent entry", "p", 2);
318            aws->update_option_menu();
319        }
320
321        aws->at("keep");
322        aws->create_toggle(ST_ML_AWAR_CQ_KEEP_REPORTS);
323
324        aws->at("del");
325        aws->callback(makeWindowCallback(st_remove_entries_cb, gb_main));
326        aws->create_button("DEL_ENTRIES", "Remove them now!", "R");
327
328        aws->button_length(10);
329        aws->at("GO");
330        aws->callback(makeWindowCallback(st_check_cb, cb_data));
331        aws->create_button("GO", "GO", "G");
332
333        aws->at("config");
334        AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "chimera", chimera_config_mapping);
335    }
336    return aws;
337}
Note: See TracBrowser for help on using the repository browser.