source: tags/svn.1.5.4/SEQ_QUALITY/SQ_GroupData.h

Last change on this file was 7812, checked in by westram, 13 years ago

merge from dev [7751] [7752]

  • updated many old-style typedefs (typedef struct/enum)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
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
26#ifndef DOWNCAST_H
27#include <downcast.h>
28#endif
29#ifndef ARB_ASSERT_H
30# include <arb_assert.h>
31#endif
32#define seq_assert(bed) arb_assert(bed)
33
34struct consensus_result {
35    double conformity;
36    double deviation;
37};
38
39class SQ_GroupData {
40public:
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
78protected:
79    int size;
80    int avg_bases;
81    int nr_sequences;
82    double gc_prop;
83    bool initialized;
84};
85
86template <int I> class Int {
87    Int(const Int& other); // copying not allowed
88
89public:
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
114template <int I> class SQ_GroupData_Impl : public SQ_GroupData {
115    SQ_GroupData_Impl(const SQ_GroupData_Impl& other);
116
117public:
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
127        if (!initialized) SQ_init_consensus(other.size);
128        seq_assert(size==other.size);
129
130        avg_bases = other.avg_bases;
131        gc_prop   = other.gc_prop;
132       
133        for (int s=0; s<size; ++s) {
134            consensus[s] = other.consensus[s];
135        }
136        nr_sequences = other.nr_sequences;
137        return *this;
138    }
139
140    void SQ_init_consensus(int size);
141    int SQ_print_on_screen();
142    void SQ_add_column(int col);
143    void SQ_add(const SQ_GroupData& other); // add's other to this
144
145protected:
146    Int<I> *consensus;
147};
148
149class SQ_GroupData_RNA : public SQ_GroupData_Impl<6> {
150    typedef SQ_GroupData_Impl<6> Base;
151    SQ_GroupData_RNA(const SQ_GroupData_RNA& other); // copying not allowed
152public:
153    SQ_GroupData_RNA() {
154    }
155
156    SQ_GroupData_RNA *clone() const {
157        return new SQ_GroupData_RNA;
158    }
159    SQ_GroupData_RNA& operator = (const SQ_GroupData& other) {
160        Base::operator=(*DOWNCAST(const Base*, &other));
161        return *this;
162    }
163
164    consensus_result SQ_calc_consensus (const char *sequence) const;
165    void SQ_add_sequence (const char *sequence);
166protected:
167    static int class_counter;
168};
169
170class SQ_GroupData_PRO : public SQ_GroupData_Impl<20> {
171    typedef SQ_GroupData_Impl<20> Base;
172    SQ_GroupData_PRO(const SQ_GroupData_PRO& other); // copying not allowed
173public:
174    SQ_GroupData_PRO() {
175    }
176
177    SQ_GroupData_PRO *clone() const {
178        return new SQ_GroupData_PRO;
179    }
180    SQ_GroupData_PRO& operator = (const SQ_GroupData& other) {
181        Base::operator=(*DOWNCAST(const Base*, &other));
182        return *this;
183    }
184
185    consensus_result SQ_calc_consensus (const char *sequence) const;
186    void SQ_add_sequence (const char *sequence);
187};
188
189// -----------------------
190//      implementation
191// -----------------------
192
193
194template <int I> SQ_GroupData_Impl<I>::~SQ_GroupData_Impl() {
195    delete [] consensus;
196}
197
198template <int I> void SQ_GroupData_Impl<I>::SQ_init_consensus(int size_) {
199    seq_assert (!initialized);
200
201    size = size_;
202    consensus = new Int<I>[size];
203    initialized = true;
204}
205
206template <int I> void SQ_GroupData_Impl<I>::SQ_add(
207        const SQ_GroupData& other_base) {
208    const SQ_GroupData_Impl<I>& other =
209            dynamic_cast<const SQ_GroupData_Impl<I>&> (other_base);
210    seq_assert (size==other.size);
211    for (int i = 0; i<size; ++i) {
212        consensus[i] += other.consensus[i];
213    }
214    nr_sequences+=other.nr_sequences;
215    avg_bases+=other.avg_bases;
216    gc_prop += other.gc_prop;
217}
218
219template <int I> int SQ_GroupData_Impl<I>::SQ_print_on_screen() {
220    for (int i=0; i < size; i++) {
221        for (int j = 0; j<I; j++) {
222            std::cout << consensus[i].i[j];
223        }
224    }
225    return (0);
226}
227
228#else
229#error SQ_GroupData.h included twice
230#endif
Note: See TracBrowser for help on using the repository browser.