source: tags/cvs_2_svn/PARSIMONY/ap_tree_nlen.hxx

Last change on this file was 5390, checked in by westram, 16 years ago
  • TAB-Ex
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1#include <iostream>
2#include <limits.h>
3
4#define ap_assert(x) arb_assert(x)
5
6class       AP_tree_nlen;
7extern long global_combineCount;
8
9struct AP_CO_LIST {     // liste fuer crossover
10    GBDATA *leaf0;
11    GBDATA *leaf1;
12    int     node0;
13    int     node1;
14
15    class AP_tree_nlen *pntr;
16};
17
18typedef enum {  // flags fuer die Rekursionsart bei Kernighan Lin
19    AP_DYNAMIK       = 1,
20    AP_STATIC        = 2,
21    AP_BETTER        = 4,
22    // Funktionstyp der Schwellwertfunktion
23    AP_QUADRAT_START = 5,
24    AP_QUADRAT_MAX   = 6
25} AP_KL_FLAG;
26
27typedef enum {
28    APBL_NONE                = 0,
29    AP_BL_NNI_ONLY           = 1, // try te find a better tree only
30    AP_BL_BL_ONLY            = 2, // try to calculate the branch lengths
31    AP_BL_NNI_BL             = 3, // better tree & branch lengths
32    AP_BL_BOOTSTRAP_LIMIT    = 4, // calculate upper bootstrap limits
33    AP_BL_BOOTSTRAP_ESTIMATE = 12 // calculate estimate of bootstrap
34} AP_BL_MODE;
35
36class AP_tree_edge;
37
38class AP_tree_nlen : public AP_tree {           /* tree that is independent of branch lengths and root */
39    AP_tree *dup(void);
40
41    AP_BOOL clear(unsigned long stack_update,unsigned long user_push_counter);
42    void    unhash_sequence(void);
43    void    createListRekUp(AP_CO_LIST *list,int *cn);
44    void    createListRekSide(AP_CO_LIST *list,int *cn);
45
46
47    AP_TREE_SIDE kernighan;     // Flag zum markieren
48    int          distance;      // distance to tree border (0=leaf, INT_MAX=UNKNOWN)
49
50    // definitions for AP_tree_edge:
51
52    AP_tree_edge *edge[3];      // the edges to the father and the sons
53    int           index[3];     // index to node[] in AP_tree_edge
54
55protected:
56
57public:
58    AP_FLOAT costs(void);       /* cost of a tree (number of changes ..)*/
59    AP_BOOL  push(AP_STACK_MODE, unsigned long); /* push state of costs */
60    void     pop(unsigned long); /* pop old tree costs */
61   
62    virtual AP_UPDATE_FLAGS check_update(); // disable  load !!!!
63
64
65    AP_tree_nlen(AP_tree_root *tree_root);
66    virtual ~AP_tree_nlen() {}
67
68    void copy(AP_tree_nlen *tree);
69    int  Distance();
70
71    // tree reconstruction methods:
72    GB_ERROR insert(AP_tree *new_brother);
73    GB_ERROR remove(void);
74    GB_ERROR swap_assymetric(AP_TREE_SIDE modus);
75    GB_ERROR move(AP_tree *new_brother,AP_FLOAT rel_pos);
76    GB_ERROR set_root();
77
78    // tree optimization methods:
79    void parsimony_rek();
80
81    AP_FLOAT nn_interchange_rek(AP_BOOL     openclosestatus,
82                                int        &abort,
83                                int         deep, // -1 means: do whole subtree
84                                AP_BL_MODE  mode        = AP_BL_NNI_ONLY,
85                                GB_BOOL     skip_hidden = GB_FALSE);
86
87    AP_FLOAT nn_interchange(AP_FLOAT parsimony,AP_BL_MODE mode);
88
89    void kernighan_rek(int         rek_deep,
90                       int        *rek_breite,
91                       int         rek_breite_anz,
92                       const int   rek_deep_max,
93                       double (*funktion)(double,double *,int),
94                       double     *param_liste,
95                       int         param_anz,
96                       AP_FLOAT    pars_best,
97                       AP_FLOAT    pars_start,
98                       AP_FLOAT    pars_prev,
99                       AP_KL_FLAG  searchflag,
100                       AP_BOOL    *abbruch_flag);
101
102    // for crossover creates a list of 3 times the nodes with all
103    // ancestors in it
104    AP_CO_LIST * createList(int *size);
105
106    class AP_tree_stack stack;  /* tree stack */
107
108    // misc stuff:
109
110    void setBranchlen(double leftLen,double rightLen) { leftlen = leftLen; rightlen = rightLen; }
111
112    int         test() const;
113    const char* fullname() const;
114
115    const char* sortByName();
116
117    // casted access to neighbours:
118
119    AP_tree_nlen *Father() const { return (AP_tree_nlen*)father; }
120    AP_tree_nlen *Brother() const { return (AP_tree_nlen*)brother(); }
121    AP_tree_nlen *Leftson() const { return (AP_tree_nlen*)leftson; }
122    AP_tree_nlen *Rightson() const { return (AP_tree_nlen*)rightson; }
123
124    // AP_tree_edge access functions:
125
126    int indexOf(const AP_tree_edge *e) const { int i; for (i=0; i<3; i++) if (edge[i]==e) return i;return -1; }
127
128    AP_tree_edge* edgeTo(const AP_tree_nlen *brother) const;
129    AP_tree_edge* nextEdge(const AP_tree_edge *thisEdge=NULL) const;
130    int unusedEdge() const;
131
132    // more complex edge functions:
133
134    void linkAllEdges(AP_tree_edge *edge1, AP_tree_edge *edge2, AP_tree_edge *edge3);
135    void unlinkAllEdges(AP_tree_edge **edgePtr1, AP_tree_edge **edgePtr2, AP_tree_edge **edgePtr3);
136
137    //void      destroyAllEdges();
138
139    // test stuff:
140
141    char *getSequence();
142
143    friend      class AP_tree_edge;
144    friend      std::ostream& operator<<(std::ostream&,const AP_tree_nlen&);
145};
146
147
148/************ AP_tree_edge  ************/
149class AP_tree_edge
150{
151    // the following members are stored/restored by
152    // AP_tree_nlen::push/pop:
153
154    AP_tree_nlen      *node[2]; // the two nodes of this edge
155    int                index[2]; // index to edge[] in AP_tree_nlen
156    AP_tree_edge_data  data;    // data stored in edge (defined in AP_buffer.hxx)
157
158    // and these arent pushed:
159
160    AP_tree_edge *next;                 // temporary next pointer used by some methods
161    int           used;                 // test-counter
162    long          age;                  // age of the edge
163
164    static long timeStamp;              // static counter for edge-age
165    // recursive methods:
166    //
167    //  deep:   determines how deep we go into the tree
168    //          -1 means we go through the whole tree
169    //           0 means we only act on the actual edge
170
171    //    int clearValues(int deep,AP_tree_nlen *skip=NULL);    // clears used and data.distance
172    AP_tree_edge *buildChain(int                 deep,
173                             GB_BOOL             skip_hidden = GB_FALSE,
174                             int                 distance    = 0,
175                             const AP_tree_nlen *skip        = NULL,
176                             AP_tree_edge       *comesFrom   = NULL);
177
178    long sizeofChain(void);
179    void calcDistance();
180    void tailDistance(AP_tree_nlen*);
181   
182    int distanceOK() const { int diff = node[0]->distance-node[1]->distance; return diff>=-1 && diff<=1; }
183
184    // my friends:
185
186    friend class AP_tree_nlen;
187    friend std::ostream& operator<<(std::ostream&,const AP_tree_edge&);
188
189protected:
190
191    ~AP_tree_edge();
192
193    // relink & unlink (they are also used by constructor & destructor)
194
195    void relink(AP_tree_nlen *node1, AP_tree_nlen *node2);
196    AP_tree_edge *unlink();
197
198public:
199
200    AP_tree_edge(AP_tree_nlen *node1, AP_tree_nlen *node2);
201
202    static void initialize(AP_tree_nlen *root);
203
204    // access methods:
205
206    int isConnectedTo(const AP_tree_nlen *n) const              { return node[0]==n || node[1]==n; }
207    int indexOf(const AP_tree_nlen *n) const                    { ap_assert(isConnectedTo(n)); return node[1] == n; }
208    AP_tree_nlen* otherNode(const AP_tree_nlen *n) const        { return node[1-indexOf(n)]; }
209    AP_tree_nlen* sonNode() const                               { return node[0]->Father() == node[1] ? node[0] : node[1]; }
210    AP_tree_edge* Next() const                                  { return next; }
211    long Age() const                                            { return age; }
212
213    // encapsulated AP_tree_nlen methods:
214
215    GB_ERROR set_root()                                         { return sonNode()->set_root(); }
216
217    // tree optimization:
218
219    AP_FLOAT nni_rek(AP_BOOL       openclosestatus,
220                     int&          Abort,
221                     int           deep,
222                     GB_BOOL       skip_hidden = GB_FALSE,
223                     AP_BL_MODE    mode        = AP_BL_NNI_ONLY,
224                     AP_tree_nlen *skipNode    = NULL);
225
226    AP_FLOAT nni(AP_FLOAT pars_one, AP_BL_MODE mode);
227
228    // test methods:
229
230    void mixTree(int cnt);
231    int test() const;
232    int dumpChain() const;
233    void testChain(int deep);
234
235    int Distance() const { ap_assert(distanceOK()); return (node[0]->distance+node[1]->distance) >> 1; }
236    int distanceToBorder(int maxsearch=INT_MAX,AP_tree_nlen *skip=NULL) const;  // obsolete
237
238    static int dumpNNI;             // should NNI dump its values?
239    static int distInsertBorder; // distance from insert pos to tree border
240    static int basesChanged;    // no of bases which were changed
241    // in fathers sequence because of insertion
242
243    void countSpecies(int deep=-1,const AP_tree_nlen* skip=NULL);
244
245    static int speciesInTree;   // no of species (leafs) in tree
246    static int nodesInTree;     // no of nodes in tree
247};
248
249std::ostream& operator<<(std::ostream&, const AP_tree_edge&);
250
251/******** two easy-access-functions for the root *********/
252
253inline AP_tree_nlen *rootNode() {
254    return ((AP_tree_nlen*)*ap_main->tree_root);
255}
256
257inline AP_tree_edge *rootEdge() {
258    return rootNode()->Leftson()->edgeTo(rootNode()->Rightson());
259}
260
261/**************************************************/
Note: See TracBrowser for help on using the repository browser.