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