Example #1
0
void to_newick(std::ostream& out, treetree::const_subtree<T> tr) {
  if (!tr.childless()) {
    out << "(";
    for (typename treetree::const_subtree<T>::const_sub_child_iterator
           it=tr.begin_sub_child(); it != tr.end_sub_child(); ++it) {
      to_newick(out, *it);
      out << (boost::next(it) == tr.end_sub_child() ? ")" : ",");
    }
  }
  to_newick(out, tr.root());
}
int test_reroot_2()
{
	const char *test_name = "test_reroot_2";

	/* A tree whose root has 3 children:
	 * (A:3,B:3,(C:2,(D:1,E:1)f:1)g:1)h; */

	struct rooted_tree tree = tree_5();	
	struct hash *map = create_label2node_map(tree.nodes_in_order);	
	struct rnode *node_f = hash_get(map, "f");
	const char *exp = "((D:1,E:1)f:0.5,(C:2,(A:3,B:3)h:1)g:0.5);";

	reroot_tree(&tree, node_f);

	const char *obt = to_newick(tree.root);
	
	if (strcmp (exp, obt) != 0) {
		printf ("%s: expected '%s', got '%s'.\n", test_name, 
				exp, obt);
		return 1;
	}

	printf ("%s: ok.\n", test_name);
	return 0;
}
Example #3
0
std::string to_newick_string(treetree::tree<T> tr, int digits) {
  std::stringstream out;
  out << std::setprecision(digits);
  to_newick(out, tr);
  out << ";";
  return out.str();
}
int test_collapse_pure_clades()
{
	char *test_name = "test_collapse_pure_clade";
	char *exp = "((A:1,B:1.0)f:2.0,C:3)i;";
	struct rooted_tree tree = tree_4();

	collapse_pure_clades(&tree);
	char *obt = to_newick(tree.root);

	if (0 != strcmp(exp, obt)) {
		printf ("%s: expected %s, got %s.\n", test_name,
				exp, obt);
		return 1;
	}

	printf ("%s: ok.\n", test_name);
	return 0;
}
int test_reroot()
{
	const char *test_name = "test_reroot";
	struct rooted_tree tree = tree_3();	/* ((A:1,B:1.0)f:2.0,(C:1,(D:1,E:1)g:2)h:3)i; */
	struct hash *map = create_label2node_map(tree.nodes_in_order);	
	struct rnode *node_g = hash_get(map, "g");
	const char *exp = "((D:1,E:1)g:1,(C:1,(A:1,B:1.0)f:5)h:1);";

	reroot_tree(&tree, node_g);

	const char *obt = to_newick(tree.root);
	
	if (strcmp (exp, obt) != 0) {
		printf ("%s: expected '%s', got '%s'.\n", test_name, 
				exp, obt);
		return 1;
	}

	printf ("%s: ok.\n", test_name);
	return 0;
}
int test_clone_subtree()
{
	const char *test_name = "test_clone_subtree";
	/* ((HRV_A1:1,HRV_A2:1.0)HRV_A:2.0,(HRV_C:1,(HRV_B1:1,HRV_B2:1)HRV_B:2):3)HRV; */
	struct rooted_tree tree = tree_8();
	struct rooted_tree *clone = clone_subtree(tree.root);

	char *orig_newick = to_newick(tree.root);
	char *clone_newick = to_newick(clone->root);

	struct list_elem *orig_el, *clone_el;
	struct rnode *orig_node, *clone_node;

	if (strcmp(orig_newick, clone_newick) != 0) {
		printf ("%s: Newick strings are different ('%s' != '%s').\n",
				test_name, orig_newick, clone_newick);
		return 1;
	}

	/* Check all nodes */
	for (orig_el = tree.nodes_in_order->head,
			clone_el = clone->nodes_in_order->head;
			NULL != orig_el;
			orig_el = orig_el->next, clone_el = clone_el->next) {
		orig_node = orig_el->data;
		clone_node = clone_el->data;
		if (orig_node == clone_node) {
			printf ("%s: nodes %p and %p are the same object.\n",
					test_name, orig_node, clone_node);
			return 1;
		}
		if (strcmp(orig_node->label, clone_node->label) != 0) {
			printf ("%s: nodes have different labels ('%s'"
					" vs '%s')\n",
				test_name, orig_node->label, clone_node->label);
			return 1;
		}
		if (NULL == orig_node->parent) {
			if (NULL != clone_node->parent) {
				printf ("%s: original's parent is NULL,"
					" but clone's is not (%p).\n",
					test_name, clone_node->parent);
				return 1;
			}
		}
		else {
			if (orig_node->parent == clone_node->parent) {
				printf ("%s: parent edges are non-NULL yet "
					"identical (%p, %p).\n", test_name,
					orig_node->parent,
					clone_node->parent);
				return 1;
			}
			if (strcmp(orig_node->edge_length_as_string,
				   clone_node->edge_length_as_string)
				!= 0) {
				printf ("%s: parent edges have different "
					"lengths (%s, %s)\n", test_name,
					orig_node->edge_length_as_string,
					clone_node->edge_length_as_string);
				return 1;
			}
		}
	}
	if (NULL != clone_el) {
		printf ("%s: clone's children list is not terminated.\n",
				test_name);
		return 1;
	}

	printf ("%s: ok.\n", test_name);
	return 0;
}
Example #7
0
void to_newick(std::ostream& out,const treetree::tree<T>& tr) {
  if (!tr.empty())
    to_newick(out, treetree::const_subtree<T>(tr));
}
Example #8
0
void to_newick(std::ostream& out, treetree::subtree<T> tr) {
  to_newick(out, treetree::const_subtree<T>(tr));
}
Example #9
0
void process_tree(struct rooted_tree *tree, struct parameters params)
{
	struct llist *descendants;

	switch (params.mode) {
	case EXACT:
		descendants = nodes_from_labels(tree, params.labels);
		if (NULL == descendants) { perror(NULL); exit(EXIT_FAILURE); }
		if (0 == descendants->count) {
			fprintf (stderr, "WARNING: no label matches.\n");
			/* I don't consider this a failure: it is just the case
			 * that the tree does not contain the specified labels.
			 * */
			exit(EXIT_SUCCESS);
		}
		break;
	case REGEXP:
		descendants = nodes_from_regexp(tree, params.regexp);
		if (NULL == descendants) { perror(NULL); exit(EXIT_FAILURE); }
		if (0 == descendants->count) {
			fprintf (stderr, "WARNING: no match for regexp /%s/\n",
					params.regexp_string);
			exit(EXIT_SUCCESS); /** see above */
		}
		break;
	default:
		fprintf (stderr, "Unknown mode %d\n", params.mode);
		exit(EXIT_FAILURE);
	}

	/* We need a copy b/c lca() modifies its arg */
	struct llist *desc_clone = shallow_copy(descendants);
	if (NULL == desc_clone) { perror(NULL); exit(EXIT_FAILURE); }
	struct rnode *subtree_root = lca(tree, desc_clone);
	if (NULL == subtree_root) { perror(NULL); exit(EXIT_FAILURE); }
	free(desc_clone); /* elems freed in lca() */

	/* Jump up tree to get context, if any was required ('context' > 0) */
	int context;
	for (context = params.context; context > 0; context--)
		if (! is_root(subtree_root))
			subtree_root = subtree_root->parent;

	// TODO: could not replace to_newick() by dump_newick() due to side
	// effects. Investigate.

	if (NULL != subtree_root) {
		if ((! params.check_monophyly) ||
		    (is_monophyletic(descendants, subtree_root))) {
			/* monophyly of input labels is verified or not
			 * requested */
			char *newick;
			if (params.siblings) {
				struct llist *sibs = siblings(subtree_root);
				if (NULL == sibs) {
					perror(NULL);
					exit(EXIT_FAILURE);
				}
				struct list_elem *el;
				for (el=sibs->head;NULL!=el;el=el->next) {
					struct rnode *sib;
					sib = el->data;
					newick = to_newick(sib);
					printf ("%s\n", newick);
					free(newick);
				}
				destroy_llist(sibs);
			} else {
				/* normal operation: print clade defined by
				 * labels. */
				newick = to_newick(subtree_root);
				printf ("%s\n", newick);
				free(newick);
			}
		}
	} else {
		fprintf (stderr, "WARNING: LCA not found\n");
	}

	destroy_llist(descendants);

}