1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : SaiProbeVisualization.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "SaiProbeVisualization.hxx" |
---|
12 | #include "probe_match_parser.hxx" |
---|
13 | |
---|
14 | #include <nds.h> |
---|
15 | #include <items.h> |
---|
16 | #include <awt_sel_boxes.hxx> |
---|
17 | #include <awt_config_manager.hxx> |
---|
18 | #include <aw_awars.hxx> |
---|
19 | #include <aw_root.hxx> |
---|
20 | #include <aw_preset.hxx> |
---|
21 | #include <aw_msg.hxx> |
---|
22 | #include <arbdbt.h> |
---|
23 | |
---|
24 | #include <iostream> |
---|
25 | |
---|
26 | |
---|
27 | using namespace std; |
---|
28 | |
---|
29 | #define PROBE_PREFIX_LENGTH 9 |
---|
30 | #define PROBE_SUFFIX_LENGTH 9 |
---|
31 | |
---|
32 | static saiProbeData *g_pbdata = 0; |
---|
33 | static char *saiValues = 0; |
---|
34 | static bool in_colorDefChanged_callback = false; // used to avoid colorDef correction |
---|
35 | |
---|
36 | #define BUFSIZE 100 |
---|
37 | static const char *getAwarName(int awarNo) { |
---|
38 | static char buf[BUFSIZE]; |
---|
39 | |
---|
40 | strcpy(buf, AWAR_SPV_SAI_COLOR); |
---|
41 | (strchr(buf, 0)-1)[0] = '0'+awarNo; |
---|
42 | |
---|
43 | return buf; |
---|
44 | } |
---|
45 | |
---|
46 | AW_gc_manager SAI_graphic::init_devices(AW_window *aww, AW_device *device, AWT_canvas *scr) { |
---|
47 | AW_gc_manager gc_manager = |
---|
48 | AW_manage_GC(aww, |
---|
49 | scr->get_gc_base_name(), |
---|
50 | device, |
---|
51 | SAI_GC_HIGHLIGHT, |
---|
52 | SAI_GC_MAX, |
---|
53 | AW_GCM_DATA_AREA, |
---|
54 | makeWindowCallback(AWT_resize_cb, scr), |
---|
55 | false, |
---|
56 | "#005500", |
---|
57 | "Selected Probe$#FF0000", |
---|
58 | "Foreground$#FFAA00", |
---|
59 | "Probe$#FFFF00", |
---|
60 | "+-COLOR 0$#FFFFFF", "-COLOR 1$#E0E0E0", |
---|
61 | "+-COLOR 2$#C0C0C0", "-COLOR 3$#A0A0A0", |
---|
62 | "+-COLOR 4$#909090", "-COLOR 5$#808080", |
---|
63 | "+-COLOR 6$#707070", "-COLOR 7$#505050", |
---|
64 | "+-COLOR 8$#404040", "-COLOR 9$#303030", |
---|
65 | NULL); |
---|
66 | |
---|
67 | return gc_manager; |
---|
68 | } |
---|
69 | |
---|
70 | SAI_graphic::SAI_graphic(AW_root *aw_rooti, GBDATA *gb_maini) { |
---|
71 | exports.zoom_mode = AWT_ZOOM_NEVER; |
---|
72 | exports.fit_mode = AWT_FIT_NEVER; |
---|
73 | |
---|
74 | exports.set_standard_default_padding(); |
---|
75 | |
---|
76 | this->aw_root = aw_rooti; |
---|
77 | this->gb_main = gb_maini; |
---|
78 | } |
---|
79 | |
---|
80 | void SAI_graphic::handle_command(AW_device *, AWT_graphic_event& event) { |
---|
81 | if (event.type() == AW_Mouse_Press && event.button() != AW_BUTTON_MIDDLE) { |
---|
82 | const AW_clicked_element *clicked = event.best_click(); |
---|
83 | if (clicked->is_text()) { |
---|
84 | int clicked_idx = (int)clicked->cd1(); |
---|
85 | const char *species_name = g_pbdata->probeSpecies[clicked_idx]; |
---|
86 | |
---|
87 | aw_root->awar(AWAR_SPECIES_NAME)->write_string(species_name); |
---|
88 | aw_root->awar(AWAR_SPV_SELECTED_PROBE)->write_string(species_name); |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | SAI_graphic::~SAI_graphic() {} |
---|
94 | |
---|
95 | void SAI_graphic::show(AW_device *device) { |
---|
96 | paint(device); |
---|
97 | } |
---|
98 | |
---|
99 | void SAI_graphic::info(AW_device */*device*/, AW_pos /*x*/, AW_pos /*y*/, AW_clicked_line */*cl*/, AW_clicked_text */*ct*/) { |
---|
100 | aw_message("INFO MESSAGE"); |
---|
101 | } |
---|
102 | |
---|
103 | static void colorDefChanged_callback(AW_root *awr, int awarNo) { |
---|
104 | if (!in_colorDefChanged_callback) { |
---|
105 | LocallyModify<bool> flag(in_colorDefChanged_callback, true); |
---|
106 | unsigned char charUsed[256]; memset(charUsed, 255, 256); |
---|
107 | { |
---|
108 | for (int i=0; i<10; i++) { |
---|
109 | char *awarString_next = awr->awar_string(getAwarName(i))->read_string(); |
---|
110 | for (int c=0; awarString_next[c]; ++c) { |
---|
111 | charUsed[(unsigned char)awarString_next[c]] = i; |
---|
112 | } |
---|
113 | free(awarString_next); |
---|
114 | } |
---|
115 | char *awarString = awr->awar_string(getAwarName(awarNo))->read_string(); |
---|
116 | |
---|
117 | for (int c = 0; awarString[c]; ++c) { |
---|
118 | charUsed[(unsigned char)awarString[c]] = awarNo; |
---|
119 | } |
---|
120 | free(awarString); |
---|
121 | |
---|
122 | typedef unsigned char mystr[256]; |
---|
123 | mystr s[10]; |
---|
124 | for (int i=0; i<10; i++) s[i][0]=0; // initializing the strings |
---|
125 | |
---|
126 | for (int i=0; i<256; i++) { |
---|
127 | int table = charUsed[i]; |
---|
128 | if (table != 255) { |
---|
129 | char *eos = strchr((char *)s[table], 0); // get pointer to end of string |
---|
130 | eos[0] = char(i); |
---|
131 | eos[1] = 0; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | for (int i=0; i<10; i++) { |
---|
136 | awr->awar_string(getAwarName(i))->write_string((char *)s[i]); |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|
140 | awr->awar(AWAR_SPV_DISP_SAI)->touch(); // refreshes the display |
---|
141 | } |
---|
142 | |
---|
143 | static void refreshCanvas(AW_root*, AWT_canvas *scr) { |
---|
144 | // repaints the canvas |
---|
145 | scr->refresh(); |
---|
146 | } |
---|
147 | |
---|
148 | static void createSaiProbeAwars(AW_root *aw_root) { |
---|
149 | // creating awars specific for the painting routine |
---|
150 | aw_root->awar_int(AWAR_SPV_DISP_SAI, 0, AW_ROOT_DEFAULT); // to display SAI values |
---|
151 | |
---|
152 | for (int i = 0; i < 10; i++) { // initializing 10 color definition string AWARS |
---|
153 | AW_awar *def_awar = aw_root->awar_string(getAwarName(i), "", AW_ROOT_DEFAULT); |
---|
154 | def_awar->add_callback(makeRootCallback(colorDefChanged_callback, i)); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | static void addCallBacks(AW_root *awr, AWT_canvas *scr) { |
---|
159 | // add callbacks to the awars (refresh display on change) |
---|
160 | RootCallback refresh_cb = makeRootCallback(refreshCanvas, scr); |
---|
161 | |
---|
162 | awr->awar(AWAR_SPV_DISP_SAI) ->add_callback(refresh_cb); |
---|
163 | awr->awar(AWAR_SPV_SAI_2_PROBE) ->add_callback(refresh_cb); |
---|
164 | awr->awar(AWAR_SPV_DB_FIELD_NAME) ->add_callback(refresh_cb); |
---|
165 | awr->awar(AWAR_SPV_DB_FIELD_WIDTH)->add_callback(refresh_cb); |
---|
166 | awr->awar(AWAR_SPV_SELECTED_PROBE)->add_callback(refresh_cb); |
---|
167 | awr->awar(AWAR_SPV_ACI_COMMAND) ->add_callback(refresh_cb); |
---|
168 | } |
---|
169 | |
---|
170 | static const char *translateSAItoColors(AW_root *awr, GBDATA *gb_main, int start, int end, int speciesNo) { |
---|
171 | // translating SAIs to color strings |
---|
172 | static int seqBufferSize = 0; |
---|
173 | static char *saiColors = 0; |
---|
174 | |
---|
175 | if (start >= end) return 0; |
---|
176 | |
---|
177 | int seqSize = (end - start) + 1; |
---|
178 | |
---|
179 | if (seqSize > seqBufferSize) { |
---|
180 | free(saiColors); |
---|
181 | seqBufferSize = seqSize; |
---|
182 | saiColors = (char*)malloc(seqBufferSize); |
---|
183 | saiValues = (char*)malloc(seqBufferSize); |
---|
184 | } |
---|
185 | |
---|
186 | memset(saiColors, '0'-1, seqSize); |
---|
187 | memset(saiValues, '0'-1, seqSize); |
---|
188 | |
---|
189 | char *saiSelected = awr->awar(AWAR_SPV_SAI_2_PROBE)->read_string(); |
---|
190 | |
---|
191 | GB_push_transaction(gb_main); |
---|
192 | char *alignment_name = GBT_get_default_alignment(gb_main); |
---|
193 | GBDATA *gb_extended = GBT_find_SAI(gb_main, saiSelected); |
---|
194 | int positions = 0; |
---|
195 | |
---|
196 | if (gb_extended) { |
---|
197 | GBDATA *gb_ali = GB_entry(gb_extended, alignment_name); |
---|
198 | if (gb_ali) { |
---|
199 | const char *saiData = 0; |
---|
200 | const char *seqData = 0; |
---|
201 | bool free_saiData = false; |
---|
202 | |
---|
203 | { |
---|
204 | GBDATA *saiSequence = GB_entry(gb_ali, "data"); // search "data" entry (normal SAI) |
---|
205 | if (saiSequence) { |
---|
206 | saiData = GB_read_char_pntr(saiSequence); // not allocated |
---|
207 | } |
---|
208 | else { |
---|
209 | saiSequence = GB_entry(gb_ali, "bits"); // search "bits" entry (binary SAI) |
---|
210 | if (saiSequence) { |
---|
211 | saiData = GB_read_as_string(saiSequence); // allocated |
---|
212 | free_saiData = true; // free saiData below |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | const char *species_name = g_pbdata->probeSpecies[speciesNo]; |
---|
217 | GBDATA *gb_species = GBT_find_species(gb_main, species_name); |
---|
218 | GBDATA *gb_seq_data = GBT_find_sequence(gb_species, alignment_name); |
---|
219 | if (gb_seq_data) seqData = GB_read_char_pntr(gb_seq_data); |
---|
220 | } |
---|
221 | |
---|
222 | if (saiData) { |
---|
223 | char trans_table[256]; |
---|
224 | { |
---|
225 | |
---|
226 | // @@@ FIXME: build trans_table ONCE for each refresh only (not for each translation) |
---|
227 | |
---|
228 | // build the translation table |
---|
229 | memset(trans_table, '0'-1, 256); |
---|
230 | for (int i = 0; i < SAI_CLR_COUNT; ++i) { |
---|
231 | char *def = awr->awar(getAwarName(i))->read_string(); |
---|
232 | int clrRange = i + '0'; // configured values use '0' to '9' (unconfigured use '0'-1) |
---|
233 | |
---|
234 | for (const char *d = def; *d; ++d) { |
---|
235 | trans_table[(unsigned char)*d] = clrRange; |
---|
236 | } |
---|
237 | free(def); |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | // translate SAI to colors |
---|
242 | int i, j; |
---|
243 | for (i = start, j = 0; i <= end; ++i) { |
---|
244 | if ((seqData[i] != '.') && (seqData[i] != '-')) { |
---|
245 | saiColors[j] = trans_table[(unsigned char)saiData[i]]; |
---|
246 | saiValues[j] = saiData[i]; |
---|
247 | ++j; |
---|
248 | } |
---|
249 | } |
---|
250 | positions = j; |
---|
251 | } |
---|
252 | |
---|
253 | if (free_saiData) { |
---|
254 | free(const_cast<char*>(saiData)); // in this case saiData is allocated (see above) |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | saiColors[positions] = 0; |
---|
260 | saiValues[positions] = 0; |
---|
261 | |
---|
262 | free(alignment_name); |
---|
263 | free(saiSelected); |
---|
264 | GB_pop_transaction(gb_main); |
---|
265 | |
---|
266 | return saiColors; |
---|
267 | } |
---|
268 | |
---|
269 | static int calculateEndPosition(GBDATA *gb_main, int startPos, int speciesNo, int mode, int probeLen, GB_ERROR *err) { |
---|
270 | // returns -2 in case of error |
---|
271 | // Note: if mode == 'PROBE_PREFIX' the result is 1 in front of last base (and so may be -1) |
---|
272 | |
---|
273 | int i, endPos, baseCntr; |
---|
274 | i = baseCntr = 0; |
---|
275 | endPos = -2; |
---|
276 | *err = 0; |
---|
277 | |
---|
278 | GB_push_transaction(gb_main); |
---|
279 | char *alignment_name = GBT_get_default_alignment(gb_main); |
---|
280 | const char *species_name = g_pbdata->probeSpecies[speciesNo]; |
---|
281 | GBDATA *gb_species = GBT_find_species(gb_main, species_name); |
---|
282 | if (gb_species) { |
---|
283 | GBDATA *gb_seq_data = GBT_find_sequence(gb_species, alignment_name); |
---|
284 | if (gb_seq_data) { |
---|
285 | const char *seqData = GB_read_char_pntr(gb_seq_data); |
---|
286 | |
---|
287 | if (seqData) { |
---|
288 | switch (mode) { |
---|
289 | case PROBE: |
---|
290 | for (i = startPos; baseCntr < probeLen; ++i) { |
---|
291 | if ((seqData[i] != '-') && (seqData[i] != '.')) |
---|
292 | baseCntr++; |
---|
293 | } |
---|
294 | break; |
---|
295 | case PROBE_PREFIX: |
---|
296 | for (i = startPos; baseCntr < PROBE_PREFIX_LENGTH && i >= 0; --i) { |
---|
297 | if ((seqData[i] != '-') && (seqData[i] != '.')) |
---|
298 | baseCntr++; |
---|
299 | } |
---|
300 | break; |
---|
301 | case PROBE_SUFFIX: |
---|
302 | for (i = startPos; baseCntr < PROBE_SUFFIX_LENGTH && seqData[i]; ++i) { |
---|
303 | if ((seqData[i] != '-') && (seqData[i] != '.')) |
---|
304 | baseCntr++; |
---|
305 | } |
---|
306 | break; |
---|
307 | } |
---|
308 | endPos = i; |
---|
309 | } |
---|
310 | else { |
---|
311 | *err = "can't read data"; |
---|
312 | } |
---|
313 | } |
---|
314 | else { |
---|
315 | *err = "no data"; |
---|
316 | } |
---|
317 | } |
---|
318 | else { |
---|
319 | *err = "species not found"; |
---|
320 | } |
---|
321 | free(alignment_name); |
---|
322 | GB_pop_transaction(gb_main); |
---|
323 | |
---|
324 | return endPos; |
---|
325 | } |
---|
326 | |
---|
327 | // -------------------------------------------------------------------------------- |
---|
328 | // painting routine |
---|
329 | |
---|
330 | static void paintBackgroundAndSAI (AW_device *device, size_t probeRegionLen, AW_pos pbRgX1, AW_pos pbY, AW_pos pbMaxWidth, AW_pos pbMaxHeight, |
---|
331 | const char *saiCols, int dispSai) |
---|
332 | { |
---|
333 | // painting background in translated colors from the chosen SAI values |
---|
334 | // and also printing the values based on the options set by user |
---|
335 | for (size_t j = 0; j<probeRegionLen; j++) { |
---|
336 | if (saiCols[j] >= '0') { |
---|
337 | device->box(saiCols[j]-'0'+SAI_GC_0, true, pbRgX1+j*pbMaxWidth, pbY-pbMaxHeight+1, pbMaxWidth, pbMaxHeight); |
---|
338 | } |
---|
339 | if (dispSai && saiValues[j]) { |
---|
340 | char saiVal[2]; |
---|
341 | saiVal[0] = saiValues[j]; |
---|
342 | saiVal[1] = 0; |
---|
343 | device->text(SAI_GC_FOREGROUND, saiVal, (pbRgX1+(j*pbMaxWidth)), pbY+pbMaxHeight, 0, AW_SCREEN, 0); |
---|
344 | } |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | // static void paintProbeInfo(AW_device *device, const char *probe_info, AW_pos x, AW_pos y, AW_pos xoff, AW_pos yoff, AW_pos maxDescent, AW_CL clientdata, int textCOLOR) { |
---|
349 | static void paintProbeInfo(AW_device *device, const char *probe_info, AW_pos x, AW_pos y, AW_pos xoff, AW_pos yoff, AW_pos maxDescent, int textCOLOR) { |
---|
350 | char probeChar[2]; |
---|
351 | probeChar[1] = 0; |
---|
352 | |
---|
353 | for (size_t j = 0; probe_info[j]; ++j) { |
---|
354 | if (probe_info[j] == '=') { |
---|
355 | AW_pos yl = y-maxDescent-(yoff-maxDescent)/3; |
---|
356 | AW_pos xl = x+xoff*j; |
---|
357 | device->line(SAI_GC_PROBE, xl, yl, xl+xoff-1, yl); |
---|
358 | } |
---|
359 | else { |
---|
360 | probeChar[0] = probe_info[j]; |
---|
361 | // device->text(textCOLOR, probeChar, x+j*xoff, y-maxDescent, 0, AW_SCREEN|AW_CLICK, clientdata, 0); |
---|
362 | device->text(textCOLOR, probeChar, x+j*xoff, y-maxDescent, 0, AW_SCREEN|AW_CLICK); |
---|
363 | } |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | static char *GetDisplayInfo(AW_root *root, GBDATA *gb_main, const char *speciesName, size_t displayWidth, const char *default_tree_name) |
---|
368 | { |
---|
369 | GB_ERROR error = 0; |
---|
370 | char *displayInfo = 0; |
---|
371 | GB_transaction ta(gb_main); |
---|
372 | GBDATA *gb_Species = GBT_expect_species(gb_main, speciesName); |
---|
373 | |
---|
374 | if (!gb_Species) error = GB_await_error(); |
---|
375 | else { |
---|
376 | char *field_content = 0; |
---|
377 | { |
---|
378 | char *dbFieldName = root->awar_string(AWAR_SPV_DB_FIELD_NAME)->read_string(); |
---|
379 | GBDATA *gb_field = GB_search(gb_Species, dbFieldName, GB_FIND); |
---|
380 | if (gb_field) { |
---|
381 | field_content = GB_read_as_string(gb_field); |
---|
382 | if (!field_content) error = GB_await_error(); |
---|
383 | } |
---|
384 | else { |
---|
385 | error = GBS_global_string("No entry '%s' in species '%s'", dbFieldName, speciesName); |
---|
386 | } |
---|
387 | free(dbFieldName); |
---|
388 | } |
---|
389 | |
---|
390 | if (!error) { |
---|
391 | char *aciCommand = root->awar_string(AWAR_SPV_ACI_COMMAND)->read_string(); |
---|
392 | displayInfo = GB_command_interpreter(gb_main, field_content, aciCommand, gb_Species, default_tree_name); |
---|
393 | if (!displayInfo) error = GB_await_error(); |
---|
394 | free(aciCommand); |
---|
395 | } |
---|
396 | |
---|
397 | if (displayInfo && strlen(displayInfo)>displayWidth) displayInfo[displayWidth] = 0; // shorten result |
---|
398 | free(field_content); |
---|
399 | } |
---|
400 | |
---|
401 | if (error) freedup(displayInfo, error); // display the error |
---|
402 | |
---|
403 | sai_assert(displayInfo); |
---|
404 | return displayInfo; |
---|
405 | } |
---|
406 | |
---|
407 | void SAI_graphic::paint(AW_device *device) { |
---|
408 | // Painting routine of the canvas based on the probe match results |
---|
409 | |
---|
410 | double xStep_info = 0; |
---|
411 | double xStep_border = 0; |
---|
412 | double xStep_target = 0; |
---|
413 | double yStep = 0; |
---|
414 | double maxDescent = 0; |
---|
415 | // detect x/y step to use |
---|
416 | { |
---|
417 | const AW_font_limits& fgFontLim = device->get_font_limits(SAI_GC_FOREGROUND_FONT, 0); |
---|
418 | const AW_font_limits& pbFontLim = device->get_font_limits(SAI_GC_PROBE_FONT, 0); |
---|
419 | const AW_font_limits& hlFontLim = device->get_font_limits(SAI_GC_HIGHLIGHT_FONT, 0); |
---|
420 | |
---|
421 | AW_font_limits target_font_limits(pbFontLim, hlFontLim); |
---|
422 | AW_font_limits all_font_limits(fgFontLim, target_font_limits); |
---|
423 | |
---|
424 | xStep_info = fgFontLim.width; |
---|
425 | xStep_border = pbFontLim.width; |
---|
426 | xStep_target = target_font_limits.width; |
---|
427 | |
---|
428 | yStep = all_font_limits.height; |
---|
429 | maxDescent = all_font_limits.descent; |
---|
430 | } |
---|
431 | |
---|
432 | AW_pos fgY = yStep + 10; |
---|
433 | AW_pos pbY = yStep + 10; |
---|
434 | |
---|
435 | char *saiSelected = aw_root->awar(AWAR_SPV_SAI_2_PROBE)->read_string(); |
---|
436 | int dispSai = aw_root->awar(AWAR_SPV_DISP_SAI)->read_int(); // to display SAI below probe targets |
---|
437 | int displayWidth = aw_root->awar(AWAR_SPV_DB_FIELD_WIDTH)->read_int(); // display column width of the selected database field |
---|
438 | |
---|
439 | { |
---|
440 | char buf[1024]; |
---|
441 | if (strcmp(saiSelected, "")==0) sprintf(buf, "Selected SAI = Not Selected!"); |
---|
442 | else sprintf(buf, "Selected SAI = %s", saiSelected); |
---|
443 | device->text(SAI_GC_PROBE, buf, 100, -30, 0.0, AW_SCREEN); |
---|
444 | } |
---|
445 | |
---|
446 | double yLineStep = dispSai ? yStep*2 : yStep; |
---|
447 | |
---|
448 | if (g_pbdata) { |
---|
449 | device->text(SAI_GC_PROBE, "Species INFO", 0, 8, 0.0, AW_SCREEN); |
---|
450 | if (!g_pbdata->probeSpecies.empty()) { |
---|
451 | char *default_tree = aw_root->awar(AWAR_TREE)->read_string(); |
---|
452 | char *selectedProbe = aw_root->awar(AWAR_SPV_SELECTED_PROBE)->read_string(); |
---|
453 | |
---|
454 | for (size_t j = 0; j < g_pbdata->probeSpecies.size(); ++j) { |
---|
455 | const char *name = g_pbdata->probeSpecies[j]; |
---|
456 | char *displayInfo = GetDisplayInfo(aw_root, gb_main, name, displayWidth, default_tree); |
---|
457 | |
---|
458 | AW_pos fgX = 0; |
---|
459 | |
---|
460 | AW_click_cd cd(device, j); |
---|
461 | if (strcmp(selectedProbe, name) == 0) { |
---|
462 | device->box(SAI_GC_FOREGROUND, true, fgX, (fgY - (yStep * 0.9)), (displayWidth * xStep_info), yStep); |
---|
463 | device->text(SAI_GC_HIGHLIGHT, displayInfo, fgX, fgY-1, 0, AW_SCREEN|AW_CLICK, 0); |
---|
464 | } |
---|
465 | else { |
---|
466 | device->text(SAI_GC_FOREGROUND, displayInfo, fgX, fgY, 0, AW_SCREEN|AW_CLICK, 0); |
---|
467 | } |
---|
468 | fgY += yLineStep; |
---|
469 | |
---|
470 | free(displayInfo); |
---|
471 | } |
---|
472 | |
---|
473 | free(selectedProbe); |
---|
474 | free(default_tree); |
---|
475 | } |
---|
476 | |
---|
477 | double spacer = 4.0; |
---|
478 | AW_pos lineXpos = 0; |
---|
479 | |
---|
480 | AW_pos pbRgX1 = ((displayWidth+1) * xStep_info); |
---|
481 | AW_pos pbX = pbRgX1 + (9 * xStep_border) + spacer; |
---|
482 | AW_pos pbRgX2 = pbX + (g_pbdata->getProbeTargetLen() * xStep_target) + spacer; |
---|
483 | |
---|
484 | int probeLen = g_pbdata->getProbeTargetLen(); |
---|
485 | |
---|
486 | device->box(SAI_GC_FOREGROUND, true, pbX, 10-yStep, (probeLen * xStep_target), yStep); |
---|
487 | paintProbeInfo(device, g_pbdata->getProbeTarget(), pbX, 10, xStep_target, yStep, maxDescent, SAI_GC_HIGHLIGHT); |
---|
488 | device->set_line_attributes(SAI_GC_FOREGROUND, 2, AW_SOLID); |
---|
489 | |
---|
490 | |
---|
491 | ProbeMatchParser parser(g_pbdata->getProbeTarget(), g_pbdata->getHeadline()); |
---|
492 | if (parser.get_error()) { |
---|
493 | device->text(SAI_GC_PROBE, GBS_global_string("Error: %s", parser.get_error()), pbRgX2, pbY, 0, AW_SCREEN, 0); |
---|
494 | } |
---|
495 | else { |
---|
496 | for (size_t i = 0; i < g_pbdata->probeSeq.size(); ++i) { // loop over all matches |
---|
497 | GB_ERROR error; |
---|
498 | ParsedProbeMatch parsed(g_pbdata->probeSeq[i], parser); |
---|
499 | AW_click_cd cd(device, i); |
---|
500 | |
---|
501 | if ((error = parsed.get_error())) { |
---|
502 | device->text(SAI_GC_PROBE, GBS_global_string("Error: %s", error), pbRgX2, pbY, 0, AW_SCREEN, 0); |
---|
503 | } |
---|
504 | else { |
---|
505 | const char *probeRegion = parsed.get_probe_region(); |
---|
506 | sai_assert(probeRegion); |
---|
507 | char *probeRegion_copy = strdup(probeRegion); |
---|
508 | |
---|
509 | const char *tok_prefix = strtok(probeRegion_copy, "-"); |
---|
510 | const char *tok_infix = tok_prefix ? strtok(0, "-") : 0; |
---|
511 | const char *tok_suffix = tok_infix ? strtok(0, "-") : 0; |
---|
512 | |
---|
513 | if (!tok_suffix) { |
---|
514 | // handle special case where no suffix exists |
---|
515 | const char *end = strchr(probeRegion, 0); |
---|
516 | if (end>probeRegion && end[-1] == '-') tok_suffix = ""; |
---|
517 | } |
---|
518 | |
---|
519 | const char *err = 0; |
---|
520 | if (tok_suffix) { |
---|
521 | // -------------------- |
---|
522 | // pre-probe region - 9 bases |
---|
523 | int startPos = parsed.get_position(); |
---|
524 | if (parsed.get_error()) { |
---|
525 | err = GBS_global_string("Could not parse match position (Reason: %s).", parsed.get_error()); |
---|
526 | } |
---|
527 | else { |
---|
528 | const char *endErr; |
---|
529 | int endPos = calculateEndPosition(gb_main, startPos-1, i, PROBE_PREFIX, probeLen, &endErr); |
---|
530 | if (endPos == -2) { |
---|
531 | err = GBS_global_string("Can't handle '%s' (%s)", g_pbdata->probeSpecies[i], endErr); |
---|
532 | } |
---|
533 | else { |
---|
534 | sai_assert(!endErr); |
---|
535 | sai_assert(endPos >= -1); // note: -1 gets fixed in the next line |
---|
536 | endPos++; // calculateEndPosition returns 'one position in front of start' |
---|
537 | const char *saiCols = translateSAItoColors(aw_root, gb_main, endPos, startPos-1, i); |
---|
538 | if (saiCols) { |
---|
539 | int positions = strlen(saiCols); |
---|
540 | int skipLeft = PROBE_PREFIX_LENGTH-positions; |
---|
541 | sai_assert(skipLeft >= 0); |
---|
542 | paintBackgroundAndSAI(device, positions, pbRgX1+skipLeft*xStep_border, pbY, xStep_border, yStep, saiCols, dispSai); |
---|
543 | } |
---|
544 | paintProbeInfo(device, tok_prefix, pbRgX1, pbY, xStep_border, yStep, maxDescent, SAI_GC_PROBE); |
---|
545 | |
---|
546 | // -------------------- |
---|
547 | // probe region |
---|
548 | endPos = calculateEndPosition(gb_main, startPos, i, PROBE, probeLen, &endErr); |
---|
549 | sai_assert(endPos >= startPos); |
---|
550 | sai_assert(!endErr); |
---|
551 | saiCols = translateSAItoColors(aw_root, gb_main, startPos, endPos, i); |
---|
552 | |
---|
553 | paintBackgroundAndSAI(device, strlen(tok_infix), pbX, pbY, xStep_target, yStep, saiCols, dispSai); |
---|
554 | paintProbeInfo(device, tok_infix, pbX, pbY, xStep_target, yStep, maxDescent, SAI_GC_PROBE); |
---|
555 | |
---|
556 | // post-probe region - 9 bases |
---|
557 | size_t post_start_pos = endPos; |
---|
558 | |
---|
559 | endPos = calculateEndPosition(gb_main, post_start_pos, i, PROBE_SUFFIX, probeLen, &endErr); |
---|
560 | sai_assert(endPos >= int(post_start_pos)); |
---|
561 | sai_assert(!endErr); |
---|
562 | |
---|
563 | saiCols = translateSAItoColors(aw_root, gb_main, post_start_pos, endPos, i); |
---|
564 | if (saiCols) paintBackgroundAndSAI(device, strlen(tok_suffix), pbRgX2, pbY, xStep_border, yStep, saiCols, dispSai); |
---|
565 | paintProbeInfo(device, tok_suffix, pbRgX2, pbY, xStep_border, yStep, maxDescent, SAI_GC_PROBE); |
---|
566 | } |
---|
567 | } |
---|
568 | } |
---|
569 | else { |
---|
570 | err = GBS_global_string("probe-region '%s' has invalid format", probeRegion); |
---|
571 | } |
---|
572 | |
---|
573 | if (err) device->text(SAI_GC_PROBE, err, pbRgX2, pbY, 0, AW_SCREEN, 0); |
---|
574 | free(probeRegion_copy); |
---|
575 | } |
---|
576 | pbY += yLineStep; |
---|
577 | } |
---|
578 | } |
---|
579 | lineXpos = pbRgX2 + (9 * xStep_border); |
---|
580 | device->set_line_attributes(SAI_GC_FOREGROUND, 1, AW_SOLID); |
---|
581 | |
---|
582 | device->line(SAI_GC_FOREGROUND, 0, -20, lineXpos, -20); |
---|
583 | device->line(SAI_GC_FOREGROUND, 0, pbY, lineXpos, pbY); |
---|
584 | |
---|
585 | { |
---|
586 | double vert_x1 = pbX-spacer/2; |
---|
587 | double vert_x2 = pbRgX2-spacer/2; |
---|
588 | device->line(SAI_GC_FOREGROUND, vert_x1, -20, vert_x1, pbY); |
---|
589 | device->line(SAI_GC_FOREGROUND, vert_x2, -20, vert_x2, pbY); |
---|
590 | } |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | void transferProbeData(saiProbeData *spd) { |
---|
595 | // store pointer to currently used probe data |
---|
596 | g_pbdata = spd; |
---|
597 | |
---|
598 | } |
---|
599 | |
---|
600 | // ---------------------------------- Creating WINDOWS ------------------------------------------------ |
---|
601 | |
---|
602 | static void saiColorDefs_init_config(AWT_config_definition& cdef) { |
---|
603 | for (int i = 0; i < 10; i++) { |
---|
604 | const char *awarDef = getAwarName(i); |
---|
605 | cdef.add(awarDef, "", i); |
---|
606 | } |
---|
607 | } |
---|
608 | |
---|
609 | static char *saiColorDefs_store_config(AW_CL, AW_CL) { |
---|
610 | AWT_config_definition cdef; |
---|
611 | saiColorDefs_init_config(cdef); |
---|
612 | return cdef.read(); |
---|
613 | } |
---|
614 | |
---|
615 | static void saiColorDefs_restore_config(const char *stored_string, AW_CL, AW_CL) { |
---|
616 | AWT_config_definition cdef; |
---|
617 | saiColorDefs_init_config(cdef); |
---|
618 | cdef.write(stored_string); |
---|
619 | } |
---|
620 | |
---|
621 | static AW_window *create_colorTranslationTable_window(AW_root *aw_root) { // creates color translation table window |
---|
622 | // window to define color translations of selected SAI |
---|
623 | static AW_window_simple *aws = 0; |
---|
624 | if (!aws) { |
---|
625 | aws = new AW_window_simple; |
---|
626 | aws->init(aw_root, "SAI_CTT", "Color Translation Table"); |
---|
627 | aws->load_xfig("probeSaiColors.fig"); |
---|
628 | |
---|
629 | char at_name[] = "rangex"; |
---|
630 | char *dig = strchr(at_name, 0)-1; |
---|
631 | |
---|
632 | for (int i = 0; i<SAI_CLR_COUNT; ++i) { |
---|
633 | dig[0] = '0'+i; |
---|
634 | aws->at(at_name); |
---|
635 | aws->create_input_field(getAwarName(i), 15); |
---|
636 | } |
---|
637 | |
---|
638 | aws->at("config"); |
---|
639 | AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "saveSaiColorDefs", saiColorDefs_store_config, saiColorDefs_restore_config, 0, 0); |
---|
640 | |
---|
641 | aws->at("dispSai"); |
---|
642 | aws->create_toggle(AWAR_SPV_DISP_SAI); |
---|
643 | |
---|
644 | aws->at("close"); |
---|
645 | aws->callback(AW_POPDOWN); |
---|
646 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
647 | } |
---|
648 | return aws; |
---|
649 | } |
---|
650 | |
---|
651 | static AW_window *createDisplayField_window(AW_root *aw_root, GBDATA *gb_main) { |
---|
652 | // window to select SAI from the existing SAIs in the database |
---|
653 | static AW_window_simple *aws = 0; |
---|
654 | if (!aws) { |
---|
655 | aws = new AW_window_simple; |
---|
656 | aws->init(aw_root, "SELECT_DISPLAY_FIELD", "SELECT DISPLAY FIELD"); |
---|
657 | aws->load_xfig("displayField.fig"); |
---|
658 | |
---|
659 | aws->at("dbField"); |
---|
660 | aws->button_length(20); |
---|
661 | FieldSelectionParam *fsp = new FieldSelectionParam(gb_main, AWAR_SPV_DB_FIELD_NAME, true); |
---|
662 | aws->callback(makeWindowCallback(popup_select_species_field_window, fsp)); // fsp belongs to callback now |
---|
663 | aws->create_button("SELECT_DB_FIELD", AWAR_SPV_DB_FIELD_NAME); |
---|
664 | |
---|
665 | aws->at("aciSelect"); |
---|
666 | aws->button_length(12); |
---|
667 | aws->callback(makeWindowCallback(AWT_popup_select_srtaci_window, AWAR_SPV_ACI_COMMAND)); |
---|
668 | aws->create_button("SELECT_ACI", "Select ACI"); |
---|
669 | |
---|
670 | aws->at("aciCmd"); |
---|
671 | aws->button_length(20); |
---|
672 | aws->create_input_field(AWAR_SPV_ACI_COMMAND, 40); |
---|
673 | |
---|
674 | aws->at("width"); |
---|
675 | aws->button_length(5); |
---|
676 | aws->create_input_field(AWAR_SPV_DB_FIELD_WIDTH, 4); |
---|
677 | |
---|
678 | aws->at("close"); |
---|
679 | aws->button_length(10); |
---|
680 | aws->callback(AW_POPDOWN); |
---|
681 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
682 | |
---|
683 | aws->window_fit(); |
---|
684 | } |
---|
685 | return aws; |
---|
686 | } |
---|
687 | |
---|
688 | static AW_window *createSaiColorWindow(AW_root *aw_root, AW_gc_manager gc_manager) { |
---|
689 | return AW_create_gc_window_named(aw_root, gc_manager, "GC_PROPS_SAI", "Probe/SAI Colors and Fonts"); |
---|
690 | } |
---|
691 | |
---|
692 | AW_window *createSaiProbeMatchWindow(AW_root *awr, GBDATA *gb_main) { |
---|
693 | // Main Window - Canvas on which the actual painting is done |
---|
694 | GB_transaction ta(gb_main); |
---|
695 | |
---|
696 | createSaiProbeAwars(awr); // creating awars for colors ( 0 to 9) |
---|
697 | |
---|
698 | AW_window_menu *awm = new AW_window_menu; |
---|
699 | awm->init(awr, "MATCH_SAI", "PROBE AND SAI", 200, 300); |
---|
700 | |
---|
701 | SAI_graphic *saiProbeGraphic = new SAI_graphic(awr, gb_main); |
---|
702 | AWT_canvas *scr = new AWT_canvas(gb_main, awm, awm->get_window_id(), saiProbeGraphic, AWAR_TARGET_STRING); |
---|
703 | |
---|
704 | scr->recalc_size(); |
---|
705 | |
---|
706 | awm->insert_help_topic("How to Visualize SAI`s ?", "V", "saiProbeHelp.hlp", AWM_ALL, makeHelpCallback("saiProbeHelp.hlp")); |
---|
707 | |
---|
708 | awm->create_menu("File", "F", AWM_ALL); |
---|
709 | awm->insert_menu_topic("close", "Close", "C", "quit.hlp", AWM_ALL, AW_POPDOWN); |
---|
710 | |
---|
711 | awm->create_menu("Properties", "P", AWM_ALL); |
---|
712 | awm->insert_menu_topic("selectDispField", "Select Display Field", "F", "displayField.hlp", AWM_ALL, makeCreateWindowCallback(createDisplayField_window, gb_main)); |
---|
713 | awm->insert_menu_topic("selectSAI", "Select SAI", "S", NULL, AWM_ALL, makeWindowCallback(awt_popup_SAI_selection_list, AWAR_SPV_SAI_2_PROBE, gb_main)); |
---|
714 | awm->insert_menu_topic("clrTransTable", "Define Color Translations", "D", NULL, AWM_ALL, create_colorTranslationTable_window); |
---|
715 | awm->insert_menu_topic("SetColors", "Set Colors and Fonts", "t", "setColors.hlp", AWM_ALL, makeCreateWindowCallback(createSaiColorWindow, scr->gc_manager)); |
---|
716 | |
---|
717 | addCallBacks(awr, scr); |
---|
718 | |
---|
719 | return awm; |
---|
720 | } |
---|
721 | |
---|