| 1 | // ================================================================ // |
|---|
| 2 | // // |
|---|
| 3 | // File : AWT_seq_colors.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ================================================================ // |
|---|
| 10 | |
|---|
| 11 | #include "awt_seq_colors.hxx" |
|---|
| 12 | #include "awt.hxx" |
|---|
| 13 | |
|---|
| 14 | #include <aw_root.hxx> |
|---|
| 15 | #include <aw_awar.hxx> |
|---|
| 16 | #include <aw_msg.hxx> |
|---|
| 17 | #include <arbdbt.h> |
|---|
| 18 | |
|---|
| 19 | #include <cctype> |
|---|
| 20 | |
|---|
| 21 | static int default_NUC_set = 0; // number of default nucleotide set |
|---|
| 22 | static int default_AMI_set = 3; // number of default amino acid set |
|---|
| 23 | |
|---|
| 24 | #define SEQ_COLOR_SETS 8 |
|---|
| 25 | #define SEQ_COLOR_SET_ELEMS 28 // has to be a even number! |
|---|
| 26 | |
|---|
| 27 | static const char *default_sets[SEQ_COLOR_SETS] = { |
|---|
| 28 | //A B C D E F G H I J K L M N O P Q R S T U V W X Y Z * - |
|---|
| 29 | "=2=0=3=0=0=0=4=0=0=0=0=0=0=6=0=0=0=0=0=5=5=0=0=0=0=0=0=6", // A, C, G, TU and N in 5 colors |
|---|
| 30 | "R2=0Y3=0=0=0R2=0=0=0=0=0=0=0=0=0=0=2=0Y3Y3=0=0=0=3=0=0=6", // AG and CTU in 2 colors |
|---|
| 31 | "=0=5=0=5=7=7=0=5=7=7=3=7=3=9=7=7=7=3=3=0=0=5=3=7=3=7=0=6", // ambiguity |
|---|
| 32 | "=7=0=7=8=2=9=8=9=3=0=2=3=7=8=0=8=2=2=2=2=0=3=9=6=9=0=0=6", // Protein colors |
|---|
| 33 | |
|---|
| 34 | "=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=6", |
|---|
| 35 | "o9=0|2=0=0=0o5=0=0=0=0=0=0=0=0=0=0=0=0|8|8=0=0=0=0=0=0=6", // ambiguity (symbols) |
|---|
| 36 | "=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=6", |
|---|
| 37 | "=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=0=6", |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | static bool seq_color_awars_created = false; |
|---|
| 41 | |
|---|
| 42 | // -------------------------------------------------------------------------------- |
|---|
| 43 | |
|---|
| 44 | static const char *default_characters(int elem) { |
|---|
| 45 | static char result[3] = "xX"; |
|---|
| 46 | |
|---|
| 47 | if (elem<26) { // first 26 elements (0-25) are characters |
|---|
| 48 | result[0] = 'a'+elem; |
|---|
| 49 | result[1] = 'A'+elem; |
|---|
| 50 | } |
|---|
| 51 | else if (elem == 26) { // stop codon |
|---|
| 52 | result[0] = '*'; |
|---|
| 53 | result[1] = 0; |
|---|
| 54 | } |
|---|
| 55 | else if (elem == 27) { // gaps |
|---|
| 56 | result[0] = '-'; |
|---|
| 57 | result[1] = '.'; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | return result; |
|---|
| 61 | } |
|---|
| 62 | static const char *default_color(int cset, int elem) { |
|---|
| 63 | // returns default color numbers for seq-color-set |
|---|
| 64 | static char result[3] = "=0"; |
|---|
| 65 | const char *pos = default_sets[cset]+2*elem; |
|---|
| 66 | |
|---|
| 67 | result[0] = pos[0]; |
|---|
| 68 | result[1] = pos[1]; |
|---|
| 69 | |
|---|
| 70 | if (result[0] == '=' && result[1] == '0') result[0] = 0; |
|---|
| 71 | |
|---|
| 72 | return result; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | static void awt_awar_changed_cb(AW_root *, AW_CL cl_sc) { |
|---|
| 76 | AWT_seq_colors *sc = (AWT_seq_colors *)cl_sc; |
|---|
| 77 | sc->reload(); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | static void create_seq_color_awars(AW_root *awr, AWT_seq_colors *asc) { |
|---|
| 81 | awt_assert(!seq_color_awars_created); |
|---|
| 82 | |
|---|
| 83 | awr->awar_int(AWAR_SEQ_NAME_SELECTOR_NA, default_NUC_set, AW_ROOT_DEFAULT)->add_callback(awt_awar_changed_cb, (AW_CL)asc); |
|---|
| 84 | awr->awar_int(AWAR_SEQ_NAME_SELECTOR_AA, default_AMI_set, AW_ROOT_DEFAULT)->add_callback(awt_awar_changed_cb, (AW_CL)asc); |
|---|
| 85 | |
|---|
| 86 | for (int elem = 0; elem<SEQ_COLOR_SET_ELEMS; ++elem) { |
|---|
| 87 | const char *awar_name = GBS_global_string(AWAR_SEQ_NAME_STRINGS_TEMPLATE, elem); |
|---|
| 88 | awr->awar_string(awar_name, default_characters(elem))->add_callback(awt_awar_changed_cb, (AW_CL)asc); |
|---|
| 89 | |
|---|
| 90 | for (int cset = 0; cset<SEQ_COLOR_SETS; ++cset) { |
|---|
| 91 | awar_name = GBS_global_string(AWAR_SEQ_NAME_TEMPLATE, cset, elem); |
|---|
| 92 | AW_awar *awar_col = awr->awar_string(awar_name, default_color(cset, elem))->add_callback(awt_awar_changed_cb, (AW_CL)asc); |
|---|
| 93 | |
|---|
| 94 | if (strcmp(awar_col->read_char_pntr(), "=0") == 0) { // translate old->new default |
|---|
| 95 | awar_col->write_string(""); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | seq_color_awars_created = true; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | AW_window *create_seq_colors_window(AW_root *awr, AWT_seq_colors *asc) { |
|---|
| 104 | char buf[256]; |
|---|
| 105 | static AW_window_simple *aws = 0; |
|---|
| 106 | if (aws) return aws; |
|---|
| 107 | |
|---|
| 108 | if (!seq_color_awars_created) create_seq_color_awars(awr, asc); |
|---|
| 109 | |
|---|
| 110 | aws = new AW_window_simple; |
|---|
| 111 | aws->init(awr, "SEQUENCE_MAPPING", "Sequence color mapping"); |
|---|
| 112 | |
|---|
| 113 | aws->at(10, 10); |
|---|
| 114 | aws->auto_space(0, 3); |
|---|
| 115 | |
|---|
| 116 | aws->callback(AW_POPDOWN); |
|---|
| 117 | aws->create_button("CLOSE", "CLOSE", "C"); |
|---|
| 118 | |
|---|
| 119 | aws->callback(AW_POPUP_HELP, (AW_CL)"sequence_colors.hlp"); |
|---|
| 120 | aws->create_button("HELP", "HELP"); |
|---|
| 121 | |
|---|
| 122 | aws->at_newline(); |
|---|
| 123 | |
|---|
| 124 | for (int seqType=0; seqType<2; seqType++) { |
|---|
| 125 | if (seqType==0) { |
|---|
| 126 | aws->label("Select color-set for Nucleotides (NA):"); |
|---|
| 127 | aws->create_toggle_field(AWAR_SEQ_NAME_SELECTOR_NA, 1); |
|---|
| 128 | } |
|---|
| 129 | else { |
|---|
| 130 | aws->label("Select color-set for Amino Acids (AA):"); |
|---|
| 131 | aws->create_toggle_field(AWAR_SEQ_NAME_SELECTOR_AA, 1); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | for (int cset = 0; cset < SEQ_COLOR_SETS; cset++) { |
|---|
| 135 | sprintf(buf, "%i", cset+1); |
|---|
| 136 | aws->insert_toggle(buf, " ", cset); |
|---|
| 137 | } |
|---|
| 138 | aws->update_toggle_field(); |
|---|
| 139 | aws->at_newline(); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | const int BIG_COLUMNS = 2; |
|---|
| 143 | const int CHAR_COL_WIDTH = 4; |
|---|
| 144 | const int SET_COL_WIDTH = 2; |
|---|
| 145 | |
|---|
| 146 | int col_x_off[BIG_COLUMNS][SEQ_COLOR_SETS+1]; |
|---|
| 147 | |
|---|
| 148 | aws->auto_space(3, 2); |
|---|
| 149 | |
|---|
| 150 | for (int bcol = 0; bcol<BIG_COLUMNS; ++bcol) { |
|---|
| 151 | col_x_off[bcol][0] = aws->get_at_xposition(); |
|---|
| 152 | aws->button_length(CHAR_COL_WIDTH); |
|---|
| 153 | aws->create_button(0, "Chars"); |
|---|
| 154 | |
|---|
| 155 | aws->button_length(SET_COL_WIDTH); |
|---|
| 156 | for (int cset = 0; cset < SEQ_COLOR_SETS; cset++) { |
|---|
| 157 | sprintf(buf, " %i", cset+1); |
|---|
| 158 | col_x_off[bcol][cset+1] = aws->get_at_xposition(); |
|---|
| 159 | aws->create_button(0, buf); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | if (!bcol) { |
|---|
| 163 | int set_col_pixel_width = col_x_off[0][1]-col_x_off[0][0]; |
|---|
| 164 | aws->at_x(aws->get_at_xposition()+set_col_pixel_width); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | aws->at_newline(); |
|---|
| 169 | |
|---|
| 170 | const int ROWS = SEQ_COLOR_SET_ELEMS/2; |
|---|
| 171 | for (int r = 0; r<ROWS; r++) { |
|---|
| 172 | for (int bcol = 0; bcol<BIG_COLUMNS; ++bcol) { |
|---|
| 173 | int elem = bcol*ROWS+r; |
|---|
| 174 | |
|---|
| 175 | sprintf(buf, AWAR_SEQ_NAME_STRINGS_TEMPLATE, elem); |
|---|
| 176 | aws->at_x(col_x_off[bcol][0]); |
|---|
| 177 | aws->create_input_field(buf, CHAR_COL_WIDTH); |
|---|
| 178 | |
|---|
| 179 | for (int cset = 0; cset < SEQ_COLOR_SETS; cset++) { |
|---|
| 180 | sprintf(buf, AWAR_SEQ_NAME_TEMPLATE, cset, elem); |
|---|
| 181 | aws->at_x(col_x_off[bcol][cset+1]); |
|---|
| 182 | aws->create_input_field(buf, SET_COL_WIDTH); |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | aws->at_newline(); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | aws->window_fit(); |
|---|
| 189 | |
|---|
| 190 | return aws; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | void AWT_seq_colors::reload() { |
|---|
| 194 | for (int i=0; i<256; i++) { |
|---|
| 195 | char_2_gc[i] = char_2_gc_aa[i] = base_gc; |
|---|
| 196 | char_2_char[i] = char_2_char_aa[i] = i; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | AW_root *aw_root = AW_root::SINGLETON; |
|---|
| 200 | |
|---|
| 201 | if (!seq_color_awars_created) create_seq_color_awars(aw_root, this); |
|---|
| 202 | |
|---|
| 203 | const char *selector_awar[2] = { AWAR_SEQ_NAME_SELECTOR_NA, AWAR_SEQ_NAME_SELECTOR_AA }; |
|---|
| 204 | |
|---|
| 205 | for (int selector = 0; selector<2; selector++) { |
|---|
| 206 | long def_set = selector == 0 ? default_NUC_set : default_AMI_set; |
|---|
| 207 | long cset = aw_root->awar(selector_awar[selector])->read_int(); |
|---|
| 208 | |
|---|
| 209 | if (cset < 0 || cset >= SEQ_COLOR_SETS) { |
|---|
| 210 | cset = def_set; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | for (int elem = 0; elem < SEQ_COLOR_SET_ELEMS; elem++) { |
|---|
| 214 | char awar_name[256]; |
|---|
| 215 | |
|---|
| 216 | sprintf(awar_name, AWAR_SEQ_NAME_STRINGS_TEMPLATE, elem); |
|---|
| 217 | unsigned char *sc = (unsigned char *)aw_root->awar(awar_name)->read_string(); |
|---|
| 218 | |
|---|
| 219 | sprintf(awar_name, AWAR_SEQ_NAME_TEMPLATE, (int)cset, elem); |
|---|
| 220 | char *val = aw_root->awar(awar_name)->read_string(); |
|---|
| 221 | if (!val[0]) freedup(val, "=0"); // interpret '' as ' = 0' |
|---|
| 222 | |
|---|
| 223 | if (strlen(val) != 2 || val[1] >'9' || val[1] < '0') { |
|---|
| 224 | aw_message(GB_export_errorf("Error in Color Lookup Table: '%s' is not of type X#", val)); |
|---|
| 225 | } |
|---|
| 226 | else { |
|---|
| 227 | if (selector == 0) { // Nucleotide colors |
|---|
| 228 | for (int i=0; sc[i]; i++) { |
|---|
| 229 | char_2_gc[sc[i]] = val[1]-'0' + base_gc; |
|---|
| 230 | if (val[0] != '=') char_2_char[sc[i]] = val[0]; |
|---|
| 231 | } |
|---|
| 232 | } |
|---|
| 233 | else { |
|---|
| 234 | for (int i=0; sc[i]; i++) { |
|---|
| 235 | char_2_gc_aa[sc[i]] = val[1]-'0' + base_gc; |
|---|
| 236 | if (val[0] != '=') char_2_char_aa[sc[i]] = val[0]; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | free(val); |
|---|
| 241 | free(sc); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | run_cb(); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | AWT_seq_colors::AWT_seq_colors(int baseGC, void (*changed_cb)()) { |
|---|
| 249 | cb = changed_cb; |
|---|
| 250 | base_gc = baseGC; |
|---|
| 251 | |
|---|
| 252 | this->reload(); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | AWT_reference::AWT_reference(GBDATA *_gb_main) { |
|---|
| 258 | reference = 0; |
|---|
| 259 | ref_len = 0; |
|---|
| 260 | gb_main = _gb_main; |
|---|
| 261 | init_species_name = 0; |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | void AWT_reference::init() { |
|---|
| 265 | free(reference); |
|---|
| 266 | reference = 0; |
|---|
| 267 | ref_len = 0; |
|---|
| 268 | delete init_species_name; |
|---|
| 269 | init_species_name = 0; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | void AWT_reference::expand_to_length(int len) { |
|---|
| 273 | if (len > ref_len) { |
|---|
| 274 | char *ref2 = (char *)GB_calloc(sizeof(char), len+1); |
|---|
| 275 | |
|---|
| 276 | if (reference) { |
|---|
| 277 | strcpy(ref2, reference); |
|---|
| 278 | free(reference); |
|---|
| 279 | } |
|---|
| 280 | reference = ref2; |
|---|
| 281 | ref_len = len; |
|---|
| 282 | } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | void AWT_reference::init(const char *species_name, const char *alignment_name) { |
|---|
| 286 | |
|---|
| 287 | awt_assert(species_name); |
|---|
| 288 | awt_assert(alignment_name); |
|---|
| 289 | |
|---|
| 290 | GB_transaction dummy(gb_main); |
|---|
| 291 | GBDATA *gb_species = GBT_find_species(gb_main, species_name); |
|---|
| 292 | |
|---|
| 293 | init(); |
|---|
| 294 | if (gb_species) { |
|---|
| 295 | GBDATA *gb_data = GBT_read_sequence(gb_species, alignment_name); |
|---|
| 296 | if (gb_data) { |
|---|
| 297 | reference = GB_read_as_string(gb_data); |
|---|
| 298 | if (reference) { |
|---|
| 299 | ref_len = strlen(reference); |
|---|
| 300 | init_species_name = strdup(species_name); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | void AWT_reference::init(const char *name, const char *sequence_data, int len) { |
|---|
| 307 | |
|---|
| 308 | awt_assert(name); |
|---|
| 309 | awt_assert(sequence_data); |
|---|
| 310 | awt_assert(len>0); |
|---|
| 311 | |
|---|
| 312 | init(); |
|---|
| 313 | |
|---|
| 314 | reference = (char*)GB_calloc(sizeof(char), len+1); |
|---|
| 315 | memcpy(reference, sequence_data, len); |
|---|
| 316 | reference[len] = 0; |
|---|
| 317 | ref_len = len; |
|---|
| 318 | init_species_name = strdup(name); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | AWT_reference::~AWT_reference() { |
|---|
| 322 | delete reference; |
|---|
| 323 | } |
|---|