source: tags/arb_5.0/AWT/AWT_translate.cxx

Last change on this file was 5825, checked in by westram, 15 years ago
  • replaced GB_entry/GB_create/GB_search+GB_read_XXX by GBT_read_XXX
  • improved error handling in many functions
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
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_getTranslationInfo(GBDATA *gb_item, int& arb_transl_table, int& codon_start) {
35    // looks for sub-entries 'transl_table' and 'codon_start' of species (works for genes as well)
36    // if found -> test for validity and translate 'transl_table' from EMBL to ARB table number
37    //
38    // returns: an error in case of problems
39    //
40    // 'arb_transl_table' is set to -1 if not found, otherwise it contains the arb table number
41    // 'codon_start'      is set to -1 if not found, otherwise it contains the codon_start (0..2)
42
43    arb_transl_table = -1;          // not found yet
44    codon_start      = -1;          // not found yet
45   
46    GB_ERROR  error           = 0;
47    GBDATA   *gb_transl_table = GB_entry(gb_item, "transl_table");
48
49    if (gb_transl_table) {
50        int embl_table   = atoi(GB_read_char_pntr(gb_transl_table));
51        arb_transl_table = AWT_embl_transl_table_2_arb_code_nr(embl_table);
52        if (arb_transl_table == -1) { // ill. table
53            error = GBS_global_string("Illegal (or unsupported) value (%i) in 'transl_table'", embl_table);
54        }
55    }
56
57    if (!error) {
58        GBDATA *gb_codon_start = GB_entry(gb_item, "codon_start");
59        if (gb_codon_start) {
60            int codon_start_value = atoi(GB_read_char_pntr(gb_codon_start));
61
62            if (codon_start_value<1 || codon_start_value>3) {
63                error = GBS_global_string("Illegal value (%i) in 'codon_start' (allowed: 1..3)", codon_start_value);
64            }
65            else {
66                codon_start = codon_start_value-1; // internal value is 0..2
67            }
68        }
69        else if (arb_transl_table != -1) {
70            // default to codon_start 1
71            error = GBT_write_int(gb_item, "codon_start", 1);
72            if (!error) codon_start = 0; // internal value is 0..2
73        }
74    }
75
76    if (!error && arb_transl_table != codon_start) {
77        if (arb_transl_table == -1) error = "Found 'codon_start', but 'transl_table' is missing";
78        else if (codon_start == -1) error = "Found 'transl_table', but 'codon_start' is missing";
79    }
80
81    if (error) { // append species name to error message
82        error = GBS_global_string("%s (item='%s')", error, GBT_read_name(gb_item));
83    }
84
85    return error;
86}
87
88inline void memcpy3(char *dest, const char *source) {
89    dest[0] = source[0];
90    dest[1] = source[1];
91    dest[2] = source[2];
92}
93
94int 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) {
95    // if translate_all == true -> 'pos' > 1 produces a leading 'X' in protein data
96    //                             (otherwise nucleotides in front of the starting pos are simply ignored)
97    //
98    // if 'create_start_codon' is true and the first generated codon is a start codon of the used
99    //                                 code, a 'M' is inserted instead of the codon
100    // if 'append_stop_codon' is true, the stop codon is appended as '*'. This is only done, if the last
101    //                                 character not already is a stop codon. (Note: provide data with correct size)
102    //
103    // returns:
104    // - the translated protein sequence in 'data'
105    // - the length of the translated protein sequence in 'translatedSize' (if != 0)
106    // - number of stop-codons in translated sequence as result
107
108    gb_assert(pos <= 2);
109
110    for (char *p = data; *p ; p++) {
111        char c = *p;
112        if ((c>='a') && (c<='z')) c = c+'A'-'a';
113        if (c=='U') c = 'T';
114        *p = c;
115    }
116
117    char buffer[4];
118    buffer[3] = 0;
119
120    char *dest  = data;
121
122    if (pos && translate_all) {
123        for (char *p = data; p<data+pos; ++p) {
124            char c = *p;
125            if (c!='.' && c!='-') { // found a nucleotide
126                *dest++ = 'X';
127                break;
128            }
129        }
130    }
131
132    int            stops      = 0;
133    size_t         i          = pos;
134    char           startCodon = 0;
135    const GB_HASH *t2i_hash   = AWT_get_translator(arb_code_nr)->T2iHash();
136
137    if (create_start_codon) {
138        memcpy3(buffer, data+pos);
139        startCodon = AWT_is_start_codon(buffer, arb_code_nr);
140    }
141
142    for (char *p = data+pos; i+2<size; p+=3,i+=3) {
143        memcpy3(buffer, p);
144        int spro = (int)GBS_read_hash(t2i_hash,buffer);
145        int C;
146        if (!spro) {
147            C = 'X';
148        }
149        else {
150            if (spro == '*') stops++;
151            C = spro;
152            if (spro == 's') C = 'S';
153        }
154        *(dest++) = (char)C;
155    }
156
157    int tsize = dest-data;
158
159    if (tsize>0) {            // at least 1 amino written
160        if (create_start_codon && startCodon) data[0] = startCodon;
161        if (append_stop_codon && dest[-1] != '*') {
162            *dest++ = '*';
163            tsize++;
164        }
165    }
166    dest[0] = 0;
167
168    if (translatedSize) *translatedSize = tsize;
169   
170    return stops;
171}
172
173
174
175
176
Note: See TracBrowser for help on using the repository browser.