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