1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <arbdb.h> |
---|
5 | #include <arbdbt.h> |
---|
6 | #include <aw_root.hxx> |
---|
7 | #include <aw_device.hxx> |
---|
8 | #include <aw_window.hxx> |
---|
9 | #include <aw_awars.hxx> |
---|
10 | |
---|
11 | #include "awt.hxx" |
---|
12 | #include "awtfilter.hxx" |
---|
13 | #include <awt_tree.hxx> |
---|
14 | #include <awt_sel_boxes.hxx> |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | /** recalc filter */ |
---|
19 | void awt_create_select_filter_window_aw_cb(void *dummy, struct adfiltercbstruct *cbs) |
---|
20 | { // update the variables |
---|
21 | AW_root *aw_root = cbs->awr; |
---|
22 | AWUSE(dummy); |
---|
23 | char buffer[256]; |
---|
24 | GB_push_transaction(cbs->gb_main); |
---|
25 | char *target = aw_root->awar(cbs->def_subname)->read_string(); |
---|
26 | char *to_free_target = target; |
---|
27 | char *use = aw_root->awar(cbs->def_alignment)->read_string(); |
---|
28 | char *name = strchr(target,1); |
---|
29 | GBDATA *gbd = 0; |
---|
30 | if (name){ |
---|
31 | *(name++) = 0; |
---|
32 | target++; |
---|
33 | GBDATA *gb_species; |
---|
34 | if (target[-1] == '@'){ |
---|
35 | gb_species = GBT_find_species(cbs->gb_main,name); |
---|
36 | }else{ |
---|
37 | gb_species = GBT_find_SAI(cbs->gb_main,name); |
---|
38 | } |
---|
39 | if (gb_species){ |
---|
40 | GBDATA *gb_ali = GB_search(gb_species,use,GB_FIND); |
---|
41 | if (gb_ali) { |
---|
42 | gbd = GB_search(gb_ali,target,GB_FIND); |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | if (!gbd){ // nothing selected |
---|
47 | aw_root->awar(cbs->def_name)->write_string("none"); |
---|
48 | aw_root->awar(cbs->def_source)->write_string("No Filter Sequence ->All Columns Selected"); |
---|
49 | aw_root->awar(cbs->def_filter)->write_string(""); |
---|
50 | aw_root->awar(cbs->def_len) ->write_int(-1); // export filter |
---|
51 | }else{ |
---|
52 | GBDATA *gb_name = GB_get_father(gbd); // ali_xxxx |
---|
53 | gb_name = GB_brother(gb_name,"name"); |
---|
54 | char *name2 = GB_read_string(gb_name); |
---|
55 | aw_root->awar(cbs->def_name)->write_string(name2); |
---|
56 | free(name2); |
---|
57 | char *_2filter = aw_root->awar(cbs->def_2filter)->read_string(); |
---|
58 | long _2filter_len = strlen(_2filter); |
---|
59 | |
---|
60 | char *s,*str; |
---|
61 | long len = GBT_get_alignment_len(cbs->gb_main,use); |
---|
62 | GBS_strstruct *strstruct = GBS_stropen(5000); |
---|
63 | long i; for (i=0;i<len;i++) { // build position line |
---|
64 | if (i%10 == 0) { |
---|
65 | GBS_chrcat(strstruct,'#'); |
---|
66 | }else if (i%5==0) { |
---|
67 | GBS_chrcat(strstruct,'|'); |
---|
68 | }else{ |
---|
69 | GBS_chrcat(strstruct,'.'); |
---|
70 | } |
---|
71 | } |
---|
72 | GBS_chrcat(strstruct,'\n'); |
---|
73 | char *data = GBS_mempntr(strstruct); |
---|
74 | |
---|
75 | for (i=0;i<len-10;i++) { // place markers |
---|
76 | if (i%10 == 0) { |
---|
77 | sprintf(buffer,"%li",i+1); |
---|
78 | strncpy(data+i+1,buffer,strlen(buffer)); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | if (GB_read_type(gbd) == GB_STRING) { // read the filter |
---|
83 | str = GB_read_string(gbd); |
---|
84 | }else{ |
---|
85 | str = GB_read_bits(gbd,'-','+'); |
---|
86 | } |
---|
87 | GBS_strcat(strstruct,str); |
---|
88 | GBS_chrcat(strstruct,'\n'); |
---|
89 | char *canc = aw_root->awar(cbs->def_cancel)->read_string(); |
---|
90 | long min = aw_root->awar(cbs->def_min)->read_int()-1; |
---|
91 | long max = aw_root->awar(cbs->def_max)->read_int()-1; |
---|
92 | long flen = 0; |
---|
93 | for (i=0,s=str; *s; ++s,++i){ // transform the filter |
---|
94 | if (strchr(canc,*s) || (i<min) || (max>0 && i > max) ) |
---|
95 | { |
---|
96 | *s = '0'; |
---|
97 | }else{ |
---|
98 | if (i > _2filter_len || _2filter[i] != '0') { |
---|
99 | *s = '1'; |
---|
100 | flen++; |
---|
101 | }else{ |
---|
102 | *s = '0'; |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | GBS_strcat(strstruct,str); |
---|
107 | GBS_chrcat(strstruct,'\n'); |
---|
108 | data = GBS_strclose(strstruct); |
---|
109 | aw_root->awar(cbs->def_len) ->write_int(flen); // export filter |
---|
110 | aw_root->awar(cbs->def_filter)->write_string(str); // export filter |
---|
111 | aw_root->awar(cbs->def_source)->write_string(data); // set display |
---|
112 | free(_2filter); |
---|
113 | free(str); |
---|
114 | free(canc); |
---|
115 | free(data); |
---|
116 | } |
---|
117 | free(to_free_target); |
---|
118 | free(use); |
---|
119 | GB_pop_transaction(cbs->gb_main); |
---|
120 | } |
---|
121 | |
---|
122 | static void awt_add_sequences_to_list(struct adfiltercbstruct *cbs, const char *use, GBDATA *gb_extended,const char *pre,char tpre){ |
---|
123 | GBDATA *gb_name; |
---|
124 | GBDATA *gb_ali; |
---|
125 | GBDATA *gb_data; |
---|
126 | int count; |
---|
127 | |
---|
128 | gb_ali = GB_entry(gb_extended,use); |
---|
129 | if (!gb_ali) return; |
---|
130 | count = 0; |
---|
131 | GBDATA *gb_type = GB_entry(gb_ali,"_TYPE"); |
---|
132 | char *TYPE = strdup(""); |
---|
133 | if (gb_type) TYPE = GB_read_string(gb_type); |
---|
134 | |
---|
135 | gb_name = GB_entry(gb_extended,"name"); |
---|
136 | if (!gb_name) return; |
---|
137 | char *name = GB_read_string(gb_name); |
---|
138 | |
---|
139 | for (gb_data = GB_child(gb_ali); gb_data; gb_data = GB_nextChild(gb_data)) { |
---|
140 | if (GB_read_key_pntr(gb_data)[0] == '_') continue; |
---|
141 | long type = GB_read_type(gb_data); |
---|
142 | if (type == GB_BITS || type == GB_STRING) { |
---|
143 | char *str; |
---|
144 | if (count){ |
---|
145 | str = GBS_global_string_copy("%s%-20s SEQ_%i %s", pre,name,count+1,TYPE); |
---|
146 | }else{ |
---|
147 | str = GBS_global_string_copy("%s%-20s %s", pre,name,TYPE); |
---|
148 | } |
---|
149 | char *target = (char *)GBS_global_string("%c%s%c%s",tpre,GB_read_key_pntr(gb_data),1,name); |
---|
150 | cbs->aws->insert_selection( cbs->id,(char *)str, target ); |
---|
151 | free(str); |
---|
152 | count++; |
---|
153 | } |
---|
154 | } |
---|
155 | free(TYPE); |
---|
156 | free(name); |
---|
157 | } |
---|
158 | |
---|
159 | void awt_create_select_filter_window_gb_cb(void *dummy,struct adfiltercbstruct *cbs){ // update list widget and variables |
---|
160 | AWUSE(dummy); |
---|
161 | AW_root *aw_root = cbs->awr; |
---|
162 | GB_push_transaction(cbs->gb_main); |
---|
163 | char *use = aw_root->awar(cbs->def_alignment)->read_string(); |
---|
164 | GBDATA *gb_extended; |
---|
165 | |
---|
166 | if (cbs->id) { |
---|
167 | |
---|
168 | cbs->aws->clear_selection_list(cbs->id); |
---|
169 | cbs->aws->insert_default_selection( cbs->id, "none", "" ); |
---|
170 | GBDATA *gb_sel = GB_search(cbs->gb_main,AWAR_SPECIES_NAME,GB_STRING); |
---|
171 | char *name = GB_read_string(gb_sel); |
---|
172 | if (strlen(name)){ |
---|
173 | GBDATA *gb_species = GBT_find_species(cbs->gb_main,name); |
---|
174 | if (gb_species){ |
---|
175 | awt_add_sequences_to_list(cbs,use,gb_species,"SEL. SPECIES:",'@'); |
---|
176 | } |
---|
177 | } |
---|
178 | free(name); |
---|
179 | for ( gb_extended = GBT_first_SAI(cbs->gb_main); |
---|
180 | gb_extended; |
---|
181 | gb_extended = GBT_next_SAI(gb_extended)){ |
---|
182 | awt_add_sequences_to_list(cbs,use,gb_extended,"",' '); |
---|
183 | } |
---|
184 | |
---|
185 | cbs->aws->update_selection_list( cbs->id ); |
---|
186 | } |
---|
187 | awt_create_select_filter_window_aw_cb(0,cbs); |
---|
188 | GB_pop_transaction(cbs->gb_main); |
---|
189 | free(use); |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | AW_CL awt_create_select_filter(AW_root *aw_root,GBDATA *gb_main,const char *def_name) |
---|
194 | // def_name = filter name awar (has to exist, name has to be "SOMETHING/name") |
---|
195 | // awars "SOMETHING/filter" (STRING) and "SOMETHING/alignment" (STRING) have to exist as well! |
---|
196 | { |
---|
197 | struct adfiltercbstruct *acbs = new adfiltercbstruct ; |
---|
198 | acbs->gb_main = gb_main; |
---|
199 | GB_push_transaction(acbs->gb_main); |
---|
200 | AW_default aw_def = aw_root->get_default(def_name); |
---|
201 | |
---|
202 | #if defined(DEBUG) |
---|
203 | { |
---|
204 | int len = strlen(def_name); |
---|
205 | |
---|
206 | awt_assert(len >= 5); |
---|
207 | awt_assert(strcmp(def_name+len-5, "/name") == 0); // filter awar has to be "SOMETHING/name" |
---|
208 | } |
---|
209 | #endif // DEBUG |
---|
210 | |
---|
211 | acbs->def_name = GBS_string_eval(def_name,"/name=/name",0); |
---|
212 | acbs->def_filter = GBS_string_eval(def_name,"/name=/filter",0); |
---|
213 | acbs->def_alignment = GBS_string_eval(def_name,"/name=/alignment",0); |
---|
214 | |
---|
215 | acbs->def_min = GBS_string_eval(def_name,"*/name=tmp/*1/min:tmp/tmp=tmp",0); |
---|
216 | aw_root->awar_int( acbs->def_min) ->add_callback((AW_RCB1)awt_create_select_filter_window_aw_cb,(AW_CL)acbs); |
---|
217 | acbs->def_max = GBS_string_eval(def_name,"*/name=tmp/*1/max:tmp/tmp=tmp",0); |
---|
218 | aw_root->awar_int( acbs->def_max) ->add_callback((AW_RCB1)awt_create_select_filter_window_aw_cb,(AW_CL)acbs); |
---|
219 | |
---|
220 | acbs->def_len = GBS_string_eval(def_name,"*/name=tmp/*1/len:tmp/tmp=tmp",0); |
---|
221 | aw_root->awar_int( acbs->def_len); |
---|
222 | |
---|
223 | acbs->def_dest = GBS_string_eval(def_name,"*/name=tmp/*1/dest:tmp/tmp=tmp",0); |
---|
224 | aw_root->awar_string( acbs->def_dest,"",aw_def); |
---|
225 | |
---|
226 | acbs->def_cancel = GBS_string_eval(def_name,"*/name=*1/cancel",0); |
---|
227 | aw_root->awar_string( acbs->def_cancel,".0-=",aw_def); |
---|
228 | |
---|
229 | acbs->def_simplify = GBS_string_eval(def_name,"*/name=*1/simplify",0); |
---|
230 | aw_root->awar_int( acbs->def_simplify,0,aw_def); |
---|
231 | |
---|
232 | acbs->def_subname = GBS_string_eval(def_name,"*/name=tmp/*1/subname:tmp/tmp=tmp",0); |
---|
233 | aw_root->awar_string( acbs->def_subname); |
---|
234 | |
---|
235 | acbs->def_source = GBS_string_eval(def_name,"*/name=tmp/*/source:tmp/tmp=tmp",0); |
---|
236 | aw_root->awar_string( acbs->def_source); |
---|
237 | |
---|
238 | acbs->def_2name = GBS_string_eval(def_name,"*/name=tmp/*/2filter/name:tmp/tmp=tmp",0); |
---|
239 | acbs->def_2filter = GBS_string_eval(def_name,"*/name=tmp/*/2filter/filter:tmp/tmp=tmp",0); |
---|
240 | acbs->def_2alignment = GBS_string_eval(def_name,"*/name=tmp/*/2filter/alignment:tmp/tmp=tmp",0); |
---|
241 | |
---|
242 | aw_root->awar_string( acbs->def_2name ) ->write_string( "- none -" ); |
---|
243 | aw_root->awar_string( acbs->def_2filter ); |
---|
244 | aw_root->awar_string( acbs->def_2alignment ); |
---|
245 | |
---|
246 | acbs->id = 0; |
---|
247 | acbs->aws = 0; |
---|
248 | acbs->awr = aw_root; |
---|
249 | { |
---|
250 | char *fname = aw_root->awar(acbs->def_name)->read_string(); |
---|
251 | const char *fsname = GBS_global_string(" data%c%s",1,fname); |
---|
252 | free(fname); |
---|
253 | aw_root->awar(acbs->def_subname)->write_string(fsname); // cause an callback |
---|
254 | } |
---|
255 | |
---|
256 | aw_root->awar(acbs->def_subname)->touch(); // cause an callback |
---|
257 | |
---|
258 | GBDATA *gb_extended_data = GB_search(acbs->gb_main,"extended_data",GB_CREATE_CONTAINER); |
---|
259 | |
---|
260 | GB_add_callback(gb_extended_data,GB_CB_CHANGED, (GB_CB)awt_create_select_filter_window_gb_cb, (int *)acbs); |
---|
261 | |
---|
262 | GBDATA *gb_sel = GB_search(acbs->gb_main,AWAR_SPECIES_NAME,GB_STRING); |
---|
263 | |
---|
264 | GB_add_callback(gb_sel,GB_CB_CHANGED, (GB_CB)awt_create_select_filter_window_gb_cb, (int *)acbs); |
---|
265 | |
---|
266 | aw_root->awar(acbs->def_alignment)->add_callback( (AW_RCB1)awt_create_select_filter_window_gb_cb,(AW_CL)acbs); |
---|
267 | aw_root->awar(acbs->def_2filter)->add_callback( (AW_RCB1)awt_create_select_filter_window_aw_cb,(AW_CL)acbs); |
---|
268 | aw_root->awar(acbs->def_subname)->add_callback( (AW_RCB1)awt_create_select_filter_window_aw_cb,(AW_CL)acbs); |
---|
269 | |
---|
270 | awt_create_select_filter_window_gb_cb(0,acbs); |
---|
271 | |
---|
272 | GB_pop_transaction(acbs->gb_main); |
---|
273 | return (AW_CL)acbs; |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | void awt_set_awar_to_valid_filter_good_for_tree_methods(GBDATA *gb_main, AW_root *awr, const char *awar_name){ |
---|
278 | GB_transaction transaction_var(gb_main); |
---|
279 | if (GBT_find_SAI(gb_main,"POS_VAR_BY_PARSIMONY")){ |
---|
280 | awr->awar(awar_name)->write_string("POS_VAR_BY_PARSIMONY"); |
---|
281 | return; |
---|
282 | } |
---|
283 | if (GBT_find_SAI(gb_main,"ECOLI")){ |
---|
284 | awr->awar(awar_name)->write_string("ECOLI"); |
---|
285 | return; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | AW_window *awt_create_2_filter_window(AW_root *aw_root,AW_CL res_of_create_select_filter) |
---|
290 | { |
---|
291 | struct adfiltercbstruct *acbs = (struct adfiltercbstruct *) res_of_create_select_filter; |
---|
292 | GB_push_transaction(acbs->gb_main); |
---|
293 | aw_root->awar(acbs->def_2alignment)->map( acbs->def_alignment); |
---|
294 | AW_CL s2filter = awt_create_select_filter(aw_root, acbs->gb_main,acbs->def_2name); |
---|
295 | GB_pop_transaction(acbs->gb_main); |
---|
296 | return awt_create_select_filter_win(aw_root,s2filter); |
---|
297 | } |
---|
298 | |
---|
299 | char *AWT_get_combined_filter_name(AW_root *aw_root, GB_CSTR prefix) { |
---|
300 | char *combined_name = aw_root->awar(GBS_global_string("%s/filter/name", prefix))->read_string(); // "gde/filter/name" |
---|
301 | const char *awar_prefix = AWAR_GDE_FILTER; |
---|
302 | const char *awar_repeated = "/2filter"; |
---|
303 | const char *awar_postfix = "/name"; |
---|
304 | int prefix_len = strlen(awar_prefix); |
---|
305 | int repeated_len = strlen(awar_repeated); |
---|
306 | int postfix_len = strlen(awar_postfix); |
---|
307 | int count; |
---|
308 | |
---|
309 | for (count = 1; ; ++count) { |
---|
310 | char *awar_name = new char[prefix_len + count*repeated_len + postfix_len + 1]; |
---|
311 | strcpy(awar_name, awar_prefix); |
---|
312 | int c; |
---|
313 | for (c=0; c<count; ++c) strcat(awar_name, awar_repeated); |
---|
314 | strcat(awar_name, awar_postfix); |
---|
315 | |
---|
316 | AW_awar *awar_found = aw_root->awar_no_error(awar_name); |
---|
317 | delete [] awar_name; |
---|
318 | |
---|
319 | if (!awar_found) break; // no more filters defined |
---|
320 | char *content = awar_found->read_string(); |
---|
321 | |
---|
322 | if (strstr(content, "none")==0) { // don't add filters named 'none' |
---|
323 | char *new_combined_name = (char*)malloc(strlen(combined_name)+1+strlen(content)+1); |
---|
324 | sprintf(new_combined_name, "%s/%s", combined_name, content); |
---|
325 | free(combined_name); |
---|
326 | combined_name = new_combined_name; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | return combined_name; |
---|
331 | } |
---|
332 | |
---|
333 | AW_window *awt_create_select_filter_win(AW_root *aw_root,AW_CL res_of_create_select_filter) |
---|
334 | { |
---|
335 | struct adfiltercbstruct *acbs = (struct adfiltercbstruct *) res_of_create_select_filter; |
---|
336 | GB_push_transaction(acbs->gb_main); |
---|
337 | |
---|
338 | AW_window_simple *aws = new AW_window_simple; |
---|
339 | aws->init( aw_root, "FILTER_SELECT", "Select Filter"); |
---|
340 | aws->load_xfig("awt/filter.fig"); |
---|
341 | aws->button_length( 10 ); |
---|
342 | |
---|
343 | aws->at("close");aws->callback((AW_CB0)AW_POPDOWN); |
---|
344 | aws->create_button("CLOSE", "CLOSE","C"); |
---|
345 | |
---|
346 | aws->at("help");aws->callback(AW_POPUP_HELP,(AW_CL)"sel_fil.hlp"); |
---|
347 | aws->create_button("HELP", "HELP","H"); |
---|
348 | |
---|
349 | acbs->aws = (AW_window *)aws; |
---|
350 | aws->at("filter"); |
---|
351 | acbs->id = aws->create_selection_list(acbs->def_subname,0,"",20,3); |
---|
352 | |
---|
353 | aws->at("2filter"); |
---|
354 | aws->callback(AW_POPUP,(AW_CL)awt_create_2_filter_window,(AW_CL)acbs); |
---|
355 | aws->create_button(acbs->def_2name,acbs->def_2name); |
---|
356 | |
---|
357 | aws->at("zero"); |
---|
358 | aws->callback((AW_CB1)awt_create_select_filter_window_aw_cb,(AW_CL)acbs); |
---|
359 | aws->create_input_field(acbs->def_cancel,10); |
---|
360 | |
---|
361 | aws->at("sequence"); |
---|
362 | aws->create_text_field(acbs->def_source,1,1); |
---|
363 | |
---|
364 | aws->at("min"); |
---|
365 | aws->create_input_field(acbs->def_min,4); |
---|
366 | |
---|
367 | aws->at("max"); |
---|
368 | aws->create_input_field(acbs->def_max,4); |
---|
369 | |
---|
370 | aws->at("simplify"); |
---|
371 | aws->create_option_menu(acbs->def_simplify); |
---|
372 | aws->insert_option("ORIGINAL DATA","O",0); |
---|
373 | aws->insert_option("TRANSVERSIONS ONLY","T",1); |
---|
374 | aws->insert_option("SIMPLIFIED AA","A",2); |
---|
375 | aws->update_option_menu(); |
---|
376 | |
---|
377 | awt_create_select_filter_window_gb_cb(0,acbs); |
---|
378 | |
---|
379 | aws->button_length( 7 ); |
---|
380 | aws->at("len"); aws->create_button(0,acbs->def_len); |
---|
381 | |
---|
382 | GB_pop_transaction(acbs->gb_main); |
---|
383 | return (AW_window *)aws; |
---|
384 | } |
---|
385 | |
---|
386 | AP_filter *awt_get_filter(AW_root *aw_root,AW_CL res_of_create_select_filter) |
---|
387 | { |
---|
388 | adfiltercbstruct *acbs = (adfiltercbstruct *) res_of_create_select_filter; |
---|
389 | AP_filter *filter = new AP_filter; |
---|
390 | bool initialized = false; |
---|
391 | |
---|
392 | if (acbs) { |
---|
393 | GB_push_transaction(acbs->gb_main); |
---|
394 | |
---|
395 | char *filter_string = aw_root->awar(acbs->def_filter)->read_string(); |
---|
396 | long len = 0; |
---|
397 | |
---|
398 | { |
---|
399 | char *use = aw_root->awar(acbs->def_alignment)->read_string(); |
---|
400 | |
---|
401 | len = GBT_get_alignment_len(acbs->gb_main,use); |
---|
402 | free(use); |
---|
403 | } |
---|
404 | |
---|
405 | if (len != -1) { // have alignment |
---|
406 | filter->init(filter_string,"0",len); |
---|
407 | initialized = true; |
---|
408 | |
---|
409 | int sim = aw_root->awar(acbs->def_simplify)->read_int(); |
---|
410 | filter->enable_simplify((AWT_FILTER_SIMPLIFY)sim); |
---|
411 | free(filter_string); |
---|
412 | } |
---|
413 | |
---|
414 | GB_pop_transaction(acbs->gb_main); |
---|
415 | } |
---|
416 | |
---|
417 | if (!initialized) { |
---|
418 | filter->init("","0",10); |
---|
419 | } |
---|
420 | |
---|
421 | return filter; |
---|
422 | } |
---|
423 | |
---|