source: branches/alilink/SL/GUI_ALIVIEW/AWT_filter.cxx

Last change on this file was 17180, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1#include "awt_filter.hxx"
2#include "awt_sel_boxes.hxx"
3#include "ga_local.h"
4
5#include <aw_awars.hxx>
6#include <aw_root.hxx>
7#include <aw_select.hxx>
8#include <AP_filter.hxx>
9#include <arbdbt.h>
10#include <arb_strbuf.h>
11#include <ad_cb.h>
12#include <arb_str.h>
13
14//! recalc filter
15static void awt_create_select_filter_window_aw_cb(UNFIXED, adfiltercbstruct *cbs) {
16    // update the variables
17    GB_push_transaction(cbs->gb_main);
18
19    AW_root *aw_root        = cbs->awr;
20    char    *target         = aw_root->awar(cbs->def_subname)->read_string();
21    char    *to_free_target = target;
22    char    *use            = aw_root->awar(cbs->def_alignment)->read_string();
23    char    *name           = strchr(target, 1);
24    GBDATA  *gbd            = NULp;
25
26    if (name) {
27        *(name++) = 0;
28        target++;
29        GBDATA *gb_species;
30        if (target[-1] == '@') {
31            gb_species = GBT_find_species(cbs->gb_main, name);
32        }
33        else {
34            gb_species = GBT_find_SAI(cbs->gb_main, name);
35        }
36        if (gb_species) {
37            GBDATA *gb_ali = GB_search(gb_species, use, GB_FIND);
38            if (gb_ali) {
39                gbd = GB_search(gb_ali, target, GB_FIND);
40            }
41            else {
42                GB_clear_error();
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    }
52    else {
53        GBDATA *gb_name = GB_get_father(gbd);   // ali_xxxx
54        gb_name = GB_brother(gb_name, "name");
55        char *name2 = GB_read_string(gb_name);
56        aw_root->awar(cbs->def_name)->write_string(name2);
57        free(name2);
58        char *_2filter = aw_root->awar(cbs->def_2filter)->read_string();
59        long _2filter_len = strlen(_2filter);
60
61        char *s, *str;
62        long len = GBT_get_alignment_len(cbs->gb_main, use);
63        GBS_strstruct *strstruct = GBS_stropen(5000);
64        long i; for (i=0; i<len; i++) { // build position line
65            if (i%10 == 0) {
66                GBS_chrcat(strstruct, '#');
67            }
68            else if (i%5==0) {
69                GBS_chrcat(strstruct, '|');
70            }
71            else {
72                GBS_chrcat(strstruct, '.');
73            }
74        }
75        GBS_chrcat(strstruct, '\n');
76        char *data = GBS_mempntr(strstruct);
77
78        for (i=0; i<len-10; i++) {  // place markers
79            if (i%10 == 0) {
80                char buffer[256];
81                sprintf(buffer, "%li", i+1);
82                memcpy(data+i+1, buffer, strlen(buffer)); // copy string w/o trailing 0-byte!
83            }
84        }
85
86        if (GB_read_type(gbd) == GB_STRING) {   // read the filter
87            str = GB_read_string(gbd);
88        }
89        else {
90            str = GB_read_bits(gbd, '-', '+');
91        }
92        GBS_strcat(strstruct, str);
93        GBS_chrcat(strstruct, '\n');
94        char *canc = aw_root->awar(cbs->def_cancel)->read_string();
95        long min = aw_root->awar(cbs->def_min)->read_int()-1;
96        long max = aw_root->awar(cbs->def_max)->read_int()-1;
97        long flen = 0;
98        for (i=0, s=str; *s; ++s, ++i) {    // transform the filter
99            if (strchr(canc, *s) || (i<min) || (max>0 && i > max)) {
100                *s = '0';
101            }
102            else {
103                if (i > _2filter_len || _2filter[i] != '0') {
104                    *s = '1';
105                    flen++;
106                }
107                else {
108                    *s = '0';
109                }
110            }
111        }
112        GBS_strcat(strstruct, str);
113        GBS_chrcat(strstruct, '\n');
114
115        aw_root->awar(cbs->def_len)   ->write_int(flen);    // export filter
116        aw_root->awar(cbs->def_filter)->write_string(str);  // export filter
117        aw_root->awar(cbs->def_source)->write_string(GBS_mempntr(strstruct)); // set display
118
119        free(_2filter);
120        free(str);
121        free(canc);
122        GBS_strforget(strstruct);
123    }
124    free(to_free_target);
125    free(use);
126    GB_pop_transaction(cbs->gb_main);
127}
128
129static void awt_add_sequences_to_list(adfiltercbstruct *cbs, const char *use, GBDATA *gb_extended, const char *pre, char tpre) {
130    GBDATA *gb_ali = GB_entry(gb_extended, use);
131
132    if (gb_ali) {
133        int         count   = 0;
134        GBDATA     *gb_type = GB_entry(gb_ali, "_TYPE");
135        const char *TYPE    = gb_type ? GB_read_char_pntr(gb_type) : "";
136        const char *name    = GBT_read_name(gb_extended);
137        GBDATA     *gb_data;
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] != '_') {
141                long type = GB_read_type(gb_data);
142
143                if (type == GB_BITS || type == GB_STRING) {
144                    char *str;
145
146                    if (count) str = GBS_global_string_copy("%s%-20s SEQ_%i %s", pre, name, count + 1, TYPE);
147                    else str       = GBS_global_string_copy("%s%-20s       %s", pre, name, TYPE);
148
149                    const char *target = GBS_global_string("%c%s%c%s", tpre, GB_read_key_pntr(gb_data), 1, name);
150
151                    cbs->filterlist->insert(str, target);
152                    free(str);
153                    count++;
154                }
155            }
156        }
157    }
158}
159
160static void awt_create_select_filter_window_gb_cb(UNFIXED, adfiltercbstruct *cbs) {
161    // update list widget and variables
162    GB_push_transaction(cbs->gb_main);
163
164    if (cbs->filterlist) {
165        char *use = cbs->awr->awar(cbs->def_alignment)->read_string();
166
167        cbs->filterlist->clear();
168        cbs->filterlist->insert_default("none", "");
169
170        const char *name = GBT_readOrCreate_char_pntr(cbs->gb_main, AWAR_SPECIES_NAME, "");
171        if (name[0]) {
172            GBDATA *gb_species = GBT_find_species(cbs->gb_main, name);
173            if (gb_species) {
174                awt_add_sequences_to_list(cbs, use, gb_species, "SEL. SPECIES:", '@');
175            }
176        }
177
178        for (GBDATA *gb_extended = GBT_first_SAI(cbs->gb_main);
179             gb_extended;
180             gb_extended = GBT_next_SAI(gb_extended))
181        {
182            awt_add_sequences_to_list(cbs, use, gb_extended, "", ' ');
183        }
184
185        cbs->filterlist->update();
186        free(use);
187    }
188    awt_create_select_filter_window_aw_cb(NULp, cbs);
189    GB_pop_transaction(cbs->gb_main);
190}
191
192void awt_create_filter_awars(AW_root *aw_root, AW_default aw_def, const char *awar_filtername, const char *awar_mapto_alignment) {
193    /*! creates awars needed for filter definition (see awt_create_select_filter())
194     *
195     * created awars: "SOMETHING/name" (as specified in param awar_filtername; type STRING)
196     *                "SOMETHING/filter" (type STRING)
197     *                "SOMETHING/alignment" (type STRING)
198     *
199     * The names of the created awars are available via adfiltercbstruct (after awt_create_select_filter() was called).
200     *
201     * @param aw_root              application root
202     * @param aw_def               database for created awars
203     * @param awar_filtername      name of filtername awar (has to be "SOMETHING/name";
204     *                             awarname should start with "tmp/", saving in properties/db does not work correctly!).
205     * @param awar_mapto_alignment if given, "SOMETHING/alignment" is mapped to this awar
206     */
207
208    ga_assert(ARB_strBeginsWith(awar_filtername, "tmp/")); // see above
209
210    char *awar_filter    = GBS_string_eval(awar_filtername, "/name=/filter");
211    char *awar_alignment = GBS_string_eval(awar_filtername, "/name=/alignment");
212
213    aw_root->awar_string(awar_filtername, "none", aw_def);
214    aw_root->awar_string(awar_filter,     "",     aw_def);
215
216    AW_awar *awar_ali = aw_root->awar_string(awar_alignment, "", aw_def);
217    if (awar_mapto_alignment) awar_ali->map(awar_mapto_alignment);
218
219    free(awar_alignment);
220    free(awar_filter);
221}
222
223adfiltercbstruct *awt_create_select_filter(AW_root *aw_root, GBDATA *gb_main, const char *def_name) {
224    /*! Create filter definition (allowing customization and programmatical use of a filter)
225     *
226     * @param aw_root  application root
227     * @param gb_main  DB root node
228     * @param def_name filtername awarname (name has to be "SOMETHING/name"; create using awt_create_filter_awars())
229     * @return the filter definition
230     * @see awt_create_select_filter_win(), awt_get_filter()
231     */
232
233    adfiltercbstruct *acbs   = new adfiltercbstruct;
234    acbs->gb_main            = gb_main;
235    AW_default        aw_def = AW_ROOT_DEFAULT;
236
237    GB_push_transaction(acbs->gb_main);
238
239#if defined(DEBUG)
240    {
241        int len = strlen(def_name);
242
243        ga_assert(len >= 5);
244        ga_assert(strcmp(def_name+len-5, "/name") == 0); // filter awar has to be "SOMETHING/name"
245    }
246#endif                          // DEBUG
247
248    acbs->def_name      = GBS_string_eval(def_name, "/name=/name");
249    acbs->def_filter    = GBS_string_eval(def_name, "/name=/filter");
250    acbs->def_alignment = GBS_string_eval(def_name, "/name=/alignment");
251
252    acbs->def_min = GBS_string_eval(def_name, "*/name=tmp/*1/min:tmp/tmp=tmp");
253    acbs->def_max = GBS_string_eval(def_name, "*/name=tmp/*1/max:tmp/tmp=tmp");
254    aw_root->awar_int(acbs->def_min)->add_callback(makeRootCallback(awt_create_select_filter_window_aw_cb, acbs));
255    aw_root->awar_int(acbs->def_max)->add_callback(makeRootCallback(awt_create_select_filter_window_aw_cb, acbs));
256
257    acbs->def_len = GBS_string_eval(def_name, "*/name=tmp/*1/len:tmp/tmp=tmp");
258    aw_root->awar_int(acbs->def_len);
259
260    acbs->def_dest = GBS_string_eval(def_name, "*/name=tmp/*1/dest:tmp/tmp=tmp");
261    aw_root->awar_string(acbs->def_dest, "", aw_def);
262
263    acbs->def_cancel = GBS_string_eval(def_name, "*/name=*1/cancel");
264    aw_root->awar_string(acbs->def_cancel, ".0-=", aw_def);
265
266    acbs->def_simplify = GBS_string_eval(def_name, "*/name=*1/simplify");
267    aw_root->awar_int(acbs->def_simplify, 0, aw_def);
268
269    acbs->def_subname = GBS_string_eval(def_name, "*/name=tmp/*1/subname:tmp/tmp=tmp");
270    aw_root->awar_string(acbs->def_subname);
271
272    acbs->def_source = GBS_string_eval(def_name, "*/name=tmp/*/source:tmp/tmp=tmp");
273    aw_root->awar_string(acbs->def_source);
274
275    acbs->def_2name      = GBS_string_eval(def_name, "*/name=tmp/*/2filter/name:tmp/tmp=tmp");
276    acbs->def_2filter    = GBS_string_eval(def_name, "*/name=tmp/*/2filter/filter:tmp/tmp=tmp");
277    acbs->def_2alignment = GBS_string_eval(def_name, "*/name=tmp/*/2filter/alignment:tmp/tmp=tmp");
278
279    aw_root->awar_string(acbs->def_2name)->write_string("- none -");
280    aw_root->awar_string(acbs->def_2filter);
281    aw_root->awar_string(acbs->def_2alignment);
282
283    acbs->filterlist = NULp;
284    acbs->aw_filt    = NULp;
285    acbs->awr        = aw_root;
286    {
287        char *fname = aw_root->awar(acbs->def_name)->read_string();
288        const char *fsname = GBS_global_string(" data%c%s", 1, fname);
289        free(fname);
290        aw_root->awar(acbs->def_subname)->write_string(fsname);     // cause an callback
291    }
292
293    aw_root->awar(acbs->def_subname)->touch();      // cause an callback
294
295    GBDATA *gb_sai_data = GBT_get_SAI_data(acbs->gb_main);
296    GBDATA *gb_sel      = GB_search(acbs->gb_main, AWAR_SPECIES_NAME, GB_STRING);
297
298    GB_add_callback(gb_sai_data, GB_CB_CHANGED, makeDatabaseCallback(awt_create_select_filter_window_gb_cb, acbs));
299    GB_add_callback(gb_sel,      GB_CB_CHANGED, makeDatabaseCallback(awt_create_select_filter_window_gb_cb, acbs));
300
301    aw_root->awar(acbs->def_alignment)->add_callback(makeRootCallback(awt_create_select_filter_window_gb_cb, acbs));
302    aw_root->awar(acbs->def_2filter)  ->add_callback(makeRootCallback(awt_create_select_filter_window_aw_cb, acbs));
303    aw_root->awar(acbs->def_subname)  ->add_callback(makeRootCallback(awt_create_select_filter_window_aw_cb, acbs));
304
305    awt_create_select_filter_window_gb_cb(NULp, acbs);
306
307    GB_pop_transaction(acbs->gb_main);
308    return acbs;
309}
310
311
312void awt_set_awar_to_valid_filter_good_for_tree_methods(GBDATA *gb_main, AW_root *awr, const char *awar_name) {
313    GB_transaction transaction_var(gb_main);
314    if (GBT_find_SAI(gb_main, "POS_VAR_BY_PARSIMONY")) {
315        awr->awar(awar_name)->write_string("POS_VAR_BY_PARSIMONY");
316        return;
317    }
318    if (GBT_find_SAI(gb_main, "ECOLI")) {
319        awr->awar(awar_name)->write_string("ECOLI");
320        return;
321    }
322}
323
324static AW_window *awt_create_2_filter_window(AW_root *aw_root, adfiltercbstruct *acbs) {
325    GB_push_transaction(acbs->gb_main);
326    aw_root->awar(acbs->def_2alignment)->map(acbs->def_alignment);
327    adfiltercbstruct *s2filter = awt_create_select_filter(aw_root, acbs->gb_main, acbs->def_2name);
328    GB_pop_transaction(acbs->gb_main);
329    return awt_create_select_filter_win(aw_root, s2filter);
330}
331
332char *AWT_get_combined_filter_name(AW_root *aw_root, GB_CSTR prefix) {
333    char       *combined_name = aw_root->awar(GBS_global_string("%s/filter/name", prefix))->read_string(); // "gde/filter/name"
334    const char *awar_prefix   = AWAR_GDE_FILTER;
335    const char *awar_repeated = "/2filter";
336    const char *awar_postfix  = "/name";
337    int         prefix_len    = strlen(awar_prefix);
338    int         repeated_len  = strlen(awar_repeated);
339    int         postfix_len   = strlen(awar_postfix);
340    int         count;
341
342    for (count = 1; ; ++count) {
343        char *awar_name = new char[prefix_len + count*repeated_len + postfix_len + 1];
344        strcpy(awar_name, awar_prefix);
345        int c;
346        for (c=0; c<count; ++c) strcat(awar_name, awar_repeated);
347        strcat(awar_name, awar_postfix);
348
349        AW_awar *awar_found = aw_root->awar_no_error(awar_name);
350        delete [] awar_name;
351
352        if (!awar_found) break; // no more filters defined
353        char *content = awar_found->read_string();
354
355        if (!strstr(content, "none")) { // don't add filters named 'none'
356            freeset(combined_name, GBS_global_string_copy("%s/%s", combined_name, content));
357        }
358    }
359
360    return combined_name;
361}
362
363AW_window *awt_create_select_filter_win(AW_root *aw_root, adfiltercbstruct *acbs) {
364    //! Create a filter selection window
365    if (!acbs->aw_filt) {
366        GB_push_transaction(acbs->gb_main);
367
368        AW_window_simple *aws = new AW_window_simple;
369        {
370            int   checksum  = GBS_checksum(acbs->def_name, true, NULp);
371            char *window_id = GBS_global_string_copy("FILTER_SELECT_%i", checksum); // make window id awar specific
372
373            aws->init(aw_root, window_id, "Select Filter");
374            free(window_id);
375        }
376        aws->load_xfig("awt/filter.fig");
377        aws->button_length(10);
378
379        aws->at("close");
380        aws->callback(AW_POPDOWN);
381        aws->create_button("CLOSE", "CLOSE", "C");
382
383        aws->at("help"); aws->callback(makeHelpCallback("sel_fil.hlp"));
384        aws->create_button("HELP", "HELP", "H");
385
386        acbs->aw_filt = aws; // store the filter selection window in 'acbs'
387
388        aws->at("filter");
389        acbs->filterlist = aws->create_selection_list(acbs->def_subname, 20, 3, true);
390
391        aws->at("2filter");
392        aws->callback(makeCreateWindowCallback(awt_create_2_filter_window, acbs));
393        aws->create_button(acbs->def_2name, acbs->def_2name);
394
395        aws->at("zero");
396        aws->callback(makeWindowCallback(awt_create_select_filter_window_aw_cb, acbs)); // @@@ used as INPUTFIELD_CB (see #559)
397        aws->create_input_field(acbs->def_cancel, 10);
398
399        aws->at("sequence");
400        aws->create_text_field(acbs->def_source, 1, 1);
401
402        aws->at("min");
403        aws->create_input_field(acbs->def_min, 4);
404
405        aws->at("max");
406        aws->create_input_field(acbs->def_max, 4);
407
408        aws->at("simplify");
409        aws->create_option_menu(acbs->def_simplify, true);
410        aws->insert_option("ORIGINAL DATA", "O", 0);
411        aws->sens_mask(AWM_EXP);
412        aws->insert_option("TRANSVERSIONS ONLY", "T", 1);
413        aws->insert_option("SIMPLIFIED AA", "A", 2);
414        aws->sens_mask(AWM_ALL);
415        aws->update_option_menu();
416
417        awt_create_select_filter_window_gb_cb(NULp, acbs);
418
419        aws->button_length(7);
420        aws->at("len");
421        aws->create_button(NULp, acbs->def_len);
422
423        GB_pop_transaction(acbs->gb_main);
424    }
425
426    return acbs->aw_filt;
427}
428
429AP_filter *awt_get_filter(adfiltercbstruct *acbs) {
430    /*! create a filter from settings made in filter-definition window.
431     *  always returns a filter, use awt_invalid_filter() to check for validity
432     */
433    AP_filter *filter = NULp;
434
435    if (acbs) {
436        GB_push_transaction(acbs->gb_main);
437
438        char *filter_string = acbs->awr->awar(acbs->def_filter)->read_string();
439        long  len           = 0;
440
441        {
442            char *use = acbs->awr->awar(acbs->def_alignment)->read_string();
443
444            len = GBT_get_alignment_len(acbs->gb_main, use);
445            free(use);
446        }
447
448        if (len == -1) { // no alignment -> uses dummy filter
449            GB_clear_error();
450        }
451        else { // have alignment
452            filter  = new AP_filter(filter_string, "0", len);
453            int sim = acbs->awr->awar(acbs->def_simplify)->read_int();
454            filter->enable_simplify((AWT_FILTER_SIMPLIFY)sim);
455            free(filter_string);
456        }
457
458        GB_pop_transaction(acbs->gb_main);
459    }
460
461    if (!filter) filter = new AP_filter(0); // empty dummy filter
462    return filter;
463}
464
465GB_ERROR awt_invalid_filter(AP_filter *filter) {
466    return filter->is_invalid();
467}
468
469void awt_destroy_filter(AP_filter *filter) {
470    delete filter;
471}
Note: See TracBrowser for help on using the repository browser.