ARB
AWTI_export.cxx
Go to the documentation of this file.
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 #include "awti_exp_local.hxx"
13 #include "awti_edit.hxx"
14 
15 #include <xfergui.h>
16 #include <FileWatch.h>
17 #include <seqio.hxx>
18 #include <AP_filter.hxx>
19 #include <db_scanner.hxx>
20 
21 #include <aw_awars.hxx>
22 #include <aw_file.hxx>
23 #include <aw_msg.hxx>
24 
25 #include <arbdbt.h>
26 #include <gb_aci.h>
27 
28 #include <arb_progress.h>
29 #include <arb_strbuf.h>
30 #include <arb_stdstring.h>
31 
32 #include <set>
33 #include <string>
34 
35 using namespace SEQIO;
36 
39 
40  GB_transaction ta(gb_main);
41  arb_progress progress("Exporting data");
42 
43  AW_root *awr = aww->get_root();
44 
45  int multiple = awr->awar(AWAR_EXPORT_MULTIPLE_FILES)->read_int();
46  int cut_stop_codon = awr->awar(AWAR_EXPORT_CUTSTOP)->read_int();
47  int compress = awr->awar(AWAR_EXPORT_COMPRESS)->read_int();
48 
50  AP_filter *filter = awt_get_filter(acbs);
51  const char *ftsname = XFER_getFullFTS(awr->awar(AWAR_EXPORT_FTS)->read_char_pntr());
52 
54  char *outname = awr->awar(AWAR_EXPORT_FILENAME)->read_string();
55  char *db_name = awr->awar(AWAR_DB_NAME)->read_string();
56 
57  char *real_outname = NULp; // with suffix (name of first file if multiple)
58 
59  GB_ERROR error = export_by_format(gb_main, which, NULp,
60  filter, cut_stop_codon, compress,
61  db_name, formname, ftsname,
62  outname, multiple, &real_outname);
63 
64  if (error) aw_message(error);
65  if (real_outname) awr->awar(AWAR_EXPORT_FILENAME)->write_string(real_outname);
66 
68 
69  free(db_name);
70  free(real_outname);
71  free(outname);
72  free(formname);
73 }
74 
75 static void avoid_empty_target_name(char*& nameOnly) {
76  static char *last_used_name = ARB_strdup("noname");
77  if (nameOnly) freedup(last_used_name, nameOnly);
78  else nameOnly = ARB_strdup(last_used_name); // if name was lost (by changing directory) -> reuse last_used_name
79 }
80 
81 static bool in_export_filename_changed_cb = false;
82 static void export_filename_changed_cb(AW_root *aw_root) {
85 
86  AW_awar *awar_export = aw_root->awar(AWAR_EXPORT_FILENAME);
87  char *exportname = awar_export->read_string();
88 
89  if (!GB_is_directory(exportname)) {
90  char *path, *nameOnly, *suffix;
91  GB_split_full_path(exportname, &path, NULp, &nameOnly, &suffix);
92 
93  avoid_empty_target_name(nameOnly);
94 
95  const char *new_exportname = GB_concat_path(path, GB_append_suffix(nameOnly, suffix));
96  if (new_exportname) awar_export->write_string(new_exportname);
97  }
98  free(exportname);
99  }
100 }
101 
102 static void create_export_awars(AW_root *awr) {
103  {
104  GBS_strstruct path(500);
105  path.cat(GB_path_in_arbprop("filter"));
106  path.put(':');
107  path.cat(GB_path_in_ARBLIB("export"));
108 
110  AW_create_fileselection_awars(awr, AWAR_EXPORT_FILEBASE, "", "", "noname");
111  }
112 
113  awr->awar_string(AWAR_EXPORT_FORMAT_DESC, "<no format selected>");
115 
118  awr->awar_int(AWAR_EXPORT_COMPRESS, 1); // vertical gaps
119  awr->awar_int(AWAR_EXPORT_CUTSTOP); // don't cut stop-codon
120 
122 
124 }
125 
126 static void update_format_description_and_suffix(const char *eft) {
127  // called when export format has changed (filter content or selected filter)
128  // -> automatically correct filename suffix
129  // -> restrict view to suffix
130  // -> update description
131 
132  ExportFormatInfo eft_info;
134 
135  AW_root *aw_root = AW_root::SINGLETON;
136  AW_awar *awar_desc = aw_root->awar(AWAR_EXPORT_FORMAT_DESC);
137 
138  if (error) awar_desc->write_string(error); // display format-errors in description field
139  else {
140  const char *current_suffix = eft_info.suffix.content(); // Note: current_suffix may be NULp.. is that ok?
141  static char *previous_suffix = NULp;
142 
143  // modify export filename and view
144  AW_awar *awar_filter = aw_root->awar(AWAR_EXPORT_FILESUFFIX);
145  AW_awar *awar_export = aw_root->awar(AWAR_EXPORT_FILENAME);
146 
147  awar_filter->write_string("");
148 
149  char *exportname = awar_export->read_string();
150 
151  {
153 
154  char *path, *nameOnly, *suffix;
155  GB_split_full_path(exportname, &path, NULp, &nameOnly, &suffix);
156 
157  avoid_empty_target_name(nameOnly);
158 
159  if (suffix) {
160  if (previous_suffix && ARB_stricmp(suffix, previous_suffix) == 0) freedup(suffix, current_suffix); // remove old suffix
161  else freedup(suffix, GB_append_suffix(suffix, current_suffix)); // don't know existing suffix -> append
162  }
163  else suffix = ARB_strdup(current_suffix);
164 
165  const char *new_exportname = GB_concat_path(path, GB_append_suffix(nameOnly, suffix));
166  if (new_exportname) awar_export->write_string(new_exportname);
167 
168  free(suffix);
169  free(nameOnly);
170  free(path);
171  }
172 
173  free(exportname);
174 
175  awar_filter->write_string(current_suffix);
176 
177  // remember last applied suffix
178  freedup(previous_suffix, current_suffix);
179 
180  // update description
181  if (eft_info.description.isSet()) awar_desc->write_string(&*eft_info.description);
182  else awar_desc->write_string("<no description>");
183  }
184 }
185 
186 class ExportFieldTracker : public FieldTracker, virtual Noncopyable {
187  typedef std::set<std::string> StrSet;
188 
189  StrSet tracked; // fields
190 
191 public:
193  void track_field(const char *fieldname) OVERRIDE {
194  tracked.insert(fieldname);
195  }
196 
197  void add_tracked_to(StrArray& addTo) const {
198  for (StrSet::const_iterator elem = tracked.begin(); elem != tracked.end(); ++elem) {
199  char *copy = ARB_stringdup(*elem);
200  addTo.put(copy);
201  }
202  }
203 };
204 
207 
208  static const char* fake_exported_sequence(GBDATA*, unsigned long *len, const char**) {
209  *len = 5;
210  return "faked";
211  }
212 
213 public:
214  ExportFieldScanner() : gb_main(NULp) {}
215 
216  void scanFields(StrArray& fields, FieldsToScan whatToScan) const OVERRIDE {
217  if (whatToScan & SCAN_INPUT_FIELDS) {
218  if (gb_main) {
219  collectKeysRegisteredInDatabase(fields, gb_main, SPECIES_get_selector(), true, false);
220  }
221  else {
222  fields.put(ARB_strdup("<no known source database>"));
223  }
224  }
225 
226  if (whatToScan & SCAN_OUTPUT_FIELDS) {
228  if (eft[0]) {
229  GB_ERROR scan_error = NULp;
230  {
231  char *eftName;
232  GB_split_full_path(eft, NULp, &eftName, NULp, NULp);
233  free(eftName);
234  }
235 
236  if (gb_main) {
237  GB_transaction ta(gb_main);
238 
239  ItemSelector& itemtype = SPECIES_get_selector();
240  GBDATA *gb_someSpecies = itemtype.get_selected_item(gb_main, AW_root::SINGLETON);
241  if (!gb_someSpecies) gb_someSpecies = MutableBoundItemSel(gb_main, itemtype).get_any_item();
242 
243  if (gb_someSpecies) {
244  GB_ERROR error = NULp;
245  char *form = get_exportFormat_evalForm(eft, error);
246 
247  if (!error) {
248  if (form) {
249  // create 'callEnv' to track fields:
250  ExportFieldTracker tracker;
251  GBL_env env(gb_main, "tree_faked");
252  {
253  GBL_call_env callEnv(gb_someSpecies, env);
254 
255  callEnv.useFieldTracker(&tracker);
256  GB_set_export_sequence_hook(fake_exported_sequence);
257 
258  // evaluate form (causes fields to become tracked by 'tracker')
259  {
260  char *result = GBS_string_eval_in_env(" ", form, callEnv);
261  awti_assert(!GB_have_error()); // if eval reports error -> tracker did not see fields behind error-command
262  free(result);
263  }
264 
266  }
267  // collect tracked fields:
268  tracker.add_tracked_to(fields);
269  }
270  else {
271  scan_error = "unknown form access failure";
272  }
273  }
274  else {
275  scan_error = error;
276  }
277  free(form);
278  }
279  else {
280  scan_error = "no species";
281  }
282  }
283  else {
284  scan_error = "no database";
285  }
286 
287  if (scan_error) {
288  fields.put(GBS_global_string_copy("<scan filter: %s>", scan_error));
289  }
290  }
291  else {
292  fields.put(ARB_strdup("<no export filter selected>"));
293  }
294  free(eft);
295  }
296  }
297 
298  void announce_source_database(GBDATA *gb_src) { gb_main = gb_src; }
299 };
300 
302 
304  static AW_window_simple *aws = NULp;
305 
306  if (!aws) {
307  create_export_awars(awr);
308 
309  aws = new AW_window_simple;
310 
311  aws->init(awr, "ARB_EXPORT", "ARB EXPORT");
312  aws->load_xfig("awt/export_db.fig");
313 
314  aws->at("close");
315  aws->callback(AW_POPDOWN);
316  aws->create_button("CLOSE", "CLOSE", "C");
317 
318  aws->at("help");
319  aws->callback(makeHelpCallback("arb_export.hlp"));
320  aws->create_button("HELP", "HELP", "H");
321 
322  AW_create_fileselection(aws, AWAR_EXPORT_FILEBASE, "f", "PWD", ANY_DIR, false); // select export filename
323  AW_create_fileselection(aws, AWAR_EXPORT_FORMATBASE, "", "ARBHOME", MULTI_DIRS, false); // select export filter
324 
325  aws->at("allmarked");
326  aws->create_option_menu(AWAR_EXPORT_MARKED);
327  aws->insert_option("all", "a", EBF_ALL);
328  aws->insert_option("marked", "m", EBF_MARKED);
329  aws->update_option_menu();
330 
331  aws->at("compress");
332  aws->create_option_menu(AWAR_EXPORT_COMPRESS);
333  aws->insert_option("no", "n", 0);
334  aws->insert_option("vertical gaps", "v", 1);
335  aws->insert_option("all gaps", "a", 2);
336  aws->update_option_menu();
337 
338  aws->at("seqfilter");
340  aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, filtercd));
341  aws->create_button("SELECT_FILTER", AWAR_EXPORT_FILTER_NAME);
342 
343  aws->at("cutstop");
344  aws->create_toggle(AWAR_EXPORT_CUTSTOP);
345 
346  aws->at("multiple");
347  aws->create_toggle(AWAR_EXPORT_MULTIPLE_FILES);
348 
349  aws->at("desc");
350  aws->create_text_field(AWAR_EXPORT_FORMAT_DESC);
351 
352  aws->at("go");
353  aws->highlight();
354  aws->callback(makeWindowCallback(export_go_cb, gb_main, filtercd));
355  aws->create_button("GO", "GO", "G", "+");
356 
357  aws->at("test");
358  aws->callback(makeWindowCallback(AWTI_activate_export_test_window, gb_main, filtercd));
359  aws->create_button("TEST", "Test", "T");
360 
361  aws->at("fts");
362  aws->callback(makeWindowCallback(XFER_select_RuleSet, AWAR_EXPORT_FTS, static_cast<AvailableFieldScanner*>(&fieldScanner)));
363  aws->create_button("SELECT_FTS", AWAR_EXPORT_FTS);
364 
365  static FileWatch fwatch(AWAR_EXPORT_FORMATNAME, makeFileChangedCallback(update_format_description_and_suffix));
366  awr->awar(AWAR_EXPORT_FORMATNAME)->add_callback(makeRootCallback(XFER_refresh_available_fields, static_cast<AvailableFieldScanner*>(&fieldScanner), SCAN_OUTPUT_FIELDS));
367  }
368 
369  fieldScanner.announce_source_database(gb_main);
370 
371  return aws;
372 }
ExportWhich
Definition: seqio.hxx:28
void awt_create_filter_awars(AW_root *aw_root, AW_default aw_def, const char *awar_filtername, const char *awar_mapto_alignment)
Definition: AWT_filter.cxx:194
static void create_export_awars(AW_root *awr)
#define AWAR_DB_NAME
Definition: aw_awar_defs.hxx:6
string result
set< string, lt_field > StrSet
Definition: xfergui.cxx:84
void useFieldTracker(FieldTracker *tracker_)
Definition: gb_aci.h:163
GB_CSTR GB_path_in_arbprop(const char *relative_path)
Definition: adsocket.cxx:1166
void XFER_select_RuleSet(AW_window *aww, const char *awar_selected_fts, const AvailableFieldScanner *fieldScanner)
Definition: xfergui.cxx:1204
void scanFields(StrArray &fields, FieldsToScan whatToScan) const OVERRIDE
char * GBS_string_eval_in_env(const char *insource, const char *icommand, const GBL_call_env &callEnv)
Definition: admatch.cxx:493
AW_window * create_AWTC_export_window(AW_root *awr, GBDATA *gb_main)
GBDATA * get_any_item() const
Definition: items.cxx:14
NOT4PERL void GB_set_export_sequence_hook(gb_export_sequence_cb escb)
Definition: adlang1.cxx:35
#define AWAR_EXPORT_COMPRESS
int ARB_stricmp(const char *s1, const char *s2)
Definition: arb_str.h:28
char * ARB_strdup(const char *str)
Definition: arb_string.h:27
#define AWAR_DEFAULT_ALIGNMENT
Definition: aw_awar_defs.hxx:8
long read_int() const
Definition: AW_awar.cxx:184
static ExportFieldScanner fieldScanner
#define AWAR_EXPORT_MULTIPLE_FILES
bool GB_have_error()
Definition: arb_msg.cxx:338
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
char * ARB_stringdup(const std::string &str)
Definition: arb_stdstring.h:24
void cat(const char *from)
Definition: arb_strbuf.h:199
SmartCharPtr description
Definition: seqio.hxx:54
AW_awar * add_callback(const RootCallback &cb)
Definition: AW_awar.cxx:231
#define AWAR_EXPORT_FILENAME
const char * read_char_pntr() const
Definition: AW_awar.cxx:168
static AW_root * SINGLETON
Definition: aw_root.hxx:102
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
#define AWAR_EXPORT_FILEBASE
void announce_source_database(GBDATA *gb_src)
#define AWAR_EXPORT_FILESUFFIX
GBDATA *(* get_selected_item)(GBDATA *gb_main, AW_root *aw_root)
Definition: items.h:76
static void update_format_description_and_suffix(const char *eft)
static void error(const char *msg)
Definition: mkptypes.cxx:96
void AW_create_fileselection_awars(AW_root *awr, const char *awar_base, const char *directories, const char *filter, const char *file_name)
Definition: AW_file.cxx:72
#define AWAR_EXPORT_FORMATBASE
FieldsToScan
Definition: xfergui.h:21
GB_ERROR get_exportFormat_information(const char *eft_formname, ExportFormatInfo &info)
Definition: seq_export.cxx:812
adfiltercbstruct * awt_create_select_filter(AW_root *aw_root, GBDATA *gb_main, const char *def_name)
Definition: AWT_filter.cxx:225
GB_ERROR export_by_format(GBDATA *gb_main, ExportWhich which, const char *one_species, AP_filter *filter, int cut_stop_codon, int compress, const char *dbname, const char *formname, const char *field_transfer_set, const char *outname, int multiple, char **real_outname)
Definition: seq_export.cxx:763
#define AWAR_EXPORT_FORMATNAME
AW_window * awt_create_select_filter_win(AW_root *aw_root, adfiltercbstruct *acbs)
Definition: AWT_filter.cxx:365
char * read_string() const
Definition: AW_awar.cxx:198
GB_CSTR GB_path_in_ARBLIB(const char *relative_path)
Definition: adsocket.cxx:1156
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
const char * XFER_getFullFTS(const char *name)
Definition: xfergui.cxx:96
void AW_refresh_fileselection(AW_root *awr, const char *awar_prefix)
Definition: AW_file.cxx:944
void track_field(const char *fieldname) OVERRIDE
GB_CSTR GB_append_suffix(const char *name, const char *suffix)
Definition: adsocket.cxx:984
AP_filter * awt_get_filter(adfiltercbstruct *acbs)
Definition: AWT_filter.cxx:431
GB_CSTR GB_concat_path(GB_CSTR anypath_left, GB_CSTR anypath_right)
Definition: adsocket.cxx:1069
static void copy(double **i, double **j)
Definition: trnsprob.cxx:32
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:580
SmartCharPtr suffix
Definition: seqio.hxx:53
static void export_go_cb(AW_window *aww, GBDATA *gb_main, adfiltercbstruct *acbs)
Definition: AWTI_export.cxx:37
#define AWAR_EXPORT_FILTER_NAME
static void export_filename_changed_cb(AW_root *aw_root)
Definition: AWTI_export.cxx:82
#define awti_assert(cond)
bool GB_is_directory(const char *path)
Definition: arb_file.cxx:176
#define OVERRIDE
Definition: cxxforward.h:112
const struct formatTable formname[]
ItemSelector & SPECIES_get_selector()
Definition: species.cxx:139
char * get_exportFormat_evalForm(const char *eft_formname, GB_ERROR &error)
Definition: seq_export.cxx:830
void aw_message(const char *msg)
Definition: AW_status.cxx:1142
AW_root * get_root()
Definition: aw_window.hxx:359
void GB_split_full_path(const char *fullpath, char **res_dir, char **res_fullname, char **res_name_only, char **res_suffix)
Definition: adsocket.cxx:1268
#define NULp
Definition: cxxforward.h:116
#define AWAR_EXPORT_FORMAT_DESC
static bool in_export_filename_changed_cb
Definition: AWTI_export.cxx:81
#define AWAR_EXPORT_MARKED
void add_tracked_to(StrArray &addTo) const
void AWTI_activate_export_test_window(AW_window *awp, GBDATA *gb_main, adfiltercbstruct *acbs)
Definition: AWTI_edit.cxx:510
void AW_create_fileselection(AW_window *aws, const char *awar_prefix, const char *at_prefix, const char *pwd, DirDisplay disp_dirs, bool allow_wildcards)
Definition: AW_file.cxx:870
GB_ERROR write_string(const char *aw_string)
#define AWAR_EXPORT_FTS
static void avoid_empty_target_name(char *&nameOnly)
Definition: AWTI_export.cxx:75
const char * get_data() const
Definition: arb_strbuf.h:120
GB_transaction ta(gb_var)
#define AWAR_EXPORT_CUTSTOP
GBDATA * gb_main
Definition: adname.cxx:32
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
void XFER_refresh_available_fields(AW_root *awr, const AvailableFieldScanner *fieldScanner, FieldsToScan whatToScan)
Definition: xfergui.cxx:531
#define AW_ROOT_DEFAULT
Definition: aw_base.hxx:106
void collectKeysRegisteredInDatabase(StrArray &fields, GBDATA *gb_main, ItemSelector &sel, bool skipContainers, bool skipHidden)
Definition: db_scanner.cxx:520
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:194
void put(char c)
Definition: arb_strbuf.h:174