A polynomial time algorithm for calculating the probability of a ranked gene tree given a species tree

Background The ancestries of genes form gene trees which do not necessarily have the same topology as the species tree due to incomplete lineage sorting. Available algorithms determining the probability of a gene tree given a species tree require exponential computational runtime. Results In this paper, we provide a polynomial time algorithm to calculate the probability of a ranked gene tree topology for a given species tree, where a ranked tree topology is a tree topology with the internal vertices being ordered. The probability of a gene tree topology can thus be calculated in polynomial time if the number of orderings of the internal vertices is a polynomial number. However, the complexity of calculating the probability of a gene tree topology with an exponential number of rankings for a given species tree remains unknown. Conclusions Polynomial algorithms for calculating ranked gene tree probabilities may become useful in developing methodology to infer species trees based on a collection of gene trees, leading to a more accurate reconstruction of ancestral species relationships.


Background
Phylogenetic reconstruction methods aim to infer the species phylogeny which gave rise to a group of extant species. Typically, this species phylogeny is obtained based on genetic data from representative individuals of each extant species. The ancestries of genes at different loci form gene trees which do not necessarily have the same topology as the species tree. Gene tree topologies and species tree topologies might be different due to such phenomena as incomplete lineage sorting, gene duplication, recombination within gene loci, and horizontal gene transfer [1]. In this paper, we focus on incomplete lineage sorting as the mechanism for incongruence of gene tree and species tree topologies, in which two gene lineages do not coalesce in the most recent population ancestral to the individuals from which the genes were sampled. As an example, the lineages sampled from species A and B in  Given a fixed species tree, and assuming the gene tree evolved under the multi-species coalescent [1], the most probable gene tree topology can have a different topology from that of the species tree. Such a gene tree topology is called an anomalous gene tree. In fact, for every species tree topology with at least 5 leaves, we can choose edge lengths in the species tree topology such that anomalous gene trees exist [2]. This implies that the gene tree topology appearing most often when considering different genes might not agree with the species tree topology, thus we cannot use a simple majority-heuristic to infer the species tree from a collection of gene trees. Instead we need statistical tools rather than majority rule heuristics for inferring the species tree based on gene trees.
Current methods for inferring species trees from gene trees in this setting can be divided into topologybased and genealogy-based methods, in which the input for a reconstruction algorithm accepts either gene tree http://www.almob.org/content/7/1/7 a c b d Figure 1 In (a)-(d) the ranked species tree topology is (((A, B) 4 , C) 2 , (D, E) 3 ) 1 . (a) The ranked gene tree matches the ranked species tree. (b) The (ranked or unranked) gene tree does not match the species tree, and there is an incomplete lineage sorting event (a deep coalescence) because the lineages from species A and B fail to coalesce more recently than s 2 . (c) The gene tree and species tree have the same unranked topology but have different ranked topologies, as D and E coalesce in the gene tree more recently than A and B, while A and B is the most recent divergence in the species tree. The gene tree in (c) has ranked topology (((A, B) 3 , C) 2 , (D, E) 4 ) 1 . In (c), there are no incomplete lineage sorting events (no deep coalescences); however, there is an extra lineage at time s 3 which leads to the gene tree and species tree having different rankings. In (c), all coalescences occur in the most recent possible interval consistent with the ranked gene tree, and we have 1 = 2, 2 = 3, 3 = 5, 4 = 5, and g 1 = 2, g 2 = 3, g 3 = 5, g 4 = 5. (d) A gene tree with the same ranked topology as the gene tree in (c) but with coalescences occurring in different intervals.
topologies or genealogies, i.e., gene trees with branch lengths (coalescence times). Topology-based methods include Minimize Deep Coalescence (MDC) [3,4], STAR [5], STELLS [6], rooted triple consensus [7] and other consensus and supertree methods [8,9]. Genealogy-based methods include Bayesian and likelihood methods such as BEST, *BEAST, and STEM [10][11][12] and clustering and distance-based methods [5,[13][14][15]. Possible pros and cons of the two approaches are that topology-based methods can be computationally faster and less sensitive to errors in estimating gene trees (and gene tree branch lengths) from sequence data [16], while methods that use coalescence times, particularly using Bayesian modelling, can be the most accurate when model assumptions are correct [17].
Another possibility that has been so far unexplored in methods for inferring species trees from gene trees is to use ranked gene trees, in which the temporal order of the nodes of the gene tree (the coalescence times) is used, but not the continuous-valued branch lengths. This approach might therefore be intermediate between purely topology-based methods and genealogy-based methods. By preserving more of the temporal information in the gene tree nodes, the hope is to develop methods that are more powerful than purely topology-based methods and that are still computationally efficient and robust to errors in estimating gene trees and gene tree branch lengths from sequence data.
In [18], a first step toward developing methods that use ranked gene trees for inferring species trees was taken by providing formulae to calculate the probability of a ranked gene tree given a species tree. The previous work, however, was based on an exponential enumeration of what were called ranked coalescent histories and did not provide an algorithm for computing some of the key terms in the probability of individual ranked histories. In this http://www.almob.org/content/7/1/7 paper, we improve this previous (computationally inefficient) approach, by providing a method for computing probabilities of ranked gene trees given species trees which is polynomial in the number of leaves using a dynamic programming approach.
Methods for computing probabilities of ranked gene trees efficiently may also be of interest in the context of computing probabilities of unranked gene trees, particularly because no polynomial time algorithm has been found for calculating the probability of a gene tree topology given a species tree under the multispecies coalescent [6,[19][20][21]. The probability of an unranked gene tree topology can be obtained by summing over all ranked gene tree topologies with the same topology. Thus, for unranked gene trees with particular shapes where the number of rankings increases in polynomial time, using ranked gene trees can potentially increase the speed of computing probabilities of unranked gene trees as well. We note that a completely unbalanced gene tree has only one ranking, while the number of rankings can be exponential in the number of leaves when gene trees become more balanced. Thus, our approach for calculating unranked gene tree probabilities will be most useful for less balanced ranked gene trees.
The bulk of the paper consists of the derivation of the polynomial time method for computing ranked gene tree probabilities. The algorithm is summarized in section 'An algorithm' . This is followed by a discussion of applications to computing probabilities of unranked gene tree topologies and to inferring ranked species trees under maximum likelihood and a modification to the MDC criterion.

Calculating the probability of a ranked gene tree topology
In the following, we will derive the probability of a ranked gene tree topology given a species tree, P[ G | T ]. Equations (1,2,3,4,8,10) allow the calculation of P[ G | T ] in time O(n 5 ). The model giving rise to the gene tree is the multi-species coalescent with constant population sizes [1]. Each species consists of a population of constant size where lineages merge according to the coalescent. Thus, lineages from two different species may coalesce any time previous to the split of the two species. We begin with some notation, which is also summarized in Table 1. Let time be 0 today and increasing going into the past. Let T be a species tree with n species, and thus n − 1 speciation events (denoted by 1, . . . , n − 1) occurring at times s 1 > · · · > s n−1 . Denote the interval between speciation event i − 1 and speciation event i by τ i , see Figure 1.
Let G be a ranked gene tree topology. It is convenient to use the same labels for the leaves of G and of T . This is a slight abuse of notation, as leaf A of T refers to a population (or species), and A of G refers to a gene sampled from The number of lineages available for coalescence in population y i,z just after the jth coalescence (considered forward in time) in interval τ i ; k i,0,z is the number of lineages "exiting" at time The set of leaves descended from a node of the species tree or gene tree, respectively lca(u) For a node u of the gene tree, the node y of the species tree with largest rank such that δ(u) ⊂ δ(y) τ (y) For a node y with rank i on the species tree, we denote τ (y) = τ i (the interval immediately above y) The overall coalescence rate in interval τ i immediately preceding (backwards in time) the jth coalescence H k Number of sequences of coalescences above the root of the species tree starting with k lineages f i The joint density of coalescence times in interval τ i population A. We denote the nodes of G (which are coalescence events) by u 1 , . . . , u n−1 , where node u j has rank j, and where higher rank indicates a more recent coalescence. A ranked tree topology can be notated similarly to Newick notation, putting the rank as a subscript for each node, see also Figure 1.
Let G i, i be part of a ranked gene tree evolving on a species tree between time s i and time 0 (i.e. the present). G i, i consists of i gene tree lineages at speciation time s i and the coalescent history of G i, i in time interval (0, s i ) is consistent with the ranked gene tree G . Let g i be the minimum number of lineages required in the ranked gene tree at time s i such that G can be embedded into the species tree T . Note that n ≥ i ≥ g i > i. Next we provide a dynamic programming approach for calculating the probability of a ranked gene tree given a species tree. An efficient way to determine the required quantities g 1 , . . . , g n−1 is provided in Section 'Calculation of g i and k i,j,z ' .
Essentially, in our approach, we traverse the intervals between speciation events going back in time, τ n−1 , . . . , τ 2 http://www.almob.org/content/7/1/7 (formalized in Theorem 2), and calculate the probability of the appropriate coalescent events occurring in interval τ i based on how many coalescent events happened in the later intervals τ i+1 , . . . , τ n−1 (Theorem 3). Finally with Theorem 1, we account for the most ancestral time interval τ 1 . Theorem 1. The probability of a ranked gene tree given a species tree is, where is the probability for the coalescences above the root appearing in the right order [22].

The complexity of calculating
Proof. At the time of the most recent speciation event, s n−1 , we have n lineages with probability 1, which is the initial value of the recursion. Calculating P[ G i, i |T ] for i < n − 1 can be done in the following way, Note that during the interval τ i , we have i branches in the species tree. Let m i be the number of coalescent events in τ i , so Let the number of lineages on branch z just after the jth coalescent event (going forward in time) in τ i be k i,j,z . Calculation of k i,j,z can be done efficiently as shown in Section 'Calculation of g i and k i,j,z ' .
Proof. The density for the coalescence events in interval τ i can be obtained by considering the waiting time to the "next" coalescent event (going backwards in time) as being due to competing exponentials in the different branches, where the coalescence rate within branch z is k i,j,z 2 . Thus, the waiting time until the next coalescent event has rate λ i,j = i z=1 k i,j,z 2 . We denote the time between the jth and (j + 1)st coalescent event as v j , where v 0 is the time between s i−1 and the first (least recent) coalescent event in τ i and with v m i being the time between s i and coalescent event m i .
The density for the coalescent events in the interval τ i is [18], It remains to integrate over v, for which we distinguish between case (i) λ i,0 = 0, and case (ii) λ i,0 > 0.
, all lineages within each population coalesce), then we rewrite f i as, Using the fact that the integral of the numerator of Equation (5) is a hypoexponential distribution based on the sum of m i exponential random variables [23] (with density functions λ i,j e −λ i,j v j , j = 1, . . . , m i ), the probability of the coalescent events in the interval is the cumulative http://www.almob.org/content/7/1/7 distribution function of the hypoexponential distribution evaluated at where the second line follows because −λ For integrating f i , we use the fact that the integral of the numerator in Equation (7) is the convolution of m i + 1 exponential random variables with parameters λ i,0 , . . . , λ i,m i , which is the hypoexponential distribution. Now, since λ i,j < λ i,j+1 , we observe, using the probability density function of the hypoexponential distribution, , which is the same expression as for the λ i,0 = 0 case (6). Note that for case (i) we made use of the cumulative distribution function of the hypoexponential distribution, while for case (ii) we made use of the density function of the hypoexponential distribution. Both cases yield the same final expression for P[ G i−1, i−1 |G i, i , T ], which establishes the proof.
We note that the terms P[ G i−1, i−1 |G i, i , T ] are analogous to the functions g i,j defined in [24,25], which give the probability that i lineages coalesce into j within time t in a single population and are used extensively in computing probabilities related to unranked gene trees [6,19,26,27]. In particular, if only one population, say z * , has coalescence events, then we have r , a product of g i,j functions with the denominator counting the number of sequences in which m i coalescences could have occurred. The terms P[ G i−1, i−1 |G i, i , T ] allow for the coalescences to occur in separate populations, however, and are constrained by the ranking of the gene tree. For example, in interval τ 3 of Figure 1c, there are two coalescences which occur in different populations. If the ranking of the gene tree were not important, the branches could be considered independent, and the probability of this event would be g 2,1 (s 2 − s 3 )g 2,1 (s 2 − s 3 ). However, the gene tree http://www.almob.org/content/7/1/7 ranking constrains the coalescence of A and B to be less recent than that of D and E, so the probability for events in this interval is, r We illustrate that we get the same result from Theorem 3: there are two coalescence events in interval τ 3 , so we use j = 0, 1, 2, and calculate Thus, Equation (4) from Theorem 3 evaluates to

Remark 6.
The probability of a gene tree topology is the sum of the probabilities of each ranked gene tree with the given topology. A given tree topology has (n − 1)! / n−1 i=1 (c i − 1) rankings, where c i is the number of descendant leaves of interior vertex i. A proof can be found in [28]. For a completely balanced tree on n = 2 k leaves, the number of rankings grows faster than polynomial: the numerator can be approximated by, n! ≈ √ 2πn(n/e) n , and the denominator can be approximated by, showing that the ratio grows faster than polynomial in n.

Calculation of g i and k i,j,z Calculation of g i
If T and G have the same ranked topology, then g i = i + 1.
In general, to compute g i , we let lca(u j ) be the least common ancestor node on the species tree for a node u j on the ranked gene tree -i.e., the node with the largest rank on the species tree which is ancestral to all species represented in u j . For a node y on the species tree, let τ (y) be the interval immediately above y. For example, in Figure 1c, τ (lca(u 4 )) = τ 3 where u 4 is the gene tree node with rank 4 -the node ancestral to D and E only. In order to compute g i , we count the number of gene tree nodes which may occur closer to the present than s i . These are precisely all gene tree nodes u j where lca(u j ) is in any of the intervals τ i+1 , . . . , τ n−1 . Since at the present, n lineages are able to coalesce, we can express g i as,

Calculation of k i,j,z
We let y i,j be the jth population (read left to right) in interval τ i (equivalently, the jth branch or jth node subtending the branch). In order to label every population before and after a speciation time s i uniquely, extra nodes can be added to the species tree to form a beaded species tree (Figure 2), so that there are i nodes at time s i , i = 1, . . . , n − 1. For each i ∈ {1, . . . , n − 1}, there is one node of outdegree 2, and i − 1 nodes of outdegree 1. Thus, population y i,j corresponds to a branch (equivalently, a node) in the beaded species tree. We denote the outdegree of a node y by outdeg(y).
In the remainder of this section, we compute the values k i,j,z , i.e. the number of lineages on branch y i,z of the beaded species tree during the interval immediately after the jth coalescence event (going forward in time), with The value of k i,j,z depends on the number of lineages entering branch i, i , as well as the number of lineages exiting the branch, and not just on the number of coalescence events in the interval. For example, in Figure 1c, k 2,0,1 = 1 and k 2,1,1 = 2, while in Figure 1d, k 2,0,1 = 2 and k 2,1,1 = 3, although the two gene trees have the same ranked topology and m 2 = 1 for both cases.
To determine the terms k i,j,z we note that the number of coalescences that have occurred more recently than interval τ i is n − i . In a given interval τ i , we let z (1) and z (2) be the left and right children, respectively, of population z of outdegree 2, and let z (1) = z (2) be the only child of a node z of outdegree 1.
The number of lineages available to coalesce in population z of interval τ i is where the z (j) are the daughter populations (one or two) of z. Further, k n,0,z = 0 for all z. Since the beaded species tree has n 2 /2 nodes, precalculating outdeg(y i,z ) requires O(n 2 ). For 0 ≤ j < m i , we have Consequently, determining a particular k i,j,z is O(1). Thus determining k i,j,z for all possible i, m i and i is (see also Corollary 4), Note that taking the sum over all z is not necessary, as in all but one branch the k i,j,z equals the k i,j+1,z .

An algorithm
In summary, we derived an algorithm with runtime O(n 5 ) for calculating the probability of a ranked gene tree given a species tree on n tips:

Conclusions
In this paper, we provide a polynomial-time algorithm (O(n 5 ) where n is the number of species) to calculate the probability of a ranked gene tree topology given a species tree, summarized in Section 'An algorithm' . We now discuss applying these results to computing probabilities of unranked gene tree topologies and to inferring ranked species trees.

Computing probabilities of unranked gene tree topologies
Previous work on computing probabilities of unranked gene tree topologies used the concept of coalescent histories, which specify the branches in the species tree in which each node of the gene tree occurs. An unranked gene tree probability can then be computed by enumerating all coalescent histories and computing the probability of each. The number of coalescent histories grows at least exponentially when the (unranked) gene tree matches the species tree, making this approach computationally intensive. Coalescent histories can be enumerated either recursively (e.g., in PHYLONET [31] or [20]) or nonrecursively (COAL [19]).
A much faster approach using dynamic programming similar to that used in this paper is implemented in STELLS [6], which conditions on the ancestral configuration in each branch rather than the number of lineages.
Here an ancestral configuration keeps track not only of the number of lineages in a branch in the species tree, but also the particular nodes of the gene tree. Different ancestral configurations can potentially have the same number of lineages within a population. Enumerating ancestral configurations turns out to have exponential running time for arbitrarily shaped trees, but the number of ancestral configurations is still much smaller than the number of coalescent histories. When computing probabilities of ranked gene tree topologies, however, the ranking specifies the sequence of coalescence events, leading to a unique ancestral configuration given the number of lineages in a time interval. This fortuitously enables probabilities of ranked gene tree topologies to be computed in polynomial time.
We note that although the number of rankings for a gene tree is not polynomial in the number of leaves in general, the number of rankings can be small for certain tree shapes. For example, if the gene tree has a caterpillar shape, in which each internal node has a leaf as a descendant, then there is only one ranking, and thus computing the ranked and unranked gene tree are equivalent. For http://www.almob.org/content/7/1/7 a pseudo-caterpillar, a tree made by replacing the subtree with four leaves of a caterpillar with a balanced tree on four leaves [20], there are only two rankings possible, and for a bicaterpillar [20], for which the left subtree is a caterpillar with n L leaves and the right subtree is a caterpillar with n − n L leaves, there are n−2 n L −1 rankings. Thus computing unranked gene tree probabilities by summing ranked gene tree probabilities can be done in polynomial time for some tree shapes. We note that for the approach used by STELLS, some tree shapes can also be computed in polynomial time, including the cases we mentioned with a polynomial number of rankings (caterpillar and pseudo-caterpillar). An open question is whether there are any classes of unranked gene trees which have a polynomial number of rankings but an exponential number of ancestral configurations, or vice versa.

Inferring species trees from ranked gene trees
Our fast calculation of the probability of ranked gene tree topologies can be used to determine the maximum likelihood species tree from a collection of known gene trees. Assume we have observed N ranked gene trees (i.e., N loci). Now the maximum likelihood species tree T ML (with branch lengths on internal branches) is is a multinomial likelihood. Here P[ G k |T ] can be determined with our polynomial-time algorithm, we let G (i) denote the ith ranked topology, and n i is the number of times ranked topology i is observed, with H n i=1 n i = N. Note in particular that the ranked topology of T ML might differ from the most frequent ranked gene tree topology [18].
Our derivation of the ranked gene tree probability also suggests a way to infer a ranked species tree topology from ranked gene tree topologies with a similar flavor as the MDC criterion. In MDC, for an input gene tree and candidate species tree, the number of extra lineages (lineages which necessarily fail to coalesce due to topological differences between gene and species trees) on each edge of the species tree is counted. For MDC, whether the edge of the species tree is long or short does not affect the deep coalescence cost. In working with ranked gene trees, however, we can keep track of the minimum number of extra lineages within each time interval τ i . The total number of extra lineages in this sense is Minimizing (12) as a criterion for the ranked species tree will tend to penalize long edges of the species tree which have multiple lineages persisting through multiple species divergence events. As an example, in Figure 1b, the gene tree has a MDC cost of 1 since there are two lineages exiting the population immediately ancestral to A and B; however the cost according (12) is 2 because there are two edges on the beaded version of the species tree ( Figure 2) that each have an extra lineage. In Figure 1c, the gene tree has a MDC cost of 0 for the species tree since it has the matching unranked topology; however, the number of extra lineages from equation (12) is 1. We note that in Figure 1c, interval τ 3 , incomplete lineage sorting (and deep coalescence) have not occurred as these concepts are normally used. To capture the idea that coalescence has nevertheless occurred in a more ancient time interval than allowed, we might refer to the coalescence of A and B in Figure 1c as an "ancient lineage sorting" event (rather than incomplete lineage sorting event) or an ancient coalescence rather than a deep coalescence. We could therefore refer to minimizing equation (12) as the Minimize Ancient Coalescence (MAC) criterion, which would provide an interesting comparison to the usual topology-based MDC criterion.
In practice, a method of inferring a species tree from ranked gene trees would require estimating the ranked gene trees. This would require clock-like gene trees, or trees with times estimated for nodes, which can also be inferred under relaxed clock models in BEAST [32]. To account for the uncertainty in the gene trees, the counts for different ranked gene trees could be weighted by their posterior probabilities obtained from Bayesian estimation of the gene trees [33]. Thus, in equation (11), we would let n ik be the posterior probability of ranked topology i at locus k, and use n i = H n k=1 n ik as the estimated number of times that ranked topology i was observed. Similarly, for equation (12), the coalescence cost at a locus could be distributed over multiple topologies weighted by their posterior probabilities.