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