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); |
---|
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 | GBDATA *gb_extended = GBT_find_SAI(gb_main, saiSelected); |
---|
519 | bool visualize = false; // set to true if all goes fine |
---|
520 | |
---|
521 | if (gb_extended) { |
---|
522 | GBDATA *gb_ali = GB_entry(gb_extended, ED4_ROOT->get_alignment_name()); |
---|
523 | if (gb_ali) { |
---|
524 | const char *saiData = NULp; |
---|
525 | bool free_saiData = false; |
---|
526 | |
---|
527 | { |
---|
528 | GBDATA *saiSequence = GB_entry(gb_ali, "data"); // search "data" entry (normal SAI) |
---|
529 | if (saiSequence) { |
---|
530 | saiData = GB_read_char_pntr(saiSequence); // not allocated |
---|
531 | } |
---|
532 | else { |
---|
533 | saiSequence = GB_entry(gb_ali, "bits"); // search "bits" entry (binary SAI) |
---|
534 | if (saiSequence) { |
---|
535 | saiData = GB_read_as_string(saiSequence); // allocated |
---|
536 | free_saiData = true; // free saiData below |
---|
537 | } |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | if (saiData) { |
---|
542 | char trans_table[256]; |
---|
543 | { |
---|
544 | // build the translation table: |
---|
545 | memset(trans_table, 0, 256); |
---|
546 | for (int i = 0; i<AWAR_SAI_CLR_COUNT; ++i) { |
---|
547 | char *def = awr->awar(getAwarName(i))->read_string(); |
---|
548 | int clrRange = ED4_G_CBACK_0 + i; |
---|
549 | |
---|
550 | for (char *d = def; *d; ++d) { |
---|
551 | trans_table[(unsigned char)*d] = clrRange; |
---|
552 | } |
---|
553 | free(def); |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | // translate SAI to colors |
---|
558 | for (int i = start; i <= end; ++i) { |
---|
559 | saiColors[i-start] = trans_table[(unsigned char)saiData[i]]; |
---|
560 | } |
---|
561 | |
---|
562 | visualize = true; |
---|
563 | } |
---|
564 | |
---|
565 | if (free_saiData) { |
---|
566 | free(const_cast<char*>(saiData)); // in this case saiData is allocated (see above) |
---|
567 | } |
---|
568 | } |
---|
569 | } |
---|
570 | free(saiSelected); |
---|
571 | GB_pop_transaction(gb_main); |
---|
572 | |
---|
573 | lastVisualize = visualize; |
---|
574 | if (visualize) { |
---|
575 | e4_assert(saiColors); |
---|
576 | return saiColors-start; |
---|
577 | } |
---|
578 | |
---|
579 | return NULp; // don't visualize (sth went wrong) |
---|
580 | } |
---|
581 | |
---|
582 | |
---|
583 | // -------------------- Creating Windows and Display dialogs -------------------- |
---|
584 | |
---|
585 | static void reverseColorTranslationTable(AW_window *aww) { |
---|
586 | AW_root *aw_root = aww->get_root(); |
---|
587 | |
---|
588 | int j = AWAR_SAI_CLR_COUNT-1; |
---|
589 | for (int i = 0; i<AWAR_SAI_CLR_COUNT/2+1; ++i, --j) { |
---|
590 | if (i<j) { |
---|
591 | AW_awar *aw_i = aw_root->awar(getAwarName(i)); |
---|
592 | AW_awar *aw_j = aw_root->awar(getAwarName(j)); |
---|
593 | |
---|
594 | char *ci = aw_i->read_string(); |
---|
595 | char *cj = aw_j->read_string(); |
---|
596 | |
---|
597 | aw_i->write_string(cj); |
---|
598 | aw_j->write_string(ci); |
---|
599 | |
---|
600 | free(ci); |
---|
601 | free(cj); |
---|
602 | } |
---|
603 | } |
---|
604 | } |
---|
605 | |
---|
606 | static AW_window *create_editColorTranslationTable_window(AW_root *aw_root) { // creates edit color translation table window |
---|
607 | static AW_window_simple *aws = NULp; |
---|
608 | if (!aws) { |
---|
609 | aws = new AW_window_simple; |
---|
610 | aws->init(aw_root, "EDIT_CTT", "Color Translation Table"); |
---|
611 | aws->load_xfig("saiColorRange.fig"); |
---|
612 | |
---|
613 | char at_name[] = "rangex"; |
---|
614 | char *dig = strchr(at_name, 0)-1; |
---|
615 | |
---|
616 | for (int i = 0; i<AWAR_SAI_CLR_COUNT; ++i) { |
---|
617 | dig[0] = '0'+i; |
---|
618 | aws->at(at_name); |
---|
619 | aws->create_input_field(getAwarName(i), 20); |
---|
620 | } |
---|
621 | |
---|
622 | aws->at("close"); |
---|
623 | aws->callback(AW_POPDOWN); |
---|
624 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
625 | |
---|
626 | aws->at("reverse"); |
---|
627 | aws->callback(reverseColorTranslationTable); |
---|
628 | aws->create_button("REVERSE", "Reverse", "R"); |
---|
629 | |
---|
630 | aws->at("colors"); |
---|
631 | aws->callback(makeWindowCallback(ED4_popup_gc_window, ED4_ROOT->gc_manager)); |
---|
632 | aws->button_length(0); |
---|
633 | aws->create_button("COLORS", "#colors.xpm"); |
---|
634 | } |
---|
635 | return aws; |
---|
636 | } |
---|
637 | |
---|
638 | AW_window *ED4_createVisualizeSAI_window(AW_root *aw_root) { |
---|
639 | static AW_window_simple *aws = NULp; |
---|
640 | if (!aws) { |
---|
641 | aws = new AW_window_simple; |
---|
642 | |
---|
643 | aws->init(aw_root, "VISUALIZE_SAI", "Visualize SAIs"); |
---|
644 | aws->load_xfig("visualizeSAI.fig"); |
---|
645 | |
---|
646 | aws->callback(makeHelpCallback("visualizeSAI.hlp")); |
---|
647 | aws->at("help"); |
---|
648 | aws->create_button("HELP", "HELP", "H"); |
---|
649 | |
---|
650 | aws->at("close"); |
---|
651 | aws->callback(AW_POPDOWN); |
---|
652 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
653 | |
---|
654 | aws->at("enable"); |
---|
655 | aws->create_toggle(AWAR_SAI_ENABLE); |
---|
656 | |
---|
657 | aws->at("sai"); |
---|
658 | aws->button_length(30); |
---|
659 | awt_create_SAI_selection_button(ED4_ROOT->get_gb_main(), aws, AWAR_SAI_VISUALIZED); |
---|
660 | |
---|
661 | aws->at("auto_select"); |
---|
662 | aws->create_toggle(AWAR_SAI_AUTO_SELECT); |
---|
663 | |
---|
664 | aws->at("clrTrList"); |
---|
665 | AW_selection_list *clrTransTableLst = buildClrTransTabNamesList(aws); |
---|
666 | |
---|
667 | aws->at("edit"); |
---|
668 | aws->button_length(10); |
---|
669 | aws->callback(create_editColorTranslationTable_window); |
---|
670 | aws->create_button("EDIT", "EDIT"); |
---|
671 | |
---|
672 | aws->at("create"); |
---|
673 | aws->callback(makeWindowCallback(createCopy_ClrTransTab_cb, ED4_VIS_CREATE)); |
---|
674 | aws->create_button("CREATE", "CREATE"); |
---|
675 | |
---|
676 | aws->at("copy"); |
---|
677 | aws->callback(makeWindowCallback(createCopy_ClrTransTab_cb, ED4_VIS_COPY)); |
---|
678 | aws->create_button("COPY", "COPY"); |
---|
679 | |
---|
680 | aws->at("delete"); |
---|
681 | aws->callback(deleteColorTranslationTable); |
---|
682 | aws->create_button("DELETE", "DELETE"); |
---|
683 | |
---|
684 | aws->at("marked"); |
---|
685 | aws->create_toggle_field(AWAR_SAI_ALL_SPECIES, AW_HORIZONTAL); |
---|
686 | aws->insert_toggle("MARKED SPECIES", "M", 0); |
---|
687 | aws->insert_toggle("ALL SPECIES", "A", 1); |
---|
688 | aws->update_toggle_field(); |
---|
689 | |
---|
690 | AW_awar *trans_tabs = aw_root->awar(AWAR_SAI_CLR_TRANS_TAB_NAMES); |
---|
691 | trans_tabs->add_callback(makeRootCallback(update_ClrTransTabNamesList_cb, clrTransTableLst)); |
---|
692 | trans_tabs->touch(); // force update |
---|
693 | } |
---|
694 | aws->show(); |
---|
695 | |
---|
696 | return aws; |
---|
697 | } |
---|
698 | |
---|