source: tags/svn.1.5.4/PROBE_DESIGN/SaiProbeVisualization.cxx

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