| 1 | // ================================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : SEC_bonddef.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in September 2007 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // ================================================================= // |
|---|
| 11 | |
|---|
| 12 | #include "SEC_bonddef.hxx" |
|---|
| 13 | #include "SEC_defs.hxx" |
|---|
| 14 | |
|---|
| 15 | #include <arbdbt.h> |
|---|
| 16 | |
|---|
| 17 | #include <cctype> |
|---|
| 18 | |
|---|
| 19 | void SEC_bond_def::clear() |
|---|
| 20 | { |
|---|
| 21 | int i, j; |
|---|
| 22 | for (i=0; i<SEC_BOND_BASE_CHARS; i++) { |
|---|
| 23 | for (j=0; j<SEC_BOND_BASE_CHARS; j++) { |
|---|
| 24 | bond[i][j] = ' '; |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | int SEC_bond_def::get_index(char base) const |
|---|
| 30 | { |
|---|
| 31 | if (base == 0) return -1; |
|---|
| 32 | |
|---|
| 33 | const char *allowed = SEC_BOND_BASE_CHAR; |
|---|
| 34 | const char *found = strchr(allowed, toupper(base)); |
|---|
| 35 | |
|---|
| 36 | if (!found) return -1; |
|---|
| 37 | |
|---|
| 38 | int idx = int(found-allowed); |
|---|
| 39 | sec_assert(idx>=0 && idx<SEC_BOND_BASE_CHARS); |
|---|
| 40 | return idx; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | GB_ERROR SEC_bond_def::insert(const char *pairs, char pair_char) |
|---|
| 44 | { |
|---|
| 45 | GB_ERROR error = 0; |
|---|
| 46 | |
|---|
| 47 | if (pair_char==0) pair_char = ' '; |
|---|
| 48 | |
|---|
| 49 | if (!strchr(SEC_BOND_PAIR_CHAR, pair_char)) { |
|---|
| 50 | error = GBS_global_string("Illegal pair-character '%c' (allowed: '%s')", pair_char, SEC_BOND_PAIR_CHAR); |
|---|
| 51 | } |
|---|
| 52 | else { |
|---|
| 53 | char c1 = 0; |
|---|
| 54 | int idx = 0; |
|---|
| 55 | |
|---|
| 56 | while (1) { |
|---|
| 57 | char c2 = pairs[idx++]; |
|---|
| 58 | |
|---|
| 59 | if (!c2) { // end of string |
|---|
| 60 | if (c1) error = "Odd number of characters in pair definition"; |
|---|
| 61 | break; |
|---|
| 62 | } |
|---|
| 63 | if (c2 == ' ') continue; // ignore spaces |
|---|
| 64 | if (c1==0) { c1 = c2; continue; } // store first char |
|---|
| 65 | |
|---|
| 66 | int i1 = get_index(c1); |
|---|
| 67 | int i2 = get_index(c2); |
|---|
| 68 | |
|---|
| 69 | if (i1==-1 || i2==-1) { |
|---|
| 70 | char ic = i1==-1 ? c1 : c2; |
|---|
| 71 | error = GBS_global_string("Illegal base-character '%c' (allowed: '%s')", ic, SEC_BOND_BASE_CHAR); |
|---|
| 72 | break; |
|---|
| 73 | } |
|---|
| 74 | else { |
|---|
| 75 | bond[i1][i2] = pair_char; |
|---|
| 76 | bond[i2][i1] = pair_char; |
|---|
| 77 | } |
|---|
| 78 | c1 = 0; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | if (error && pair_char != '@') { |
|---|
| 82 | insert(pairs, '@'); // means error |
|---|
| 83 | } |
|---|
| 84 | return error; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | char SEC_bond_def::get_bond(char base1, char base2) const |
|---|
| 88 | { |
|---|
| 89 | int i1 = get_index(base1); |
|---|
| 90 | int i2 = get_index(base2); |
|---|
| 91 | |
|---|
| 92 | if (i1==-1 || i2==-1) { |
|---|
| 93 | return ' '; |
|---|
| 94 | } |
|---|
| 95 | return bond[i1][i2]; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | char *SEC_bond_def::get_pair_string(char pair_char) { |
|---|
| 99 | char *str = (char*)malloc(5*5*3+1); |
|---|
| 100 | char *ins = str; |
|---|
| 101 | |
|---|
| 102 | for (int i = 0; i<SEC_BOND_BASE_CHARS; ++i) { |
|---|
| 103 | for (int j = i; j<SEC_BOND_BASE_CHARS; ++j) { |
|---|
| 104 | if (bond[i][j] == pair_char) { |
|---|
| 105 | if (ins>str) *ins++ = ' '; |
|---|
| 106 | *ins++ = SEC_BOND_BASE_CHAR[i]; |
|---|
| 107 | *ins++ = SEC_BOND_BASE_CHAR[j]; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | *ins = 0; |
|---|
| 112 | |
|---|
| 113 | return str; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|