1 | // ----------------------------------------------------------------------------- |
---|
2 | // Include-Dateien |
---|
3 | // ----------------------------------------------------------------------------- |
---|
4 | |
---|
5 | #ifndef _POSTREE_HXX |
---|
6 | #include "postree.hxx" |
---|
7 | #endif |
---|
8 | |
---|
9 | #ifndef _BASEN_H |
---|
10 | #include "basen.h" |
---|
11 | #endif |
---|
12 | |
---|
13 | #ifndef _FSTREAM_H |
---|
14 | #include <fstream.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef _STDLIB_H |
---|
18 | #include <stdlib.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | // ----------------------------------------------------------------------------- |
---|
22 | static int int_compare ( const void *a, |
---|
23 | const void *b ) |
---|
24 | // ----------------------------------------------------------------------------- |
---|
25 | { |
---|
26 | int result = 0, |
---|
27 | diff = *(int*)a - *(int*)b; |
---|
28 | |
---|
29 | if (diff < 0) result = -1; |
---|
30 | else if (diff > 0) result = 1; |
---|
31 | |
---|
32 | return result; |
---|
33 | } |
---|
34 | |
---|
35 | // ----------------------------------------------------------------------------- |
---|
36 | static void SortIntArray ( int *array, |
---|
37 | int *anz ) |
---|
38 | // ----------------------------------------------------------------------------- |
---|
39 | { |
---|
40 | int count = 0; |
---|
41 | |
---|
42 | qsort(array,*anz,sizeof(int),int_compare); |
---|
43 | |
---|
44 | while (count < (*anz - 1)) |
---|
45 | { |
---|
46 | int same = 0; |
---|
47 | |
---|
48 | while (((count + 1+ same) < *anz) && |
---|
49 | (array[count] == array[count + 1 + same])) same++; |
---|
50 | |
---|
51 | if (same) |
---|
52 | { |
---|
53 | memmove(&array[count],&array[count + same], |
---|
54 | (*anz - (count + same)) * sizeof(int)); |
---|
55 | |
---|
56 | (*anz) -= same; |
---|
57 | } |
---|
58 | |
---|
59 | count++; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | // ----------------------------------------------------------------------------- |
---|
64 | static void CountAppearances ( str seq, |
---|
65 | int *pos, |
---|
66 | int num, |
---|
67 | int off, |
---|
68 | int *anz ) |
---|
69 | // ----------------------------------------------------------------------------- |
---|
70 | { |
---|
71 | if (!num) |
---|
72 | { |
---|
73 | int base; |
---|
74 | |
---|
75 | while ((base = *seq++) != 0) |
---|
76 | { |
---|
77 | int index = BIndex[base]; |
---|
78 | |
---|
79 | if ((index >= 0) && |
---|
80 | (index < BASENPUR)) anz[index]++; |
---|
81 | } |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | while (num--) |
---|
86 | { |
---|
87 | int base = seq[pos[num] + off]; |
---|
88 | |
---|
89 | if (base == 0) anz[BASEN] = 1; |
---|
90 | else anz[BIndex[base]]++; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | // ----------------------------------------------------------------------------- |
---|
96 | static void FindAppearances ( int *list, |
---|
97 | int base, |
---|
98 | int anz, |
---|
99 | str seq, |
---|
100 | int *pos, |
---|
101 | int num, |
---|
102 | int off ) |
---|
103 | // ----------------------------------------------------------------------------- |
---|
104 | { |
---|
105 | int next = 0, |
---|
106 | count = 0, |
---|
107 | tmp; |
---|
108 | |
---|
109 | if (!num) |
---|
110 | { |
---|
111 | num = strlen(seq); |
---|
112 | |
---|
113 | while ((count < num) && (next < anz)) |
---|
114 | { |
---|
115 | if (BIndex[seq[count]] == base) list[next++] = count; |
---|
116 | |
---|
117 | count++; |
---|
118 | } |
---|
119 | } |
---|
120 | else |
---|
121 | while ((count < num) && (next < anz)) |
---|
122 | { |
---|
123 | if (base == BASEN) |
---|
124 | { |
---|
125 | if (!seq[pos[count]+ off]) list[next++] = pos[count]; |
---|
126 | } |
---|
127 | else |
---|
128 | { |
---|
129 | if (BIndex[seq[pos[count]+ off]] == base) list[next++] = pos[count]; |
---|
130 | } |
---|
131 | |
---|
132 | count++; |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | // ----------------------------------------------------------------------------- |
---|
137 | // Liefert die Anzahl aller Positionen unterhalb eines bestimmten Knotens |
---|
138 | // ----------------------------------------------------------------------------- |
---|
139 | static int CountLeaves ( PtNode &node ) |
---|
140 | // ----------------------------------------------------------------------------- |
---|
141 | { |
---|
142 | int panz = node.NumOfPos(), |
---|
143 | nanz = node.NumOfNext(); |
---|
144 | |
---|
145 | while (nanz--) panz += CountLeaves(*node.next[nanz]); |
---|
146 | |
---|
147 | return panz; |
---|
148 | } |
---|
149 | |
---|
150 | // ----------------------------------------------------------------------------- |
---|
151 | // Sucht alle Positionen unterhalb eines bestimmten Knotens |
---|
152 | // ----------------------------------------------------------------------------- |
---|
153 | static int GetLeaves ( PtNode &node, |
---|
154 | int *field ) |
---|
155 | // ----------------------------------------------------------------------------- |
---|
156 | { |
---|
157 | int panz = node.NumOfPos(), |
---|
158 | nanz = node.NumOfNext(), |
---|
159 | count = 0; |
---|
160 | |
---|
161 | while (count < panz) field[count] = node.position[count++]; |
---|
162 | |
---|
163 | while (nanz--) panz += GetLeaves(*node.next[nanz],&field[panz]); |
---|
164 | |
---|
165 | return panz; |
---|
166 | } |
---|
167 | |
---|
168 | // ----------------------------------------------------------------------------- |
---|
169 | // Sucht alle Positionen unterhalb eines bestimmten Knotens |
---|
170 | // ----------------------------------------------------------------------------- |
---|
171 | static int *FindLeaves ( PtNode &node, |
---|
172 | int *anz ) |
---|
173 | // ----------------------------------------------------------------------------- |
---|
174 | { |
---|
175 | int *field = NULL; |
---|
176 | |
---|
177 | if (((*anz = CountLeaves(node)) > 0) && |
---|
178 | ((field = new int [*anz]) != NULL)) GetLeaves(node,field); |
---|
179 | |
---|
180 | return field; |
---|
181 | } |
---|
182 | |
---|
183 | // ----------------------------------------------------------------------------- |
---|
184 | // Konstruktor zum Erstellen eines Positionsbaumes aus einer Sequenz |
---|
185 | // ----------------------------------------------------------------------------- |
---|
186 | PtNode::PtNode ( str seq, |
---|
187 | int *pos, |
---|
188 | int num, |
---|
189 | int rec ) |
---|
190 | // ----------------------------------------------------------------------------- |
---|
191 | { |
---|
192 | if (!seq) mask = (Mask)0, position = NULL, next = NULL; |
---|
193 | else |
---|
194 | { |
---|
195 | int anz[BASEN + 1], |
---|
196 | base; |
---|
197 | |
---|
198 | mask = (Mask)0; |
---|
199 | position = NULL; |
---|
200 | next = NULL; |
---|
201 | |
---|
202 | memset(anz,0,(BASEN + 1) * sizeof(int)); |
---|
203 | |
---|
204 | CountAppearances(seq,pos,num,rec,anz); |
---|
205 | |
---|
206 | if (anz[ADENIN] > 0) |
---|
207 | if (anz[ADENIN] > 1) mask = (Mask)(mask | N_ADENIN); |
---|
208 | else mask = (Mask)(mask | P_ADENIN); |
---|
209 | |
---|
210 | if (anz[CYTOSIN] > 0) |
---|
211 | if (anz[CYTOSIN] > 1) mask = (Mask)(mask | N_CYTOSIN); |
---|
212 | else mask = (Mask)(mask | P_CYTOSIN); |
---|
213 | |
---|
214 | if (anz[GUANIN] > 0) |
---|
215 | if (anz[GUANIN] > 1) mask = (Mask)(mask | N_GUANIN); |
---|
216 | else mask = (Mask)(mask | P_GUANIN); |
---|
217 | |
---|
218 | if (anz[URACIL] > 0) |
---|
219 | if (anz[URACIL] > 1) mask = (Mask)(mask | N_URACIL); |
---|
220 | else mask = (Mask)(mask | P_URACIL); |
---|
221 | |
---|
222 | if (anz[ONE] > 0) |
---|
223 | if (anz[ONE] > 1) mask = (Mask)(mask | N_ONE); |
---|
224 | else mask = (Mask)(mask | P_ONE); |
---|
225 | |
---|
226 | if (anz[ANY] > 0) |
---|
227 | if (anz[ANY] > 1) mask = (Mask)(mask | N_ANY); |
---|
228 | else mask = (Mask)(mask | P_ANY); |
---|
229 | |
---|
230 | if (anz[BASEN]) mask = (Mask)(mask | P_FINISH); |
---|
231 | |
---|
232 | { |
---|
233 | int panz = NumOfPos(), |
---|
234 | nanz = NumOfNext(); |
---|
235 | |
---|
236 | if (panz) position = new int [panz]; |
---|
237 | if (nanz) next = new PtNode* [nanz]; |
---|
238 | } |
---|
239 | |
---|
240 | for (base = 0;base < (BASEN + 1);base++) |
---|
241 | { |
---|
242 | switch (anz[base]) |
---|
243 | { |
---|
244 | case 0: break; |
---|
245 | case 1: |
---|
246 | { |
---|
247 | int list, |
---|
248 | which = base; |
---|
249 | Mask tmp = P_ADENIN; |
---|
250 | |
---|
251 | while (which--) tmp = (Mask)(tmp * 2); |
---|
252 | |
---|
253 | FindAppearances(&list,base,1,seq,pos,num,rec); |
---|
254 | |
---|
255 | position[IndexOfPos(tmp)] = list; |
---|
256 | |
---|
257 | break; |
---|
258 | } |
---|
259 | default: |
---|
260 | { |
---|
261 | int *list = new int [anz[base]], |
---|
262 | which = base, |
---|
263 | index; |
---|
264 | Mask tmp = N_ADENIN; |
---|
265 | PtNode *ptn; |
---|
266 | |
---|
267 | while (which--) tmp = (Mask)( tmp * 2); |
---|
268 | |
---|
269 | FindAppearances(list,base,anz[base],seq,pos,num,rec); |
---|
270 | |
---|
271 | index = IndexOfNext(tmp); |
---|
272 | ptn = new PtNode(seq,list,anz[base],rec + 1); |
---|
273 | next[index] = ptn; |
---|
274 | |
---|
275 | delete list; |
---|
276 | |
---|
277 | break; |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | // ----------------------------------------------------------------------------- |
---|
285 | // Kopierkonstruktor |
---|
286 | // ----------------------------------------------------------------------------- |
---|
287 | PtNode::PtNode ( PtNode &node ) |
---|
288 | // ----------------------------------------------------------------------------- |
---|
289 | { |
---|
290 | cout << "\nKopierkonstruktor(PtNode)\n"; |
---|
291 | |
---|
292 | mask = node.mask; |
---|
293 | |
---|
294 | if (position) |
---|
295 | { |
---|
296 | int anz = node.NumOfPos(); |
---|
297 | |
---|
298 | position = new int [anz]; |
---|
299 | |
---|
300 | memcpy(position,node.position,anz * sizeof(int)); |
---|
301 | } |
---|
302 | |
---|
303 | if (next) |
---|
304 | { |
---|
305 | int anz = node.NumOfNext(); |
---|
306 | |
---|
307 | next = new PtNode* [anz]; |
---|
308 | |
---|
309 | while (anz--) |
---|
310 | if (node.next[anz]) next[anz] = new PtNode(*node.next[anz]); |
---|
311 | else next[anz] = NULL; |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | // ----------------------------------------------------------------------------- |
---|
316 | // Destruktor |
---|
317 | // ----------------------------------------------------------------------------- |
---|
318 | PtNode::~PtNode ( void ) |
---|
319 | // ----------------------------------------------------------------------------- |
---|
320 | { |
---|
321 | if (position) delete position; |
---|
322 | |
---|
323 | if (next) |
---|
324 | { |
---|
325 | int anz = NumOfNext(); |
---|
326 | |
---|
327 | while (anz--) if (next[anz]) delete next[anz]; |
---|
328 | |
---|
329 | delete next; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | // ----------------------------------------------------------------------------- |
---|
334 | // Anzahl der vorhandenen Positionen |
---|
335 | // ----------------------------------------------------------------------------- |
---|
336 | int PtNode::NumOfPos ( void ) |
---|
337 | // ----------------------------------------------------------------------------- |
---|
338 | { |
---|
339 | int anz = 0, |
---|
340 | test = P_ADENIN; |
---|
341 | |
---|
342 | while (test <= P_FINISH) |
---|
343 | { |
---|
344 | if (mask & test) anz++; |
---|
345 | |
---|
346 | test *= 2; |
---|
347 | } |
---|
348 | |
---|
349 | return anz; |
---|
350 | } |
---|
351 | |
---|
352 | // ----------------------------------------------------------------------------- |
---|
353 | // Positionsindex einer Base |
---|
354 | // ----------------------------------------------------------------------------- |
---|
355 | int PtNode::IndexOfPos ( Mask base ) |
---|
356 | // ----------------------------------------------------------------------------- |
---|
357 | { |
---|
358 | int index = INVALID; |
---|
359 | |
---|
360 | if ((base <= P_FINISH) && (mask & base)) |
---|
361 | { |
---|
362 | int test = P_ADENIN; |
---|
363 | |
---|
364 | index = 0; |
---|
365 | |
---|
366 | while (test <= base) |
---|
367 | { |
---|
368 | if (mask & test) index++; |
---|
369 | |
---|
370 | test *= 2; |
---|
371 | } |
---|
372 | |
---|
373 | index--; |
---|
374 | } |
---|
375 | |
---|
376 | return index; |
---|
377 | } |
---|
378 | |
---|
379 | // ----------------------------------------------------------------------------- |
---|
380 | // Anzahl der vorhandenen Verzweigungen |
---|
381 | // ----------------------------------------------------------------------------- |
---|
382 | int PtNode::NumOfNext ( void ) |
---|
383 | // ----------------------------------------------------------------------------- |
---|
384 | { |
---|
385 | int anz = 0, |
---|
386 | test = N_ADENIN; |
---|
387 | |
---|
388 | while (test < (N_ANY * 2)) |
---|
389 | { |
---|
390 | if (mask & test) anz++; |
---|
391 | |
---|
392 | test *= 2; |
---|
393 | } |
---|
394 | |
---|
395 | return anz; |
---|
396 | } |
---|
397 | |
---|
398 | // ----------------------------------------------------------------------------- |
---|
399 | // Verzweigungsindex einer Base |
---|
400 | // ----------------------------------------------------------------------------- |
---|
401 | int PtNode::IndexOfNext ( Mask base ) |
---|
402 | // ----------------------------------------------------------------------------- |
---|
403 | { |
---|
404 | int index = INVALID; |
---|
405 | |
---|
406 | if ((base >= N_ADENIN) && (base <= N_ANY) && (mask & base)) |
---|
407 | { |
---|
408 | int test = N_ADENIN; |
---|
409 | |
---|
410 | index = 0; |
---|
411 | |
---|
412 | while (test <= base) |
---|
413 | { |
---|
414 | if (mask & test) index++; |
---|
415 | |
---|
416 | test *= 2; |
---|
417 | } |
---|
418 | |
---|
419 | index--; |
---|
420 | } |
---|
421 | |
---|
422 | return index; |
---|
423 | } |
---|
424 | |
---|
425 | // ----------------------------------------------------------------------------- |
---|
426 | // Ausgabefunktion fuer ein Knoten eines Positionsbaumes |
---|
427 | // ----------------------------------------------------------------------------- |
---|
428 | void PtNode::Dump ( void ) |
---|
429 | // ----------------------------------------------------------------------------- |
---|
430 | { |
---|
431 | cout << "\nnode: " << this; |
---|
432 | cout << "\nmask = 0x" << hex << mask << "\n"; |
---|
433 | |
---|
434 | if (position) |
---|
435 | { |
---|
436 | int anz = NumOfPos(), |
---|
437 | pos = 0; |
---|
438 | |
---|
439 | cout << "position[" << dec << anz << "] ="; |
---|
440 | |
---|
441 | while (pos < anz) cout << " " << position[pos++]; |
---|
442 | |
---|
443 | cout << "\n"; |
---|
444 | } |
---|
445 | |
---|
446 | if (next) |
---|
447 | { |
---|
448 | int anz = NumOfNext(), |
---|
449 | pos = 0; |
---|
450 | |
---|
451 | cout << "next[" << dec << anz << "]:"; |
---|
452 | |
---|
453 | while (pos < anz) cout << " " << next[pos++]; |
---|
454 | |
---|
455 | cout << "\n"; |
---|
456 | |
---|
457 | pos = 0; |
---|
458 | |
---|
459 | while (pos < anz) next[pos++]->Dump(); |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | // ----------------------------------------------------------------------------- |
---|
464 | // Ausgabefunktion fuer einen Positionsbaum |
---|
465 | // ----------------------------------------------------------------------------- |
---|
466 | void PtNode::Show ( int rec, |
---|
467 | int *where ) |
---|
468 | // ----------------------------------------------------------------------------- |
---|
469 | { |
---|
470 | int panz = NumOfPos(), |
---|
471 | nanz = NumOfNext(); |
---|
472 | str prefix = new char [rec * 3]; |
---|
473 | |
---|
474 | prefix[0] = 0; |
---|
475 | |
---|
476 | if (rec) |
---|
477 | { |
---|
478 | int count = 0; |
---|
479 | |
---|
480 | prefix[0] = 0; |
---|
481 | |
---|
482 | while (count < rec) |
---|
483 | if (where[count++]) strcat(prefix,"| "); |
---|
484 | else strcat(prefix," "); |
---|
485 | } |
---|
486 | |
---|
487 | if (panz) |
---|
488 | { |
---|
489 | Base base = ADENIN; |
---|
490 | Mask which = P_ADENIN; |
---|
491 | |
---|
492 | while (which <= P_FINISH) |
---|
493 | { |
---|
494 | int index = IndexOfPos(which); |
---|
495 | |
---|
496 | if ((index != INVALID) && (index < panz)) |
---|
497 | { |
---|
498 | if (base == BASEN) |
---|
499 | { |
---|
500 | if (!nanz && (index == (panz - 1))) |
---|
501 | cout << prefix << "\\- # " << position[index] << "\n"; |
---|
502 | else |
---|
503 | cout << prefix << "|- # " << position[index] << "\n"; |
---|
504 | } |
---|
505 | else |
---|
506 | { |
---|
507 | if (!nanz && (index == (panz - 1))) |
---|
508 | cout << prefix << "\\- " << (char)BCharacter[base], |
---|
509 | cout << " " << position[index] << "\n"; |
---|
510 | else |
---|
511 | cout << prefix << "|- " << (char)BCharacter[base], |
---|
512 | cout << " " << position[index] << "\n"; |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | which = (Mask)(which * 2); |
---|
517 | base = (Base)(base + 1); |
---|
518 | } |
---|
519 | } |
---|
520 | |
---|
521 | if (nanz) |
---|
522 | { |
---|
523 | Base base = ADENIN; |
---|
524 | Mask which = N_ADENIN; |
---|
525 | int *wherenew = new int [rec + 1]; |
---|
526 | |
---|
527 | memcpy(wherenew,where,rec * sizeof(int)); |
---|
528 | |
---|
529 | while (which <= N_ANY) |
---|
530 | { |
---|
531 | int index = IndexOfNext(which); |
---|
532 | |
---|
533 | if ((index != INVALID) && (index < nanz)) |
---|
534 | { |
---|
535 | if (base == BASEN) |
---|
536 | { |
---|
537 | if (index == (nanz - 1)) |
---|
538 | cout << prefix << "\\- #\n", |
---|
539 | wherenew[rec] = 0; |
---|
540 | else |
---|
541 | cout << prefix << "|- #\n", |
---|
542 | wherenew[rec] = 1; |
---|
543 | } |
---|
544 | else |
---|
545 | { |
---|
546 | if (index == (nanz - 1)) |
---|
547 | cout << prefix << "\\- " << (char)BCharacter[base] << "\n", |
---|
548 | wherenew[rec] = 0; |
---|
549 | else |
---|
550 | cout << prefix << "|- " << (char)BCharacter[base] << "\n", |
---|
551 | wherenew[rec] = 1; |
---|
552 | } |
---|
553 | |
---|
554 | next[index]->Show(rec + 1,wherenew); |
---|
555 | } |
---|
556 | |
---|
557 | which = (Mask)(which * 2); |
---|
558 | base = (Base)(base + 1); |
---|
559 | } |
---|
560 | |
---|
561 | delete wherenew; |
---|
562 | } |
---|
563 | |
---|
564 | delete prefix; |
---|
565 | } |
---|
566 | |
---|
567 | // ----------------------------------------------------------------------------- |
---|
568 | // Konstruktor zum Erzeugen einer leeren Instanz der Klasse Postree |
---|
569 | // ----------------------------------------------------------------------------- |
---|
570 | Postree::Postree ( void ) : sequence () |
---|
571 | // ----------------------------------------------------------------------------- |
---|
572 | { |
---|
573 | topnode = NULL; |
---|
574 | } |
---|
575 | |
---|
576 | // ----------------------------------------------------------------------------- |
---|
577 | // Konstruktor zum Erzeugen einer Instanz der |
---|
578 | // Klasse Postree aus einer vorgegebenen Sequenz |
---|
579 | // ----------------------------------------------------------------------------- |
---|
580 | Postree::Postree ( str seq ) : sequence ( seq ) |
---|
581 | // ----------------------------------------------------------------------------- |
---|
582 | { |
---|
583 | str seq = sequence.Compressed(); |
---|
584 | |
---|
585 | if (seq) topnode = new PtNode(seq,NULL,0,0), delete seq; |
---|
586 | } |
---|
587 | |
---|
588 | // ----------------------------------------------------------------------------- |
---|
589 | // Konstruktor zum Erzeugen einer Instanz der Klasse Postree aus einer |
---|
590 | // zufaellig besetzten Instanz der Klasse Sequenz mit vorgebener Sequenzlaenge |
---|
591 | // ----------------------------------------------------------------------------- |
---|
592 | Postree::Postree ( uint len ) : sequence ( len ) |
---|
593 | // ----------------------------------------------------------------------------- |
---|
594 | { |
---|
595 | str seq = sequence.Compressed(); |
---|
596 | |
---|
597 | if (seq) topnode = new PtNode(seq,NULL,0,0), delete seq; |
---|
598 | } |
---|
599 | |
---|
600 | // ----------------------------------------------------------------------------- |
---|
601 | // Konstruktor zum Erzeugen einer leeren Instanz der Klasse Postree |
---|
602 | // aus einer Instanz der Klasse Sequenz aus ein vorgebener Datei mit |
---|
603 | // vorgegebener Zeilennummer |
---|
604 | // ----------------------------------------------------------------------------- |
---|
605 | Postree::Postree ( str file, |
---|
606 | uint line ) : sequence ( file, line ) |
---|
607 | // ----------------------------------------------------------------------------- |
---|
608 | { |
---|
609 | str seq = sequence.Compressed(); |
---|
610 | |
---|
611 | if (seq) topnode = new PtNode(seq,NULL,0,0), delete seq; |
---|
612 | } |
---|
613 | |
---|
614 | // ----------------------------------------------------------------------------- |
---|
615 | // Kopierkonstruktor |
---|
616 | // ----------------------------------------------------------------------------- |
---|
617 | Postree::Postree ( Postree &postree ) : sequence ( postree.sequence ) |
---|
618 | // ----------------------------------------------------------------------------- |
---|
619 | { |
---|
620 | cout << "\nKopierkonstruktor(Postree)\n"; |
---|
621 | |
---|
622 | if (!postree.topnode) topnode = NULL; |
---|
623 | else topnode = new PtNode(*postree.topnode); |
---|
624 | } |
---|
625 | |
---|
626 | // ----------------------------------------------------------------------------- |
---|
627 | // Destruktor |
---|
628 | // ----------------------------------------------------------------------------- |
---|
629 | Postree::~Postree ( void ) |
---|
630 | // ----------------------------------------------------------------------------- |
---|
631 | { |
---|
632 | if (topnode) delete topnode; |
---|
633 | } |
---|
634 | |
---|
635 | // ----------------------------------------------------------------------------- |
---|
636 | // Liefert Laenge der kompremierten Sequenz |
---|
637 | // ----------------------------------------------------------------------------- |
---|
638 | int Postree::SeqLen ( void ) |
---|
639 | // ----------------------------------------------------------------------------- |
---|
640 | { |
---|
641 | return sequence.CompressedLen(); |
---|
642 | } |
---|
643 | |
---|
644 | // ----------------------------------------------------------------------------- |
---|
645 | // Exakte Suche nach den Vorkommen einer Teilsequenz |
---|
646 | // ----------------------------------------------------------------------------- |
---|
647 | int Postree::Find ( str seq, |
---|
648 | int **pos, |
---|
649 | int *anz, |
---|
650 | int *len, |
---|
651 | int sort ) |
---|
652 | // ----------------------------------------------------------------------------- |
---|
653 | { |
---|
654 | int result = INVALID; |
---|
655 | |
---|
656 | *pos = NULL; |
---|
657 | *anz = 0; |
---|
658 | *len = 0; |
---|
659 | |
---|
660 | if (seq && *seq && pos && anz && len) |
---|
661 | { |
---|
662 | PtNode *node = topnode; |
---|
663 | |
---|
664 | result = 0; |
---|
665 | |
---|
666 | while (!result && *seq) |
---|
667 | { |
---|
668 | int base = BIndex[*seq++]; |
---|
669 | |
---|
670 | if ((base < ADENIN) || |
---|
671 | (base > URACIL)) result = INVALID; |
---|
672 | else |
---|
673 | { |
---|
674 | Mask pmask = P_ADENIN, |
---|
675 | nmask = N_ADENIN; |
---|
676 | |
---|
677 | while (base--) |
---|
678 | { |
---|
679 | pmask = (Mask)(pmask * 2); |
---|
680 | nmask = (Mask)(nmask * 2); |
---|
681 | } |
---|
682 | |
---|
683 | if (node->mask & pmask) // Position gefunden |
---|
684 | { |
---|
685 | (*len)++; |
---|
686 | |
---|
687 | *anz = 1; |
---|
688 | *pos = new int [1]; |
---|
689 | |
---|
690 | if (!*pos) result = INVALID; // Speicher |
---|
691 | else |
---|
692 | { |
---|
693 | *pos[0] = node->position[node->IndexOfPos(pmask)]; |
---|
694 | |
---|
695 | if (*seq) // Gesuchte Sequenz ist laenger als Baum tief |
---|
696 | { |
---|
697 | str org = sequence.Compressed(); |
---|
698 | |
---|
699 | if (!org) result = INVALID; // Speicher |
---|
700 | else |
---|
701 | { |
---|
702 | str ptr = &org[*pos[0] + *len]; |
---|
703 | |
---|
704 | while (*ptr && *seq && (*seq == *ptr)) |
---|
705 | { |
---|
706 | (*len)++; |
---|
707 | seq++; |
---|
708 | ptr++; |
---|
709 | } |
---|
710 | |
---|
711 | if (*seq && *ptr) result = 1; // Unterschied |
---|
712 | |
---|
713 | delete org; |
---|
714 | } |
---|
715 | } |
---|
716 | } |
---|
717 | } |
---|
718 | else if (node->mask & nmask) // Verzweigung existiert |
---|
719 | { |
---|
720 | (*len)++; |
---|
721 | |
---|
722 | node = node->next[node->IndexOfNext(nmask)]; |
---|
723 | } |
---|
724 | else // Unterschied => alle Moeglichkeiten suchen |
---|
725 | { |
---|
726 | result = 1; |
---|
727 | |
---|
728 | *pos = FindLeaves(*node,anz); |
---|
729 | } |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | if (!result && !*seq && !*pos) // Sequenz ist kuerzer als Baum tief |
---|
734 | { |
---|
735 | *pos = FindLeaves(*node,anz); |
---|
736 | |
---|
737 | if (!*pos) result = INVALID; // Speicher |
---|
738 | } |
---|
739 | } |
---|
740 | |
---|
741 | if (sort && *pos && (*anz > 1)) SortIntArray(*pos,anz); |
---|
742 | |
---|
743 | return result; |
---|
744 | } |
---|
745 | |
---|
746 | // ----------------------------------------------------------------------------- |
---|
747 | // Suche nach den Vorkommen einer Teilsequenz mit bis zu einer Substitution |
---|
748 | // ----------------------------------------------------------------------------- |
---|
749 | int Postree::OneSubstitution ( str seq, |
---|
750 | int **pos, |
---|
751 | int *anz, |
---|
752 | int *len ) |
---|
753 | // ----------------------------------------------------------------------------- |
---|
754 | { |
---|
755 | int result = INVALID, |
---|
756 | seqlen = strlen(seq), |
---|
757 | *lpos = NULL, |
---|
758 | lanz = 0, |
---|
759 | llen = 0, |
---|
760 | res, |
---|
761 | count; |
---|
762 | str search = new char [seqlen + 1]; |
---|
763 | |
---|
764 | for (count = 0;count < seqlen;count++) |
---|
765 | { |
---|
766 | int base; |
---|
767 | |
---|
768 | strcpy(search,seq); |
---|
769 | |
---|
770 | for (base = ADENIN;base <= URACIL;base++) |
---|
771 | { |
---|
772 | if (search[count] == BCharacter[base]) continue; |
---|
773 | |
---|
774 | search[count] = BCharacter[base]; |
---|
775 | |
---|
776 | res = Find(search,&lpos,&lanz,&llen,0); |
---|
777 | |
---|
778 | if (res >= 0) |
---|
779 | { |
---|
780 | if (llen > *len) |
---|
781 | { |
---|
782 | if (*pos) delete *pos; |
---|
783 | |
---|
784 | *pos = lpos; |
---|
785 | *len = llen; |
---|
786 | *anz = lanz; |
---|
787 | result = res; |
---|
788 | } |
---|
789 | else if (llen == *len) |
---|
790 | { |
---|
791 | int *tmp = new int [*anz + lanz]; |
---|
792 | |
---|
793 | if (*anz) memcpy(tmp,*pos,*anz * sizeof(int)); |
---|
794 | |
---|
795 | memcpy(&tmp[*anz],lpos,lanz * sizeof(int)); |
---|
796 | |
---|
797 | if (*pos) delete *pos; |
---|
798 | |
---|
799 | *pos = tmp; |
---|
800 | *anz += lanz; |
---|
801 | result = res; |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | search[count] = seq[count]; |
---|
806 | } |
---|
807 | } |
---|
808 | |
---|
809 | delete search; |
---|
810 | |
---|
811 | return result; |
---|
812 | } |
---|
813 | |
---|
814 | // ----------------------------------------------------------------------------- |
---|
815 | // Suche nach den Vorkommen einer Teilsequenz mit bis zu einer Deletion |
---|
816 | // ----------------------------------------------------------------------------- |
---|
817 | int Postree::OneDeletion ( str seq, |
---|
818 | int **pos, |
---|
819 | int *anz, |
---|
820 | int *len ) |
---|
821 | // ----------------------------------------------------------------------------- |
---|
822 | { |
---|
823 | int result = INVALID, |
---|
824 | seqlen = strlen(seq), |
---|
825 | *lpos = NULL, |
---|
826 | lanz = 0, |
---|
827 | llen = 0, |
---|
828 | res, |
---|
829 | count; |
---|
830 | str search = new char [seqlen + 1]; |
---|
831 | |
---|
832 | for (count = 0;count < seqlen;count++) |
---|
833 | { |
---|
834 | int base; |
---|
835 | |
---|
836 | if (!count) *search = 0; |
---|
837 | else strncpy(search,seq,count), search[count] = 0; |
---|
838 | |
---|
839 | strcat(search,&seq[count + 1]); |
---|
840 | |
---|
841 | res = Find(search,&lpos,&lanz,&llen,0); |
---|
842 | |
---|
843 | if (res >= 0) |
---|
844 | { |
---|
845 | if (llen > *len) |
---|
846 | { |
---|
847 | if (*pos) delete *pos; |
---|
848 | |
---|
849 | *pos = lpos; |
---|
850 | *len = llen; |
---|
851 | *anz = lanz; |
---|
852 | result = res; |
---|
853 | } |
---|
854 | else if (llen == *len) |
---|
855 | { |
---|
856 | int *tmp = new int [*anz + lanz]; |
---|
857 | |
---|
858 | if (*anz) memcpy(tmp,*pos,*anz * sizeof(int)); |
---|
859 | |
---|
860 | memcpy(&tmp[*anz],lpos,lanz * sizeof(int)); |
---|
861 | |
---|
862 | if (*pos) delete *pos; |
---|
863 | |
---|
864 | *pos = tmp; |
---|
865 | *anz += lanz; |
---|
866 | result = res; |
---|
867 | } |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | delete search; |
---|
872 | |
---|
873 | return result; |
---|
874 | } |
---|
875 | |
---|
876 | // ----------------------------------------------------------------------------- |
---|
877 | // Suche nach den Vorkommen einer Teilsequenz mit bis zu einer Insertion |
---|
878 | // ----------------------------------------------------------------------------- |
---|
879 | int Postree::OneInsertion ( str seq, |
---|
880 | int **pos, |
---|
881 | int *anz, |
---|
882 | int *len ) |
---|
883 | // ----------------------------------------------------------------------------- |
---|
884 | { |
---|
885 | int result = INVALID, |
---|
886 | seqlen = strlen(seq), |
---|
887 | *lpos = NULL, |
---|
888 | lanz = 0, |
---|
889 | llen = 0, |
---|
890 | res, |
---|
891 | count; |
---|
892 | str search = new char [seqlen + 2]; |
---|
893 | |
---|
894 | for (count = 0;count <= seqlen;count++) |
---|
895 | { |
---|
896 | int base; |
---|
897 | |
---|
898 | if (!count) search[0] = ' ', |
---|
899 | search[1] = 0; |
---|
900 | else strncpy(search,seq,count), |
---|
901 | search[count] = ' ', |
---|
902 | search[count + 1] = 0; |
---|
903 | |
---|
904 | strcat(search,&seq[count]); |
---|
905 | |
---|
906 | for (base = ADENIN;base <= URACIL;base++) |
---|
907 | { |
---|
908 | search[count] = BCharacter[base]; |
---|
909 | |
---|
910 | res = Find(search,&lpos,&lanz,&llen,0); |
---|
911 | |
---|
912 | if (res >= 0) |
---|
913 | { |
---|
914 | if (llen > *len) |
---|
915 | { |
---|
916 | if (*pos) delete *pos; |
---|
917 | |
---|
918 | *pos = lpos; |
---|
919 | *len = llen; |
---|
920 | *anz = lanz; |
---|
921 | result = res; |
---|
922 | } |
---|
923 | else if (llen == *len) |
---|
924 | { |
---|
925 | int *tmp = new int [*anz + lanz]; |
---|
926 | |
---|
927 | if (*anz) memcpy(tmp,*pos,*anz * sizeof(int)); |
---|
928 | |
---|
929 | memcpy(&tmp[*anz],lpos,lanz * sizeof(int)); |
---|
930 | |
---|
931 | if (*pos) delete *pos; |
---|
932 | |
---|
933 | *pos = tmp; |
---|
934 | *anz += lanz; |
---|
935 | result = res; |
---|
936 | } |
---|
937 | } |
---|
938 | |
---|
939 | search[count] = seq[count]; |
---|
940 | } |
---|
941 | } |
---|
942 | |
---|
943 | delete search; |
---|
944 | |
---|
945 | return result; |
---|
946 | } |
---|
947 | |
---|
948 | // ----------------------------------------------------------------------------- |
---|
949 | // Suche nach den Vorkommen einer Teilsequenz mit bis zu einem Fehler |
---|
950 | // ----------------------------------------------------------------------------- |
---|
951 | int Postree::OneMismatch ( str seq, |
---|
952 | int **pos, |
---|
953 | int *anz, |
---|
954 | int *len ) |
---|
955 | // ----------------------------------------------------------------------------- |
---|
956 | { |
---|
957 | int result = Find(seq,pos,anz,len,0); |
---|
958 | |
---|
959 | if (result >= 0) |
---|
960 | { |
---|
961 | int *lpos[3] = { NULL, NULL, NULL }, |
---|
962 | lanz[3] = { 0, 0, 0 }, |
---|
963 | llen[3] = { *len, 0, *len }, |
---|
964 | res [3] = { INVALID, INVALID, INVALID }, |
---|
965 | count; |
---|
966 | |
---|
967 | res[0] = OneSubstitution(seq,&lpos[0],&lanz[0],&llen[0]); |
---|
968 | res[1] = OneDeletion (seq,&lpos[1],&lanz[1],&llen[1]); |
---|
969 | res[2] = OneInsertion (seq,&lpos[2],&lanz[2],&llen[2]); |
---|
970 | |
---|
971 | for (count = 0; count < 3;count++) |
---|
972 | { |
---|
973 | if (res[count] >= 0) |
---|
974 | { |
---|
975 | result = 1; |
---|
976 | |
---|
977 | if (llen[count] > *len) |
---|
978 | { |
---|
979 | if (*pos) delete *pos; |
---|
980 | |
---|
981 | *pos = lpos[count]; |
---|
982 | *len = llen[count]; |
---|
983 | *anz = lanz[count]; |
---|
984 | result = res [count]; |
---|
985 | |
---|
986 | lpos[count] = NULL; |
---|
987 | } |
---|
988 | else if (llen[count] == *len) |
---|
989 | { |
---|
990 | int *tmp = new int [*anz + lanz[count]]; |
---|
991 | |
---|
992 | if (*anz) memcpy(tmp,*pos,*anz * sizeof(int)); |
---|
993 | |
---|
994 | memcpy(&tmp[*anz],lpos[count],lanz[count] * sizeof(int)); |
---|
995 | |
---|
996 | if (*pos) delete *pos; |
---|
997 | |
---|
998 | *pos = tmp; |
---|
999 | *anz += lanz[count]; |
---|
1000 | } |
---|
1001 | } |
---|
1002 | |
---|
1003 | if (lpos[count]) delete lpos[count]; |
---|
1004 | } |
---|
1005 | |
---|
1006 | if (*pos && (*anz > 1)) SortIntArray(*pos,anz); |
---|
1007 | } |
---|
1008 | |
---|
1009 | return result; |
---|
1010 | } |
---|
1011 | |
---|
1012 | // ----------------------------------------------------------------------------- |
---|
1013 | // Ausgabefunktion fur eine Instanz der Klasse Postree zu debug-Zwecken |
---|
1014 | // ----------------------------------------------------------------------------- |
---|
1015 | void Postree::Dump ( void ) |
---|
1016 | // ----------------------------------------------------------------------------- |
---|
1017 | { |
---|
1018 | cout << "\nsequence :\n"; |
---|
1019 | |
---|
1020 | sequence.Dump(); |
---|
1021 | |
---|
1022 | cout << "topnode :\n"; |
---|
1023 | |
---|
1024 | if (topnode) topnode->Dump(), cout << "\n"; |
---|
1025 | } |
---|
1026 | |
---|
1027 | // ----------------------------------------------------------------------------- |
---|
1028 | // Ausgabefunktion fur eine Instanz der Klasse Postree |
---|
1029 | // ----------------------------------------------------------------------------- |
---|
1030 | void Postree::Show ( int mode ) |
---|
1031 | // ----------------------------------------------------------------------------- |
---|
1032 | { |
---|
1033 | switch (mode) |
---|
1034 | { |
---|
1035 | case 0: cout << "\npostree:\n\n"; |
---|
1036 | if (topnode) topnode->Show(0,NULL), cout << "\n"; |
---|
1037 | break; |
---|
1038 | case 1: |
---|
1039 | { |
---|
1040 | str seq = sequence.Compressed(); |
---|
1041 | |
---|
1042 | cout << "\nsequence:\n\n"; |
---|
1043 | |
---|
1044 | if (seq) |
---|
1045 | { |
---|
1046 | cout << "Laenge: " << strlen(seq) << "\n"; |
---|
1047 | cout << seq << "\n"; |
---|
1048 | delete seq; |
---|
1049 | } |
---|
1050 | |
---|
1051 | break; |
---|
1052 | } |
---|
1053 | case 2: cout << "\nsequence :\n"; |
---|
1054 | sequence.Dump(); |
---|
1055 | cout << "\npostree :\n\n"; |
---|
1056 | if (topnode) topnode->Show(0,NULL), cout << "\n"; |
---|
1057 | break; |
---|
1058 | } |
---|
1059 | } |
---|