| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : AP_codon_table.hxx // |
|---|
| 4 | // Purpose : // |
|---|
| 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 | // -------------------------------------------------------------------------------- |
|---|
| 28 | |
|---|
| 29 | struct AWT_Codon_Code_Definition { |
|---|
| 30 | const char *name; |
|---|
| 31 | const char *aa; // amino-codes |
|---|
| 32 | const char *starts; |
|---|
| 33 | int embl_feature_transl_table; // number of transl_table-entry in EMBL/GENEBANK features list |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | #define AWT_CODON_TABLES 17 // number of different Amino-Translation-Tables |
|---|
| 37 | #define AWT_MAX_CODONS 64 // maximum of possible codon ( = 4^3) |
|---|
| 38 | |
|---|
| 39 | const int AWAR_PROTEIN_TYPE_bacterial_code_index = 8; // contains the index of the bacterial code table |
|---|
| 40 | |
|---|
| 41 | // -------------------------------------------------------------------------------- |
|---|
| 42 | |
|---|
| 43 | #define ALL_TABLES_MASK ((1<<AWT_CODON_TABLES)-1) |
|---|
| 44 | |
|---|
| 45 | class TransTables { |
|---|
| 46 | uint32_t allowed; |
|---|
| 47 | |
|---|
| 48 | static uint32_t bitmask(int nr) { pn_assert(nr >= 0 && nr<AWT_CODON_TABLES); return 1<<nr; } |
|---|
| 49 | |
|---|
| 50 | public: |
|---|
| 51 | TransTables() { allowAll(); } |
|---|
| 52 | |
|---|
| 53 | bool is_allowed(int nr) const { return (allowed & bitmask(nr)) != 0; } |
|---|
| 54 | bool any() const { return allowed; } |
|---|
| 55 | bool none() const { return !allowed; } |
|---|
| 56 | bool all() const { return allowed == ALL_TABLES_MASK; } |
|---|
| 57 | |
|---|
| 58 | void allow(int nr) { allowed |= bitmask(nr); } |
|---|
| 59 | void forbid(int nr) { allowed &= ~bitmask(nr); } |
|---|
| 60 | void forbid(const TransTables& other) { allowed &= ~other.allowed; } |
|---|
| 61 | |
|---|
| 62 | void allowAll() { |
|---|
| 63 | STATIC_ASSERT(sizeof(allowed)*8 > AWT_CODON_TABLES); // one bit wasted |
|---|
| 64 | allowed = ALL_TABLES_MASK; |
|---|
| 65 | } |
|---|
| 66 | void forbidAll() { allowed = 0; } |
|---|
| 67 | void forbidAllBut(int nr) { allowed &= bitmask(nr); } |
|---|
| 68 | |
|---|
| 69 | int explicit_table() const { |
|---|
| 70 | // return explicit table number (or -1 if not exactly one table is allowed) |
|---|
| 71 | if (any()) { |
|---|
| 72 | uint32_t tabs = allowed; |
|---|
| 73 | for (int t = 0; t<AWT_CODON_TABLES; ++t) { |
|---|
| 74 | if (tabs&1) return (tabs == 1) ? t : -1; |
|---|
| 75 | tabs >>= 1; |
|---|
| 76 | } |
|---|
| 77 | pn_assert(0); |
|---|
| 78 | } |
|---|
| 79 | return -1; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | bool is_subset_of(const TransTables& other) const { return (allowed&other.allowed) == allowed; } |
|---|
| 83 | |
|---|
| 84 | const char *to_string() const { |
|---|
| 85 | const int MAX_LEN = 42; |
|---|
| 86 | static char buffer[MAX_LEN]; |
|---|
| 87 | char *out = buffer; |
|---|
| 88 | |
|---|
| 89 | for (int a = 0; a<AWT_CODON_TABLES; ++a) { |
|---|
| 90 | if (is_allowed(a)) { |
|---|
| 91 | out += sprintf(out, ",%i", a); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | pn_assert((out-buffer)<MAX_LEN); |
|---|
| 95 | out[0] = 0; |
|---|
| 96 | return buffer+1; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | // -------------------------------------------------------------------------------- |
|---|
| 102 | |
|---|
| 103 | void AP_initialize_codon_tables(); |
|---|
| 104 | |
|---|
| 105 | int AWT_embl_transl_table_2_arb_code_nr(int embl_code_nr); |
|---|
| 106 | int AWT_arb_code_nr_2_embl_transl_table(int arb_code_nr); |
|---|
| 107 | |
|---|
| 108 | bool AWT_is_codon(char protein, const char *const dna, const TransTables& allowed, TransTables& allowed_left, const char **fail_reason_ptr = 0); |
|---|
| 109 | const char *AP_get_codons(char protein, int code_nr); |
|---|
| 110 | |
|---|
| 111 | char AWT_is_start_codon(const char *dna, int arb_code_nr); |
|---|
| 112 | |
|---|
| 113 | const char *AP_get_protein_name(char protein); |
|---|
| 114 | const char* AWT_get_codon_code_name(int code); |
|---|
| 115 | |
|---|
| 116 | #ifdef DEBUG |
|---|
| 117 | void AWT_dump_codons(); |
|---|
| 118 | void test_AWT_get_codons(); |
|---|
| 119 | #endif |
|---|
| 120 | |
|---|
| 121 | // -------------------------------------------------------------------------------- |
|---|
| 122 | |
|---|
| 123 | #else |
|---|
| 124 | #error AP_codon_table.hxx included twice |
|---|
| 125 | #endif // AP_CODON_TABLE_HXX |
|---|