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