1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <ctype.h> |
---|
5 | // #include <malloc.h> |
---|
6 | #include <arbdb.h> |
---|
7 | #include <arbdbt.h> |
---|
8 | |
---|
9 | |
---|
10 | #include <aw_root.hxx> |
---|
11 | #include <aw_device.hxx> |
---|
12 | #include <aw_window.hxx> |
---|
13 | |
---|
14 | #include "awt_seq_colors.hxx" |
---|
15 | #include "awt.hxx" |
---|
16 | |
---|
17 | |
---|
18 | static int default_NUC_set = 0; // number of default nucleotide set |
---|
19 | static int default_AMI_set = 3; // number of default amino acid set |
---|
20 | |
---|
21 | static const char *default_sets[AWT_SEQ_COLORS_MAX_SET] = { |
---|
22 | //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 * - |
---|
23 | "=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 |
---|
24 | "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 |
---|
25 | "=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 |
---|
26 | "=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 |
---|
27 | "=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", |
---|
28 | }; |
---|
29 | |
---|
30 | static bool seq_color_awars_created = false; |
---|
31 | |
---|
32 | // -------------------------------------------------------------------------------- |
---|
33 | |
---|
34 | static const char *default_characters(int elem) { |
---|
35 | static char result[3] = "xX"; |
---|
36 | |
---|
37 | if (elem<26) { // first 26 elements (0-25) are characters |
---|
38 | result[0] = 'a'+elem; |
---|
39 | result[1] = 'A'+elem; |
---|
40 | } |
---|
41 | else if (elem == 26) { // stop codon |
---|
42 | result[0] = '*'; |
---|
43 | result[1] = 0; |
---|
44 | } |
---|
45 | else if (elem == 27) { // gaps |
---|
46 | result[0] = '-'; |
---|
47 | result[1] = '.'; |
---|
48 | } |
---|
49 | |
---|
50 | return result; |
---|
51 | } |
---|
52 | static const char *default_color(int set, int elem) { |
---|
53 | // returns default color numbers for seq-color-set |
---|
54 | static char result[3] = "=0"; |
---|
55 | const char *pos = default_sets[set]+2*elem; |
---|
56 | |
---|
57 | result[0] = pos[0]; |
---|
58 | result[1] = pos[1]; |
---|
59 | |
---|
60 | return result; |
---|
61 | } |
---|
62 | |
---|
63 | static void awt_awar_changed_cb(GBDATA *, int *cl, GB_CB_TYPE){ |
---|
64 | AWT_seq_colors *sc = (AWT_seq_colors *)cl; |
---|
65 | sc->reload(); |
---|
66 | } |
---|
67 | |
---|
68 | static void create_seq_color_awars(AW_root *awr, AWT_seq_colors *asc) { |
---|
69 | awt_assert(!seq_color_awars_created); |
---|
70 | |
---|
71 | awr->awar_int(AWAR_SEQ_NAME_SELECTOR_NA, default_NUC_set, AW_ROOT_DEFAULT)->add_callback((AW_RCB)awt_awar_changed_cb, (AW_CL)asc, 0); |
---|
72 | awr->awar_int(AWAR_SEQ_NAME_SELECTOR_AA, default_AMI_set, AW_ROOT_DEFAULT)->add_callback((AW_RCB)awt_awar_changed_cb, (AW_CL)asc, 0); |
---|
73 | |
---|
74 | for (int elem = 0; elem<AWT_SEQ_COLORS_MAX_ELEMS; ++elem) { |
---|
75 | const char *awar_name = GBS_global_string(AWAR_SEQ_NAME_STRINGS_TEMPLATE, elem); |
---|
76 | awr->awar_string(awar_name, default_characters(elem)); |
---|
77 | |
---|
78 | for (int set = 0; set<AWT_SEQ_COLORS_MAX_SET; ++set) { |
---|
79 | awar_name = GBS_global_string(AWAR_SEQ_NAME_TEMPLATE, set, elem); |
---|
80 | awr->awar_string(awar_name, default_color(set, elem)); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | seq_color_awars_created = true; |
---|
85 | } |
---|
86 | |
---|
87 | AW_window *create_seq_colors_window(AW_root *awr, AWT_seq_colors *asc){ |
---|
88 | char buf[256]; |
---|
89 | static AW_window_simple *aws = 0; |
---|
90 | if (aws) return aws; |
---|
91 | |
---|
92 | if (!seq_color_awars_created) create_seq_color_awars(awr, asc); |
---|
93 | |
---|
94 | aws = new AW_window_simple; |
---|
95 | aws->init( awr, "SEQUENCE_COLOR_MAPPING", "SEQUENCE COLORS"); |
---|
96 | |
---|
97 | aws->at ( 10,10 ); |
---|
98 | aws->auto_space(0,3); |
---|
99 | |
---|
100 | aws->callback ( AW_POPDOWN );aws->create_button( "CLOSE", "CLOSE", "C" ); |
---|
101 | aws->callback ( AW_POPUP_HELP,(AW_CL)"sequence_colors.hlp" );aws->create_button( "HELP", "HELP" ); |
---|
102 | aws->at_newline(); |
---|
103 | |
---|
104 | // aws->label_length( 6 ); // @@@ has no effect |
---|
105 | // aws->button_length( 6 ); |
---|
106 | int set; |
---|
107 | |
---|
108 | for (int seqType=0; seqType<2; seqType++) { |
---|
109 | if (seqType==0) { |
---|
110 | aws->label("Select Colors For Nucleotides (NA) :"); |
---|
111 | aws->create_toggle_field(AWAR_SEQ_NAME_SELECTOR_NA,1); |
---|
112 | } |
---|
113 | else { |
---|
114 | aws->label("Select Colors For Amino Acids (AA) :"); |
---|
115 | aws->create_toggle_field(AWAR_SEQ_NAME_SELECTOR_AA,1); |
---|
116 | } |
---|
117 | |
---|
118 | for (set = 0; set < AWT_SEQ_COLORS_MAX_SET;set++){ |
---|
119 | sprintf(buf,"S_%i",set); |
---|
120 | aws->insert_toggle( buf," ",set ); |
---|
121 | } |
---|
122 | aws->update_toggle_field(); |
---|
123 | aws->at_newline(); |
---|
124 | } |
---|
125 | |
---|
126 | aws->label_length( 6 ); |
---|
127 | aws->button_length( 6 ); |
---|
128 | |
---|
129 | for (int big_columns = 0; big_columns <= 1; ++big_columns) { |
---|
130 | aws->create_button(0,"Char"); |
---|
131 | for (set = 0; set < AWT_SEQ_COLORS_MAX_SET;set++){ |
---|
132 | sprintf(buf,"S %i",set); |
---|
133 | aws->create_button(0,buf); |
---|
134 | } |
---|
135 | |
---|
136 | buf[0] = 0; aws->create_button(0,buf); // empty |
---|
137 | } |
---|
138 | aws->at_newline(); |
---|
139 | aws->auto_space(2,2); |
---|
140 | |
---|
141 | for (int elem = 0; elem < (AWT_SEQ_COLORS_MAX_ELEMS/2); elem++){ |
---|
142 | for (int big_columns = 0; big_columns <= 1; ++big_columns) { |
---|
143 | int my_elem = elem+big_columns*AWT_SEQ_COLORS_MAX_ELEMS/2; |
---|
144 | |
---|
145 | sprintf(buf,AWAR_SEQ_NAME_STRINGS_TEMPLATE,my_elem); |
---|
146 | aws->create_input_field(buf,4); |
---|
147 | for (set = 0; set < AWT_SEQ_COLORS_MAX_SET;set++){ |
---|
148 | sprintf(buf,AWAR_SEQ_NAME_TEMPLATE,set,my_elem); |
---|
149 | aws->create_input_field(buf,4); |
---|
150 | } |
---|
151 | if (big_columns == 0) { |
---|
152 | buf[0] = 0; aws->create_button(0,buf); // empty |
---|
153 | } |
---|
154 | } |
---|
155 | aws->at_newline(); |
---|
156 | } |
---|
157 | |
---|
158 | aws->window_fit(); |
---|
159 | return (AW_window *)aws; |
---|
160 | } |
---|
161 | |
---|
162 | void AWT_seq_colors::run_cb(){ |
---|
163 | if (callback && aww) callback(aww,cd1,cd2); |
---|
164 | } |
---|
165 | |
---|
166 | void AWT_seq_colors::reload(){ |
---|
167 | char buf[256]; |
---|
168 | GB_transaction dummy(gb_def); |
---|
169 | int i; |
---|
170 | |
---|
171 | for (i=0;i<256;i++) { |
---|
172 | char_2_gc[i] = char_2_gc_aa[i] = base_gc; |
---|
173 | char_2_char[i] = char_2_char_aa[i] = i; |
---|
174 | } |
---|
175 | |
---|
176 | const char *selector_awar[2] = { AWAR_SEQ_NAME_SELECTOR_NA, AWAR_SEQ_NAME_SELECTOR_AA }; |
---|
177 | |
---|
178 | for (int selector = 0; selector<2; selector++) { |
---|
179 | long def_set = selector == 0 ? default_NUC_set : default_AMI_set; |
---|
180 | long set = *GBT_readOrCreate_int(gb_def, selector_awar[selector], def_set); |
---|
181 | |
---|
182 | if (set < 0 || set >= AWT_SEQ_COLORS_MAX_SET) { |
---|
183 | set = def_set; |
---|
184 | } |
---|
185 | |
---|
186 | for (int elem = 0; elem < AWT_SEQ_COLORS_MAX_ELEMS; elem++){ |
---|
187 | sprintf(buf,AWAR_SEQ_NAME_STRINGS_TEMPLATE,elem); |
---|
188 | unsigned char *sc = (unsigned char *)GBT_readOrCreate_string(gb_def, buf, default_characters(elem)); |
---|
189 | if (!cbexists) { |
---|
190 | GBDATA *gb_ne = GB_search(gb_def,buf,GB_STRING); |
---|
191 | GB_ensure_callback(gb_ne,GB_CB_CHANGED,awt_awar_changed_cb,(int *)this); |
---|
192 | for (int s2=0; s2<AWT_SEQ_COLORS_MAX_SET; s2++){ |
---|
193 | sprintf(buf,AWAR_SEQ_NAME_TEMPLATE,s2,elem); |
---|
194 | GBT_readOrCreate_char_pntr(gb_def, buf, default_color(s2, elem)); // add default if missing |
---|
195 | gb_ne = GB_search(gb_def,buf,GB_STRING); |
---|
196 | GB_ensure_callback(gb_ne,GB_CB_CHANGED,awt_awar_changed_cb,(int *)this); |
---|
197 | } |
---|
198 | } |
---|
199 | sprintf(buf,AWAR_SEQ_NAME_TEMPLATE,(int)set,elem); |
---|
200 | char *val = GBT_read_string(gb_def,buf); |
---|
201 | if (strlen(val) != 2 || val[1] >'9' || val[1] < '0'){ |
---|
202 | aw_message(GB_export_errorf("Error in Color Lookup Table: '%s' is not of type X#",val)); |
---|
203 | delete val; |
---|
204 | delete sc; |
---|
205 | continue; |
---|
206 | } |
---|
207 | |
---|
208 | if (selector == 0) { // Nucleotide colors |
---|
209 | for (i=0;sc[i];i++) { |
---|
210 | char_2_gc[sc[i]] = val[1]-'0' + base_gc; |
---|
211 | if (val[0] != '=') char_2_char[sc[i]] = val[0]; |
---|
212 | } |
---|
213 | } |
---|
214 | else { |
---|
215 | for (i=0;sc[i];i++) { |
---|
216 | char_2_gc_aa[sc[i]] = val[1]-'0' + base_gc; |
---|
217 | if (val[0] != '=') char_2_char_aa[sc[i]] = val[0]; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | free(val); |
---|
222 | free(sc); |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | cbexists = 1; |
---|
227 | run_cb(); |
---|
228 | } |
---|
229 | |
---|
230 | AWT_seq_colors::AWT_seq_colors(GBDATA *gb_default, int _base_gc, |
---|
231 | AW_CB _cb,AW_CL _cd1,AW_CL _cd2){ |
---|
232 | aww = 0; |
---|
233 | cd1 = _cd1; |
---|
234 | cd2 = _cd2; |
---|
235 | callback = _cb; |
---|
236 | gb_def = gb_default; |
---|
237 | base_gc = _base_gc; |
---|
238 | GB_transaction dummy(gb_def); |
---|
239 | cbexists = 0; |
---|
240 | this->reload(); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | AWT_reference::AWT_reference(GBDATA *_gb_main){ |
---|
246 | reference = 0; |
---|
247 | ref_len = 0; |
---|
248 | gb_main = _gb_main; |
---|
249 | init_species_name = 0; |
---|
250 | } |
---|
251 | |
---|
252 | void AWT_reference::init() { |
---|
253 | delete reference; |
---|
254 | reference = 0; |
---|
255 | ref_len = 0; |
---|
256 | delete init_species_name; |
---|
257 | init_species_name = 0; |
---|
258 | } |
---|
259 | |
---|
260 | void AWT_reference::expand_to_length(int len){ |
---|
261 | if (len > ref_len){ |
---|
262 | char *ref2 = (char *)GB_calloc(sizeof(char),len+1); |
---|
263 | |
---|
264 | if (reference) { |
---|
265 | strcpy(ref2,reference); |
---|
266 | free(reference); |
---|
267 | } |
---|
268 | reference = ref2; |
---|
269 | ref_len = len; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | void AWT_reference::init(const char *species_name, const char *alignment_name) { |
---|
274 | |
---|
275 | awt_assert(species_name); |
---|
276 | awt_assert(alignment_name); |
---|
277 | |
---|
278 | GB_transaction dummy(gb_main); |
---|
279 | GBDATA *gb_species = GBT_find_species(gb_main,species_name); |
---|
280 | |
---|
281 | init(); |
---|
282 | if (gb_species){ |
---|
283 | GBDATA *gb_data = GBT_read_sequence(gb_species,alignment_name); |
---|
284 | if (gb_data){ |
---|
285 | reference = GB_read_as_string(gb_data); |
---|
286 | if (reference) { |
---|
287 | ref_len = strlen(reference); |
---|
288 | init_species_name = strdup(species_name); |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | void AWT_reference::init(const char *name, const char *sequence_data, int len) { |
---|
295 | |
---|
296 | awt_assert(name); |
---|
297 | awt_assert(sequence_data); |
---|
298 | awt_assert(len>0); |
---|
299 | |
---|
300 | init(); |
---|
301 | |
---|
302 | reference = (char*)GB_calloc(sizeof(char), len+1); |
---|
303 | memcpy(reference, sequence_data, len); |
---|
304 | reference[len] = 0; |
---|
305 | ref_len = len; |
---|
306 | init_species_name = strdup(name); |
---|
307 | } |
---|
308 | |
---|
309 | AWT_reference::~AWT_reference(){ |
---|
310 | delete reference; |
---|
311 | } |
---|