source: tags/ms_r17q1/NTREE/NT_dbrepair.cxx

Last change on this file was 15640, checked in by westram, 7 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.8 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : NT_dbrepair.cxx                                   //
4//   Purpose   : repair database bugs                              //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in May 2008       //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include "NT_local.h"
13
14#include <arbdbt.h>
15#include <adGene.h>
16
17#include <items.h>
18#include <GEN.hxx>
19#include <EXP.hxx>
20#include <aw_msg.hxx>
21#include <arb_progress.h>
22#include <aw_question.hxx>
23
24#include <arb_str.h>
25#include <arb_strarray.h>
26
27#include <map>
28#include <set>
29#include <string>
30#include <vector>
31#include <ad_colorset.h>
32
33using namespace std;
34
35#if defined(WARN_TODO)
36#warning the whole fix mechanism should be part of some lower-level-library
37// meanwhile DB checks are only performed by ARB_NTREE
38// ItemSelector should go to same library as this module
39#endif
40
41// --------------------------------------------------------------------------------
42// CheckedConsistencies provides an easy way to automatically correct flues in the database
43// by calling a check routine exactly once.
44//
45// For an example see nt_check_database_consistency()
46//
47// Note: this makes problems if DB is loaded with older ARB version and some already
48// fixed flues a put into DB again.
49// see http://bugs.arb-home.de/ticket/143
50
51typedef GB_ERROR (*item_check_fun)(GBDATA *gb_item, ItemSelector& sel);
52
53typedef map<string, item_check_fun>    item_check_map;
54typedef item_check_map::const_iterator item_check_iter;
55
56class CheckedConsistencies : virtual Noncopyable {
57    GBDATA         *gb_main;
58    size_t          species_count;
59    size_t          sai_count;
60    set<string>     consistencies;
61    item_check_map  item_checks;
62
63    GB_ERROR perform_selected_item_checks(ItemSelector& sel);
64
65public:
66
67    CheckedConsistencies(GBDATA *gb_main_) : gb_main(gb_main_) {
68        GB_transaction ta(gb_main);
69        GBDATA *gb_checks = GB_search(gb_main, "checks", GB_CREATE_CONTAINER);
70
71        for (GBDATA *gb_check = GB_entry(gb_checks, "check"); gb_check; gb_check = GB_nextEntry(gb_check)) {
72            consistencies.insert(GB_read_char_pntr(gb_check));
73        }
74
75        species_count = GBT_get_species_count(gb_main);
76        sai_count = GBT_get_SAI_count(gb_main);
77    }
78
79    bool was_performed(const string& check_name) const {
80        return consistencies.find(check_name) != consistencies.end();
81    }
82
83    GB_ERROR register_as_performed(const string& check_name) {
84        GB_ERROR error = 0;
85        if (was_performed(check_name)) {
86            printf("check '%s' already has been registered before. Duplicated check name?\n", check_name.c_str());
87        }
88        else {
89            GB_transaction ta(gb_main);
90
91            GBDATA *gb_checks = GB_search(gb_main, "checks", GB_CREATE_CONTAINER);
92            GBDATA *gb_check  = GB_create(gb_checks, "check", GB_STRING);
93
94            if (!gb_check) error = GB_await_error();
95            else error           = GB_write_string(gb_check, check_name.c_str());
96
97            if (!error) consistencies.insert(check_name);
98        }
99        return error;
100    }
101
102    void perform_check(const string& check_name,
103                       GB_ERROR (*do_check)(GBDATA *gb_main, size_t species, size_t sais),
104                       GB_ERROR& error)
105    {
106        if (!error && !was_performed(check_name)) {
107            arb_progress progress(check_name.c_str());
108            error = do_check(gb_main, species_count, sai_count);
109            if (!error) register_as_performed(check_name);
110        }
111    }
112
113    void register_item_check(const string& check_name, item_check_fun item_check) {
114        if (!was_performed(check_name)) {
115            item_checks[check_name] = item_check;
116        }
117    }
118
119    void perform_item_checks(GB_ERROR& error);
120
121    GB_ERROR forgetDoneChecks() {
122        GB_ERROR       error = 0;
123        GB_transaction ta(gb_main);
124
125        GBDATA *gb_checks = GB_search(gb_main, "checks", GB_CREATE_CONTAINER);
126        for (GBDATA *gb_check = GB_entry(gb_checks, "check"); gb_check && !error; gb_check = GB_nextEntry(gb_check)) {
127            char *check_name = GB_read_string(gb_check);
128
129#if defined(DEBUG)
130            printf("Deleting check '%s'\n", check_name);
131#endif // DEBUG
132            error = GB_delete(gb_check);
133            consistencies.erase(check_name);
134            free(check_name);
135        }
136        return error;
137    }
138};
139
140GB_ERROR CheckedConsistencies::perform_selected_item_checks(ItemSelector& sel) {
141    GB_ERROR        error = NULL;
142    item_check_iter end   = item_checks.end();
143
144    for (GBDATA *gb_cont = sel.get_first_item_container(gb_main, NULL, QUERY_ALL_ITEMS);
145         gb_cont && !error;
146         gb_cont = sel.get_next_item_container(gb_cont, QUERY_ALL_ITEMS))
147    {
148        for (GBDATA *gb_item = sel.get_first_item(gb_cont, QUERY_ALL_ITEMS);
149             gb_item && !error;
150             gb_item = sel.get_next_item(gb_item, QUERY_ALL_ITEMS))
151        {
152            for (item_check_iter chk = item_checks.begin(); chk != end && !error; ++chk) {
153                error = chk->second(gb_item, sel);
154            }
155        }
156    }
157
158    return error;
159}
160
161void CheckedConsistencies::perform_item_checks(GB_ERROR& error) {
162    if (!item_checks.empty()) {
163        if (!error) {
164            GB_transaction ta(gb_main);
165            bool           is_genome_db = GEN_is_genome_db(gb_main, -1);
166
167            error = perform_selected_item_checks(SPECIES_get_selector());
168            if (!error && is_genome_db) {
169                error             = perform_selected_item_checks(GEN_get_selector());
170                if (!error) error = perform_selected_item_checks(EXP_get_selector());
171            }
172
173            error = ta.close(error);
174        }
175
176        if (!error) {
177            item_check_iter end = item_checks.end();
178            for (item_check_iter chk = item_checks.begin(); chk != end && !error; ++chk) {
179                error = register_as_performed(chk->first);
180            }
181
182            if (!error) item_checks.clear();
183        }
184    }
185}
186
187// --------------------------------------------------------------------------------
188
189static GB_ERROR NT_fix_gene_data(GBDATA *gb_main, size_t species_count, size_t /* sai_count */) {
190    GB_transaction ta(gb_main);
191    arb_progress   progress(species_count);
192
193    size_t   deleted_gene_datas   = 0;
194    size_t   generated_gene_datas = 0;
195    GB_ERROR error                = 0;
196
197    for (GBDATA *gb_species = GBT_first_species(gb_main);
198         gb_species && !error;
199         gb_species = GBT_next_species(gb_species))
200    {
201        bool    is_organism  = (GB_entry(gb_species, GENOM_ALIGNMENT) != 0); // same test as GEN_is_organism, but w/o genome-db-assertion
202        GBDATA *gb_gene_data = GEN_find_gene_data(gb_species);
203
204        if (is_organism && !gb_gene_data) {
205            gb_gene_data = GEN_findOrCreate_gene_data(gb_species); // @@@ check result & handle error
206            generated_gene_datas++;
207        }
208        else if (!is_organism && gb_gene_data) {
209            GBDATA *gb_child = GB_child(gb_gene_data);
210            if (!gb_child) {
211                error = GB_delete(gb_gene_data);
212                if (!error) deleted_gene_datas++;
213            }
214            else {
215                error = GBS_global_string("Non-empty 'gene_data' found for species '%s',\n"
216                                          "which has no alignment '" GENOM_ALIGNMENT "',\n"
217                                          "i.e. which is not regarded as full-genome organism.\n"
218                                          "This causes problems - please fix!",
219                                          GBT_read_name(gb_species));
220            }
221        }
222
223        progress.inc_and_check_user_abort(error);
224    }
225
226    if (!error) {
227        if (deleted_gene_datas) {
228            aw_message(GBS_global_string("Deleted %zu useless empty 'gene_data' entries.", deleted_gene_datas));
229        }
230        if (generated_gene_datas) {
231            aw_message(GBS_global_string("Re-created %zu missing 'gene_data' entries.\nThese organisms have no genes yet!", generated_gene_datas));
232        }
233    }
234    return ta.close(error);
235}
236
237// --------------------------------------------------------------------------------
238
239static GBDATA *expectField(GBDATA *gb_gene, const char *field, GB_ERROR& data_error) {
240    GBDATA *gb_field = 0;
241    if (!data_error) {
242        gb_field = GB_entry(gb_gene, field);
243        if (!gb_field) data_error = GBS_global_string("Expected field '%s' missing", field);
244    }
245    return gb_field;
246}
247
248static GBDATA *disexpectField(GBDATA *gb_gene, const char *field, GB_ERROR& data_error) {
249    GBDATA *gb_field = 0;
250    if (!data_error) {
251        gb_field = GB_entry(gb_gene, field);
252        if (gb_field) data_error = GBS_global_string("Unexpected field '%s' exists (wrong value in pos_joined?)", field);
253    }
254    GBS_reuse_buffer(field);
255    return gb_field;
256}
257
258static GB_ERROR NT_convert_gene_locations(GBDATA *gb_main, size_t species_count, size_t /* sai_count */) {
259    GB_transaction ta(gb_main);
260    GB_ERROR       error         = 0;
261    long           fixed_genes   = 0;
262    long           skipped_genes = 0;
263    long           genes         = 0;
264
265    typedef vector<GBDATA*> GBvec;
266    GBvec                   toDelete;
267
268    arb_progress progress(species_count);
269   
270    for (GBDATA *gb_organism = GEN_first_organism(gb_main);
271         gb_organism && !error;
272         gb_organism = GEN_next_organism(gb_organism))
273    {
274        GBDATA *gb_gene_data = GEN_find_gene_data(gb_organism);
275        nt_assert(gb_gene_data);
276        if (gb_gene_data) {
277            for (GBDATA *gb_gene = GEN_first_gene_rel_gene_data(gb_gene_data);
278                 gb_gene && !error;
279                 gb_gene = GEN_next_gene(gb_gene))
280            {
281                genes++;
282
283                int parts = 1;
284                {
285                    GBDATA *gb_pos_joined    = GB_entry(gb_gene, "pos_joined");
286                    if (gb_pos_joined) parts = GB_read_int(gb_pos_joined); // its a joined gene
287                }
288
289                GBDATA *gb_pos_start = GB_entry(gb_gene, "pos_start"); // test for new format
290                if (!gb_pos_start) {
291                    GBDATA *gb_pos_begin = GB_entry(gb_gene, "pos_begin"); // test for old format
292                    if (!gb_pos_begin) {
293                        error = "Neither 'pos_begin' nor 'pos_start' found - format of gene location is unknown";
294                    }
295                }
296
297                if (!gb_pos_start && !error) { // assume old format
298                    // parts<-1 would be valid in new format, but here we have old format
299                    if (parts<1) error = GBS_global_string("Illegal value in 'pos_joined' (%i)", parts);
300
301                    GB_ERROR      data_error = 0;   // error in this gene -> don't convert
302                    GEN_position *pos        = GEN_new_position(parts, false); // all were joinable (no information about it was stored)
303
304                    // parse old gene information into 'pos'
305                    //
306                    // old-format was:
307                    // Start-Positions:  pos_begin, pos_begin2, pos_begin3, ...
308                    // End-Positions:    pos_end, pos_end2, pos_end3, ...
309                    // Joined?:          pos_joined (always >= 1)
310                    // Complement:       complement (one entry for all parts)
311                    // Certainty:        pos_uncertain (maybe pos_uncertain1 etc.)
312
313                    int complement = 0;
314                    {
315                        GBDATA *gb_complement = GB_entry(gb_gene, "complement");
316                        if (gb_complement) {
317                            complement = GB_read_byte(gb_complement);
318                            toDelete.push_back(gb_complement);
319                        }
320                    }
321
322                    bool has_uncertain_fields = false;
323                    for (int p = 1; p <= parts && !error && !data_error; ++p) {
324                        GBDATA     *gb_pos_begin        = 0;
325                        GBDATA     *gb_pos_end          = 0;
326                        const char *pos_uncertain_field = 0;
327
328                        if (p == 1) {
329                            gb_pos_begin = expectField(gb_gene, "pos_begin", data_error);
330                            gb_pos_end   = expectField(gb_gene, "pos_end", data_error);
331
332                            pos_uncertain_field = "pos_uncertain";
333                        }
334                        else {
335                            const char *pos_begin_field = GBS_global_string("pos_begin%i", p);
336                            const char *pos_end_field   = GBS_global_string("pos_end%i", p);
337
338                            gb_pos_begin = expectField(gb_gene, pos_begin_field, data_error);
339                            gb_pos_end   = expectField(gb_gene, pos_end_field, data_error);
340
341                            GBS_reuse_buffer(pos_end_field);
342                            GBS_reuse_buffer(pos_begin_field);
343
344                            if (!data_error) pos_uncertain_field = GBS_global_string("pos_uncertain%i", p);
345                        }
346
347                        int pospos = complement ? (parts-p) : (p-1);
348
349                        if (!data_error) {
350                            GBDATA *gb_pos_uncertain = GB_entry(gb_gene, pos_uncertain_field);
351
352                            if (!gb_pos_uncertain) {
353                                if (has_uncertain_fields) data_error = GBS_global_string("Expected field '%s' missing", pos_uncertain_field);
354                            }
355                            else {
356                                if (p == 1) has_uncertain_fields = true;
357                                else {
358                                    if (!has_uncertain_fields) {
359                                        data_error = GBS_global_string("Found '%s' as first certainty-information", pos_uncertain_field);
360                                    }
361                                }
362                            }
363
364                            if (!data_error) {
365                                int begin = GB_read_int(gb_pos_begin);
366                                int end   = GB_read_int(gb_pos_end);
367
368                                pos->start_pos[pospos]  = begin;
369                                pos->stop_pos[pospos]   = end;
370                                pos->complement[pospos] = complement; // set all complement entries to same value (old format only had one complement entry)
371
372                                if (gb_pos_uncertain) {
373                                    const char *uncertain = GB_read_char_pntr(gb_pos_uncertain);
374
375                                    if (!uncertain) error = GB_await_error();
376                                    else {
377                                        if (!pos->start_uncertain) GEN_use_uncertainties(pos);
378
379                                        if (strlen(uncertain) != 2) {
380                                            data_error = "wrong length";
381                                        }
382                                        else {
383                                            for (int up = 0; up<2; up++) {
384                                                if (strchr("<=>", uncertain[up]) == 0) {
385                                                    data_error = GBS_global_string("illegal character '%c'", uncertain[up]);
386                                                }
387                                                else {
388                                                    (up == 0 ? pos->start_uncertain[pospos] : pos->stop_uncertain[pospos]) = uncertain[up];
389                                                }
390                                            }
391                                        }
392
393
394                                        toDelete.push_back(gb_pos_uncertain);
395                                    }
396                                }
397
398                                toDelete.push_back(gb_pos_begin);
399                                toDelete.push_back(gb_pos_end);
400                            }
401                        }
402                    }
403
404                    for (int p = parts+1; p <= parts+4 && !error && !data_error; ++p) {
405                        disexpectField(gb_gene, GBS_global_string("pos_begin%i", p), data_error);
406                        disexpectField(gb_gene, GBS_global_string("pos_end%i", p), data_error);
407                        disexpectField(gb_gene, GBS_global_string("complement%i", p), data_error);
408                        disexpectField(gb_gene, GBS_global_string("pos_uncertain%i", p), data_error);
409                    }
410
411                    // now save new position data
412
413                    if (data_error) {
414                        skipped_genes++;
415                    }
416                    else if (!error) {
417                        error = GEN_write_position(gb_gene, pos, 0);
418
419                        if (!error) {
420                            // delete old-format entries
421                            GBvec::const_iterator end = toDelete.end();
422                            for (GBvec::const_iterator i = toDelete.begin(); i != end && !error; ++i) {
423                                GBDATA *gb_del = *i;
424                                error = GB_delete(gb_del);
425                            }
426
427                            if (!error) fixed_genes++;
428                        }
429                    }
430
431                    toDelete.clear();
432                    GEN_free_position(pos);
433
434                    if (data_error || error) {
435                        char *gene_id = GEN_global_gene_identifier(gb_gene, gb_organism);
436                        if (error) {
437                            error = GBS_global_string("Gene '%s': %s", gene_id, error);
438                        }
439                        else {
440                            aw_message(GBS_global_string("Gene '%s' was not converted, fix data manually!\nReason: %s", gene_id, data_error));
441                        }
442                        free(gene_id);
443                    }
444                }
445            }
446        }
447
448        progress.inc_and_check_user_abort(error);
449    }
450
451    if (!error) {
452        if (fixed_genes>0) aw_message(GBS_global_string("Fixed location entries of %li genes.", fixed_genes));
453        if (skipped_genes>0) {
454            aw_message(GBS_global_string("Didn't fix location entries of %li genes (see warnings).", skipped_genes));
455            error = "Not all gene locations were fixed.\nFix manually, save DB and restart ARB with that DB.\nMake sure you have a backup copy of the original DB!";
456        }
457
458        if (fixed_genes || skipped_genes) {
459            long already_fixed_genes = genes-(fixed_genes+skipped_genes);
460            if (already_fixed_genes>0) aw_message(GBS_global_string("Location entries of %li genes already were in new format.", already_fixed_genes));
461        }
462    }
463
464    return error;
465}
466
467
468// --------------------------------------------------------------------------------
469
470static GB_ERROR NT_del_mark_move_REF(GBDATA *gb_main, size_t species_count, size_t sai_count) {
471    GB_transaction ta(gb_main);
472    GB_ERROR       error   = 0;
473    size_t         all     = species_count+sai_count;
474    size_t         removed = 0;
475
476    // delete 'mark' entries from all alignments of species/SAIs
477
478    arb_progress  progress(all);
479    ConstStrArray ali_names;
480    GBT_get_alignment_names(ali_names, gb_main);
481
482    for (int pass = 0; pass < 2 && !error; ++pass) {
483        for (GBDATA *gb_item = (pass == 0) ? GBT_first_species(gb_main) : GBT_first_SAI(gb_main);
484             gb_item && !error;
485             gb_item = (pass == 0) ? GBT_next_species(gb_item) : GBT_next_SAI(gb_item))
486        {
487            for (int ali = 0; ali_names[ali] && !error; ++ali) {
488                GBDATA *gb_ali = GB_entry(gb_item, ali_names[ali]);
489                if (gb_ali) {
490                    GBDATA *gb_mark = GB_entry(gb_ali, "mark");
491                    if (gb_mark) {
492                        error = GB_delete(gb_mark);
493                        removed++;
494                    }
495                }
496            }
497
498            progress.inc_and_check_user_abort(error);
499        }
500    }
501
502    {
503        char   *helix_name = GBT_get_default_helix(gb_main);
504        GBDATA *gb_helix   = GBT_find_SAI(gb_main, helix_name);
505
506        if (gb_helix) {
507            for (int ali = 0; ali_names[ali] && !error; ++ali) {
508                GBDATA *gb_ali     = GB_entry(gb_helix, ali_names[ali]);
509                GBDATA *gb_old_ref = GB_entry(gb_ali, "REF");
510                GBDATA *gb_new_ref = GB_entry(gb_ali, "_REF");
511
512                if (gb_old_ref) {
513                    if (gb_new_ref) {
514                        error = GBS_global_string("SAI:%s has 'REF' and '_REF' in '%s' (data corrupt?!)",
515                                                  helix_name, ali_names[ali]);
516                    }
517                    else { // move info from REF -> _REF
518                        char *content       = GB_read_string(gb_old_ref);
519                        if (!content) error = GB_await_error();
520                        else {
521                            gb_new_ref             = GB_create(gb_ali, "_REF", GB_STRING);
522                            if (!gb_new_ref) error = GB_await_error();
523                            else {
524                                error = GB_write_string(gb_new_ref, content);
525                                if (!error) error = GB_delete(gb_old_ref);
526                            }
527                            free(content);
528                        }
529                    }
530                }
531            }
532        }
533
534        free(helix_name);
535    }
536
537    if (!error) {
538        if (removed) {
539            aw_message(GBS_global_string("Deleted %zu useless 'mark' entries.", removed));
540        }
541    }
542
543    return ta.close(error);
544}
545
546// --------------------------------------------------------------------------------
547
548static bool testDictionaryCompression(GBDATA *gbd, GBQUARK key_quark, bool testUse) {
549    // returns true, if
550    // testUse == true  and ANY entries below 'gbd' with quark 'key_quark' uses dictionary compression
551    // testUse == false and ALL entries below 'gbd' with quark 'key_quark' can be decompressed w/o errors
552
553    nt_assert(GB_read_type(gbd) == GB_DB);
554
555    for (GBDATA *gb_sub = GB_child(gbd); gb_sub; gb_sub = GB_nextChild(gb_sub)) {
556        switch (GB_read_type(gb_sub)) {
557            case GB_DB:
558                // return false if any compression failed or return true if any uses dict-compression
559                if (testDictionaryCompression(gb_sub, key_quark, testUse) == testUse) return testUse;
560                break;
561
562            case GB_STRING:
563                if (GB_get_quark(gb_sub) == key_quark && GB_is_dictionary_compressed(gb_sub)) {
564                    if (testUse) return true;
565
566                    const char *decompressed = GB_read_char_pntr(gb_sub);
567                    if (!decompressed) return false;
568                }
569                break;
570
571            default:
572                break;
573        }
574    }
575
576    return !testUse;
577}
578
579class                  Dict;
580typedef SmartPtr<Dict> DictPtr;
581
582
583class KeyInfo : virtual Noncopyable {
584    string  name;               // keyname
585    DictPtr original;
586
587    bool compressionTested;
588    bool compressed;
589
590    void init() {
591        compressionTested = false;
592        compressed        = false;
593    }
594
595public:
596    KeyInfo(const char *Name)                       : name(Name)                         { init(); }
597    KeyInfo(const char *Name, DictPtr originalDict) : name(Name), original(originalDict) { init(); }
598
599    void testCompressed(GBDATA *gb_main) {
600        nt_assert(!compressionTested);
601        compressed        = testDictionaryCompression(gb_main, GB_find_or_create_quark(gb_main, name.c_str()), true);
602        compressionTested = true;
603    }
604
605    const string& getName() const { return name; }
606
607    bool isCompressed() const {
608        nt_assert(compressionTested);
609        return compressed;
610    }
611};
612
613
614class Dict : virtual Noncopyable {
615    string    group;            // lowercase keyname
616    string    orgkey;
617    DictData *data;
618
619    map<string, bool> decompressWorks; // key -> bool
620
621public:
622    static GBDATA *gb_main;
623
624    Dict(const char *Group, const char *OrgKey, DictData *Data) : group(Group), orgkey(OrgKey), data(Data) {}
625
626    const string& getGroup() const { return group; }
627    const string& getOriginalKey() const { return orgkey; }
628
629    bool mayBeUsedWith(const string& key) const { return strcasecmp(group.c_str(), key.c_str()) == 0; }
630
631    GB_ERROR assignToKey(const string& key) const { return GB_set_dictionary(gb_main, key.c_str(), data); }
632    GB_ERROR unassignFromKey(const string& key) const { return GB_set_dictionary(gb_main, key.c_str(), NULL); }
633
634    bool canDecompress(const string& key) {
635        nt_assert(mayBeUsedWith(key));
636        if (decompressWorks.find(key) == decompressWorks.end()) {
637            bool     works = false;
638            GB_ERROR error = assignToKey(key);
639
640            if (!error) works    = testDictionaryCompression(gb_main, GB_find_or_create_quark(gb_main, key.c_str()), false);
641            decompressWorks[key] = works;
642
643            GB_ERROR err2 = unassignFromKey(key);
644            if (err2) {
645                aw_message(GBS_global_string("Error while removing @dictionary from key '%s': %s", key.c_str(), err2));
646            }
647        }
648        return decompressWorks[key];
649    }
650};
651GBDATA *Dict::gb_main = NULL;
652
653
654typedef map<string, int>        KeyCounter; // groupname -> occur count
655typedef SmartPtr<KeyInfo>       KeyInfoPtr;
656typedef map<string, KeyInfoPtr> Keys; // keyname -> info
657typedef map<string, DictPtr>    DictMap;
658typedef vector<DictPtr>         Dicts;
659typedef set<string>             StringSet;
660
661#define STATUS_PREFIX "Dictionary: "
662
663template<typename CONT, typename KEY>
664bool contains(const CONT& container, const KEY& key) {
665    return container.find(key) != container.end();
666}
667
668static GB_ERROR findAffectedKeys(GBDATA *gb_key_data, KeyCounter& kcount, Keys& keys, Dicts& dicts) {
669    GB_ERROR  error   = 0;
670    GBDATA   *gb_main = GB_get_root(gb_key_data);
671
672    for (int pass = 1; pass <= 2; ++pass) {
673        for (GBDATA *gb_key = GB_entry(gb_key_data, "@key"); !error && gb_key; gb_key = GB_nextEntry(gb_key)) {
674            GBDATA     *gb_name = GB_entry(gb_key, "@name");
675            const char *keyName = GB_read_char_pntr(gb_name);
676
677            if (!keyName) {
678                error = GBS_global_string("@key w/o @name (%s)", GB_await_error());
679            }
680            else {
681                char *keyGroup = ARB_strdup(keyName);
682                ARB_strlower(keyGroup);
683
684                switch (pass) {
685                    case 1:
686                        kcount[keyGroup]++;
687                        break;
688                    case 2:
689                        if (kcount[keyGroup]>1) {
690                            GBDATA *gb_dictionary = GB_entry(gb_key, "@dictionary");
691                            if (gb_dictionary) {
692                                DictPtr dict  = new Dict(keyGroup, keyName, GB_get_dictionary(gb_main, keyName));
693                                keys[keyName] = new KeyInfo(keyName, dict);
694                                dicts.push_back(dict);
695                            }
696                            else keys[keyName] = new KeyInfo(keyName);
697                        }
698                        else kcount.erase(keyGroup);
699                        break;
700                }
701                free(keyGroup);
702            }
703        }
704    }
705    return error;
706}
707
708static GB_ERROR deleteDataOfKey(GBDATA *gbd, GBQUARK key_quark, StringSet& deletedData, long& deleted, long& notDeleted) {
709    GB_ERROR error = 0;
710    for (GBDATA *gb_sub = GB_child(gbd); gb_sub; gb_sub = GB_nextChild(gb_sub)) {
711        switch (GB_read_type(gb_sub)) {
712            case GB_DB:
713                error = deleteDataOfKey(gb_sub, key_quark, deletedData, deleted, notDeleted);
714                break;
715
716            case GB_STRING:
717                if (GB_get_quark(gb_sub) == key_quark) {
718                    if (GB_is_dictionary_compressed(gb_sub)) {
719                        string path(GB_get_db_path(gb_sub));
720                        error = GB_delete(gb_sub);
721                        if (!error) {
722                            deletedData.insert(path);
723                            deleted++;
724                        }
725                    }
726                    else {
727                        notDeleted++;
728                    }
729                }
730                break;
731            default:
732                break;
733        }
734    }
735    return error;
736}
737
738static char *readFirstCompressedDataOf(GBDATA *gbd, GBQUARK key_quark) {
739    char *data = 0;
740    for (GBDATA *gb_sub = GB_child(gbd); !data && gb_sub; gb_sub = GB_nextChild(gb_sub)) {
741        switch (GB_read_type(gb_sub)) {
742            case GB_DB:
743                data = readFirstCompressedDataOf(gb_sub, key_quark);
744                break;
745
746            case GB_STRING:
747                if (GB_get_quark(gb_sub) == key_quark) {
748                    if (GB_is_dictionary_compressed(gb_sub)) {
749                        data = GB_read_as_string(gb_sub);
750                    }
751                }
752                break;
753            default:
754                break;
755        }
756    }
757    return data;
758}
759
760
761static GB_ERROR NT_fix_dict_compress(GBDATA *gb_main, size_t, size_t) {
762    GB_transaction  ta(gb_main);
763    GBDATA         *gb_key_data = GB_search(gb_main, GB_SYSTEM_FOLDER "/" GB_SYSTEM_KEY_DATA, GB_FIND);
764    GB_ERROR        error       = 0;
765
766    Dict::gb_main = gb_main;
767
768    if (!gb_key_data) {
769        error = "No " GB_SYSTEM_KEY_DATA " found.. DB corrupted?";
770    }
771    else {
772        KeyCounter kcount;      // strlwr(keyname) -> count
773        Keys       keys;
774        Dicts      dicts;
775
776        error = findAffectedKeys(gb_key_data, kcount, keys, dicts);
777
778        // count affected keys
779        int affectedKeys = 0;
780        for (KeyCounter::iterator kci = kcount.begin(); kci != kcount.end(); ++kci) {
781            affectedKeys += kci->second;
782        }
783
784        if (!error && affectedKeys>0) {
785            // check which keys are compressed
786
787            {
788                arb_progress progress(STATUS_PREFIX "search compressed data", affectedKeys);
789
790                for (Keys::iterator ki = keys.begin(); ki != keys.end(); ++ki) {
791                    KeyInfoPtr k = ki->second;
792                    k->testCompressed(gb_main);
793                    ++progress;
794                }
795            }
796
797            // test which key/dict combinations work
798            int combinations = 0;          // possible key/dict combinations
799
800            DictMap   use;      // keyname -> dictionary (which dictionary to use)
801            StringSet multiDecompressible; // keys which can be decompressed with multiple dictionaries
802
803            for (int pass = 1; pass <= 2; ++pass) {
804                arb_progress *progress  = NULL;
805                if (pass == 2 && combinations) progress = new arb_progress(STATUS_PREFIX "test compression", combinations);
806
807                for (Dicts::iterator di = dicts.begin(); di != dicts.end(); ++di) {
808                    DictPtr d = *di;
809
810                    for (Keys::iterator ki = keys.begin(); ki != keys.end(); ++ki) {
811                        KeyInfoPtr    k       = ki->second;
812                        const string& keyname = k->getName();
813
814                        if (k->isCompressed() && d->mayBeUsedWith(keyname)) {
815                            switch (pass) {
816                                case 1:
817                                    combinations++;
818                                    break;
819                                case 2:
820                                    if (d->canDecompress(keyname)) {
821                                        if (!contains(use, keyname)) { // first dictionary working with keyname
822                                            use[keyname] = d;
823                                        }
824                                        else { // already have another dictionary working with keyname
825                                            multiDecompressible.insert(keyname);
826                                        }
827                                    }
828                                    ++(*progress);
829                                    break;
830                            }
831                        }
832                    }
833                }
834                delete progress;
835            }
836
837            StringSet notDecompressible; // keys which can be decompressed with none of the dictionaries
838            for (Keys::iterator ki = keys.begin(); ki != keys.end(); ++ki) {
839                KeyInfoPtr    k       = ki->second;
840                const string& keyname = k->getName();
841
842                if (k->isCompressed()) {
843                    if (!contains(use, keyname)) notDecompressible.insert(keyname);
844                    if (contains(multiDecompressible, keyname)) use.erase(keyname);
845                }
846            }
847
848            bool dataLost   = false;
849            int  reassigned = 0;
850
851            if (!notDecompressible.empty()) {
852                // bad .. found undecompressible data
853                int nd_count = notDecompressible.size();
854                aw_message(GBS_global_string("Detected corrupted dictionary compression\n"
855                                             "Data of %i DB-keys is lost and will be deleted", nd_count));
856
857                arb_progress progress(STATUS_PREFIX "deleting corrupt data", nd_count);
858
859                StringSet deletedData;
860                long      deleted    = 0;
861                long      notDeleted = 0;
862
863                for (StringSet::iterator ki = notDecompressible.begin(); !error && ki != notDecompressible.end(); ++ki) {
864                    const string& keyname    = *ki;
865
866                    error = deleteDataOfKey(gb_main, GB_find_or_create_quark(gb_main, keyname.c_str()), deletedData, deleted, notDeleted);
867                    ++progress;
868                }
869
870                if (!error) {
871                    nt_assert(deleted); // at least 1 db-entry should have been deleted
872
873                    aw_message(GBS_global_string("Deleted %li of %li affected DB-entries", deleted, deleted+notDeleted));
874                    aw_message("see console for a list of affected keys");
875
876                    printf("Deleted keys:\n");
877                    for (StringSet::iterator di = deletedData.begin(); di != deletedData.end(); ++di) {
878                        printf("* %s\n", di->c_str());
879                    }
880                }
881            }
882
883            if (!error && !multiDecompressible.empty()) {
884                for (StringSet::iterator ki = multiDecompressible.begin(); !error && ki != multiDecompressible.end(); ++ki) {
885                    const string&   keyname  = *ki;
886                    int             possible = 0;
887                    vector<DictPtr> possibleDicts;
888
889                    printf("--------------------------------------------------------------------------------\n");
890
891                    for (Dicts::iterator di = dicts.begin(); !error && di != dicts.end(); ++di) {
892                        DictPtr d = *di;
893                        if (d->mayBeUsedWith(keyname) && d->canDecompress(keyname)) {
894                            error = d->assignToKey(keyname);
895                            if (!error) {
896                                char *data = readFirstCompressedDataOf(gb_main, GB_find_or_create_quark(gb_main, keyname.c_str()));
897
898                                nt_assert(data);
899                                possible++;
900                                printf("possibility %i = '%s'\n", possible, data);
901                                free(data);
902
903                                possibleDicts.push_back(d);
904
905                                error = d->unassignFromKey(keyname);
906                            }
907                        }
908                    }
909
910                    if (!error) {
911                        nt_assert(possible>0);
912
913                        int selected;
914                        if (possible>1) {
915                            char *question = GBS_global_string_copy("%i possibilities to decompress field '%s' have been detected\n"
916                                                                    "and example data was dumped to the console.\n"
917                                                                    "Please examine output and decide which is the correct possibility!",
918                                                                    possible, keyname.c_str());
919
920                            const char *buttons = "Abort";
921                            for (int p = 1; p <= possible; ++p) buttons = GBS_global_string("%s,%i", buttons, p);
922                            selected = aw_question("dict_decompress_bug", question, buttons, false, NULL);
923                            free(question);
924                        }
925                        else {
926                            selected = 1;
927                        }
928
929                        if (!selected) {
930                            error = "Aborted by user";
931                        }
932                        else {
933                            use[keyname] = possibleDicts[selected-1];
934                        }
935                    }
936                }
937            }
938
939            // now all redundancies should be eliminated and we can assign dictionaries to affected keys
940            if (!error) {
941                for (Keys::iterator ki = keys.begin(); !error && ki != keys.end(); ++ki) {
942                    KeyInfoPtr    k       = ki->second;
943                    const string& keyname = k->getName();
944
945                    if (k->isCompressed()) {
946                        if (!contains(use, keyname)) {
947                            error = GBS_global_string("No dictionary detected for key '%s'", keyname.c_str());
948                        }
949                        else {
950                            DictPtr d = use[keyname];
951
952                            if (d->getOriginalKey() != keyname) {
953                                d->assignToKey(keyname); // set the dictionary
954                                aw_message(GBS_global_string("Assigning '%s'-dictionary to '%s'",
955                                                             d->getOriginalKey().c_str(), keyname.c_str()));
956                                reassigned++;
957                            }
958                        }
959                    }
960                }
961            }
962
963            if (dataLost||reassigned) {
964                aw_message(dataLost
965                           ? "We apologize for the data-loss."
966                           : "No conflicts detected in compressed data.");
967                aw_message("Dictionaries fixed.\n"
968                           "Please save your database with a new name.");
969            }
970        }
971    }
972
973    Dict::gb_main = NULL;
974    return ta.close(error);
975}
976
977// --------------------------------------------------------------------------------
978
979static GB_ERROR remove_dup_colors(GBDATA *gb_item, ItemSelector& IF_DEBUG(sel)) {
980    // Databases out there may contain multiple 'ARB_color' entries.
981    // Due to some already fixed bug - maybe introduced in r5309 and fixed in r5825
982
983    GBDATA   *gb_color = GB_entry(gb_item, GB_COLORGROUP_ENTRY);
984    GB_ERROR  error    = NULL;
985
986#if defined(DEBUG)
987    int del_count = 0;
988#endif // DEBUG
989
990    if (gb_color) {
991        GB_push_my_security(gb_color);
992        while (!error) {
993            GBDATA *gb_next_color = GB_nextEntry(gb_color);
994            if (!gb_next_color) break;
995
996            error = GB_delete(gb_next_color);
997#if defined(DEBUG)
998            if (!error) del_count++;
999#endif // DEBUG
1000        }
1001        GB_pop_my_security(gb_color);
1002    }
1003
1004#if defined(DEBUG)
1005    if (del_count) fprintf(stderr,
1006                           "- deleted %i duplicated '" GB_COLORGROUP_ENTRY "' from %s '%s'\n",
1007                           del_count,
1008                           sel.item_name,
1009                           sel.generate_item_id(GB_get_root(gb_item), gb_item));
1010#endif // DEBUG
1011
1012    return error;
1013}
1014
1015// --------------------------------------------------------------------------------
1016
1017GB_ERROR NT_repair_DB(GBDATA *gb_main) {
1018    // status is already open and will be closed by caller!
1019
1020    CheckedConsistencies check(gb_main);
1021    GB_ERROR             err = 0;
1022    bool                 is_genome_db;
1023    {
1024        GB_transaction ta(gb_main);
1025        is_genome_db = GEN_is_genome_db(gb_main, -1);
1026    }
1027
1028    check.perform_check("fix gene_data",     NT_fix_gene_data,     err);
1029    check.perform_check("fix_dict_compress", NT_fix_dict_compress, err); // do this before NT_del_mark_move_REF (cause 'REF' is affected)
1030    check.perform_check("del_mark_move_REF", NT_del_mark_move_REF, err);
1031
1032    if (is_genome_db) {
1033        check.perform_check("convert_gene_locations", NT_convert_gene_locations, err);
1034    }
1035
1036    check.register_item_check("duplicated_item_colors", remove_dup_colors);
1037    check.perform_item_checks(err);
1038
1039    return err;
1040}
1041
1042void NT_rerepair_DB(AW_window*, GBDATA *gb_main) {
1043    // re-perform all DB checks
1044    GB_ERROR err = 0;
1045    {
1046        CheckedConsistencies check(gb_main);
1047        err = check.forgetDoneChecks();
1048    }
1049    if (!err) {
1050        arb_progress progress("DB-Repair");
1051        err = NT_repair_DB(gb_main);
1052    }
1053
1054    if (err) aw_message(err);
1055}
1056
1057
Note: See TracBrowser for help on using the repository browser.