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 color_awar_changed_cb(AW_root *, AWT_seq_colors *sc) { |
---|
76 | sc->reload(); |
---|
77 | } |
---|
78 | |
---|
79 | static void create_seq_color_awars(AW_root *awr, AWT_seq_colors *asc) { |
---|
80 | awt_assert(!seq_color_awars_created); |
---|
81 | |
---|
82 | RootCallback update_cb = makeRootCallback(color_awar_changed_cb, asc); |
---|
83 | awr->awar_int(AWAR_SEQ_NAME_SELECTOR_NA, default_NUC_set, AW_ROOT_DEFAULT)->add_callback(update_cb); |
---|
84 | awr->awar_int(AWAR_SEQ_NAME_SELECTOR_AA, default_AMI_set, AW_ROOT_DEFAULT)->add_callback(update_cb); |
---|
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(update_cb); |
---|
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)); |
---|
93 | |
---|
94 | if (strcmp(awar_col->read_char_pntr(), "=0") == 0) { // translate old->new default |
---|
95 | awar_col->write_string(""); |
---|
96 | } |
---|
97 | |
---|
98 | // add callback AFTER writing to awar above to avoid recursion |
---|
99 | // (the CB calls this function again, and seq_color_awars_created is set |
---|
100 | // to true at the very end... |
---|
101 | awar_col->add_callback(update_cb); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | seq_color_awars_created = true; |
---|
106 | } |
---|
107 | |
---|
108 | AW_window *create_seq_colors_window(AW_root *awr, AWT_seq_colors *asc) { |
---|
109 | char buf[256]; |
---|
110 | static AW_window_simple *aws = 0; |
---|
111 | if (aws) return aws; |
---|
112 | |
---|
113 | if (!seq_color_awars_created) create_seq_color_awars(awr, asc); |
---|
114 | |
---|
115 | aws = new AW_window_simple; |
---|
116 | aws->init(awr, "SEQUENCE_MAPPING", "Sequence color mapping"); |
---|
117 | |
---|
118 | aws->at(10, 10); |
---|
119 | aws->auto_space(0, 3); |
---|
120 | |
---|
121 | aws->callback(AW_POPDOWN); |
---|
122 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
123 | |
---|
124 | aws->callback(makeHelpCallback("sequence_colors.hlp")); |
---|
125 | aws->create_button("HELP", "HELP"); |
---|
126 | |
---|
127 | aws->at_newline(); |
---|
128 | |
---|
129 | for (int seqType=0; seqType<2; seqType++) { |
---|
130 | if (seqType==0) { |
---|
131 | aws->label("Select color-set for Nucleotides (NA):"); |
---|
132 | aws->create_toggle_field(AWAR_SEQ_NAME_SELECTOR_NA, 1); |
---|
133 | } |
---|
134 | else { |
---|
135 | aws->label("Select color-set for Amino Acids (AA):"); |
---|
136 | aws->create_toggle_field(AWAR_SEQ_NAME_SELECTOR_AA, 1); |
---|
137 | } |
---|
138 | |
---|
139 | for (int cset = 0; cset < SEQ_COLOR_SETS; cset++) { |
---|
140 | sprintf(buf, "%i", cset+1); |
---|
141 | aws->insert_toggle(buf, " ", cset); |
---|
142 | } |
---|
143 | aws->update_toggle_field(); |
---|
144 | aws->at_newline(); |
---|
145 | } |
---|
146 | |
---|
147 | const int BIG_COLUMNS = 2; |
---|
148 | const int CHAR_COL_WIDTH = 4; |
---|
149 | const int SET_COL_WIDTH = 2; |
---|
150 | |
---|
151 | int col_x_off[BIG_COLUMNS][SEQ_COLOR_SETS+1]; |
---|
152 | |
---|
153 | aws->auto_space(3, 2); |
---|
154 | |
---|
155 | for (int bcol = 0; bcol<BIG_COLUMNS; ++bcol) { |
---|
156 | col_x_off[bcol][0] = aws->get_at_xposition(); |
---|
157 | aws->button_length(CHAR_COL_WIDTH); |
---|
158 | aws->create_button(0, "Chars"); |
---|
159 | |
---|
160 | aws->button_length(SET_COL_WIDTH); |
---|
161 | for (int cset = 0; cset < SEQ_COLOR_SETS; cset++) { |
---|
162 | sprintf(buf, " %i", cset+1); |
---|
163 | col_x_off[bcol][cset+1] = aws->get_at_xposition(); |
---|
164 | aws->create_button(0, buf); |
---|
165 | } |
---|
166 | |
---|
167 | if (!bcol) { |
---|
168 | int set_col_pixel_width = col_x_off[0][1]-col_x_off[0][0]; |
---|
169 | aws->at_x(aws->get_at_xposition()+set_col_pixel_width); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | aws->at_newline(); |
---|
174 | |
---|
175 | const int ROWS = SEQ_COLOR_SET_ELEMS/2; |
---|
176 | for (int r = 0; r<ROWS; r++) { |
---|
177 | for (int bcol = 0; bcol<BIG_COLUMNS; ++bcol) { |
---|
178 | int elem = bcol*ROWS+r; |
---|
179 | |
---|
180 | sprintf(buf, AWAR_SEQ_NAME_STRINGS_TEMPLATE, elem); |
---|
181 | aws->at_x(col_x_off[bcol][0]); |
---|
182 | aws->create_input_field(buf, CHAR_COL_WIDTH); |
---|
183 | |
---|
184 | for (int cset = 0; cset < SEQ_COLOR_SETS; cset++) { |
---|
185 | sprintf(buf, AWAR_SEQ_NAME_TEMPLATE, cset, elem); |
---|
186 | aws->at_x(col_x_off[bcol][cset+1]); |
---|
187 | aws->create_input_field(buf, SET_COL_WIDTH); |
---|
188 | } |
---|
189 | } |
---|
190 | aws->at_newline(); |
---|
191 | } |
---|
192 | |
---|
193 | aws->window_fit(); |
---|
194 | |
---|
195 | return aws; |
---|
196 | } |
---|
197 | |
---|
198 | void AWT_seq_colors::reload() { |
---|
199 | for (int i=0; i<256; i++) { |
---|
200 | char_2_gc[i] = char_2_gc_aa[i] = base_gc; |
---|
201 | char_2_char[i] = char_2_char_aa[i] = i; |
---|
202 | } |
---|
203 | |
---|
204 | AW_root *aw_root = AW_root::SINGLETON; |
---|
205 | |
---|
206 | if (!seq_color_awars_created) create_seq_color_awars(aw_root, this); |
---|
207 | |
---|
208 | const char *selector_awar[2] = { AWAR_SEQ_NAME_SELECTOR_NA, AWAR_SEQ_NAME_SELECTOR_AA }; |
---|
209 | |
---|
210 | for (int selector = 0; selector<2; selector++) { |
---|
211 | long def_set = selector == 0 ? default_NUC_set : default_AMI_set; |
---|
212 | long cset = aw_root->awar(selector_awar[selector])->read_int(); |
---|
213 | |
---|
214 | if (cset < 0 || cset >= SEQ_COLOR_SETS) { |
---|
215 | cset = def_set; |
---|
216 | } |
---|
217 | |
---|
218 | for (int elem = 0; elem < SEQ_COLOR_SET_ELEMS; elem++) { |
---|
219 | char awar_name[256]; |
---|
220 | |
---|
221 | sprintf(awar_name, AWAR_SEQ_NAME_STRINGS_TEMPLATE, elem); |
---|
222 | unsigned char *sc = (unsigned char *)aw_root->awar(awar_name)->read_string(); |
---|
223 | |
---|
224 | sprintf(awar_name, AWAR_SEQ_NAME_TEMPLATE, (int)cset, elem); |
---|
225 | char *val = aw_root->awar(awar_name)->read_string(); |
---|
226 | if (!val[0]) freedup(val, "=0"); // interpret '' as ' = 0' |
---|
227 | |
---|
228 | if (strlen(val) != 2 || val[1] >'9' || val[1] < '0') { |
---|
229 | aw_message(GB_export_errorf("Error in Color Lookup Table: '%s' is not of type X#", val)); |
---|
230 | } |
---|
231 | else { |
---|
232 | if (selector == 0) { // Nucleotide colors |
---|
233 | for (int i=0; sc[i]; i++) { |
---|
234 | char_2_gc[sc[i]] = val[1]-'0' + base_gc; |
---|
235 | if (val[0] != '=') char_2_char[sc[i]] = val[0]; |
---|
236 | } |
---|
237 | } |
---|
238 | else { |
---|
239 | for (int i=0; sc[i]; i++) { |
---|
240 | char_2_gc_aa[sc[i]] = val[1]-'0' + base_gc; |
---|
241 | if (val[0] != '=') char_2_char_aa[sc[i]] = val[0]; |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | free(val); |
---|
246 | free(sc); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | run_cb(); |
---|
251 | } |
---|
252 | |
---|
253 | AWT_seq_colors::AWT_seq_colors(int baseGC, void (*changed_cb)()) { |
---|
254 | cb = changed_cb; |
---|
255 | base_gc = baseGC; |
---|
256 | |
---|
257 | this->reload(); |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | AWT_reference::AWT_reference(GBDATA *_gb_main) { |
---|
263 | reference = 0; |
---|
264 | ref_len = 0; |
---|
265 | gb_main = _gb_main; |
---|
266 | init_species_name = 0; |
---|
267 | } |
---|
268 | |
---|
269 | void AWT_reference::init() { |
---|
270 | free(reference); |
---|
271 | reference = 0; |
---|
272 | ref_len = 0; |
---|
273 | delete init_species_name; |
---|
274 | init_species_name = 0; |
---|
275 | } |
---|
276 | |
---|
277 | void AWT_reference::expand_to_length(int len) { |
---|
278 | if (len > ref_len) { |
---|
279 | char *ref2 = (char *)GB_calloc(sizeof(char), len+1); |
---|
280 | |
---|
281 | if (reference) { |
---|
282 | strcpy(ref2, reference); |
---|
283 | free(reference); |
---|
284 | } |
---|
285 | reference = ref2; |
---|
286 | ref_len = len; |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | void AWT_reference::init(const char *species_name, const char *alignment_name) { |
---|
291 | |
---|
292 | awt_assert(species_name); |
---|
293 | awt_assert(alignment_name); |
---|
294 | |
---|
295 | GB_transaction ta(gb_main); |
---|
296 | GBDATA *gb_species = GBT_find_species(gb_main, species_name); |
---|
297 | |
---|
298 | init(); |
---|
299 | if (gb_species) { |
---|
300 | GBDATA *gb_data = GBT_find_sequence(gb_species, alignment_name); |
---|
301 | if (gb_data) { |
---|
302 | reference = GB_read_as_string(gb_data); |
---|
303 | if (reference) { |
---|
304 | ref_len = strlen(reference); |
---|
305 | init_species_name = strdup(species_name); |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | void AWT_reference::init(const char *name, const char *sequence_data, int len) { |
---|
312 | |
---|
313 | awt_assert(name); |
---|
314 | awt_assert(sequence_data); |
---|
315 | awt_assert(len>0); |
---|
316 | |
---|
317 | init(); |
---|
318 | |
---|
319 | reference = (char*)GB_calloc(sizeof(char), len+1); |
---|
320 | memcpy(reference, sequence_data, len); |
---|
321 | reference[len] = 0; |
---|
322 | ref_len = len; |
---|
323 | init_species_name = strdup(name); |
---|
324 | } |
---|
325 | |
---|
326 | AWT_reference::~AWT_reference() { |
---|
327 | delete reference; |
---|
328 | } |
---|