source: tags/ms_r16q3/PROBE_DESIGN/SaiProbeVisualization.cxx

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