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