ARB
NT_join.cxx
Go to the documentation of this file.
1 // =============================================================== //
2 // //
3 // File : NT_join.cxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // =============================================================== //
10 
11 #include "NT_local.h"
12 #include <item_sel_list.h>
13 #include <awt_sel_boxes.hxx>
14 #include <aw_awar.hxx>
15 #include <aw_root.hxx>
16 #include <aw_msg.hxx>
17 #include <arb_progress.h>
18 #include <arbdbt.h>
19 
20 #define AWAR_SPECIES_JOIN_FIELD "/tmp/NT/species_join/field"
21 #define AWAR_SPECIES_JOIN_SEP "/tmp/NT/species_join/separator"
22 #define AWAR_SPECIES_JOIN_SEP2 "/tmp/NT/species_join/separator_sequences"
23 
24 static GB_ERROR nt_species_join(GBDATA *dest, GBDATA *source, int deep, char *sep, char *sep2) {
25  GB_TYPES dtype = GB_read_type(dest);
26  GB_TYPES stype = GB_read_type(source);
27  if (dtype != stype) return NULp;
28 
30 
31  switch (dtype) {
32  case GB_DB:
33  for (GBDATA *gb_source_field = GB_child(source);
34  !error && gb_source_field;
35  gb_source_field = GB_nextChild(gb_source_field))
36  {
37  const char *source_field = GB_read_key_pntr(gb_source_field);
38  if (!strcmp(source_field, "name")) continue;
39 
40  GBDATA *gb_dest_field = GB_entry(dest, source_field);
41  if (gb_dest_field) { // if destination exists -> recurse
42  error = nt_species_join(gb_dest_field, gb_source_field, 0, sep, sep2);
43  }
44  else {
45  GB_TYPES type = GB_read_type(gb_source_field);
46 
47  if (type == GB_DB) {
48  GBDATA *gb_dest_container = GB_create_container(dest, source_field);
49  if (!gb_dest_container) error = GB_await_error();
50  else error = nt_species_join(gb_dest_container, gb_source_field, 0, sep, sep2);
51  }
52  else {
53  gb_dest_field = GB_create(dest, source_field, GB_read_type(gb_source_field));
54  if (!gb_dest_field) error = GB_await_error();
55  else error = GB_copy_dropProtectMarksAndTempstate(gb_dest_field, gb_source_field);
56  }
57  }
58  }
59  break;
60 
61  case GB_STRING: {
62  char *sf = GB_read_string(source);
63  char *df = GB_read_string(dest);
64  if (strcmp(sf, df) != 0) {
65  char *s = sep;
66  const char *spacers = " ";
67  if (deep) {
68  s = sep2;
69  spacers = ".- ";
70  }
71  int i;
72  // remove trailing spacers;
73  for (i = strlen(df)-1; i>=0; i--) {
74  if (strchr(spacers, df[i])) df[i] = 0;
75  }
76  // remove leading spacers
77  int end = strlen(sf);
78  for (i=0; i<end; i++) {
79  if (!strchr(spacers, sf[i])) break;
80  }
81  char *str = new char [strlen(sf) + strlen(df) + strlen(s) + 1];
82  sprintf(str, "%s%s%s", df, s, sf+i);
83  error = GB_write_string(dest, str);
84  delete [] str;
85  }
86  free(sf);
87  free(df);
88  break;
89  }
90  default: {
91  break;
92  }
93  }
94  return error;
95 }
96 
97 static void species_rename_join(AW_window *aww) {
98  char *field = aww->get_root()->awar(AWAR_SPECIES_JOIN_FIELD)->read_string();
99  char *sep = aww->get_root()->awar(AWAR_SPECIES_JOIN_SEP)->read_string();
100  char *sep2 = aww->get_root()->awar(AWAR_SPECIES_JOIN_SEP2)->read_string();
102 
103  if (!error) {
106 
107  arb_progress progress("Joining species", maxs);
108 
109  GBDATA *gb_next = NULp;
110  for (GBDATA *gb_species = GBT_first_marked_species(GLOBAL.gb_main);
111  gb_species && !error;
112  gb_species = gb_next)
113  {
114  gb_next = GBT_next_marked_species(gb_species);
115 
116  GBDATA *gb_field = GB_entry(gb_species, field);
117  if (gb_field) {
118  char *field_value = GB_read_as_string(gb_field);
119  GBDATA *gb_old = (GBDATA *)GBS_read_hash(hash, field_value);
120 
121  if (!gb_old) {
122  GBS_write_hash(hash, field_value, (long)gb_species);
123  }
124  else {
125  error = nt_species_join(gb_old, gb_species, 0, sep, sep2);
126  if (!error) error = GB_delete(gb_species);
127  }
128  free(field_value);
129  }
130  progress.inc_and_check_user_abort(error);
131  }
132 
133  GBS_free_hash(hash);
134  }
136 
137  free(sep2);
138  free(sep);
139  free(field);
140 }
141 
142 
144  static AW_window_simple *aws = NULp;
145  if (!aws) {
149 
150  aws = new AW_window_simple;
151  aws->init(root, "SPECIES_JOIN", "JOIN SPECIES");
152  aws->load_xfig("join_species.fig");
153 
154  aws->at("close");
155  aws->callback(AW_POPDOWN);
156  aws->create_button("CLOSE", "CLOSE", "C");
157 
158  aws->at("help"); aws->callback(makeHelpCallback("species_join.hlp"));
159  aws->create_button("HELP", "HELP", "H");
160 
161  aws->at("sym");
162  aws->create_input_field(AWAR_SPECIES_JOIN_SEP);
163 
164  aws->at("symseq");
165  aws->create_input_field(AWAR_SPECIES_JOIN_SEP2);
166 
167  aws->at("go");
168  aws->callback(species_rename_join);
169  aws->help_text("species_join.hlp");
170  aws->create_button("GO", "GO", "G");
171 
173  }
174  return aws;
175 }
GB_ERROR GB_begin_transaction(GBDATA *gbd)
Definition: arbdb.cxx:2528
GB_ERROR GB_copy_dropProtectMarksAndTempstate(GBDATA *dest, GBDATA *source)
Definition: arbdb.cxx:2152
const char * GB_ERROR
Definition: arb_core.h:25
GB_TYPES type
GBDATA * GBT_first_marked_species(GBDATA *gb_main)
Definition: aditem.cxx:113
static void species_rename_join(AW_window *aww)
Definition: NT_join.cxx:97
GBDATA * GB_child(GBDATA *father)
Definition: adquery.cxx:322
long GBS_write_hash(GB_HASH *hs, const char *key, long val)
Definition: adhash.cxx:454
GB_ERROR GB_write_string(GBDATA *gbd, const char *s)
Definition: arbdb.cxx:1387
void GB_end_transaction_show_error(GBDATA *gbd, GB_ERROR error, void(*error_handler)(GB_ERROR))
Definition: arbdb.cxx:2584
char * GB_read_as_string(GBDATA *gbd)
Definition: arbdb.cxx:1060
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
void GBS_free_hash(GB_HASH *hs)
Definition: adhash.cxx:538
GB_ERROR GB_delete(GBDATA *&source)
Definition: arbdb.cxx:1916
void create_itemfield_selection_button(AW_window *aws, const FieldSelDef &selDef, const char *at)
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
GBDATA * GB_create_container(GBDATA *father, const char *key)
Definition: arbdb.cxx:1829
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
Definition: arbdb.h:78
GB_TYPES GB_read_type(GBDATA *gbd)
Definition: arbdb.cxx:1643
#define AWAR_SPECIES_JOIN_FIELD
Definition: NT_join.cxx:20
GB_CSTR GB_read_key_pntr(GBDATA *gbd)
Definition: arbdb.cxx:1656
GBDATA * GB_create(GBDATA *father, const char *key, GB_TYPES type)
Definition: arbdb.cxx:1781
#define AWAR_SPECIES_JOIN_SEP
Definition: NT_join.cxx:21
static GB_ERROR nt_species_join(GBDATA *dest, GBDATA *source, int deep, char *sep, char *sep2)
Definition: NT_join.cxx:24
static void error(const char *msg)
Definition: mkptypes.cxx:96
GBDATA * GBT_next_marked_species(GBDATA *gb_species)
Definition: aditem.cxx:116
char * str
Definition: defines.h:20
char * read_string() const
Definition: AW_awar.cxx:198
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
long GBT_count_marked_species(GBDATA *gb_main)
Definition: aditem.cxx:372
AW_window * NT_create_species_join_window(AW_root *root)
Definition: NT_join.cxx:143
char * GB_read_string(GBDATA *gbd)
Definition: arbdb.cxx:909
ItemSelector & SPECIES_get_selector()
Definition: species.cxx:139
void aw_message(const char *msg)
Definition: AW_status.cxx:1142
AW_root * get_root()
Definition: aw_window.hxx:359
#define NULp
Definition: cxxforward.h:116
GBDATA * gb_main
Definition: NT_local.h:37
GB_TYPES
Definition: arbdb.h:62
GBDATA * GB_nextChild(GBDATA *child)
Definition: adquery.cxx:326
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
#define AW_ROOT_DEFAULT
Definition: aw_base.hxx:106
long GBS_read_hash(const GB_HASH *hs, const char *key)
Definition: adhash.cxx:392
GBDATA * GB_entry(GBDATA *father, const char *key)
Definition: adquery.cxx:334
void inc_and_check_user_abort(GB_ERROR &error)
Definition: arb_progress.h:332
CONSTEXPR long FIELD_FILTER_STRING_READABLE
Definition: item_sel_list.h:47
NT_global GLOBAL
Definition: NT_main.cxx:46
GB_HASH * GBS_create_hash(long estimated_elements, GB_CASE case_sens)
Definition: adhash.cxx:253
#define AWAR_SPECIES_JOIN_SEP2
Definition: NT_join.cxx:22
GB_write_int const char s
Definition: AW_awar.cxx:154