source: tags/arb-6.0/AWTI/AWTI_export.cxx

Last change on this file was 12267, checked in by westram, 10 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1// ============================================================= //
2//                                                               //
3//   File      : AWTI_export.cxx                                 //
4//   Purpose   :                                                 //
5//                                                               //
6//   Institute of Microbiology (Technical University Munich)     //
7//   http://www.arb-home.de/                                     //
8//                                                               //
9// ============================================================= //
10
11#include "awti_export.hxx"
12
13#include <seqio.hxx>
14#include <awt_filter.hxx>
15#include <AP_filter.hxx>
16#include <aw_awars.hxx>
17#include <aw_file.hxx>
18#include <aw_window.hxx>
19#include <aw_msg.hxx>
20#include <arb_progress.h>
21#include <aw_root.hxx>
22#include <arbdbt.h>
23#include <arb_str.h>
24#include <arb_file.h>
25#include <arb_strbuf.h>
26
27#define AWAR_EXPORT_FILE           "tmp/export_db/file"
28#define AWAR_EXPORT_FORM           "tmp/export/form"
29#define AWAR_EXPORT_ALI            "tmp/export/alignment"
30#define AWAR_EXPORT_MULTIPLE_FILES "tmp/export/multiple_files"
31#define AWAR_EXPORT_MARKED         "export/marked"
32#define AWAR_EXPORT_COMPRESS       "export/compress"
33#define AWAR_EXPORT_FILTER_PREFIX  "export/filter"
34#define AWAR_EXPORT_FILTER_NAME    AWAR_EXPORT_FILTER_PREFIX "/name"
35#define AWAR_EXPORT_FILTER_FILTER  AWAR_EXPORT_FILTER_PREFIX "/filter"
36#define AWAR_EXPORT_FILTER_ALI     AWAR_EXPORT_FILTER_PREFIX "/alignment"
37#define AWAR_EXPORT_CUTSTOP        "export/cutstop"
38
39#define awti_assert(cond) arb_assert(cond)
40
41static void export_go_cb(AW_window *aww, AW_CL cl_gb_main, AW_CL res_from_awt_create_select_filter) {
42    awti_assert(!GB_have_error());
43
44    GBDATA           *gb_main = (GBDATA*)cl_gb_main;
45    GB_transaction    ta(gb_main);
46    adfiltercbstruct *acbs    = (adfiltercbstruct*)res_from_awt_create_select_filter;
47
48    arb_progress progress("Exporting data");
49
50    AW_root  *awr            = aww->get_root();
51    char     *formname       = awr->awar(AWAR_EXPORT_FORM"/file_name")->read_string();
52    int       multiple       = awr->awar(AWAR_EXPORT_MULTIPLE_FILES)->read_int();
53    int       marked_only    = awr->awar(AWAR_EXPORT_MARKED)->read_int();
54    int       cut_stop_codon = awr->awar(AWAR_EXPORT_CUTSTOP)->read_int();
55    int       compress       = awr->awar(AWAR_EXPORT_COMPRESS)->read_int();
56    GB_ERROR  error          = 0;
57
58    char *outname      = awr->awar(AWAR_EXPORT_FILE"/file_name")->read_string();
59    char *real_outname = 0;     // with suffix (name of first file if multiple)
60
61    AP_filter *filter = awt_get_filter(acbs);
62    char *db_name = awr->awar(AWAR_DB_NAME)->read_string();
63
64    error = SEQIO_export_by_format(gb_main, marked_only, filter, cut_stop_codon, compress,
65                                   db_name, formname, outname, multiple, &real_outname);
66    free(db_name);
67    if (error) aw_message(error);
68
69    if (real_outname) awr->awar(AWAR_EXPORT_FILE"/file_name")->write_string(real_outname);
70
71    AW_refresh_fileselection(awr, AWAR_EXPORT_FILE);
72
73    free(real_outname);
74    free(outname);
75    free(formname);
76
77}
78
79static void create_export_awars(AW_root *awr, AW_default def) {
80    {
81        GBS_strstruct path(500);
82        path.cat(GB_path_in_arbprop("filter"));
83        path.put(':');
84        path.cat(GB_path_in_ARBLIB("export"));
85
86        AW_create_fileselection_awars(awr, AWAR_EXPORT_FORM, path.get_data(), ".eft", "*");
87        AW_create_fileselection_awars(awr, AWAR_EXPORT_FILE, "",              "",     "noname");
88    }
89
90    awr->awar_string(AWAR_EXPORT_ALI, "16s", def);
91    awr->awar_int(AWAR_EXPORT_MULTIPLE_FILES, 0, def);
92
93    awr->awar_int(AWAR_EXPORT_MARKED, 1, def); // marked only
94    awr->awar_int(AWAR_EXPORT_COMPRESS, 1, def); // vertical gaps
95    awr->awar_string(AWAR_EXPORT_FILTER_NAME, "none", def); // no default filter
96    awr->awar_string(AWAR_EXPORT_FILTER_FILTER, "", def);
97    AW_awar *awar_ali = awr->awar_string(AWAR_EXPORT_FILTER_ALI, "", def);
98    awar_ali->map(AWAR_DEFAULT_ALIGNMENT);
99
100    awr->awar_int(AWAR_EXPORT_CUTSTOP, 0, def); // don't cut stop-codon
101}
102
103
104static void export_form_changed_cb(AW_root *aw_root) {
105    // called when selected export format changes
106    // -> automatically correct filename suffix
107    // -> restrict view to suffix
108
109    static char *previous_suffix = 0;
110
111    GB_ERROR  error          = 0;
112    AW_awar  *awar_form      = aw_root->awar(AWAR_EXPORT_FORM"/file_name");
113    char     *current_format = awar_form->read_string();
114
115    if (current_format) {
116        if (GB_is_regularfile(current_format)) {
117            char *current_suffix = SEQIO_exportFormat_get_outfile_default_suffix(current_format, error);
118            if (!error) {
119                // Note: current_suffix may be NULL.. is that ok?
120
121                // modify export filename and view
122
123                AW_awar *awar_filter = aw_root->awar(AWAR_EXPORT_FILE"/filter");
124                AW_awar *awar_export = aw_root->awar(AWAR_EXPORT_FILE"/file_name");
125
126                awar_filter->write_string("");
127
128                char *exportname = awar_export->read_string();
129
130                {
131                    char *path, *nameOnly, *suffix;
132                    GB_split_full_path(exportname, &path, NULL, &nameOnly, &suffix);
133
134                    if (suffix) {
135                        if (previous_suffix && ARB_stricmp(suffix, previous_suffix) == 0) freedup(suffix, current_suffix); // remove old suffix
136                        else freedup(suffix, GB_append_suffix(suffix, current_suffix)); // don't know existing suffix -> append
137                    }
138                    else suffix = strdup(current_suffix);
139
140                    const char *new_exportname = GB_concat_path(path, GB_append_suffix(nameOnly, suffix));
141                    if (new_exportname) awar_export->write_string(new_exportname);
142
143                    free(suffix);
144                    free(nameOnly);
145                    free(path);
146                }
147
148                free(exportname);
149
150                awar_filter->write_string(current_suffix);
151
152                // remember last applied suffix
153                reassign(previous_suffix, current_suffix);
154            }
155
156            free(current_suffix);
157        }
158        free(current_format);
159    }
160
161    if (error) aw_message(error);
162}
163
164AW_window *create_AWTC_export_window(AW_root *awr, GBDATA *gb_main)
165{
166    static AW_window_simple *aws = 0;
167    if (aws) return aws;
168
169    create_export_awars(awr, AW_ROOT_DEFAULT);
170
171    aws = new AW_window_simple;
172
173    aws->init(awr, "ARB_EXPORT", "ARB EXPORT");
174    aws->load_xfig("awt/export_db.fig");
175
176    aws->at("close");
177    aws->callback(AW_POPDOWN);
178    aws->create_button("CLOSE", "CLOSE", "C");
179
180    aws->at("help");
181    aws->callback(makeHelpCallback("arb_export.hlp"));
182    aws->create_button("HELP", "HELP", "H");
183
184    AW_create_fileselection(aws, AWAR_EXPORT_FILE, "f", "PWD",     ANY_DIR,    false); // select export filename
185    AW_create_fileselection(aws, AWAR_EXPORT_FORM, "",  "ARBHOME", MULTI_DIRS, false); // select export filter
186
187    aws->get_root()->awar(AWAR_EXPORT_FORM"/file_name")->add_callback(export_form_changed_cb);
188
189    aws->at("allmarked");
190    aws->create_option_menu(AWAR_EXPORT_MARKED, true);
191    aws->insert_option("all", "a", 0);
192    aws->insert_option("marked", "m", 1);
193    aws->update_option_menu();
194
195    aws->at("compress");
196    aws->create_option_menu(AWAR_EXPORT_COMPRESS, true);
197    aws->insert_option("no", "n", 0);
198    aws->insert_option("vertical gaps", "v", 1);
199    aws->insert_option("all gaps", "a", 2);
200    aws->update_option_menu();
201
202    aws->at("seqfilter");
203    adfiltercbstruct *filtercd = awt_create_select_filter(aws->get_root(), gb_main, AWAR_EXPORT_FILTER_NAME);
204    aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, filtercd));
205    aws->create_button("SELECT_FILTER", AWAR_EXPORT_FILTER_NAME);
206
207    aws->at("cutstop");
208    aws->create_toggle(AWAR_EXPORT_CUTSTOP);
209
210    aws->at("multiple");
211    aws->create_toggle(AWAR_EXPORT_MULTIPLE_FILES);
212
213    aws->at("go");
214    aws->highlight();
215    aws->callback(export_go_cb, (AW_CL)gb_main, (AW_CL)filtercd);
216    aws->create_button("GO", "GO", "G");
217
218    return aws;
219}
Note: See TracBrowser for help on using the repository browser.