source: tags/ms_r16q2/SEQ_QUALITY/SQ_main.cxx

Last change on this file was 14453, checked in by westram, 10 years ago
  • do not cast AW_POPDOWN
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
Line 
1//  ==================================================================== //
2//                                                                       //
3//    File      : SQ_main.cxx                                            //
4//    Purpose   : Entrypoint to Seq. Quality analysis; calls functions   //
5//                                                                       //
6//                                                                       //
7//  Coded by Juergen Huber in July 2003 - February 2004                  //
8//  Coded by Kai Bader (baderk@in.tum.de) in 2007 - 2008                 //
9//  Copyright Department of Microbiology (Technical University Munich)   //
10//                                                                       //
11//  Visit our web site at: http://www.arb-home.de/                       //
12//                                                                       //
13//  ==================================================================== //
14
15#include "seq_quality.h"
16#include "SQ_functions.h"
17
18#include <awt_filter.hxx>
19#include <awt_sel_boxes.hxx>
20
21#include <aw_awars.hxx>
22#include <aw_msg.hxx>
23#include <aw_root.hxx>
24
25#include <arb_progress.h>
26#include <TreeNode.h>
27#include <arb_global_defs.h>
28#include <awt_config_manager.hxx>
29
30// --------------------------------------------------------------------------------
31
32#define AWAR_SQ_PERM "seq_quality/"     // saved in properties
33#define AWAR_SQ_TEMP "tmp/seq_quality/" // not saved in properties
34
35#define AWAR_SQ_WEIGHT_BASES     AWAR_SQ_PERM "weight_bases"
36#define AWAR_SQ_WEIGHT_DEVIATION AWAR_SQ_PERM "weight_deviation"
37#define AWAR_SQ_WEIGHT_HELIX     AWAR_SQ_PERM "weight_helix"
38#define AWAR_SQ_WEIGHT_CONSENSUS AWAR_SQ_PERM "weight_consensus"
39#define AWAR_SQ_WEIGHT_IUPAC     AWAR_SQ_PERM "weight_iupac"
40#define AWAR_SQ_WEIGHT_GC        AWAR_SQ_PERM "weight_gc"
41
42#define AWAR_SQ_MARK_ONLY_FLAG AWAR_SQ_PERM "mark_only_flag"
43#define AWAR_SQ_MARK_FLAG      AWAR_SQ_PERM "mark_flag"
44#define AWAR_SQ_MARK_BELOW     AWAR_SQ_PERM "mark_below"
45#define AWAR_SQ_REEVALUATE     AWAR_SQ_PERM "reevaluate"
46#define AWAR_SQ_FILTER_NAME    AWAR_SQ_TEMP "filter/name"
47
48void SQ_create_awars(AW_root *aw_root, AW_default aw_def) {
49    aw_root->awar_int(AWAR_SQ_WEIGHT_BASES,     5,  aw_def);
50    aw_root->awar_int(AWAR_SQ_WEIGHT_DEVIATION, 15, aw_def);
51    aw_root->awar_int(AWAR_SQ_WEIGHT_HELIX,     15, aw_def);
52    aw_root->awar_int(AWAR_SQ_WEIGHT_CONSENSUS, 50, aw_def);
53    aw_root->awar_int(AWAR_SQ_WEIGHT_IUPAC,     5,  aw_def);
54    aw_root->awar_int(AWAR_SQ_WEIGHT_GC,        10, aw_def);
55    aw_root->awar_int(AWAR_SQ_MARK_ONLY_FLAG,   0,  aw_def);
56    aw_root->awar_int(AWAR_SQ_MARK_FLAG,        1,  aw_def);
57    aw_root->awar_int(AWAR_SQ_MARK_BELOW,       40, aw_def);
58    aw_root->awar_int(AWAR_SQ_REEVALUATE,       0,  aw_def);
59
60    awt_create_filter_awars(aw_root, aw_def, AWAR_SQ_FILTER_NAME, AWAR_DEFAULT_ALIGNMENT);
61}
62
63// --------------------------------------------------------------------------------
64
65
66static void sq_calc_seq_quality_cb(AW_window * aww, adfiltercbstruct *acbs, GBDATA *gb_main) {
67    AW_root  *aw_root     = aww->get_root();
68    GB_ERROR  error       = 0;
69    TreeNode *tree        = 0;
70    bool      marked_only = (aw_root->awar(AWAR_SQ_MARK_ONLY_FLAG)->read_int() > 0);
71
72    arb_progress main_progress("Calculating sequence quality");
73
74    {
75        char *treename = aw_root->awar(AWAR_TREE)->read_string();
76
77        if (treename && strcmp(treename, NO_TREE_SELECTED) != 0) {
78            error = GB_push_transaction(gb_main);
79
80            if (!error) {
81                tree = GBT_read_tree(gb_main, treename, new SimpleRoot);
82                if (!tree) error = GB_await_error();
83                else {
84                    error = GBT_link_tree(tree, gb_main, false, NULL, NULL);
85                    if (!error) {
86                        GBT_TreeRemoveType mode = marked_only ? GBT_KEEP_MARKED : GBT_REMOVE_ZOMBIES;
87                        tree = GBT_remove_leafs(tree, mode, NULL, NULL, NULL);
88                        if (!tree || tree->is_leaf) {
89                            error = GBS_global_string("Tree contains less than 2 species after removing zombies%s",
90                                                      marked_only ? " and non-marked" : "");
91                        }
92                    }
93                }
94            }
95
96            error = GB_end_transaction(gb_main, error);
97        }
98        free(treename);
99    }
100
101    // if tree == 0 -> do basic quality calculations that are possible without tree information
102    // otherwise    -> use all groups found in tree and compare sequences against the groups they are contained in
103
104    if (!error) {
105        struct SQ_weights weights;
106
107        weights.bases = aw_root->awar(AWAR_SQ_WEIGHT_BASES)->read_int();
108        weights.diff_from_average = aw_root->awar(AWAR_SQ_WEIGHT_DEVIATION)->read_int();
109        weights.helix = aw_root->awar(AWAR_SQ_WEIGHT_HELIX)->read_int();
110        weights.consensus = aw_root->awar(AWAR_SQ_WEIGHT_CONSENSUS)->read_int();
111        weights.iupac = aw_root->awar(AWAR_SQ_WEIGHT_IUPAC)->read_int();
112        weights.gc = aw_root->awar(AWAR_SQ_WEIGHT_GC)->read_int();
113
114        int mark_flag = aw_root->awar(AWAR_SQ_MARK_FLAG)->read_int();
115        int mark_below = aw_root->awar(AWAR_SQ_MARK_BELOW)->read_int();
116        int reevaluate = aw_root->awar(AWAR_SQ_REEVALUATE)->read_int();
117
118        // Load and use Sequence-Filter
119        AP_filter *filter = awt_get_filter(acbs);
120        error             = awt_invalid_filter(filter);
121
122        /*
123          SQ_evaluate() generates the final estimation for the quality of an alignment.
124          It takes the values from the different containers, which are generated by the other functions, weights them
125          and calculates a final value. The final value is stored in "value_of_evaluation" (see options).
126          With the values stored in "weights" one can customize how important a value stored in a container becomes
127          for the final result.
128        */
129
130        if (!error) {
131            if (tree == 0) {
132                if (reevaluate) {
133                    SQ_mark_species(gb_main, mark_below, marked_only);
134                }
135                else {
136                    arb_progress  progress(GBT_get_species_count(gb_main)*2);
137                    SQ_GroupData *globalData = new SQ_GroupData_RNA;
138
139                    progress.subtitle("pass1");
140                    error = SQ_pass1_no_tree(globalData, gb_main, filter, progress);
141                    if (!error) {
142                        progress.subtitle("pass2");
143                        error = SQ_pass2_no_tree(globalData, gb_main, filter, progress);
144                        if (!error) {
145                            error = SQ_evaluate(gb_main, weights, marked_only);
146                            if (mark_flag && !error) {
147                                SQ_mark_species(gb_main, mark_below, marked_only);
148                            }
149                        }
150                    }
151                    if (error) progress.done();
152                    delete globalData;
153                }
154            }
155            else {
156                SQ_TREE_ERROR check = SQ_check_tree_structure(tree);
157                if (check != NONE) {
158                    switch (check) {
159                        case ZOMBIE:
160                            error = "Found one or more zombies in the tree.\n"
161                                "Please remove them or use another tree before running the quality check tool.";
162                            break;
163                        case MISSING_NODE:
164                            error = "Missing node(s) or unusable tree structure.\n"
165                                "Please fix the tree before running the quality check tool.";
166                            break;
167                        default:
168                            error = "An error occurred while traversing the tree.\n"
169                                "Please fix the tree before running the quality check tool.";
170                            break;
171                    }
172                }
173                else if (reevaluate) {
174                    SQ_mark_species(gb_main, mark_below, marked_only);
175                }
176                else {
177                    arb_progress progress(SQ_count_nodes(tree)*2);
178                    SQ_GroupData *globalData = new SQ_GroupData_RNA;
179
180                    progress.subtitle("pass1");
181                    SQ_calc_and_apply_group_data(tree, gb_main, globalData, filter, progress);
182                    progress.subtitle("pass2");
183                    SQ_calc_and_apply_group_data2(tree, gb_main, globalData, filter, progress);
184                    SQ_evaluate(gb_main, weights, marked_only);
185                    if (mark_flag) SQ_mark_species(gb_main, mark_below, marked_only);
186                    delete globalData;
187                }
188            }
189        }
190        awt_destroy_filter(filter);
191    }
192
193    if (error) aw_message(error);
194
195    SQ_clear_group_dictionary();
196    UNCOVERED();
197    destroy(tree);
198}
199
200static void sq_remove_quality_entries_cb(AW_window*, GBDATA *gb_main) {
201    GB_ERROR error = SQ_remove_quality_entries(gb_main);
202    aw_message_if(error);
203}
204
205static AWT_config_mapping_def seq_quality_config_mapping[] = {
206    { AWAR_SQ_WEIGHT_BASES,     "wbases" },
207    { AWAR_SQ_WEIGHT_DEVIATION, "wdeviation" },
208    { AWAR_SQ_WEIGHT_HELIX,     "whelix" },
209    { AWAR_SQ_WEIGHT_CONSENSUS, "wconsens" },
210    { AWAR_SQ_WEIGHT_IUPAC,     "wiupac" },
211    { AWAR_SQ_WEIGHT_GC,        "wgc" },
212    { AWAR_SQ_MARK_ONLY_FLAG,   "onlymarked" },
213    { AWAR_SQ_MARK_FLAG,        "markbad" },
214    { AWAR_SQ_MARK_BELOW,       "markbelow" },
215    { AWAR_SQ_REEVALUATE,       "reeval" },
216
217    { 0, 0 }
218};
219
220AW_window *SQ_create_seq_quality_window(AW_root *aw_root, GBDATA *gb_main) {
221    // create window for sequence quality calculation (called only once)
222
223    AW_window_simple *aws = new AW_window_simple;
224
225    aws->init(aw_root, "CALC_SEQ_QUALITY", "CALCULATE SEQUENCE QUALITY");
226    aws->load_xfig("seq_quality.fig");
227
228    aws->at("close");
229    aws->callback(AW_POPDOWN);
230    aws->create_button("CLOSE", "CLOSE", "C");
231
232    aws->at("help");
233    aws->callback(makeHelpCallback("seq_quality.hlp"));
234    aws->create_button("HELP", "HELP", "H");
235
236    aws->at("base");
237    aws->create_input_field(AWAR_SQ_WEIGHT_BASES, 3);
238
239    aws->at("deviation");
240    aws->create_input_field(AWAR_SQ_WEIGHT_DEVIATION, 3);
241
242    aws->at("no_helices");
243    aws->create_input_field(AWAR_SQ_WEIGHT_HELIX, 3);
244
245    aws->at("consensus");
246    aws->create_input_field(AWAR_SQ_WEIGHT_CONSENSUS, 3);
247
248    aws->at("iupac");
249    aws->create_input_field(AWAR_SQ_WEIGHT_IUPAC, 3);
250
251    aws->at("gc_proportion");
252    aws->create_input_field(AWAR_SQ_WEIGHT_GC, 3);
253
254    aws->at("monly");
255    aws->create_toggle(AWAR_SQ_MARK_ONLY_FLAG);
256
257    aws->at("mark");
258    aws->create_toggle(AWAR_SQ_MARK_FLAG);
259
260    aws->at("mark_below");
261    aws->create_input_field(AWAR_SQ_MARK_BELOW, 3);
262
263    aws->at("tree");
264    awt_create_TREE_selection_list(gb_main, aws, AWAR_TREE, true);
265
266    aws->at("filter");
267    adfiltercbstruct *adfilter = awt_create_select_filter(aws->get_root(), gb_main, AWAR_SQ_FILTER_NAME);
268    aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, adfilter));
269    aws->create_button("SELECT_FILTER", AWAR_SQ_FILTER_NAME);
270
271    aws->at("go");
272    aws->callback(makeWindowCallback(sq_calc_seq_quality_cb, adfilter, gb_main));
273    aws->highlight();
274    aws->create_button("GO", "GO", "G");
275
276    aws->at("config");
277    AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "seq_quality", seq_quality_config_mapping);
278
279    aws->at("reevaluate");
280    aws->label("Re-Evaluate only");
281    aws->create_toggle(AWAR_SQ_REEVALUATE);
282
283    aws->at("remove");
284    aws->callback(makeWindowCallback(sq_remove_quality_entries_cb, gb_main));
285    aws->create_button("Remove", "Remove", "R");
286
287    return aws;
288}
Note: See TracBrowser for help on using the repository browser.