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 | #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 | |
---|
34 | GB_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 | |
---|
49 | static 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 | |
---|
62 | GBDATA *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 | |
---|
87 | GB_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 = ARB_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 error = GBS_global_string("Cannot add '%s' to your key list (illegal character '%c')", name, *c); |
---|
99 | |
---|
100 | free(new_name); |
---|
101 | } |
---|
102 | |
---|
103 | if (!error) { |
---|
104 | if (!gb_key) { // create new key |
---|
105 | GBDATA *gb_key_data = GB_search(gb_main, keypath, GB_CREATE_CONTAINER); |
---|
106 | gb_key = gb_key_data ? GB_create_container(gb_key_data, CHANGEKEY) : 0; |
---|
107 | |
---|
108 | if (!gb_key) error = GB_await_error(); |
---|
109 | else { |
---|
110 | error = GBT_write_string(gb_key, CHANGEKEY_NAME, name); |
---|
111 | if (!error) error = GBT_write_int(gb_key, CHANGEKEY_TYPE, type); |
---|
112 | } |
---|
113 | } |
---|
114 | else { // check type of existing key |
---|
115 | long *elem_type = GBT_read_int(gb_key, CHANGEKEY_TYPE); |
---|
116 | |
---|
117 | if (!elem_type) error = GB_await_error(); |
---|
118 | else if (*elem_type != type) error = GBS_global_string("Key '%s' exists, but has different type", name); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | gb_assert(gb_key || error); |
---|
123 | |
---|
124 | return error; |
---|
125 | } |
---|
126 | |
---|
127 | GB_ERROR GBT_add_new_changekey(GBDATA *gb_main, const char *name, int type) { |
---|
128 | return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH); |
---|
129 | } |
---|
130 | |
---|
131 | GB_ERROR GBT_add_new_gene_changekey(GBDATA *gb_main, const char *name, int type) { |
---|
132 | return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_GENES); |
---|
133 | } |
---|
134 | |
---|
135 | GB_ERROR GBT_add_new_experiment_changekey(GBDATA *gb_main, const char *name, int type) { |
---|
136 | return GBT_add_new_changekey_to_keypath(gb_main, name, type, CHANGE_KEY_PATH_EXPERIMENTS); |
---|
137 | } |
---|
138 | |
---|
139 | static GB_ERROR write_as_int(GBDATA *gbfield, const char *data, bool trimmed, size_t *rounded) { |
---|
140 | char *end = 0; |
---|
141 | unsigned long i = strtoul(data, &end, 10); |
---|
142 | GB_ERROR error = NULL; |
---|
143 | |
---|
144 | if (end == data || end[0] != 0) { |
---|
145 | if (trimmed) { |
---|
146 | // fallback: convert to double and round |
---|
147 | |
---|
148 | double d = strtod(data, &end); |
---|
149 | if (end == data || end[0] != 0) { |
---|
150 | error = GBS_global_string("cannot convert '%s' to rounded numeric value", data); |
---|
151 | } |
---|
152 | else { |
---|
153 | (*rounded)++; |
---|
154 | i = d>0 ? (int)(d+0.5) : (int)(d-0.5); |
---|
155 | error = GB_write_int(gbfield, i); |
---|
156 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
157 | } |
---|
158 | } |
---|
159 | else { |
---|
160 | char *trimmed_data = GBS_trim(data); |
---|
161 | error = write_as_int(gbfield, trimmed_data, true, rounded); |
---|
162 | free(trimmed_data); |
---|
163 | } |
---|
164 | } |
---|
165 | else { |
---|
166 | error = GB_write_int(gbfield, i); |
---|
167 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
168 | } |
---|
169 | |
---|
170 | return error; |
---|
171 | } |
---|
172 | |
---|
173 | static GB_ERROR write_as_float(GBDATA *gbfield, const char *data, bool trimmed) { |
---|
174 | char *end = 0; |
---|
175 | float f = strtof(data, &end); |
---|
176 | GB_ERROR error = NULL; |
---|
177 | |
---|
178 | if (end == data || end[0] != 0) { |
---|
179 | if (trimmed) { |
---|
180 | error = GBS_global_string("cannot convert '%s' to numeric value", data); |
---|
181 | } |
---|
182 | else { |
---|
183 | char *trimmed_data = GBS_trim(data); |
---|
184 | error = write_as_float(gbfield, trimmed_data, true); |
---|
185 | free(trimmed_data); |
---|
186 | } |
---|
187 | } |
---|
188 | else { |
---|
189 | error = GB_write_float(gbfield, f); |
---|
190 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
191 | } |
---|
192 | |
---|
193 | return error; |
---|
194 | } |
---|
195 | |
---|
196 | |
---|
197 | GB_ERROR GBT_convert_changekey(GBDATA *gb_main, const char *name, GB_TYPES target_type) { |
---|
198 | GB_ERROR error = GB_push_transaction(gb_main); |
---|
199 | bool need_convert = true; |
---|
200 | |
---|
201 | if (!error) { |
---|
202 | GBDATA *gbkey = GBT_get_changekey(gb_main, name, CHANGE_KEY_PATH); |
---|
203 | if (gbkey) { |
---|
204 | GB_TYPES source_type = (GB_TYPES)*GBT_read_int(gbkey, CHANGEKEY_TYPE); |
---|
205 | if (source_type == target_type) need_convert = false; |
---|
206 | } |
---|
207 | else { |
---|
208 | if (!name[0] || strcmp(name, NO_FIELD_SELECTED) == 0) { |
---|
209 | error = "Please select field to convert"; |
---|
210 | } |
---|
211 | else { |
---|
212 | error = GBS_global_string("Unknown changekey '%s'", name); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | if (!error && need_convert) { |
---|
218 | GBDATA *gbspec = GBT_first_species(gb_main); |
---|
219 | size_t rounded = 0; |
---|
220 | |
---|
221 | for (; gbspec; gbspec = GBT_next_species(gbspec)) { |
---|
222 | GBDATA *gbfield = GB_entry(gbspec, name); |
---|
223 | |
---|
224 | // If entry does not exist, no need to convert (sparse population is valid => 'NULL' value) |
---|
225 | if (gbfield) { |
---|
226 | char *data = GB_read_as_string(gbfield); |
---|
227 | if (!data) { |
---|
228 | error = GBS_global_string("read error (%s)", GB_await_error()); |
---|
229 | } |
---|
230 | else { |
---|
231 | error = GB_delete(gbfield); |
---|
232 | if (!error) { |
---|
233 | gbfield = GB_create(gbspec, name, target_type); |
---|
234 | if (!gbfield) { |
---|
235 | error = GBS_global_string("create error (%s)", GB_await_error()); |
---|
236 | } |
---|
237 | else { |
---|
238 | switch (target_type) { |
---|
239 | case GB_INT: |
---|
240 | error = write_as_int(gbfield, data, false, &rounded); |
---|
241 | break; |
---|
242 | |
---|
243 | case GB_FLOAT: |
---|
244 | error = write_as_float(gbfield, data, false); |
---|
245 | break; |
---|
246 | |
---|
247 | case GB_STRING: |
---|
248 | error = GB_write_string(gbfield, data); |
---|
249 | if (error) error = GBS_global_string("write error (%s)", error); |
---|
250 | break; |
---|
251 | |
---|
252 | default: |
---|
253 | error = "Conversion is not possible"; |
---|
254 | break; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | free(data); |
---|
259 | } |
---|
260 | } |
---|
261 | if (error) break; |
---|
262 | } |
---|
263 | |
---|
264 | if (error && gbspec) { |
---|
265 | const char *spname = GBT_read_name(gbspec); |
---|
266 | error = GBS_global_string("%s for species '%s'", error, spname); |
---|
267 | } |
---|
268 | |
---|
269 | if (!error) error = gbt_set_type_of_changekey(gb_main, name, target_type, CHANGE_KEY_PATH); |
---|
270 | if (!error && rounded>0) { |
---|
271 | GB_warningf("%zi values were rounded (loss of precision)", rounded); |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | if (error) error = GBS_global_string("GBT_convert: %s", error); |
---|
276 | |
---|
277 | return GB_end_transaction(gb_main, error); |
---|
278 | } |
---|
279 | |
---|