source: tags/ms_r16q2/ARBDB/adChangeKey.cxx

Last change on this file was 14786, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : adChangeKey.cxx                                   //
4//   Purpose   : Changekey management                              //
5//                                                                 //
6//   Coded by Elmar Pruesse and Ralf Westram in May 2009           //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include <arbdbt.h>
13#include <arb_global_defs.h>
14#include "gb_local.h"
15
16GBDATA *GBT_get_changekey(GBDATA *gb_main, const char *key, const char *change_key_path) {
17    // get the container of an item key description
18#if defined(WARN_TODO)
19#warning check if search for CHANGEKEY_NAME should be case-sensitive!
20#endif
21    GBDATA *gb_key      = 0;
22    GBDATA *gb_key_data = GB_search(gb_main, change_key_path,
23                                    GB_CREATE_CONTAINER);
24
25    if (gb_key_data) {
26        GBDATA *gb_key_name = GB_find_string(gb_key_data, CHANGEKEY_NAME, key, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
27        if (gb_key_name) {
28            gb_key = GB_get_father(gb_key_name);
29        }
30    }
31    return gb_key;
32}
33
34GB_TYPES GBT_get_type_of_changekey(GBDATA *gb_main, const char *field_name, const char *change_key_path) {
35    // get the type of an item key
36    GB_TYPES  type = GB_NONE;
37    GBDATA   *gbd  = GBT_get_changekey(gb_main, field_name, change_key_path);
38
39    if (gbd) {
40        long *typePtr     = GBT_read_int(gbd, CHANGEKEY_TYPE);
41        if (typePtr) {
42            type = (GB_TYPES)*typePtr;
43        }
44    }
45
46    return type;
47}
48
49static GB_ERROR gbt_set_type_of_changekey(GBDATA *gb_main, const char *field_name, GB_TYPES type, const char *change_key_path) {
50    GB_ERROR  error = NULL;
51    GBDATA   *gbd   = GBT_get_changekey(gb_main, field_name, change_key_path);
52
53    if (!gbd) {
54        error = GBS_global_string("Can't set type of nonexistent changekey \"%s\"", field_name);
55    }
56    else {
57        error = GBT_write_int(gbd, CHANGEKEY_TYPE, type);
58    }
59    return error;
60}
61
62GBDATA *GBT_searchOrCreate_itemfield_according_to_changekey(GBDATA *gb_item, const char *field_name, const char *change_key_path) {
63    /*! search or create an item entry.
64     * If the entry exists, the result is identical to GB_search(gb_item, field_name, GB_FIND).
65     * If the entry does not exist, an entry with the type stored in the changekey-table will be created.
66     * @return created itemfield or NULL in case of error (which is exported in that case)
67     */
68
69    gb_assert(!GB_have_error());
70    GBDATA *gb_entry = GB_search(gb_item, field_name, GB_FIND);
71    if (!gb_entry) {
72        GB_clear_error();
73
74        GB_TYPES type = GBT_get_type_of_changekey(GB_get_root(gb_item), field_name, change_key_path);
75        if (type == GB_NONE) {
76            GB_export_errorf("Cannot create field '%s' (no type information available)", field_name);
77        }
78        else {
79            gb_entry = GB_search(gb_item, field_name, type);
80        }
81    }
82    gb_assert(gb_entry || GB_have_error());
83    return gb_entry;
84}
85
86
87GB_ERROR GBT_add_new_changekey_to_keypath(GBDATA *gb_main, const char *name, int type, const char *keypath) {
88    GB_ERROR    error  = NULL;
89    GBDATA     *gb_key = GBT_get_changekey(gb_main, name, keypath);
90    const char *c      = GB_first_non_key_char(name);
91
92    if (c) {
93        char *new_name = strdup(name);
94
95        *(char*)GB_first_non_key_char(new_name) = 0;
96
97        if      (*c == '/') error = GBT_add_new_changekey(gb_main, new_name, GB_DB);
98        else if (*c == '-') error = GBT_add_new_changekey(gb_main, new_name, GB_LINK);
99        else               error = GBS_global_string("Cannot add '%s' to your key list (illegal character '%c')", name, *c);
100
101        free(new_name);
102    }
103
104    if (!error) {
105        if (!gb_key) {          // create new key
106            GBDATA *gb_key_data = GB_search(gb_main, keypath, GB_CREATE_CONTAINER);
107            gb_key              = gb_key_data ? GB_create_container(gb_key_data, CHANGEKEY) : 0;
108
109            if (!gb_key) error = GB_await_error();
110            else {
111                error             = GBT_write_string(gb_key, CHANGEKEY_NAME, name);
112                if (!error) error = GBT_write_int(gb_key, CHANGEKEY_TYPE, type);
113            }
114        }
115        else {                  // check type of existing key
116            long *elem_type = GBT_read_int(gb_key, CHANGEKEY_TYPE);
117
118            if (!elem_type)              error = GB_await_error();
119            else if (*elem_type != type) error = GBS_global_string("Key '%s' exists, but has different type", name);
120        }
121    }
122
123    gb_assert(gb_key || error);
124
125    return error;
126}
127
128GB_ERROR GBT_add_new_changekey(GBDATA *gb_main, const char *name, int type) {
129    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH);
130}
131
132GB_ERROR GBT_add_new_gene_changekey(GBDATA *gb_main, const char *name, int type) {
133    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_GENES);
134}
135
136GB_ERROR GBT_add_new_experiment_changekey(GBDATA *gb_main, const char *name, int type) {
137    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_EXPERIMENTS);
138}
139
140static GB_ERROR write_as_int(GBDATA *gbfield, const char *data, bool trimmed, size_t *rounded) {
141    char          *end   = 0;
142    unsigned long  i     = strtoul(data, &end, 10);
143    GB_ERROR       error = NULL;
144
145    if (end == data || end[0] != 0) {
146        if (trimmed) {
147            // fallback: convert to double and round
148
149            double d = strtod(data, &end);
150            if (end == data || end[0] != 0) {
151                error = GBS_global_string("cannot convert '%s' to rounded numeric value", data);
152            }
153            else {
154                (*rounded)++;
155                i                = d>0 ? (int)(d+0.5) : (int)(d-0.5);
156                error            = GB_write_int(gbfield, i);
157                if (error) error = GBS_global_string("write error (%s)", error);
158            }
159        }
160        else {
161            char *trimmed_data = GBS_trim(data);
162            error              = write_as_int(gbfield, trimmed_data, true, rounded);
163            free(trimmed_data);
164        }
165    }
166    else {
167        error = GB_write_int(gbfield, i);
168        if (error) error = GBS_global_string("write error (%s)", error);
169    }
170
171    return error;
172}
173
174static GB_ERROR write_as_float(GBDATA *gbfield, const char *data, bool trimmed) {
175    char     *end   = 0;
176    float     f     = strtof(data, &end);
177    GB_ERROR  error = NULL;
178
179    if (end == data || end[0] != 0) {
180        if (trimmed) {
181            error = GBS_global_string("cannot convert '%s' to numeric value", data);
182        }
183        else {
184            char *trimmed_data = GBS_trim(data);
185            error              = write_as_float(gbfield, trimmed_data, true);
186            free(trimmed_data);
187        }
188    }
189    else {
190        error = GB_write_float(gbfield, f);
191        if (error) error = GBS_global_string("write error (%s)", error);
192    }
193
194    return error;
195}
196
197
198GB_ERROR GBT_convert_changekey(GBDATA *gb_main, const char *name, GB_TYPES target_type) {
199    GB_ERROR error        = GB_push_transaction(gb_main);
200    bool     need_convert = true;
201
202    if (!error) {
203        GBDATA *gbkey = GBT_get_changekey(gb_main, name, CHANGE_KEY_PATH);
204        if (gbkey) {
205            GB_TYPES source_type = (GB_TYPES)*GBT_read_int(gbkey, CHANGEKEY_TYPE);
206            if (source_type == target_type) need_convert = false;
207        }
208        else {
209            if (!name[0] || strcmp(name, NO_FIELD_SELECTED) == 0) {
210                error = "Please select field to convert";
211            }
212            else {
213                error = GBS_global_string("Unknown changekey '%s'", name);
214            }
215        }
216    }
217
218    if (!error && need_convert) {
219        GBDATA *gbspec  = GBT_first_species(gb_main);
220        size_t  rounded = 0;
221
222        for (; gbspec; gbspec = GBT_next_species(gbspec)) {
223            GBDATA *gbfield = GB_entry(gbspec, name);
224
225            // If entry does not exist, no need to convert (sparse population is valid => 'NULL' value)
226            if (gbfield) {
227                char *data = GB_read_as_string(gbfield);
228                if (!data) {
229                    error = GBS_global_string("read error (%s)", GB_await_error());
230                }
231                else {
232                    error = GB_delete(gbfield);
233                    if (!error) {
234                        gbfield = GB_create(gbspec, name, target_type);
235                        if (!gbfield) {
236                            error = GBS_global_string("create error (%s)", GB_await_error());
237                        }
238                        else {
239                            switch (target_type) {
240                                case GB_INT:
241                                    error = write_as_int(gbfield, data, false, &rounded);
242                                    break;
243
244                                case GB_FLOAT:
245                                    error = write_as_float(gbfield, data, false);
246                                    break;
247
248                                case GB_STRING:
249                                    error = GB_write_string(gbfield, data);
250                                    if (error) error = GBS_global_string("write error (%s)", error);
251                                    break;
252
253                                default:
254                                    error = "Conversion is not possible";
255                                    break;
256                            }
257                        }
258                    }
259                    free(data);
260                }
261            }
262            if (error) break;
263        }
264
265        if (error && gbspec) {
266            const char *spname = GBT_read_name(gbspec);
267            error              = GBS_global_string("%s for species '%s'", error, spname);
268        }
269
270        if (!error) error = gbt_set_type_of_changekey(gb_main, name, target_type, CHANGE_KEY_PATH);
271        if (!error && rounded>0) {
272            GB_warningf("%zi values were rounded (loss of precision)", rounded);
273        }
274    }
275
276    if (error) error  = GBS_global_string("GBT_convert: %s", error);
277
278    return GB_end_transaction(gb_main, error);
279}
280
Note: See TracBrowser for help on using the repository browser.