ARB
BI_basepos.hxx
Go to the documentation of this file.
1 // ============================================================= //
2 // //
3 // File : BI_basepos.hxx //
4 // Purpose : //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // ============================================================= //
10 
11 #ifndef BI_BASEPOS_HXX
12 #define BI_BASEPOS_HXX
13 
14 #ifndef ARBDB_BASE_H
15 #include <arbdb_base.h>
16 #endif
17 #ifndef ARB_ASSERT_H
18 #include <arb_assert.h>
19 #endif
20 #ifndef ARBTOOLS_H
21 #include <arbtools.h>
22 #endif
23 
24 #ifndef bi_assert
25 #define bi_assert(bed) arb_assert(bed)
26 #endif
27 
28 typedef bool (*char_predicate_fun)(char);
29 
30 class CharPredicate { // general predicate for char
31  bool isTrue[256];
32 public:
33  explicit CharPredicate(char_predicate_fun is_true) {
34  for (int i = 0; i<256; ++i) { // IRRELEVANT_LOOP
35  isTrue[i] = is_true(safeCharIndex((unsigned char)i));
36  }
37  }
38 
39  bool applies(char c) const { return isTrue[safeCharIndex(c)]; }
40  bool applies(unsigned char c) const { return isTrue[c]; }
41  bool applies(int i) const { bi_assert(i == (unsigned char)i); return isTrue[i]; }
42 };
43 
45 
46 class BasePosition : virtual Noncopyable {
47  int absLen;
48  int baseCount;
49 
50  int *abs2rel;
51  int *rel2abs;
52 
53  void setup() {
54  absLen = 0;
55  baseCount = 0;
56  abs2rel = NULp;
57  rel2abs = NULp;
58  }
59  void cleanup() {
60  delete [] abs2rel;
61  delete [] rel2abs;
62  setup();
63  }
64 
65 public:
66  void initialize(const char *seq, int size);
67  void initialize(const char *seq, int size, const CharPredicate& is_gap);
68 
69  BasePosition() { setup(); }
70  BasePosition(const char *seq, int size) { setup(); initialize(seq, size); }
71  BasePosition(const char *seq, int size, const CharPredicate& is_gap) { setup(); initialize(seq, size, is_gap); }
72  ~BasePosition() { cleanup(); }
73 
74  bool gotData() const { return abs2rel; }
75 
76  int abs_2_rel(int abs) const {
77  // returns the number of base characters in range [0..abs-1]!
78  // (i.e. result is in [0..bases])
79  // 'abs' has to be in range [0..abs_count()]
80 
81  bi_assert(gotData());
82  if (abs<0) return 0;
83  if (abs > absLen) abs = absLen;
84  return abs2rel[abs];
85  }
86 
87  int rel_2_abs(int rel) const {
88  // 'rel' is [0..N-1] (0 = 1st base, 1 = 2nd base)
89  // returns abs. position of that base
90 
91  bi_assert(gotData());
92  if (rel >= baseCount) rel = baseCount-1;
93  if (rel<0) return 0;
94  return rel2abs[rel];
95  }
96 
97  int first_base_abspos() const { return rel_2_abs(0); }
98  int last_base_abspos() const { return rel_2_abs(baseCount-1); }
99 
100  int base_count() const { return baseCount; }
101  int abs_count() const { return absLen; }
102 };
103 
104 
105 
106 struct BI_ecoli_ref : public BasePosition {
108 
109  void init(const char *seq, int size) { initialize(seq, size); }
111  GB_ERROR init(GBDATA *gb_main, char *alignment_name, char *ref_name);
112 };
113 
114 
115 #else
116 #error BI_basepos.hxx included twice
117 #endif // BI_BASEPOS_HXX
const char * GB_ERROR
Definition: arb_core.h:25
int rel_2_abs(int rel) const
Definition: BI_basepos.hxx:87
CONSTEXPR_INLINE unsigned char safeCharIndex(char c)
Definition: dupstr.h:73
void initialize(const char *seq, int size)
Definition: BI_basepos.cxx:22
static char * alignment_name
int base_count() const
Definition: BI_basepos.hxx:100
CharPredicate(char_predicate_fun is_true)
Definition: BI_basepos.hxx:33
BasePosition(const char *seq, int size)
Definition: BI_basepos.hxx:70
FILE * seq
Definition: rns.c:46
char_predicate_fun is_gap_fun
Definition: BI_basepos.hxx:44
int abs_2_rel(int abs) const
Definition: BI_basepos.hxx:76
bool applies(int i) const
Definition: BI_basepos.hxx:41
#define bi_assert(bed)
Definition: BI_basepos.hxx:25
int abs_count() const
Definition: BI_basepos.hxx:101
void init(const char *seq, int size)
Definition: BI_basepos.hxx:109
bool is_gap(char c)
Definition: seq_search.hxx:48
bool(* char_predicate_fun)(char)
Definition: BI_basepos.hxx:28
bool applies(unsigned char c) const
Definition: BI_basepos.hxx:40
BasePosition(const char *seq, int size, const CharPredicate &is_gap)
Definition: BI_basepos.hxx:71
int first_base_abspos() const
Definition: BI_basepos.hxx:97
#define abs(x)
Definition: f2c.h:151
#define NULp
Definition: cxxforward.h:116
int last_base_abspos() const
Definition: BI_basepos.hxx:98
bool gotData() const
Definition: BI_basepos.hxx:74
GBDATA * gb_main
Definition: adname.cxx:32
bool applies(char c) const
Definition: BI_basepos.hxx:39