| 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 <arbdbt.h> |
|---|
| 27 | #include <arb_global_defs.h> |
|---|
| 28 | |
|---|
| 29 | // -------------------------------------------------------------------------------- |
|---|
| 30 | |
|---|
| 31 | #define AWAR_SQ_PERM "seq_quality/" // saved in properties |
|---|
| 32 | #define AWAR_SQ_TEMP "tmp/seq_quality/" // not saved in properties |
|---|
| 33 | #define AWAR_SQ_WEIGHT_BASES AWAR_SQ_PERM "weight_bases" |
|---|
| 34 | #define AWAR_SQ_WEIGHT_DEVIATION AWAR_SQ_PERM "weight_deviation" |
|---|
| 35 | #define AWAR_SQ_WEIGHT_HELIX AWAR_SQ_PERM "weight_helix" |
|---|
| 36 | #define AWAR_SQ_WEIGHT_CONSENSUS AWAR_SQ_PERM "weight_consensus" |
|---|
| 37 | #define AWAR_SQ_WEIGHT_IUPAC AWAR_SQ_PERM "weight_iupac" |
|---|
| 38 | #define AWAR_SQ_WEIGHT_GC AWAR_SQ_PERM "weight_gc" |
|---|
| 39 | |
|---|
| 40 | #define AWAR_SQ_MARK_ONLY_FLAG AWAR_SQ_PERM "mark_only_flag" |
|---|
| 41 | #define AWAR_SQ_MARK_FLAG AWAR_SQ_PERM "mark_flag" |
|---|
| 42 | #define AWAR_SQ_MARK_BELOW AWAR_SQ_PERM "mark_below" |
|---|
| 43 | #define AWAR_SQ_REEVALUATE AWAR_SQ_PERM "reevaluate" |
|---|
| 44 | |
|---|
| 45 | #define AWAR_FILTER_PREFIX AWAR_SQ_TEMP "filter/" |
|---|
| 46 | #define AWAR_FILTER_NAME AWAR_FILTER_PREFIX "name" |
|---|
| 47 | #define AWAR_FILTER_FILTER AWAR_FILTER_PREFIX "filter" |
|---|
| 48 | #define AWAR_FILTER_ALI AWAR_FILTER_PREFIX "alignment" |
|---|
| 49 | |
|---|
| 50 | void SQ_create_awars(AW_root * aw_root, AW_default aw_def) { |
|---|
| 51 | aw_root->awar_int(AWAR_SQ_WEIGHT_BASES, 5, aw_def); |
|---|
| 52 | aw_root->awar_int(AWAR_SQ_WEIGHT_DEVIATION, 15, aw_def); |
|---|
| 53 | aw_root->awar_int(AWAR_SQ_WEIGHT_HELIX, 15, aw_def); |
|---|
| 54 | aw_root->awar_int(AWAR_SQ_WEIGHT_CONSENSUS, 50, aw_def); |
|---|
| 55 | aw_root->awar_int(AWAR_SQ_WEIGHT_IUPAC, 5, aw_def); |
|---|
| 56 | aw_root->awar_int(AWAR_SQ_WEIGHT_GC, 10, aw_def); |
|---|
| 57 | aw_root->awar_int(AWAR_SQ_MARK_ONLY_FLAG, 0, aw_def); |
|---|
| 58 | aw_root->awar_int(AWAR_SQ_MARK_FLAG, 1, aw_def); |
|---|
| 59 | aw_root->awar_int(AWAR_SQ_MARK_BELOW, 40, aw_def); |
|---|
| 60 | aw_root->awar_int(AWAR_SQ_REEVALUATE, 0, aw_def); |
|---|
| 61 | aw_root->awar_string(AWAR_FILTER_NAME, "none", aw_def); |
|---|
| 62 | aw_root->awar_string(AWAR_FILTER_FILTER, "", aw_def); |
|---|
| 63 | AW_awar *awar_ali = aw_root->awar_string(AWAR_FILTER_ALI, "", aw_def); |
|---|
| 64 | awar_ali->map(AWAR_DEFAULT_ALIGNMENT); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | // -------------------------------------------------------------------------------- |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | static void sq_calc_seq_quality_cb(AW_window * aww, AW_CL res_from_awt_create_select_filter, AW_CL cl_gb_main) { |
|---|
| 71 | GBDATA *gb_main = (GBDATA*)cl_gb_main; |
|---|
| 72 | AW_root *aw_root = aww->get_root(); |
|---|
| 73 | GB_ERROR error = 0; |
|---|
| 74 | GBT_TREE *tree = 0; |
|---|
| 75 | bool marked_only = (aw_root->awar(AWAR_SQ_MARK_ONLY_FLAG)->read_int() > 0); |
|---|
| 76 | |
|---|
| 77 | arb_progress main_progress("Calculating sequence quality"); |
|---|
| 78 | |
|---|
| 79 | { |
|---|
| 80 | char *treename = aw_root->awar(AWAR_TREE)->read_string(); |
|---|
| 81 | |
|---|
| 82 | if (treename && strcmp(treename, NO_TREE_SELECTED) != 0) { |
|---|
| 83 | error = GB_push_transaction(gb_main); |
|---|
| 84 | |
|---|
| 85 | if (!error) { |
|---|
| 86 | tree = GBT_read_tree(gb_main, treename, GBT_TREE_NodeFactory()); |
|---|
| 87 | if (!tree) error = GB_await_error(); |
|---|
| 88 | else { |
|---|
| 89 | error = GBT_link_tree(tree, gb_main, false, NULL, NULL); |
|---|
| 90 | if (!error) { |
|---|
| 91 | GBT_TreeRemoveType mode = marked_only ? GBT_KEEP_MARKED : GBT_REMOVE_ZOMBIES; |
|---|
| 92 | tree = GBT_remove_leafs(tree, mode, NULL, NULL, NULL); |
|---|
| 93 | if (!tree || tree->is_leaf) { |
|---|
| 94 | error = GBS_global_string("Tree contains less than 2 species after removing zombies%s", |
|---|
| 95 | marked_only ? " and non-marked" : ""); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | error = GB_end_transaction(gb_main, error); |
|---|
| 102 | } |
|---|
| 103 | free(treename); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | // if tree == 0 -> do basic quality calculations that are possible without tree information |
|---|
| 107 | // otherwise -> use all groups found in tree and compare sequences against the groups they are contained in |
|---|
| 108 | |
|---|
| 109 | if (!error) { |
|---|
| 110 | struct SQ_weights weights; |
|---|
| 111 | |
|---|
| 112 | weights.bases = aw_root->awar(AWAR_SQ_WEIGHT_BASES)->read_int(); |
|---|
| 113 | weights.diff_from_average = aw_root->awar(AWAR_SQ_WEIGHT_DEVIATION)->read_int(); |
|---|
| 114 | weights.helix = aw_root->awar(AWAR_SQ_WEIGHT_HELIX)->read_int(); |
|---|
| 115 | weights.consensus = aw_root->awar(AWAR_SQ_WEIGHT_CONSENSUS)->read_int(); |
|---|
| 116 | weights.iupac = aw_root->awar(AWAR_SQ_WEIGHT_IUPAC)->read_int(); |
|---|
| 117 | weights.gc = aw_root->awar(AWAR_SQ_WEIGHT_GC)->read_int(); |
|---|
| 118 | |
|---|
| 119 | int mark_flag = aw_root->awar(AWAR_SQ_MARK_FLAG)->read_int(); |
|---|
| 120 | int mark_below = aw_root->awar(AWAR_SQ_MARK_BELOW)->read_int(); |
|---|
| 121 | int reevaluate = aw_root->awar(AWAR_SQ_REEVALUATE)->read_int(); |
|---|
| 122 | |
|---|
| 123 | // Load and use Sequence-Filter |
|---|
| 124 | AP_filter *filter = awt_get_filter((adfiltercbstruct*)res_from_awt_create_select_filter); |
|---|
| 125 | error = awt_invalid_filter(filter); |
|---|
| 126 | |
|---|
| 127 | /* |
|---|
| 128 | SQ_evaluate() generates the final estimation for the quality of an alignment. |
|---|
| 129 | It takes the values from the different containers, which are generated by the other functions, weights them |
|---|
| 130 | and calculates a final value. The final value is stored in "value_of_evaluation" (see options). |
|---|
| 131 | With the values stored in "weights" one can customize how important a value stored in a container becomes |
|---|
| 132 | for the final result. |
|---|
| 133 | */ |
|---|
| 134 | |
|---|
| 135 | if (!error) { |
|---|
| 136 | if (tree == 0) { |
|---|
| 137 | if (reevaluate) { |
|---|
| 138 | SQ_mark_species(gb_main, mark_below, marked_only); |
|---|
| 139 | } |
|---|
| 140 | else { |
|---|
| 141 | arb_progress progress(GBT_get_species_count(gb_main)*2); |
|---|
| 142 | SQ_GroupData *globalData = new SQ_GroupData_RNA; |
|---|
| 143 | |
|---|
| 144 | progress.subtitle("pass1"); |
|---|
| 145 | error = SQ_pass1_no_tree(globalData, gb_main, filter, progress); |
|---|
| 146 | if (!error) { |
|---|
| 147 | progress.subtitle("pass2"); |
|---|
| 148 | error = SQ_pass2_no_tree(globalData, gb_main, filter, progress); |
|---|
| 149 | if (!error) { |
|---|
| 150 | error = SQ_evaluate(gb_main, weights, marked_only); |
|---|
| 151 | if (mark_flag && !error) { |
|---|
| 152 | SQ_mark_species(gb_main, mark_below, marked_only); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | if (error) progress.done(); |
|---|
| 157 | delete globalData; |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | else { |
|---|
| 161 | SQ_TREE_ERROR check = SQ_check_tree_structure(tree); |
|---|
| 162 | if (check != NONE) { |
|---|
| 163 | switch (check) { |
|---|
| 164 | case ZOMBIE: |
|---|
| 165 | error = "Found one or more zombies in the tree.\n" |
|---|
| 166 | "Please remove them or use another tree before running the quality check tool."; |
|---|
| 167 | break; |
|---|
| 168 | case MISSING_NODE: |
|---|
| 169 | error = "Missing node(s) or unusable tree structure.\n" |
|---|
| 170 | "Please fix the tree before running the quality check tool."; |
|---|
| 171 | break; |
|---|
| 172 | default: |
|---|
| 173 | error = "An error occurred while traversing the tree.\n" |
|---|
| 174 | "Please fix the tree before running the quality check tool."; |
|---|
| 175 | break; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | else if (reevaluate) { |
|---|
| 179 | SQ_mark_species(gb_main, mark_below, marked_only); |
|---|
| 180 | } |
|---|
| 181 | else { |
|---|
| 182 | arb_progress progress(SQ_count_nodes(tree)*2); |
|---|
| 183 | SQ_GroupData *globalData = new SQ_GroupData_RNA; |
|---|
| 184 | |
|---|
| 185 | progress.subtitle("pass1"); |
|---|
| 186 | SQ_calc_and_apply_group_data(tree, gb_main, globalData, filter, progress); |
|---|
| 187 | progress.subtitle("pass2"); |
|---|
| 188 | SQ_calc_and_apply_group_data2(tree, gb_main, globalData, filter, progress); |
|---|
| 189 | SQ_evaluate(gb_main, weights, marked_only); |
|---|
| 190 | if (mark_flag) SQ_mark_species(gb_main, mark_below, marked_only); |
|---|
| 191 | delete globalData; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | awt_destroy_filter(filter); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | if (error) aw_message(error); |
|---|
| 199 | |
|---|
| 200 | SQ_clear_group_dictionary(); |
|---|
| 201 | delete tree; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | static void sq_remove_quality_entries_cb(AW_window *, AW_CL cl_gb_main) { |
|---|
| 205 | GBDATA *gb_main = (GBDATA*)cl_gb_main; |
|---|
| 206 | SQ_remove_quality_entries(gb_main); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | AW_window *SQ_create_seq_quality_window(AW_root *aw_root, GBDATA *gb_main) { |
|---|
| 210 | // create window for sequence quality calculation (called only once) |
|---|
| 211 | |
|---|
| 212 | AW_window_simple *aws = new AW_window_simple; |
|---|
| 213 | |
|---|
| 214 | aws->init(aw_root, "CALC_SEQ_QUALITY", "CALCULATE SEQUENCE QUALITY"); |
|---|
| 215 | aws->load_xfig("seq_quality.fig"); |
|---|
| 216 | |
|---|
| 217 | aws->at("close"); |
|---|
| 218 | aws->callback((AW_CB0) AW_POPDOWN); |
|---|
| 219 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 220 | |
|---|
| 221 | aws->at("help"); |
|---|
| 222 | aws->callback(makeHelpCallback("seq_quality.hlp")); |
|---|
| 223 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 224 | |
|---|
| 225 | aws->at("base"); |
|---|
| 226 | aws->create_input_field(AWAR_SQ_WEIGHT_BASES, 3); |
|---|
| 227 | |
|---|
| 228 | aws->at("deviation"); |
|---|
| 229 | aws->create_input_field(AWAR_SQ_WEIGHT_DEVIATION, 3); |
|---|
| 230 | |
|---|
| 231 | aws->at("no_helices"); |
|---|
| 232 | aws->create_input_field(AWAR_SQ_WEIGHT_HELIX, 3); |
|---|
| 233 | |
|---|
| 234 | aws->at("consensus"); |
|---|
| 235 | aws->create_input_field(AWAR_SQ_WEIGHT_CONSENSUS, 3); |
|---|
| 236 | |
|---|
| 237 | aws->at("iupac"); |
|---|
| 238 | aws->create_input_field(AWAR_SQ_WEIGHT_IUPAC, 3); |
|---|
| 239 | |
|---|
| 240 | aws->at("gc_proportion"); |
|---|
| 241 | aws->create_input_field(AWAR_SQ_WEIGHT_GC, 3); |
|---|
| 242 | |
|---|
| 243 | aws->at("monly"); |
|---|
| 244 | aws->create_toggle(AWAR_SQ_MARK_ONLY_FLAG); |
|---|
| 245 | |
|---|
| 246 | aws->at("mark"); |
|---|
| 247 | aws->create_toggle(AWAR_SQ_MARK_FLAG); |
|---|
| 248 | |
|---|
| 249 | aws->at("mark_below"); |
|---|
| 250 | aws->create_input_field(AWAR_SQ_MARK_BELOW, 3); |
|---|
| 251 | |
|---|
| 252 | aws->at("tree"); |
|---|
| 253 | awt_create_TREE_selection_list(gb_main, aws, AWAR_TREE, true); |
|---|
| 254 | |
|---|
| 255 | aws->at("filter"); |
|---|
| 256 | adfiltercbstruct *adfilter = awt_create_select_filter(aws->get_root(), gb_main, AWAR_FILTER_NAME); |
|---|
| 257 | aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, adfilter)); |
|---|
| 258 | aws->create_button("SELECT_FILTER", AWAR_FILTER_NAME); |
|---|
| 259 | |
|---|
| 260 | aws->at("go"); |
|---|
| 261 | aws->callback(sq_calc_seq_quality_cb, (AW_CL)adfilter, (AW_CL)gb_main); |
|---|
| 262 | aws->highlight(); |
|---|
| 263 | aws->create_button("GO", "GO", "G"); |
|---|
| 264 | |
|---|
| 265 | aws->at("remove"); |
|---|
| 266 | aws->callback(sq_remove_quality_entries_cb, (AW_CL)gb_main); |
|---|
| 267 | aws->create_button("Remove", "Remove", "R"); |
|---|
| 268 | |
|---|
| 269 | aws->at("reevaluate"); |
|---|
| 270 | aws->label("Re-Evaluate only"); |
|---|
| 271 | aws->create_toggle(AWAR_SQ_REEVALUATE); |
|---|
| 272 | |
|---|
| 273 | return aws; |
|---|
| 274 | } |
|---|