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 | |
---|
16 | GBDATA *GBT_get_changekey(GBDATA *gb_main, const char *key, const char *change_key_path) { |
---|
17 | // get the container of an item key description |
---|
18 | |
---|
19 | // @@@ check if search for CHANGEKEY_NAME should be case-sensitive! |
---|
20 | GBDATA *gb_key = NULp; |
---|
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 | |
---|
33 | GB_TYPES GBT_get_type_of_changekey(GBDATA *gb_main, const char *field_name, const char *change_key_path) { |
---|
34 | // get the type of an item key |
---|
35 | GB_TYPES type = GB_NONE; |
---|
36 | GBDATA *gbd = GBT_get_changekey(gb_main, field_name, change_key_path); |
---|
37 | |
---|
38 | if (gbd) { |
---|
39 | long *typePtr = GBT_read_int(gbd, CHANGEKEY_TYPE); |
---|
40 | if (typePtr) { |
---|
41 | type = (GB_TYPES)*typePtr; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | return type; |
---|
46 | } |
---|
47 | |
---|
48 | static GB_ERROR gbt_set_type_of_changekey(GBDATA *gb_main, const char *field_name, GB_TYPES type, const char *change_key_path) { |
---|
49 | GB_ERROR error = NULp; |
---|
50 | GBDATA *gbd = GBT_get_changekey(gb_main, field_name, change_key_path); |
---|
51 | |
---|
52 | if (!gbd) { |
---|
53 | error = GBS_global_string("Can't set type of nonexistent changekey \"%s\"", field_name); |
---|
54 | } |
---|
55 | else { |
---|
56 | error = GBT_write_int(gbd, CHANGEKEY_TYPE, type); |
---|
57 | } |
---|
58 | return error; |
---|
59 | } |
---|
60 | |
---|
61 | GBDATA *GBT_searchOrCreate_itemfield_according_to_changekey(GBDATA *gb_item, const char *field_name, const char *change_key_path) { |
---|
62 | /*! search or create an item entry. |
---|
63 | * If the entry exists, the result is identical to GB_search(gb_item, field_name, GB_FIND). |
---|
64 | * If the entry does not exist, an entry with the type stored in the changekey-table will be created. |
---|
65 | * @return created itemfield or NULp in case of error (which is exported in that case) |
---|
66 | */ |
---|
67 | |
---|
68 | gb_assert(!GB_have_error()); |
---|
69 | GBDATA *gb_entry = GB_search(gb_item, field_name, GB_FIND); |
---|
70 | if (!gb_entry) { |
---|
71 | GB_clear_error(); |
---|
72 | |
---|
73 | GB_TYPES type = GBT_get_type_of_changekey(GB_get_root(gb_item), field_name, change_key_path); |
---|
74 | if (type == GB_NONE) { |
---|
75 | GB_export_errorf("Cannot create field '%s' (no type information available)", field_name); |
---|
76 | } |
---|
77 | else { |
---|
78 | gb_entry = GB_search(gb_item, field_name, type); |
---|
79 | } |
---|
80 | } |
---|
81 | gb_assert(gb_entry || GB_have_error()); |
---|
82 | return gb_entry; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | GB_ERROR GBT_add_new_changekey_to_keypath(GBDATA *gb_main, const char *name, int type, const char *keypath) { // @@@ type should be GB_TYPES (not int) |
---|
87 | GB_ERROR error = NULp; |
---|
88 | GBDATA *gb_key = GBT_get_changekey(gb_main, name, keypath); |
---|
89 | const char *c = GB_first_non_key_char(name); |
---|
90 | |
---|
91 | if (c) { |
---|
92 | char *new_name = ARB_strdup(name); |
---|
93 | |
---|
94 | *(char*)GB_first_non_key_char(new_name) = 0; |
---|
95 | |
---|
96 | if (*c == '/') error = GBT_add_new_changekey(gb_main, new_name, GB_DB); |
---|
97 | else error = GBS_global_string("Cannot add '%s' to your key list (illegal character '%c')", name, *c); |
---|
98 | |
---|
99 | free(new_name); |
---|
100 | } |
---|
101 | |
---|
102 | if (!error) { |
---|
103 | if (!gb_key) { // create new key |
---|
104 | GBDATA *gb_key_data = GB_search(gb_main, keypath, GB_CREATE_CONTAINER); |
---|
105 | gb_key = gb_key_data ? GB_create_container(gb_key_data, CHANGEKEY) : NULp; |
---|
106 | |
---|
107 | if (!gb_key) error = GB_await_error(); |
---|
108 | else { |
---|
109 | error = GBT_write_string(gb_key, CHANGEKEY_NAME, name); |
---|
110 | if (!error) error = GBT_write_int(gb_key, CHANGEKEY_TYPE, type); |
---|
111 | } |
---|
112 | } |
---|
113 | else { // check type of existing key |
---|
114 | long *elem_type = GBT_read_int(gb_key, CHANGEKEY_TYPE); |
---|
115 | |
---|
116 | if (!elem_type) error = GB_await_error(); |
---|
117 | else if (*elem_type != type) error = GBS_global_string("Key '%s' exists, but has different type", name); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | gb_assert(gb_key || error); |
---|
122 | |
---|
123 | return error; |
---|
124 | } |
---|
125 | |
---|
126 | GB_ERROR GBT_add_new_changekey(GBDATA *gb_main, const char *name, int type) { // @@@ type should be GB_TYPES (not int) |
---|
127 | return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH); |
---|
128 | } |
---|
129 | |
---|
130 | GB_ERROR GBT_add_new_gene_changekey(GBDATA *gb_main, const char *name, int type) { |
---|
131 | return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_GENES); // @@@ type should be GB_TYPES (not int) |
---|
132 | } |
---|
133 | |
---|
134 | GB_ERROR GBT_add_new_experiment_changekey(GBDATA *gb_main, const char *name, int type) { |
---|
135 | return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_EXPERIMENTS); // @@@ type should be GB_TYPES (not int) |
---|
136 | } |
---|
137 | |
---|
138 | static GB_ERROR write_int_converted(GBDATA *gbfield, const char *data, bool trimmed, size_t *rounded) { |
---|
139 | // 'rounded' is incremented each time a float value was |
---|
140 | // converted to integer (even if float value can convert lossless, e.g. "4.0"). |
---|
141 | |
---|
142 | char *end = NULp; |
---|
143 | unsigned long i = strtoul(data, &end, 10); |
---|
144 | GB_ERROR error = NULp; |
---|
145 | |
---|
146 | if (end == data || end[0] != 0) { |
---|
147 | if (trimmed) { |
---|
148 | // fallback: convert to double and round |
---|
149 | |
---|
150 | double d = strtod(data, &end); |
---|
151 | if (end == data || end[0] != 0) { |
---|
152 | error = GBS_global_string("cannot convert '%s' to rounded numeric value", data); |
---|
153 | } |
---|
154 | else { |
---|
155 | (*rounded)++; |
---|
156 | i = d>0 ? (int)(d+0.5) : (int)(d-0.5); |
---|
157 | error = GB_write_int(gbfield, i); |
---|
158 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
159 | } |
---|
160 | } |
---|
161 | else { |
---|
162 | char *trimmed_data = GBS_trim(data); |
---|
163 | error = write_int_converted(gbfield, trimmed_data, true, rounded); |
---|
164 | free(trimmed_data); |
---|
165 | } |
---|
166 | } |
---|
167 | else { |
---|
168 | error = GB_write_int(gbfield, i); |
---|
169 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
170 | } |
---|
171 | |
---|
172 | return error; |
---|
173 | } |
---|
174 | |
---|
175 | static GB_ERROR write_float_converted(GBDATA *gbfield, const char *data, bool trimmed) { |
---|
176 | char *end = NULp; |
---|
177 | float f = strtof(data, &end); |
---|
178 | GB_ERROR error = NULp; |
---|
179 | |
---|
180 | if (end == data || end[0] != 0) { |
---|
181 | if (trimmed) { |
---|
182 | error = GBS_global_string("cannot convert '%s' to numeric value", data); |
---|
183 | } |
---|
184 | else { |
---|
185 | char *trimmed_data = GBS_trim(data); |
---|
186 | error = write_float_converted(gbfield, trimmed_data, true); |
---|
187 | free(trimmed_data); |
---|
188 | } |
---|
189 | } |
---|
190 | else { |
---|
191 | error = GB_write_float(gbfield, f); |
---|
192 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
193 | } |
---|
194 | |
---|
195 | return error; |
---|
196 | } |
---|
197 | |
---|
198 | GB_ERROR GBT_write_int_converted(GBDATA *gb_int_writable, const char *data, size_t *rounded) { |
---|
199 | // Note: has functional similarities to GB_write_autoconv_string |
---|
200 | return write_int_converted(gb_int_writable, data, false, rounded); |
---|
201 | } |
---|
202 | GB_ERROR GBT_write_float_converted(GBDATA *gb_float_writable, const char *data) { |
---|
203 | // Note: has functional similarities to GB_write_autoconv_string |
---|
204 | return write_float_converted(gb_float_writable, data, false); |
---|
205 | } |
---|
206 | |
---|
207 | GB_ERROR GBT_convert_changekey(GBDATA *gb_main, const char *name, GB_TYPES target_type) { |
---|
208 | GB_ERROR error = GB_push_transaction(gb_main); |
---|
209 | bool need_convert = true; |
---|
210 | |
---|
211 | if (!error) { |
---|
212 | GBDATA *gbkey = GBT_get_changekey(gb_main, name, CHANGE_KEY_PATH); |
---|
213 | if (gbkey) { |
---|
214 | GB_TYPES source_type = (GB_TYPES)*GBT_read_int(gbkey, CHANGEKEY_TYPE); |
---|
215 | if (source_type == target_type) need_convert = false; |
---|
216 | } |
---|
217 | else { |
---|
218 | if (!name[0] || strcmp(name, NO_FIELD_SELECTED) == 0) { |
---|
219 | error = "Please select field to convert"; |
---|
220 | } |
---|
221 | else { |
---|
222 | error = GBS_global_string("Unknown changekey '%s'", name); |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | if (!error && need_convert) { |
---|
228 | GBDATA *gbspec = GBT_first_species(gb_main); |
---|
229 | size_t rounded = 0; |
---|
230 | |
---|
231 | for (; gbspec; gbspec = GBT_next_species(gbspec)) { |
---|
232 | GBDATA *gbfield = GB_entry(gbspec, name); |
---|
233 | |
---|
234 | // If entry does not exist, no need to convert (sparse population is valid => 'NULp' value) |
---|
235 | if (gbfield) { |
---|
236 | char *data = GB_read_as_string(gbfield); |
---|
237 | if (!data) { |
---|
238 | error = GBS_global_string("read error (%s)", GB_await_error()); |
---|
239 | } |
---|
240 | else { |
---|
241 | error = GB_delete(gbfield); |
---|
242 | if (!error) { |
---|
243 | gbfield = GB_create(gbspec, name, target_type); |
---|
244 | if (!gbfield) { |
---|
245 | error = GBS_global_string("create error (%s)", GB_await_error()); |
---|
246 | } |
---|
247 | else { |
---|
248 | switch (target_type) { |
---|
249 | case GB_INT: |
---|
250 | error = GBT_write_int_converted(gbfield, data, &rounded); |
---|
251 | break; |
---|
252 | |
---|
253 | case GB_FLOAT: |
---|
254 | error = GBT_write_float_converted(gbfield, data); |
---|
255 | break; |
---|
256 | |
---|
257 | case GB_STRING: |
---|
258 | error = GB_write_string(gbfield, data); |
---|
259 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
260 | break; |
---|
261 | |
---|
262 | default: |
---|
263 | error = "Conversion is not possible"; |
---|
264 | break; |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | free(data); |
---|
269 | } |
---|
270 | } |
---|
271 | if (error) break; |
---|
272 | } |
---|
273 | |
---|
274 | if (error && gbspec) { |
---|
275 | const char *spname = GBT_get_name_or_description(gbspec); |
---|
276 | error = GBS_global_string("%s for species '%s'", error, spname); |
---|
277 | } |
---|
278 | |
---|
279 | if (!error) error = gbt_set_type_of_changekey(gb_main, name, target_type, CHANGE_KEY_PATH); |
---|
280 | if (!error && rounded>0) { |
---|
281 | GB_warningf("%zi values were rounded (loss of precision)", rounded); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | if (error) error = GBS_global_string("GBT_convert: %s", error); |
---|
286 | |
---|
287 | return GB_end_transaction(gb_main, error); |
---|
288 | } |
---|
289 | |
---|