question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method to 'prune back' a tree sequence

See original GitHub issue

I want a function that will remove the topology after (forwards in time) a specific time, and mark each extant lineage with as a sample. The intuition is that we’re drawing a line accross the tees, and sort of “pruning back” to this line. (This will allow us to generate the haplotypes extant at this time.)

Here’s a simple implementation:

def prune_leaves(ts, time):
    """
    Returns a tree sequence in which all the topology and mutation
    information after the specified time is remove. That is, the
    samples in the returned tree sequence will be the lineages
    extant at the specified time.
    """
    tables = ts.dump_tables()
    t = tables.nodes.time
        
    tables.edges.clear()
    samples = []
    edge_map = {}
    for edge in ts.edges():
        if t[edge.child] > time:
            tables.edges.add_row(edge.left, edge.right, edge.parent, edge.child)
        elif t[edge.child] <= time and t[edge.parent] > time:
            key = (edge.child, edge.parent)
            if key in edge_map:
                # If the same parent/child combination exists, then we should map
                # to the same ancestor node.
                u = edge_map[key]
            else:
                u = tables.nodes.add_row(
                    flags=tskit.NODE_IS_SAMPLE, time=time)
                samples.append(u)
                edge_map[key] = u
            tables.edges.add_row(edge.left, edge.right, edge.parent, u)
    tables.sort()
    tables.simplify(samples)
    return tables.tree_sequence()

There’s probably some subtleties and corner case I’ve missed out here, but this probably the basic idea.

I think this would be a useful addition to tskit. I don’t know what we’d call it though, and I think it’s worth keeping this in mind with the discussion going on over in #261 where we’re chopping up the tree sequence in the space rather than time direction.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:17 (17 by maintainers)

github_iconTop GitHub Comments

2reactions
molpopgencommented, Aug 1, 2019

If removing all nodes ancestral to a certain time is decapitating, then this would be to amputate or dismember the tree:

import tskit.gory_ops
1reaction
jeromekellehercommented, May 4, 2022

Yep, agreed. That’s basically what I’ve done in #2240

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Prune a Tree: 13 Steps (with Pictures) - wikiHow
1. Think about why you want to prune. Are you trying to shape a tree for shade or height? Has the tree been...
Read more >
A Complete Guide to Pruning and Trimming Trees
Crown reduction is a tree pruning method generally used on older, more mature trees. It can help strengthen the tree and encourage new...
Read more >
Pruning trees and shrubs | UMN Extension
At planting, remove only diseased, dead or broken branches. Begin training a plant during the dormant season following planting. Prune to shape young...
Read more >
Pruning in Three Steps, a Pictorial - Gardening Solutions
The three-cut pruning method is a great technique to make sure your pruning cuts are clean and where you want them. A small...
Read more >
Pruning 101: A Guide to Pruning Trees and Shrubs
How to Prune Trees and Shrubs · The first cut should be made underneath the branch, about 6 to 12 inches away from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found