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