source: tags/ms_r16q3/ARBDB/adname.cxx

Last change on this file was 15176, checked in by westram, 8 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : adname.cxx                                        //
4//   Purpose   : species names                                     //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "gb_local.h"
12
13#include <ad_config.h>
14#include "TreeNode.h"
15
16#include <arb_progress.h>
17#include <arb_strbuf.h>
18#include <arb_strarray.h>
19#include <arb_file.h>
20#include <arb_diff.h>
21
22#include <cctype>
23#include "ad_colorset.h"
24
25struct gbt_renamed {
26    int     used_by;
27    char    data[1];
28};
29
30static struct {
31    GB_HASH *renamed_hash;
32    GB_HASH *old_species_hash;
33    GBDATA  *gb_main;
34    GBDATA  *gb_species_data;
35    int      all_flag;
36} NameSession;
37
38#if defined(WARN_TODO)
39#warning change all_flag into estimated number of renames ( == 0 shall mean all)
40#endif
41
42GB_ERROR GBT_begin_rename_session(GBDATA *gb_main, int all_flag) {
43    /* Starts a rename session (to rename one or many species)
44     * all_flag == 1 -> rename all species in DB
45     * Call GBT_abort_rename_session() or GBT_commit_rename_session() to close the session.
46     */
47
48    GB_ERROR error = GB_push_transaction(gb_main);
49    if (!error) {
50        NameSession.gb_main         = gb_main;
51        NameSession.gb_species_data = GBT_get_species_data(gb_main);
52
53        if (!all_flag) { // this is meant to be used for single or few species
54            int hash_size = 128;
55
56            NameSession.renamed_hash     = GBS_create_dynaval_hash(hash_size, GB_MIND_CASE, GBS_dynaval_free);
57            NameSession.old_species_hash = 0;
58        }
59        else {
60            NameSession.renamed_hash     = GBS_create_dynaval_hash(GBT_get_species_count(gb_main), GB_MIND_CASE, GBS_dynaval_free);
61            NameSession.old_species_hash = GBT_create_species_hash(gb_main);
62        }
63        NameSession.all_flag = all_flag;
64    }
65    return error;
66}
67
68GB_ERROR GBT_rename_species(const char *oldname, const  char *newname, bool ignore_protection)
69{
70    GBDATA   *gb_species;
71    GBDATA   *gb_name;
72    GB_ERROR  error;
73
74    if (strcmp(oldname, newname) == 0)
75        return 0;
76
77#if defined(DEBUG) && 1
78    if (isdigit(oldname[0])) {
79        printf("oldname='%s' newname='%s'\n", oldname, newname);
80    }
81#endif
82
83    if (NameSession.all_flag) {
84        gb_assert(NameSession.old_species_hash);
85        gb_species = (GBDATA *)GBS_read_hash(NameSession.old_species_hash, oldname);
86    }
87    else {
88        GBDATA *gb_found_species;
89
90        gb_assert(NameSession.old_species_hash == 0);
91        gb_found_species = GBT_find_species_rel_species_data(NameSession.gb_species_data, newname);
92        gb_species       = GBT_find_species_rel_species_data(NameSession.gb_species_data, oldname);
93
94        if (gb_found_species && gb_species != gb_found_species) {
95            return GBS_global_string("A species named '%s' already exists.", newname);
96        }
97    }
98
99    if (!gb_species) {
100        return GBS_global_string("Expected that a species named '%s' exists (maybe there are duplicate species, database might be corrupt)", oldname);
101    }
102
103    gb_name = GB_entry(gb_species, "name");
104    if (ignore_protection) GB_push_my_security(NameSession.gb_main);
105    error   = GB_write_string(gb_name, newname);
106    if (ignore_protection) GB_pop_my_security(NameSession.gb_main);
107
108    if (!error) {
109        if (NameSession.old_species_hash) {
110            GBS_write_hash(NameSession.old_species_hash, oldname, 0);
111        }
112        gbt_renamed *rns = (gbt_renamed*)ARB_calloc<char>(strlen(newname)+sizeof(gbt_renamed));
113        strcpy(&rns->data[0], newname);
114        GBS_write_hash(NameSession.renamed_hash, oldname, (long)rns);
115    }
116    return error;
117}
118
119static void gbt_free_rename_session_data() {
120    if (NameSession.renamed_hash) {
121        GBS_free_hash(NameSession.renamed_hash);
122        NameSession.renamed_hash = 0;
123    }
124    if (NameSession.old_species_hash) {
125        GBS_free_hash(NameSession.old_species_hash);
126        NameSession.old_species_hash = 0;
127    }
128}
129
130GB_ERROR GBT_abort_rename_session() {
131    gbt_free_rename_session_data();
132    return GB_abort_transaction(NameSession.gb_main);
133}
134
135static const char *currentTreeName = 0;
136
137static GB_ERROR gbt_rename_tree_rek(TreeNode *tree, int tree_index) {
138    if (tree) {
139        if (tree->is_leaf) {
140            if (tree->name) {
141                gbt_renamed *rns = (gbt_renamed *)GBS_read_hash(NameSession.renamed_hash, tree->name);
142                if (rns) {
143                    char *newname;
144                    if (rns->used_by == tree_index) { // species more than once in the tree
145                        static int counter = 0;
146                        char       buffer[256];
147
148                        sprintf(buffer, "%s_%i", rns->data, counter++);
149                        GB_warningf("Species '%s' more than once in '%s', creating zombie '%s'",
150                                    tree->name, currentTreeName, buffer);
151                        newname = buffer;
152                    }
153                    else {
154                        newname = &rns->data[0];
155                    }
156                    freedup(tree->name, newname);
157                    rns->used_by = tree_index;
158                }
159            }
160        }
161        else {
162            gbt_rename_tree_rek(tree->get_leftson(), tree_index);
163            gbt_rename_tree_rek(tree->get_rightson(), tree_index);
164        }
165    }
166    return NULL;
167}
168
169GB_ERROR GBT_commit_rename_session() { // goes to header: __ATTR__USERESULT
170    bool         is_genome_db = GEN_is_genome_db(NameSession.gb_main, -1);
171    arb_progress commit_progress("Correcting name references", 3+is_genome_db);
172    GB_ERROR     error        = 0;
173
174    commit_progress.allow_title_reuse();
175
176    // rename species in trees
177    {
178        ConstStrArray tree_names;
179        GBT_get_tree_names(tree_names, NameSession.gb_main, false);
180
181        if (!tree_names.empty()) {
182            int          tree_count = tree_names.size();
183            arb_progress progress(GBS_global_string("Correcting names in %i tree%c", tree_count, "s"[tree_count<2]),
184                                  tree_count*3);
185
186            for (int count = 0; count<tree_count && !error; ++count) {
187                const char *tname = tree_names[count];
188                TreeNode   *tree  = GBT_read_tree(NameSession.gb_main, tname, new SimpleRoot);
189                ++progress;
190
191                if (tree) {
192                    currentTreeName = tname; // provide tree name (used for error message)
193                    gbt_rename_tree_rek(tree, count+1);
194                    currentTreeName = 0;
195
196                    ++progress;
197
198                    GBT_write_tree(NameSession.gb_main, tname, tree);
199                    destroy(tree);
200
201                    progress.inc_and_check_user_abort(error);
202                }
203                else {
204                    GBT_message(NameSession.gb_main, GBS_global_string("Warning: failed to read '%s'\n"
205                                                                       "(Reason: %s)\n"
206                                                                       "Please note that this tree is useless now!",
207                                                                       tname, GB_await_error()));
208                    ++progress;
209                    ++progress;
210                }
211            }
212        }
213        commit_progress.inc_and_check_user_abort(error);
214    }
215    // rename configurations
216    if (!error) {
217        ConstStrArray config_names;
218        GBT_get_configuration_names(config_names, NameSession.gb_main);
219
220        if (!config_names.empty()) {
221            int          config_count = config_names.size();
222            arb_progress progress(GBS_global_string("Correcting names in %i config%c", config_count, "s"[config_count<2]), config_count);
223
224            for (int count = 0; !error && count<config_count; ++count) {
225                GBT_config config(NameSession.gb_main, config_names[count], error);
226                if (!error) {
227                    int need_save = 0;
228                    for (int area = 0; !error && area<2; ++area) {
229                        GBT_config_parser   parser(config, area);
230                        GBS_strstruct      *strstruct    = GBS_stropen(1000);
231
232                        while (1) {
233                            const GBT_config_item& item = parser.nextItem(error);
234                            if (error || item.type == CI_END_OF_CONFIG) break;
235
236                            if (item.type == CI_SPECIES) {
237                                gbt_renamed *rns = (gbt_renamed *)GBS_read_hash(NameSession.renamed_hash, item.name);
238                                if (rns) { // species was renamed
239                                    GBT_append_to_config_string(GBT_config_item(CI_SPECIES, rns->data), strstruct);
240                                    need_save = 1;
241                                    continue;
242                                }
243                            }
244                            GBT_append_to_config_string(item, strstruct);
245                        }
246
247                        if (!error) {
248                            config.set_definition(area, GBS_strclose(strstruct));
249                        }
250                        else {
251                            error = GBS_global_string("Failed to parse configuration '%s' (Reason: %s)", config_names[count], error);
252                            GBS_strforget(strstruct);
253                        }
254                    }
255
256                    if (!error && need_save) {
257                        error = config.save(NameSession.gb_main, config_names[count], false);
258                    }
259                }
260                progress.inc_and_check_user_abort(error);
261            }
262        }
263        commit_progress.inc_and_check_user_abort(error);
264    }
265
266    // rename species in saved colorsets
267    if (!error) {
268        GBDATA *gb_species_colorset_root = GBT_colorset_root(NameSession.gb_main, "species");
269        if (gb_species_colorset_root) {
270            ConstStrArray colorset_names;
271            GBT_get_colorset_names(colorset_names, gb_species_colorset_root);
272
273            int colorset_count = colorset_names.size();
274            if (colorset_count>0) {
275                arb_progress progress(GBS_global_string("Correcting names in %i colorset%c", colorset_count, "s"[colorset_count<2]), colorset_count);
276
277                for (int c = 0; c<colorset_count && !error; ++c) {
278                    GBDATA *gb_colorset     = GBT_find_colorset(gb_species_colorset_root, colorset_names[c]);
279                    if (!gb_colorset) error = GB_await_error();
280                    else {
281                        ConstStrArray colorDefs;
282                        error = GBT_load_colorset(gb_colorset, colorDefs);
283                        if (!error) {
284                            StrArray modifiedDefs;
285                            bool     changed = false;
286
287                            for (int d = colorDefs.size()-1; d>=0; --d) {
288                                const char *def   = colorDefs[d];
289                                const char *equal = strchr(def, '=');
290
291                                if (equal) { // only handle correct entries (do not touch rest)
292                                    if (strcmp(equal+1, "0") == 0) { // unneeded "no color"-entry (see [14094])
293                                        colorDefs.remove(d);
294                                        changed = true;
295                                    }
296                                    else {
297                                        gbt_renamed *rns;
298                                        {
299                                            LocallyModify<char>  tempSplit(const_cast<char*>(equal)[0], 0);
300                                            rns = (gbt_renamed *)GBS_read_hash(NameSession.renamed_hash, def);
301                                        }
302                                        if (rns) { // species was renamed
303                                            char *newDef = GBS_global_string_copy("%s%s", rns->data, equal);
304                                            colorDefs.replace(d, newDef); // replace colorDefs
305                                            modifiedDefs.put(newDef);     // keep heapcopy until colorDefs gets written
306
307                                            changed = true;
308                                        }
309                                    }
310                                }
311                            }
312
313                            if (changed && !error) error = GBT_save_colorset(gb_colorset, colorDefs);
314                        }
315                    }
316                    progress.inc_and_check_user_abort(error);
317                }
318            }
319        }
320        commit_progress.inc_and_check_user_abort(error);
321    }
322
323    // rename links in pseudo-species
324    if (!error && is_genome_db) {
325        {
326            arb_progress progress("Correcting names of organism references");
327
328            GBDATA *gb_pseudo;
329            for (gb_pseudo = GEN_first_pseudo_species(NameSession.gb_main);
330                 gb_pseudo && !error;
331                 gb_pseudo = GEN_next_pseudo_species(gb_pseudo))
332            {
333                GBDATA *gb_origin_organism = GB_entry(gb_pseudo, "ARB_origin_species");
334                if (gb_origin_organism) {
335                    const char  *origin = GB_read_char_pntr(gb_origin_organism);
336                    gbt_renamed *rns    = (gbt_renamed *)GBS_read_hash(NameSession.renamed_hash, origin);
337                    if (rns) {          // species was renamed
338                        const char *newname = &rns->data[0];
339                        error               = GB_write_string(gb_origin_organism, newname);
340                    }
341                }
342            }
343        }
344        commit_progress.inc_and_check_user_abort(error);
345    }
346
347    gbt_free_rename_session_data();
348
349    error = GB_end_transaction(NameSession.gb_main, error);
350    return error;
351}
352
353// --------------------------------------------------------------------------------
354
355#ifdef UNIT_TESTS
356#ifndef TEST_UNIT_H
357#include <test_unit.h>
358#endif
359
360// #define TEST_AUTO_UPDATE // uncomment to auto-update test result db
361
362void TEST_SLOW_rename_session() {
363    const char *inputname    = "TEST_opti_ascii_in.arb";
364    const char *outputname   = "TEST_opti_ascii_renamed.arb";
365    const char *expectedname = "TEST_opti_ascii_renamed_expected.arb";
366
367    {
368        GB_shell  shell;
369        GBDATA   *gb_main;
370        TEST_EXPECT_RESULT__NOERROREXPORTED(gb_main = GB_open(inputname, "rw"));
371
372        for (int session = 1; session <= 2; ++session) {
373            TEST_ANNOTATE(GBS_global_string("session=%i", session));
374
375            TEST_EXPECT_NO_ERROR(GBT_begin_rename_session(gb_main, 0));
376            if (session == 2) { // session 1 tests renaming nothing
377                // only in config 'some':
378                TEST_EXPECT_NO_ERROR(GBT_rename_species("FrnPhilo", "olihPnrF", true));
379                TEST_EXPECT_NO_ERROR(GBT_rename_species("DsfDesul", "luseDfsD", true));
380                // also in config 'other':
381                TEST_EXPECT_NO_ERROR(GBT_rename_species("CalSacch", "hccaSlaC", true));
382                TEST_EXPECT_NO_ERROR(GBT_rename_species("LacReute", "etueRcaL", true));
383            }
384            TEST_EXPECT_NO_ERROR(GBT_commit_rename_session());
385        }
386
387        TEST_EXPECT_NO_ERROR(GB_save_as(gb_main, outputname, "a"));
388        GB_close(gb_main);
389    }
390
391#if defined(TEST_AUTO_UPDATE)
392    TEST_COPY_FILE(outputname, expectedname);
393#endif
394    TEST_EXPECT_TEXTFILE_DIFFLINES(outputname, expectedname, 0);
395    TEST_EXPECT_ZERO_OR_SHOW_ERRNO(GB_unlink(outputname));
396}
397TEST_PUBLISH(TEST_SLOW_rename_session);
398
399#endif // UNIT_TESTS
400
401// --------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.