| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : ED4_visualizeSAI.cxx // |
|---|
| 4 | // Purpose : Visualize sequence associated information (SAI) // |
|---|
| 5 | // in the Editor // |
|---|
| 6 | // // |
|---|
| 7 | // Coded by Yadhu Kumar // |
|---|
| 8 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 9 | // http://www.arb-home.de/ // |
|---|
| 10 | // // |
|---|
| 11 | // =============================================================== // |
|---|
| 12 | |
|---|
| 13 | #include <ed4_extern.hxx> |
|---|
| 14 | #include "ed4_class.hxx" |
|---|
| 15 | |
|---|
| 16 | #include <awt_canvas.hxx> |
|---|
| 17 | #include <awt_sel_boxes.hxx> |
|---|
| 18 | #include <awt_prompt.hxx> |
|---|
| 19 | |
|---|
| 20 | #include <aw_awars.hxx> |
|---|
| 21 | #include <aw_preset.hxx> |
|---|
| 22 | #include <aw_msg.hxx> |
|---|
| 23 | #include <aw_root.hxx> |
|---|
| 24 | #include <aw_question.hxx> |
|---|
| 25 | #include <aw_select.hxx> |
|---|
| 26 | |
|---|
| 27 | #include <arbdbt.h> |
|---|
| 28 | #include <ad_cb.h> |
|---|
| 29 | |
|---|
| 30 | #include <arb_strbuf.h> |
|---|
| 31 | |
|---|
| 32 | #include <iostream> |
|---|
| 33 | |
|---|
| 34 | // -------------------------------------------------------------------------------- |
|---|
| 35 | |
|---|
| 36 | #define AWAR_SAI_CLR_TAB "saicolors/" |
|---|
| 37 | #define AWAR_SAI_VISUALIZED AWAR_SAI_CLR_TAB "current" // current visualized SAI |
|---|
| 38 | #define AWAR_SAI_CLR_DEF AWAR_SAI_CLR_TAB "clr_trans_tab/" // container for definitions |
|---|
| 39 | #define AWAR_SAI_ENABLE AWAR_SAI_CLR_TAB "enable" // global enable of visualization |
|---|
| 40 | #define AWAR_SAI_ALL_SPECIES AWAR_SAI_CLR_TAB "all_species" // 1 = all / 0 = marked |
|---|
| 41 | #define AWAR_SAI_AUTO_SELECT AWAR_SAI_CLR_TAB "auto_select" // 1 = auto select / 0 = manual select |
|---|
| 42 | #define AWAR_SAI_CLR_TRANS_TABLE AWAR_SAI_CLR_TAB "clr_trans_table" // current translation table |
|---|
| 43 | #define AWAR_SAI_CLR_TRANS_TAB_NAMES AWAR_SAI_CLR_TAB "clr_trans_tab_names" // ;-separated list of existing translation tables |
|---|
| 44 | #define AWAR_SAI_CLR_TRANS_TAB_REL AWAR_SAI_CLR_TAB "sai_relation/" // container to store trans tables for each SAI |
|---|
| 45 | |
|---|
| 46 | #define AWAR_SAI_CLR "tmp/sai/color_0" // the definition of the current translation table (number runs from 0 to 9) |
|---|
| 47 | #define AWAR_SAI_CLR_COUNT 10 |
|---|
| 48 | |
|---|
| 49 | // -------------------------------------------------------------------------------- |
|---|
| 50 | |
|---|
| 51 | static bool clrDefinitionsChanged = false; |
|---|
| 52 | static bool inCallback = false; // used to avoid multiple refreshes |
|---|
| 53 | static bool in_colorDefChanged_callback = false; // used to avoid colorDef correction |
|---|
| 54 | |
|---|
| 55 | // -------------------------------------------------------------------------------- |
|---|
| 56 | |
|---|
| 57 | #define BUFSIZE 100 |
|---|
| 58 | static const char *getAwarName(int awarNo) { |
|---|
| 59 | static char buf[BUFSIZE]; |
|---|
| 60 | |
|---|
| 61 | strcpy(buf, AWAR_SAI_CLR); |
|---|
| 62 | (strchr(buf, 0)-1)[0] = '0'+awarNo; |
|---|
| 63 | |
|---|
| 64 | return buf; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | static const char *getClrDefAwar(const char *awarName) { |
|---|
| 68 | static char buf[BUFSIZE]; |
|---|
| 69 | |
|---|
| 70 | e4_assert(awarName); |
|---|
| 71 | e4_assert(awarName[0]); // empty awar is bad |
|---|
| 72 | |
|---|
| 73 | IF_ASSERTION_USED(int size =) sprintf(buf, AWAR_SAI_CLR_DEF "%s", awarName); |
|---|
| 74 | e4_assert(size<BUFSIZE); |
|---|
| 75 | return buf; |
|---|
| 76 | } |
|---|
| 77 | #undef BUFSIZE |
|---|
| 78 | |
|---|
| 79 | // --------------------------------------------------------- |
|---|
| 80 | |
|---|
| 81 | static void setVisualizeSAI_cb(AW_root *awr) { |
|---|
| 82 | ED4_ROOT->visualizeSAI = awr->awar(AWAR_SAI_ENABLE)->read_int(); |
|---|
| 83 | ED4_ROOT->request_refresh_for_sequence_terminals(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | static void setVisualizeSAI_options_cb(AW_root *awr) { |
|---|
| 87 | ED4_ROOT->visualizeSAI_allSpecies = awr->awar(AWAR_SAI_ALL_SPECIES)->read_int(); |
|---|
| 88 | ED4_ROOT->request_refresh_for_sequence_terminals(); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | static bool colorTransTable_exists(AW_root *awr, const char *name) { |
|---|
| 92 | char *tableNames = awr->awar(AWAR_SAI_CLR_TRANS_TAB_NAMES)->read_string(); |
|---|
| 93 | const char *searchFrom = tableNames; |
|---|
| 94 | int len = strlen(name); |
|---|
| 95 | |
|---|
| 96 | while (searchFrom) { |
|---|
| 97 | const char *found = strstr(searchFrom, name); |
|---|
| 98 | |
|---|
| 99 | if (found) { |
|---|
| 100 | if ((found == tableNames || found[-1] == '\n') && // found start of entry |
|---|
| 101 | (found[len] == '\n' || found[len] == 0)) // avoid partial entry |
|---|
| 102 | { |
|---|
| 103 | break; // exists! |
|---|
| 104 | } |
|---|
| 105 | else { // search next match |
|---|
| 106 | searchFrom = found+1; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | else { |
|---|
| 110 | searchFrom = NULp; |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | free(tableNames); |
|---|
| 115 | return searchFrom; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | static void colorDefChanged_callback(AW_root *awr, int awarNo) { |
|---|
| 119 | clrDefinitionsChanged = true; |
|---|
| 120 | |
|---|
| 121 | if (!in_colorDefChanged_callback) { // this callback is special, because it may change all other color defs |
|---|
| 122 | LocallyModify<bool> flag(in_colorDefChanged_callback, true); |
|---|
| 123 | |
|---|
| 124 | { |
|---|
| 125 | LocallyModify<bool> in_cb(inCallback, true); |
|---|
| 126 | |
|---|
| 127 | char *clrTabName = awr->awar(AWAR_SAI_CLR_TRANS_TABLE)->read_string(); |
|---|
| 128 | if (clrTabName[0]) { |
|---|
| 129 | unsigned char charUsed[256]; memset(charUsed, 255, 256); |
|---|
| 130 | |
|---|
| 131 | { |
|---|
| 132 | for (int i=0; i<10; i++) { |
|---|
| 133 | char *awarString_next = awr->awar_string(getAwarName(i))->read_string(); |
|---|
| 134 | for (int c=0; awarString_next[c]; ++c) { |
|---|
| 135 | charUsed[(unsigned char)awarString_next[c]] = i; |
|---|
| 136 | } |
|---|
| 137 | free(awarString_next); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | char *awarString = awr->awar_string(getAwarName(awarNo))->read_string(); |
|---|
| 141 | for (int c=0; awarString[c]; ++c) { |
|---|
| 142 | charUsed[(unsigned char)awarString[c]] = awarNo; |
|---|
| 143 | } |
|---|
| 144 | free(awarString); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | typedef unsigned char mystr[256]; |
|---|
| 148 | mystr s[10]; |
|---|
| 149 | for (int i=0; i<10; i++) s[i][0]=0; // initializing the strings |
|---|
| 150 | |
|---|
| 151 | for (int i=0; i<256; i++) { |
|---|
| 152 | int table = charUsed[i]; |
|---|
| 153 | if (table != 255) { |
|---|
| 154 | char *eos = strchr((char *)s[table], 0); // get pointer to end of string |
|---|
| 155 | eos[0] = char(i); |
|---|
| 156 | eos[1] = 0; |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | { |
|---|
| 161 | GBS_strstruct clrDefStr(500); |
|---|
| 162 | for (int i=0; i<10; i++) { |
|---|
| 163 | awr->awar_string(getAwarName(i))->write_string((char *)s[i]); |
|---|
| 164 | |
|---|
| 165 | char *escaped = GBS_escape_string((char*)s[i], ";", '&'); |
|---|
| 166 | clrDefStr.cat(escaped); |
|---|
| 167 | free(escaped); |
|---|
| 168 | |
|---|
| 169 | clrDefStr.put(';'); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | AW_awar *awar_def = awr->awar_string(getClrDefAwar(clrTabName), "", AW_ROOT_DEFAULT); |
|---|
| 173 | awar_def->write_string(clrDefStr.get_data()); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | else { |
|---|
| 177 | if (!in_cb.old_value()) { // only warn when user really changed the setting |
|---|
| 178 | aw_message("Please select a VALID Color Translation Table to EDIT."); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | free(clrTabName); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if (!inCallback) ED4_ROOT->request_refresh_for_sequence_terminals(); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | static AW_awar *sai_transtab_awar(AW_root *awr, const char *saiName) { |
|---|
| 189 | char *sai_key = GBS_string_2_key(saiName); |
|---|
| 190 | const char *awarname = GBS_global_string(AWAR_SAI_CLR_TRANS_TAB_REL "%s", sai_key); |
|---|
| 191 | free(sai_key); |
|---|
| 192 | return awr->awar_string(awarname, "", AW_ROOT_DEFAULT); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | static void colorDefTabNameChanged_callback(AW_root *awr) { |
|---|
| 196 | char *clrTabName = awr->awar(AWAR_SAI_CLR_TRANS_TABLE)->read_string(); |
|---|
| 197 | { |
|---|
| 198 | LocallyModify<bool> flag(inCallback, true); |
|---|
| 199 | { |
|---|
| 200 | LocallyModify<bool> flag2(in_colorDefChanged_callback, true); // avoid correction |
|---|
| 201 | |
|---|
| 202 | // clear current translation table definition |
|---|
| 203 | for (int i=0; i<10; i++) { |
|---|
| 204 | AW_awar *transDef_awar = awr->awar_string(getAwarName(i), "", AW_ROOT_DEFAULT); |
|---|
| 205 | transDef_awar->write_string(""); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | if (clrTabName[0]) { |
|---|
| 209 | AW_awar *clrTabDef_awar = awr->awar_string(getClrDefAwar(clrTabName), "", AW_ROOT_DEFAULT); |
|---|
| 210 | char *clrTabDef = clrTabDef_awar->read_string(); |
|---|
| 211 | |
|---|
| 212 | if (clrTabDef[0]) { |
|---|
| 213 | int i = 0; |
|---|
| 214 | int tokStart = 0; |
|---|
| 215 | |
|---|
| 216 | for (int si = 0; clrTabDef[si]; ++si) { |
|---|
| 217 | if (clrTabDef[si] == ';') { |
|---|
| 218 | e4_assert(i >= 0 && i<10); |
|---|
| 219 | AW_awar *awar = awr->awar(getAwarName(i)); |
|---|
| 220 | |
|---|
| 221 | if (tokStart == si) { // empty definition |
|---|
| 222 | awar->write_string(""); |
|---|
| 223 | } |
|---|
| 224 | else { |
|---|
| 225 | int toklen = si-tokStart; |
|---|
| 226 | |
|---|
| 227 | e4_assert(toklen > 0); |
|---|
| 228 | e4_assert(clrTabDef[tokStart+toklen] == ';'); |
|---|
| 229 | clrTabDef[tokStart+toklen] = 0; |
|---|
| 230 | |
|---|
| 231 | char *unescaped = GBS_unescape_string(clrTabDef+tokStart, ";", '&'); |
|---|
| 232 | awar->write_string(unescaped); |
|---|
| 233 | free(unescaped); |
|---|
| 234 | |
|---|
| 235 | clrTabDef[tokStart+toklen] = ';'; |
|---|
| 236 | } |
|---|
| 237 | ++i; |
|---|
| 238 | tokStart = si+1; |
|---|
| 239 | } |
|---|
| 240 | } |
|---|
| 241 | e4_assert(i == 10); |
|---|
| 242 | } |
|---|
| 243 | free(clrTabDef); |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | colorDefChanged_callback(awr, 0); // correct first def manually |
|---|
| 247 | |
|---|
| 248 | // store the selected table as default for the visualized SAI: |
|---|
| 249 | const char *saiName = awr->awar(AWAR_SAI_VISUALIZED)->read_char_pntr(); |
|---|
| 250 | if (saiName[0]) sai_transtab_awar(awr, saiName)->write_string(clrTabName); |
|---|
| 251 | } |
|---|
| 252 | free(clrTabName); |
|---|
| 253 | |
|---|
| 254 | if (!inCallback && clrDefinitionsChanged) ED4_ROOT->request_refresh_for_sequence_terminals(); |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | static void refresh_display_cb(GB_CB_TYPE cb_type) { |
|---|
| 258 | if ((cb_type & GB_CB_CHANGED) && |
|---|
| 259 | ED4_ROOT->aw_root->awar(AWAR_SAI_ENABLE)->read_int()) |
|---|
| 260 | { |
|---|
| 261 | clrDefinitionsChanged = 1; |
|---|
| 262 | ED4_ROOT->request_refresh_for_sequence_terminals(); |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | static void saiChanged_callback(AW_root *awr) { |
|---|
| 267 | char *saiName = NULp; |
|---|
| 268 | { |
|---|
| 269 | LocallyModify<bool> flag(inCallback, true); |
|---|
| 270 | { |
|---|
| 271 | static GBDATA *gb_last_SAI = NULp; |
|---|
| 272 | |
|---|
| 273 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
|---|
| 274 | if (gb_last_SAI) { |
|---|
| 275 | GB_transaction ta(gb_main); |
|---|
| 276 | GB_remove_callback(gb_last_SAI, GB_CB_CHANGED, makeDatabaseCallback(refresh_display_cb)); |
|---|
| 277 | gb_last_SAI = NULp; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | saiName = awr->awar(AWAR_SAI_VISUALIZED)->read_string(); |
|---|
| 281 | char *transTabName = NULp; |
|---|
| 282 | |
|---|
| 283 | if (saiName[0]) { |
|---|
| 284 | transTabName = sai_transtab_awar(awr, saiName)->read_string(); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | { |
|---|
| 288 | GB_transaction ta(gb_main); |
|---|
| 289 | gb_last_SAI = GBT_find_SAI(gb_main, saiName); |
|---|
| 290 | if (gb_last_SAI) { |
|---|
| 291 | GB_add_callback(gb_last_SAI, GB_CB_CHANGED, makeDatabaseCallback(refresh_display_cb)); |
|---|
| 292 | } |
|---|
| 293 | } |
|---|
| 294 | awr->awar(AWAR_SAI_CLR_TRANS_TABLE)->write_string(null2empty(transTabName)); |
|---|
| 295 | free(transTabName); |
|---|
| 296 | |
|---|
| 297 | clrDefinitionsChanged = true; // SAI changed -> update needed |
|---|
| 298 | } |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | if (!inCallback && clrDefinitionsChanged) { |
|---|
| 302 | // SAI changed notify Global SAI Awar AWAR_SAI_GLOBAL |
|---|
| 303 | awr->awar(AWAR_SAI_GLOBAL)->write_string(saiName); |
|---|
| 304 | ED4_ROOT->request_refresh_for_sequence_terminals(); |
|---|
| 305 | } |
|---|
| 306 | free(saiName); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | static void update_ClrTransTabNamesList_cb(AW_root *awr, AW_selection_list *colorTransList) { |
|---|
| 310 | char *clrTransTabNames = awr->awar(AWAR_SAI_CLR_TRANS_TAB_NAMES)->read_string(); |
|---|
| 311 | |
|---|
| 312 | colorTransList->clear(); |
|---|
| 313 | |
|---|
| 314 | for (char *tok = strtok(clrTransTabNames, "\n"); tok; tok = strtok(NULp, "\n")) { |
|---|
| 315 | colorTransList->insert(tok, tok); |
|---|
| 316 | } |
|---|
| 317 | colorTransList->insert_default("????", ""); |
|---|
| 318 | colorTransList->update(); |
|---|
| 319 | |
|---|
| 320 | free(clrTransTabNames); |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | static void autoselect_cb(AW_root *aw_root) { |
|---|
| 324 | char *curr_sai = aw_root->awar(AWAR_SAI_NAME)->read_string(); |
|---|
| 325 | #if defined(DEBUG) |
|---|
| 326 | printf("curr_sai='%s'\n", curr_sai); |
|---|
| 327 | #endif // DEBUG |
|---|
| 328 | aw_root->awar(AWAR_SAI_VISUALIZED)->write_string(curr_sai); |
|---|
| 329 | free(curr_sai); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | static void set_autoselect_cb(AW_root *aw_root) { |
|---|
| 333 | static bool callback_active = false; |
|---|
| 334 | |
|---|
| 335 | if (aw_root->awar(AWAR_SAI_AUTO_SELECT)->read_int()) { // auto select is activated |
|---|
| 336 | aw_root->awar(AWAR_SAI_NAME)->add_callback(autoselect_cb); |
|---|
| 337 | callback_active = true; |
|---|
| 338 | } |
|---|
| 339 | else { |
|---|
| 340 | if (callback_active) { // only remove if added |
|---|
| 341 | aw_root->awar(AWAR_SAI_NAME)->remove_callback(autoselect_cb); |
|---|
| 342 | callback_active = false; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | } |
|---|
| 346 | |
|---|
| 347 | static void addOrUpdateTransTable(AW_root *aw_root, const char *newClrTransTabName, const char *defaultDefinition, bool autoselect) { |
|---|
| 348 | AW_awar *table_def_awar = aw_root->awar_string(getClrDefAwar(newClrTransTabName), defaultDefinition, AW_ROOT_DEFAULT); |
|---|
| 349 | table_def_awar->write_string(defaultDefinition); |
|---|
| 350 | |
|---|
| 351 | if (!colorTransTable_exists(aw_root, newClrTransTabName)) { |
|---|
| 352 | AW_awar *names_awar = aw_root->awar(AWAR_SAI_CLR_TRANS_TAB_NAMES); |
|---|
| 353 | const char *old_names = names_awar->read_char_pntr(); |
|---|
| 354 | names_awar->write_string(old_names[0] |
|---|
| 355 | ? GBS_global_string("%s\n%s", old_names, newClrTransTabName) |
|---|
| 356 | : newClrTransTabName); // add new name |
|---|
| 357 | } |
|---|
| 358 | |
|---|
| 359 | if (autoselect) { |
|---|
| 360 | aw_root->awar(AWAR_SAI_CLR_TRANS_TABLE)->write_string(newClrTransTabName); // select new |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | static void addDefaultTransTable(AW_root *aw_root, const char *newClrTransTabName, const char *defaultDefinition) { |
|---|
| 365 | addOrUpdateTransTable(aw_root, newClrTransTabName, defaultDefinition, false); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | void ED4_createVisualizeSAI_Awars(AW_root *aw_root, AW_default aw_def) { // --- Creating and initializing AWARS ----- |
|---|
| 369 | aw_root->awar_int(AWAR_SAI_ENABLE, 0, aw_def); |
|---|
| 370 | aw_root->awar_int(AWAR_SAI_ALL_SPECIES, 0, aw_def); |
|---|
| 371 | aw_root->awar_int(AWAR_SAI_AUTO_SELECT, 0, aw_def); |
|---|
| 372 | |
|---|
| 373 | aw_root->awar_string(AWAR_SAI_VISUALIZED, "", aw_def); |
|---|
| 374 | aw_root->awar_string(AWAR_SAI_CLR_TRANS_TABLE, "", aw_def); |
|---|
| 375 | aw_root->awar_string(AWAR_SAI_CLR_TRANS_TAB_NAMES, "", aw_def); |
|---|
| 376 | |
|---|
| 377 | for (int i=0; i<10; i++) { // initializing 10 color definition string AWARS |
|---|
| 378 | AW_awar *def_awar = aw_root->awar_string(getAwarName(i), "", aw_def); |
|---|
| 379 | def_awar->add_callback(makeRootCallback(colorDefChanged_callback, i)); |
|---|
| 380 | } |
|---|
| 381 | aw_root->awar(AWAR_SAI_ENABLE) ->add_callback(setVisualizeSAI_cb); |
|---|
| 382 | aw_root->awar(AWAR_SAI_ALL_SPECIES) ->add_callback(setVisualizeSAI_options_cb); |
|---|
| 383 | aw_root->awar(AWAR_SAI_AUTO_SELECT) ->add_callback(set_autoselect_cb); |
|---|
| 384 | aw_root->awar(AWAR_SAI_VISUALIZED) ->add_callback(saiChanged_callback); |
|---|
| 385 | aw_root->awar(AWAR_SAI_CLR_TRANS_TABLE)->add_callback(colorDefTabNameChanged_callback); |
|---|
| 386 | |
|---|
| 387 | ED4_ROOT->visualizeSAI = aw_root->awar(AWAR_SAI_ENABLE)->read_int(); |
|---|
| 388 | ED4_ROOT->visualizeSAI_allSpecies = aw_root->awar(AWAR_SAI_ALL_SPECIES)->read_int(); |
|---|
| 389 | |
|---|
| 390 | // create some defaults: |
|---|
| 391 | addDefaultTransTable(aw_root, "numeric", "0;1;2;3;4;5;6;7;8;9;"); |
|---|
| 392 | addDefaultTransTable(aw_root, "binary", ".0;;+1;;;;;;;;"); |
|---|
| 393 | addDefaultTransTable(aw_root, "consensus", "=ACGTU;;acgtu;.;;;;;;;"); |
|---|
| 394 | addDefaultTransTable(aw_root, "PVP", "012;345;678;9ABab;CDEcde;FGHfgh;IJKLijkl;MNOPmnop;QRSTqrst;UVWXYZuvwxyz;"); |
|---|
| 395 | addDefaultTransTable(aw_root, "helix", ";;<>;;;;;[];;;"); |
|---|
| 396 | addDefaultTransTable(aw_root, "xstring", ";x;;;;;;;;;"); |
|---|
| 397 | addDefaultTransTable(aw_root, "gaps", ";-.;;;;;;;;;"); |
|---|
| 398 | |
|---|
| 399 | LocallyModify<bool> flag(inCallback, true); // avoid refresh |
|---|
| 400 | saiChanged_callback(aw_root); |
|---|
| 401 | colorDefTabNameChanged_callback(aw_root); // init awars for selected table |
|---|
| 402 | set_autoselect_cb(aw_root); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | enum CreationMode { |
|---|
| 406 | ED4_VIS_CREATE, // creates a new (empty) color translation table |
|---|
| 407 | ED4_VIS_COPY, // copies the selected color translation table |
|---|
| 408 | }; |
|---|
| 409 | |
|---|
| 410 | static GB_ERROR createCopy_table_handler(const char *dest_table_input, CreationMode mode) { |
|---|
| 411 | GB_ERROR error = NULp; |
|---|
| 412 | if (!dest_table_input[0]) error = "not a valid name"; |
|---|
| 413 | else { |
|---|
| 414 | AW_root *aw_root = AW_root::SINGLETON; |
|---|
| 415 | char *dest_table = GBS_string_2_key(dest_table_input); |
|---|
| 416 | if (colorTransTable_exists(aw_root, dest_table)) { |
|---|
| 417 | error = GBS_global_string("Color translation table '%s' already exists.", dest_table); |
|---|
| 418 | } |
|---|
| 419 | else { |
|---|
| 420 | switch (mode) { |
|---|
| 421 | case ED4_VIS_CREATE: |
|---|
| 422 | addOrUpdateTransTable(aw_root, dest_table, "", true); |
|---|
| 423 | break; |
|---|
| 424 | |
|---|
| 425 | case ED4_VIS_COPY: { |
|---|
| 426 | const char *src_table = aw_root->awar(AWAR_SAI_CLR_TRANS_TABLE)->read_char_pntr(); |
|---|
| 427 | if (!src_table[0]) error = "Please select a valid source table"; |
|---|
| 428 | else { |
|---|
| 429 | const char *src_def = aw_root->awar(getClrDefAwar(src_table))->read_char_pntr(); |
|---|
| 430 | addOrUpdateTransTable(aw_root, dest_table, src_def, true); |
|---|
| 431 | } |
|---|
| 432 | break; |
|---|
| 433 | } |
|---|
| 434 | } |
|---|
| 435 | } |
|---|
| 436 | |
|---|
| 437 | free(dest_table); |
|---|
| 438 | } |
|---|
| 439 | return error; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | static void createCopy_ClrTransTab_cb(AW_window *aww, CreationMode mode) { |
|---|
| 443 | AW_root *aw_root = aww->get_root(); |
|---|
| 444 | const char *selected_table = aw_root->awar(AWAR_SAI_CLR_TRANS_TABLE)->read_char_pntr(); |
|---|
| 445 | ResultHandler handler = makeResultHandler(createCopy_table_handler, mode); |
|---|
| 446 | const char *helpfile = "visualizeSAI.hlp"; |
|---|
| 447 | |
|---|
| 448 | switch (mode) { |
|---|
| 449 | case ED4_VIS_CREATE: AWT_activate_prompt("Create new color translation table", "Enter name of new table:", selected_table, "Create", handler, helpfile); break; |
|---|
| 450 | case ED4_VIS_COPY: AWT_activate_prompt("Copy color translation table", "Enter destination table name:", selected_table, "Copy", handler, helpfile); break; |
|---|
| 451 | } |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | static void deleteColorTranslationTable(AW_window *aws) { |
|---|
| 455 | AW_root *aw_root = aws->get_root(); |
|---|
| 456 | const char *clrTabName = aw_root->awar_string(AWAR_SAI_CLR_TRANS_TABLE)->read_char_pntr(); |
|---|
| 457 | |
|---|
| 458 | if (clrTabName[0]) { |
|---|
| 459 | AW_awar *awar_tabNames = aw_root->awar(AWAR_SAI_CLR_TRANS_TAB_NAMES); |
|---|
| 460 | char *clrTransTabNames = awar_tabNames->read_string(); |
|---|
| 461 | GBS_strstruct newTransTabName(strlen(clrTransTabNames)); |
|---|
| 462 | |
|---|
| 463 | for (const char *tok = strtok(clrTransTabNames, "\n"); tok; tok = strtok(NULp, "\n")) { |
|---|
| 464 | if (strcmp(clrTabName, tok) != 0) { // merge all not to delete |
|---|
| 465 | newTransTabName.cat(tok); |
|---|
| 466 | newTransTabName.put('\n'); |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | |
|---|
| 470 | aw_root->awar_string(getClrDefAwar(clrTabName))->write_string(""); |
|---|
| 471 | awar_tabNames->write_string(newTransTabName.get_data()); // updates selection list |
|---|
| 472 | |
|---|
| 473 | free(clrTransTabNames); |
|---|
| 474 | } |
|---|
| 475 | else { |
|---|
| 476 | aw_message("Selected Color Translation Table is not VALID and cannot be DELETED!"); |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | static AW_selection_list *buildClrTransTabNamesList(AW_window *aws) { |
|---|
| 481 | AW_root *awr = aws->get_root(); |
|---|
| 482 | AW_selection_list *colorTransList = aws->create_selection_list(AWAR_SAI_CLR_TRANS_TABLE, true); |
|---|
| 483 | |
|---|
| 484 | update_ClrTransTabNamesList_cb(awr, colorTransList); |
|---|
| 485 | |
|---|
| 486 | return colorTransList; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | const char *ED4_getSaiColorString(AW_root *awr, int start, int end) { |
|---|
| 490 | static int seqBufferSize = 0; |
|---|
| 491 | static char *saiColors = NULp; |
|---|
| 492 | static int lastStart = -1; |
|---|
| 493 | static int lastEnd = -1; |
|---|
| 494 | static bool lastVisualize = false; |
|---|
| 495 | |
|---|
| 496 | e4_assert(start<=end); |
|---|
| 497 | |
|---|
| 498 | if (lastStart==start && lastEnd==end && !clrDefinitionsChanged && lastVisualize) { |
|---|
| 499 | return saiColors-start; // if start and end positions are same as the previous positions and no settings |
|---|
| 500 | } // were changed return the last calculated saiColors string |
|---|
| 501 | |
|---|
| 502 | lastStart = start; lastEnd = end; clrDefinitionsChanged = false; // storing start and end positions |
|---|
| 503 | |
|---|
| 504 | int seqSize = end-start+1; |
|---|
| 505 | |
|---|
| 506 | if (seqSize>seqBufferSize) { |
|---|
| 507 | free(saiColors); |
|---|
| 508 | seqBufferSize = seqSize; |
|---|
| 509 | ARB_calloc(saiColors, seqBufferSize); |
|---|
| 510 | } |
|---|
| 511 | else memset(saiColors, 0, sizeof(char)*seqSize); |
|---|
| 512 | |
|---|
| 513 | char *saiSelected = awr->awar(AWAR_SAI_VISUALIZED)->read_string(); |
|---|
| 514 | |
|---|
| 515 | GBDATA *gb_main = ED4_ROOT->get_gb_main(); |
|---|
| 516 | GB_push_transaction(gb_main); |
|---|
| 517 | |
|---|
| 518 | char *alignment_name = GBT_get_default_alignment(gb_main); |
|---|
| 519 | GBDATA *gb_extended = GBT_find_SAI(gb_main, saiSelected); |
|---|
| 520 | bool visualize = false; // set to true if all goes fine |
|---|
| 521 | |
|---|
| 522 | if (gb_extended) { |
|---|
| 523 | GBDATA *gb_ali = GB_entry(gb_extended, alignment_name); |
|---|
| 524 | if (gb_ali) { |
|---|
| 525 | const char *saiData = NULp; |
|---|
| 526 | bool free_saiData = false; |
|---|
| 527 | |
|---|
| 528 | { |
|---|
| 529 | GBDATA *saiSequence = GB_entry(gb_ali, "data"); // search "data" entry (normal SAI) |
|---|
| 530 | if (saiSequence) { |
|---|
| 531 | saiData = GB_read_char_pntr(saiSequence); // not allocated |
|---|
| 532 | } |
|---|
| 533 | else { |
|---|
| 534 | saiSequence = GB_entry(gb_ali, "bits"); // search "bits" entry (binary SAI) |
|---|
| 535 | if (saiSequence) { |
|---|
| 536 | saiData = GB_read_as_string(saiSequence); // allocated |
|---|
| 537 | free_saiData = true; // free saiData below |
|---|
| 538 | } |
|---|
| 539 | } |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | if (saiData) { |
|---|
| 543 | char trans_table[256]; |
|---|
| 544 | { |
|---|
| 545 | // build the translation table: |
|---|
| 546 | memset(trans_table, 0, 256); |
|---|
| 547 | for (int i = 0; i<AWAR_SAI_CLR_COUNT; ++i) { |
|---|
| 548 | char *def = awr->awar(getAwarName(i))->read_string(); |
|---|
| 549 | int clrRange = ED4_G_CBACK_0 + i; |
|---|
| 550 | |
|---|
| 551 | for (char *d = def; *d; ++d) { |
|---|
| 552 | trans_table[(unsigned char)*d] = clrRange; |
|---|
| 553 | } |
|---|
| 554 | free(def); |
|---|
| 555 | } |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | // translate SAI to colors |
|---|
| 559 | for (int i = start; i <= end; ++i) { |
|---|
| 560 | saiColors[i-start] = trans_table[(unsigned char)saiData[i]]; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | visualize = true; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | if (free_saiData) { |
|---|
| 567 | free(const_cast<char*>(saiData)); // in this case saiData is allocated (see above) |
|---|
| 568 | } |
|---|
| 569 | } |
|---|
| 570 | } |
|---|
| 571 | free(alignment_name); |
|---|
| 572 | free(saiSelected); |
|---|
| 573 | GB_pop_transaction(gb_main); |
|---|
| 574 | |
|---|
| 575 | lastVisualize = visualize; |
|---|
| 576 | if (visualize) { |
|---|
| 577 | e4_assert(saiColors); |
|---|
| 578 | return saiColors-start; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | return NULp; // don't visualize (sth went wrong) |
|---|
| 582 | } |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | // -------------------- Creating Windows and Display dialogs -------------------- |
|---|
| 586 | |
|---|
| 587 | static void reverseColorTranslationTable(AW_window *aww) { |
|---|
| 588 | AW_root *aw_root = aww->get_root(); |
|---|
| 589 | |
|---|
| 590 | int j = AWAR_SAI_CLR_COUNT-1; |
|---|
| 591 | for (int i = 0; i<AWAR_SAI_CLR_COUNT/2+1; ++i, --j) { |
|---|
| 592 | if (i<j) { |
|---|
| 593 | AW_awar *aw_i = aw_root->awar(getAwarName(i)); |
|---|
| 594 | AW_awar *aw_j = aw_root->awar(getAwarName(j)); |
|---|
| 595 | |
|---|
| 596 | char *ci = aw_i->read_string(); |
|---|
| 597 | char *cj = aw_j->read_string(); |
|---|
| 598 | |
|---|
| 599 | aw_i->write_string(cj); |
|---|
| 600 | aw_j->write_string(ci); |
|---|
| 601 | |
|---|
| 602 | free(ci); |
|---|
| 603 | free(cj); |
|---|
| 604 | } |
|---|
| 605 | } |
|---|
| 606 | } |
|---|
| 607 | |
|---|
| 608 | static AW_window *create_editColorTranslationTable_window(AW_root *aw_root) { // creates edit color translation table window |
|---|
| 609 | static AW_window_simple *aws = NULp; |
|---|
| 610 | if (!aws) { |
|---|
| 611 | aws = new AW_window_simple; |
|---|
| 612 | aws->init(aw_root, "EDIT_CTT", "Color Translation Table"); |
|---|
| 613 | aws->load_xfig("saiColorRange.fig"); |
|---|
| 614 | |
|---|
| 615 | char at_name[] = "rangex"; |
|---|
| 616 | char *dig = strchr(at_name, 0)-1; |
|---|
| 617 | |
|---|
| 618 | for (int i = 0; i<AWAR_SAI_CLR_COUNT; ++i) { |
|---|
| 619 | dig[0] = '0'+i; |
|---|
| 620 | aws->at(at_name); |
|---|
| 621 | aws->create_input_field(getAwarName(i), 20); |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | aws->at("close"); |
|---|
| 625 | aws->callback(AW_POPDOWN); |
|---|
| 626 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 627 | |
|---|
| 628 | aws->at("reverse"); |
|---|
| 629 | aws->callback(reverseColorTranslationTable); |
|---|
| 630 | aws->create_button("REVERSE", "Reverse", "R"); |
|---|
| 631 | |
|---|
| 632 | aws->at("colors"); |
|---|
| 633 | aws->callback(makeWindowCallback(ED4_popup_gc_window, ED4_ROOT->gc_manager)); |
|---|
| 634 | aws->button_length(0); |
|---|
| 635 | aws->create_button("COLORS", "#colors.xpm"); |
|---|
| 636 | } |
|---|
| 637 | return aws; |
|---|
| 638 | } |
|---|
| 639 | |
|---|
| 640 | AW_window *ED4_createVisualizeSAI_window(AW_root *aw_root) { |
|---|
| 641 | static AW_window_simple *aws = NULp; |
|---|
| 642 | if (!aws) { |
|---|
| 643 | aws = new AW_window_simple; |
|---|
| 644 | |
|---|
| 645 | aws->init(aw_root, "VISUALIZE_SAI", "Visualize SAIs"); |
|---|
| 646 | aws->load_xfig("visualizeSAI.fig"); |
|---|
| 647 | |
|---|
| 648 | aws->callback(makeHelpCallback("visualizeSAI.hlp")); |
|---|
| 649 | aws->at("help"); |
|---|
| 650 | aws->create_button("HELP", "HELP", "H"); |
|---|
| 651 | |
|---|
| 652 | aws->at("close"); |
|---|
| 653 | aws->callback(AW_POPDOWN); |
|---|
| 654 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 655 | |
|---|
| 656 | aws->at("enable"); |
|---|
| 657 | aws->create_toggle(AWAR_SAI_ENABLE); |
|---|
| 658 | |
|---|
| 659 | aws->at("sai"); |
|---|
| 660 | aws->button_length(30); |
|---|
| 661 | awt_create_SAI_selection_button(ED4_ROOT->get_gb_main(), aws, AWAR_SAI_VISUALIZED); |
|---|
| 662 | |
|---|
| 663 | aws->at("auto_select"); |
|---|
| 664 | aws->create_toggle(AWAR_SAI_AUTO_SELECT); |
|---|
| 665 | |
|---|
| 666 | aws->at("clrTrList"); |
|---|
| 667 | AW_selection_list *clrTransTableLst = buildClrTransTabNamesList(aws); |
|---|
| 668 | |
|---|
| 669 | aws->at("edit"); |
|---|
| 670 | aws->button_length(10); |
|---|
| 671 | aws->callback(create_editColorTranslationTable_window); |
|---|
| 672 | aws->create_button("EDIT", "EDIT"); |
|---|
| 673 | |
|---|
| 674 | aws->at("create"); |
|---|
| 675 | aws->callback(makeWindowCallback(createCopy_ClrTransTab_cb, ED4_VIS_CREATE)); |
|---|
| 676 | aws->create_button("CREATE", "CREATE"); |
|---|
| 677 | |
|---|
| 678 | aws->at("copy"); |
|---|
| 679 | aws->callback(makeWindowCallback(createCopy_ClrTransTab_cb, ED4_VIS_COPY)); |
|---|
| 680 | aws->create_button("COPY", "COPY"); |
|---|
| 681 | |
|---|
| 682 | aws->at("delete"); |
|---|
| 683 | aws->callback(deleteColorTranslationTable); |
|---|
| 684 | aws->create_button("DELETE", "DELETE"); |
|---|
| 685 | |
|---|
| 686 | aws->at("marked"); |
|---|
| 687 | aws->create_toggle_field(AWAR_SAI_ALL_SPECIES, 1); |
|---|
| 688 | aws->insert_toggle("MARKED SPECIES", "M", 0); |
|---|
| 689 | aws->insert_toggle("ALL SPECIES", "A", 1); |
|---|
| 690 | aws->update_toggle_field(); |
|---|
| 691 | |
|---|
| 692 | AW_awar *trans_tabs = aw_root->awar(AWAR_SAI_CLR_TRANS_TAB_NAMES); |
|---|
| 693 | trans_tabs->add_callback(makeRootCallback(update_ClrTransTabNamesList_cb, clrTransTableLst)); |
|---|
| 694 | trans_tabs->touch(); // force update |
|---|
| 695 | } |
|---|
| 696 | aws->show(); |
|---|
| 697 | |
|---|
| 698 | return aws; |
|---|
| 699 | } |
|---|
| 700 | |
|---|