| 1 | // PatMaN DNA pattern matcher |
| 2 | // (C) 2007 Kay Pruefer, Udo Stenzel |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or (at |
| 7 | // your option) any later version. See the LICENSE file for details. |
| 8 | |
| 9 | #ifndef INCLUDED_PREFIX_TREE_H |
| 10 | #define INCLUDED_PREFIX_TREE_H |
| 11 | |
| 12 | #include <deque> |
| 13 | #include <string> |
| 14 | |
| 15 | #ifndef NDEBUG |
| 16 | #include <istream> |
| 17 | #endif |
| 18 | |
| 19 | #include "global.h" |
| 20 | |
| 21 | struct probe_s { |
| 22 | probe_s *next ; |
| 23 | std::string name ; |
| 24 | int length ; |
| 25 | char strand ; // + or - |
| 26 | } ; |
| 27 | |
| 28 | struct node { |
| 29 | node* childs[5] ; |
| 30 | node* suffix ; |
| 31 | probe_s *probes ; |
| 32 | unsigned char drop[5] ; |
| 33 | unsigned char drop_suffix ; |
| 34 | unsigned char depth ; |
| 35 | #ifndef NDEBUG |
| 36 | std::string label ; |
| 37 | #endif |
| 38 | |
| 39 | node() : suffix(0), probes(0), drop_suffix(0), depth(0) |
| 40 | { |
| 41 | for( int i = 0 ; i != 5 ; ++i ) |
| 42 | { |
| 43 | childs[i] = 0 ; |
| 44 | drop[i] = 0 ; |
| 45 | } |
| 46 | } |
| 47 | } ; |
| 48 | |
| 49 | /* Mismatch pointers (elsewhere called "walkers") point into the prefix |
| 50 | * trie, track the history of an alignment and are advanced according to |
| 51 | * the read database. |
| 52 | * |
| 53 | * We will keep mismatch pointers in a queue, implemented as singly |
| 54 | * linked list, and because we splice inside that list, it's probably |
| 55 | * best to implement it directly (instead of encapsulating everything |
| 56 | * behind an object). Unused nodes will be collected in another list |
| 57 | * (the junk heap). |
| 58 | */ |
| 59 | struct mismatch_ptr { |
| 60 | struct mismatch_ptr *next ; |
| 61 | const node *ptr ; |
| 62 | unsigned char mismatch ; |
| 63 | unsigned char first ; |
| 64 | unsigned char matched ; |
| 65 | unsigned char isgap : 1 ; |
| 66 | unsigned char gaps : 7 ; |
| 67 | } ; |
| 68 | |
| 69 | class prefix_tree |
| 70 | { |
| 71 | public: |
| 72 | prefix_tree( ) ; |
| 73 | void add_seq( const std::string& seq, const std::string& name, bool ambi_codes ) ; |
| 74 | mismatch_ptr *compare( char c, mismatch_ptr *ptrs ) const ; |
| 75 | void add_suffix_links() ; |
| 76 | mismatch_ptr *init( mismatch_ptr* ) const ; |
| 77 | |
| 78 | #ifndef NDEBUG |
| 79 | void debug( std::ostream& s ) const { debug( s, nodes[0] ) ; } |
| 80 | void debug( std::ostream&, const node& ) const ; |
| 81 | #endif |
| 82 | |
| 83 | private: |
| 84 | node* create_go_node( node* n, int pos ) ; |
| 85 | mismatch_ptr *seed() const ; |
| 86 | void add_recursion( const std::string& seq, const std::string& name, node* ptr, size_t i, char strand ) ; |
| 87 | void add_recursion_ambiguity( const std::string& seq, const std::string& name, node* ptr, size_t i, char strand ) ; |
| 88 | |
| 89 | std::deque<node> nodes ; |
| 90 | } ; |
| 91 | |
| 92 | #endif |