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 _CPP_CSTDDEF |
---|
20 | # include <cstddef> |
---|
21 | #endif |
---|
22 | #ifndef _CPP_IOSTREAM |
---|
23 | # include <iostream> |
---|
24 | #endif |
---|
25 | #ifndef _MEMORY_H |
---|
26 | #include <memory.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifndef ARB_ASSERT_H |
---|
30 | # include <arb_assert.h> |
---|
31 | #endif |
---|
32 | #define seq_assert(bed) arb_assert(bed) |
---|
33 | |
---|
34 | typedef struct { |
---|
35 | double conformity; |
---|
36 | double deviation; |
---|
37 | } consensus_result; |
---|
38 | |
---|
39 | class SQ_GroupData { |
---|
40 | public: |
---|
41 | SQ_GroupData(); |
---|
42 | virtual ~SQ_GroupData(); |
---|
43 | virtual SQ_GroupData& operator =(const SQ_GroupData& other) = 0; |
---|
44 | virtual SQ_GroupData *clone() const = 0; |
---|
45 | |
---|
46 | void SQ_set_avg_bases(int bases) { |
---|
47 | avg_bases += bases; |
---|
48 | } |
---|
49 | int SQ_get_avg_bases() const { |
---|
50 | return (avg_bases/nr_sequences ); |
---|
51 | } |
---|
52 | void SQ_set_avg_gc(double gc) { |
---|
53 | gc_prop += gc; |
---|
54 | } |
---|
55 | double SQ_get_avg_gc() const { |
---|
56 | return (gc_prop/nr_sequences ); |
---|
57 | } |
---|
58 | void SQ_count_sequences() { |
---|
59 | nr_sequences++; |
---|
60 | } |
---|
61 | int SQ_get_nr_sequences() const { |
---|
62 | return nr_sequences; |
---|
63 | } |
---|
64 | bool SQ_is_initialized() const { |
---|
65 | return initialized; |
---|
66 | } |
---|
67 | |
---|
68 | virtual void SQ_init_consensus(int size) = 0; |
---|
69 | virtual int SQ_print_on_screen() = 0; |
---|
70 | virtual consensus_result SQ_calc_consensus(const char *sequence) const = 0; |
---|
71 | virtual void SQ_add_sequence(const char *sequence) = 0; |
---|
72 | virtual void SQ_add(const SQ_GroupData& other) = 0; |
---|
73 | |
---|
74 | int getSize() const { |
---|
75 | return size; |
---|
76 | } |
---|
77 | |
---|
78 | protected: |
---|
79 | int size; |
---|
80 | int avg_bases; |
---|
81 | int nr_sequences; |
---|
82 | double gc_prop; |
---|
83 | bool initialized; |
---|
84 | }; |
---|
85 | |
---|
86 | template <int I> class Int { |
---|
87 | Int(const Int& other); // copying not allowed |
---|
88 | |
---|
89 | public: |
---|
90 | int i[I]; |
---|
91 | |
---|
92 | int size() const { |
---|
93 | return I; |
---|
94 | } |
---|
95 | |
---|
96 | Int() { |
---|
97 | memset(i, 0, I*sizeof(int)); |
---|
98 | } |
---|
99 | |
---|
100 | Int& operator +=(const Int& other) { |
---|
101 | const int *otheri = other.i; |
---|
102 | for (int j = 0; j<I; ++j) { |
---|
103 | i[j] += otheri[j]; |
---|
104 | } |
---|
105 | return *this; |
---|
106 | } |
---|
107 | |
---|
108 | Int& operator =(const Int& other) { |
---|
109 | memcpy(i, other.i, I*sizeof(int)); |
---|
110 | return *this; |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | template <int I> class SQ_GroupData_Impl : public SQ_GroupData { |
---|
115 | SQ_GroupData_Impl(const SQ_GroupData_Impl& other); |
---|
116 | |
---|
117 | public: |
---|
118 | SQ_GroupData_Impl() { |
---|
119 | consensus = 0; |
---|
120 | } |
---|
121 | |
---|
122 | virtual ~SQ_GroupData_Impl(); |
---|
123 | |
---|
124 | SQ_GroupData_Impl& operator=(const SQ_GroupData_Impl& other) { |
---|
125 | seq_assert ( other.size>0 && other.initialized ); |
---|
126 | if ( !initialized) |
---|
127 | SQ_init_consensus(other.size);seq_assert ( size==other.size ); |
---|
128 | avg_bases = other.avg_bases; |
---|
129 | gc_prop = other.gc_prop; |
---|
130 | for (int s=0; s<size; ++s) { |
---|
131 | consensus[s] = other.consensus[s]; |
---|
132 | } |
---|
133 | nr_sequences = other.nr_sequences; |
---|
134 | return *this; |
---|
135 | } |
---|
136 | |
---|
137 | void SQ_init_consensus(int size); |
---|
138 | int SQ_print_on_screen(); |
---|
139 | void SQ_add_column(int col); |
---|
140 | void SQ_add(const SQ_GroupData& other); // add's other to this |
---|
141 | |
---|
142 | protected: |
---|
143 | Int<I> *consensus; |
---|
144 | }; |
---|
145 | |
---|
146 | class SQ_GroupData_RNA : public SQ_GroupData_Impl<6> { |
---|
147 | SQ_GroupData_RNA(const SQ_GroupData_RNA& other); // copying not allowed |
---|
148 | public: |
---|
149 | SQ_GroupData_RNA() { |
---|
150 | } |
---|
151 | |
---|
152 | SQ_GroupData_RNA *clone() const { |
---|
153 | return new SQ_GroupData_RNA; |
---|
154 | } |
---|
155 | SQ_GroupData_RNA& operator=(const SQ_GroupData& other) { |
---|
156 | return static_cast<SQ_GroupData_RNA&> (SQ_GroupData_Impl<6>::operator= |
---|
157 | ( static_cast<const SQ_GroupData_Impl<6>&> ( other ) ) ); |
---|
158 | } |
---|
159 | |
---|
160 | consensus_result SQ_calc_consensus ( const char *sequence ) const; |
---|
161 | void SQ_add_sequence ( const char *sequence ); |
---|
162 | protected: |
---|
163 | static int class_counter; |
---|
164 | }; |
---|
165 | |
---|
166 | class SQ_GroupData_PRO : public SQ_GroupData_Impl<20> { |
---|
167 | SQ_GroupData_PRO(const SQ_GroupData_PRO& other); // copying not allowed |
---|
168 | public: |
---|
169 | SQ_GroupData_PRO() { |
---|
170 | } |
---|
171 | |
---|
172 | SQ_GroupData_PRO *clone() const { |
---|
173 | return new SQ_GroupData_PRO; |
---|
174 | } |
---|
175 | SQ_GroupData_PRO& operator=(const SQ_GroupData& other) { |
---|
176 | return static_cast<SQ_GroupData_PRO&> (SQ_GroupData_Impl<20>::operator= |
---|
177 | ( static_cast<const SQ_GroupData_Impl<20>&> ( other ) ) ); |
---|
178 | } |
---|
179 | |
---|
180 | consensus_result SQ_calc_consensus ( const char *sequence ) const; |
---|
181 | void SQ_add_sequence ( const char *sequence ); |
---|
182 | }; |
---|
183 | |
---|
184 | // ----------------------- |
---|
185 | // implementation |
---|
186 | // ----------------------- |
---|
187 | |
---|
188 | |
---|
189 | template <int I> SQ_GroupData_Impl<I>::~SQ_GroupData_Impl() { |
---|
190 | delete [] consensus; |
---|
191 | } |
---|
192 | |
---|
193 | template <int I> void SQ_GroupData_Impl<I>::SQ_init_consensus(int size_) { |
---|
194 | seq_assert ( !initialized ); |
---|
195 | |
---|
196 | size = size_; |
---|
197 | consensus = new Int<I>[size]; |
---|
198 | initialized = true; |
---|
199 | } |
---|
200 | |
---|
201 | template <int I> void SQ_GroupData_Impl<I>::SQ_add( |
---|
202 | const SQ_GroupData& other_base) { |
---|
203 | const SQ_GroupData_Impl<I>& other = |
---|
204 | dynamic_cast<const SQ_GroupData_Impl<I>&> (other_base ); |
---|
205 | seq_assert ( size==other.size ); |
---|
206 | for (int i = 0; i<size; ++i) { |
---|
207 | consensus[i] += other.consensus[i]; |
---|
208 | } |
---|
209 | nr_sequences+=other.nr_sequences; |
---|
210 | avg_bases+=other.avg_bases; |
---|
211 | gc_prop += other.gc_prop; |
---|
212 | } |
---|
213 | |
---|
214 | template <int I> int SQ_GroupData_Impl<I>::SQ_print_on_screen() { |
---|
215 | for (int i=0; i < size; i++) { |
---|
216 | for (int j = 0; j<I; j++) { |
---|
217 | std::cout << consensus[i].i[j]; |
---|
218 | } |
---|
219 | } |
---|
220 | return ( 0 ); |
---|
221 | } |
---|
222 | |
---|
223 | #else |
---|
224 | #error SQ_GroupData.h included twice |
---|
225 | #endif |
---|