source: tags/arb-6.0.4/ARBDB/aditem.cxx

Last change on this file was 10949, checked in by westram, 12 years ago
  • fix cppcheck warnings
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : aditem.cxx                                        //
4//   Purpose   : item functions                                    //
5//               items are e.g. species, SAIs, genes, etc          //
6//                                                                 //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include "gb_local.h"
13
14#include <arbdbt.h>
15#include <arb_strbuf.h>
16
17
18GBDATA *GBT_find_or_create_item_rel_item_data(GBDATA *gb_item_data, const char *itemname, const char *id_field, const char *id, bool markCreated) {
19    /* Search for a item with field 'id_field' set to given 'id' (id compare is case-insensitive)
20     * If item does not exist, create it.
21     * Newly created items are automatically marked, if 'markCreated' is true
22     * items may be: species, genes, SAIs, ...
23     */
24
25    GBDATA   *gb_item = 0;
26    GB_ERROR  error   = 0;
27
28    if (!gb_item_data) error = "No container";
29    else {
30        gb_item = GBT_find_item_rel_item_data(gb_item_data, id_field, id);
31        if (!gb_item) {
32            error = GB_push_transaction(gb_item_data);
33            if (!error) {
34                gb_item             = GB_create_container(gb_item_data, itemname); // create a new item
35                if (!gb_item) error = GB_await_error();
36                else {
37                    error = GBT_write_string(gb_item, id_field, id); // write item identifier
38                    if (!error && markCreated) GB_write_flag(gb_item, 1); // mark generated item
39                }
40            }
41            error = GB_end_transaction(gb_item_data, error);
42        }
43    }
44
45    if (!gb_item && !error) error = GB_await_error();
46    if (error) {
47        gb_item = 0;
48        GB_export_errorf("Can't create %s '%s': %s", itemname, id, error);
49    }
50
51    return gb_item;
52}
53
54GBDATA *GBT_find_or_create_species_rel_species_data(GBDATA *gb_species_data, const char *name) {
55    return GBT_find_or_create_item_rel_item_data(gb_species_data, "species", "name", name, true);
56}
57
58GBDATA *GBT_find_or_create_species(GBDATA *gb_main, const char *name) {
59    return GBT_find_or_create_species_rel_species_data(GBT_get_species_data(gb_main), name);
60}
61
62GBDATA *GBT_find_or_create_SAI(GBDATA *gb_main, const char *name) {
63    //! Search for an SAI, when SAI does not exist, create it
64    return GBT_find_or_create_item_rel_item_data(GBT_get_SAI_data(gb_main), "extended", "name", name, true);
65}
66
67
68// ------------------------------------
69//      some simple find procedures
70
71GBDATA *GBT_find_item_rel_item_data(GBDATA *gb_item_data, const char *id_field, const char *id_value) {
72    /*! search for items starting at item container
73     *
74     * @param 'gb_item_data' is a container containing items
75     * @param 'id_field' is a field containing a unique identifier for each item (e.g. 'name' for species)
76     * @param 'id_value' expected value of 'id_field'
77     *
78     * @return a pointer to an item with 'id_field' containing 'id_value'
79     * or NULL (in this case an error MAY be exported)
80     *
81     * Note: If you expect the item to exist, use GBT_expect_item_rel_item_data()
82     */
83
84    GBDATA *gb_item_id = GB_find_string(gb_item_data, id_field, id_value, GB_IGNORE_CASE, SEARCH_GRANDCHILD);
85    return gb_item_id ? GB_get_father(gb_item_id) : 0;
86}
87
88static GBDATA *GBT_expect_item_rel_item_data(GBDATA *gb_item_data, const char *id_field, const char *id_value) {
89    //! like GBT_find_item_rel_item_data(), but also exports an error if the item is not present
90
91    GBDATA *gb_found = GBT_find_item_rel_item_data(gb_item_data, id_field, id_value);
92    if (!gb_found && !GB_have_error()) { // item simply not exists
93        GBDATA     *gb_any   = GB_find(gb_item_data, id_field, SEARCH_GRANDCHILD);
94        const char *itemname = gb_any ? GB_read_key_pntr(GB_get_father(gb_any)) : "<item>";
95        GB_export_errorf("Could not find %s with %s '%s'", itemname, id_field, id_value);
96    }
97    return gb_found;
98}
99
100// --------------------------------------------------------------------------------
101
102GBDATA *GBT_get_species_data(GBDATA *gb_main) {
103    return GBT_find_or_create(gb_main, "species_data", 7);
104}
105
106GBDATA *GBT_first_marked_species_rel_species_data(GBDATA *gb_species_data) {
107    return GB_first_marked(gb_species_data, "species");
108}
109
110GBDATA *GBT_first_marked_species(GBDATA *gb_main) {
111    return GB_first_marked(GBT_get_species_data(gb_main), "species");
112}
113GBDATA *GBT_next_marked_species(GBDATA *gb_species) {
114    gb_assert(GB_has_key(gb_species, "species"));
115    return GB_next_marked(gb_species, "species");
116}
117
118GBDATA *GBT_first_species_rel_species_data(GBDATA *gb_species_data) {
119    return GB_entry(gb_species_data, "species");
120}
121GBDATA *GBT_first_species(GBDATA *gb_main) {
122    return GB_entry(GBT_get_species_data(gb_main), "species");
123}
124
125GBDATA *GBT_next_species(GBDATA *gb_species) {
126    gb_assert(GB_has_key(gb_species, "species"));
127    return GB_nextEntry(gb_species);
128}
129
130GBDATA *GBT_find_species_rel_species_data(GBDATA *gb_species_data, const char *name) {
131    return GBT_find_item_rel_item_data(gb_species_data, "name", name);
132}
133GBDATA *GBT_find_species(GBDATA *gb_main, const char *name) {
134    // Search for a species.
135    // Return found species or NULL (in this case an error MAY be exported).
136    //
137    // Note: If you expect the species to exists, use GBT_expect_species!
138    return GBT_find_item_rel_item_data(GBT_get_species_data(gb_main), "name", name);
139}
140
141GBDATA *GBT_expect_species(GBDATA *gb_main, const char *name) {
142    // Returns an existing species or
143    // NULL (in that case an error is exported)
144    return GBT_expect_item_rel_item_data(GBT_get_species_data(gb_main), "name", name);
145}
146
147// --------------------------------------------------------------------------------
148
149GBDATA *GBT_get_SAI_data(GBDATA *gb_main) {
150    return GBT_find_or_create(gb_main, "extended_data", 7);
151}
152
153// Search SAIs
154GBDATA *GBT_first_SAI_rel_SAI_data(GBDATA *gb_sai_data) {
155    return GB_entry(gb_sai_data, "extended");
156}
157GBDATA *GBT_first_SAI(GBDATA *gb_main) {
158    return GB_entry(GBT_get_SAI_data(gb_main), "extended");
159}
160
161GBDATA *GBT_next_SAI(GBDATA *gb_sai) {
162    gb_assert(GB_has_key(gb_sai, "extended"));
163    return GB_nextEntry(gb_sai);
164}
165
166GBDATA *GBT_find_SAI_rel_SAI_data(GBDATA *gb_sai_data, const char *name) {
167    return GBT_find_item_rel_item_data(gb_sai_data, "name", name);
168}
169GBDATA *GBT_find_SAI(GBDATA *gb_main, const char *name) {
170    // Search for a SAI.
171    // Return found SAI or NULL (in this case an error MAY be exported).
172    //
173    // Note: If you expect the SAI to exist, use GBT_expect_SAI!
174    return GBT_find_item_rel_item_data(GBT_get_SAI_data(gb_main), "name", name);
175}
176
177GBDATA *GBT_expect_SAI(GBDATA *gb_main, const char *name) {
178    // Returns an existing SAI or
179    // NULL (in that case an error is exported)
180    return GBT_expect_item_rel_item_data(GBT_get_SAI_data(gb_main), "name", name);
181}
182
183// ---------------------
184//      count items
185
186static long GBT_get_item_count(GBDATA *gb_parent_of_container, const char *item_container_name) {
187    // returns elements stored in a container
188
189    GBDATA *gb_item_data;
190    long    count = 0;
191
192    GB_push_transaction(gb_parent_of_container);
193    gb_item_data = GB_entry(gb_parent_of_container, item_container_name);
194    if (gb_item_data) count = GB_number_of_subentries(gb_item_data);
195    GB_pop_transaction(gb_parent_of_container);
196
197    return count;
198}
199
200long GBT_get_species_count(GBDATA *gb_main) {
201    return GBT_get_item_count(gb_main, "species_data");
202}
203
204long GBT_get_SAI_count(GBDATA *gb_main) {
205    return GBT_get_item_count(gb_main, "extended_data");
206}
207
208// --------------------------------------------------------------------------------
209
210static char *GBT_create_unique_item_identifier(GBDATA *gb_item_container, const char *id_field, const char *default_id) {
211    // returns an identifier not used by items in 'gb_item_container'
212    // 'id_field' is the entry that is used as identifier (e.g. 'name' for species)
213    // 'default_id' will be suffixed with a number to generate a unique id
214    //
215    // Note:
216    // * The resulting id may be longer than 8 characters
217    // * This function is slow, so just use in extra-ordinary situations
218
219    GBDATA *gb_item   = GBT_find_item_rel_item_data(gb_item_container, id_field, default_id);
220    char   *unique_id;
221
222    if (!gb_item) {
223        unique_id = strdup(default_id); // default_id is unused
224    }
225    else {
226        char   *generated_id  = (char*)malloc(strlen(default_id)+20);
227        size_t  min_num = 1;
228
229#define GENERATE_ID(num) sprintf(generated_id, "%s%zu", default_id, num);
230
231        GENERATE_ID(min_num);
232        gb_item = GBT_find_item_rel_item_data(gb_item_container, id_field, generated_id);
233
234        if (gb_item) {
235            size_t num_items = GB_number_of_subentries(gb_item_container);
236            size_t max_num   = 0;
237
238            gb_assert(num_items); // otherwise deadlock below
239
240            do {
241                max_num += num_items;
242                GENERATE_ID(max_num);
243                gb_item  = GBT_find_item_rel_item_data(gb_item_container, id_field, generated_id);
244            } while (gb_item && max_num >= num_items);
245
246            if (max_num<num_items) { // overflow
247                char *uid;
248                generated_id[0] = 'a'+GB_random(26);
249                generated_id[1] = 'a'+GB_random(26);
250                generated_id[2] = 0;
251
252                uid = GBT_create_unique_item_identifier(gb_item_container, id_field, generated_id);
253                strcpy(generated_id, uid);
254                free(uid);
255            }
256            else {
257                // max_num is unused
258                while ((max_num-min_num)>1) {
259                    size_t mid = (min_num+max_num)/2;
260                    gb_assert(mid != min_num && mid != max_num);
261
262                    GENERATE_ID(mid);
263                    gb_item = GBT_find_item_rel_item_data(gb_item_container, id_field, generated_id);
264
265                    if (gb_item) min_num = mid;
266                    else max_num = mid;
267                }
268                GENERATE_ID(max_num);
269                gb_assert(GBT_find_item_rel_item_data(gb_item_container, id_field, generated_id) == NULL);
270            }
271        }
272        unique_id = generated_id;
273
274#undef GENERATE_ID
275    }
276
277    return unique_id;
278}
279
280char *GBT_create_unique_species_name(GBDATA *gb_main, const char *default_name) {
281    return GBT_create_unique_item_identifier(GBT_get_species_data(gb_main), "name", default_name);
282}
283
284
285// --------------------------------
286//      mark and unmark species
287
288void GBT_mark_all(GBDATA *gb_main, int flag) {
289    // flag == 0 -> unmark
290    // flag == 1 -> mark
291    // flag == 2 -> invert
292
293    GBDATA *gb_species;
294    GB_push_transaction(gb_main);
295
296    if (flag == 2) {
297        for (gb_species = GBT_first_species(gb_main);
298             gb_species;
299             gb_species = GBT_next_species(gb_species))
300        {
301            GB_write_flag(gb_species, !GB_read_flag(gb_species));
302        }
303    }
304    else {
305        gb_assert(flag == 0 || flag == 1);
306
307        for (gb_species = GBT_first_species(gb_main);
308             gb_species;
309             gb_species = GBT_next_species(gb_species))
310        {
311            GB_write_flag(gb_species, flag);
312        }
313    }
314    GB_pop_transaction(gb_main);
315}
316void GBT_mark_all_that(GBDATA *gb_main, int flag, int (*condition)(GBDATA*, void*), void *cd)
317{
318    GBDATA *gb_species;
319    GB_push_transaction(gb_main);
320
321    if (flag == 2) {
322        for (gb_species = GBT_first_species(gb_main);
323             gb_species;
324             gb_species = GBT_next_species(gb_species))
325        {
326            if (condition(gb_species, cd)) {
327                GB_write_flag(gb_species, !GB_read_flag(gb_species));
328            }
329        }
330    }
331    else {
332        gb_assert(flag == 0 || flag == 1);
333
334        for (gb_species = GBT_first_species(gb_main);
335             gb_species;
336             gb_species = GBT_next_species(gb_species))
337        {
338            int curr_flag = GB_read_flag(gb_species);
339            if (curr_flag != flag && condition(gb_species, cd)) {
340                GB_write_flag(gb_species, flag);
341            }
342        }
343    }
344    GB_pop_transaction(gb_main);
345}
346
347long GBT_count_marked_species(GBDATA *gb_main) {
348    GB_transaction ta(gb_main);
349    return GB_number_of_marked_subentries(GBT_get_species_data(gb_main));
350}
351
352char *GBT_store_marked_species(GBDATA *gb_main, int unmark_all)
353{
354    /* stores the currently marked species in a string
355       if (unmark_all != 0) then unmark them too
356    */
357
358    GBS_strstruct *out = GBS_stropen(10000);
359    GBDATA        *gb_species;
360
361    for (gb_species = GBT_first_marked_species(gb_main);
362         gb_species;
363         gb_species = GBT_next_marked_species(gb_species))
364    {
365        GBS_strcat(out, GBT_read_name(gb_species));
366        GBS_chrcat(out, ';');
367        if (unmark_all) GB_write_flag(gb_species, 0);
368    }
369
370    GBS_str_cut_tail(out, 1); // remove trailing ';'
371    return GBS_strclose(out);
372}
373
374NOT4PERL GB_ERROR GBT_with_stored_species(GBDATA *gb_main, const char *stored, species_callback doit, int *clientdata) {
375    // call function 'doit' with all species stored in 'stored'
376
377#define MAX_NAME_LEN 20
378    char     name[MAX_NAME_LEN+1];
379    GB_ERROR error = 0;
380
381    while (!error) {
382        const char   *p   = strchr(stored, ';');
383        int     len = p ? (p-stored) : (int)strlen(stored);
384        GBDATA *gb_species;
385
386        gb_assert(len <= MAX_NAME_LEN);
387        memcpy(name, stored, len);
388        name[len] = 0;
389
390        gb_species = GBT_find_species(gb_main, name);
391        if (gb_species) {
392            error = doit(gb_species, clientdata);
393        }
394        else {
395            error = "Some stored species where not found.";
396        }
397
398        if (!p) break;
399        stored = p+1;
400    }
401#undef MAX_NAME_LEN
402    return error;
403}
404
405static GB_ERROR restore_mark(GBDATA *gb_species, int *) {
406    GB_write_flag(gb_species, 1);
407    return 0;
408}
409
410GB_ERROR GBT_restore_marked_species(GBDATA *gb_main, const char *stored_marked) {
411    /* restores the species-marks to a state currently saved
412     * into 'stored_marked' by GBT_store_marked_species
413     */
414
415    GBT_mark_all(gb_main, 0);   // unmark all species
416    return GBT_with_stored_species(gb_main, stored_marked, restore_mark, 0);
417}
418
419// ---------------------------------
420//      read species information
421
422#if defined(WARN_TODO)
423#warning rename the following functions to make the difference more obvious??
424#endif
425GB_CSTR GBT_read_name(GBDATA *gb_item) {
426    GB_CSTR result      = GBT_read_char_pntr(gb_item, "name");
427    if (!result) result = GBS_global_string("<unnamed_%s>", GB_read_key_pntr(gb_item));
428    return result;
429}
430
431const char *GBT_get_name(GBDATA *gb_item) {
432    GBDATA *gb_name = GB_entry(gb_item, "name");
433    if (!gb_name) return 0;
434    return GB_read_char_pntr(gb_name);
435}
436
437GBDATA **GBT_gen_species_array(GBDATA *gb_main, long *pspeccnt)
438{
439    GBDATA *gb_species;
440    GBDATA *gb_species_data = GBT_get_species_data(gb_main);
441    GBDATA **result;
442    *pspeccnt = 0;
443    for (gb_species = GBT_first_species_rel_species_data(gb_species_data);
444         gb_species;
445         gb_species = GBT_next_species(gb_species)) {
446        (*pspeccnt) ++;
447    }
448    result = (GBDATA **)malloc((size_t)(sizeof(GBDATA *)* (*pspeccnt))); // @@@ fails if no species present
449    *pspeccnt = 0;
450    for (gb_species = GBT_first_species_rel_species_data(gb_species_data);
451         gb_species;
452         gb_species = GBT_next_species(gb_species)) {
453        result[(*pspeccnt)++]=gb_species;
454    }
455    return result;
456}
457
Note: See TracBrowser for help on using the repository browser.