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 | |
---|
34 | struct consensus_result { |
---|
35 | double conformity; |
---|
36 | double deviation; |
---|
37 | }; |
---|
38 | |
---|
39 | class SQ_GroupData { |
---|
40 | protected: |
---|
41 | int size; |
---|
42 | int avg_bases; |
---|
43 | int nr_sequences; |
---|
44 | double gc_prop; |
---|
45 | bool initialized; |
---|
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 | } |
---|
65 | void SQ_count_sequences() { |
---|
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: |
---|
120 | SQ_GroupData_Impl() { |
---|
121 | consensus = NULp; |
---|
122 | } |
---|
123 | ~SQ_GroupData_Impl() OVERRIDE; |
---|
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 | |
---|
142 | void SQ_init_consensus(int size) FINAL_OVERRIDE; |
---|
143 | int SQ_print_on_screen() OVERRIDE; |
---|
144 | void SQ_add(const SQ_GroupData& other) OVERRIDE; // add's other to this |
---|
145 | |
---|
146 | protected: |
---|
147 | Int<I> *consensus; |
---|
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: |
---|
154 | SQ_GroupData_RNA() { |
---|
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 | class SQ_GroupData_PRO : public SQ_GroupData_Impl<20> { |
---|
172 | typedef SQ_GroupData_Impl<20> Base; |
---|
173 | SQ_GroupData_PRO(const SQ_GroupData_PRO& other); // copying not allowed |
---|
174 | public: |
---|
175 | SQ_GroupData_PRO() { |
---|
176 | } |
---|
177 | |
---|
178 | SQ_GroupData_PRO *clone() const OVERRIDE { |
---|
179 | return new SQ_GroupData_PRO; |
---|
180 | } |
---|
181 | SQ_GroupData_PRO& operator = (const SQ_GroupData& other) OVERRIDE { |
---|
182 | Base::operator=(other); |
---|
183 | return *this; |
---|
184 | } |
---|
185 | |
---|
186 | consensus_result SQ_calc_consensus (const char *sequence) const OVERRIDE; |
---|
187 | void SQ_add_sequence (const char *sequence) OVERRIDE; |
---|
188 | }; |
---|
189 | |
---|
190 | // ----------------------- |
---|
191 | // implementation |
---|
192 | |
---|
193 | template <int I> SQ_GroupData_Impl<I>::~SQ_GroupData_Impl() { |
---|
194 | delete [] consensus; |
---|
195 | } |
---|
196 | |
---|
197 | template <int I> void SQ_GroupData_Impl<I>::SQ_init_consensus(int size_) { |
---|
198 | seq_assert (!initialized); |
---|
199 | |
---|
200 | size = size_; |
---|
201 | consensus = new Int<I>[size]; |
---|
202 | initialized = true; |
---|
203 | } |
---|
204 | |
---|
205 | template <int I> void SQ_GroupData_Impl<I>::SQ_add( |
---|
206 | const SQ_GroupData& other_base) { |
---|
207 | const SQ_GroupData_Impl<I>& other = |
---|
208 | dynamic_cast<const SQ_GroupData_Impl<I>&> (other_base); |
---|
209 | seq_assert (size==other.size); |
---|
210 | for (int i = 0; i<size; ++i) { // @@@ does vectorize (eg. 520, 610); used in SQ_GroupData.cxx and SQ_main.cxx -> move where? |
---|
211 | consensus[i] += other.consensus[i]; |
---|
212 | } |
---|
213 | nr_sequences+=other.nr_sequences; |
---|
214 | avg_bases+=other.avg_bases; |
---|
215 | gc_prop += other.gc_prop; |
---|
216 | } |
---|
217 | |
---|
218 | template <int I> int SQ_GroupData_Impl<I>::SQ_print_on_screen() { |
---|
219 | for (int i=0; i < size; i++) { |
---|
220 | for (int j = 0; j<I; j++) { |
---|
221 | std::cout << consensus[i].i[j]; |
---|
222 | } |
---|
223 | } |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | #else |
---|
228 | #error SQ_GroupData.h included twice |
---|
229 | #endif |
---|