Example #1
0
int main(int argc, char *argv[])
{
	struct rooted_tree *tree;	
	struct hash *rename_map;
	struct parameters params;
	
	params = get_params(argc, argv);

	rename_map = read_map(params.map_filename);

	while (NULL != (tree = parse_tree())) {
		process_tree(tree, rename_map, params);
		destroy_tree(tree, DONT_FREE_NODE_DATA);
	}

	struct llist *keys = hash_keys(rename_map);
	if (NULL == keys) { perror(NULL); exit(EXIT_FAILURE); }
	struct list_elem *e;
	for (e = keys->head; NULL != e; e = e->next) {
		char *key = (char *) e->data;
		char *val = hash_get(rename_map, key);
		free(val);
	}
	destroy_llist(keys);
	destroy_hash(rename_map);

	return 0;
}
Example #2
0
static struct rooted_tree * process_tree_direct(
		struct rooted_tree *tree, set_t *prune_labels)
{
	struct llist *rev_nodes = llist_reverse(tree->nodes_in_order);
	struct list_elem *el;
	struct rnode *current;
	char *label;

	for (el = rev_nodes->head; NULL != el; el = el->next) {
		current = el->data;
		label = current->label;
		/* skip this node iff parent is marked ("seen") */
		if (!is_root(current) && current->parent->seen) {
			current->seen = true;	/* inherit mark */
			continue;
		}
		if (set_has_element(prune_labels, label)) {
			unlink_rnode(current);
			current->seen = true;
		}
	}

