source: branches/stable/SL/PRONUC/AP_codon_table.hxx

Last change on this file was 17593, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : AP_codon_table.hxx                                //
4//   Purpose   : codon definitions for DNA -> AA translation       //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de)                   //
7//   Institute of Microbiology (Technical University Munich)       //
8//   http://www.arb-home.de/                                       //
9//                                                                 //
10// =============================================================== //
11
12#ifndef AP_CODON_TABLE_HXX
13#define AP_CODON_TABLE_HXX
14
15#ifndef ARB_ASSERT_H
16#include <arb_assert.h>
17#endif
18#ifndef STATIC_ASSERT_H
19#include <static_assert.h>
20#endif
21#ifndef _STDINT_H
22#include <stdint.h>
23#endif
24
25#define pn_assert(cond) arb_assert(cond)
26
27// @@@ fix all historical prefixes to 'PN_' or remove them
28// @@@ fix filenames
29
30// --------------------------------------------------------------------------------
31
32struct AWT_Codon_Code_Definition {
33    const char *name;
34    const char *aa;             // amino-codes
35    const char *startStop;      // may contain 'M', '*' and '-'
36    int         embl_feature_transl_table; // number of transl_table-entry in EMBL/GENEBANK features list
37};
38
39#define AWT_CODON_TABLES 25     // number of different translation tables (aka genetic codes and codon tables)
40#define AWT_MAX_CODONS   64     // maximum of possible codon ( = 4^3)
41
42const int AWAR_PROTEIN_TYPE_bacterial_code_index = 8; // contains the index of the bacterial code table
43
44// --------------------------------------------------------------------------------
45// initialize this module
46
47void AP_initialize_codon_tables(); // call early
48
49// --------------------------------------------------------------------------------
50
51#define ALL_TABLES_MASK ((1<<AWT_CODON_TABLES)-1)
52
53enum TranslationTableIndexType {
54    TTIT_ARB, // arb internal table numbering [0 .. AWT_CODON_TABLES-1]. consecutive.
55    TTIT_EMBL // embl numbering (AWT_Codon_Code_Definition::embl_feature_transl_table)
56};
57
58int TTIT_embl2arb(int embl_code_nr);
59int TTIT_arb2embl(int arb_code_nr);
60
61// --------------------------------------------------------------------------------
62
63class TransTables {
64    uint32_t allowed; // one bit for each arb-translation-table
65
66    static uint32_t bitmask(int nr) { pn_assert(nr >= 0 && nr<AWT_CODON_TABLES); return 1<<nr; }
67
68public:
69    TransTables() { allowAll(); }
70
71    bool is_allowed(int nr) const { return (allowed & bitmask(nr)) != 0; }
72    bool any()  const { return  allowed; }
73    bool none() const { return !allowed; }
74    bool all() const { return allowed == ALL_TABLES_MASK; }
75
76    void allow(int nr) { allowed |= bitmask(nr); }
77    void forbid(int nr) { allowed &= ~bitmask(nr); }
78    void forbid(const TransTables& other) { allowed &= ~other.allowed; }
79
80    void allowAll() {
81        STATIC_ASSERT((sizeof(allowed)*8) > AWT_CODON_TABLES); // one bit wasted
82        allowed = ALL_TABLES_MASK;
83    }
84    void forbidAll() { allowed = 0; }
85    void forbidAllBut(int nr) { allowed &= bitmask(nr); }
86
87    int explicit_table() const {
88        // return explicit table number (or -1 if not exactly one table is allowed)
89        if (any()) {
90            uint32_t tabs = allowed;
91            for (int t = 0; t<AWT_CODON_TABLES; ++t) {
92                if (tabs&1) return (tabs == 1) ? t : -1;
93                tabs >>= 1;
94            }
95            pn_assert(0);
96        }
97        return -1;
98    }
99
100    bool is_subset_of(const TransTables& other) const { return (allowed&other.allowed) == allowed; }
101
102    const char *to_string(TranslationTableIndexType type) const {
103        const int    MAX_LEN = AWT_CODON_TABLES*3-1+1;
104        static char  buffer[MAX_LEN];
105        char        *out     = buffer;
106
107        for (int a = 0; a<AWT_CODON_TABLES; ++a) {
108            if (is_allowed(a)) {
109                int shownNum =
110                    type == TTIT_ARB
111                    ? a
112                    : TTIT_arb2embl(a);
113
114                out += sprintf(out, ",%i", shownNum);
115            }
116        }
117        pn_assert((out-buffer)<MAX_LEN);
118        out[0] = 0;
119        return buffer+1;
120    }
121
122};
123
124// --------------------------------------------------------------------------------
125
126bool        AWT_is_codon(char protein, const char *const dna, const TransTables& allowed, TransTables& allowed_left, const char **fail_reason_ptr = NULp);
127const char *AP_get_codons(char protein, int code_nr);
128
129const char *getAminoAcidAbbr(char aa);
130const char* AWT_get_codon_code_name(int code);
131
132#ifdef DEBUG
133void AWT_dump_codons(TranslationTableIndexType type, bool skipX);
134void test_AWT_get_codons();
135#endif
136
137// --------------------------------------------------------------------------------
138
139#else
140#error AP_codon_table.hxx included twice
141#endif // AP_CODON_TABLE_HXX
Note: See TracBrowser for help on using the repository browser.