1 | #ifndef PS_NODE_HXX |
---|
2 | #define PS_NODE_HXX |
---|
3 | |
---|
4 | #ifndef SMARTPTR_H |
---|
5 | #include <smartptr.h> |
---|
6 | #endif |
---|
7 | #ifndef PS_DEFS_HXX |
---|
8 | #include "ps_defs.hxx" |
---|
9 | #endif |
---|
10 | #ifndef PS_FILEBUFFER_HXX |
---|
11 | #include "ps_filebuffer.hxx" |
---|
12 | #endif |
---|
13 | |
---|
14 | using namespace std; |
---|
15 | |
---|
16 | struct PS_Probe { |
---|
17 | short int quality; // negative quality <=> matches inverse path |
---|
18 | unsigned short int length; |
---|
19 | unsigned short int GC_content; |
---|
20 | }; |
---|
21 | |
---|
22 | typedef SmartPtr<PS_Probe> PS_ProbePtr; |
---|
23 | |
---|
24 | inline void PS_printProbe(const PS_Probe *p) { |
---|
25 | printf("%+i_%u_%u", p->quality, p->length, p->GC_content); |
---|
26 | } |
---|
27 | |
---|
28 | inline void PS_printProbe(const PS_ProbePtr& p) { |
---|
29 | printf("%+i_%u_%u", p->quality, p->length, p->GC_content); |
---|
30 | } |
---|
31 | |
---|
32 | struct lt_probe { |
---|
33 | bool operator()(const PS_ProbePtr& p1, const PS_ProbePtr& p2) const { |
---|
34 | if (abs(p1->quality) == abs(p2->quality)) { |
---|
35 | if (p1->length == p2->length) { |
---|
36 | return p1->GC_content < p2->GC_content; // equal quality & length => sort by GC-content |
---|
37 | } |
---|
38 | else { |
---|
39 | return p1->length < p2->length; // equal quality => sort by length |
---|
40 | } |
---|
41 | } |
---|
42 | else { |
---|
43 | return p1->quality < p2->quality; // sort by quality |
---|
44 | } |
---|
45 | } |
---|
46 | }; |
---|
47 | |
---|
48 | |
---|
49 | typedef set<PS_ProbePtr, lt_probe> PS_ProbeSet; |
---|
50 | typedef PS_ProbeSet* PS_ProbeSetPtr; |
---|
51 | typedef PS_ProbeSet::iterator PS_ProbeSetIter; |
---|
52 | typedef PS_ProbeSet::const_iterator PS_ProbeSetCIter; |
---|
53 | class PS_Node; |
---|
54 | typedef SmartPtr<PS_Node> PS_NodePtr; |
---|
55 | typedef map<SpeciesID, PS_NodePtr> PS_NodeMap; |
---|
56 | typedef PS_NodeMap::iterator PS_NodeMapIterator; |
---|
57 | typedef PS_NodeMap::reverse_iterator PS_NodeMapRIterator; |
---|
58 | typedef PS_NodeMap::const_iterator PS_NodeMapConstIterator; |
---|
59 | typedef PS_NodeMap::const_reverse_iterator PS_NodeMapConstRIterator; |
---|
60 | |
---|
61 | class PS_Node : virtual Noncopyable { |
---|
62 | SpeciesID num; |
---|
63 | PS_NodeMap children; |
---|
64 | PS_ProbeSetPtr probes; |
---|
65 | |
---|
66 | public: |
---|
67 | |
---|
68 | // |
---|
69 | // *** num *** |
---|
70 | // |
---|
71 | void setNum(SpeciesID id) { num = id; } |
---|
72 | SpeciesID getNum() const { return num; } |
---|
73 | |
---|
74 | // |
---|
75 | // *** children *** |
---|
76 | // |
---|
77 | bool addChild(PS_NodePtr& _child) { |
---|
78 | PS_NodeMapIterator it = children.find(_child->getNum()); |
---|
79 | if (it == children.end()) { |
---|
80 | children[_child->getNum()] = _child; |
---|
81 | return true; |
---|
82 | } |
---|
83 | else { |
---|
84 | printf("child[#%u] already exists\n", _child->getNum()); |
---|
85 | return false; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | PS_NodePtr assertChild(SpeciesID _id) { |
---|
90 | PS_NodeMapIterator it = children.find(_id); |
---|
91 | if (it == children.end()) { |
---|
92 | PS_NodePtr new_child(new PS_Node(_id)); |
---|
93 | children[_id] = new_child; |
---|
94 | return new_child; |
---|
95 | } |
---|
96 | else { |
---|
97 | return it->second; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | pair<bool, PS_NodePtr> getChild(SpeciesID id) { |
---|
102 | PS_NodeMapIterator it = children.find(id); |
---|
103 | return pair<bool, PS_NodePtr>(it!=children.end(), it->second); |
---|
104 | } |
---|
105 | |
---|
106 | pair<bool, const PS_NodePtr> getChild(SpeciesID id) const { |
---|
107 | PS_NodeMapConstIterator it = children.find(id); |
---|
108 | return pair<bool, const PS_NodePtr>(it!=children.end(), it->second); |
---|
109 | } |
---|
110 | |
---|
111 | size_t countChildren() const { return children.size(); } |
---|
112 | bool hasChildren() const { return !children.empty(); } |
---|
113 | |
---|
114 | PS_NodeMapIterator getChildrenBegin() { return children.begin(); } |
---|
115 | PS_NodeMapRIterator getChildrenRBegin() { return children.rbegin(); } |
---|
116 | PS_NodeMapConstIterator getChildrenBegin() const { return children.begin(); } |
---|
117 | PS_NodeMapConstRIterator getChildrenRBegin() const { return children.rbegin(); } |
---|
118 | PS_NodeMapIterator getChildrenEnd() { return children.end(); } |
---|
119 | PS_NodeMapRIterator getChildrenREnd() { return children.rend(); } |
---|
120 | PS_NodeMapConstIterator getChildrenEnd() const { return children.end(); } |
---|
121 | PS_NodeMapConstRIterator getChildrenREnd() const { return children.rend(); } |
---|
122 | |
---|
123 | // |
---|
124 | // *** probes *** |
---|
125 | // |
---|
126 | bool addProbe(const PS_ProbePtr& probe) { |
---|
127 | if (!probes) { |
---|
128 | probes = new PS_ProbeSet; |
---|
129 | probes->insert(probe); |
---|
130 | return true; |
---|
131 | } |
---|
132 | else { |
---|
133 | pair<PS_ProbeSetCIter, bool> p = probes->insert(probe); |
---|
134 | return p.second; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | void addProbes(PS_ProbeSetCIter _begin, PS_ProbeSetCIter _end) { |
---|
139 | if (_begin == _end) return; |
---|
140 | if (!probes) probes = new PS_ProbeSet; |
---|
141 | for (PS_ProbeSetCIter probe = _begin; probe != _end; ++probe) { |
---|
142 | probes->insert(*probe); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | void addProbesInverted(PS_ProbeSetCIter _begin, PS_ProbeSetCIter _end) { |
---|
147 | if (_begin == _end) return; |
---|
148 | if (!probes) probes = new PS_ProbeSet; |
---|
149 | for (PS_ProbeSetCIter probe = _begin; probe != _end; ++probe) { |
---|
150 | PS_ProbePtr new_probe(new PS_Probe); |
---|
151 | new_probe->length = (*probe)->length; |
---|
152 | new_probe->quality = -((*probe)->quality); |
---|
153 | new_probe->GC_content = (*probe)->GC_content; |
---|
154 | probes->insert(new_probe); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | size_t countProbes() const { return probes ? probes->size() : 0; } |
---|
159 | bool hasProbes() const { return probes; } |
---|
160 | bool hasPositiveProbes() const { |
---|
161 | if (!probes) return false; |
---|
162 | for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) { |
---|
163 | if ((*i)->quality >= 0) return true; |
---|
164 | } |
---|
165 | return false; |
---|
166 | } |
---|
167 | |
---|
168 | bool hasInverseProbes() const { |
---|
169 | if (!probes) return false; |
---|
170 | for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) { |
---|
171 | if ((*i)->quality < 0) return true; |
---|
172 | } |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | PS_ProbeSetCIter getProbesBegin() const { |
---|
177 | ps_assert(probes); |
---|
178 | return probes->begin(); |
---|
179 | } |
---|
180 | PS_ProbeSetCIter getProbesEnd() const { |
---|
181 | ps_assert(probes); |
---|
182 | return probes->end(); |
---|
183 | } |
---|
184 | |
---|
185 | void removeProbe(PS_ProbeSetCIter it) { |
---|
186 | ps_assert(probes); |
---|
187 | probes->erase(it); |
---|
188 | } |
---|
189 | void removeProbes() { |
---|
190 | if (probes) { |
---|
191 | delete probes; |
---|
192 | probes = NULp; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | // |
---|
197 | // *** output ** |
---|
198 | // |
---|
199 | void print() { |
---|
200 | printf("\nN[%d] P[ ", num); |
---|
201 | if (probes) { |
---|
202 | for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) { |
---|
203 | PS_printProbe(*i); |
---|
204 | fputc(' ', stdout); |
---|
205 | } |
---|
206 | } |
---|
207 | printf("] C["); |
---|
208 | for (PS_NodeMapIterator i=children.begin(); i!=children.end(); ++i) { |
---|
209 | i->second->print(); |
---|
210 | } |
---|
211 | fputc(']', stdout); |
---|
212 | } |
---|
213 | |
---|
214 | void printOnlyMe() const { |
---|
215 | printf("N[%d] P[ ", num); |
---|
216 | if (probes) { |
---|
217 | for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) { |
---|
218 | PS_printProbe(*i); |
---|
219 | fputc(' ', stdout); |
---|
220 | } |
---|
221 | } |
---|
222 | printf("] C[ %zu ]", children.size()); |
---|
223 | } |
---|
224 | |
---|
225 | // |
---|
226 | // *** disk i/o *** |
---|
227 | // |
---|
228 | bool save(PS_FileBuffer *_fb); |
---|
229 | bool saveASCII(PS_FileBuffer *_fb, char *buffer); |
---|
230 | bool load(PS_FileBuffer *_fb); |
---|
231 | bool append(PS_FileBuffer *_fb); // load from file and append to node |
---|
232 | bool read(PS_FileBuffer *_fb, PS_Callback *_call_destination); // parse file and callback after probes read |
---|
233 | |
---|
234 | // |
---|
235 | // *** constructors *** |
---|
236 | // |
---|
237 | PS_Node(SpeciesID id) { num = id; probes = NULp; } |
---|
238 | |
---|
239 | // |
---|
240 | // *** destructor *** |
---|
241 | // |
---|
242 | ~PS_Node() { |
---|
243 | if (probes) delete probes; |
---|
244 | children.clear(); |
---|
245 | } |
---|
246 | }; |
---|
247 | |
---|
248 | #else |
---|
249 | #error ps_node.hxx included twice |
---|
250 | #endif |
---|