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