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