source: tags/arb-6.0.1/ARBDB/adChangeKey.cxx

Last change on this file was 8607, checked in by westram, 12 years ago

merge from e4fix [8135] [8136] [8137] [8138] [8139] [8140] [8141] [8142] [8143] [8144] [8222]
(this revives the reverted patches [8129] [8130] [8131] [8132]; see [8133])

  • fixes
    • some free/delete mismatches
    • wrong definition of ORF objects (Level was no bit value)
    • amino consensus (failed for columns only containing 'C')
  • rename
    • AA_sequence_term → orf_term
    • ED4_sequence_terminal_basic → ED4_abstract_sequence_terminal
  • cleaned up hierarchy dumps
  • tweaked is_terminal()/to_terminal()
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 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 "gb_local.h"
14
15// get the container of a species key description
16GBDATA *GBT_get_changekey(GBDATA *gb_main, const char *key, const char *change_key_path) {
17#if defined(WARN_TODO)
18#warning check if search for CHANGEKEY_NAME should be case-sensitive!
19#endif
20    GBDATA *gb_key      = 0;
21    GBDATA *gb_key_data = GB_search(gb_main, change_key_path,
22                                    GB_CREATE_CONTAINER);
23
24    if (gb_key_data) {
25        GBDATA *gb_key_name = GB_find_string(gb_key_data, CHANGEKEY_NAME, key, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
26        if (gb_key_name) {
27            gb_key = GB_get_father(gb_key_name);
28        }
29    }
30    return gb_key;
31}
32
33GB_TYPES GBT_get_type_of_changekey(GBDATA *gb_main, const char *field_name, const char *change_key_path) {
34    GB_TYPES  type = GB_NONE;
35    GBDATA   *gbd  = GBT_get_changekey(gb_main, field_name, change_key_path);
36
37    if (gbd) {
38        long *typePtr     = GBT_read_int(gbd, CHANGEKEY_TYPE);
39        if (typePtr) {
40            type = (GB_TYPES)*typePtr;
41        }
42    }
43
44    return type;
45}
46
47static GB_ERROR gbt_set_type_of_changekey(GBDATA *gb_main, const char *field_name, GB_TYPES type, const char *change_key_path) {
48    GB_ERROR  error = NULL;
49    GBDATA   *gbd   = GBT_get_changekey(gb_main, field_name, change_key_path);
50
51    if (!gbd) {
52        error = GBS_global_string("Can't set type of nonexistent changekey " "\"%s\"", field_name);
53    }
54    else {
55        error = GBT_write_int(gbd, CHANGEKEY_TYPE, type);
56    }
57    return error;
58}
59
60
61GB_ERROR GBT_add_new_changekey_to_keypath(GBDATA *gb_main, const char *name, int type, const char *keypath) {
62    GB_ERROR    error  = NULL;
63    GBDATA     *gb_key = GBT_get_changekey(gb_main, name, keypath);
64    const char *c      = GB_first_non_key_char(name);
65
66    if (c) {
67        char *new_name = strdup(name);
68
69        *(char*)GB_first_non_key_char(new_name) = 0;
70
71        if      (*c == '/') error = GBT_add_new_changekey(gb_main, new_name, GB_DB);
72        else if (*c == '-') error = GBT_add_new_changekey(gb_main, new_name, GB_LINK);
73        else               error = GBS_global_string("Cannot add '%s' to your key list (illegal character '%c')", name, *c);
74
75        free(new_name);
76    }
77
78    if (!error) {
79        if (!gb_key) {          // create new key
80            GBDATA *gb_key_data = GB_search(gb_main, keypath, GB_CREATE_CONTAINER);
81            gb_key              = gb_key_data ? GB_create_container(gb_key_data, CHANGEKEY) : 0;
82
83            if (!gb_key) error = GB_await_error();
84            else {
85                error             = GBT_write_string(gb_key, CHANGEKEY_NAME, name);
86                if (!error) error = GBT_write_int(gb_key, CHANGEKEY_TYPE, type);
87            }
88        }
89        else {                  // check type of existing key
90            long *elem_type = GBT_read_int(gb_key, CHANGEKEY_TYPE);
91
92            if (!elem_type)              error = GB_await_error();
93            else if (*elem_type != type) error = GBS_global_string("Key '%s' exists, but has different type", name);
94        }
95    }
96
97    gb_assert(gb_key || error);
98
99    return error;
100}
101
102GB_ERROR GBT_add_new_changekey(GBDATA *gb_main, const char *name, int type) {
103    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH);
104}
105
106GB_ERROR GBT_add_new_gene_changekey(GBDATA *gb_main, const char *name, int type) {
107    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_GENES);
108}
109
110GB_ERROR GBT_add_new_experiment_changekey(GBDATA *gb_main, const char *name, int type) {
111    return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_EXPERIMENTS);
112}
113
114static GB_ERROR write_as_int(GBDATA *gbfield, const char *data, bool trimmed, size_t *rounded) {
115    char          *end   = 0;
116    unsigned long  i     = strtoul(data, &end, 10);
117    GB_ERROR       error = NULL;
118
119    if (end == data || end[0] != 0) {
120        if (trimmed) {
121            // fallback: convert to double and round
122
123            double d = strtod(data, &end);
124            if (end == data || end[0] != 0) {
125                error = GBS_global_string("cannot convert '%s' to rounded numeric value", data);
126            }
127            else {
128                (*rounded)++;
129                i                = d>0 ? (int)(d+0.5) : (int)(d-0.5);
130                error            = GB_write_int(gbfield, i);
131                if (error) error = GBS_global_string("write error (%s)", error);
132            }
133        }
134        else {
135            char *trimmed_data = GBS_trim(data);
136            error              = write_as_int(gbfield, trimmed_data, true, rounded);
137            free(trimmed_data);
138        }
139    }
140    else {
141        error = GB_write_int(gbfield, i);
142        if (error) error = GBS_global_string("write error (%s)", error);
143    }
144
145    return error;
146}
147
148static GB_ERROR write_as_float(GBDATA *gbfield, const char *data, bool trimmed) {
149    char     *end   = 0;
150    double    d     = strtod(data, &end);
151    GB_ERROR  error = NULL;
152
153    if (end == data || end[0] != 0) {
154        if (trimmed) {
155            error = GBS_global_string("cannot convert '%s' to numeric value", data);
156        }
157        else {
158            char *trimmed_data = GBS_trim(data);
159            error              = write_as_float(gbfield, trimmed_data, true);
160            free(trimmed_data);
161        }
162    }
163    else {
164        error = GB_write_float(gbfield, d);
165        if (error) error = GBS_global_string("write error (%s)", error);
166    }
167
168    return error;
169}
170
171
172GB_ERROR GBT_convert_changekey(GBDATA *gb_main, const char *name, GB_TYPES target_type) {
173    GB_ERROR error        = GB_push_transaction(gb_main);
174    bool     need_convert = true;
175
176    if (!error) {
177        GBDATA *gbkey = GBT_get_changekey(gb_main, name, CHANGE_KEY_PATH);
178        if (gbkey) {
179            GB_TYPES source_type = (GB_TYPES)*GBT_read_int(gbkey, CHANGEKEY_TYPE);
180            if (source_type == target_type) need_convert = false;
181        }
182        else {
183            error = GBS_global_string("Unknown changekey '%s'", name);
184        }
185    }
186
187    if (!error && need_convert) {
188        GBDATA *gbspec  = GBT_first_species(gb_main);
189        size_t  rounded = 0;
190
191        for (; gbspec; gbspec = GBT_next_species(gbspec)) {
192            GBDATA *gbfield = GB_entry(gbspec, name);
193
194            // If entry does not exist, no need to convert (sparse population is valid => 'NULL' value)
195            if (gbfield) {
196                char *data = GB_read_as_string(gbfield);
197                if (!data) {
198                    error = GBS_global_string("read error (%s)", GB_await_error());
199                }
200                else {
201                    error = GB_delete(gbfield);
202                    if (!error) {
203                        gbfield = GB_create(gbspec, name, target_type);
204                        if (!gbfield) {
205                            error = GBS_global_string("create error (%s)", GB_await_error());
206                        }
207                        else {
208                            switch (target_type) {
209                                case GB_INT:
210                                    error = write_as_int(gbfield, data, false, &rounded);
211                                    break;
212
213                                case GB_FLOAT:
214                                    error = write_as_float(gbfield, data, false);
215                                    break;
216
217                                case GB_STRING:
218                                    error = GB_write_string(gbfield, data);
219                                    if (error) error = GBS_global_string("write error (%s)", error);
220                                    break;
221
222                                default:
223                                    error = "Conversion is not possible";
224                                    break;
225                            }
226                        }
227                    }
228                    free(data);
229                }
230            }
231            if (error) break;
232        }
233
234        if (error && gbspec) {
235            const char *spname = GBT_read_name(gbspec);
236            error              = GBS_global_string("%s for species '%s'", error, spname);
237        }
238
239        if (!error) error = gbt_set_type_of_changekey(gb_main, name, target_type, CHANGE_KEY_PATH);
240        if (!error && rounded>0) {
241            GB_warningf("%zi values were rounded (loss of precision)", rounded);
242        }
243    }
244
245    if (error) error  = GBS_global_string("GBT_convert: %s", error);
246
247    return GB_end_transaction(gb_main, error);
248}
249
Note: See TracBrowser for help on using the repository browser.