source: tags/ms_r16q2/ARBDB/aditem.cxx

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