Skip to content

Check mutation parents on tree sequence init #3212

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion c/tests/test_tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -9011,7 +9011,7 @@ test_sort_tables_mutation_times(void)
CU_ASSERT_EQUAL_FATAL(ret, 0);

/* Check to make sure we have legal mutations */
ret = tsk_treeseq_init(&ts, &tables, 0);
ret = tsk_treeseq_init(&ts, &tables, TSK_TS_INIT_COMPUTE_MUTATION_PARENTS);
CU_ASSERT_EQUAL_FATAL(ret, 0);

ret = tsk_treeseq_copy_tables(&ts, &t1, 0);
Expand Down
12 changes: 6 additions & 6 deletions c/tests/test_trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -3886,16 +3886,16 @@ test_simplest_mutation_edges(void)
"1 2 2 0\n";
const char *sites = "0.5 0\n"
"1.5 0\n";
const char *mutations = "0 1 1\n"
const char *mutations = "0 2 1\n"
"0 1 1\n"
"0 0 1\n"
"0 2 1\n"
"1 0 1\n"
"1 2 1\n"
"1 1 1\n"
"1 2 1\n";
"1 0 1\n";
tsk_treeseq_t ts;
tsk_tree_t tree;
/* We have mutations over roots, samples and just isolated nodes */
tsk_id_t mutation_edges[] = { -1, 0, -1, 1, -1, -1 };
tsk_id_t mutation_edges[] = { -1, -1, 0, -1, -1, 1 };
tsk_size_t i, j, k, t;
tsk_mutation_t mut;
tsk_site_t site;
Expand Down Expand Up @@ -4238,7 +4238,7 @@ test_single_tree_bad_mutations(void)
ret = tsk_treeseq_init(&ts, &tables, load_flags);
CU_ASSERT_EQUAL(ret, TSK_ERR_MUTATION_PARENT_AFTER_CHILD);
tsk_treeseq_free(&ts);
tables.mutations.parent[3] = TSK_NULL;
tables.mutations.parent[3] = 2;

/* time < node time */
tables.mutations.time[2] = 0;
Expand Down
18 changes: 17 additions & 1 deletion c/tests/testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ tsk_treeseq_from_text(tsk_treeseq_t *ts, double sequence_length, const char *nod
tsk_table_collection_t tables;
tsk_id_t max_population_id;
tsk_size_t j;
tsk_flags_t ts_flags;
bool all_parents_null;

CU_ASSERT_FATAL(ts != NULL);
CU_ASSERT_FATAL(nodes != NULL);
Expand Down Expand Up @@ -807,7 +809,21 @@ tsk_treeseq_from_text(tsk_treeseq_t *ts, double sequence_length, const char *nod
}
}

ret = tsk_treeseq_init(ts, &tables, TSK_TS_INIT_BUILD_INDEXES);
/* If all mutation.parent are TSK_NULL, use TSK_TS_COMPUTE_MUTATION_PARENTS flag too
*/
ts_flags = TSK_TS_INIT_BUILD_INDEXES;
all_parents_null = true;
for (j = 0; j < tables.mutations.num_rows; j++) {
if (tables.mutations.parent[j] != TSK_NULL) {
all_parents_null = false;
break;
}
}
if (all_parents_null) {
ts_flags |= TSK_TS_INIT_COMPUTE_MUTATION_PARENTS;
}

ret = tsk_treeseq_init(ts, &tables, ts_flags);
/* tsk_treeseq_print_state(ts, stdout); */
if (ret != 0) {
printf("\nret = %s\n", tsk_strerror(ret));
Expand Down
6 changes: 6 additions & 0 deletions c/tskit/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ tsk_strerror_internal(int err)
"(TSK_ERR_DISALLOWED_UNKNOWN_MUTATION_TIME)";
break;

case TSK_ERR_BAD_MUTATION_PARENT:
ret = "A mutation's parent is not consistent with the topology of the tree. "
"Use compute_mutation_parents to set the parents correctly."
"(TSK_ERR_BAD_MUTATION_PARENT)";
break;

/* Migration errors */
case TSK_ERR_UNSORTED_MIGRATIONS:
ret = "Migrations must be sorted by time. (TSK_ERR_UNSORTED_MIGRATIONS)";
Expand Down
6 changes: 6 additions & 0 deletions c/tskit/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,12 @@ Some mutations have TSK_UNKNOWN_TIME in an algorithm where that's
disallowed (use compute_mutation_times?).
*/
#define TSK_ERR_DISALLOWED_UNKNOWN_MUTATION_TIME -510

/**
A mutation's parent was not consistent with the topology of the tree.
*/
#define TSK_ERR_BAD_MUTATION_PARENT -511

/** @} */

/**
Expand Down
Loading
Loading