source: trunk/SL/GUI_ALIVIEW/AWT_filter.cxx

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