Skip to content

Remove tree.left_root #1862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions c/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
(:user:`benjeffery`, :issue:`1723`, :pr:`1727`)

- The previously deprecated option ``TSK_SAMPLE_COUNTS`` has been removed. (:user:`benjeffery`, :issue:`1744`, :pr:`1761`).

- FIXME breaking changes for tree API and virtual root

- Individuals are no longer guaranteed or required to be topologically sorted in a tree sequence.
- Individuals are no longer guaranteed or required to be topologically sorted in a tree sequence.
``tsk_table_collection_sort`` no longer sorts individuals.
(:user:`benjeffery`, :issue:`1774`, :pr:`1789`)

- FIXME breaking changes for tree API and virtual root.

- The ``tsk_tree_t.left_root`` member has been removed. Client code can be updated
most easily by using the equivalent ``tsk_tree_get_left_root`` function. However,
it may be worth considering updating code to use either the standard traversal
functions (which automatically iterate over roots) or to use the ``virtual_root``
member (which may lead to more concise code). (:user:`jeromekelleher`, :issue:`1796`,
:pr:`1862`)


**Features**

- Add ``tsk_table_collection_individual_topological_sort`` to sort the individuals as this is no longer done by the
Expand Down
33 changes: 10 additions & 23 deletions c/dev-tools/dev-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,8 @@ print_ld_matrix(tsk_treeseq_t *ts)
if (ret != 0) {
fatal_library_error(ret, "get_site");
}
printf("%d\t%f\t%d\t%f\t%.3f\n",
(int) sA.id,
sA.position,
(int) sB.id,
sB.position,
r2[k]);
printf("%d\t%f\t%d\t%f\t%.3f\n", (int) sA.id, sA.position, (int) sB.id,
sB.position, r2[k]);
}
}
free(r2);
Expand Down Expand Up @@ -156,8 +152,8 @@ print_newick_trees(tsk_treeseq_t *ts)
fatal_error("ERROR: %d: %s\n", ret, tsk_strerror(ret));
}
for (ret = tsk_tree_first(&tree); ret == 1; ret = tsk_tree_next(&tree)) {
ret = tsk_convert_newick(
&tree, tree.left_root, precision, 0, newick_buffer_size, newick);
ret = tsk_convert_newick(&tree, tsk_tree_get_left_root(&tree), precision, 0,
newick_buffer_size, newick);
if (ret != 0) {
fatal_library_error(ret, "newick");
}
Expand Down Expand Up @@ -187,9 +183,7 @@ print_tree_sequence(tsk_treeseq_t *ts, int verbose)
}
for (ret = tsk_tree_first(&tree); ret == 1; ret = tsk_tree_next(&tree)) {
printf("-------------------------\n");
printf("New tree: %d: %f (%d)\n",
(int) tree.index,
tree.right - tree.left,
printf("New tree: %d: %f (%d)\n", (int) tree.index, tree.right - tree.left,
(int) tree.num_nodes);
printf("-------------------------\n");
tsk_tree_print_state(&tree, stdout);
Expand Down Expand Up @@ -242,11 +236,8 @@ run_newick(const char *filename, int TSK_UNUSED(verbose))
}

static void
run_simplify(const char *input_filename,
const char *output_filename,
size_t num_samples,
bool filter_sites,
int verbose)
run_simplify(const char *input_filename, const char *output_filename, size_t num_samples,
bool filter_sites, int verbose)
{
tsk_treeseq_t ts, subset;
const tsk_id_t *samples;
Expand Down Expand Up @@ -290,9 +281,7 @@ main(int argc, char **argv)
/* SYNTAX 1: simplify [-vi] [-s] <input-file> <output-file> */
struct arg_rex *cmd1 = arg_rex1(NULL, NULL, "simplify", NULL, REG_ICASE, NULL);
struct arg_lit *verbose1 = arg_lit0("v", "verbose", NULL);
struct arg_int *num_samples1 = arg_int0("s",
"sample-size",
"<sample-size>",
struct arg_int *num_samples1 = arg_int0("s", "sample-size", "<sample-size>",
"Number of samples to keep in the simplified tree sequence.");
struct arg_lit *filter_sites1
= arg_lit0("i", "filter-invariant-sites", "<filter-invariant-sites>");
Expand Down Expand Up @@ -348,10 +337,8 @@ main(int argc, char **argv)
nerrors5 = arg_parse(argc, argv, argtable5);

if (nerrors1 == 0) {
run_simplify(infiles1->filename[0],
outfiles1->filename[0],
(size_t) num_samples1->ival[0],
(bool) filter_sites1->count,
run_simplify(infiles1->filename[0], outfiles1->filename[0],
(size_t) num_samples1->ival[0], (bool) filter_sites1->count,
verbose1->count);
} else if (nerrors2 == 0) {
run_ld(infiles2->filename[0], verbose2->count);
Expand Down
63 changes: 43 additions & 20 deletions c/examples/tree_traversal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@
errx(EXIT_FAILURE, "line %d: %s", __LINE__, tsk_strerror(val)); \
}


static void
traverse_standard(tsk_tree_t *tree)
{
int ret;
tsk_size_t num_nodes, j;
tsk_id_t *nodes = malloc(tsk_tree_get_size_bound(tree) * sizeof(*nodes));

if (nodes == NULL) {
errx(EXIT_FAILURE, "Out of memory");
}

ret = tsk_tree_preorder(tree, -1, nodes, &num_nodes);
check_tsk_error(ret);
for (j = 0; j < num_nodes; j++) {
printf("Visit preorder %lld\n", (long long) nodes[j]);
}

ret = tsk_tree_postorder(tree, -1, nodes, &num_nodes);
check_tsk_error(ret);
for (j = 0; j < num_nodes; j++) {
printf("Visit postorder %lld\n", (long long) nodes[j]);
}

free(nodes);
}


static void
_traverse(tsk_tree_t *tree, tsk_id_t u, int depth)
{
Expand All @@ -27,36 +55,29 @@ _traverse(tsk_tree_t *tree, tsk_id_t u, int depth)
static void
traverse_recursive(tsk_tree_t *tree)
{
tsk_id_t root;

for (root = tree->left_root; root != TSK_NULL; root = tree->right_sib[root]) {
_traverse(tree, root, 0);
}
_traverse(tree, tree->virtual_root, -1);
}

static void
traverse_stack(tsk_tree_t *tree)
{
int stack_top;
tsk_id_t u, v, root;
tsk_id_t *stack
= malloc(tsk_treeseq_get_num_nodes(tree->tree_sequence) * sizeof(*stack));
tsk_id_t u, v;
tsk_id_t *stack = malloc(tsk_tree_get_size_bound(tree) * sizeof(*stack));

if (stack == NULL) {
errx(EXIT_FAILURE, "Out of memory");
}
for (root = tree->left_root; root != TSK_NULL; root = tree->right_sib[root]) {
stack_top = 0;
stack[stack_top] = root;
while (stack_top >= 0) {
u = stack[stack_top];
stack_top--;
printf("Visit stack %lld\n", (long long) u);
/* Put nodes on the stack right-to-left, so we visit in left-to-right */
for (v = tree->right_child[u]; v != TSK_NULL; v = tree->left_sib[v]) {
stack_top++;
stack[stack_top] = v;
}
stack_top = 0;
stack[stack_top] = tree->virtual_root;
while (stack_top >= 0) {
u = stack[stack_top];
stack_top--;
printf("Visit stack %lld\n", (long long) u);
/* Put nodes on the stack right-to-left, so we visit in left-to-right */
for (v = tree->right_child[u]; v != TSK_NULL; v = tree->left_sib[v]) {
stack_top++;
stack[stack_top] = v;
}
}
free(stack);
Expand Down Expand Up @@ -96,6 +117,8 @@ main(int argc, char **argv)
ret = tsk_tree_first(&tree);
check_tsk_error(ret);

traverse_standard(&tree);

traverse_recursive(&tree);

traverse_stack(&tree);
Expand Down
37 changes: 14 additions & 23 deletions c/tests/test_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,15 @@ verify_branch_general_stat_identity(tsk_treeseq_t *ts)
int ret;
tsk_size_t num_samples = tsk_treeseq_get_num_samples(ts);
double *W = tsk_malloc(num_samples * sizeof(double));
tsk_id_t *stack = tsk_malloc(tsk_treeseq_get_num_nodes(ts) * sizeof(*stack));
tsk_id_t root, u, v;
int stack_top;
tsk_id_t *nodes = tsk_malloc(tsk_treeseq_get_num_nodes(ts) * sizeof(*nodes));
tsk_id_t u;
tsk_size_t num_nodes;
double s, branch_length;
double *sigma = tsk_malloc(tsk_treeseq_get_num_trees(ts) * sizeof(*sigma));
tsk_tree_t tree;
tsk_size_t j;
CU_ASSERT_FATAL(W != NULL);
CU_ASSERT_FATAL(stack != NULL);
CU_ASSERT_FATAL(nodes != NULL);

for (j = 0; j < num_samples; j++) {
W[j] = 1;
Expand All @@ -658,30 +658,21 @@ verify_branch_general_stat_identity(tsk_treeseq_t *ts)
CU_ASSERT_EQUAL(ret, 0);

for (ret = tsk_tree_first(&tree); ret == 1; ret = tsk_tree_next(&tree)) {
ret = tsk_tree_preorder(&tree, -1, nodes, &num_nodes);
CU_ASSERT_EQUAL_FATAL(ret, 0);

s = 0;
for (root = tree.left_root; root != TSK_NULL; root = tree.right_sib[root]) {
stack_top = 0;
stack[stack_top] = root;
while (stack_top >= 0) {
u = stack[stack_top];
stack_top--;

for (v = tree.right_child[u]; v != TSK_NULL; v = tree.left_sib[v]) {
branch_length
= ts->tables->nodes.time[u] - ts->tables->nodes.time[v];
/* printf("u = %d v= %d s = %d bl = %f \n", u, v,
* tree.num_samples[v], */
/* branch_length); */
s += branch_length * (double) tree.num_samples[v];
stack_top++;
stack[stack_top] = v;
}
}
for (j = 0; j < num_nodes; j++) {
u = nodes[j];
ret = tsk_tree_get_branch_length(&tree, u, &branch_length);
CU_ASSERT_EQUAL_FATAL(ret, 0);
s += branch_length * (double) tree.num_samples[u];
}
CU_ASSERT_DOUBLE_EQUAL_FATAL(sigma[tree.index], s, 1e-6);
}
CU_ASSERT_EQUAL_FATAL(ret, 0);
free(stack);

free(nodes);
tsk_tree_free(&tree);
free(W);
free(sigma);
Expand Down
Loading