source: tags/old_import_filter/NTREE/NT_trackAliChanges.cxx

Last change on this file was 10117, checked in by westram, 11 years ago
  • fix include-chaos in NTREE
    • all headers named
      • .h (some had .hxx)
      • as .cxx file
    • removed obsolete header (NT_dbrepair.hxx, nt_join.hxx)
  • removed some more definitions of nt_assert
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : NT_trackAliChanges.cxx                            //
4//   Purpose   : Track alignment and sequences changes             //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in June 2007      //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include "NT_local.h"
13#include "NT_trackAliChanges.h"
14
15#include <awt_sel_boxes.hxx>
16#include <aw_awar.hxx>
17#include <aw_window.hxx>
18#include <aw_root.hxx>
19#include <aw_msg.hxx>
20#include <arb_progress.h>
21#include <arbdbt.h>
22#include <ctime>
23
24#define AWAR_TRACK_BASE     "track/"
25#define AWAR_TRACK_ALI      AWAR_TRACK_BASE "ali"
26#define AWAR_TRACK_INITIALS AWAR_TRACK_BASE "initials"
27
28static GB_ERROR writeHistory(GBDATA *gb_species, const char *stamp, const char *entry) {
29    char     *newContent = GBS_global_string_copy("%s %s", stamp, entry);
30    GB_ERROR  error      = 0;
31    GBDATA   *gb_history = GB_entry(gb_species, "seq_history");
32
33    if (!gb_history) error = GBT_write_string(gb_species, "seq_history", newContent);
34    else {
35        char *oldContent = GB_read_string(gb_history);
36        long  oldSize    = GB_read_string_count(gb_history);
37        long  newSize    = strlen(newContent);
38        long  size       = oldSize+1+newSize+1;
39        char *content    = (char*)malloc(size);
40
41        memcpy(content, newContent, newSize);
42        content[newSize] = '\n';
43        memcpy(content+newSize+1, oldContent, oldSize+1);
44
45        error = GB_write_string(gb_history, content);
46
47        free(content);
48        free(oldContent);
49    }
50
51    free(newContent);
52
53    return error;
54}
55
56static void trackAlignmentChanges(AW_window *aww) {
57    GB_transaction ta(GLOBAL.gb_main);
58
59    AW_root  *root           = aww->get_root();
60    char     *ali            = root->awar(AWAR_TRACK_ALI)->read_string();
61    char     *checksum_field = GBS_string_2_key(GBS_global_string("checksum_%s", ali));
62    long      newSpecies     = 0;
63    long      ali_changed    = 0;
64    long      seq_changed    = 0;
65    long      unchanged      = 0;
66    GB_ERROR  error          = 0;
67    char     *stamp;
68
69    {
70        char      *initials = root->awar(AWAR_TRACK_INITIALS)->read_string();
71        char       atime[256];
72        time_t     t        = time(0);
73        struct tm *tms      = localtime(&t);
74
75        strftime(atime, 255, "%Y/%m/%d %k:%M", tms);
76        stamp = GBS_global_string_copy("%s %s", atime, initials);
77        free(initials);
78    }
79
80    arb_progress progress(GBS_global_string("Tracking changes in '%s'", ali), GBT_get_species_count(GLOBAL.gb_main));
81
82    for (GBDATA *gb_species = GBT_first_species(GLOBAL.gb_main);
83         gb_species && !error;
84         gb_species = GBT_next_species(gb_species))
85    {
86        GBDATA *gb_seq = GBT_read_sequence(gb_species, ali);
87        if (gb_seq) {
88            // has data in wanted alignment
89            const char *seq          = GB_read_char_pntr(gb_seq);
90            long        char_cs      = GBS_checksum(seq, 0, ".-");
91            long        ali_cs       = GBS_checksum(seq, 0, "");
92            char       *char_entry   = GBS_global_string_copy("%lx", char_cs);
93            char       *ali_entry    = GBS_global_string_copy("%lx", ali_cs);
94            const char *save_comment = 0;
95
96            GBDATA *gb_checksum = GB_entry(gb_species, checksum_field);
97            if (!gb_checksum) {
98                newSpecies++;
99                gb_checksum             = GB_create(gb_species, checksum_field, GB_STRING);
100                if (!gb_checksum) error = GB_await_error();
101                else save_comment       = "new";
102            }
103            else {
104                char *oldValue       = GB_read_string(gb_checksum);
105                if (!oldValue) error = GB_await_error();
106                else {
107                    char *comma = strchr(oldValue, ',');
108                    if (!comma) {
109                        error = GBS_global_string("Invalid value '%s' in field '%s'", oldValue, checksum_field);
110                    }
111                    else {
112                        comma[0] = 0;
113                        if (strcmp(char_entry, oldValue) == 0) { // seq checksum is equal
114                            if (strcmp(ali_entry, comma+1) == 0) { // ali checksum is equal
115                                unchanged++;
116                            }
117                            else { // only alignment checksum changed
118                                ali_changed++;
119                                save_comment = "alignment changed";
120                            }
121                        }
122                        else {
123                            seq_changed++;
124                            save_comment = "sequence changed";
125                        }
126                    }
127                    free(oldValue);
128                }
129            }
130
131            if (save_comment) {
132                error             = GB_write_string(gb_checksum, GBS_global_string("%s,%s", char_entry, ali_entry));
133                if (!error) error = writeHistory(gb_species, stamp, GBS_global_string("%s %s", ali, save_comment));
134            }
135
136            free(ali_entry);
137            free(char_entry);
138        }
139        progress.inc_and_check_user_abort(error);
140    }
141
142    if (error) aw_message(error);
143    else {
144        if (seq_changed) aw_message(GBS_global_string("%li species with changed sequence", seq_changed));
145        if (ali_changed) aw_message(GBS_global_string("%li species with changed alignment", ali_changed));
146        if (newSpecies) aw_message(GBS_global_string("%li new species", newSpecies));
147    }
148
149    free(stamp);
150    free(checksum_field);
151    free(ali);
152}
153
154void NT_create_trackAliChanges_Awars(AW_root *root, AW_default properties) {
155    root->awar_string(AWAR_TRACK_ALI, "???", GLOBAL.gb_main);
156    root->awar_string(AWAR_TRACK_INITIALS, GB_getenvUSER(), properties);
157}
158
159AW_window *NT_create_trackAliChanges_window(AW_root *root) {
160    AW_window_simple *aws = new AW_window_simple;
161    aws->init(root, "TRACK_ALI_CHANGES", "Track alignment changes");
162    aws->load_xfig("trackali.fig");
163
164    aws->at("close");
165    aws->callback((AW_CB0)AW_POPDOWN);
166    aws->create_button("CLOSE", "CLOSE", "C");
167
168    aws->at("help");
169    aws->callback(AW_POPUP_HELP, (AW_CL)"track_ali_changes.hlp");
170    aws->create_button("HELP", "HELP", "H");
171
172    aws->at("initials");
173    aws->create_input_field(AWAR_TRACK_INITIALS);
174
175    aws->at("ali_sel");
176    awt_create_selection_list_on_alignments(GLOBAL.gb_main, aws, AWAR_TRACK_ALI, "*=");
177
178    aws->at("go");
179    aws->callback(trackAlignmentChanges);
180    aws->create_autosize_button("TRACK", "Track changes", "T");
181
182    return aws;
183}
184
185
Note: See TracBrowser for help on using the repository browser.