source: branches/stable/SL/ITEMS/changekey.cxx

Last change on this file was 16763, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1// ==================================================================== //
2//                                                                      //
3//   File      : changekey.cxx                                          //
4//   Purpose   : changekey management                                   //
5//                                                                      //
6//                                                                      //
7// Coded by Ralf Westram (coder@reallysoft.de) in May 2005              //
8// Copyright Department of Microbiology (Technical University Munich)   //
9//                                                                      //
10// Visit our web site at: http://www.arb-home.de/                       //
11//                                                                      //
12// ==================================================================== //
13
14#include "item_sel_list.h"
15
16#include <arbdbt.h>
17#include <arb_strarray.h>
18
19static const char GENE_DATA_PATH[]       = "gene_data/gene/";
20static const char EXPERIMENT_DATA_PATH[] = "experiment_data/experiment/";
21
22#define GENE_DATA_PATH_LEN       (sizeof(GENE_DATA_PATH)-1)
23#define EXPERIMENT_DATA_PATH_LEN (sizeof(EXPERIMENT_DATA_PATH)-1)
24
25inline bool is_in_GENE_path(const char *fieldpath)          { return strncmp(fieldpath, GENE_DATA_PATH,         GENE_DATA_PATH_LEN) == 0; }
26inline bool is_in_EXPERIMENT_path(const char *fieldpath)    { return strncmp(fieldpath, EXPERIMENT_DATA_PATH,   EXPERIMENT_DATA_PATH_LEN) == 0; }
27
28inline bool is_in_reserved_path(const char *fieldpath) {
29    return
30        is_in_GENE_path(fieldpath) ||
31        is_in_EXPERIMENT_path(fieldpath);
32}
33
34// --------------------------------------------------------------------------------
35
36
37static void delete_unused_changekeys(GBDATA *gb_main, const CharPtrArray& names, const char *change_key_path) {
38    // deletes all keys from 'change_key_path' which are not listed in 'names'
39    GBDATA *gb_key_data = GB_search(gb_main, change_key_path, GB_CREATE_CONTAINER);
40    GBDATA *gb_key      = gb_key_data ? GB_entry(gb_key_data, CHANGEKEY) : NULp;
41
42    while (gb_key) {
43        bool        found    = false;
44        int         key_type = *GBT_read_int(gb_key, CHANGEKEY_TYPE);
45        const char *key_name = GBT_read_char_pntr(gb_key, CHANGEKEY_NAME);
46
47        for (int i = 0; names[i]; ++i) {
48            const char *name = names[i];
49            if (strcmp(key_name, name+1) == 0) { // key with this name exists
50                if (key_type == name[0]) {
51                    found = true;
52                }
53                // otherwise key exists, but type mismatches = > delete this key
54                break;
55            }
56        }
57
58        it_assert(GB_has_key(gb_key, CHANGEKEY));
59        GBDATA *gb_next_key = GB_nextEntry(gb_key);
60
61        if (!found) {
62            if (key_type == GB_DB) { // it's a container
63                // test if any subkey is used
64                int keylen = strlen(key_name);
65                for (int i = 0; names[i]; ++i) {
66                    const char *n = names[i]+1;
67
68                    if (strncmp(key_name, n, keylen) == 0 && n[keylen] == '/') { // found a subkey -> do not delete
69                        found = true;
70                        break;
71                    }
72                }
73            }
74
75            if (!found) {       // key no longer exists = > delete from key list
76                GB_delete(gb_key);
77            }
78        }
79
80        gb_key = gb_next_key;
81    }
82}
83
84static void show_all_changekeys(GBDATA *gb_main, const char *change_key_path) {
85    GBDATA *gb_key_data = GB_search(gb_main, change_key_path, GB_CREATE_CONTAINER);
86    for (GBDATA *gb_key = gb_key_data ? GB_entry(gb_key_data, CHANGEKEY) : NULp;
87         gb_key;
88         gb_key = GB_nextEntry(gb_key))
89    {
90        GBDATA *gb_key_hidden = GB_entry(gb_key, CHANGEKEY_HIDDEN);
91        if (gb_key_hidden) {
92            if (GB_read_int(gb_key_hidden)) GB_write_int(gb_key_hidden, 0); // unhide
93        }
94    }
95}
96
97void species_field_selection_list_rescan(GBDATA *gb_main, RescanMode mode) {
98    GB_push_transaction(gb_main);
99    GBDATA *gb_species_data = GBT_get_species_data(gb_main);
100
101    StrArray names;
102    GBT_scan_db(names, gb_species_data, NULp);
103
104    if (mode & RESCAN_REFRESH)  delete_unused_changekeys(gb_main, names, CHANGE_KEY_PATH);
105    if (mode & RESCAN_SHOW_ALL) show_all_changekeys(gb_main, CHANGE_KEY_PATH);
106
107    if (mode & RESCAN_REFRESH) {
108        GBT_add_new_changekey(gb_main, "name", GB_STRING);
109        GBT_add_new_changekey(gb_main, "acc", GB_STRING);
110        GBT_add_new_changekey(gb_main, "full_name", GB_STRING);
111        GBT_add_new_changekey(gb_main, "tmp", GB_STRING);
112
113        const long bitfilter = FIELD_FILTER_STRING_READABLE;
114        for (int i = 0; names[i]; ++i) {
115            const char *name = names[i];
116            if ((1<<name[0]) & bitfilter) {         // look if already exists
117                if (!is_in_reserved_path(name+1)) { // ignore gene, experiment, ... entries
118                    GBT_add_new_changekey(gb_main, name+1, (int)(name[0]));
119                }
120            }
121        }
122    }
123
124    GB_pop_transaction(gb_main);
125}
126
127
128
129void gene_field_selection_list_rescan(GBDATA *gb_main, RescanMode mode) {
130    GB_push_transaction(gb_main);
131    GBDATA *gb_species_data = GBT_get_species_data(gb_main);
132
133    StrArray names;
134    GBT_scan_db(names, gb_species_data, GENE_DATA_PATH);
135
136    if (mode & RESCAN_REFRESH)  delete_unused_changekeys(gb_main, names, CHANGE_KEY_PATH_GENES);
137    if (mode & RESCAN_SHOW_ALL) show_all_changekeys(gb_main, CHANGE_KEY_PATH_GENES);
138
139    if (mode & RESCAN_REFRESH) {
140        GBT_add_new_gene_changekey(gb_main, "name", GB_STRING);
141
142        GBT_add_new_gene_changekey(gb_main, "pos_start", GB_STRING);
143        GBT_add_new_gene_changekey(gb_main, "pos_stop", GB_STRING);
144        GBT_add_new_gene_changekey(gb_main, "pos_complement", GB_STRING);
145
146        GBT_add_new_gene_changekey(gb_main, "pos_joined", GB_INT);
147        GBT_add_new_gene_changekey(gb_main, "pos_certain", GB_STRING);
148
149        const long bitfilter = FIELD_FILTER_STRING_READABLE;
150        for (int i = 0; names[i]; ++i) {
151            const char *name = names[i];
152            if ((1<<name[0]) & bitfilter) {            // look if already exists
153                GBT_add_new_gene_changekey(gb_main, name+1, (int)(name[0]));
154            }
155        }
156    }
157
158    GB_pop_transaction(gb_main);
159}
160
161static void experiment_field_selection_list_rescan(GBDATA *gb_main, RescanMode mode) {
162    GB_push_transaction(gb_main);
163    GBDATA *gb_species_data = GBT_get_species_data(gb_main);
164
165    StrArray names;
166    GBT_scan_db(names, gb_species_data, EXPERIMENT_DATA_PATH);
167
168    if (mode & RESCAN_REFRESH)  delete_unused_changekeys(gb_main, names, CHANGE_KEY_PATH_EXPERIMENTS);
169    if (mode & RESCAN_SHOW_ALL) show_all_changekeys(gb_main, CHANGE_KEY_PATH_EXPERIMENTS);
170
171    if (mode & RESCAN_REFRESH) {
172        GBT_add_new_experiment_changekey(gb_main, "name", GB_STRING);
173
174        const long bitfilter = FIELD_FILTER_STRING_READABLE;
175        for (int i = 0; names[i]; ++i) {
176            const char *name = names[i];
177            if ((1<<name[0]) & bitfilter) {   // look if already exists
178                if (is_in_EXPERIMENT_path(name+1)) {
179                    GBT_add_new_experiment_changekey(gb_main, name+1+EXPERIMENT_DATA_PATH_LEN, (int)(name[0]));
180                }
181            }
182        }
183    }
184
185    GB_pop_transaction(gb_main);
186}
187
188void species_field_selection_list_unhide_all_cb(AW_window *, GBDATA *gb_main) { species_field_selection_list_rescan(gb_main, RESCAN_SHOW_ALL); }
189void species_field_selection_list_update_cb    (AW_window *, GBDATA *gb_main) { species_field_selection_list_rescan(gb_main, RESCAN_REFRESH);  }
190
191void gene_field_selection_list_unhide_all_cb(AW_window *, GBDATA *gb_main) { gene_field_selection_list_rescan(gb_main, RESCAN_SHOW_ALL); }
192void gene_field_selection_list_update_cb    (AW_window *, GBDATA *gb_main) { gene_field_selection_list_rescan(gb_main, RESCAN_REFRESH);  }
193
194void experiment_field_selection_list_unhide_all_cb(AW_window *, GBDATA *gb_main) { experiment_field_selection_list_rescan(gb_main, RESCAN_SHOW_ALL); }
195void experiment_field_selection_list_update_cb    (AW_window *, GBDATA *gb_main) { experiment_field_selection_list_rescan(gb_main, RESCAN_REFRESH);  }
Note: See TracBrowser for help on using the repository browser.