You should definitely avoid revisiting the same edges multiple times. If
all you want is min/max age *values* (as opposed to the nodes withs
those values), then just call `tree.internal_node_ages()`. This returns
a (sorted) list of internal node ages, where "age" is defined as sum of
edge lengths from the tips. I am assuming that you want to exclude tip
nodes (age == 0), and that's why I suggested "tree.internal_node_ages()"
as opposed to just "tree.node_ages()". You also probably want to exclude
to root age:
node_ages = tree.internal_node_ages()
n2 = [a for a in node_ages if a < tree.seed_node.age]
min_age = min(n2)
max_age = max(n2)
(NOTE: I have not actually run the above fragment, and just wrote it off
the the top of my head, so it may need tweaking to work).
Things get a little more tricky if you actually want the nodes
associated with those ages. You can just make a second pass through the
tree to find them (via the age attribute), but if efficiency is a
concern, you almost definitely want to roll your own custom age
calculating function based on `tree.calc_node_ages()` that gets you the
nodes directly.
On 12/9/14, 6:20 PM, Yan Wong wrote:
>
> Thanks. I was wondering if there was a measure built-in to the tree
> metadata, or a routine that did it for me without having to iterate
> through nodes and so revisit the same edges multiple times? I
> guess calc_node_ages() is the thing I need to calculate values over the
> whole tree, even though I only need to store the max and min lengths.
> I'm probably just being overly worried about optimisation, but then I am
> expecting to deal with trees with > 100,000 nodes.