Obtain a list of trees based on edge ID or node ID #2507
-
Hi, I wonder how I can obtain the list of trees efficiently given a list of edge ID or node ID. I want to take a look at the number of samples of select edges (or children nodes of those edges), using the num_samples function of each tree. Every time I get a new edge, I need to seek the tree based on the edge coordinate, which is not efficient. I wonder if there are alternative ways to do that? Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @Proteios1998 👋, welcome to tskit-dev! I moved this to our Discussion board as I think it's a good question, and could be useful to other folks to see the answer. First, I guess it would be helpful if you could show us some code that does what you want inefficiently, in as simple a way as you can. That'll help us understand exactly what it is you want to do. |
Beta Was this translation helpful? Give feedback.
-
Would something like this do the trick? import numpy as np
# Total number of samples below each mutation on an edge
edge_mutation_samples = np.zeros(ts.num_edges)
# Total number of mutations that fall on each edge
edge_mutations = np.zeros(ts.num_edges)
for tree in ts.trees():
for mut in tree.mutations():
edge_mutations[mut.edge] += 1
edge_mutation_samples[mut.edge] += tree.num_samples(mut.node)
print(edge_mutations)
print(edge_mutation_samples)
keep = edge_mutations > 0
print(edge_mutation_samples[keep] / edge_mutations[keep]) for a little example I made this gives:
|
Beta Was this translation helpful? Give feedback.
Would something like this do the trick?
for a little example I made this gives: