root/branches/stable_5.0/AWT/AWT_translate.cxx

Revision 8301, 6.6 KB (checked in by westram, 5 months ago)
  • fixed some realigner bugs
    • when species did not contain transl_table and codon_start, the realigner inserted only codon_start.
      • the next realignment/translation failed since it expected BOTH or NONE of the two fields.
      • now the realigner
        • either writes both fields (if used transl_table is well-defined)
        • deletes both fields
    • when some/all species did not contain data in source and/or destination alignment, the realigner silently skipped them
      • now prints a message about skipped species
    • errors during realignment (e.g. protection errors) were not shown

Note: grml .. similar patch was already in trunk as [6600]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : AWT_translate.cxx                                 //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in June 2006      //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#include <cstdio>
13#include <cstdlib>
14#include <arbdb.h>
15#include <arbdbt.h>
16
17#include <awt_pro_a_nucs.hxx>
18#include <awt_codon_table.hxx>
19
20#include "awt_translate.hxx"
21
22GB_ERROR AWT_saveTranslationInfo(GBDATA *gb_species, int arb_transl_table, int codon_start) {
23    int embl_transl_table = AWT_arb_code_nr_2_embl_transl_table(arb_transl_table);
24
25    awt_assert(codon_start >= 0 && codon_start<3); // codon_start has to be 0..2
26    awt_assert(embl_transl_table >= 0);
27
28    GB_ERROR error    = GBT_write_string(gb_species, "transl_table", GBS_global_string("%i", embl_transl_table));
29    if (!error) error = GBT_write_string(gb_species, "codon_start",  GBS_global_string("%i", codon_start+1));
30
31    return error;
32}
33
34GB_ERROR AWT_removeTranslationInfo(GBDATA *gb_species) {
35    GB_ERROR error = NULL;
36
37    GBDATA *gb_transl_table    = GB_entry(gb_species, "transl_table");
38    if (gb_transl_table) error = GB_delete(gb_transl_table);
39
40    if (!error) {
41        GBDATA *gb_codon_start    = GB_entry(gb_species, "codon_start");
42        if (gb_codon_start) error = GB_delete(gb_codon_start);
43    }
44
45    return error;
46}
47
48GB_ERROR AWT_getTranslationInfo(GBDATA *gb_item, int& arb_transl_table, int& codon_start) {
49    // looks for sub-entries 'transl_table' and 'codon_start' of species (works for genes as well)
50    // if found -> test for validity and translate 'transl_table' from EMBL to ARB table number
51    //
52    // returns: an error in case of problems
53    //
54    // 'arb_transl_table' is set to -1 if not found, otherwise it contains the arb table number
55    // 'codon_start'      is set to -1 if not found, otherwise it contains the codon_start (0..2)
56
57    arb_transl_table = -1;          // not found yet
58    codon_start      = -1;          // not found yet
59   
60    GB_ERROR  error           = 0;
61    GBDATA   *gb_transl_table = GB_entry(gb_item, "transl_table");
62
63    if (gb_transl_table) {
64        int embl_table   = atoi(GB_read_char_pntr(gb_transl_table));
65        arb_transl_table = AWT_embl_transl_table_2_arb_code_nr(embl_table);
66        if (arb_transl_table == -1) { // ill. table
67            error = GBS_global_string("Illegal (or unsupported) value (%i) in 'transl_table'", embl_table);
68        }
69    }
70
71    if (!error) {
72        GBDATA *gb_codon_start = GB_entry(gb_item, "codon_start");
73        if (gb_codon_start) {
74            int codon_start_value = atoi(GB_read_char_pntr(gb_codon_start));
75
76            if (codon_start_value<1 || codon_start_value>3) {
77                error = GBS_global_string("Illegal value (%i) in 'codon_start' (allowed: 1..3)", codon_start_value);
78            }
79            else {
80                codon_start = codon_start_value-1; // internal value is 0..2
81            }
82        }
83        else if (arb_transl_table != -1) {
84            // default to codon_start 1
85            error = GBT_write_string(gb_item, "codon_start", "1");
86            if (!error) codon_start = 0; // internal value is 0..2
87        }
88    }
89
90    if (!error && arb_transl_table != codon_start) {
91        if (arb_transl_table == -1) error = "Found 'codon_start', but 'transl_table' is missing";
92        else if (codon_start == -1) error = "Found 'transl_table', but 'codon_start' is missing";
93    }
94
95    if (error) { // append species name to error message
96        error = GBS_global_string("%s (item='%s')", error, GBT_read_name(gb_item));
97    }
98
99    return error;
100}
101
102inline void memcpy3(char *dest, const char *source) {
103    dest[0] = source[0];
104    dest[1] = source[1];
105    dest[2] = source[2];
106}
107
108int AWT_pro_a_nucs_convert(int arb_code_nr, char *data, size_t size, size_t pos, bool translate_all, bool create_start_codon, bool append_stop_codon, int *translatedSize) {
109    // if translate_all == true -> 'pos' > 1 produces a leading 'X' in protein data
110    //                             (otherwise nucleotides in front of the starting pos are simply ignored)
111    //
112    // if 'create_start_codon' is true and the first generated codon is a start codon of the used
113    //                                 code, a 'M' is inserted instead of the codon
114    // if 'append_stop_codon' is true, the stop codon is appended as '*'. This is only done, if the last
115    //                                 character not already is a stop codon. (Note: provide data with correct size)
116    //
117    // returns:
118    // - the translated protein sequence in 'data'
119    // - the length of the translated protein sequence in 'translatedSize' (if != 0)
120    // - number of stop-codons in translated sequence as result
121
122    gb_assert(pos <= 2);
123
124    for (char *p = data; *p ; p++) {
125        char c = *p;
126        if ((c>='a') && (c<='z')) c = c+'A'-'a';
127        if (c=='U') c = 'T';
128        *p = c;
129    }
130
131    char buffer[4];
132    buffer[3] = 0;
133
134    char *dest  = data;
135
136    if (pos && translate_all) {
137        for (char *p = data; p<data+pos; ++p) {
138            char c = *p;
139            if (c!='.' && c!='-') { // found a nucleotide
140                *dest++ = 'X';
141                break;
142            }
143        }
144    }
145
146    int            stops      = 0;
147    size_t         i          = pos;
148    char           startCodon = 0;
149    const GB_HASH *t2i_hash   = AWT_get_translator(arb_code_nr)->T2iHash();
150
151    if (create_start_codon) {
152        memcpy3(buffer, data+pos);
153        startCodon = AWT_is_start_codon(buffer, arb_code_nr);
154    }
155
156    for (char *p = data+pos; i+2<size; p+=3,i+=3) {
157        memcpy3(buffer, p);
158        int spro = (int)GBS_read_hash(t2i_hash,buffer);
159        int C;
160        if (!spro) {
161            C = 'X';
162        }
163        else {
164            if (spro == '*') stops++;
165            C = spro;
166            if (spro == 's') C = 'S';
167        }
168        *(dest++) = (char)C;
169    }
170
171    int tsize = dest-data;
172
173    if (tsize>0) {            // at least 1 amino written
174        if (create_start_codon && startCodon) data[0] = startCodon;
175        if (append_stop_codon && dest[-1] != '*') {
176            *dest++ = '*';
177            tsize++;
178        }
179    }
180    dest[0] = 0;
181
182    if (translatedSize) *translatedSize = tsize;
183   
184    return stops;
185}
186
187
188
189
Note: See TracBrowser for help on using the browser.