1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : ad_trees.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #include "NT_local.h" |
---|
12 | #include "ad_trees.h" |
---|
13 | #include "NT_tree_cmp.h" |
---|
14 | |
---|
15 | #include <CT_ctree.hxx> |
---|
16 | |
---|
17 | #include <TreeAdmin.h> |
---|
18 | #include <TreeRead.h> |
---|
19 | #include <TreeWrite.h> |
---|
20 | #include <TreeCallbacks.hxx> |
---|
21 | |
---|
22 | #include <awt_sel_boxes.hxx> |
---|
23 | #include <awt_modules.hxx> |
---|
24 | #include <awt_TreeAwars.hxx> |
---|
25 | |
---|
26 | #include <aw_awars.hxx> |
---|
27 | #include <aw_edit.hxx> |
---|
28 | #include <aw_file.hxx> |
---|
29 | #include <aw_msg.hxx> |
---|
30 | #include <aw_root.hxx> |
---|
31 | #include <aw_select.hxx> |
---|
32 | |
---|
33 | #include <arb_strbuf.h> |
---|
34 | #include <arb_file.h> |
---|
35 | #include <arb_diff.h> |
---|
36 | |
---|
37 | #include <cctype> |
---|
38 | |
---|
39 | #define AWAR_TREE_SAV "ad_tree/" |
---|
40 | #define AWAR_TREE_TMP "tmp/ad_tree/" |
---|
41 | |
---|
42 | #define AWAR_TREE_SECURITY AWAR_TREE_TMP "tree_security" |
---|
43 | #define AWAR_TREE_REM AWAR_TREE_TMP "tree_rem" |
---|
44 | #define AWAR_TREE_IMPORT AWAR_TREE_TMP "import_tree" |
---|
45 | #define AWAR_NODE_INFO_ONLY_MARKED AWAR_TREE_TMP "import_only_marked_node_info" |
---|
46 | |
---|
47 | #define AWAR_TREE_EXPORT_FILEBASE AWAR_TREE_TMP "export_tree" |
---|
48 | #define AWAR_TREE_EXPORT_FILTER AWAR_TREE_EXPORT_FILEBASE "/filter" |
---|
49 | #define AWAR_TREE_EXPORT_NAME AWAR_TREE_EXPORT_FILEBASE "/file_name" |
---|
50 | |
---|
51 | #define AWAR_TREE_EXPORT_SAV AWAR_TREE_SAV "export_tree/" |
---|
52 | |
---|
53 | #define AWAR_TREE_EXPORT_FORMAT AWAR_TREE_EXPORT_SAV "format" |
---|
54 | #define AWAR_TREE_EXPORT_NDS AWAR_TREE_EXPORT_SAV "NDS" |
---|
55 | #define AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS AWAR_TREE_EXPORT_SAV "bootstraps" |
---|
56 | #define AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS AWAR_TREE_EXPORT_SAV "branchlens" |
---|
57 | #define AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES AWAR_TREE_EXPORT_SAV "groupnames" |
---|
58 | #define AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS AWAR_TREE_EXPORT_SAV "hide_folded" |
---|
59 | #define AWAR_TREE_EXPORT_QUOTEMODE AWAR_TREE_EXPORT_SAV "quote_mode" |
---|
60 | #define AWAR_TREE_EXPORT_REPLACE AWAR_TREE_EXPORT_SAV "replace" |
---|
61 | |
---|
62 | |
---|
63 | #define AWAR_TREE_CONSENSE_TMP AWAR_TREE_TMP "consense/" |
---|
64 | #define AWAR_TREE_CONSENSE_SAV AWAR_TREE_SAV "consense/" |
---|
65 | |
---|
66 | #define AWAR_TREE_CONSENSE_TREE AWAR_TREE_CONSENSE_SAV "tree" |
---|
67 | #define AWAR_TREE_CONSENSE_SELECTED AWAR_TREE_CONSENSE_TMP "selected" |
---|
68 | |
---|
69 | static void tree_vars_callback(AW_root *aw_root) // Map tree vars to display objects |
---|
70 | { |
---|
71 | if (GLOBAL.gb_main) { |
---|
72 | GB_push_transaction(GLOBAL.gb_main); |
---|
73 | char *treename = aw_root->awar(AWAR_TREE_NAME)->read_string(); |
---|
74 | GBDATA *ali_cont = GBT_find_tree(GLOBAL.gb_main, treename); |
---|
75 | if (!ali_cont) { |
---|
76 | aw_root->awar(AWAR_TREE_SECURITY)->unmap(); |
---|
77 | aw_root->awar(AWAR_TREE_REM)->unmap(); |
---|
78 | } |
---|
79 | else { |
---|
80 | GBDATA *tree_prot = GB_search(ali_cont, "security", GB_FIND); |
---|
81 | if (!tree_prot) GBT_readOrCreate_int(ali_cont, "security", GB_read_security_write(ali_cont)); |
---|
82 | tree_prot = GB_search(ali_cont, "security", GB_INT); |
---|
83 | |
---|
84 | GBDATA *tree_rem = GB_search(ali_cont, "remark", GB_STRING); |
---|
85 | aw_root->awar(AWAR_TREE_SECURITY)->map(tree_prot); |
---|
86 | aw_root->awar(AWAR_TREE_REM) ->map(tree_rem); |
---|
87 | } |
---|
88 | char *suffix = aw_root->awar(AWAR_TREE_EXPORT_FILTER)->read_string(); |
---|
89 | char *fname = GBS_string_eval(treename, GBS_global_string("*=*1.%s:tree_*=*1", suffix), 0); |
---|
90 | aw_root->awar(AWAR_TREE_EXPORT_NAME)->write_string(fname); // create default file name |
---|
91 | free(fname); |
---|
92 | free(suffix); |
---|
93 | GB_pop_transaction(GLOBAL.gb_main); |
---|
94 | free(treename); |
---|
95 | } |
---|
96 | } |
---|
97 | // update import tree name depending on file name |
---|
98 | static void tree_import_callback(AW_root *aw_root) { |
---|
99 | GB_transaction ta(GLOBAL.gb_main); |
---|
100 | char *treename = aw_root->awar(AWAR_TREE_IMPORT "/file_name")->read_string(); |
---|
101 | char *treename_nopath = strrchr(treename, '/'); |
---|
102 | |
---|
103 | if (treename_nopath) { |
---|
104 | ++treename_nopath; |
---|
105 | } |
---|
106 | else { |
---|
107 | treename_nopath = treename; |
---|
108 | } |
---|
109 | |
---|
110 | char *fname = GBS_string_eval(treename_nopath, "*.tree=tree_*1:*.ntree=tree_*1:*.xml=tree_*1:.=", 0); |
---|
111 | aw_root->awar(AWAR_TREE_IMPORT "/tree_name")->write_string(fname); |
---|
112 | |
---|
113 | free(fname); |
---|
114 | free(treename); |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | static void ad_tree_set_security(AW_root *aw_root) |
---|
119 | { |
---|
120 | if (GLOBAL.gb_main) { |
---|
121 | GB_transaction ta(GLOBAL.gb_main); |
---|
122 | char *treename = aw_root->awar(AWAR_TREE_NAME)->read_string(); |
---|
123 | GBDATA *ali_cont = GBT_find_tree(GLOBAL.gb_main, treename); |
---|
124 | if (ali_cont) { |
---|
125 | long prot = aw_root->awar(AWAR_TREE_SECURITY)->read_int(); |
---|
126 | long old; |
---|
127 | old = GB_read_security_delete(ali_cont); |
---|
128 | GB_ERROR error = 0; |
---|
129 | if (old != prot) { |
---|
130 | error = GB_write_security_delete(ali_cont, prot); |
---|
131 | if (!error) |
---|
132 | error = GB_write_security_write(ali_cont, prot); |
---|
133 | } |
---|
134 | if (error) aw_message(error); |
---|
135 | } |
---|
136 | free(treename); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | enum ExportTreeType { |
---|
141 | AD_TREE_EXPORT_FORMAT_NEWICK, |
---|
142 | AD_TREE_EXPORT_FORMAT_XML, |
---|
143 | AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY, |
---|
144 | }; |
---|
145 | |
---|
146 | enum ExportNodeType { |
---|
147 | AD_TREE_EXPORT_NODE_SPECIES_NAME, |
---|
148 | AD_TREE_EXPORT_NODE_NDS |
---|
149 | }; |
---|
150 | |
---|
151 | static void update_filter_cb(AW_root *root) { |
---|
152 | const char *filter_type = 0; |
---|
153 | |
---|
154 | switch (ExportTreeType(root->awar(AWAR_TREE_EXPORT_FORMAT)->read_int())) { |
---|
155 | case AD_TREE_EXPORT_FORMAT_XML: filter_type = "xml"; break; |
---|
156 | case AD_TREE_EXPORT_FORMAT_NEWICK: |
---|
157 | case AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY: |
---|
158 | switch (ExportNodeType(root->awar(AWAR_TREE_EXPORT_NDS)->read_int())) { |
---|
159 | case AD_TREE_EXPORT_NODE_SPECIES_NAME: filter_type = "tree"; break; |
---|
160 | case AD_TREE_EXPORT_NODE_NDS: filter_type = "ntree"; break; |
---|
161 | default: nt_assert(0); break; |
---|
162 | } |
---|
163 | break; |
---|
164 | default: nt_assert(0); break; |
---|
165 | } |
---|
166 | |
---|
167 | nt_assert(filter_type); |
---|
168 | root->awar(AWAR_TREE_EXPORT_FILTER)->write_string(filter_type); |
---|
169 | } |
---|
170 | |
---|
171 | void create_trees_var(AW_root *aw_root, AW_default aw_def) { |
---|
172 | AW_awar *awar_tree_name = aw_root->awar_string(AWAR_TREE_NAME, 0, aw_def)->set_srt(GBT_TREE_AWAR_SRT); |
---|
173 | |
---|
174 | TreeAdmin::create_awars(aw_root, aw_def, true); |
---|
175 | |
---|
176 | aw_root->awar_int (AWAR_TREE_SECURITY, 0, aw_def); |
---|
177 | aw_root->awar_string(AWAR_TREE_REM, 0, aw_def); |
---|
178 | |
---|
179 | AW_create_fileselection_awars(aw_root, AWAR_TREE_EXPORT_FILEBASE, "", ".tree", "treefile"); |
---|
180 | aw_root->awar_int(AWAR_TREE_EXPORT_FORMAT, AD_TREE_EXPORT_FORMAT_NEWICK, aw_def)-> add_callback(update_filter_cb); |
---|
181 | aw_root->awar_int(AWAR_TREE_EXPORT_NDS, AD_TREE_EXPORT_NODE_SPECIES_NAME, aw_def)-> add_callback(update_filter_cb); |
---|
182 | |
---|
183 | aw_root->awar_int(AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS, 0, aw_def); |
---|
184 | aw_root->awar_int(AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS, 1, aw_def); |
---|
185 | aw_root->awar_int(AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS, 0, aw_def); |
---|
186 | aw_root->awar_int(AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES, 1, aw_def); |
---|
187 | aw_root->awar_int(AWAR_TREE_EXPORT_QUOTEMODE, TREE_SINGLE_QUOTES, aw_def); // old default behavior |
---|
188 | aw_root->awar_int(AWAR_TREE_EXPORT_REPLACE, 0, aw_def); // old default behavior |
---|
189 | |
---|
190 | AW_create_fileselection_awars(aw_root, AWAR_TREE_IMPORT, "", ".tree", "treefile"); |
---|
191 | |
---|
192 | aw_root->awar_string(AWAR_TREE_IMPORT "/tree_name", "tree_", aw_def)->set_srt(GBT_TREE_AWAR_SRT); |
---|
193 | |
---|
194 | aw_root->awar(AWAR_TREE_IMPORT "/file_name")->add_callback(tree_import_callback); |
---|
195 | awar_tree_name->add_callback(tree_vars_callback); |
---|
196 | awar_tree_name->map(AWAR_TREE); |
---|
197 | aw_root->awar(AWAR_TREE_SECURITY)->add_callback(ad_tree_set_security); |
---|
198 | |
---|
199 | aw_root->awar_int(AWAR_NODE_INFO_ONLY_MARKED, 0, aw_def); |
---|
200 | |
---|
201 | aw_root->awar_string(AWAR_TREE_CONSENSE_TREE, "tree_consensus", aw_def)->set_srt(GBT_TREE_AWAR_SRT); |
---|
202 | AW_awar *ctree_awar = aw_root->awar_string(AWAR_TREE_CONSENSE_SELECTED, "", aw_def); |
---|
203 | AWT_registerTreeAwarSimple(ctree_awar); |
---|
204 | |
---|
205 | update_filter_cb(aw_root); |
---|
206 | tree_vars_callback(aw_root); |
---|
207 | } |
---|
208 | |
---|
209 | static void tree_save_cb(AW_window *aww) { |
---|
210 | AW_root *aw_root = aww->get_root(); |
---|
211 | char *tree_name = aw_root->awar(AWAR_TREE_NAME)->read_string(); |
---|
212 | |
---|
213 | GB_ERROR error = 0; |
---|
214 | |
---|
215 | if (!tree_name || !strlen(tree_name)) { |
---|
216 | error = "Please select a tree first"; |
---|
217 | } |
---|
218 | else { |
---|
219 | char *fname = aw_root->awar(AWAR_TREE_EXPORT_NAME)->read_string(); |
---|
220 | char *db_name = aw_root->awar(AWAR_DB_NAME)->read_string(); |
---|
221 | |
---|
222 | bool use_NDS = ExportNodeType(aw_root->awar(AWAR_TREE_EXPORT_NDS)->read_int()) == AD_TREE_EXPORT_NODE_NDS; |
---|
223 | ExportTreeType exportType = static_cast<ExportTreeType>(aw_root->awar(AWAR_TREE_EXPORT_FORMAT)->read_int()); |
---|
224 | TREE_node_text_gen *node_gen = use_NDS ? new TREE_node_text_gen(make_node_text_init, make_node_text_nds) : 0; |
---|
225 | |
---|
226 | switch (exportType) { |
---|
227 | case AD_TREE_EXPORT_FORMAT_XML: |
---|
228 | error = TREE_write_XML(GLOBAL.gb_main, db_name, tree_name, node_gen, |
---|
229 | aw_root->awar(AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS)->read_int(), |
---|
230 | fname); |
---|
231 | break; |
---|
232 | |
---|
233 | case AD_TREE_EXPORT_FORMAT_NEWICK: |
---|
234 | case AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY: |
---|
235 | TREE_node_quoting quoteMode = TREE_node_quoting(aw_root->awar(AWAR_TREE_EXPORT_QUOTEMODE)->read_int()); |
---|
236 | if (aw_root->awar(AWAR_TREE_EXPORT_REPLACE)->read_int()) { |
---|
237 | quoteMode = TREE_node_quoting(quoteMode|TREE_FORCE_REPLACE); |
---|
238 | } |
---|
239 | |
---|
240 | error = TREE_write_Newick(GLOBAL.gb_main, tree_name, node_gen, |
---|
241 | aw_root->awar(AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS)->read_int(), |
---|
242 | aw_root->awar(AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS)->read_int(), |
---|
243 | aw_root->awar(AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES)->read_int(), |
---|
244 | exportType == AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY, |
---|
245 | quoteMode, |
---|
246 | fname); |
---|
247 | break; |
---|
248 | } |
---|
249 | |
---|
250 | AW_refresh_fileselection(aw_root, AWAR_TREE_EXPORT_FILEBASE); |
---|
251 | |
---|
252 | delete node_gen; |
---|
253 | free(db_name); |
---|
254 | free(fname); |
---|
255 | } |
---|
256 | |
---|
257 | aww->hide_or_notify(error); |
---|
258 | free(tree_name); |
---|
259 | } |
---|
260 | |
---|
261 | static AW_window *create_tree_export_window(AW_root *root) |
---|
262 | { |
---|
263 | AW_window_simple *aws = new AW_window_simple; |
---|
264 | aws->init(root, "SAVE_TREE", "TREE SAVE"); |
---|
265 | aws->load_xfig("sel_box_user2.fig"); |
---|
266 | |
---|
267 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
268 | aws->at("close"); |
---|
269 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
270 | |
---|
271 | aws->at("help"); |
---|
272 | aws->callback(makeHelpCallback("tr_export.hlp")); |
---|
273 | aws->create_button("HELP", "HELP", "H"); |
---|
274 | |
---|
275 | aws->at("user"); |
---|
276 | aws->create_option_menu(AWAR_TREE_EXPORT_FORMAT, 0, 0); |
---|
277 | aws->insert_option("NEWICK TREE FORMAT", "N", AD_TREE_EXPORT_FORMAT_NEWICK); |
---|
278 | aws->insert_option("NEWICK TREE FORMAT (pretty, but big)", "P", AD_TREE_EXPORT_FORMAT_NEWICK_PRETTY); |
---|
279 | aws->insert_option("ARB_XML TREE FORMAT", "X", AD_TREE_EXPORT_FORMAT_XML); |
---|
280 | aws->update_option_menu(); |
---|
281 | |
---|
282 | AW_create_standard_fileselection(aws, AWAR_TREE_EXPORT_FILEBASE); |
---|
283 | |
---|
284 | aws->at("user2"); |
---|
285 | aws->auto_space(10, 10); |
---|
286 | aws->label("Nodetype"); |
---|
287 | aws->create_toggle_field(AWAR_TREE_EXPORT_NDS, 1); |
---|
288 | aws->insert_default_toggle("Species ID ('name')", "S", 0); |
---|
289 | aws->insert_toggle("NDS", "N", 1); |
---|
290 | aws->update_toggle_field(); |
---|
291 | |
---|
292 | aws->at_newline(); aws->label("Save branch lengths"); aws->create_toggle(AWAR_TREE_EXPORT_INCLUDE_BRANCHLENS); |
---|
293 | aws->at_newline(); aws->label("Save bootstrap values"); aws->create_toggle(AWAR_TREE_EXPORT_INCLUDE_BOOTSTRAPS); |
---|
294 | aws->at_newline(); aws->label("Save group names"); aws->create_toggle(AWAR_TREE_EXPORT_INCLUDE_GROUPNAMES); |
---|
295 | aws->at_newline(); aws->label("Hide folded groups (XML only)"); aws->create_toggle(AWAR_TREE_EXPORT_HIDE_FOLDED_GROUPS); |
---|
296 | |
---|
297 | aws->at_newline(); aws->label("Name quoting (Newick only)"); |
---|
298 | aws->create_option_menu(AWAR_TREE_EXPORT_QUOTEMODE, 0, 0); |
---|
299 | aws->insert_option("none", "n", TREE_DISALLOW_QUOTES); |
---|
300 | aws->insert_option("single", "s", TREE_SINGLE_QUOTES); |
---|
301 | aws->insert_option("double", "d", TREE_DOUBLE_QUOTES); |
---|
302 | aws->insert_option("single (forced)", "i", TREE_SINGLE_QUOTES|TREE_FORCE_QUOTES); |
---|
303 | aws->insert_option("double (forced)", "o", TREE_DOUBLE_QUOTES|TREE_FORCE_QUOTES); |
---|
304 | aws->update_option_menu(); |
---|
305 | |
---|
306 | aws->at_newline(); aws->label("Replace problem chars"); aws->create_toggle(AWAR_TREE_EXPORT_REPLACE); |
---|
307 | |
---|
308 | aws->at_newline(); |
---|
309 | aws->callback(tree_save_cb); |
---|
310 | aws->create_button("SAVE", "SAVE", "o"); |
---|
311 | |
---|
312 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
313 | aws->create_button("CANCEL", "CANCEL", "C"); |
---|
314 | |
---|
315 | aws->window_fit(); |
---|
316 | update_filter_cb(root); |
---|
317 | |
---|
318 | return aws; |
---|
319 | } |
---|
320 | |
---|
321 | static char *readXmlTree(char *fname) { |
---|
322 | // create a temp file |
---|
323 | char tempFile[] = "newickXXXXXX"; |
---|
324 | int createTempFile = mkstemp(tempFile); |
---|
325 | |
---|
326 | if (createTempFile) { |
---|
327 | GBS_strstruct *buf = GBS_stropen(strlen(fname)); |
---|
328 | |
---|
329 | // extract path from fname in order to place a copy of dtd file required to validate xml file |
---|
330 | { |
---|
331 | char *tmpFname = strdup(fname); |
---|
332 | for (char *tok = strtok(tmpFname, "/"); tok;) { |
---|
333 | char *tmp = tok; |
---|
334 | tok = strtok(0, "/"); |
---|
335 | if (tok) { |
---|
336 | GBS_strcat(buf, "/"); |
---|
337 | GBS_strcat(buf, tmp); |
---|
338 | } |
---|
339 | } |
---|
340 | free(tmpFname); |
---|
341 | } |
---|
342 | |
---|
343 | char *path = GBS_strclose(buf); |
---|
344 | |
---|
345 | // linking arb_tree.dtd file to the Path from where xml file is loaded |
---|
346 | #if defined(WARN_TODO) |
---|
347 | #warning fix hack |
---|
348 | #endif |
---|
349 | char *command = GBS_global_string_copy("ln -s %s/lib/dtd/arb_tree.dtd %s/.", GB_getenvARBHOME(), path); |
---|
350 | GB_xcmd(command, false, true); |
---|
351 | |
---|
352 | // execute xml2newick to convert xml format tree to newick format tree |
---|
353 | command = GBS_global_string_copy("xml2newick %s %s", fname, tempFile); |
---|
354 | GB_xcmd(command, false, true); |
---|
355 | |
---|
356 | free(command); |
---|
357 | free(path); |
---|
358 | |
---|
359 | // return newick format tree file |
---|
360 | return strdup(tempFile); |
---|
361 | } |
---|
362 | else { |
---|
363 | printf("Failed to create Temporary File to Parse xml file!\n"); |
---|
364 | return 0; |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | static void tree_load_cb(AW_window *aww) { |
---|
369 | GB_ERROR error = 0; |
---|
370 | AW_root *aw_root = aww->get_root(); |
---|
371 | char *tree_name = aw_root->awar(AWAR_TREE_IMPORT "/tree_name")->read_string(); |
---|
372 | |
---|
373 | { |
---|
374 | char *pcTreeFormat = aw_root->awar(AWAR_TREE_IMPORT "/filter")->read_string(); |
---|
375 | char *fname = aw_root->awar(AWAR_TREE_IMPORT "/file_name")->read_string(); |
---|
376 | char *warnings = 0; |
---|
377 | char *tree_comment = 0; |
---|
378 | |
---|
379 | GBT_TREE *tree; |
---|
380 | if (strcmp(pcTreeFormat, "xml") == 0) { |
---|
381 | char *tempFname = readXmlTree(fname); |
---|
382 | tree = TREE_load(tempFname, GBT_TREE_NodeFactory(), &tree_comment, true, &warnings); |
---|
383 | GB_unlink_or_warn(tempFname, NULL); |
---|
384 | free(tempFname); |
---|
385 | } |
---|
386 | else { |
---|
387 | tree = TREE_load(fname, GBT_TREE_NodeFactory(), &tree_comment, true, &warnings); |
---|
388 | } |
---|
389 | |
---|
390 | if (!tree) error = GB_await_error(); |
---|
391 | else { |
---|
392 | if (warnings) GBT_message(GLOBAL.gb_main, warnings); |
---|
393 | |
---|
394 | { |
---|
395 | GB_transaction ta(GLOBAL.gb_main); |
---|
396 | error = GBT_write_tree_with_remark(GLOBAL.gb_main, tree_name, tree, tree_comment); |
---|
397 | error = ta.close(error); |
---|
398 | } |
---|
399 | |
---|
400 | if (!error) aw_root->awar(AWAR_TREE)->write_string(tree_name); // show new tree |
---|
401 | |
---|
402 | delete tree; |
---|
403 | } |
---|
404 | |
---|
405 | free(warnings); |
---|
406 | free(tree_comment); |
---|
407 | free(fname); |
---|
408 | free(pcTreeFormat); |
---|
409 | } |
---|
410 | |
---|
411 | aww->hide_or_notify(error); |
---|
412 | free(tree_name); |
---|
413 | } |
---|
414 | |
---|
415 | static AW_window *create_tree_import_window(AW_root *root) |
---|
416 | { |
---|
417 | AW_window_simple *aws = new AW_window_simple; |
---|
418 | aws->init(root, "LOAD_TREE", "TREE LOAD"); |
---|
419 | aws->load_xfig("sel_box_tree.fig"); |
---|
420 | |
---|
421 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
422 | aws->at("close"); |
---|
423 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
424 | |
---|
425 | aws->at("format"); |
---|
426 | aws->label("Tree Format"); |
---|
427 | aws->create_option_menu(AWAR_TREE_IMPORT "/filter"); |
---|
428 | aws->insert_default_option("Newick", "t", "tree"); |
---|
429 | aws->insert_option("XML", "x", "xml"); |
---|
430 | aws->update_option_menu(); |
---|
431 | |
---|
432 | aws->at("user"); |
---|
433 | aws->label("Tree name"); |
---|
434 | aws->create_input_field(AWAR_TREE_IMPORT "/tree_name", 15); |
---|
435 | |
---|
436 | AW_create_standard_fileselection(aws, AWAR_TREE_IMPORT); |
---|
437 | |
---|
438 | aws->at("save2"); |
---|
439 | aws->callback(tree_load_cb); |
---|
440 | aws->create_button("LOAD", "LOAD", "o"); |
---|
441 | |
---|
442 | aws->callback((AW_CB0)AW_POPDOWN); |
---|
443 | aws->at("cancel2"); |
---|
444 | aws->create_button("CANCEL", "CANCEL", "C"); |
---|
445 | |
---|
446 | aws->window_fit(); |
---|
447 | |
---|
448 | return (AW_window *)aws; |
---|
449 | } |
---|
450 | |
---|
451 | static void ad_move_tree_info(AW_window *aww, TreeInfoMode mode) { |
---|
452 | bool nodes_with_marked_only = false; |
---|
453 | |
---|
454 | char *log_file = 0; |
---|
455 | GB_ERROR error = 0; |
---|
456 | |
---|
457 | if (mode == TREE_INFO_COPY || mode == TREE_INFO_ADD) { |
---|
458 | // move or add node-info writes a log file (containing errors) |
---|
459 | // compare_node_info only sets remark branches |
---|
460 | char *log_name = GB_unique_filename("arb_node", "log"); |
---|
461 | log_file = GB_create_tempfile(log_name); |
---|
462 | if (!log_file) error = GB_await_error(); |
---|
463 | free(log_name); |
---|
464 | |
---|
465 | nodes_with_marked_only = aww->get_root()->awar(AWAR_NODE_INFO_ONLY_MARKED)->read_int(); |
---|
466 | } |
---|
467 | |
---|
468 | if (!error) { |
---|
469 | AW_root *awr = aww->get_root(); |
---|
470 | char *src_tree = TreeAdmin::source_tree_awar(awr)->read_string(); |
---|
471 | char *dst_tree = TreeAdmin::dest_tree_awar(awr)->read_string(); |
---|
472 | |
---|
473 | error = AWT_move_info(GLOBAL.gb_main, src_tree, dst_tree, log_file, mode, nodes_with_marked_only); |
---|
474 | if (log_file) { |
---|
475 | AW_edit(log_file); |
---|
476 | GB_remove_on_exit(log_file); |
---|
477 | } |
---|
478 | |
---|
479 | free(dst_tree); |
---|
480 | free(src_tree); |
---|
481 | } |
---|
482 | |
---|
483 | if (error) aw_message(error); |
---|
484 | else aww->hide(); |
---|
485 | |
---|
486 | free(log_file); |
---|
487 | } |
---|
488 | |
---|
489 | static void swap_source_dest_cb(AW_window *aww) { |
---|
490 | AW_root *root = aww->get_root(); |
---|
491 | |
---|
492 | AW_awar *s = TreeAdmin::source_tree_awar(root); |
---|
493 | AW_awar *d = TreeAdmin::dest_tree_awar(root); |
---|
494 | |
---|
495 | char *old_src = s->read_string(); |
---|
496 | s->write_string(d->read_char_pntr()); |
---|
497 | d->write_string(old_src); |
---|
498 | free(old_src); |
---|
499 | } |
---|
500 | |
---|
501 | static void copy_tree_awar_cb(UNFIXED, AW_awar *aw_source, AW_awar *aw_dest) { |
---|
502 | const char *tree = aw_source->read_char_pntr(); |
---|
503 | if (tree && tree[0]) aw_dest->write_string(tree); |
---|
504 | } |
---|
505 | |
---|
506 | static AW_window_simple *create_select_two_trees_window(AW_root *root, const char *winId, const char *winTitle, const char *helpFile) { |
---|
507 | AW_window_simple *aws = new AW_window_simple; |
---|
508 | aws->init(root, winId, winTitle); |
---|
509 | aws->load_xfig("ad_two_trees.fig"); |
---|
510 | |
---|
511 | aws->at("close"); |
---|
512 | aws->auto_space(10, 3); |
---|
513 | |
---|
514 | aws->callback(AW_POPDOWN); |
---|
515 | aws->create_button("CLOSE", "Close", "C"); |
---|
516 | |
---|
517 | aws->at("help"); |
---|
518 | aws->callback(makeHelpCallback(helpFile)); |
---|
519 | aws->create_button("HELP", "Help", "H"); |
---|
520 | |
---|
521 | aws->at("tree1"); |
---|
522 | awt_create_selection_list_on_trees(GLOBAL.gb_main, aws, TreeAdmin::source_tree_awar(root)->awar_name); |
---|
523 | aws->at("tree2"); |
---|
524 | awt_create_selection_list_on_trees(GLOBAL.gb_main, aws, TreeAdmin::dest_tree_awar(root)->awar_name); |
---|
525 | |
---|
526 | AW_awar *awar_displayed_tree = root->awar(AWAR_TREE_NAME); |
---|
527 | |
---|
528 | aws->at("select1"); |
---|
529 | aws->callback(makeWindowCallback(copy_tree_awar_cb, awar_displayed_tree, TreeAdmin::source_tree_awar(root))); aws->create_autosize_button("SELECT_DISPLAYED1", "Use"); |
---|
530 | aws->callback(makeWindowCallback(copy_tree_awar_cb, TreeAdmin::source_tree_awar(root), awar_displayed_tree)); aws->create_autosize_button("DISPLAY_SELECTED1", "Display"); |
---|
531 | |
---|
532 | aws->callback(swap_source_dest_cb); |
---|
533 | aws->create_autosize_button("SWAP", "Swap"); |
---|
534 | |
---|
535 | aws->at("select2"); |
---|
536 | aws->callback(makeWindowCallback(copy_tree_awar_cb, awar_displayed_tree, TreeAdmin::dest_tree_awar(root))); aws->create_autosize_button("SELECT_DISPLAYED2", "Use"); |
---|
537 | aws->callback(makeWindowCallback(copy_tree_awar_cb, TreeAdmin::dest_tree_awar(root), awar_displayed_tree)); aws->create_autosize_button("DISPLAY_SELECTED2", "Display"); |
---|
538 | |
---|
539 | aws->at("user"); |
---|
540 | |
---|
541 | return aws; |
---|
542 | } |
---|
543 | |
---|
544 | static AW_window_simple *create_select_other_tree_window(AW_root *root, const char *winId, const char *winTitle, const char *helpFile, const char *displayed_tree_awarname) { |
---|
545 | AW_window_simple *aws = new AW_window_simple; |
---|
546 | aws->init(root, winId, winTitle); |
---|
547 | aws->load_xfig("ad_one_tree.fig"); |
---|
548 | |
---|
549 | aws->at("close"); |
---|
550 | aws->auto_space(10, 3); |
---|
551 | |
---|
552 | aws->callback(AW_POPDOWN); |
---|
553 | aws->create_button("CLOSE", "Close", "C"); |
---|
554 | |
---|
555 | aws->at("help"); |
---|
556 | aws->callback(makeHelpCallback(helpFile)); |
---|
557 | aws->create_button("HELP", "Help", "H"); |
---|
558 | |
---|
559 | AW_awar *awar_displayed_tree = root->awar(displayed_tree_awarname); |
---|
560 | |
---|
561 | aws->at("tree"); |
---|
562 | awt_create_selection_list_on_trees(GLOBAL.gb_main, aws, TreeAdmin::source_tree_awar(root)->awar_name); |
---|
563 | |
---|
564 | aws->at("select"); |
---|
565 | aws->callback(makeWindowCallback(copy_tree_awar_cb, awar_displayed_tree, TreeAdmin::source_tree_awar(root))); aws->create_autosize_button("SELECT_DISPLAYED", "Use"); |
---|
566 | aws->callback(makeWindowCallback(copy_tree_awar_cb, TreeAdmin::source_tree_awar(root), awar_displayed_tree)); aws->create_autosize_button("DISPLAY_SELECTED", "Display"); |
---|
567 | |
---|
568 | aws->at("user"); |
---|
569 | |
---|
570 | return aws; |
---|
571 | } |
---|
572 | |
---|
573 | static AW_window *create_tree_diff_window(AW_root *root) { |
---|
574 | AW_window_simple *aws = create_select_two_trees_window(root, "CMP_TOPOLOGY", "Compare tree topologies", "tree_diff.hlp"); |
---|
575 | |
---|
576 | aws->callback(makeWindowCallback(ad_move_tree_info, TREE_INFO_COMPARE)); |
---|
577 | aws->create_autosize_button("CMP_TOPOLOGY", "Compare topologies"); |
---|
578 | |
---|
579 | return aws; |
---|
580 | } |
---|
581 | |
---|
582 | static AW_window *create_tree_cmp_window(AW_root *root) { |
---|
583 | AW_window_simple *aws = create_select_two_trees_window(root, "COPY_NODE_INFO_OF_TREE", "Move tree node info", "tree_cmp.hlp"); |
---|
584 | |
---|
585 | aws->button_length(11); |
---|
586 | |
---|
587 | aws->callback(makeWindowCallback(ad_move_tree_info, TREE_INFO_COPY)); |
---|
588 | aws->create_button("COPY_INFO", "Copy info"); |
---|
589 | |
---|
590 | aws->label("copy/add only info containing marked species"); |
---|
591 | aws->create_toggle(AWAR_NODE_INFO_ONLY_MARKED); |
---|
592 | |
---|
593 | aws->at_newline(); |
---|
594 | |
---|
595 | aws->callback(makeWindowCallback(ad_move_tree_info, TREE_INFO_ADD)); |
---|
596 | aws->create_button("ADD_INFO", "Add info"); |
---|
597 | |
---|
598 | return aws; |
---|
599 | } |
---|
600 | |
---|
601 | static void reorder_trees_cb(AW_window *aww, awt_reorder_mode dest, AW_CL) { |
---|
602 | // moves the tree in the list of trees |
---|
603 | |
---|
604 | char *tree_name = aww->get_root()->awar(AWAR_TREE_NAME)->read_string(); |
---|
605 | GB_ERROR error = NULL; |
---|
606 | |
---|
607 | GB_transaction ta(GLOBAL.gb_main); |
---|
608 | GBDATA *gb_treedata = GBT_get_tree_data(GLOBAL.gb_main); |
---|
609 | GBDATA *gb_moved_tree = GB_entry(gb_treedata, tree_name); |
---|
610 | |
---|
611 | if (!gb_moved_tree) { |
---|
612 | error = "No tree selected"; |
---|
613 | } |
---|
614 | else { |
---|
615 | GBT_ORDER_MODE move_mode; |
---|
616 | GBDATA *gb_target_tree = NULL; |
---|
617 | |
---|
618 | switch (dest) { |
---|
619 | case ARM_UP: |
---|
620 | move_mode = GBT_INFRONTOF; |
---|
621 | gb_target_tree = GBT_tree_infrontof(gb_moved_tree); |
---|
622 | if (gb_target_tree) break; |
---|
623 | // fall-through (move top-tree up = move to bottom) |
---|
624 | case ARM_BOTTOM: |
---|
625 | move_mode = GBT_BEHIND; |
---|
626 | gb_target_tree = GBT_find_bottom_tree(GLOBAL.gb_main); |
---|
627 | break; |
---|
628 | |
---|
629 | case ARM_DOWN: |
---|
630 | move_mode = GBT_BEHIND; |
---|
631 | gb_target_tree = GBT_tree_behind(gb_moved_tree); |
---|
632 | if (gb_target_tree) break; |
---|
633 | // fall-through (move bottom-tree down = move to top) |
---|
634 | case ARM_TOP: |
---|
635 | move_mode = GBT_INFRONTOF; |
---|
636 | gb_target_tree = GBT_find_top_tree(GLOBAL.gb_main); |
---|
637 | break; |
---|
638 | } |
---|
639 | |
---|
640 | if (gb_target_tree && gb_target_tree != gb_moved_tree) { |
---|
641 | error = GBT_move_tree(gb_moved_tree, move_mode, gb_target_tree); |
---|
642 | } |
---|
643 | } |
---|
644 | |
---|
645 | if (error) aw_message(error); |
---|
646 | free(tree_name); |
---|
647 | } |
---|
648 | |
---|
649 | void popup_tree_admin_window(AW_window *aws) { |
---|
650 | popup_tree_admin_window(aws->get_root()); |
---|
651 | } |
---|
652 | void popup_tree_admin_window(AW_root *aw_root) { |
---|
653 | static AW_window_simple *aws = 0; |
---|
654 | if (!aws) { |
---|
655 | aws = new AW_window_simple; |
---|
656 | aws->init(aw_root, "TREE_ADMIN", "TREE ADMIN"); |
---|
657 | aws->load_xfig("ad_tree.fig"); |
---|
658 | |
---|
659 | aws->callback(AW_POPDOWN); |
---|
660 | aws->at("close"); |
---|
661 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
662 | |
---|
663 | aws->callback(makeHelpCallback("treeadm.hlp")); |
---|
664 | aws->at("help"); |
---|
665 | aws->create_button("HELP", "HELP", "H"); |
---|
666 | |
---|
667 | aws->button_length(40); |
---|
668 | |
---|
669 | aws->at("sel"); |
---|
670 | aws->create_button(0, AWAR_TREE_NAME, 0, "+"); |
---|
671 | |
---|
672 | aws->at("security"); |
---|
673 | aws->create_option_menu(AWAR_TREE_SECURITY); |
---|
674 | aws->insert_option("0", "0", 0); |
---|
675 | aws->insert_option("1", "1", 1); |
---|
676 | aws->insert_option("2", "2", 2); |
---|
677 | aws->insert_option("3", "3", 3); |
---|
678 | aws->insert_option("4", "4", 4); |
---|
679 | aws->insert_option("5", "5", 5); |
---|
680 | aws->insert_default_option("6", "6", 6); |
---|
681 | aws->update_option_menu(); |
---|
682 | |
---|
683 | aws->at("rem"); |
---|
684 | aws->create_text_field(AWAR_TREE_REM); |
---|
685 | |
---|
686 | |
---|
687 | aws->button_length(20); |
---|
688 | |
---|
689 | static TreeAdmin::Spec spec(GLOBAL.gb_main, AWAR_TREE_NAME); |
---|
690 | |
---|
691 | aws->at("delete"); |
---|
692 | aws->callback(makeWindowCallback(TreeAdmin::delete_tree_cb, &spec)); |
---|
693 | aws->create_button("DELETE", "Delete", "D"); |
---|
694 | |
---|
695 | aws->at("rename"); |
---|
696 | aws->callback(makeCreateWindowCallback(TreeAdmin::create_rename_window, &spec)); |
---|
697 | aws->create_button("RENAME", "Rename", "R"); |
---|
698 | |
---|
699 | aws->at("copy"); |
---|
700 | aws->callback(makeCreateWindowCallback(TreeAdmin::create_copy_window, &spec)); |
---|
701 | aws->create_button("COPY", "Copy", "C"); |
---|
702 | |
---|
703 | aws->at("move"); |
---|
704 | aws->callback(create_tree_cmp_window); |
---|
705 | aws->create_button("MOVE_NODE_INFO", "Move node info", "C"); |
---|
706 | |
---|
707 | aws->at("cmp"); |
---|
708 | aws->callback(create_tree_diff_window); |
---|
709 | aws->sens_mask(AWM_EXP); |
---|
710 | aws->create_button("CMP_TOPOLOGY", "Compare topology", "T"); |
---|
711 | aws->sens_mask(AWM_ALL); |
---|
712 | |
---|
713 | aws->at("export"); |
---|
714 | aws->callback(AW_POPUP, (AW_CL)create_tree_export_window, 0); |
---|
715 | aws->create_button("EXPORT", "Export", "E"); |
---|
716 | |
---|
717 | aws->at("import"); |
---|
718 | aws->callback(AW_POPUP, (AW_CL)create_tree_import_window, 0); |
---|
719 | aws->create_button("IMPORT", "Import", "I"); |
---|
720 | |
---|
721 | aws->button_length(0); |
---|
722 | |
---|
723 | aws->at("list"); |
---|
724 | awt_create_selection_list_on_trees(GLOBAL.gb_main, aws, AWAR_TREE_NAME); |
---|
725 | |
---|
726 | aws->at("sort"); |
---|
727 | awt_create_order_buttons(aws, reorder_trees_cb, 0); |
---|
728 | } |
---|
729 | |
---|
730 | aws->activate(); |
---|
731 | } |
---|
732 | |
---|
733 | // ----------------------- |
---|
734 | // consense tree |
---|
735 | |
---|
736 | |
---|
737 | static void create_consense_tree_cb(AW_window *aww, AW_CL cl_selected_trees) { |
---|
738 | AW_root *aw_root = aww->get_root(); |
---|
739 | GB_ERROR error = NULL; |
---|
740 | |
---|
741 | const char *cons_tree_name = aw_root->awar(AWAR_TREE_CONSENSE_TREE)->read_char_pntr(); |
---|
742 | if (!cons_tree_name || !cons_tree_name[0]) { |
---|
743 | error = "No name specified for the consensus tree"; |
---|
744 | } |
---|
745 | else { |
---|
746 | AW_selection *selected_trees = (AW_selection*)cl_selected_trees; |
---|
747 | |
---|
748 | StrArray tree_names; |
---|
749 | selected_trees->get_values(tree_names); |
---|
750 | |
---|
751 | if (tree_names.size()<2) { |
---|
752 | error = "Not enough trees selected (at least 2 needed)"; |
---|
753 | } |
---|
754 | else { |
---|
755 | GBDATA *gb_main = GLOBAL.gb_main; |
---|
756 | GB_transaction ta(gb_main); |
---|
757 | |
---|
758 | { |
---|
759 | arb_progress progress("Building consensus tree", 2); // 2 steps: deconstruct, reconstruct |
---|
760 | ConsensusTreeBuilder tree_builder; |
---|
761 | |
---|
762 | progress.subtitle("loading input trees"); |
---|
763 | for (size_t t = 0; t<tree_names.size() && !error; ++t) { |
---|
764 | TreeRoot *root = new TreeRoot(new SizeAwareNodeFactory, true); // will be deleted when tree gets deleted |
---|
765 | SizeAwareTree *tree = DOWNCAST(SizeAwareTree*, GBT_read_tree(gb_main, tree_names[t], *root)); |
---|
766 | if (!tree) { |
---|
767 | error = GB_await_error(); |
---|
768 | } |
---|
769 | else { |
---|
770 | tree_builder.add(tree, tree_names[t], 1.0); |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | size_t species_count; |
---|
775 | GBT_TREE *cons_tree = tree_builder.get(species_count, error); // triggers 2 implicit progress increments |
---|
776 | |
---|
777 | if (!error && progress.aborted()) { |
---|
778 | error = "user abort"; |
---|
779 | } |
---|
780 | |
---|
781 | arb_assert(contradicted(cons_tree, error)); |
---|
782 | if (cons_tree) { |
---|
783 | char *comment = tree_builder.get_remark(); |
---|
784 | error = GBT_write_tree_with_remark(gb_main, cons_tree_name, cons_tree, comment); |
---|
785 | free(comment); |
---|
786 | delete cons_tree; |
---|
787 | } |
---|
788 | if (error) progress.done(); |
---|
789 | } |
---|
790 | error = ta.close(error); |
---|
791 | } |
---|
792 | } |
---|
793 | |
---|
794 | if (!error) { |
---|
795 | aw_root->awar(AWAR_TREE_NAME)->write_string(cons_tree_name); // show in main window |
---|
796 | } |
---|
797 | |
---|
798 | aw_message_if(error); |
---|
799 | } |
---|
800 | |
---|
801 | static void use_selected_as_target_cb(AW_window *aww) { |
---|
802 | AW_root *aw_root = aww->get_root(); |
---|
803 | aw_root->awar(AWAR_TREE_CONSENSE_TREE)->write_string(aw_root->awar(AWAR_TREE_CONSENSE_SELECTED)->read_char_pntr()); |
---|
804 | } |
---|
805 | |
---|
806 | AW_window *NT_create_consense_window(AW_root *aw_root) { |
---|
807 | static AW_window_simple *aws = 0; |
---|
808 | if (!aws) { |
---|
809 | aws = new AW_window_simple; |
---|
810 | aws->init(aw_root, "CONSENSE_TREE", "Consensus Tree"); |
---|
811 | aws->load_xfig("ad_cons_tree.fig"); |
---|
812 | |
---|
813 | aws->auto_space(10, 10); |
---|
814 | |
---|
815 | aws->callback(AW_POPDOWN); |
---|
816 | aws->at("close"); |
---|
817 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
818 | |
---|
819 | aws->callback(makeHelpCallback("consense_tree.hlp")); |
---|
820 | aws->at("help"); |
---|
821 | aws->create_button("HELP", "HELP", "H"); |
---|
822 | |
---|
823 | aws->at("list"); |
---|
824 | AW_DB_selection *all_trees = awt_create_selection_list_on_trees(GLOBAL.gb_main, aws, AWAR_TREE_CONSENSE_SELECTED); |
---|
825 | AW_selection *selected_trees = awt_create_subset_selection_list(aws, all_trees->get_sellist(), "selected", "add", "sort"); |
---|
826 | |
---|
827 | aws->at("name"); |
---|
828 | aws->create_input_field(AWAR_TREE_CONSENSE_TREE); |
---|
829 | |
---|
830 | aws->callback(use_selected_as_target_cb); |
---|
831 | aws->create_button("USE_AS_TARGET", "#moveLeft.xpm", 0); |
---|
832 | |
---|
833 | aws->at("build"); |
---|
834 | aws->callback(create_consense_tree_cb, AW_CL(selected_trees)); |
---|
835 | aws->create_autosize_button("BUILD", "Build consensus tree", "B"); |
---|
836 | } |
---|
837 | return aws; |
---|
838 | } |
---|
839 | |
---|
840 | |
---|
841 | class SortByTopo { |
---|
842 | typedef std::map<std::string, unsigned> SpeciesPosition; |
---|
843 | |
---|
844 | SpeciesPosition spos; |
---|
845 | unsigned maxSpec; |
---|
846 | |
---|
847 | unsigned storeSpeciesPosition(const GBT_TREE *node, unsigned specLeftOf) { |
---|
848 | if (node->is_leaf) { |
---|
849 | spos[node->name] = specLeftOf; |
---|
850 | return 1; |
---|
851 | } |
---|
852 | unsigned leftsize = storeSpeciesPosition(node->get_leftson(), specLeftOf); |
---|
853 | return storeSpeciesPosition(node->get_rightson(), specLeftOf+leftsize) + leftsize; |
---|
854 | } |
---|
855 | |
---|
856 | public: |
---|
857 | |
---|
858 | SortByTopo(const GBT_TREE *by) { |
---|
859 | maxSpec = storeSpeciesPosition(by, 0); |
---|
860 | } |
---|
861 | |
---|
862 | double relativePos(const char *name) { |
---|
863 | SpeciesPosition::iterator found = spos.find(name); |
---|
864 | double res = (found == spos.end()) ? 0.5 : (found->second/double(maxSpec-1)); |
---|
865 | arb_assert(res>=0.0 && res<=1.0); |
---|
866 | return res; |
---|
867 | } |
---|
868 | |
---|
869 | double reorder_subtree(RootedTree *node) { // similar to ../SL/ROOTED_TREE/RootedTree.cxx@reorder_subtree |
---|
870 | static const char *smallest_leafname; // has to be set to the alphabetically smallest name (when function exits) |
---|
871 | |
---|
872 | if (node->is_leaf) { |
---|
873 | smallest_leafname = node->name; |
---|
874 | return relativePos(smallest_leafname); |
---|
875 | } |
---|
876 | |
---|
877 | double leftRelSum = reorder_subtree(node->get_leftson()); |
---|
878 | const char *smallest_leafname_left = smallest_leafname; |
---|
879 | double rightRelSum = reorder_subtree(node->get_rightson()); |
---|
880 | const char *smallest_leafname_right = smallest_leafname; |
---|
881 | |
---|
882 | bool left_leafname_bigger = strcmp(smallest_leafname_left, smallest_leafname_right)>0; |
---|
883 | { |
---|
884 | double leftRelPos = leftRelSum / node->get_leftson() ->get_leaf_count(); |
---|
885 | double rightRelPos = rightRelSum / node->get_rightson()->get_leaf_count(); |
---|
886 | |
---|
887 | if (leftRelPos>=rightRelPos) { |
---|
888 | if (leftRelPos>rightRelPos || left_leafname_bigger) { // equal -> use leafname |
---|
889 | node->swap_sons(); |
---|
890 | } |
---|
891 | } |
---|
892 | } |
---|
893 | |
---|
894 | smallest_leafname = left_leafname_bigger ? smallest_leafname_right : smallest_leafname_left; |
---|
895 | |
---|
896 | return leftRelSum+rightRelSum; |
---|
897 | } |
---|
898 | }; |
---|
899 | |
---|
900 | static GB_ERROR sort_tree_by_other_tree(GBDATA *gb_main, RootedTree *tree, const char *other_tree) { |
---|
901 | GB_ERROR error = NULL; |
---|
902 | GB_transaction ta(gb_main); |
---|
903 | |
---|
904 | GBT_TREE *otherTree = GBT_read_tree(gb_main, other_tree, GBT_TREE_NodeFactory()); |
---|
905 | if (!otherTree) error = GB_await_error(); |
---|
906 | else { |
---|
907 | SortByTopo sorter(otherTree); |
---|
908 | delete otherTree; |
---|
909 | sorter.reorder_subtree(tree); |
---|
910 | } |
---|
911 | return error; |
---|
912 | } |
---|
913 | |
---|
914 | static bool sort_dtree_by_other_tree_cb(RootedTree *tree, GB_ERROR& error) { |
---|
915 | const char *other_tree = TreeAdmin::source_tree_awar(AW_root::SINGLETON)->read_char_pntr(); |
---|
916 | error = sort_tree_by_other_tree(GLOBAL.gb_main, tree, other_tree); |
---|
917 | return !error; |
---|
918 | } |
---|
919 | |
---|
920 | static void sort_tree_by_other_tree_cb(UNFIXED, AWT_canvas *ntw) { |
---|
921 | GB_ERROR error = NT_with_displayed_tree_do(ntw, sort_dtree_by_other_tree_cb); |
---|
922 | aw_message_if(error); |
---|
923 | } |
---|
924 | |
---|
925 | AW_window *NT_create_sort_tree_by_other_tree_window(AW_root *aw_root, AWT_canvas *ntw) { |
---|
926 | AW_window_simple *aws = create_select_other_tree_window(aw_root, ntw->aww->local_id("SORT_BY_OTHER"), "Sort tree by other tree", "resortbyother.hlp", ntw->user_awar); |
---|
927 | |
---|
928 | aws->callback(makeWindowCallback(sort_tree_by_other_tree_cb, ntw)); |
---|
929 | aws->create_autosize_button("RESORT", "Sort according to source tree"); |
---|
930 | |
---|
931 | return aws; |
---|
932 | } |
---|
933 | |
---|
934 | // --------------------------- |
---|
935 | // multifurcate tree |
---|
936 | |
---|
937 | #define AWAR_MFURC "tree/multifurc/" |
---|
938 | #define AWAR_MFURC_CONSIDER_BOOTSTRAP AWAR_MFURC "use_bs" |
---|
939 | #define AWAR_MFURC_CONSIDER_LENGTH AWAR_MFURC "use_len" |
---|
940 | #define AWAR_MFURC_CONSIDER_TERMINALS AWAR_MFURC "terminals" |
---|
941 | #define AWAR_MFURC_LENGTH_LIMIT AWAR_MFURC "len" |
---|
942 | #define AWAR_MFURC_BOOTSTRAP_LIMIT AWAR_MFURC "bs" |
---|
943 | |
---|
944 | void NT_create_multifurcate_tree_awars(AW_root *aw_root, AW_default props) { |
---|
945 | aw_root->awar_int (AWAR_MFURC_CONSIDER_BOOTSTRAP, 0, props); |
---|
946 | aw_root->awar_int (AWAR_MFURC_CONSIDER_LENGTH, 1, props); |
---|
947 | aw_root->awar_int (AWAR_MFURC_CONSIDER_TERMINALS, 0, props); |
---|
948 | aw_root->awar_float(AWAR_MFURC_LENGTH_LIMIT, 0.1, props); |
---|
949 | aw_root->awar_float(AWAR_MFURC_BOOTSTRAP_LIMIT, 50, props); |
---|
950 | } |
---|
951 | static void multifurcation_cb(UNFIXED, AWT_canvas *ntw) { |
---|
952 | AW_root *aw_root = ntw->aww->get_root(); |
---|
953 | |
---|
954 | double below_bootstrap = 101.0; |
---|
955 | double below_length = 1000000.0; |
---|
956 | bool applyAtLeafs = aw_root->awar(AWAR_MFURC_CONSIDER_TERMINALS)->read_int(); |
---|
957 | |
---|
958 | if (aw_root->awar(AWAR_MFURC_CONSIDER_BOOTSTRAP)->read_int()) below_bootstrap = aw_root->awar(AWAR_MFURC_BOOTSTRAP_LIMIT)->read_float(); |
---|
959 | if (aw_root->awar(AWAR_MFURC_CONSIDER_LENGTH) ->read_int()) below_length = aw_root->awar(AWAR_MFURC_LENGTH_LIMIT) ->read_float(); |
---|
960 | |
---|
961 | NT_multifurcate_tree(ntw, RootedTree::multifurc_limits(below_bootstrap, below_length, applyAtLeafs)); |
---|
962 | } |
---|
963 | AW_window *NT_create_multifurcate_tree_window(AW_root *aw_root, AWT_canvas *ntw) { |
---|
964 | AW_window_simple *aws = new AW_window_simple; |
---|
965 | |
---|
966 | aws->init(aw_root, ntw->aww->local_id("multifurcate"), "Multifurcate tree"); |
---|
967 | aws->at(10, 10); |
---|
968 | aws->auto_space(10, 10); |
---|
969 | |
---|
970 | aws->callback((AW_CB0) AW_POPDOWN); |
---|
971 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
972 | |
---|
973 | aws->callback(makeHelpCallback("multifurcate.hlp")); |
---|
974 | aws->create_button("HELP", "HELP", "H"); |
---|
975 | |
---|
976 | const int LABEL_LENGTH = 46; |
---|
977 | aws->label_length(LABEL_LENGTH); |
---|
978 | |
---|
979 | aws->at_newline(); |
---|
980 | aws->label("Multifurcate branches with branchlength below"); |
---|
981 | aws->create_toggle(AWAR_MFURC_CONSIDER_LENGTH); |
---|
982 | aws->create_input_field(AWAR_MFURC_LENGTH_LIMIT, 10); |
---|
983 | |
---|
984 | aws->at_newline(); |
---|
985 | aws->label(" AND bootstrap below"); |
---|
986 | aws->create_toggle(AWAR_MFURC_CONSIDER_BOOTSTRAP); |
---|
987 | aws->create_input_field(AWAR_MFURC_BOOTSTRAP_LIMIT, 10); |
---|
988 | |
---|
989 | aws->label_length(0); |
---|
990 | aws->at_newline(); |
---|
991 | aws->label("Also apply to terminal branches"); |
---|
992 | aws->create_toggle(AWAR_MFURC_CONSIDER_TERMINALS); |
---|
993 | |
---|
994 | aws->at_newline(); |
---|
995 | aws->callback(makeWindowCallback(multifurcation_cb, ntw)); |
---|
996 | aws->create_autosize_button("MULTIFURCATE", "Multifurcate", "M"); |
---|
997 | |
---|
998 | return aws; |
---|
999 | } |
---|
1000 | |
---|
1001 | // -------------------------------------------------------------------------------- |
---|
1002 | |
---|
1003 | #ifdef UNIT_TESTS |
---|
1004 | #ifndef TEST_UNIT_H |
---|
1005 | #include <test_unit.h> |
---|
1006 | #endif |
---|
1007 | |
---|
1008 | static GB_ERROR sort_namedtree_by_other_tree(GBDATA *gb_main, const char *tree, const char *other_tree) { |
---|
1009 | GB_ERROR error = NULL; |
---|
1010 | GB_transaction ta(gb_main); |
---|
1011 | SizeAwareTree *Tree = DOWNCAST(SizeAwareTree*, GBT_read_tree(gb_main, tree, *new TreeRoot(new SizeAwareNodeFactory, true))); |
---|
1012 | if (!Tree) error = GB_await_error(); |
---|
1013 | else { |
---|
1014 | Tree->compute_tree(); |
---|
1015 | error = sort_tree_by_other_tree(gb_main, Tree, other_tree); |
---|
1016 | if (!error) error = GBT_write_tree(gb_main, tree, Tree); |
---|
1017 | } |
---|
1018 | delete Tree; |
---|
1019 | return error; |
---|
1020 | } |
---|
1021 | |
---|
1022 | void TEST_sort_tree_by_other_tree() { |
---|
1023 | GB_shell shell; |
---|
1024 | GBDATA *gb_main = GB_open("TEST_trees.arb", "rw"); |
---|
1025 | TEST_REJECT_NULL(gb_main); |
---|
1026 | |
---|
1027 | const char *topo_test = "(((((((CloTyro3:1.046,CloTyro4:0.061):0.026,CloTyro2:0.017):0.017,CloTyrob:0.009):0.274,CloInnoc:0.371):0.057,CloBifer:0.388):0.124,(((CloButy2:0.009,CloButyr:0.000):0.564,CloCarni:0.120):0.010,CloPaste:0.179):0.131):0.081,((((CorAquat:0.084,CurCitre:0.058):0.103,CorGluta:0.522):0.053,CelBiazo:0.059):0.207,CytAquat:0.711):0.081);"; |
---|
1028 | const char *topo_center = "(((CloPaste:0.179,((CloButy2:0.009,CloButyr:0.000):0.564,CloCarni:0.120):0.010):0.131,((CloInnoc:0.371,((CloTyro2:0.017,(CloTyro3:1.046,CloTyro4:0.061):0.026):0.017,CloTyrob:0.009):0.274):0.057,CloBifer:0.388):0.124):0.081,((CelBiazo:0.059,((CorAquat:0.084,CurCitre:0.058):0.103,CorGluta:0.522):0.053):0.207,CytAquat:0.711):0.081);"; |
---|
1029 | const char *topo_bottom = "((CytAquat:0.711,(CelBiazo:0.059,(CorGluta:0.522,(CorAquat:0.084,CurCitre:0.058):0.103):0.053):0.207):0.081,((CloPaste:0.179,(CloCarni:0.120,(CloButy2:0.009,CloButyr:0.000):0.564):0.010):0.131,(CloBifer:0.388,(CloInnoc:0.371,(CloTyrob:0.009,(CloTyro2:0.017,(CloTyro3:1.046,CloTyro4:0.061):0.026):0.017):0.274):0.057):0.124):0.081);"; |
---|
1030 | |
---|
1031 | TEST_EXPECT_DIFFERENT(topo_test, topo_center); |
---|
1032 | TEST_EXPECT_DIFFERENT(topo_test, topo_bottom); |
---|
1033 | TEST_EXPECT_DIFFERENT(topo_center, topo_bottom); |
---|
1034 | |
---|
1035 | // create sorted copies of tree_test |
---|
1036 | { |
---|
1037 | GB_transaction ta(gb_main); |
---|
1038 | SizeAwareTree *tree = DOWNCAST(SizeAwareTree*, GBT_read_tree(gb_main, "tree_test", *new TreeRoot(new SizeAwareNodeFactory, true))); |
---|
1039 | TEST_REJECT_NULL(tree); |
---|
1040 | TEST_EXPECT_NEWICK(nLENGTH, tree, topo_test); |
---|
1041 | |
---|
1042 | tree->reorder_tree(BIG_BRANCHES_TO_CENTER); TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_sorted_center", tree)); TEST_EXPECT_NEWICK(nLENGTH, tree, topo_center); |
---|
1043 | tree->reorder_tree(BIG_BRANCHES_TO_BOTTOM); TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_sorted_bottom", tree)); TEST_EXPECT_NEWICK(nLENGTH, tree, topo_bottom); |
---|
1044 | |
---|
1045 | // test SortByTopo |
---|
1046 | { |
---|
1047 | SortByTopo sbt(tree); |
---|
1048 | const double EPSILON = 0.0001; |
---|
1049 | |
---|
1050 | TEST_EXPECT_SIMILAR(sbt.relativePos("CytAquat"), 0.0, EPSILON); // leftmost species (in topo_bottom) |
---|
1051 | TEST_EXPECT_SIMILAR(sbt.relativePos("CloTyro4"), 1.0, EPSILON); // rightmost species |
---|
1052 | |
---|
1053 | TEST_EXPECT_SIMILAR(sbt.relativePos("CurCitre"), 0.2857, EPSILON); // (5 of 15) |
---|
1054 | TEST_EXPECT_SIMILAR(sbt.relativePos("CloButy2"), 0.5, EPSILON); // center species (8 of 15) |
---|
1055 | TEST_EXPECT_SIMILAR(sbt.relativePos("CloTyrob"), 0.7857, EPSILON); // (12 of 15) |
---|
1056 | } |
---|
1057 | |
---|
1058 | tree->reorder_tree(BIG_BRANCHES_TO_EDGE); TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_work", tree)); |
---|
1059 | |
---|
1060 | delete tree; |
---|
1061 | } |
---|
1062 | |
---|
1063 | |
---|
1064 | TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_sorted_center")); TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_center); |
---|
1065 | TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_sorted_bottom")); TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_bottom); |
---|
1066 | TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_work", "tree_test")); TEST_EXPECT_SAVED_NEWICK(nLENGTH, gb_main, "tree_work", topo_test); |
---|
1067 | |
---|
1068 | GB_close(gb_main); |
---|
1069 | } |
---|
1070 | |
---|
1071 | void TEST_move_node_info() { |
---|
1072 | GB_shell shell; |
---|
1073 | GBDATA *gb_main = GB_open("TEST_trees.arb", "r"); |
---|
1074 | |
---|
1075 | #define GROUP_TEST "(CloTyrob,(CloTyro2,(CloTyro3,CloTyro4)))" |
---|
1076 | |
---|
1077 | #define NAMED_GROUP_TEST GROUP_TEST "'test'" |
---|
1078 | #define OVERWRITTEN_GROUP_TEST GROUP_TEST "'g2 [was: test]'" |
---|
1079 | |
---|
1080 | const char *org_topo = "((CloInnoc," GROUP_TEST "),(CloBifer,((CloCarni,CurCitre),((CloPaste,(Zombie1,(CloButy2,CloButyr))),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2))))))));"; |
---|
1081 | |
---|
1082 | const char *unwanted_topo1 = "((CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)))),((CloPaste,(Zombie1,(CloButy2,CloButyr))),((CloCarni,CurCitre),(CloBifer,(CloInnoc," NAMED_GROUP_TEST ")))));"; |
---|
1083 | const char *unwanted_topo2 = "((CloButy2,CloButyr),(Zombie1,(CloPaste,((((CloInnoc," OVERWRITTEN_GROUP_TEST "),CloBifer),(CloCarni,CurCitre)),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)))))))'outer');"; |
---|
1084 | |
---|
1085 | const char *sorted_topo1 = "(((((CloInnoc," NAMED_GROUP_TEST "),CloBifer),(CloCarni,CurCitre)),(CloPaste,(Zombie1,(CloButy2,CloButyr)))),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)))));"; |
---|
1086 | const char *sorted_topo2 = "(((((((CloInnoc," OVERWRITTEN_GROUP_TEST "),CloBifer),(CloCarni,CurCitre)),(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2))))),CloPaste),Zombie1)'outer',(CloButy2,CloButyr));"; |
---|
1087 | |
---|
1088 | const char *compared_topo = "(((((((CloInnoc,(CloTyrob,(CloTyro2,(CloTyro3,CloTyro4)))),CloBifer),(CloCarni,CurCitre)'# 2')'# 2',(CytAquat,(CelBiazo,(CorGluta,(CorAquat,Zombie2)'# 1')'# 1')'# 1')'# 1')'# 1',CloPaste),Zombie1),(CloButy2,CloButyr));"; |
---|
1089 | |
---|
1090 | const char *LOG = "move_node_info.log"; |
---|
1091 | |
---|
1092 | // create copy of 'tree_removal' |
---|
1093 | { |
---|
1094 | GB_transaction ta(gb_main); |
---|
1095 | GBT_TREE *tree = GBT_read_tree(gb_main, "tree_removal", GBT_TREE_NodeFactory()); |
---|
1096 | |
---|
1097 | TEST_EXPECT_NEWICK(nSIMPLE, tree, org_topo); |
---|
1098 | TEST_EXPECT_NO_ERROR(GBT_write_tree(gb_main, "tree_removal_copy", tree)); |
---|
1099 | delete tree; |
---|
1100 | } |
---|
1101 | |
---|
1102 | // move node info |
---|
1103 | { |
---|
1104 | TEST_EXPECT_NO_ERROR(AWT_move_info(gb_main, "tree_test", "tree_removal", LOG, TREE_INFO_COPY, false)); |
---|
1105 | |
---|
1106 | TEST_EXPECT_SAVED_NEWICK__BROKEN(nSIMPLE, gb_main, "tree_removal", org_topo); // @@@ moving node info modifies topology (might be necessary to insert groups) |
---|
1107 | TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", unwanted_topo1); |
---|
1108 | |
---|
1109 | // @@@ when we have a function to set the root according to another tree (#449), |
---|
1110 | // use that function here. sorting tree after that, should again result in 'org_topo'! |
---|
1111 | |
---|
1112 | TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_removal", "tree_removal_copy")); |
---|
1113 | TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", sorted_topo1); |
---|
1114 | } |
---|
1115 | |
---|
1116 | // add node info |
---|
1117 | { |
---|
1118 | TEST_EXPECT_NO_ERROR(AWT_move_info(gb_main, "tree_tree2", "tree_removal", LOG, TREE_INFO_ADD, false)); |
---|
1119 | |
---|
1120 | TEST_EXPECT_SAVED_NEWICK__BROKEN(nSIMPLE, gb_main, "tree_removal", org_topo); // @@@ moving node info modifies topology (might be necessary to insert groups) |
---|
1121 | TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", unwanted_topo2); |
---|
1122 | |
---|
1123 | // @@@ when we have a function to set the root according to another tree (#449), |
---|
1124 | // use that function here. sorting tree after that, should again result in 'org_topo'! |
---|
1125 | |
---|
1126 | TEST_EXPECT_NO_ERROR(sort_namedtree_by_other_tree(gb_main, "tree_removal", "tree_removal_copy")); |
---|
1127 | TEST_EXPECT_SAVED_NEWICK(nGROUP, gb_main, "tree_removal", sorted_topo2); |
---|
1128 | } |
---|
1129 | |
---|
1130 | // compare node info |
---|
1131 | { |
---|
1132 | TEST_EXPECT_NO_ERROR(AWT_move_info(gb_main, "tree_test", "tree_removal", NULL, TREE_INFO_COMPARE, false)); |
---|
1133 | TEST_EXPECT_SAVED_NEWICK(nREMARK, gb_main, "tree_removal", compared_topo); |
---|
1134 | } |
---|
1135 | |
---|
1136 | GB_unlink(LOG); |
---|
1137 | GB_close(gb_main); |
---|
1138 | } |
---|
1139 | |
---|
1140 | void TEST_edges() { |
---|
1141 | GB_shell shell; |
---|
1142 | GBDATA *gb_main = GB_open("TEST_trees.arb", "rw"); |
---|
1143 | TEST_REJECT_NULL(gb_main); |
---|
1144 | |
---|
1145 | { |
---|
1146 | GB_transaction ta(gb_main); |
---|
1147 | RootedTree *tree = DOWNCAST(RootedTree*, GBT_read_tree(gb_main, "tree_test", *new TreeRoot(new SizeAwareNodeFactory, true))); |
---|
1148 | |
---|
1149 | RootedTree *left = tree->findLeafNamed("CloTyro3"); TEST_REJECT_NULL(left); |
---|
1150 | RootedTree *node = left->get_father(); TEST_REJECT_NULL(node); |
---|
1151 | RootedTree *right = node->findLeafNamed("CloTyro4"); TEST_REJECT_NULL(right); |
---|
1152 | |
---|
1153 | TEST_EXPECT(node == right->get_father()); |
---|
1154 | TEST_EXPECT(node->get_leftson() == left); |
---|
1155 | TEST_EXPECT(node->get_rightson() == right); |
---|
1156 | |
---|
1157 | RootedTree *parent = node->get_father(); TEST_REJECT_NULL(parent); |
---|
1158 | RootedTree *brother = parent->findLeafNamed("CloTyro2"); TEST_REJECT_NULL(brother); |
---|
1159 | |
---|
1160 | TEST_EXPECT(node->get_brother() == brother); |
---|
1161 | |
---|
1162 | RootedTree *grandpa = parent->get_father(); TEST_REJECT_NULL(grandpa); |
---|
1163 | |
---|
1164 | // topology: |
---|
1165 | // |
---|
1166 | // grandpa |
---|
1167 | // / |
---|
1168 | // / |
---|
1169 | // / |
---|
1170 | // parent |
---|
1171 | // /\ . |
---|
1172 | // / \ . |
---|
1173 | // / \ . |
---|
1174 | // node brother |
---|
1175 | // /\ . |
---|
1176 | // / \ . |
---|
1177 | // / \ . |
---|
1178 | // left right |
---|
1179 | |
---|
1180 | // test next() and otherNext() for inner edge 'node->parent' |
---|
1181 | { |
---|
1182 | ARB_edge nodeUp = parentEdge(node); |
---|
1183 | |
---|
1184 | TEST_EXPECT(node->is_leftson()); // if child is left son.. |
---|
1185 | TEST_EXPECT(nodeUp.next().dest() == grandpa); // .. next() continues rootwards |
---|
1186 | TEST_EXPECT(nodeUp.otherNext().dest() == brother); |
---|
1187 | |
---|
1188 | ARB_edge brotherUp = parentEdge(brother); |
---|
1189 | |
---|
1190 | TEST_EXPECT(brother->is_rightson()); // if child is right son.. |
---|
1191 | TEST_EXPECT(brotherUp.next().dest() == node); // .. next() continues with other son |
---|
1192 | TEST_EXPECT(brotherUp.otherNext().dest() == grandpa); |
---|
1193 | |
---|
1194 | ARB_edge down = nodeUp.inverse(); |
---|
1195 | |
---|
1196 | TEST_EXPECT(down.next().dest() == right); // next descends into right son |
---|
1197 | TEST_EXPECT(down.otherNext().dest() == left); |
---|
1198 | |
---|
1199 | ARB_edge toLeaf(node, left); |
---|
1200 | TEST_EXPECT(toLeaf.at_leaf()); |
---|
1201 | |
---|
1202 | // both iterators should turn around at leaf: |
---|
1203 | TEST_EXPECT(toLeaf.next().dest() == node); |
---|
1204 | TEST_EXPECT(toLeaf.otherNext().dest() == node); |
---|
1205 | |
---|
1206 | // test adjacent_distance |
---|
1207 | const double EPSILON = 0.000001; |
---|
1208 | |
---|
1209 | const double NLEN = 0.025806; |
---|
1210 | const double BLEN = 0.017316; |
---|
1211 | const double PLEN = 0.017167; |
---|
1212 | const double LLEN = 1.045690; |
---|
1213 | const double RLEN = 0.060606; |
---|
1214 | |
---|
1215 | TEST_EXPECT_SIMILAR(node->get_branchlength(), NLEN, EPSILON); |
---|
1216 | TEST_EXPECT_SIMILAR(nodeUp.length(), NLEN, EPSILON); |
---|
1217 | TEST_EXPECT_SIMILAR(down.length(), NLEN, EPSILON); |
---|
1218 | TEST_EXPECT_SIMILAR(nodeUp.length_or_adjacent_distance(), NLEN, EPSILON); |
---|
1219 | TEST_EXPECT_SIMILAR(down.length_or_adjacent_distance(), NLEN, EPSILON); |
---|
1220 | |
---|
1221 | TEST_EXPECT_SIMILAR(brother->get_branchlength(), BLEN, EPSILON); |
---|
1222 | TEST_EXPECT_SIMILAR(parent ->get_branchlength(), PLEN, EPSILON); |
---|
1223 | TEST_EXPECT_SIMILAR(nodeUp.adjacent_distance(), BLEN+PLEN, EPSILON); |
---|
1224 | |
---|
1225 | TEST_EXPECT_SIMILAR(left ->get_branchlength(), LLEN, EPSILON); |
---|
1226 | TEST_EXPECT_SIMILAR(right->get_branchlength(), RLEN, EPSILON); |
---|
1227 | TEST_EXPECT_SIMILAR(down.adjacent_distance(), LLEN+RLEN, EPSILON); |
---|
1228 | |
---|
1229 | // modify lengths |
---|
1230 | const double MOD_NLEN = 0.123456; |
---|
1231 | const double MOD_LLEN = 0.246802; |
---|
1232 | |
---|
1233 | toLeaf.set_length(MOD_LLEN); |
---|
1234 | nodeUp.set_length(MOD_NLEN); |
---|
1235 | |
---|
1236 | TEST_EXPECT_SIMILAR(toLeaf.length(), MOD_LLEN, EPSILON); |
---|
1237 | TEST_EXPECT_SIMILAR(nodeUp.length(), MOD_NLEN, EPSILON); |
---|
1238 | TEST_EXPECT_SIMILAR(down.length(), MOD_NLEN, EPSILON); |
---|
1239 | } |
---|
1240 | |
---|
1241 | delete tree; |
---|
1242 | } |
---|
1243 | |
---|
1244 | GB_close(gb_main); |
---|
1245 | } |
---|
1246 | |
---|
1247 | void TEST_toggle_bootstraps100() { |
---|
1248 | GB_shell shell; |
---|
1249 | GBDATA *gb_main = GB_open("TEST_trees.arb", "rw"); |
---|
1250 | TEST_REJECT_NULL(gb_main); |
---|
1251 | |
---|
1252 | { |
---|
1253 | GB_transaction ta(gb_main); |
---|
1254 | RootedTree *tree = DOWNCAST(RootedTree*, GBT_read_tree(gb_main, "tree_test", *new TreeRoot(new SizeAwareNodeFactory, true))); |
---|
1255 | TEST_REJECT_NULL(tree); |
---|
1256 | |
---|
1257 | const char *topo_org = "(((((((CloTyro3,CloTyro4)'40%',CloTyro2)'0%',CloTyrob)'97%',CloInnoc)'0%',CloBifer)'53%',(((CloButy2,CloButyr)'100%',CloCarni)'33%',CloPaste)'97%')'100%',((((CorAquat,CurCitre)'100%',CorGluta)'17%',CelBiazo)'40%',CytAquat)'100%');"; |
---|
1258 | const char *topo_no100 = "(((((((CloTyro3,CloTyro4)'40%',CloTyro2)'0%',CloTyrob)'97%',CloInnoc)'0%',CloBifer)'53%',(((CloButy2,CloButyr)" ",CloCarni)'33%',CloPaste)'97%')" ",((((CorAquat,CurCitre)" ",CorGluta)'17%',CelBiazo)'40%',CytAquat)" ");"; |
---|
1259 | const char *topo_rem = "(((((((CloTyro3,CloTyro4),CloTyro2),CloTyrob),CloInnoc),CloBifer),(((CloButy2,CloButyr),CloCarni),CloPaste)),((((CorAquat,CurCitre),CorGluta),CelBiazo),CytAquat));"; |
---|
1260 | |
---|
1261 | TEST_EXPECT_NEWICK(nREMARK, tree, topo_org); |
---|
1262 | |
---|
1263 | tree->toggle_bootstrap100(); |
---|
1264 | TEST_EXPECT_NEWICK(nREMARK, tree, topo_no100); |
---|
1265 | |
---|
1266 | tree->toggle_bootstrap100(); |
---|
1267 | TEST_EXPECT_NEWICK(nREMARK, tree, topo_org); |
---|
1268 | |
---|
1269 | tree->remove_bootstrap(); |
---|
1270 | TEST_EXPECT_NEWICK(nREMARK, tree, topo_rem); |
---|
1271 | |
---|
1272 | delete tree; |
---|
1273 | } |
---|
1274 | |
---|
1275 | GB_close(gb_main); |
---|
1276 | } |
---|
1277 | |
---|
1278 | void TEST_multifurcate_tree() { |
---|
1279 | GB_shell shell; |
---|
1280 | GBDATA *gb_main = GB_open("TEST_trees.arb", "rw"); |
---|
1281 | TEST_REJECT_NULL(gb_main); |
---|
1282 | |
---|
1283 | const char *topo_test = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.026,CloTyro2:0.017)'0%':0.017,CloTyrob:0.009)'97%:test':0.274,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);"; |
---|
1284 | // changes = " +0.307 -0.371 +0.064 " |
---|
1285 | const char *topo_single = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.026,CloTyro2:0.017)'0%':0.017,CloTyrob:0.009)'97%:test':0.581,CloInnoc:0.000)'0%':0.121,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);"; |
---|
1286 | const char *topo_bs_less_101_005 = "(((((((CloTyro3:1.098,CloTyro4:0.064)" ":0.000,CloTyro2:0.000)" ":0.000,CloTyrob:0.000)'97%:test':0.287,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.000,CloButyr:0.000)'100%':0.578,CloCarni:0.121)" ":0.000,CloPaste:0.181)'97%':0.132)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);"; |
---|
1287 | const char *topo_bs_less_101_005_NT = "(((((((CloTyro3:1.078,CloTyro4:0.062)" ":0.000,CloTyro2:0.018)" ":0.000,CloTyrob:0.009)'97%:test':0.282,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.570,CloCarni:0.121)" ":0.000,CloPaste:0.181)'97%':0.132)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);"; |
---|
1288 | const char *topo_bs_less_30_005 = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.027,CloTyro2:0.018)" ":0.000,CloTyrob:0.009)'97%:test':0.288,CloInnoc:0.371)'0%':0.057,CloBifer:0.388)'53%':0.124,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.103,CorGluta:0.522)'17%':0.053,CelBiazo:0.059)'40%':0.207,CytAquat:0.711)'100%':0.081);"; |
---|
1289 | const char *topo_bs_less_30 = "(((((((CloTyro3:1.046,CloTyro4:0.061)'40%':0.027,CloTyro2:0.018)" ":0.000,CloTyrob:0.009)'97%:test':0.302,CloInnoc:0.390)" ":0.000,CloBifer:0.407)'53%':0.131,(((CloButy2:0.009,CloButyr:0.000)'100%':0.564,CloCarni:0.120)'33%':0.010,CloPaste:0.179)'97%':0.131)'100%':0.081,((((CorAquat:0.084,CurCitre:0.058)'100%':0.109,CorGluta:0.554)" ":0.000,CelBiazo:0.062)'40%':0.220,CytAquat:0.711)'100%':0.081);"; |
---|
1290 | const char *topo_all = "(((((((CloTyro3:0.000,CloTyro4:0.000)" ":0.000,CloTyro2:0.000)" ":0.000,CloTyrob:0.000)'" "test':0.000,CloInnoc:0.000)" ":0.000,CloBifer:0.000)" ":0.000,(((CloButy2:0.000,CloButyr:0.000)" ":0.000,CloCarni:0.000)" ":0.000,CloPaste:0.000)" ":0.000)" ":0.000,((((CorAquat:0.000,CurCitre:0.000)" ":0.000,CorGluta:0.000)" ":0.000,CelBiazo:0.000)" ":0.000,CytAquat:0.000)" ":0.000);"; |
---|
1291 | |
---|
1292 | const double STABLE_LENGTH = 5.362750; |
---|
1293 | const double EPSILON = 0.000001; |
---|
1294 | |
---|
1295 | for (int test = 1; test<=6; ++test) { |
---|
1296 | GB_transaction ta(gb_main); |
---|
1297 | RootedTree *tree = DOWNCAST(RootedTree*, GBT_read_tree(gb_main, "tree_test", *new TreeRoot(new SizeAwareNodeFactory, true))); |
---|
1298 | |
---|
1299 | TEST_REJECT_NULL(tree); |
---|
1300 | if (test == 1) { |
---|
1301 | TEST_EXPECT_NEWICK(nALL, tree, topo_test); |
---|
1302 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON); |
---|
1303 | } |
---|
1304 | |
---|
1305 | switch (test) { |
---|
1306 | case 1: |
---|
1307 | tree->multifurcate_whole_tree(RootedTree::multifurc_limits(101, 0.05, true)); |
---|
1308 | TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_101_005); |
---|
1309 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON); |
---|
1310 | break; |
---|
1311 | case 6: |
---|
1312 | tree->multifurcate_whole_tree(RootedTree::multifurc_limits(101, 0.05, false)); |
---|
1313 | TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_101_005_NT); |
---|
1314 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON); |
---|
1315 | break; |
---|
1316 | case 2: |
---|
1317 | tree->multifurcate_whole_tree(RootedTree::multifurc_limits(30, 0.05, true)); |
---|
1318 | TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_30_005); |
---|
1319 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON); |
---|
1320 | break; |
---|
1321 | case 3: |
---|
1322 | tree->multifurcate_whole_tree(RootedTree::multifurc_limits(30, 1000, true)); |
---|
1323 | TEST_EXPECT_NEWICK(nALL, tree, topo_bs_less_30); |
---|
1324 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON); |
---|
1325 | break; |
---|
1326 | case 4: |
---|
1327 | tree->multifurcate_whole_tree(RootedTree::multifurc_limits(101, 1000, true)); // multifurcate all |
---|
1328 | TEST_EXPECT_NEWICK(nALL, tree, topo_all); |
---|
1329 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), 0.0, EPSILON); |
---|
1330 | break; |
---|
1331 | case 5: { |
---|
1332 | RootedTree *CloInnoc = tree->findLeafNamed("CloInnoc"); |
---|
1333 | TEST_REJECT_NULL(CloInnoc); |
---|
1334 | |
---|
1335 | parentEdge(CloInnoc).multifurcate(); |
---|
1336 | TEST_EXPECT_NEWICK(nALL, tree, topo_single); |
---|
1337 | |
---|
1338 | TEST_EXPECT_SIMILAR(tree->sum_child_lengths(), STABLE_LENGTH, EPSILON); |
---|
1339 | break; |
---|
1340 | } |
---|
1341 | default: |
---|
1342 | nt_assert(0); |
---|
1343 | break; |
---|
1344 | } |
---|
1345 | |
---|
1346 | delete tree; |
---|
1347 | } |
---|
1348 | |
---|
1349 | GB_close(gb_main); |
---|
1350 | } |
---|
1351 | |
---|
1352 | #endif // UNIT_TESTS |
---|
1353 | |
---|
1354 | // -------------------------------------------------------------------------------- |
---|
1355 | |
---|