source: tags/svn.1.5.4/NTREE/ad_transpro.cxx

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