ARB
SEC_bonddef.cxx
Go to the documentation of this file.
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  int i, j;
21  for (i=0; i<SEC_BOND_BASE_CHARS; i++) {
22  for (j=0; j<SEC_BOND_BASE_CHARS; j++) {
23  bond[i][j] = ' ';
24  }
25  }
26 }
27 
28 int SEC_bond_def::get_index(char base) const {
29  if (base == 0) return -1;
30 
31  const char *allowed = SEC_BOND_BASE_CHAR;
32  const char *found = strchr(allowed, toupper(base));
33 
34  if (!found) return -1;
35 
36  int idx = int(found-allowed);
37  sec_assert(idx>=0 && idx<SEC_BOND_BASE_CHARS);
38  return idx;
39 }
40 
41 GB_ERROR SEC_bond_def::insert(const char *pairs, char pair_char) {
43 
44  if (pair_char==0) pair_char = ' ';
45 
46  if (!strchr(SEC_BOND_PAIR_CHAR, pair_char)) {
47  error = GBS_global_string("Illegal pair-character '%c' (allowed: '%s')", pair_char, SEC_BOND_PAIR_CHAR);
48  }
49  else {
50  char c1 = 0;
51  int idx = 0;
52 
53  while (1) {
54  char c2 = pairs[idx++];
55 
56  if (!c2) { // end of string
57  if (c1) error = "Odd number of characters in pair definition";
58  break;
59  }
60  if (c2 == ' ') continue; // ignore spaces
61  if (c1==0) { c1 = c2; continue; } // store first char
62 
63  int i1 = get_index(c1);
64  int i2 = get_index(c2);
65 
66  if (i1==-1 || i2==-1) {
67  char ic = i1==-1 ? c1 : c2;
68  error = GBS_global_string("Illegal base-character '%c' (allowed: '%s')", ic, SEC_BOND_BASE_CHAR);
69  break;
70  }
71  else {
72  bond[i1][i2] = pair_char;
73  bond[i2][i1] = pair_char;
74  }
75  c1 = 0;
76  }
77  }
78  if (error && pair_char != '@') {
79  insert(pairs, '@'); // means error
80  }
81  return error;
82 }
83 
84 char SEC_bond_def::get_bond(char base1, char base2) const {
85  int i1 = get_index(base1);
86  int i2 = get_index(base2);
87 
88  if (i1==-1 || i2==-1) {
89  return ' ';
90  }
91  return bond[i1][i2];
92 }
93 
94 char *SEC_bond_def::get_pair_string(char pair_char) {
95  char *str = ARB_alloc<char>(5*5*3+1);
96  char *ins = str;
97 
98  for (int i = 0; i<SEC_BOND_BASE_CHARS; ++i) {
99  for (int j = i; j<SEC_BOND_BASE_CHARS; ++j) {
100  if (bond[i][j] == pair_char) {
101  if (ins>str) *ins++ = ' ';
102  *ins++ = SEC_BOND_BASE_CHAR[i];
103  *ins++ = SEC_BOND_BASE_CHAR[j];
104  }
105  }
106  }
107  *ins = 0;
108 
109  return str;
110 }
111 
112 
const char * GB_ERROR
Definition: arb_core.h:25
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
#define SEC_BOND_BASE_CHAR
Definition: SEC_bonddef.hxx:26
#define SEC_BOND_PAIR_CHAR
Definition: SEC_bonddef.hxx:28
static void error(const char *msg)
Definition: mkptypes.cxx:96
char * str
Definition: defines.h:20
#define SEC_BOND_BASE_CHARS
Definition: SEC_bonddef.hxx:25
#define NULp
Definition: cxxforward.h:114
#define sec_assert(cond)
Definition: SEC_defs.hxx:19