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