You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In #668 we explored the possibility of associating an edge ID with a mutation instead of the most recent node. We decided that this would not be a good idea.
It would still be useful to be able to do this, though. It seems like it should be possible to do this using the existing indexes, which are defined as follows:
A mutation is at a site, which has a given position. Can we find the edges that intersect with a given position efficiently using these indexes, or do we need a different index?
(This is basically the same question as efficiently seeking to a given tree, #684)
Isn't this one a bit different from #684 because we know that we're only looking for a subset of nodes? A mutation on an edge is above the child node of that edge, etc.. So, given the mutation's position and node, we should be able to find the entries in each index that comprise the edge? That still doesn't give you the index to the edge table row, but that only matters if you want the metadata.
I don't think that helps @molpopgen - think about the long edge example. We have an edge spanning (0, L) large tree sequence with a crap ton of edges. We have a mutation at position L/2 - finding the edges that start and end at L / 2 is of no help in finding the long edge. You'd have to do a linear sweep in worst case.
And then try to find them with binary search to do an "equal range" kinda search with a linear follow up:
#include<cstddef>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<tuple>// for std::tie
#include<tskit.h>intmain(int argc, char **argv)
{
tsk_table_collection_t tables;
tsk_table_collection_init(&tables, 0);
tsk_table_collection_load(&tables, "treefile.trees", 0);
// NOTE: I am breaking the indexes now.std::sort(tables.indexes.edge_insertion_order,
tables.indexes.edge_insertion_order + tables.edges.num_rows,
[&tables](auto i, auto j) {
returnstd::tie(tables.edges.child[i], tables.edges.left[i])
< std::tie(tables.edges.child[j], tables.edges.left[j]);
});
auto b = tables.indexes.edge_insertion_order;
auto e = b + tables.edges.num_rows;
for (int i = 0; i < tables.mutations.num_rows; ++i) {
auto mnode = tables.mutations.node[i];
auto mnode_time = tables.nodes.time[mnode];
auto mpos = tables.sites.position[tables.mutations.site[i]];
auto l = std::lower_bound(b, e, mnode, [&tables](int i, int j) {
return tables.edges.child[i] < j;
});
auto u = std::upper_bound(l, e, mnode, [&tables](int i, int j) {
return i < tables.edges.child[j];
});
for (auto j = l; j < u; ++j) {
auto c = tables.edges.child[*j];
auto l = tables.edges.left[*j];
auto r = tables.edges.right[*j];
if (mpos >= l && mpos < r) {
std::cout << mnode << '' << mnode_time << '' << mpos << " | " << *j
<< '' << c << '' << '' << l << ''
<< r << '\n';
}
}
}
}
We get the same results modulo rounding issues in the default prints.
I'd forgotten that the tskit indexes were just arrays of indexes. That's not true in fwdpp, so withtskit, you can get back to edge metadata or whatnot.
The second linear search within [l, u) can also be done with a binary search, but I omitted it to keep it short.
I see, so if we had another index where we sort by (child, left), we can find edge in log time because we can search for the node first. Then, as a node cannot be a child on two overlapping intervals, we can also avoid the general interval search case on the subset. That's clever!
I wonder if it's worth the extra index to support this query though, when we'll need the more general interval overlap query at some point for #684 anyway?
I think that the more general solution is preferable, although this is a useful method to know about. It can be improved in a few ways, I think, too. For example, mapping unique mutation node to mutation row indexes would cut down the number of binary searches.
There is a straightforward way to derive this information during the standard tree algorithm:
defalgorithm_T(ts):
sequence_length=ts.sequence_lengthedges=list(ts.edges())
M=len(edges)
in_order=ts.tables.indexes.edge_insertion_orderout_order=ts.tables.indexes.edge_removal_ordersites=ts.tables.sitesmutations=ts.tables.mutationsmutation_edge=np.zeros_like(ts.tables.mutations.node) -1parent=np.zeros(ts.num_nodes, dtype=int) -1# Map the child node to the ID of its edge in the current tree.node_edge_map=np.zeros(ts.num_nodes, dtype=int) -1j=0k=0left=0site_id=0mutation_id=0whilej<Morleft<sequence_length:
whilek<Mandedges[out_order[k]].right==left:
edge=edges[out_order[k]]
parent[edge.child] =-1node_edge_map[edge.child] =-1k+=1whilej<Mandedges[in_order[j]].left==left:
edge=edges[in_order[j]]
node_edge_map[edge.child] =in_order[j]
parent[edge.child] =edge.parentj+=1right=sequence_lengthifj<M:
right=min(right, edges[in_order[j]].left)
ifk<M:
right=min(right, edges[out_order[k]].right)
whilesite_id<ts.num_sitesandsites.position[site_id] <right:
assertsites.position[site_id] >=leftwhile (
mutation_id<ts.num_mutationsandmutations.site[mutation_id] ==site_id
):
mutation_edge[mutation_id] =node_edge_map[mutations.node[mutation_id]]
mutation_id+=1site_id+=1yield (left, right), parent, mutation_edgeleft=right
Basically, we maintain a map of child node -> edge ID for each tree, and then use this map to fill out the mutation_edge map from mutation ID to edge id.
I suggest we add a edge attribute to the tsk_mutation_t struct, and fill this information out during the initialisation step of treeseq_t. Specifically, we'd update the tsk_treeseq_init_trees function to perform this logic, and store the edge ID for each mutation there. It requires a bit of extra memory during startup, but I think it's well worth it as this is a recurring headache.
We would be careful to make sure that any mutations that are not on edges (i.e. above roots or just not on tree nodes) are assigned -1.
Activity
molpopgen commentedon Jun 18, 2020
Isn't this one a bit different from #684 because we know that we're only looking for a subset of nodes? A mutation on an edge is above the child node of that edge, etc.. So, given the mutation's position and node, we should be able to find the entries in each index that comprise the edge? That still doesn't give you the index to the edge table row, but that only matters if you want the metadata.
jeromekelleher commentedon Jun 18, 2020
I don't think that helps @molpopgen - think about the long edge example. We have an edge spanning (0, L) large tree sequence with a crap ton of edges. We have a mutation at position L/2 - finding the edges that start and end at L / 2 is of no help in finding the long edge. You'd have to do a linear sweep in worst case.
molpopgen commentedon Jun 18, 2020
It looks like one needs a different sorting. Take this simulation:
Find the edges via brute force:
And then try to find them with binary search to do an "equal range" kinda search with a linear follow up:
We get the same results modulo rounding issues in the default prints.
Python:
And the C++:
Quick comments:
tskitindexes were just arrays of indexes. That's not true infwdpp, so withtskit, you can get back to edge metadata or whatnot.[l, u)can also be done with a binary search, but I omitted it to keep it short.For posterity, the
Makefile:jeromekelleher commentedon Jun 19, 2020
I see, so if we had another index where we sort by (child, left), we can find edge in log time because we can search for the node first. Then, as a node cannot be a child on two overlapping intervals, we can also avoid the general interval search case on the subset. That's clever!
I wonder if it's worth the extra index to support this query though, when we'll need the more general interval overlap query at some point for #684 anyway?
molpopgen commentedon Jun 19, 2020
I think that the more general solution is preferable, although this is a useful method to know about. It can be improved in a few ways, I think, too. For example, mapping unique mutation node to mutation row indexes would cut down the number of binary searches.
jeromekelleher commentedon May 17, 2022
There is a straightforward way to derive this information during the standard tree algorithm:
Basically, we maintain a map of child node -> edge ID for each tree, and then use this map to fill out the
mutation_edgemap from mutation ID to edge id.I suggest we add a
edgeattribute to thetsk_mutation_tstruct, and fill this information out during the initialisation step oftreeseq_t. Specifically, we'd update the tsk_treeseq_init_trees function to perform this logic, and store the edge ID for each mutation there. It requires a bit of extra memory during startup, but I think it's well worth it as this is a recurring headache.We would be careful to make sure that any mutations that are not on edges (i.e. above roots or just not on tree nodes) are assigned -1.
Any thoughts?
molpopgen commentedon May 17, 2022
Seems straightforward!
benjeffery commentedon May 17, 2022
Very nice. Much simpler than going via the random-access-tree that was discussed before.
Add mutation.edge for tsk_treeseq accessors.
Add mutation.edge for tsk_treeseq accessors.
Add mutation.edge for tsk_treeseq accessors.
Add mutation.edge for tsk_treeseq accessors.
Add mutation.edge for tsk_treeseq accessors.