1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : ad_transpro.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "NT_local.h" |
---|
12 | |
---|
13 | #include <awt_sel_boxes.hxx> |
---|
14 | #include <Translate.hxx> |
---|
15 | #include <AP_codon_table.hxx> |
---|
16 | #include <AP_pro_a_nucs.hxx> |
---|
17 | #include <aw_awars.hxx> |
---|
18 | #include <aw_root.hxx> |
---|
19 | #include <aw_question.hxx> |
---|
20 | #include <aw_msg.hxx> |
---|
21 | #include <arb_progress.h> |
---|
22 | #include <arbdbt.h> |
---|
23 | #include <cctype> |
---|
24 | #include <arb_defs.h> |
---|
25 | |
---|
26 | static GB_ERROR arb_r2a(GBDATA *gb_main, bool use_entries, bool save_entries, int selected_startpos, |
---|
27 | bool translate_all, const char *ali_source, const char *ali_dest) |
---|
28 | { |
---|
29 | // if use_entries == true -> use fields 'codon_start' and 'transl_table' for translation |
---|
30 | // (selected_startpos and AWAR_PROTEIN_TYPE are only used both fields are missing, |
---|
31 | // if only one is missing, now an error occurs) |
---|
32 | // if use_entries == false -> always use selected_startpos and AWAR_PROTEIN_TYPE |
---|
33 | // if translate_all == true -> a selected_startpos > 1 produces a leading 'X' in protein data |
---|
34 | // (otherwise nucleotides in front of the starting pos are simply ignored) |
---|
35 | |
---|
36 | nt_assert(selected_startpos >= 0 && selected_startpos < 3); |
---|
37 | |
---|
38 | GB_ERROR error = 0; |
---|
39 | char *to_free = 0; |
---|
40 | |
---|
41 | // check/create alignments |
---|
42 | { |
---|
43 | GBDATA *gb_source = GBT_get_alignment(gb_main, ali_source); |
---|
44 | if (!gb_source) { |
---|
45 | error = GBS_global_string("No valid source alignment (%s)", GB_await_error()); |
---|
46 | } |
---|
47 | else { |
---|
48 | GBDATA *gb_dest = GBT_get_alignment(gb_main, ali_dest); |
---|
49 | if (!gb_dest) { |
---|
50 | GB_clear_error(); |
---|
51 | const char *msg = GBS_global_string("You have not selected a destination alignment\n" |
---|
52 | "Shall I create one ('%s_pro') for you?", ali_source); |
---|
53 | if (!aw_ask_sure("create_protein_ali", msg)) { |
---|
54 | error = "Cancelled by user"; |
---|
55 | } |
---|
56 | else { |
---|
57 | long slen = GBT_get_alignment_len(gb_main, ali_source); |
---|
58 | to_free = GBS_global_string_copy("%s_pro", ali_source); |
---|
59 | ali_dest = to_free; |
---|
60 | gb_dest = GBT_create_alignment(gb_main, ali_dest, slen/3+1, 0, 1, "ami"); |
---|
61 | |
---|
62 | if (!gb_dest) error = GB_await_error(); |
---|
63 | else { |
---|
64 | char *fname = GBS_global_string_copy("%s/data", ali_dest); |
---|
65 | error = GBT_add_new_changekey(gb_main, fname, GB_STRING); |
---|
66 | free(fname); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | int no_data = 0; // count species w/o data |
---|
74 | int spec_no_transl_info = 0; // counts species w/o or with illegal transl_table and/or codon_start |
---|
75 | int count = 0; // count translated species |
---|
76 | int stops = 0; // count overall stop codons |
---|
77 | int selected_ttable = -1; |
---|
78 | |
---|
79 | if (!error) { |
---|
80 | arb_progress progress("Translating", GBT_count_marked_species(gb_main)); |
---|
81 | |
---|
82 | bool table_used[AWT_CODON_TABLES]; |
---|
83 | memset(table_used, 0, sizeof(table_used)); |
---|
84 | selected_ttable = *GBT_read_int(gb_main, AWAR_PROTEIN_TYPE); // read selected table |
---|
85 | |
---|
86 | if (use_entries) { |
---|
87 | for (GBDATA *gb_species = GBT_first_marked_species(gb_main); |
---|
88 | gb_species && !error; |
---|
89 | gb_species = GBT_next_marked_species(gb_species)) |
---|
90 | { |
---|
91 | int arb_table, codon_start; |
---|
92 | error = AWT_getTranslationInfo(gb_species, arb_table, codon_start); |
---|
93 | |
---|
94 | if (!error) { |
---|
95 | if (arb_table == -1) arb_table = selected_ttable; // no transl_table entry -> default to selected standard code |
---|
96 | table_used[arb_table] = true; |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | else { |
---|
101 | table_used[selected_ttable] = true; // and mark it used |
---|
102 | } |
---|
103 | |
---|
104 | for (int table = 0; table<AWT_CODON_TABLES && !error; ++table) { |
---|
105 | if (!table_used[table]) continue; |
---|
106 | |
---|
107 | for (GBDATA *gb_species = GBT_first_marked_species(gb_main); |
---|
108 | gb_species && !error; |
---|
109 | gb_species = GBT_next_marked_species(gb_species)) |
---|
110 | { |
---|
111 | bool found_transl_info = false; |
---|
112 | int startpos = selected_startpos; |
---|
113 | |
---|
114 | if (use_entries) { // if entries are used, test if field 'transl_table' matches current table |
---|
115 | int sp_arb_table, sp_codon_start; |
---|
116 | |
---|
117 | error = AWT_getTranslationInfo(gb_species, sp_arb_table, sp_codon_start); |
---|
118 | |
---|
119 | nt_assert(!error); // should already have been handled after first call to AWT_getTranslationInfo above |
---|
120 | |
---|
121 | if (sp_arb_table == -1) { // no table in DB |
---|
122 | nt_assert(sp_codon_start == -1); // either both should be defined or none |
---|
123 | sp_arb_table = selected_ttable; // use selected translation table as default (if 'transl_table' field is missing) |
---|
124 | sp_codon_start = selected_startpos; // use selected codon startpos (if 'codon_start' field is missing) |
---|
125 | } |
---|
126 | else { |
---|
127 | nt_assert(sp_codon_start != -1); // either both should be defined or none |
---|
128 | found_transl_info = true; |
---|
129 | } |
---|
130 | |
---|
131 | if (sp_arb_table != table) continue; // species has not current transl_table |
---|
132 | |
---|
133 | startpos = sp_codon_start; |
---|
134 | } |
---|
135 | |
---|
136 | GBDATA *gb_source = GB_entry(gb_species, ali_source); |
---|
137 | if (!gb_source) { ++no_data; } |
---|
138 | else { |
---|
139 | GBDATA *gb_source_data = GB_entry(gb_source, "data"); |
---|
140 | if (!gb_source_data) { ++no_data; } |
---|
141 | else { |
---|
142 | char *data = GB_read_string(gb_source_data); |
---|
143 | if (!data) { |
---|
144 | GB_print_error(); // cannot read data (ignore species) |
---|
145 | ++no_data; |
---|
146 | } |
---|
147 | else { |
---|
148 | if (!found_transl_info) ++spec_no_transl_info; // count species with missing info |
---|
149 | |
---|
150 | stops += AWT_pro_a_nucs_convert(table, data, GB_read_string_count(gb_source_data), startpos, translate_all, false, false, 0); // do the translation |
---|
151 | ++count; |
---|
152 | |
---|
153 | GBDATA *gb_dest_data = GBT_add_data(gb_species, ali_dest, "data", GB_STRING); |
---|
154 | if (!gb_dest_data) error = GB_await_error(); |
---|
155 | else error = GB_write_string(gb_dest_data, data); |
---|
156 | |
---|
157 | |
---|
158 | if (!error && save_entries && !found_transl_info) { |
---|
159 | error = AWT_saveTranslationInfo(gb_species, selected_ttable, startpos); |
---|
160 | } |
---|
161 | |
---|
162 | free(data); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | progress.inc_and_check_user_abort(error); |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | if (!error) { |
---|
172 | if (use_entries) { // use 'transl_table' and 'codon_start' fields ? |
---|
173 | if (spec_no_transl_info) { |
---|
174 | int embl_transl_table = AWT_arb_code_nr_2_embl_transl_table(selected_ttable); |
---|
175 | aw_message(GBS_global_string("%i taxa had no valid translation info (fields 'transl_table' and 'codon_start')\n" |
---|
176 | "Defaults (%i and %i) have been used%s.", |
---|
177 | spec_no_transl_info, |
---|
178 | embl_transl_table, selected_startpos+1, |
---|
179 | save_entries ? " and written to DB entries" : "")); |
---|
180 | } |
---|
181 | else { // all entries were present |
---|
182 | aw_message("codon_start and transl_table entries were found for all translated taxa"); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | if (no_data>0) { |
---|
187 | aw_message(GBS_global_string("%i taxa had no data in '%s'", no_data, ali_source)); |
---|
188 | } |
---|
189 | if ((count+no_data) == 0) { |
---|
190 | aw_message("Please mark species to translate"); |
---|
191 | } |
---|
192 | else { |
---|
193 | aw_message(GBS_global_string("%i taxa converted\n %f stops per sequence found", |
---|
194 | count, (double)stops/(double)count)); |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | free(to_free); |
---|
199 | |
---|
200 | return error; |
---|
201 | } |
---|
202 | |
---|
203 | #define AWAR_TRANSPRO_PREFIX "transpro/" |
---|
204 | #define AWAR_TRANSPRO_SOURCE AWAR_TRANSPRO_PREFIX "source" |
---|
205 | #define AWAR_TRANSPRO_DEST AWAR_TRANSPRO_PREFIX "dest" |
---|
206 | #define AWAR_TRANSPRO_POS AWAR_TRANSPRO_PREFIX "pos" // [0..N-1] |
---|
207 | #define AWAR_TRANSPRO_MODE AWAR_TRANSPRO_PREFIX "mode" |
---|
208 | #define AWAR_TRANSPRO_XSTART AWAR_TRANSPRO_PREFIX "xstart" |
---|
209 | #define AWAR_TRANSPRO_WRITE AWAR_TRANSPRO_PREFIX "write" |
---|
210 | |
---|
211 | static void transpro_event(AW_window *aww) { |
---|
212 | GB_ERROR error = GB_begin_transaction(GLOBAL.gb_main); |
---|
213 | if (!error) { |
---|
214 | #if defined(DEBUG) && 0 |
---|
215 | test_AWT_get_codons(); |
---|
216 | #endif |
---|
217 | AW_root *aw_root = aww->get_root(); |
---|
218 | char *ali_source = aw_root->awar(AWAR_TRANSPRO_SOURCE)->read_string(); |
---|
219 | char *ali_dest = aw_root->awar(AWAR_TRANSPRO_DEST)->read_string(); |
---|
220 | char *mode = aw_root->awar(AWAR_TRANSPRO_MODE)->read_string(); |
---|
221 | int startpos = aw_root->awar(AWAR_TRANSPRO_POS)->read_int(); |
---|
222 | bool save2fields = aw_root->awar(AWAR_TRANSPRO_WRITE)->read_int(); |
---|
223 | bool translate_all = aw_root->awar(AWAR_TRANSPRO_XSTART)->read_int(); |
---|
224 | |
---|
225 | error = arb_r2a(GLOBAL.gb_main, strcmp(mode, "fields") == 0, save2fields, startpos, translate_all, ali_source, ali_dest); |
---|
226 | if (!error) error = GBT_check_data(GLOBAL.gb_main, 0); |
---|
227 | |
---|
228 | free(mode); |
---|
229 | free(ali_dest); |
---|
230 | free(ali_source); |
---|
231 | } |
---|
232 | GB_end_transaction_show_error(GLOBAL.gb_main, error, aw_message); |
---|
233 | } |
---|
234 | |
---|
235 | static void nt_trans_cursorpos_changed(AW_root *awr) { |
---|
236 | int pos = bio2info(awr->awar(AWAR_CURSOR_POSITION)->read_int()); |
---|
237 | pos = pos % 3; |
---|
238 | awr->awar(AWAR_TRANSPRO_POS)->write_int(pos); |
---|
239 | } |
---|
240 | |
---|
241 | AW_window *NT_create_dna_2_pro_window(AW_root *root) { |
---|
242 | GB_transaction ta(GLOBAL.gb_main); |
---|
243 | |
---|
244 | AW_window_simple *aws = new AW_window_simple; |
---|
245 | aws->init(root, "TRANSLATE_DNA_TO_PRO", "TRANSLATE DNA TO PRO"); |
---|
246 | |
---|
247 | aws->load_xfig("transpro.fig"); |
---|
248 | |
---|
249 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
250 | aws->at("close"); |
---|
251 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
252 | |
---|
253 | aws->callback(makeHelpCallback("translate_dna_2_pro.hlp")); |
---|
254 | aws->at("help"); |
---|
255 | aws->create_button("HELP", "HELP", "H"); |
---|
256 | |
---|
257 | aws->at("source"); |
---|
258 | awt_create_selection_list_on_alignments(GLOBAL.gb_main, (AW_window *)aws, AWAR_TRANSPRO_SOURCE, "dna=:rna="); |
---|
259 | |
---|
260 | aws->at("dest"); |
---|
261 | awt_create_selection_list_on_alignments(GLOBAL.gb_main, (AW_window *)aws, AWAR_TRANSPRO_DEST, "pro=:ami="); |
---|
262 | |
---|
263 | root->awar_int(AWAR_PROTEIN_TYPE, AWAR_PROTEIN_TYPE_bacterial_code_index, GLOBAL.gb_main); |
---|
264 | aws->at("table"); |
---|
265 | aws->create_option_menu(AWAR_PROTEIN_TYPE, true); |
---|
266 | for (int code_nr=0; code_nr<AWT_CODON_TABLES; code_nr++) { |
---|
267 | aws->insert_option(AWT_get_codon_code_name(code_nr), "", code_nr); |
---|
268 | } |
---|
269 | aws->update_option_menu(); |
---|
270 | |
---|
271 | aws->at("mode"); |
---|
272 | aws->create_toggle_field(AWAR_TRANSPRO_MODE, 0, ""); |
---|
273 | aws->insert_toggle("from fields 'codon_start' and 'transl_table'", "", "fields"); |
---|
274 | aws->insert_default_toggle("use settings below (same for all species):", "", "settings"); |
---|
275 | aws->update_toggle_field(); |
---|
276 | |
---|
277 | aws->at("pos"); |
---|
278 | aws->create_option_menu(AWAR_TRANSPRO_POS, true); |
---|
279 | for (int p = 1; p <= 3; ++p) { |
---|
280 | char label[2] = { char(p+'0'), 0 }; |
---|
281 | aws->insert_option(label, label, bio2info(p)); |
---|
282 | } |
---|
283 | aws->update_option_menu(); |
---|
284 | aws->get_root()->awar_int(AWAR_CURSOR_POSITION)->add_callback(nt_trans_cursorpos_changed); |
---|
285 | |
---|
286 | aws->at("write"); |
---|
287 | aws->label("Save settings (to 'codon_start'+'transl_table')"); |
---|
288 | aws->create_toggle(AWAR_TRANSPRO_WRITE); |
---|
289 | |
---|
290 | aws->at("start"); |
---|
291 | aws->label("Translate all data"); |
---|
292 | aws->create_toggle(AWAR_TRANSPRO_XSTART); |
---|
293 | |
---|
294 | aws->at("translate"); |
---|
295 | aws->callback(transpro_event); |
---|
296 | aws->highlight(); |
---|
297 | aws->create_button("TRANSLATE", "TRANSLATE", "T"); |
---|
298 | |
---|
299 | aws->window_fit(); |
---|
300 | |
---|
301 | return aws; |
---|
302 | } |
---|
303 | |
---|
304 | // Realign a dna alignment with a given protein source |
---|
305 | |
---|
306 | static int synchronizeCodons(const char *proteins, const char *dna, int minCatchUp, int maxCatchUp, int *foundCatchUp, |
---|
307 | const AWT_allowedCode& initially_allowed_code, AWT_allowedCode& allowed_code_left) { |
---|
308 | |
---|
309 | for (int catchUp=minCatchUp; catchUp<=maxCatchUp; catchUp++) { |
---|
310 | const char *dna_start = dna+catchUp; |
---|
311 | AWT_allowedCode allowed_code; |
---|
312 | allowed_code = initially_allowed_code; |
---|
313 | |
---|
314 | for (int p=0; ; p++) { |
---|
315 | char prot = proteins[p]; |
---|
316 | |
---|
317 | if (!prot) { // all proteins were synchronized |
---|
318 | *foundCatchUp = catchUp; |
---|
319 | return 1; |
---|
320 | } |
---|
321 | |
---|
322 | if (!AWT_is_codon(prot, dna_start, allowed_code, allowed_code_left)) break; |
---|
323 | |
---|
324 | allowed_code = allowed_code_left; // if synchronized: use left codes as allowed codes! |
---|
325 | dna_start += 3; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | return 0; |
---|
330 | } |
---|
331 | |
---|
332 | #define SYNC_LENGTH 4 |
---|
333 | // every X in amino-alignment, it represents 1 to 3 bases in DNA-Alignment |
---|
334 | // SYNC_LENGTH is the # of codons which will be synchronized (ahead!) |
---|
335 | // before deciding "X was realigned correctly" |
---|
336 | |
---|
337 | static GB_ERROR arb_transdna(GBDATA *gb_main, char *ali_source, char *ali_dest, long *neededLength) { |
---|
338 | AP_initialize_codon_tables(); |
---|
339 | |
---|
340 | GBDATA *gb_source = GBT_get_alignment(gb_main, ali_source); if (!gb_source) return "Please select a valid source alignment"; |
---|
341 | GBDATA *gb_dest = GBT_get_alignment(gb_main, ali_dest); if (!gb_dest) return "Please select a valid destination alignment"; |
---|
342 | |
---|
343 | long ali_len = GBT_get_alignment_len(gb_main, ali_dest); |
---|
344 | long max_wanted_ali_len = 0; |
---|
345 | GB_ERROR error = 0; |
---|
346 | |
---|
347 | long no_of_marked_species = GBT_count_marked_species(gb_main); |
---|
348 | long no_of_realigned_species = 0; |
---|
349 | |
---|
350 | arb_progress progress("Re-aligner", no_of_marked_species); |
---|
351 | progress.auto_subtitles("Re-aligning species"); |
---|
352 | |
---|
353 | int ignore_fail_pos = 0; |
---|
354 | |
---|
355 | for (GBDATA *gb_species = GBT_first_marked_species(gb_main); |
---|
356 | !error && gb_species; |
---|
357 | gb_species = GBT_next_marked_species(gb_species)) |
---|
358 | { |
---|
359 | gb_source = GB_entry(gb_species, ali_source); if (!gb_source) continue; |
---|
360 | GBDATA *gb_source_data = GB_entry(gb_source, "data"); if (!gb_source_data) continue; |
---|
361 | gb_dest = GB_entry(gb_species, ali_dest); if (!gb_dest) continue; |
---|
362 | GBDATA *gb_dest_data = GB_entry(gb_dest, "data"); if (!gb_dest_data) continue; |
---|
363 | |
---|
364 | char *source = GB_read_string(gb_source_data); if (!source) { GB_print_error(); continue; } |
---|
365 | char *dest = GB_read_string(gb_dest_data); if (!dest) { GB_print_error(); continue; } |
---|
366 | |
---|
367 | long source_len = GB_read_string_count(gb_source_data); |
---|
368 | long dest_len = GB_read_string_count(gb_dest_data); |
---|
369 | |
---|
370 | // compress destination DNA (=remove align-characters): |
---|
371 | char *compressed_dest = (char*)malloc(dest_len+1); |
---|
372 | { |
---|
373 | char *f = dest; |
---|
374 | char *t = compressed_dest; |
---|
375 | |
---|
376 | while (1) { |
---|
377 | char c = *f++; |
---|
378 | if (!c) break; |
---|
379 | if (c!='.' && c!='-') *t++ = c; |
---|
380 | } |
---|
381 | *t = 0; |
---|
382 | } |
---|
383 | |
---|
384 | int failed = 0; |
---|
385 | const char *fail_reason = 0; |
---|
386 | |
---|
387 | long wanted_ali_len = source_len*3L; |
---|
388 | char *buffer = (char*)malloc(ali_len+1); |
---|
389 | if (ali_len<wanted_ali_len) { |
---|
390 | failed = 1; |
---|
391 | fail_reason = GBS_global_string("Alignment '%s' is too short (increase its length to %li)", ali_dest, wanted_ali_len); |
---|
392 | ignore_fail_pos = 1; |
---|
393 | |
---|
394 | if (wanted_ali_len>max_wanted_ali_len) max_wanted_ali_len = wanted_ali_len; |
---|
395 | } |
---|
396 | |
---|
397 | AWT_allowedCode allowed_code; // default: all allowed |
---|
398 | |
---|
399 | if (!failed) { |
---|
400 | int arb_transl_table, codon_start; |
---|
401 | GB_ERROR local_error = AWT_getTranslationInfo(gb_species, arb_transl_table, codon_start); |
---|
402 | if (local_error) { |
---|
403 | failed = 1; |
---|
404 | fail_reason = GBS_global_string("Error while reading 'transl_table' (%s)", local_error); |
---|
405 | ignore_fail_pos = 1; |
---|
406 | } |
---|
407 | else if (arb_transl_table >= 0) { |
---|
408 | // we found a 'transl_table' entry -> restrict used code to the code stored there |
---|
409 | allowed_code.forbidAllBut(arb_transl_table); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | char *d = compressed_dest; |
---|
414 | char *s = source; |
---|
415 | |
---|
416 | if (!failed) { |
---|
417 | char *p = buffer; |
---|
418 | int x_count = 0; |
---|
419 | const char *x_start = 0; |
---|
420 | |
---|
421 | for (;;) { |
---|
422 | char c = *s++; |
---|
423 | if (!c) { |
---|
424 | if (x_count) { |
---|
425 | int off = -(x_count*3); |
---|
426 | while (d[0]) { |
---|
427 | p[off++] = *d++; |
---|
428 | } |
---|
429 | } |
---|
430 | break; |
---|
431 | } |
---|
432 | |
---|
433 | if (c=='.' || c=='-') { |
---|
434 | p[0] = p[1] = p[2] = c; |
---|
435 | p += 3; |
---|
436 | } |
---|
437 | else if (toupper(c)=='X') { // one X represents 1 to 3 DNAs |
---|
438 | x_start = s-1; |
---|
439 | x_count = 1; |
---|
440 | int gap_count = 0; |
---|
441 | |
---|
442 | for (;;) { |
---|
443 | char c2 = toupper(s[0]); |
---|
444 | |
---|
445 | if (c2=='X') { |
---|
446 | x_count++; |
---|
447 | } |
---|
448 | else { |
---|
449 | if (c2!='.' && c2!='-') break; |
---|
450 | gap_count++; |
---|
451 | } |
---|
452 | s++; |
---|
453 | } |
---|
454 | |
---|
455 | int setgap = (x_count+gap_count)*3; |
---|
456 | memset(p, '.', setgap); |
---|
457 | p += setgap; |
---|
458 | } |
---|
459 | else { |
---|
460 | AWT_allowedCode allowed_code_left; |
---|
461 | |
---|
462 | if (x_count) { // synchronize |
---|
463 | char protein[SYNC_LENGTH+1]; |
---|
464 | int count; |
---|
465 | { |
---|
466 | int off; |
---|
467 | |
---|
468 | protein[0] = toupper(c); |
---|
469 | for (count=1, off=0; count<SYNC_LENGTH; off++) { |
---|
470 | char c2 = s[off]; |
---|
471 | |
---|
472 | if (c2!='.' && c2!='-') { |
---|
473 | c2 = toupper(c2); |
---|
474 | if (c2=='X') break; // can't sync X |
---|
475 | protein[count++] = c2; |
---|
476 | } |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | nt_assert(count>=1); |
---|
481 | protein[count] = 0; |
---|
482 | |
---|
483 | int catchUp; |
---|
484 | if (count<SYNC_LENGTH) { |
---|
485 | int sync_possibilities = 0; |
---|
486 | int *sync_possible_with_catchup = new int[x_count*3+1]; |
---|
487 | int maxCatchup = x_count*3; |
---|
488 | |
---|
489 | catchUp = x_count-1; |
---|
490 | for (;;) { |
---|
491 | if (!synchronizeCodons(protein, d, catchUp+1, maxCatchup, &catchUp, allowed_code, allowed_code_left)) { |
---|
492 | break; |
---|
493 | } |
---|
494 | sync_possible_with_catchup[sync_possibilities++] = catchUp; |
---|
495 | } |
---|
496 | |
---|
497 | if (sync_possibilities==0) { |
---|
498 | delete [] sync_possible_with_catchup; |
---|
499 | failed = 1; |
---|
500 | fail_reason = "Can't synchronize after 'X'"; |
---|
501 | break; |
---|
502 | } |
---|
503 | if (sync_possibilities>1) { |
---|
504 | delete [] sync_possible_with_catchup; |
---|
505 | failed = 1; |
---|
506 | fail_reason = "Not enough data behind 'X' (please contact ARB-Support)"; |
---|
507 | break; |
---|
508 | } |
---|
509 | |
---|
510 | nt_assert(sync_possibilities==1); |
---|
511 | catchUp = sync_possible_with_catchup[0]; |
---|
512 | delete [] sync_possible_with_catchup; |
---|
513 | } |
---|
514 | else if (!synchronizeCodons(protein, d, x_count, x_count*3, &catchUp, allowed_code, allowed_code_left)) { |
---|
515 | failed = 1; |
---|
516 | fail_reason = "Can't synchronize after 'X'"; |
---|
517 | break; |
---|
518 | } |
---|
519 | |
---|
520 | allowed_code = allowed_code_left; |
---|
521 | |
---|
522 | // copy 'catchUp' characters (they are the content of the found Xs): |
---|
523 | { |
---|
524 | const char *after = s-1; |
---|
525 | const char *i; |
---|
526 | int off = int(after-x_start); |
---|
527 | nt_assert(off>=x_count); |
---|
528 | off = -(off*3); |
---|
529 | int x_rest = x_count; |
---|
530 | |
---|
531 | for (i=x_start; i<after; i++) { |
---|
532 | switch (i[0]) { |
---|
533 | case 'x': |
---|
534 | case 'X': |
---|
535 | { |
---|
536 | int take_per_X = catchUp/x_rest; |
---|
537 | int o; |
---|
538 | for (o=0; o<3; o++) { |
---|
539 | if (o<take_per_X) { |
---|
540 | p[off++] = *d++; |
---|
541 | } |
---|
542 | else { |
---|
543 | p[off++] = '.'; |
---|
544 | } |
---|
545 | } |
---|
546 | x_rest--; |
---|
547 | break; |
---|
548 | } |
---|
549 | case '.': |
---|
550 | case '-': |
---|
551 | p[off++] = i[0]; |
---|
552 | p[off++] = i[0]; |
---|
553 | p[off++] = i[0]; |
---|
554 | break; |
---|
555 | default: |
---|
556 | nt_assert(0); |
---|
557 | break; |
---|
558 | } |
---|
559 | } |
---|
560 | } |
---|
561 | x_count = 0; |
---|
562 | } |
---|
563 | else { |
---|
564 | const char *why_fail; |
---|
565 | if (!AWT_is_codon(c, d, allowed_code, allowed_code_left, &why_fail)) { |
---|
566 | failed = 1; |
---|
567 | fail_reason = GBS_global_string("Not a codon (%s)", why_fail); |
---|
568 | break; |
---|
569 | } |
---|
570 | |
---|
571 | allowed_code = allowed_code_left; |
---|
572 | } |
---|
573 | |
---|
574 | // copy one codon: |
---|
575 | p[0] = d[0]; |
---|
576 | p[1] = d[1]; |
---|
577 | p[2] = d[2]; |
---|
578 | |
---|
579 | p += 3; |
---|
580 | d += 3; |
---|
581 | } |
---|
582 | } |
---|
583 | |
---|
584 | if (!failed) { |
---|
585 | int len = p-buffer; |
---|
586 | int rest = ali_len-len; |
---|
587 | |
---|
588 | memset(p, '.', rest); |
---|
589 | p += rest; |
---|
590 | p[0] = 0; |
---|
591 | } |
---|
592 | } |
---|
593 | |
---|
594 | if (failed) { |
---|
595 | int source_fail_pos = (s-1)-source+1; |
---|
596 | int dest_fail_pos = 0; |
---|
597 | { |
---|
598 | int fail_d_base_count = d-compressed_dest; |
---|
599 | char *dp = dest; |
---|
600 | |
---|
601 | for (;;) { |
---|
602 | char c = *dp++; |
---|
603 | |
---|
604 | if (!c) { |
---|
605 | nt_assert(c); |
---|
606 | break; |
---|
607 | } |
---|
608 | if (c!='.' && c!='-') { |
---|
609 | if (!fail_d_base_count) { |
---|
610 | dest_fail_pos = (dp-1)-dest+1; |
---|
611 | break; |
---|
612 | } |
---|
613 | fail_d_base_count--; |
---|
614 | } |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | { |
---|
619 | char *dup_fail_reason = strdup(fail_reason); // otherwise it may be destroyed by GBS_global_string |
---|
620 | aw_message(GBS_global_string("Automatic re-align failed for '%s'", GBT_read_name(gb_species))); |
---|
621 | |
---|
622 | if (ignore_fail_pos) { |
---|
623 | aw_message(GBS_global_string("Reason: %s", dup_fail_reason)); |
---|
624 | } |
---|
625 | else { |
---|
626 | aw_message(GBS_global_string("Reason: %s at %s:%i / %s:%i", dup_fail_reason, ali_source, source_fail_pos, ali_dest, dest_fail_pos)); |
---|
627 | } |
---|
628 | |
---|
629 | free(dup_fail_reason); |
---|
630 | } |
---|
631 | |
---|
632 | } |
---|
633 | else { |
---|
634 | nt_assert(strlen(buffer) == (unsigned)ali_len); |
---|
635 | |
---|
636 | // re-alignment successful |
---|
637 | error = GB_write_string(gb_dest_data, buffer); |
---|
638 | |
---|
639 | if (!error) { |
---|
640 | int explicit_table_known = allowed_code.explicit_table(); |
---|
641 | |
---|
642 | if (explicit_table_known >= 0) { // we know the exact code -> write codon_start and transl_table |
---|
643 | const int codon_start = 1; // by definition (after realignment) |
---|
644 | error = AWT_saveTranslationInfo(gb_species, explicit_table_known, codon_start); |
---|
645 | } |
---|
646 | else { // we dont know the exact code -> delete codon_start and transl_table |
---|
647 | error = AWT_removeTranslationInfo(gb_species); |
---|
648 | } |
---|
649 | } |
---|
650 | } |
---|
651 | |
---|
652 | free(buffer); |
---|
653 | free(compressed_dest); |
---|
654 | free(dest); |
---|
655 | free(source); |
---|
656 | |
---|
657 | progress.inc_and_check_user_abort(error); |
---|
658 | no_of_realigned_species++; |
---|
659 | } |
---|
660 | |
---|
661 | *neededLength = max_wanted_ali_len; |
---|
662 | |
---|
663 | if (!error) { |
---|
664 | int not_realigned = no_of_marked_species - no_of_realigned_species; |
---|
665 | if (not_realigned>0) { |
---|
666 | aw_message(GBS_global_string("Did not try to realign %i species (source/dest alignment missing?)", not_realigned)); |
---|
667 | } |
---|
668 | } |
---|
669 | |
---|
670 | if (!error) error = GBT_check_data(gb_main,ali_dest); |
---|
671 | |
---|
672 | return error; |
---|
673 | } |
---|
674 | |
---|
675 | #undef SYNC_LENGTH |
---|
676 | |
---|
677 | |
---|
678 | static void transdna_event(AW_window *aww) { |
---|
679 | AW_root *aw_root = aww->get_root(); |
---|
680 | char *ali_source = aw_root->awar(AWAR_TRANSPRO_DEST)->read_string(); |
---|
681 | char *ali_dest = aw_root->awar(AWAR_TRANSPRO_SOURCE)->read_string(); |
---|
682 | long neededLength = -1; |
---|
683 | bool retrying = false; |
---|
684 | GB_ERROR error = 0; |
---|
685 | |
---|
686 | while (!error && neededLength) { |
---|
687 | error = GB_begin_transaction(GLOBAL.gb_main); |
---|
688 | if (!error) error = arb_transdna(GLOBAL.gb_main, ali_source, ali_dest, &neededLength); |
---|
689 | error = GB_end_transaction(GLOBAL.gb_main, error); |
---|
690 | |
---|
691 | if (!error && neededLength>0) { |
---|
692 | if (retrying || !aw_ask_sure("increase_ali_length", GBS_global_string("Increase length of '%s' to %li?", ali_dest, neededLength))) { |
---|
693 | error = GBS_global_string("Missing %li columns in alignment '%s'", neededLength, ali_dest); |
---|
694 | } |
---|
695 | else { |
---|
696 | error = GB_begin_transaction(GLOBAL.gb_main); |
---|
697 | if (!error) error = GBT_set_alignment_len(GLOBAL.gb_main, ali_dest, neededLength); // @@@ has no effect ? ? why ? |
---|
698 | error = GB_end_transaction(GLOBAL.gb_main, error); |
---|
699 | |
---|
700 | if (!error) { |
---|
701 | aw_message(GBS_global_string("Alignment length of '%s' has been set to %li\n" |
---|
702 | "running re-aligner again!", |
---|
703 | ali_dest, neededLength)); |
---|
704 | retrying = true; |
---|
705 | neededLength = -1; |
---|
706 | } |
---|
707 | } |
---|
708 | } |
---|
709 | else { |
---|
710 | neededLength = 0; |
---|
711 | } |
---|
712 | } |
---|
713 | |
---|
714 | if (error) aw_message(error); |
---|
715 | free(ali_dest); |
---|
716 | free(ali_source); |
---|
717 | } |
---|
718 | |
---|
719 | AW_window *NT_create_realign_dna_window(AW_root *root) { |
---|
720 | AW_window_simple *aws = new AW_window_simple; |
---|
721 | aws->init(root, "REALIGN_DNA", "REALIGN DNA"); |
---|
722 | |
---|
723 | aws->load_xfig("transdna.fig"); |
---|
724 | |
---|
725 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
726 | aws->at("close"); |
---|
727 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
728 | |
---|
729 | aws->callback(makeHelpCallback("realign_dna.hlp")); |
---|
730 | aws->at("help"); |
---|
731 | aws->create_button("HELP", "HELP", "H"); |
---|
732 | |
---|
733 | aws->at("source"); |
---|
734 | awt_create_selection_list_on_alignments(GLOBAL.gb_main, (AW_window *)aws, AWAR_TRANSPRO_SOURCE, "dna=:rna="); |
---|
735 | aws->at("dest"); |
---|
736 | awt_create_selection_list_on_alignments(GLOBAL.gb_main, (AW_window *)aws, AWAR_TRANSPRO_DEST, "pro=:ami="); |
---|
737 | |
---|
738 | aws->at("realign"); |
---|
739 | aws->callback(transdna_event); |
---|
740 | aws->highlight(); |
---|
741 | aws->create_button("REALIGN", "REALIGN", "T"); |
---|
742 | |
---|
743 | return aws; |
---|
744 | } |
---|
745 | |
---|
746 | |
---|
747 | void NT_create_transpro_variables(AW_root *root, AW_default db1) |
---|
748 | { |
---|
749 | root->awar_string(AWAR_TRANSPRO_SOURCE, "", db1); |
---|
750 | root->awar_string(AWAR_TRANSPRO_DEST, "", db1); |
---|
751 | root->awar_string(AWAR_TRANSPRO_MODE, "settings", db1); |
---|
752 | root->awar_int(AWAR_TRANSPRO_POS, 0, db1); |
---|
753 | root->awar_int(AWAR_TRANSPRO_XSTART, 1, db1); |
---|
754 | root->awar_int(AWAR_TRANSPRO_WRITE, 0, db1); |
---|
755 | } |
---|