	destroy_llist(rev_nodes);
	reset_seen(tree);
	return tree;
}
Example #3
0
void process_tree(struct rooted_tree *tree, struct parameters params)
{
	struct llist *outgroup_nodes = get_outgroup_nodes(tree, params.labels);
	if (! params.deroot) {
		/* re-root according to outgroup nodes */
		enum reroot_status result = reroot(tree, outgroup_nodes, 
				params.i_node_lbl_as_support);
		switch (result) {
		case REROOT_OK:
			dump_newick(tree->root);
			break;
		case LCA_IS_TREE_ROOT:
			if (params.try_ingroup)
				try_ingroup(tree, params);
			else {
				fprintf (stderr,
					"ERROR: Outgroup's LCA is tree's root "
					"- cannot reroot. Try -l.\n");
			}
			break;
		case NOT_PHYLOGRAM:
			fprintf (stderr, 
				"ERROR: Tree must be a phylogram, but some "
				"branch lengths are not defined - aborting.\n");
			break;
		default:
			assert(0);
		}
		destroy_all_rnodes(NULL);
		destroy_tree(tree);
	} else {
		enum deroot_status result = deroot(tree);
		switch (result) {
		case DEROOT_OK:
			dump_newick(tree->root);
			break;
		case NOT_BIFURCATING:
			fprintf (stderr,
				"ERROR: tree is already unrooted, or root"
				" has only 1 child - cannot deroot.\n");
			break;
		case BALANCED:
			fprintf (stderr,
				"ERROR: can't decide which of root's "
				"children is the outgroup.\n");
			break;
		case MEM_PROB:
			perror(NULL);
			break;
		default:
			assert(0);
		}
		destroy_all_rnodes(NULL);
		destroy_tree(tree);
	}
	destroy_llist(outgroup_nodes);
}
Example #4
0
int main(int argc, const char *argv[]) {
	int fd = open("/dev/urandom", O_RDONLY);
	size_t rd_sz = 123;
	char b;
	llist *list = init_llist(0), *lul; // FOR TEH LULZ
	while (read(fd, &b, 1) == 1 && rd_sz) {
		add_llist(list, b);
		--rd_sz;
	}
	lul = list;
	list = list->next;
	free(lul);
	destroy_llist(list);
return 0;
}
Example #5
0
int main(int argc, char *argv[])
{
	struct rooted_tree *tree;	
	struct parameters params;
	
	params = get_params(argc, argv);
	while (NULL != (tree = parse_tree())) {
		/* tree is free()d in process_tree(), as derooting is
		 * compatible with ordinary free()ing (with destroy_tree()),
		 * but rerooting is not. */
		process_tree(tree, params);
	}

	destroy_llist(params.labels);
	return 0;
}
Example #6
0
int test_destroy()
{
	char *test_name = "test_destroy";
	struct llist *list_p;

	list_p = create_llist();
	prepend_element(list_p, "one");
	prepend_element(list_p, "two");
	prepend_element(list_p, "three");
	prepend_element(list_p, "four");
	prepend_element(list_p, "five");

	destroy_llist(list_p);

	printf("%s ok.\n", test_name);
	return 0;
}
Example #7
0
void process_tree(struct rooted_tree *tree, struct parameters params)
{
	struct llist *outgroup_nodes = get_outgroup_nodes(tree, params.labels);
	if (! params.deroot) {
		/* re-root according to outgroup nodes */
		enum reroot_status result = reroot(tree, outgroup_nodes);
		switch (result) {
		case REROOT_OK:
			dump_newick(tree->root);
			break;
		case LCA_IS_TREE_ROOT:
			if (params.try_ingroup)
				try_ingroup(tree, params);
			else {
				fprintf (stderr,
					"Outgroup's LCA is tree's root "
					"- cannot reroot. Try -l.\n");
			}
			break;
		default:
			assert(0);
		}
		destroy_tree_cb(tree, NULL);
	} else {
		enum deroot_status result = deroot(tree);
		switch (result) {
		case DEROOT_OK:
			dump_newick(tree->root);
			break;
		case NOT_BIFURCATING:
			fprintf (stderr,
				"WARNING: tree is already unrooted, or root"
				" has only 1 child - cannot deroot.\n");
			break;
		case BALANCED:
			fprintf (stderr,
				"WARNING: can't decide which of root's "
				"children is the outgroup.\n");
			break;
		default:
			assert(0);
		}
		destroy_tree_cb(tree, NULL);
	}
	destroy_llist(outgroup_nodes);
}
Example #8
0
int main(int argc, char *argv[])
{
	struct rooted_tree *tree;	
	struct parameters params;
	
	params = get_params(argc, argv);
	while (NULL != (tree = parse_tree())) {
		/* tree is free()d in process_tree(), as derooting is
		 * compatible with ordinary free()ing (with
		 * destroy_tree()), but rerooting is not. */
		// TODO: why not? Can this be obsolete now that rnodes are
		// free()d at the end? 
		process_tree(tree, params);
	}

	destroy_llist(params.labels);
	return 0;
}
Example #9
0
void try_ingroup(struct rooted_tree *tree, struct parameters params)
{
	/* we will try to insert the root above the ingroup - for this we'll
	 * need all leaves that are NOT in the outgroup. */
	// TODO: why just leaves?
	struct llist *ingroup_leaves;
	ingroup_leaves = get_ingroup_leaves(tree, params.labels);
	enum reroot_status result = reroot(tree, ingroup_leaves);
	switch (result) {
		case REROOT_OK:
			dump_newick(tree->root);
			break;
		case LCA_IS_TREE_ROOT:
			fprintf (stderr, "LCA is still tree's root "
				"- be sure to include ALL outgroup "
				"leaves with -l");
			break;
	}
	destroy_llist(ingroup_leaves);
}
Example #10
0
void try_ingroup(struct rooted_tree *tree, struct parameters params)
{
	/* we will try to insert the root above the ingroup - for this we'll
	 * need all leaves that are NOT in the outgroup. We don't need the
	 * inner nodes, though, since tha leaves are sufficient for determining
	 * the ingroup's LCA. This also works if some leaf labels are empty
	 * (see test case 'nolbl_ingrp' in test_nw_reroot_args) */
	struct llist *ingroup_leaves;
	ingroup_leaves = get_ingroup_leaves(tree, params.labels);
	enum reroot_status result = reroot(tree, ingroup_leaves,
			params.i_node_lbl_as_support);
	switch (result) {
		case REROOT_OK:
			dump_newick(tree->root);
			break;
		case LCA_IS_TREE_ROOT:
			fprintf (stderr, "LCA is still tree's root "
				"- be sure to include ALL outgroup "
				"leaves with -l");
			break;
	}
	destroy_llist(ingroup_leaves);
}
Example #11
0
int main(int argc, char *argv[])
{
	struct rooted_tree *tree;	
	struct parameters params;
	
	params = get_params(argc, argv);

	while ((tree = parse_tree()) != NULL) {
		process_tree(tree, params);
		destroy_tree(tree, DONT_FREE_NODE_DATA);
	}

	if (EXACT == params.mode)
		destroy_llist(params.labels);
	else {
		/* This does not free 'params.regexp' itself, only memory pointed to by 'params.regexp'
		 * members and allocated by regcomp().*/
		regfree(params.regexp);
		/* Therefore: */
		free(params.regexp);
	}

	return 0;
}
Example #12
0
void destroy_stack(STACK *mystack)
{
	destroy_llist(mystack);
}
Example #13
0
int main(int argc, char* argv[])
{
	int i;
	node * m;

	/* obligatory */
	printf("Hello, World\r\n");
	printf("\r\n---\r\n");

	/* Node stuff */

	printf("Creating and destroying 10000 nodes\r\n");
	for (i = 0; i < 10000 ; i++)
	{
		int data = i;
		node * n = build_node(&data);
		destroy_node(n);
	}
	printf("Done\r\n");

	printf("\r\n---\r\n");

	printf("Building a queue, adding 10000 nodes. Printing out every 1000\r\n");
	queue * q = build_queue();

	for (i = 0; i < 10000 ; i++)
	{
		int data = i;
		node * n = build_node(&data);
		queue_push(q, n);
	}
	printf("Queue has the size %d\r\n", q->size);

	for (i = 0; i < 10000 ; i++)
	{
		node * n = queue_pop(q);
		if((i%1000)==0)
		{
			printf("Node has the data %d\r\n", *(int*)n->data);
		}
		destroy_node(n);
	}
	printf("Queue has the size %d\r\n", q->size);

	destroy_queue(q);
	printf("\r\n---\r\n");

	printf("Building a stack, adding 10000 nodes. Printing out every 1000\r\n");
	stack * s = build_stack();

	for (i = 0; i < 10000 ; i++)
	{
		int data = i;
		node * n = build_node(&data);
		stack_push(s, n);
	}
	printf("Stack has the size %d\r\n", s->size);

	for (i = 0; i < 10000 ; i++)
	{
		node * n = stack_pop(s);
		if((i%1000)==0)
		{
			printf("Node has the data %d\r\n", *(int*)n->data);
		}
		destroy_node(n);
	}
	printf("Stack has the size %d\r\n", s->size);

	destroy_stack(s);
	printf("\r\n---\r\n");
	
	printf("Building a linked list, adding 10000 nodes. Printing out every 1000\r\n");
	llist * l = build_llist();

	for (i = 0; i < 10000 ; i++)
	{
		int data = i;
		node * n = build_node(&data);
		llist_add(l, n);
	}
	printf("llist has the size %d\r\n", l->size);
	
	printf("Testing arbitrary access...\r\n");
	m = llist_get(l, 87);
  printf("Node has the data %d\r\n", *(int*)m->data);
  m = llist_get(l, 3487);
  printf("Node has the data %d\r\n", *(int*)m->data);
  m = llist_get(l, 287);
  printf("Node has the data %d\r\n", *(int*)m->data);
  
  printf("Testing arbitrary delete...\r\n");
  
  m = llist_get(l, 299);
  printf("Node has the data %d\r\n", *(int*)m->data);
  llist_delete(l, 299);
  m = llist_get(l, 299);
  printf("Node has the data %d\r\n", *(int*)m->data);

	printf("Testing mass delete...\r\n");
	destroy_llist(l);
	
	printf("\r\n---\r\n");
	
	printf("Creating and destroying 10000 single trees\r\n");
	for (i = 0; i < 10000 ; i++)
	{
		int data = i;
		tree * n = build_tree(&data);
		destroy_tree(n);
	}
	printf("Done\r\n");

	printf("\r\n---\r\n");
	
	printf("Testing add/delete for trees\r\n");
	int data = 2;
	tree * root = build_tree(&data);
	
	//for (i = 0; i < 3 ; i++)
//	{
//		int data = i;
//		tree * n = build_tree(&data);
//		destroy_tree(n);
//		add_leaf(&root, n, &tree_int_comp);
//	}
	
	data = 1;
	tree * leaf1 = build_tree(&data);
	add_leaf(&root, leaf1, &tree_int_comp);
	
	data = 0;
	tree * leaf2 = build_tree(&data);
	add_leaf(&root, leaf2, &tree_int_comp);
	
	printf("Root node has value %d before deletion\r\n", *(int*)root->data);
	delete_node(&(root));
	printf("Root node has value %d after deletion\r\n", *(int*)root->data);
	
	destroy_tree(leaf1);
	destroy_tree(leaf2);
	
	printf("Done\r\n");

	return 0;
}
Example #14
0
int main(int argc, char *argv[])
{
	struct rooted_tree *tree;	
	struct parameters params;
	struct h_data depths;	
	params = get_params(argc, argv);

	/* I could take the switch out of the loop, since the distance type
	 * is fixed for the process's lifetime. OTOH the code is easier to
	 * understand this way, and it's unlikely the switch has a visible
	 * impact on performance. */

	while ((tree = parse_tree()) != NULL) {
		alloc_simple_node_pos(tree);
		depths = set_node_depth_cb(tree,
				set_simple_node_pos_depth,
				get_simple_node_pos_depth);
		if (FAILURE == depths.status) {
			perror(NULL);
			exit(EXIT_FAILURE);
		}
		struct rnode *lca_node;
		struct llist *selected_nodes;

		if (ARGV_LABELS == params.selection) {
			selected_nodes = nodes_from_labels(tree,
					params.labels);
			if (NULL == selected_nodes) {
				perror(NULL);
				exit(EXIT_FAILURE);
			}
		} else {
			selected_nodes = get_selected_nodes(tree,
					params.selection);
		}
		switch (params.distance_method) {
		case FROM_ROOT:
			print_distance_list(tree->root, selected_nodes,
				params.list_orientation, params.show_header);
			break;
		case FROM_LCA:
			/* if no lbl given, use root as LCA */
			/* I don't remember why I did it like that, and I
			 * don't see what it is good for, so I discard it. */
			/*
			if (0 == params.labels->count)  {
				lca_node = tree->root;
			}
			else {
				lca_node = lca_from_nodes(tree, selected_nodes);
			}
			*/
			lca_node = lca_from_nodes(tree, selected_nodes);
			if (NULL == lca_node) {
				perror(NULL);
				exit(EXIT_FAILURE);
			}
			print_distance_list(lca_node, selected_nodes,
				params.list_orientation, params.show_header);
			break;
		case MATRIX:
			switch (params.matrix_shape) {
			case SQUARE:
				print_square_distance_matrix(tree,
					selected_nodes, params.show_header);
				break;
			case TRIANGLE:
				print_triangular_distance_matrix(tree,
					selected_nodes, params.show_header);
				break;
			default:
				fprintf(stderr, "ERROR: unknown matrix form %d\n", params.matrix_shape);
				exit(EXIT_FAILURE);
			}
			break;
		case FROM_PARENT:
			print_distance_list(NULL, selected_nodes,
				params.list_orientation, params.show_header);
			break;
		default:
			fprintf (stderr,
				"ERROR: invalid distance type '%d'.\n",
				params.distance_method);
			exit(EXIT_FAILURE);
		}

		destroy_llist(selected_nodes);
		destroy_all_rnodes(NULL);
		destroy_tree(tree);
	}

	destroy_llist(params.labels);

	return 0;
}
Example #15
0
void destroy_queue(QUEUE *myqueue){
	destroy_llist(myqueue);
}
Example #16
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);

}