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