ARB
NT_trackAliChanges.cxx
Go to the documentation of this file.
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);
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) {
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;
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 
154  root->awar_string(AWAR_TRACK_ALI, "???", GLOBAL.gb_main);
155  root->awar_string(AWAR_TRACK_INITIALS, GB_getenvUSER(), properties);
156 }
157 
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");
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 
const char * GB_ERROR
Definition: arb_core.h:25
static GB_ERROR writeHistory(GBDATA *gb_species, const char *stamp, const char *entry)
#define AWAR_TRACK_INITIALS
GB_ERROR GB_write_string(GBDATA *gbd, const char *s)
Definition: arbdb.cxx:1361
void load_xfig(const char *file, bool resize=true)
Definition: AW_window.cxx:717
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:204
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
char * GBS_string_2_key(const char *str)
Definition: adstring.cxx:52
#define AWAR_TRACK_ALI
FILE * seq
Definition: rns.c:46
size_t GB_read_string_count(GBDATA *gbd)
Definition: arbdb.cxx:886
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:353
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
GBDATA * GB_create(GBDATA *father, const char *key, GB_TYPES type)
Definition: arbdb.cxx:1755
static void error(const char *msg)
Definition: mkptypes.cxx:96
AW_window * NT_create_trackAliChanges_window(AW_root *root)
char * read_string() const
Definition: AW_awar.cxx:201
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
GBDATA * GBT_find_sequence(GBDATA *gb_species, const char *aliname)
Definition: adali.cxx:670
AW_DB_selection * awt_create_ALI_selection_list(GBDATA *gb_main, AW_window *aws, const char *varname, const char *ali_type_match)
void NT_create_trackAliChanges_Awars(AW_root *root, AW_default properties)
static void trackAlignmentChanges(AW_window *aww)
GB_CSTR GB_getenvUSER(void)
Definition: adsocket.cxx:515
GB_ERROR GBT_write_string(GBDATA *gb_container, const char *fieldpath, const char *content)
Definition: adtools.cxx:451
char * GB_read_string(GBDATA *gbd)
Definition: arbdb.cxx:879
GBDATA * GBT_first_species(GBDATA *gb_main)
Definition: aditem.cxx:124
uint32_t GBS_checksum(const char *seq, int ignore_case, const char *exclude)
Definition: adstring.cxx:353
void aw_message(const char *msg)
Definition: AW_status.cxx:932
AW_root * get_root()
Definition: aw_window.hxx:348
GBDATA * GBT_next_species(GBDATA *gb_species)
Definition: aditem.cxx:128
#define NULp
Definition: cxxforward.h:97
GBDATA * gb_main
Definition: NT_local.h:36
long GBT_get_species_count(GBDATA *gb_main)
Definition: aditem.cxx:203
GB_transaction ta(gb_var)
GB_CSTR GB_read_char_pntr(GBDATA *gbd)
Definition: arbdb.cxx:874
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
GBDATA * GB_entry(GBDATA *father, const char *key)
Definition: adquery.cxx:334
void inc_and_check_user_abort(GB_ERROR &error)
Definition: arb_progress.h:274
char * GBS_global_string_copy(const char *templat,...)
Definition: arb_msg.cxx:195
NT_global GLOBAL
Definition: NT_main.cxx:44