ARB
SQ_GroupData.h
Go to the documentation of this file.
1 // ==================================================================== //
2 // //
3 // File : SQ_GroupData.h //
4 // Purpose : Classes to store global information about sequences //
5 // //
6 // //
7 // Coded by Juergen Huber in July 2003 - February 2004 //
8 // Coded by Kai Bader (baderk@in.tum.de) in 2007 - 2008 //
9 // Copyright Department of Microbiology (Technical University Munich) //
10 // //
11 // Visit our web site at: http://www.arb-home.de/ //
12 // //
13 // ==================================================================== //
14 
15 
16 #ifndef SQ_GROUPDATA_H
17 #define SQ_GROUPDATA_H
18 
19 #ifndef _GLIBCXX_CSTDDEF
20 # include <cstddef>
21 #endif
22 #ifndef _GLIBCXX_IOSTREAM
23 # include <iostream>
24 #endif
25 #ifndef ARB_ASSERT_H
26 # include <arb_assert.h>
27 #endif
28 #ifndef CXXFORWARD_H
29 #include <cxxforward.h>
30 #endif
31 
32 #define seq_assert(bed) arb_assert(bed)
33 
35  double conformity;
36  double deviation;
37 };
38 
39 class SQ_GroupData {
40 protected:
41  int size;
42  int avg_bases;
44  double gc_prop;
46 
47 public:
48  SQ_GroupData();
49  virtual ~SQ_GroupData();
50  virtual SQ_GroupData& operator = (const SQ_GroupData& other) = 0;
51  virtual SQ_GroupData *clone() const = 0;
52 
53  void SQ_set_avg_bases(int bases) {
54  avg_bases += bases;
55  }
56  int SQ_get_avg_bases() const {
57  return avg_bases/nr_sequences;
58  }
59  void SQ_set_avg_gc(double gc) {
60  gc_prop += gc;
61  }
62  double SQ_get_avg_gc() const {
63  return gc_prop/nr_sequences;
64  }
66  nr_sequences++;
67  }
68  int SQ_get_nr_sequences() const {
69  return nr_sequences;
70  }
71  bool SQ_is_initialized() const {
72  return initialized;
73  }
74 
75  virtual void SQ_init_consensus(int size) = 0;
76  virtual int SQ_print_on_screen() = 0;
77  virtual consensus_result SQ_calc_consensus(const char *sequence) const = 0;
78  virtual void SQ_add_sequence(const char *sequence) = 0;
79  virtual void SQ_add(const SQ_GroupData& other) = 0;
80 
81  int getSize() const {
82  return size;
83  }
84 
85 };
86 
87 template <int I> class Int {
88  Int(const Int& other); // copying not allowed
89 
90 public:
91  int i[I];
92 
93  int size() const {
94  return I;
95  }
96 
97  Int() {
98  memset(i, 0, I*sizeof(int));
99  }
100 
101  Int& operator += (const Int& other) {
102  const int *otheri = other.i;
103  for (int j = 0; j<I; ++j) { // @@@ does vectorize (eg. 520, 610); move to SQ_GroupData.cxx? then expect vectorization
104  i[j] += otheri[j];
105  }
106  return *this;
107  }
108 
109  Int& operator = (const Int& other) {
110  memcpy(i, other.i, I*sizeof(int));
111  return *this;
112  }
113 };
114 
115 template <int I> class SQ_GroupData_Impl : public SQ_GroupData {
116  SQ_GroupData_Impl(const SQ_GroupData_Impl& other);
117  SQ_GroupData_Impl& operator = (const SQ_GroupData_Impl& Other);
118 
119 public:
121  consensus = NULp;
122  }
124 
125  SQ_GroupData_Impl& operator = (const SQ_GroupData& Other) OVERRIDE {
126  const SQ_GroupData_Impl& other = dynamic_cast<const SQ_GroupData_Impl&>(Other);
127  seq_assert(other.size>0 && other.initialized);
128 
129  if (!initialized) SQ_init_consensus(other.size);
130  seq_assert(size==other.size);
131 
132  avg_bases = other.avg_bases;
133  gc_prop = other.gc_prop;
134 
135  for (int s=0; s<size; ++s) {
136  consensus[s] = other.consensus[s];
137  }
138  nr_sequences = other.nr_sequences;
139  return *this;
140  }
141 
144  void SQ_add(const SQ_GroupData& other) OVERRIDE; // add's other to this
145 
146 protected:
148 };
149 
150 class SQ_GroupData_RNA : public SQ_GroupData_Impl<6> {
151  typedef SQ_GroupData_Impl<6> Base;
152  SQ_GroupData_RNA(const SQ_GroupData_RNA& other); // copying not allowed
153 public:
155  }
156 
157  SQ_GroupData_RNA *clone() const OVERRIDE {
158  return new SQ_GroupData_RNA;
159  }
160  SQ_GroupData_RNA& operator = (const SQ_GroupData& other) OVERRIDE {
161  Base::operator=(other);
162  return *this;
163  }
164 
165  consensus_result SQ_calc_consensus (const char *sequence) const OVERRIDE;
166  void SQ_add_sequence (const char *sequence) OVERRIDE;
167 protected:
168  static int class_counter;
169 };
170 
171 // -----------------------
172 // implementation
173 
175  delete [] consensus;
176 }
177 
178 template <int I> void SQ_GroupData_Impl<I>::SQ_init_consensus(int size_) {
180 
181  size = size_;
182  consensus = new Int<I>[size];
183  initialized = true;
184 }
185 
186 template <int I> void SQ_GroupData_Impl<I>::SQ_add(
187  const SQ_GroupData& other_base) {
188  const SQ_GroupData_Impl<I>& other =
189  dynamic_cast<const SQ_GroupData_Impl<I>&> (other_base);
190  seq_assert (size==other.size);
191  for (int i = 0; i<size; ++i) { // @@@ does vectorize (eg. 520, 610); used in SQ_GroupData.cxx and SQ_main.cxx -> move where?
192  consensus[i] += other.consensus[i];
193  }
194  nr_sequences+=other.nr_sequences;
195  avg_bases+=other.avg_bases;
196  gc_prop += other.gc_prop;
197 }
198 
200  for (int i=0; i < size; i++) {
201  for (int j = 0; j<I; j++) {
202  std::cout << consensus[i].i[j];
203  }
204  }
205  return 0;
206 }
207 
208 #else
209 #error SQ_GroupData.h included twice
210 #endif
static int I
Definition: align.cxx:489
Int()
Definition: SQ_GroupData.h:97
Int< I > * consensus
Definition: SQ_GroupData.h:147
int getSize() const
Definition: SQ_GroupData.h:81
bool SQ_is_initialized() const
Definition: SQ_GroupData.h:71
virtual SQ_GroupData & operator=(const SQ_GroupData &other)=0
virtual void SQ_add_sequence(const char *sequence)=0
virtual ~SQ_GroupData()
int size() const
Definition: SQ_GroupData.h:93
void SQ_count_sequences()
Definition: SQ_GroupData.h:65
#define FINAL_OVERRIDE
Definition: cxxforward.h:114
virtual consensus_result SQ_calc_consensus(const char *sequence) const =0
bool initialized
Definition: SQ_GroupData.h:45
int i[I]
Definition: SQ_GroupData.h:91
virtual SQ_GroupData * clone() const =0
int SQ_get_avg_bases() const
Definition: SQ_GroupData.h:56
double gc_prop
Definition: SQ_GroupData.h:44
virtual int SQ_print_on_screen()=0
void SQ_set_avg_bases(int bases)
Definition: SQ_GroupData.h:53
Int & operator+=(const Int &other)
Definition: SQ_GroupData.h:101
virtual void SQ_add(const SQ_GroupData &other)=0
SQ_GroupData_RNA * clone() const OVERRIDE
Definition: SQ_GroupData.h:157
Int & operator=(const Int &other)
Definition: SQ_GroupData.h:109
~SQ_GroupData_Impl() OVERRIDE
Definition: SQ_GroupData.h:174
int SQ_get_nr_sequences() const
Definition: SQ_GroupData.h:68
#define OVERRIDE
Definition: cxxforward.h:112
virtual void SQ_init_consensus(int size)=0
void SQ_set_avg_gc(double gc)
Definition: SQ_GroupData.h:59
void SQ_init_consensus(int size) FINAL_OVERRIDE
Definition: SQ_GroupData.h:178
void SQ_add(const SQ_GroupData &other) OVERRIDE
Definition: SQ_GroupData.h:186
#define NULp
Definition: cxxforward.h:116
double SQ_get_avg_gc() const
Definition: SQ_GroupData.h:62
Base
#define seq_assert(bed)
Definition: SQ_GroupData.h:32
static int class_counter
Definition: SQ_GroupData.h:168
int SQ_print_on_screen() OVERRIDE
Definition: SQ_GroupData.h:199
GB_write_int const char s
Definition: AW_awar.cxx:154