Skip to content
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

test for time with keep_roots #823

Merged
merged 1 commit into from Sep 1, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion c/tskit/tables.c
Expand Up @@ -6458,7 +6458,8 @@ simplifier_insert_input_roots(simplifier_t *self)
if (ret != 0) {
goto out;
}
simplifier_map_mutations(self, input_id, x->left, x->right, x->node);
simplifier_map_mutations(
self, input_id, x->left, x->right, output_id);
}
x = x->next;
}
Expand Down
2 changes: 1 addition & 1 deletion python/tests/simplify.py
Expand Up @@ -482,7 +482,7 @@ def insert_input_roots(self):
while x is not None:
if x.node != output_id:
self.record_edge(x.left, x.right, output_id, x.node)
self.map_mutations(x.left, x.right, input_id, x.node)
self.map_mutations(x.left, x.right, input_id, output_id)
x = x.next
self.flush_edges()
root_time = self.tables.nodes.time[output_id]
Expand Down
44 changes: 29 additions & 15 deletions python/tests/test_topology.py
Expand Up @@ -3223,24 +3223,27 @@ def test_single_binary_tree_keep_roots_mutations(self):
keep_input_roots=True,
)

def test_map_mutations_with_and_without_roots(self):
def test_place_mutations_with_and_without_roots(self):
nodes_before = """\
id is_sample time
0 1 0
1 0 1
2 0 2
"""
edges_before = """\
left right parent child
0 2 1 0
0 2 2 1
"""
sites = """\
id position ancestral_state
0 1.0 0
"""
mutations_before = """\
site node derived_state
0 0 2
0 1 1
site node derived_state time
0 0 2 0
0 1 1 1
0 2 3 2
"""
# expected result without keep_input_roots
nodes_after = """\
Expand All @@ -3251,20 +3254,26 @@ def test_map_mutations_with_and_without_roots(self):
left right parent child
"""
mutations_after = """\
site node derived_state
0 0 2
0 0 1
site node derived_state time
0 0 2 0
0 0 1 1
0 0 3 2
"""
# expected result with keep_input_roots
nodes_after_keep = nodes_before
nodes_after_keep = """\
id is_sample time
0 1 0
1 0 2
"""
edges_after_keep = """\
left right parent child
0 2 1 0
"""
mutations_after_keep = """\
site node derived_state
0 0 2
0 0 1
site node derived_state time
0 0 2 0
0 0 1 1
0 1 3 2
"""
self.verify_simplify(
samples=[0],
Expand Down Expand Up @@ -5737,7 +5746,8 @@ def verify_keep_input_roots(self, ts, samples):
self.assertEqual(tree_with_roots.children(root), (mrca,))

# Any mutations that were on the path from the old MRCA
# to the root should be mapped to this node.
# to the root should be mapped to this node, and any mutations
# above the root should still be there.
u = new_to_input_map[mrca]
root_path = []
while u != tskit.NULL:
Expand Down Expand Up @@ -5767,8 +5777,11 @@ def verify_keep_input_roots(self, ts, samples):
input_site = input_sites[position]
for input_mutation in input_site.mutations:
if input_mutation.node in root_path:
# The same mutation should exist and be mapped to the
# mrca
new_node = (
mrca if input_mutation.node != input_root else root
)
# The same mutation should exist and be mapped to
# new_node
new_mutation = new_mutations[input_mutation.metadata]
# We have turned filter sites off, so sites should
# be comparable
Expand All @@ -5777,7 +5790,8 @@ def verify_keep_input_roots(self, ts, samples):
new_mutation.derived_state,
input_mutation.derived_state,
)
self.assertEqual(new_mutation.node, mrca)
self.assertEqual(new_mutation.node, new_node)

return ts_with_roots

def test_many_trees(self):
Expand Down
31 changes: 15 additions & 16 deletions python/tests/tsutil.py
Expand Up @@ -111,27 +111,26 @@ def insert_branch_mutations(ts, mutations_per_branch=1):
for tree in ts.trees():
site = tables.sites.add_row(position=tree.interval[0], ancestral_state="0")
for root in tree.roots:
state = {root: 0}
mutation = {root: -1}
state = {tskit.NULL: 0}
mutation = {tskit.NULL: -1}
stack = [root]
while len(stack) > 0:
u = stack.pop()
stack.extend(tree.children(u))
v = tree.parent(u)
if v != tskit.NULL:
state[u] = state[v]
parent = mutation[v]
for _ in range(mutations_per_branch):
state[u] = (state[u] + 1) % 2
metadata = f"{len(tables.mutations)}".encode()
mutation[u] = tables.mutations.add_row(
site=site,
node=u,
derived_state=str(state[u]),
parent=parent,
metadata=metadata,
)
parent = mutation[u]
state[u] = state[v]
parent = mutation[v]
for _ in range(mutations_per_branch):
state[u] = (state[u] + 1) % 2
metadata = f"{len(tables.mutations)}".encode()
mutation[u] = tables.mutations.add_row(
site=site,
node=u,
derived_state=str(state[u]),
parent=parent,
metadata=metadata,
)
parent = mutation[u]
add_provenance(tables.provenances, "insert_branch_mutations")
return tables.tree_sequence()

Expand Down