source: branches/stable/PHYLO/PH_main.cxx

Last change on this file was 18352, checked in by westram, 4 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : PH_main.cxx                                       //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "phylo.hxx"
12#include "phwin.hxx"
13#include "PH_display.hxx"
14
15#include <awt_sel_boxes.hxx>
16#include <aw_preset.hxx>
17#include <aw_awar.hxx>
18#include <aw_msg.hxx>
19#include <awt.hxx>
20#include <aw_root.hxx>
21#include <arbdbt.h>
22#include <arb_strarray.h>
23
24#include <macros.hxx>
25#include <aw_question.hxx>
26
27AW_HEADER_MAIN
28
29const char *filter_text[FILTER_MODES] = {
30    "don't count (ignore)                       ", // DONT_COUNT
31    "if occurs most often => forget whole column", // SKIP_COLUMN_IF_MAX
32    "if occurs => forget whole column           ", // SKIP_COLUMN_IF_OCCUR
33    "count, but do NOT use as maximum           ", // COUNT_DONT_USE_MAX
34    "treat as uppercase character               ", // TREAT_AS_UPPERCASE
35    "treat as regular character                 ", // TREAT_AS_REGULAR
36};
37
38static bool valid_alignment_selected(AW_root *aw_root, GBDATA *gb_main) {
39    GB_transaction  ta(gb_main);
40    const char     *aliname = aw_root->awar(AWAR_PHYLO_ALIGNMENT)->read_char_pntr();
41    if (GBT_get_alignment_len(gb_main, aliname)<1) {
42        GB_clear_error();
43        return false;
44    }
45    return true;
46}
47
48static void startup_sequence_cb(AW_window *alisel_window, AW_window *main_window, PH_root *ph_root) {
49    AW_root *aw_root = main_window->get_root();
50    GBDATA  *gb_main = ph_root->get_gb_main();
51    if (valid_alignment_selected(aw_root, gb_main)) {
52        if (alisel_window) alisel_window->hide();
53
54        char   *use = aw_root->awar(AWAR_PHYLO_ALIGNMENT)->read_string();
55        PHDATA *phd = new PHDATA(aw_root, ph_root);
56
57        GB_set_cache_size(gb_main, PH_DB_CACHE_SIZE);
58        phd->load(use);
59        phd->ROOT = phd;
60
61        long len = PHDATA::ROOT->get_seq_len(); // @@@ off by one?
62        aw_root->awar(AWAR_PHYLO_FILTER_STOPCOL)->write_int(len);
63        aw_root->awar(AWAR_PHYLO_FILTER_STARTCOL)->set_minmax(0, len);
64        aw_root->awar(AWAR_PHYLO_FILTER_STOPCOL)->set_minmax(0, len);
65
66        main_window->activate();
67        ph_view_species_cb();
68    }
69    else {
70        if (alisel_window) {
71            aw_message("Please select a valid alignment");
72        }
73        else {
74            GBK_terminate("Expected to have a valid alignment");
75        }
76    }
77}
78
79__ATTR__NORETURN static void ph_exit(AW_window *aw_window, PH_root *ph_root) {
80    AW_root *aw_root = aw_window->get_root();
81    shutdown_macro_recording(aw_root);
82
83    GBDATA *gb_main = ph_root->get_gb_main();
84    if (gb_main) {
85        aw_root->unlink_awars_from_DB(gb_main);
86#if defined(DEBUG)
87        AWT_browser_forget_db(gb_main);
88#endif // DEBUG
89        GB_close(gb_main);
90    }
91
92    exit(EXIT_SUCCESS);
93}
94
95
96void expose_cb() {
97    if (PH_display::ph_display && PH_display::ph_display->displayed()!=DISP_NONE) {
98        PH_display::ph_display->clear_window();
99        PH_display::ph_display->display();
100    }
101}
102
103static void resize_cb() {
104    if (PH_display::ph_display) {
105        PH_display::ph_display->resized();
106        PH_display::ph_display->display();
107    }
108}
109
110static void gc_changed_cb(GcChange whatChanged) {
111    switch (whatChanged) {
112        case GC_COLOR_GROUP_USE_CHANGED:
113            ph_assert(0); // not used atm
114            FALLTHROUGH; // in NDEBUG
115        case GC_COLOR_CHANGED:
116            expose_cb();
117            break;
118        case GC_FONT_CHANGED:
119            resize_cb();
120            break;
121    }
122}
123
124static GB_ERROR PH_create_ml_multiline_SAI(GB_CSTR sai_name, int nr, GBDATA **gb_sai_ptr, PH_root *ph_root) {
125    GBDATA   *gb_sai = GBT_find_or_create_SAI(ph_root->get_gb_main(), sai_name);
126    GBDATA   *gbd, *gb2;
127    GB_ERROR  error  = ph_check_initialized();
128
129    if (!error) {
130        for (gbd = GB_child(gb_sai); gbd; gbd = gb2) {
131            gb2 = GB_nextChild(gbd);
132
133            const char *key = GB_read_key_pntr(gbd);
134            if (!strcmp(key, "name")) continue;
135            if (!strncmp(key, "ali_", 4)) continue;
136
137            error = GB_delete(gbd);
138            if (error) break;
139        }
140    }
141
142    if (!error) {
143        GBDATA *gb_ali = GB_search(gb_sai, PHDATA::ROOT->use, GB_FIND);
144        if (gb_ali) {
145            for (gbd = GB_child(gb_ali); gbd; gbd = gb2) {
146                gb2 = GB_nextChild(gbd);
147
148                const char *key = GB_read_key_pntr(gbd);
149                if (!strcmp(key, "data")) continue;
150                if (!strcmp(key, "_TYPE")) continue;
151
152                error = GB_delete(gbd);
153                if (error) break;
154            }
155        }
156    }
157
158    GBDATA *gb_data = NULp, *gb_TYPE = NULp;
159
160    if (!error) {
161        gb_data             = GBT_add_data(gb_sai, PHDATA::ROOT->use, "data", GB_STRING);
162        if (!gb_data) error = GB_await_error();
163    }
164
165    if (!error) {
166        gb_TYPE             = GBT_add_data(gb_sai, PHDATA::ROOT->use, "_TYPE", GB_STRING);
167        if (!gb_TYPE) error = GB_await_error();
168    }
169
170    if (!error && !PHDATA::ROOT->markerline) {
171        error = "Nothing calculated yet";
172    }
173
174    if (!error) {
175        char *full_save_name = NULp;
176        FILE *saveResults    = NULp;
177        if (nr == 2) {
178            char *save_name = GB_unique_filename("conservationProfile", "gnu");
179            saveResults     = GB_fopen_tempfile(save_name, "w+", &full_save_name);
180            GB_remove_on_exit(full_save_name);
181            free(save_name);
182
183            if (!saveResults) error = GB_await_error();
184        }
185
186        if (!error) {
187            AW_window *main_win   = PH_used_windows::windowList->phylo_main_window;
188            long       minhom     = main_win->get_root()->awar(AWAR_PHYLO_FILTER_MINHOM)->read_int();
189            long       maxhom     = main_win->get_root()->awar(AWAR_PHYLO_FILTER_MAXHOM)->read_int();
190            long       startcol   = main_win->get_root()->awar(AWAR_PHYLO_FILTER_STARTCOL)->read_int();
191            long       stopcol    = main_win->get_root()->awar(AWAR_PHYLO_FILTER_STOPCOL)->read_int();
192            float     *markerline = PHDATA::ROOT->markerline;
193            long       len        = PHDATA::ROOT->get_seq_len();
194
195            char *data = ARB_calloc<char>(len+1);
196            int   cnt  = 0;
197
198
199            for (int x=0; x<len; x++) {
200                char c;
201
202                if (x<startcol || x>stopcol) {
203                    c = '.';
204                }
205                else {
206                    float ml = markerline[x];
207                    if (nr==2 && ml>0.0) {
208                        ph_assert(saveResults);
209                        fprintf(saveResults, "%i\t%.2f\n", cnt, ml);
210                        cnt++;
211                    }
212
213                    if (ml>=0.0 && ml>=minhom && ml<=maxhom) {
214                        int digit = -1;
215                        switch (nr) {
216                            case 0: // hundred
217                                if (ml>=100.0) digit = 1;
218                                break;
219                            case 1: // ten
220                                if (ml>=10.0) digit = int(ml/10);
221                                break;
222                            case 2: // one
223                                digit = int(ml);
224                                break;
225                            default:
226                                ph_assert(0);
227                                break;
228                        }
229
230                        if (digit<0) c = '-';
231                        else         c = '0' + digit%10;
232                    }
233                    else {
234                        c = '-';
235                    }
236                }
237
238                data[x] = c;
239            }
240            data[len] = 0;
241
242            if (saveResults) {
243                fclose(saveResults);
244                fprintf(stderr, "Note: Frequencies as well saved to '%s'\n", full_save_name);
245            }
246
247            error = GB_write_string(gb_data, data);
248            if (!error) {
249                const char *buffer = GBS_global_string("FMX: Filter by Maximum Frequency: "
250                                                       "Start %li; Stop %li; Minhom %li%%; Maxhom %li%%",
251                                                       startcol, stopcol, minhom, maxhom);
252                error = GB_write_string(gb_TYPE, buffer);
253            }
254            free(data);
255        }
256        free(full_save_name);
257    }
258
259    if (!error) *gb_sai_ptr = gb_sai;
260    return error;
261}
262
263static void PH_save_ml_multiline_cb(AW_window *aww, PH_root *ph_root) {
264    GB_transaction ta(ph_root->get_gb_main());
265
266    GB_ERROR  error     = NULp;
267    char     *fname     = aww->get_root()->awar(AWAR_PHYLO_MARKERLINENAME)->read_string();
268    int       fname_len = strlen(fname);
269    {
270        char *digit_appended = ARB_alloc<char>(fname_len+2);
271        memcpy(digit_appended, fname, fname_len);
272        strcpy(digit_appended+fname_len, "0");
273
274        freeset(fname, digit_appended);
275    }
276    GBDATA *gb_sai[3];
277    int i;
278    for (i=0; !error && i<3; i++) {
279        fname[fname_len] = '0'+i;
280        error = PH_create_ml_multiline_SAI(fname, i, &gb_sai[i], ph_root);
281    }
282
283    delete fname;
284
285    aw_message_if(ta.close(error));
286}
287
288static void PH_save_ml_cb(AW_window *aww, PH_root *ph_root) {
289    GBDATA  *gb_main = ph_root->get_gb_main();
290    GB_transaction ta(gb_main);
291
292    char   *fname  = aww->get_root()->awar(AWAR_PHYLO_MARKERLINENAME)->read_string();
293    GBDATA *gb_sai = GBT_find_or_create_SAI(gb_main, fname);
294
295    GB_ERROR error = ph_check_initialized();
296
297    if (!error) {
298        for (GBDATA *gbd = GB_child(gb_sai), *gbnext; gbd; gbd = gbnext) {
299            gbnext = GB_nextChild(gbd);
300
301            const char *key = GB_read_key_pntr(gbd);
302            if (!strcmp(key, "name")) continue;
303            if (!strncmp(key, "ali_", 4)) continue;
304
305            error = GB_delete(gbd);
306            if (error) break;
307        }
308    }
309
310    if (!error) {
311        GBDATA *gb_ali = GB_search(gb_sai, PHDATA::ROOT->use, GB_FIND);
312        if (gb_ali) {
313            for (GBDATA *gbd = GB_child(gb_ali), *gbnext; gbd; gbd = gbnext) {
314                gbnext = GB_nextChild(gbd);
315
316                const char *key = GB_read_key_pntr(gbd);
317                if (!strcmp(key, "bits")) continue;
318                if (!strcmp(key, "_TYPE")) continue;
319
320                error = GB_delete(gbd);
321                if (error) break;
322            }
323        }
324    }
325
326    GBDATA *gb_bits = NULp, *gb_TYPE = NULp;
327
328    if (!error) {
329        gb_bits             = GBT_add_data(gb_sai, PHDATA::ROOT->use, "bits", GB_BITS);
330        if (!gb_bits) error = GB_await_error();
331    }
332
333    if (!error) {
334        gb_TYPE             = GBT_add_data(gb_sai, PHDATA::ROOT->use, "_TYPE", GB_STRING);
335        if (!gb_TYPE) error = GB_await_error();
336    }
337
338    if (!error && !PHDATA::ROOT->markerline) {
339        error = "Nothing calculated yet";
340    }
341
342    if (!error) {
343        AW_window *main_win   = PH_used_windows::windowList->phylo_main_window;
344        long       minhom     = main_win->get_root()->awar(AWAR_PHYLO_FILTER_MINHOM)->read_int();
345        long       maxhom     = main_win->get_root()->awar(AWAR_PHYLO_FILTER_MAXHOM)->read_int();
346        long       startcol   = main_win->get_root()->awar(AWAR_PHYLO_FILTER_STARTCOL)->read_int();
347        long       stopcol    = main_win->get_root()->awar(AWAR_PHYLO_FILTER_STOPCOL)->read_int();
348        long       len        = PHDATA::ROOT->get_seq_len();
349        char      *bits       = ARB_calloc<char>(len+1);
350        int        x;
351        float     *markerline = PHDATA::ROOT->markerline;
352
353        for (x=0; x<len; x++) {
354            int bit;
355
356            if (x < startcol || x>stopcol) {
357                bit = 0;
358            }
359            else {
360                float ml = markerline[x];
361
362                if (ml>=0.0 && ml>=minhom && ml<=maxhom) bit = 1;
363                else                     bit = 0;
364
365            }
366            bits[x] = '0'+bit;
367        }
368
369        error = GB_write_bits(gb_bits, bits, len, "0");
370
371        if (!error) {
372            char buffer[1024];
373            sprintf(buffer, "FMX: Filter by Maximum Frequency: "
374                    "Start %li; Stop %li; Minhom %li%%; Maxhom %li%%",
375                    startcol, stopcol, minhom, maxhom);
376
377            error = GB_write_string(gb_TYPE, buffer);
378        }
379        free(bits);
380    }
381    free(fname);
382    aw_message_if(ta.close(error));
383}
384
385
386static AW_window *PH_save_markerline(AW_root *root, PH_root *ph_root, int multi_line) {
387    // multi_line ==1 -> save three SAI's usable as column percentage
388
389    root->awar_string(AWAR_PHYLO_MARKERLINENAME, "markerline", AW_ROOT_DEFAULT);
390
391    AW_window_simple *aws = new AW_window_simple;
392
393    if (multi_line) {
394        aws->init(root, "EXPORT_FREQUENCY_LINES", "Export Frequency Lines");
395    }
396    else {
397        aws->init(root, "EXPORT_MARKER_LINE", "Export Marker Line");
398    }
399
400    aws->load_xfig("phylo/save_markerline.fig");
401
402    aws->callback(AW_POPDOWN);
403    aws->at("close");
404    aws->create_button("CLOSE", "CLOSE", "C");
405
406    aws->callback(makeHelpCallback("ph_export_markerline.hlp"));
407    aws->at("help");
408    aws->create_button("HELP", "HELP", "H");
409
410    aws->at("name");
411    aws->create_input_field(AWAR_PHYLO_MARKERLINENAME);
412
413    aws->at("box");
414    awt_create_SAI_selection_list(ph_root->get_gb_main(), aws, AWAR_PHYLO_MARKERLINENAME, false);
415
416    aws->at("save");
417    if (multi_line) aws->callback(makeWindowCallback(PH_save_ml_multiline_cb, ph_root));
418    else            aws->callback(makeWindowCallback(PH_save_ml_cb, ph_root));
419    aws->create_button("EXPORT", "EXPORT", "E");
420
421    return aws;
422}
423
424static AW_window *create_phyl_main_window(AW_root *aw_root, PH_root *ph_root) {
425    AW_window_menu_modes *awm = new AW_window_menu_modes;
426    awm->init(aw_root, "ARB_PHYLO", "ARB_PHYLO", 830, 630);
427
428    // create menus and menu inserts with callbacks
429
430    const GcChangedCallback gcChangedCb = makeGcChangedCallback(gc_changed_cb);
431
432    AW_gc_manager *gcmiddle =
433        AW_manage_GC(awm,
434                     awm->get_window_id(),
435                     awm->get_device(AW_MIDDLE_AREA),
436                     PH_GC_DRAG, AW_GCM_DATA_AREA,
437                     gcChangedCb,
438                     "#C4B6D0",
439                     "#SEQUENCE$#000000",
440                     "#MARKER$#E0F570",
441                     "NOT_MARKER$#A270C0",
442                     NULp);
443
444    AW_manage_GC(awm,
445                 "ARB_PHYLO_BOTTOM",
446                 awm->get_device(AW_BOTTOM_AREA),
447                 PH_GC_BOTTOM_DRAG, AW_GCM_WINDOW_AREA,
448                 gcChangedCb,
449                 "lightgrey",
450                 "#FOOTER",
451                 NULp);
452
453
454#if defined(DEBUG)
455    AWT_create_debug_menu(awm);
456#endif // DEBUG
457
458    // File menu
459    awm->create_menu("File", "F");
460    awm->insert_menu_topic("export_filter", "Export Filter",      "E", "ph_export_markerline.hlp", AWM_ALL, makeCreateWindowCallback(PH_save_markerline, ph_root, 0));
461    awm->insert_menu_topic("export_freq",   "Export Frequencies", "F", "ph_export_markerline.hlp", AWM_ALL, makeCreateWindowCallback(PH_save_markerline, ph_root, 1));
462    insert_macro_menu_entry(awm, false);
463
464    awm->insert_menu_topic("quit", "Quit", "Q", "quit.hlp", AWM_ALL, makeWindowCallback(ph_exit, ph_root));
465
466    // Calculate menu
467    awm->create_menu("Calculate", "C");
468    awm->insert_menu_topic("calc_column_filter", "Column Filter", "F", "no help", AWM_ALL, makeWindowCallback(ph_calc_filter_cb));
469
470    // Config menu
471    awm->create_menu("Config", "o");
472    awm->insert_menu_topic("config_column_filter", "Column Filter", "F", "no help", AWM_ALL, PH_create_filter_window);
473
474    // Properties menu
475    awm->create_menu("Properties", "P");
476    awm->insert_menu_topic("props_menu", "Frame settings ...",   "F", "props_frame.hlp", AWM_ALL, AW_preset_window);
477    awm->insert_menu_topic("props_data", "Colors and Fonts ...", "C", "color_props.hlp", AWM_ALL, makeCreateWindowCallback(AW_create_gc_window, gcmiddle));
478    awm->sep______________();
479    AW_insert_common_property_menu_entries(awm);
480    awm->sep______________();
481    awm->insert_menu_topic("save_props", "Save Properties (phylo.arb)", "S", "savedef.hlp", AWM_ALL, AW_save_properties);
482
483
484    // set window areas
485    awm->set_info_area_height(30);
486    awm->at(5, 2);
487    awm->auto_space(5, -2);
488
489    awm->callback(makeWindowCallback(ph_exit, ph_root));
490    awm->button_length(0);
491    awm->help_text("quit.hlp");
492    awm->create_button("QUIT", "QUIT");
493
494    awm->callback(makeHelpCallback("phylo.hlp"));
495    awm->button_length(0);
496    awm->create_button("HELP", "HELP", "H");
497
498
499    awm->set_bottom_area_height(120);
500
501    awm->set_expose_callback(AW_MIDDLE_AREA, makeWindowCallback(expose_cb));
502    awm->set_resize_callback(AW_MIDDLE_AREA, makeWindowCallback(resize_cb));
503    awm->set_expose_callback(AW_BOTTOM_AREA, makeWindowCallback(display_status_cb));
504    awm->set_resize_callback(AW_BOTTOM_AREA, makeWindowCallback(display_status_cb));
505
506    return awm;
507}
508
509
510static AW_window *create_select_alignment_window(AW_root *aw_root, AW_window *main_window, PH_root *ph_root) {
511    AW_window_simple *aws = new AW_window_simple;
512
513    aws->init(aw_root, "SELECT_ALIGNMENT", "ARB_PHYLO: Select alignment");
514    aws->load_xfig("phylo/select_ali.fig");
515    aws->button_length(10);
516
517    aws->at("which_alignment");
518    awt_create_ALI_selection_list(ph_root->get_gb_main(), (AW_window *)aws, AWAR_PHYLO_ALIGNMENT, "*=");
519
520    aws->auto_space(10, 10);
521
522    aws->at("ok");
523    aws->callback(makeWindowCallback(startup_sequence_cb, main_window, ph_root));
524    aws->create_button("OK", "Ok", "D");
525
526    aws->callback(makeWindowCallback(ph_exit, ph_root));
527    aws->create_button("ABORT", "Abort", "D");
528
529    return aws;
530}
531
532
533
534PH_used_windows::PH_used_windows() {
535    memset((char *) this, 0, sizeof(PH_used_windows));
536}
537
538// initialize 'globals'
539PH_used_windows *PH_used_windows::windowList = NULp;
540PH_display      *PH_display::ph_display      = NULp;
541PHDATA          *PHDATA::ROOT                = NULp;
542
543static void create_variables(AW_root *aw_root, AW_default def, GBDATA *gb_main) {
544    aw_root->awar_string(AWAR_PHYLO_ALIGNMENT,     "", def);
545    aw_root->awar_string(AWAR_PHYLO_FILTER_FILTER, "", def);
546    PH_create_filter_variables(aw_root, def, gb_main);
547}
548
549int ARB_main(int argc, char *argv[]) {
550    aw_initstatus();
551
552    GB_shell shell;
553    AW_root  *aw_root = AWT_create_root("phylo.arb", "ARB_PHYLO", need_macro_ability());
554
555    int exitcode = EXIT_SUCCESS;
556    if (argc > 2 || (argc == 2 && strcmp(argv[1], "--help") == 0)) {
557        fprintf(stderr, "Usage: arb_phylo [database]\n");
558        exitcode = EXIT_FAILURE;
559    }
560    else {
561        const char *db_server = (argc == 2 ? argv[1] : ":");
562
563        PH_used_windows *puw = new PH_used_windows;
564        PH_display      *phd = new PH_display;
565
566        PH_root  *ph_root = new PH_root;
567        GB_ERROR  error   = ph_root->open(db_server);
568
569        if (!error) error = configure_macro_recording(aw_root, "ARB_PHYLO", ph_root->get_gb_main());
570        if (!error) {
571            GBDATA *gb_main = ph_root->get_gb_main();
572
573            // create arb_phylo awars :
574            create_variables(aw_root, AW_ROOT_DEFAULT, gb_main);
575            ARB_init_global_awars(aw_root, AW_ROOT_DEFAULT, gb_main);
576#if defined(DEBUG)
577            AWT_create_db_browser_awars(aw_root, AW_ROOT_DEFAULT);
578#endif // DEBUG
579
580#if defined(DEBUG)
581            AWT_announce_db_to_browser(gb_main, GBS_global_string("ARB-database (%s)", db_server));
582#endif // DEBUG
583
584            // create main window :
585
586            puw->phylo_main_window = create_phyl_main_window(aw_root, ph_root);
587            puw->windowList        = puw;
588
589            phd->ph_display = phd;
590
591            // loading database
592            GB_push_transaction(gb_main);
593
594            ConstStrArray alignment_names;
595            GBT_get_alignment_names(alignment_names, gb_main);
596
597            int num_alignments;
598            for (num_alignments = 0; alignment_names[num_alignments]; num_alignments++) {}
599
600            if (num_alignments > 1) {
601                char *defaultAli = GBT_get_default_alignment(gb_main);
602                aw_root->awar(AWAR_PHYLO_ALIGNMENT)->write_string(defaultAli);
603                create_select_alignment_window(aw_root, puw->phylo_main_window, ph_root)->show();
604                free(defaultAli);
605            }
606            else {
607                aw_root->awar(AWAR_PHYLO_ALIGNMENT)->write_string(alignment_names[0]);
608                startup_sequence_cb(NULp, puw->phylo_main_window, ph_root);
609            }
610            GB_pop_transaction(gb_main);
611
612            AWT_install_cb_guards();
613            aw_root->main_loop();
614        }
615
616        if (error) {
617            aw_popup_exit(error);
618            exitcode = EXIT_FAILURE;
619        }
620    }
621
622    delete aw_root;
623    return exitcode;
624}
625
Note: See TracBrowser for help on using the repository browser.