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_root.hxx> |
---|
18 | #include <aw_msg.hxx> |
---|
19 | #include <arb_progress.h> |
---|
20 | #include <arbdbt.h> |
---|
21 | #include <ctime> |
---|
22 | |
---|
23 | #define AWAR_TRACK_BASE "track/" |
---|
24 | #define AWAR_TRACK_ALI AWAR_TRACK_BASE "ali" |
---|
25 | #define AWAR_TRACK_INITIALS AWAR_TRACK_BASE "initials" |
---|
26 | |
---|
27 | static GB_ERROR writeHistory(GBDATA *gb_species, const char *stamp, const char *entry) { |
---|
28 | char *newContent = GBS_global_string_copy("%s %s", stamp, entry); |
---|
29 | GB_ERROR error = NULp; |
---|
30 | GBDATA *gb_history = GB_entry(gb_species, "seq_history"); |
---|
31 | |
---|
32 | if (!gb_history) error = GBT_write_string(gb_species, "seq_history", newContent); |
---|
33 | else { |
---|
34 | char *oldContent = GB_read_string(gb_history); |
---|
35 | long oldSize = GB_read_string_count(gb_history); |
---|
36 | long newSize = strlen(newContent); |
---|
37 | long size = oldSize+1+newSize+1; |
---|
38 | char *content = ARB_alloc<char>(size); |
---|
39 | |
---|
40 | memcpy(content, newContent, newSize); |
---|
41 | content[newSize] = '\n'; |
---|
42 | memcpy(content+newSize+1, oldContent, oldSize+1); |
---|
43 | |
---|
44 | error = GB_write_string(gb_history, content); |
---|
45 | |
---|
46 | free(content); |
---|
47 | free(oldContent); |
---|
48 | } |
---|
49 | |
---|
50 | free(newContent); |
---|
51 | |
---|
52 | return error; |
---|
53 | } |
---|
54 | |
---|
55 | static void trackAlignmentChanges(AW_window *aww) { |
---|
56 | GB_transaction ta(GLOBAL.gb_main); |
---|
57 | |
---|
58 | AW_root *root = aww->get_root(); |
---|
59 | char *ali = root->awar(AWAR_TRACK_ALI)->read_string(); |
---|
60 | char *checksum_field = GBS_string_2_key(GBS_global_string("checksum_%s", ali)); |
---|
61 | long newSpecies = 0; |
---|
62 | long ali_changed = 0; |
---|
63 | long seq_changed = 0; |
---|
64 | long unchanged = 0; |
---|
65 | GB_ERROR error = NULp; |
---|
66 | char *stamp; |
---|
67 | |
---|
68 | { |
---|
69 | char *initials = root->awar(AWAR_TRACK_INITIALS)->read_string(); |
---|
70 | char atime[256]; |
---|
71 | time_t t = time(NULp); |
---|
72 | struct tm *tms = localtime(&t); |
---|
73 | |
---|
74 | strftime(atime, 255, "%Y/%m/%d %k:%M", tms); |
---|
75 | stamp = GBS_global_string_copy("%s %s", atime, initials); |
---|
76 | free(initials); |
---|
77 | } |
---|
78 | |
---|
79 | arb_progress progress(GBS_global_string("Tracking changes in '%s'", ali), GBT_get_species_count(GLOBAL.gb_main)); |
---|
80 | |
---|
81 | for (GBDATA *gb_species = GBT_first_species(GLOBAL.gb_main); |
---|
82 | gb_species && !error; |
---|
83 | gb_species = GBT_next_species(gb_species)) |
---|
84 | { |
---|
85 | GBDATA *gb_seq = GBT_find_sequence(gb_species, ali); |
---|
86 | if (gb_seq) { |
---|
87 | // has data in wanted alignment |
---|
88 | const char *seq = GB_read_char_pntr(gb_seq); |
---|
89 | long char_cs = GBS_checksum(seq, 0, ".-"); |
---|
90 | long ali_cs = GBS_checksum(seq, 0, ""); |
---|
91 | char *char_entry = GBS_global_string_copy("%lx", char_cs); |
---|
92 | char *ali_entry = GBS_global_string_copy("%lx", ali_cs); |
---|
93 | const char *save_comment = NULp; |
---|
94 | |
---|
95 | GBDATA *gb_checksum = GB_entry(gb_species, checksum_field); |
---|
96 | if (!gb_checksum) { |
---|
97 | newSpecies++; |
---|
98 | gb_checksum = GB_create(gb_species, checksum_field, GB_STRING); |
---|
99 | if (!gb_checksum) error = GB_await_error(); |
---|
100 | else save_comment = "new"; |
---|
101 | } |
---|
102 | else { |
---|
103 | char *oldValue = GB_read_string(gb_checksum); |
---|
104 | if (!oldValue) error = GB_await_error(); |
---|
105 | else { |
---|
106 | char *comma = strchr(oldValue, ','); |
---|
107 | if (!comma) { |
---|
108 | error = GBS_global_string("Invalid value '%s' in field '%s'", oldValue, checksum_field); |
---|
109 | } |
---|
110 | else { |
---|
111 | comma[0] = 0; |
---|
112 | if (strcmp(char_entry, oldValue) == 0) { // seq checksum is equal |
---|
113 | if (strcmp(ali_entry, comma+1) == 0) { // ali checksum is equal |
---|
114 | unchanged++; |
---|
115 | } |
---|
116 | else { // only alignment checksum changed |
---|
117 | ali_changed++; |
---|
118 | save_comment = "alignment changed"; |
---|
119 | } |
---|
120 | } |
---|
121 | else { |
---|
122 | seq_changed++; |
---|
123 | save_comment = "sequence changed"; |
---|
124 | } |
---|
125 | } |
---|
126 | free(oldValue); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | if (save_comment) { |
---|
131 | error = GB_write_string(gb_checksum, GBS_global_string("%s,%s", char_entry, ali_entry)); |
---|
132 | if (!error) error = writeHistory(gb_species, stamp, GBS_global_string("%s %s", ali, save_comment)); |
---|
133 | } |
---|
134 | |
---|
135 | free(ali_entry); |
---|
136 | free(char_entry); |
---|
137 | } |
---|
138 | progress.inc_and_check_user_abort(error); |
---|
139 | } |
---|
140 | |
---|
141 | if (error) aw_message(error); |
---|
142 | else { |
---|
143 | if (seq_changed) aw_message(GBS_global_string("%li species with changed sequence", seq_changed)); |
---|
144 | if (ali_changed) aw_message(GBS_global_string("%li species with changed alignment", ali_changed)); |
---|
145 | if (newSpecies) aw_message(GBS_global_string("%li new species", newSpecies)); |
---|
146 | } |
---|
147 | |
---|
148 | free(stamp); |
---|
149 | free(checksum_field); |
---|
150 | free(ali); |
---|
151 | } |
---|
152 | |
---|
153 | void NT_create_trackAliChanges_Awars(AW_root *root, AW_default properties) { |
---|
154 | root->awar_string(AWAR_TRACK_ALI, "???", GLOBAL.gb_main); |
---|
155 | root->awar_string(AWAR_TRACK_INITIALS, GB_getenvUSER(), properties); |
---|
156 | } |
---|
157 | |
---|
158 | AW_window *NT_create_trackAliChanges_window(AW_root *root) { |
---|
159 | AW_window_simple *aws = new AW_window_simple; |
---|
160 | aws->init(root, "TRACK_ALI_CHANGES", "Track alignment changes"); |
---|
161 | aws->load_xfig("trackali.fig"); |
---|
162 | |
---|
163 | aws->at("close"); |
---|
164 | aws->callback(AW_POPDOWN); |
---|
165 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
166 | |
---|
167 | aws->at("help"); |
---|
168 | aws->callback(makeHelpCallback("track_ali_changes.hlp")); |
---|
169 | aws->create_button("HELP", "HELP", "H"); |
---|
170 | |
---|
171 | aws->at("initials"); |
---|
172 | aws->create_input_field(AWAR_TRACK_INITIALS); |
---|
173 | |
---|
174 | aws->at("ali_sel"); |
---|
175 | awt_create_ALI_selection_list(GLOBAL.gb_main, aws, AWAR_TRACK_ALI, "*="); |
---|
176 | |
---|
177 | aws->at("go"); |
---|
178 | aws->callback(trackAlignmentChanges); |
---|
179 | aws->create_autosize_button("TRACK", "Track changes", "T"); |
---|
180 | |
---|
181 | return aws; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|