source: tags/ms_r16q3/CONVERTALN/seq.h

Last change on this file was 15176, checked in by westram, 8 years ago
File size: 2.7 KB
Line 
1#ifndef SEQ_H
2#define SEQ_H
3
4#ifndef FUN_H
5#include "fun.h"
6#endif
7#ifndef GLOBAL_H
8#include "global.h"
9#endif
10#ifndef ARB_STRING_H
11#include <arb_string.h>
12#endif
13#ifndef _GLIBCXX_CCTYPE
14#include <cctype>
15#endif
16
17
18#define INITSEQ 6000
19
20struct BaseCounts {
21    int a;
22    int c;
23    int g;
24    int t;
25    int other;
26
27    BaseCounts()
28        : a(0), c(0), g(0), t(0), other(0)
29    {}
30
31    void add(char ch) {
32        switch (tolower(ch)) {
33            case 'a': a++; break;
34            case 'c': c++; break;
35            case 'g': g++; break;
36            case 'u':
37            case 't': t++; break;
38            default: other++; break;
39        }
40    }
41};
42
43class Seq : virtual Noncopyable {
44    // - holds sequence data
45
46    char *id;
47    int   len;     // sequence length
48    int   max;     // space allocated for 'sequence'
49    char *seq;     // sequence data
50
51    void zeroTerminate() { add(0); len--; }
52
53    static void check_valid(char ch) {
54        if (isalpha(ch) || is_gapchar(ch)) return;
55        throw_errorf(43, "Invalid character '%c' in sequence data", ch);
56    }
57    static void check_valid(const char *s, int len) {
58        for (int i = 0; i<len; ++i) {
59            check_valid(s[i]);
60        }
61    }
62
63public:
64    Seq(const char *id_, const char *seq_, int len_) :
65        id(ARB_strdup(id_)),
66        len(len_),
67        max(len+1),
68        seq(strndup(seq_, len))
69    {
70        check_valid(seq, len);
71    }
72    Seq() :
73        id(NULL),
74        len(0),
75        max(INITSEQ),
76        seq(ARB_alloc<char>(INITSEQ))
77    {
78    }
79    ~Seq() {
80        ca_assert(seq); // otherwise 'this' is useless!
81        freenull(id);
82        freenull(seq);
83    }
84
85    void set_id(const char *id_) {
86        ca_assert(!id);
87        ca_assert(id_);
88        freedup(id, id_);
89    }
90    void replace_id(const char *id_) {
91        freenull(id);
92        set_id(id_);
93    }
94    const char *get_id() const {
95        ca_assert(id);
96        return id;
97    }
98
99    void add(char c) {
100        if (c) check_valid(c);
101        if (len >= max) {
102            max = max*1.5+100;
103            ARB_realloc(seq, max);
104        }
105        seq[len++] = c;
106    }
107
108    int get_len() const { return len; }
109    bool is_empty() const { return len == 0; }
110
111    const char *get_seq() const {
112        const_cast<Seq*>(this)->zeroTerminate();
113        return seq;
114    }
115
116    void count(BaseCounts& counter) const {
117        for (int i = 0; i<len; ++i)
118            counter.add(seq[i]);
119    }
120
121    int count_gaps() const {
122        int gaps = 0;
123        for (int i = 0; i<len; ++i) {
124            gaps += is_gapchar(seq[i]);
125        }
126        return gaps;
127    }
128
129    void out(Writer& write, Format outType) const;
130};
131typedef SmartPtr<Seq> SeqPtr;
132
133#else
134#error seq.h included twice
135#endif // SEQ_H
Note: See TracBrowser for help on using the repository browser.