1 | #include "ps_node.hxx" |
---|
2 | #include <unistd.h> |
---|
3 | |
---|
4 | using namespace std; |
---|
5 | |
---|
6 | // |
---|
7 | // *** disk output ASCII *** |
---|
8 | // |
---|
9 | bool PS_Node::saveASCII( PS_FileBuffer* _fb, char *buffer ) { // buffer MUST be at least 100 chars long |
---|
10 | unsigned int size; |
---|
11 | int count; |
---|
12 | // |
---|
13 | // write num |
---|
14 | // |
---|
15 | count = sprintf( buffer, "N[%i", num ); |
---|
16 | _fb->put( buffer,count ); |
---|
17 | // |
---|
18 | // write probes |
---|
19 | // |
---|
20 | size = (probes == 0) ? 0 : probes->size(); |
---|
21 | if (size) { |
---|
22 | count = sprintf( buffer, " P{%i", size ); |
---|
23 | _fb->put( buffer, count ); |
---|
24 | for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) { |
---|
25 | count = sprintf( buffer, " (%i_%i_%i)", (*i)->quality, (*i)->length, (*i)->GC_content ); |
---|
26 | _fb->put( buffer, count ); |
---|
27 | } |
---|
28 | _fb->put_char( '}' ); |
---|
29 | } |
---|
30 | // |
---|
31 | // write children |
---|
32 | // |
---|
33 | size = children.size(); |
---|
34 | if (size) { |
---|
35 | count = sprintf( buffer, " C<%i", size ); |
---|
36 | _fb->put( buffer, count ); |
---|
37 | for (PS_NodeMapIterator i=children.begin(); i!=children.end(); ++i) { |
---|
38 | _fb->put_char( '\n' ); |
---|
39 | if (num == -1) _fb->put_char( '+' ); |
---|
40 | i->second->saveASCII( _fb,buffer ); |
---|
41 | } |
---|
42 | _fb->put_char( '>' ); |
---|
43 | } |
---|
44 | // |
---|
45 | // return true to signal success |
---|
46 | // |
---|
47 | _fb->put_char( ']' ); |
---|
48 | return true; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | // |
---|
53 | // *** disk output *** |
---|
54 | // |
---|
55 | bool PS_Node::save( PS_FileBuffer* _fb ) { |
---|
56 | unsigned int size; |
---|
57 | // |
---|
58 | // write num |
---|
59 | // |
---|
60 | _fb->put_int( num ); |
---|
61 | // |
---|
62 | // write probes |
---|
63 | // |
---|
64 | size = (probes == 0) ? 0 : probes->size(); |
---|
65 | _fb->put_uint( size ); |
---|
66 | if (size) { |
---|
67 | PS_Probe p; |
---|
68 | for (PS_ProbeSetCIter i=probes->begin(); i!=probes->end(); ++i) { |
---|
69 | p = **i; |
---|
70 | _fb->put( &p, sizeof(PS_Probe) ); |
---|
71 | } |
---|
72 | } |
---|
73 | // |
---|
74 | // write children |
---|
75 | // |
---|
76 | size = children.size(); |
---|
77 | _fb->put_uint( size ); |
---|
78 | for (PS_NodeMapIterator i=children.begin(); i!=children.end(); ++i) { |
---|
79 | i->second->save( _fb ); |
---|
80 | } |
---|
81 | // |
---|
82 | // return true to signal success |
---|
83 | // |
---|
84 | return true; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | // |
---|
89 | // *** disk input *** |
---|
90 | // |
---|
91 | bool PS_Node::load( PS_FileBuffer* _fb ) { |
---|
92 | unsigned int size; |
---|
93 | // |
---|
94 | // read num |
---|
95 | // |
---|
96 | _fb->get_int( num ); |
---|
97 | // |
---|
98 | // read probes |
---|
99 | // |
---|
100 | _fb->get_uint( size ); |
---|
101 | if (size) { // does node have probes ? |
---|
102 | probes = new PS_ProbeSet; // make new probeset |
---|
103 | for (unsigned int i=0; i<size; ++i) { |
---|
104 | PS_Probe *p = new PS_Probe; |
---|
105 | _fb->get( p, sizeof(PS_Probe) ); // read new probe |
---|
106 | PS_ProbePtr new_probe(p); // make new probe-smartpointer |
---|
107 | probes->insert( new_probe ); // append new probe to probeset |
---|
108 | } |
---|
109 | } else { |
---|
110 | probes = 0; // unset probeset |
---|
111 | } |
---|
112 | // |
---|
113 | // read children |
---|
114 | // |
---|
115 | _fb->get_uint( size ); |
---|
116 | if (num == -1) { |
---|
117 | for (unsigned int i=0; i<size; ++i) { |
---|
118 | PS_NodePtr new_child(new PS_Node(-1)); // make new child |
---|
119 | new_child->load( _fb ); // read new child |
---|
120 | children[new_child->getNum()] = new_child; // insert new child to childmap |
---|
121 | if (i % 200 == 0) printf( "loaded 1st level #%i (%i)\n", i+1, new_child->getNum() ); |
---|
122 | } |
---|
123 | } else { |
---|
124 | for (unsigned int i=0; i<size; ++i) { |
---|
125 | PS_NodePtr new_child(new PS_Node(-1)); // make new child |
---|
126 | new_child->load( _fb ); // read new child |
---|
127 | children[new_child->getNum()] = new_child; // insert new child to childmap |
---|
128 | } |
---|
129 | } |
---|
130 | // return true to signal success |
---|
131 | return true; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | // |
---|
136 | // *** disk input appending *** |
---|
137 | // |
---|
138 | bool PS_Node::append( PS_FileBuffer* _fb ) { |
---|
139 | unsigned int size; |
---|
140 | // |
---|
141 | // read num if root |
---|
142 | // |
---|
143 | if (num == -1) { |
---|
144 | _fb->get_int( num ); |
---|
145 | } |
---|
146 | // |
---|
147 | // read probes |
---|
148 | // |
---|
149 | _fb->get_uint( size ); |
---|
150 | if (size) { // does node have probes ? |
---|
151 | if (!probes) probes = new PS_ProbeSet; // make new probeset |
---|
152 | for (unsigned int i=0; i<size; ++i) { |
---|
153 | PS_Probe *p = new PS_Probe; |
---|
154 | _fb->get( p, sizeof(PS_Probe) ); // read new probe |
---|
155 | PS_ProbePtr new_probe(p); // make new probe-smartpointer |
---|
156 | probes->insert( new_probe ); // append new probe to probeset |
---|
157 | } |
---|
158 | } |
---|
159 | // |
---|
160 | // read children |
---|
161 | // |
---|
162 | _fb->get_uint( size ); |
---|
163 | for (unsigned int i=0; i<size; ++i) { |
---|
164 | // |
---|
165 | // read num of child |
---|
166 | // |
---|
167 | SpeciesID childNum; |
---|
168 | _fb->get_int( childNum ); |
---|
169 | if ((num == -1) && (i % 200 == 0)) printf( "appended 1st level #%i (%i)\n", i+1, childNum ); |
---|
170 | // |
---|
171 | // test if child already exists |
---|
172 | // |
---|
173 | PS_NodeMapIterator it = children.find( childNum ); |
---|
174 | if (it != children.end()) { |
---|
175 | it->second->append( _fb ); // 'update' child |
---|
176 | } else { |
---|
177 | PS_NodePtr newChild(new PS_Node(childNum)); // make new child |
---|
178 | newChild->append( _fb ); // read new child |
---|
179 | children[childNum] = newChild; // insert new child to childmap |
---|
180 | } |
---|
181 | } |
---|
182 | // return true to signal success |
---|
183 | return true; |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | // |
---|
188 | // disk read with callback |
---|
189 | // children are stored as child[0] one after the other |
---|
190 | // |
---|
191 | bool PS_Node::read( PS_FileBuffer* _fb, PS_Callback *_call_destination ) { |
---|
192 | unsigned int size; |
---|
193 | // |
---|
194 | // read num if root |
---|
195 | // |
---|
196 | if (num == -1) { |
---|
197 | _fb->get_int( num ); |
---|
198 | } |
---|
199 | // |
---|
200 | // read probes |
---|
201 | // |
---|
202 | _fb->get_uint( size ); |
---|
203 | if (size) { // does node have probes ? |
---|
204 | if (!probes) probes = new PS_ProbeSet; // make new probeset |
---|
205 | for (unsigned int i=0; i<size; ++i) { |
---|
206 | PS_Probe *p = new PS_Probe; |
---|
207 | _fb->get( p, sizeof(PS_Probe) ); // read new probe |
---|
208 | PS_ProbePtr new_probe(p); // make new probe-smartpointer |
---|
209 | probes->insert( new_probe ); // append new probe to probeset |
---|
210 | } |
---|
211 | } |
---|
212 | // |
---|
213 | // callback |
---|
214 | // |
---|
215 | _call_destination->callback( this ); |
---|
216 | // |
---|
217 | // read children |
---|
218 | // |
---|
219 | _fb->get_uint( size ); |
---|
220 | for (unsigned int i=0; i<size; ++i) { |
---|
221 | // |
---|
222 | // read num of child |
---|
223 | // |
---|
224 | SpeciesID childNum; |
---|
225 | _fb->get_int( childNum ); |
---|
226 | if ((num == -1) && (i % 200 == 0)) printf( "read 1st level #%i (%i)\n", i+1, childNum ); |
---|
227 | // |
---|
228 | // read children |
---|
229 | // |
---|
230 | PS_NodePtr child(new PS_Node(childNum)); // make child |
---|
231 | children[0] = child; // store child as Number Zero |
---|
232 | child->read( _fb, _call_destination ); // read child |
---|
233 | children.erase( 0 ); // remove previous child |
---|
234 | } |
---|
235 | // |
---|
236 | // return true to signal success |
---|
237 | // |
---|
238 | return true; |
---|
239 | } |
---|